aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/game/server/pluginvariant.h
blob: b4a798c7cddc3e94f0734bc337d3aa92d6295772 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef PLUGINVARIANT_H
#define PLUGINVARIANT_H
#ifdef _WIN32
#pragma once
#endif

#include "stdstring.h"
#include "mathlib/vmatrix.h"

/*
//Tony; including stdstring at this point (which I need) messes up the offsetof override in linux, so I need to reset it here.
#if defined( POSIX )
#undef offsetof
#define offsetof(s,m)   (size_t)&(((s *)0)->m)
#endif
 */
//
// A modified variant class for that functions almost identically to variant_t, for plugins to pass data back and forth.
//
class pluginvariant
{
	union
	{
		bool bVal;
		int iVal;
		float flVal;
		float vecVal[3];
		color32 rgbaVal;
	};
	//Tony; neither of these can be in the union because of constructors.
	edict_t *eVal;
	char iszVal[1024];

	fieldtype_t fieldType;

public:

	// constructor
	pluginvariant() : fieldType(FIELD_VOID), iVal(0) {}

	inline bool Bool( void ) const						{ return( fieldType == FIELD_BOOLEAN ) ? bVal : false; }
	inline const char *String( void ) const				{ return( ToString() );  }
	inline int Int( void ) const						{ return( fieldType == FIELD_INTEGER ) ? iVal : 0; }
	inline float Float( void ) const					{ return( fieldType == FIELD_FLOAT ) ? flVal : 0; }
	inline const edict_t *Edict(void) const;
	inline color32 Color32(void) const					{ return rgbaVal; }
	inline void Vector3D(Vector &vec) const;

	fieldtype_t FieldType( void ) { return fieldType; }

	void SetBool( bool b ) { bVal = b; fieldType = FIELD_BOOLEAN; }
	void SetString( char *str ) { Q_snprintf(iszVal, 1024, "%s", str); fieldType = FIELD_STRING; }
	void SetInt( int val ) { iVal = val, fieldType = FIELD_INTEGER; }
	void SetFloat( float val ) { flVal = val, fieldType = FIELD_FLOAT; }
	void SetEdict( edict_t *val ) { eVal = val; fieldType = FIELD_EHANDLE; }
	void SetVector3D( const Vector &val ) { vecVal[0] = val[0]; vecVal[1] = val[1]; vecVal[2] = val[2]; fieldType = FIELD_VECTOR; }
	void SetPositionVector3D( const Vector &val ) { vecVal[0] = val[0]; vecVal[1] = val[1]; vecVal[2] = val[2]; fieldType = FIELD_POSITION_VECTOR; }
	void SetColor32( color32 val ) { rgbaVal = val; fieldType = FIELD_COLOR32; }
	void SetColor32( int r, int g, int b, int a ) { rgbaVal.r = r; rgbaVal.g = g; rgbaVal.b = b; rgbaVal.a = a; fieldType = FIELD_COLOR32; }

protected:

	//
	// Returns a string representation of the value without modifying the variant.
	//
	const char *ToString( void ) const
	{
		static char szBuf[512];

		switch (fieldType)
		{
		case FIELD_STRING:
			{
				return (const char *)iszVal;
			}

		case FIELD_BOOLEAN:
			{
				if (bVal == 0)
				{
					Q_strncpy(szBuf, "false",sizeof(szBuf));
				}
				else
				{
					Q_strncpy(szBuf, "true",sizeof(szBuf));
				}
				return(szBuf);
			}

		case FIELD_INTEGER:
			{
				Q_snprintf( szBuf, sizeof( szBuf ), "%i", iVal );
				return(szBuf);
			}

		case FIELD_FLOAT:
			{
				Q_snprintf(szBuf,sizeof(szBuf), "%g", flVal);
				return(szBuf);
			}

		case FIELD_COLOR32:
			{
				Q_snprintf(szBuf,sizeof(szBuf), "%d %d %d %d", (int)rgbaVal.r, (int)rgbaVal.g, (int)rgbaVal.b, (int)rgbaVal.a);
				return(szBuf);
			}

		case FIELD_VECTOR:
			{
				Q_snprintf(szBuf,sizeof(szBuf), "[%g %g %g]", (double)vecVal[0], (double)vecVal[1], (double)vecVal[2]);
				return(szBuf);
			}

		case FIELD_VOID:
			{
				szBuf[0] = '\0';
				return(szBuf);
			}
		}

		return("No conversion to string");
	}
};


////////////////////////// pluginvariant implementation //////////////////////////


//-----------------------------------------------------------------------------
// Purpose: Returns this variant as a vector.
//-----------------------------------------------------------------------------
inline void pluginvariant::Vector3D(Vector &vec) const
{
	if (( fieldType == FIELD_VECTOR ) || ( fieldType == FIELD_POSITION_VECTOR ))
	{
		vec[0] =  vecVal[0];
		vec[1] =  vecVal[1];
		vec[2] =  vecVal[2];
	}
	else
	{
		vec = vec3_origin;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Returns this variant as an edict_t
//-----------------------------------------------------------------------------
inline const edict_t *pluginvariant::Edict(void) const
{
	if ( fieldType == FIELD_EHANDLE )
		return eVal;

	return NULL;
}


#endif // pluginvariant_H