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.

271 lines
7.7 KiB

  1. //========= Copyright (C) Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Helper for UI components - used for binding to both Scaleform ActionScript and
  4. // Panorama Javascript.
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "uicomponent_common.h"
  10. #if defined ( USE_PANORAMA_BINDINGS )
  11. static CPanoramaMarshallHelper g_PanoramaMarshallHelper;
  12. CPanoramaMarshallHelper* GetPanoramaMarshallHelper( void )
  13. {
  14. return &g_PanoramaMarshallHelper;
  15. }
  16. SFVALUEARRAY CPanoramaMarshallHelper::Params_GetArgs( SFPARAMS params )
  17. {
  18. Assert( 0 ); // IMPLEMENT ME OR REFACTOR CALLING CODE
  19. return SFVALUEARRAY();
  20. }
  21. unsigned int CPanoramaMarshallHelper::Params_GetNumArgs( SFPARAMS params )
  22. {
  23. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  24. return args.Length();
  25. }
  26. bool CPanoramaMarshallHelper::Params_ArgIs( SFPARAMS params, unsigned int index, _ValueType v )
  27. {
  28. return ( Params_GetArgType( params, index ) == v );
  29. }
  30. SFVALUE CPanoramaMarshallHelper::Params_GetArg( SFPARAMS params, int index /*= 0 */ )
  31. {
  32. Assert( 0 ); // IMPLEMENT ME OR REFACTOR CALLING CODE
  33. return NULL;
  34. }
  35. IUIMarshalHelper::_ValueType CPanoramaMarshallHelper::Params_GetArgType( SFPARAMS params, int index /*= 0 */ )
  36. {
  37. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  38. if ( args[ index ]->IsBoolean() )
  39. return VT_Boolean;
  40. else if ( args[ index ]->IsNumber() )
  41. return VT_Number;
  42. else if ( args[ index ]->IsInt32() )
  43. return VT_Int;
  44. else if ( args[ index ]->IsUint32() )
  45. return VT_UInt;
  46. else if ( args[ index ]->IsString() )
  47. return VT_String;
  48. else if ( args[ index ]->IsUndefined() )
  49. return VT_Undefined;
  50. else if ( args[ index ]->IsNull() )
  51. return VT_Null;
  52. Assert( 0 );
  53. return VT_Undefined;
  54. }
  55. double CPanoramaMarshallHelper::Params_GetArgAsNumber( SFPARAMS params, int index /*= 0 */ )
  56. {
  57. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  58. Assert( args[ index ]->IsNumber() );
  59. return args[ index ]->NumberValue();
  60. }
  61. bool CPanoramaMarshallHelper::Params_GetArgAsBool( SFPARAMS params, int index /*= 0 */ )
  62. {
  63. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  64. Assert( args[ index ]->IsBoolean() );
  65. return args[ index ]->BooleanValue();
  66. }
  67. const char* CPanoramaMarshallHelper::Params_GetArgAsString( SFPARAMS params, int index /*= 0 */ )
  68. {
  69. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  70. m_vecStringStorage.EnsureCount( index + 1 );
  71. Assert( args[ index ]->IsString() );
  72. v8::String::Utf8Value str( args[ index ] );
  73. m_vecStringStorage[ index ].Set( *str );
  74. return m_vecStringStorage[ index ];
  75. }
  76. const wchar_t* CPanoramaMarshallHelper::Params_GetArgAsStringW( SFPARAMS params, int index /*= 0 */ )
  77. {
  78. Assert( 0 ); // IMPLEMENT ME OR REFACTOR CALLING CODE
  79. return NULL;
  80. }
  81. void CPanoramaMarshallHelper::Params_DebugSpew( SFPARAMS params )
  82. {
  83. Assert( 0 );
  84. }
  85. void CPanoramaMarshallHelper::Params_SetResult( SFPARAMS params, SFVALUE value )
  86. {
  87. Assert( 0 );
  88. }
  89. void CPanoramaMarshallHelper::Params_SetResult( SFPARAMS params, int value )
  90. {
  91. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  92. args.GetReturnValue().Set( v8::Integer::New( args.GetIsolate(), value ) );
  93. }
  94. void CPanoramaMarshallHelper::Params_SetResult( SFPARAMS params, float value )
  95. {
  96. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  97. args.GetReturnValue().Set( v8::Number::New( args.GetIsolate(), value ) );
  98. }
  99. void CPanoramaMarshallHelper::Params_SetResult( SFPARAMS params, bool value )
  100. {
  101. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  102. args.GetReturnValue().Set( v8::Boolean::New( args.GetIsolate(), value ) );
  103. }
  104. void CPanoramaMarshallHelper::Params_SetResult( SFPARAMS params, const char* value, bool bMakeNewValue /*=false*/ )
  105. {
  106. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  107. args.GetReturnValue().Set( v8::String::NewFromOneByte( args.GetIsolate(), (uint8*)value ) );
  108. }
  109. void CPanoramaMarshallHelper::Params_SetResult( SFPARAMS params, const wchar_t* value, bool bMakeNewValue /*=false*/ )
  110. {
  111. const v8::FunctionCallbackInfo<v8::Value> &args = *reinterpret_cast< const v8::FunctionCallbackInfo<v8::Value> * >( params );
  112. args.GetReturnValue().Set( v8::String::NewFromTwoByte( args.GetIsolate(), (uint16*)value ) );
  113. }
  114. SFVALUE CPanoramaMarshallHelper::Params_CreateNewObject( SFPARAMS params )
  115. {
  116. Assert( 0 );
  117. return NULL;
  118. }
  119. SFVALUE CPanoramaMarshallHelper::Params_CreateNewString( SFPARAMS params, const char* value )
  120. {
  121. Assert( 0 );
  122. return NULL;
  123. }
  124. SFVALUE CPanoramaMarshallHelper::Params_CreateNewString( SFPARAMS params, const wchar_t* value )
  125. {
  126. Assert( 0 );
  127. return NULL;
  128. }
  129. SFVALUE CPanoramaMarshallHelper::Params_CreateNewArray( SFPARAMS params, int size /*= -1 */ )
  130. {
  131. Assert( 0 );
  132. return NULL;
  133. }
  134. #endif // USE_PANORAMA_BINDINGS
  135. // Using this from webapi_response to reuse some code.
  136. extern void EmitJSONString( CUtlBuffer &outputBuffer, const char *pchValue );
  137. // Convert keyvalues to a json object.
  138. bool Helper_RecursiveKeyValuesToJSONString( const KeyValues* pKV, CUtlBuffer &outBuffer )
  139. {
  140. outBuffer.PutChar( '{' );
  141. for ( KeyValues *dat = pKV->GetFirstSubKey(); dat != NULL; dat = dat->GetNextKey() )
  142. {
  143. if ( !dat->GetName() )
  144. {
  145. Assert( 0 ); // Unhandled
  146. return false;
  147. }
  148. EmitJSONString( outBuffer, dat->GetName() );
  149. outBuffer.PutChar( ':' );
  150. if ( dat->GetFirstSubKey() )
  151. {
  152. Helper_RecursiveKeyValuesToJSONString( dat, outBuffer );
  153. }
  154. else
  155. {
  156. switch ( dat->GetDataType() )
  157. {
  158. case KeyValues::TYPE_NONE:
  159. {
  160. Helper_RecursiveKeyValuesToJSONString( dat, outBuffer );
  161. break;
  162. }
  163. case KeyValues::TYPE_STRING:
  164. {
  165. if ( dat->GetString() )
  166. EmitJSONString( outBuffer, dat->GetString() );
  167. else
  168. outBuffer.Put( "null", 4 );
  169. break;
  170. }
  171. case KeyValues::TYPE_INT:
  172. {
  173. CNumStr numStr( dat->GetInt() );
  174. outBuffer.Put( numStr.String(), Q_strlen( numStr.String() ) );
  175. break;
  176. }
  177. case KeyValues::TYPE_UINT64:
  178. {
  179. CNumStr numStr( dat->GetUint64() );
  180. outBuffer.Put( numStr.String(), Q_strlen( numStr.String() ) );
  181. break;
  182. }
  183. case KeyValues::TYPE_FLOAT:
  184. {
  185. CNumStr numStr( dat->GetFloat() );
  186. outBuffer.Put( numStr.String(), Q_strlen( numStr.String() ) );
  187. break;
  188. }
  189. default:
  190. {
  191. Assert( 0 ); // Unhandled
  192. return false;
  193. }
  194. }
  195. }
  196. if ( dat->GetNextKey() )
  197. {
  198. outBuffer.PutChar( ',' );
  199. }
  200. }
  201. outBuffer.PutChar( '}' );
  202. return true;
  203. }
  204. void Helper_TrackedCachedGcValueAcrossRuns( ConVar &cv, int &nValue, bool bValueObtainedThisSession, RTime32 rtNow )
  205. {
  206. if ( !rtNow )
  207. return;
  208. bool bUpdateCachedValue = false;
  209. RTime32 const rtCached = cv.GetInt();
  210. if ( !bValueObtainedThisSession )
  211. {
  212. if ( ( int( rtCached - 24 * 3600 ) < int( rtNow ) ) &&
  213. ( int( rtNow - rtCached ) < 7 * 24 * 3600 ) )
  214. {
  215. nValue = rtCached & 0xFF; // Return a cached number
  216. }
  217. else
  218. {
  219. nValue = RandomInt( 4, 19 ); // Fake number of teammates to lure user
  220. bUpdateCachedValue = true;
  221. }
  222. }
  223. else
  224. {
  225. bUpdateCachedValue = true;
  226. }
  227. if ( bUpdateCachedValue )
  228. {
  229. rtNow &= ~0xFF; // vacate room for caching 0..255
  230. rtNow |= nValue & 0xFF; // store the count in there too
  231. if ( rtNow != rtCached )
  232. cv.SetValue( int( rtNow ) );
  233. }
  234. }