Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.1 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PLUGINVARIANT_H
  8. #define PLUGINVARIANT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "stdstring.h"
  13. #include "mathlib/vmatrix.h"
  14. //
  15. // A modified variant class for that functions almost identically to variant_t, for plugins to pass data back and forth.
  16. //
  17. class pluginvariant
  18. {
  19. union
  20. {
  21. bool bVal;
  22. int iVal;
  23. float flVal;
  24. float vecVal[3];
  25. color32 rgbaVal;
  26. };
  27. //Tony; neither of these can be in the union because of constructors.
  28. edict_t *eVal;
  29. char iszVal[1024];
  30. fieldtype_t fieldType;
  31. public:
  32. // constructor
  33. pluginvariant() : fieldType(FIELD_VOID), iVal(0) {}
  34. inline bool Bool( void ) const { return( fieldType == FIELD_BOOLEAN ) ? bVal : false; }
  35. inline const char *String( void ) const { return( ToString() ); }
  36. inline int Int( void ) const { return( fieldType == FIELD_INTEGER ) ? iVal : 0; }
  37. inline float Float( void ) const { return( fieldType == FIELD_FLOAT ) ? flVal : 0; }
  38. inline const edict_t *Edict(void) const;
  39. inline color32 Color32(void) const { return rgbaVal; }
  40. inline void Vector3D(Vector &vec) const;
  41. fieldtype_t FieldType( void ) { return fieldType; }
  42. void SetBool( bool b ) { bVal = b; fieldType = FIELD_BOOLEAN; }
  43. void SetString( char *str ) { Q_snprintf(iszVal, 1024, str); fieldType = FIELD_STRING; }
  44. void SetInt( int val ) { iVal = val, fieldType = FIELD_INTEGER; }
  45. void SetFloat( float val ) { flVal = val, fieldType = FIELD_FLOAT; }
  46. void SetEdict( edict_t *val ) { eVal = val; fieldType = FIELD_EHANDLE; }
  47. void SetVector3D( const Vector &val ) { vecVal[0] = val[0]; vecVal[1] = val[1]; vecVal[2] = val[2]; fieldType = FIELD_VECTOR; }
  48. void SetPositionVector3D( const Vector &val ) { vecVal[0] = val[0]; vecVal[1] = val[1]; vecVal[2] = val[2]; fieldType = FIELD_POSITION_VECTOR; }
  49. void SetColor32( color32 val ) { rgbaVal = val; fieldType = FIELD_COLOR32; }
  50. 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; }
  51. protected:
  52. //
  53. // Returns a string representation of the value without modifying the variant.
  54. //
  55. const char *ToString( void ) const
  56. {
  57. static char szBuf[512];
  58. switch (fieldType)
  59. {
  60. case FIELD_STRING:
  61. {
  62. return (const char *)iszVal;
  63. }
  64. case FIELD_BOOLEAN:
  65. {
  66. if (bVal == 0)
  67. {
  68. Q_strncpy(szBuf, "false",sizeof(szBuf));
  69. }
  70. else
  71. {
  72. Q_strncpy(szBuf, "true",sizeof(szBuf));
  73. }
  74. return(szBuf);
  75. }
  76. case FIELD_INTEGER:
  77. {
  78. Q_snprintf( szBuf, sizeof( szBuf ), "%i", iVal );
  79. return(szBuf);
  80. }
  81. case FIELD_FLOAT:
  82. {
  83. Q_snprintf(szBuf,sizeof(szBuf), "%g", flVal);
  84. return(szBuf);
  85. }
  86. case FIELD_COLOR32:
  87. {
  88. Q_snprintf(szBuf,sizeof(szBuf), "%d %d %d %d", (int)rgbaVal.r, (int)rgbaVal.g, (int)rgbaVal.b, (int)rgbaVal.a);
  89. return(szBuf);
  90. }
  91. case FIELD_VECTOR:
  92. {
  93. Q_snprintf(szBuf,sizeof(szBuf), "[%g %g %g]", (double)vecVal[0], (double)vecVal[1], (double)vecVal[2]);
  94. return(szBuf);
  95. }
  96. case FIELD_VOID:
  97. {
  98. szBuf[0] = '\0';
  99. return(szBuf);
  100. }
  101. }
  102. return("No conversion to string");
  103. }
  104. };
  105. ////////////////////////// pluginvariant implementation //////////////////////////
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Returns this variant as a vector.
  108. //-----------------------------------------------------------------------------
  109. inline void pluginvariant::Vector3D(Vector &vec) const
  110. {
  111. if (( fieldType == FIELD_VECTOR ) || ( fieldType == FIELD_POSITION_VECTOR ))
  112. {
  113. vec[0] = vecVal[0];
  114. vec[1] = vecVal[1];
  115. vec[2] = vecVal[2];
  116. }
  117. else
  118. {
  119. vec = vec3_origin;
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: Returns this variant as an edict_t
  124. //-----------------------------------------------------------------------------
  125. inline const edict_t *pluginvariant::Edict(void) const
  126. {
  127. if ( fieldType == FIELD_EHANDLE )
  128. return eVal;
  129. return NULL;
  130. }
  131. #endif // pluginvariant_H