Team Fortress 2 Source Code as on 22/4/2020
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.

165 lines
4.3 KiB

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