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.

244 lines
7.6 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: VPC
  4. //
  5. //=====================================================================================//
  6. #include "vpc.h"
  7. //-----------------------------------------------------------------------------
  8. //-----------------------------------------------------------------------------
  9. void CVPC::SetupDefaultConditionals()
  10. {
  11. //
  12. // PLATFORM Conditionals
  13. //
  14. {
  15. FindOrCreateConditional( "WIN32", true, CONDITIONAL_PLATFORM );
  16. FindOrCreateConditional( "WIN64", true, CONDITIONAL_PLATFORM );
  17. // LINUX is the platform but the VPC scripts use $LINUX and $DEDICATED
  18. // (which we automatically create later).
  19. FindOrCreateConditional( "LINUX32", true, CONDITIONAL_PLATFORM );
  20. FindOrCreateConditional( "LINUX64", true, CONDITIONAL_PLATFORM );
  21. FindOrCreateConditional( "CYGWIN", true, CONDITIONAL_PLATFORM );
  22. FindOrCreateConditional( "OSX32", true, CONDITIONAL_PLATFORM );
  23. FindOrCreateConditional( "OSX64", true, CONDITIONAL_PLATFORM );
  24. FindOrCreateConditional( "X360", true, CONDITIONAL_PLATFORM );
  25. FindOrCreateConditional( "PS3", true, CONDITIONAL_PLATFORM );
  26. }
  27. //
  28. // CUSTOM conditionals
  29. //
  30. {
  31. // setup default custom conditionals
  32. FindOrCreateConditional( "PROFILE", true, CONDITIONAL_CUSTOM );
  33. FindOrCreateConditional( "RETAIL", true, CONDITIONAL_CUSTOM );
  34. FindOrCreateConditional( "CALLCAP", true, CONDITIONAL_CUSTOM );
  35. FindOrCreateConditional( "FASTCAP", true, CONDITIONAL_CUSTOM );
  36. FindOrCreateConditional( "CERT", true, CONDITIONAL_CUSTOM );
  37. FindOrCreateConditional( "MEMTEST", true, CONDITIONAL_CUSTOM );
  38. FindOrCreateConditional( "NOFPO", true, CONDITIONAL_CUSTOM );
  39. FindOrCreateConditional( "POSIX", true, CONDITIONAL_CUSTOM );
  40. FindOrCreateConditional( "LV", true, CONDITIONAL_CUSTOM );
  41. FindOrCreateConditional( "DEMO", true, CONDITIONAL_CUSTOM );
  42. FindOrCreateConditional( "NO_STEAM", true, CONDITIONAL_CUSTOM );
  43. FindOrCreateConditional( "DVDEMU", true, CONDITIONAL_CUSTOM );
  44. FindOrCreateConditional( "QTDEBUG", true, CONDITIONAL_CUSTOM );
  45. FindOrCreateConditional( "NO_CEG", true, CONDITIONAL_CUSTOM );
  46. FindOrCreateConditional( "UPLOAD_CEG", true, CONDITIONAL_CUSTOM );
  47. }
  48. }
  49. //-----------------------------------------------------------------------------
  50. //-----------------------------------------------------------------------------
  51. const char *CVPC::GetTargetPlatformName()
  52. {
  53. for ( int i = 0; i < m_Conditionals.Count(); i++ )
  54. {
  55. conditional_t *pConditional = &m_Conditionals[i];
  56. if ( pConditional->type == CONDITIONAL_PLATFORM && pConditional->m_bDefined )
  57. {
  58. return pConditional->name.String();
  59. }
  60. }
  61. // fatal - should have already been default set
  62. Assert( 0 );
  63. VPCError( "Unspecified platform." );
  64. return NULL;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Case Insensitive. Returns true if platform conditional has been marked
  68. // as defined.
  69. //-----------------------------------------------------------------------------
  70. bool CVPC::IsPlatformDefined( const char *pName )
  71. {
  72. for ( int i=0; i<m_Conditionals.Count(); i++ )
  73. {
  74. if ( m_Conditionals[i].type == CONDITIONAL_PLATFORM && !V_stricmp( pName, m_Conditionals[i].name.String() ) )
  75. {
  76. return m_Conditionals[i].m_bDefined;
  77. }
  78. }
  79. return false;
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Case Insensitive
  83. //-----------------------------------------------------------------------------
  84. conditional_t *CVPC::FindOrCreateConditional( const char *pName, bool bCreate, conditionalType_e type )
  85. {
  86. for (int i=0; i<m_Conditionals.Count(); i++)
  87. {
  88. if ( !V_stricmp( pName, m_Conditionals[i].name.String() ) )
  89. {
  90. // found
  91. return &m_Conditionals[i];
  92. }
  93. }
  94. if ( !bCreate )
  95. {
  96. return NULL;
  97. }
  98. int index = m_Conditionals.AddToTail();
  99. char tempName[256];
  100. V_strncpy( tempName, pName, sizeof( tempName ) );
  101. // primary internal use as lower case, but spewed to user as upper for style consistency
  102. m_Conditionals[index].name = V_strlower( tempName );
  103. m_Conditionals[index].upperCaseName = V_strupr( tempName );
  104. m_Conditionals[index].type = type;
  105. return &m_Conditionals[index];
  106. }
  107. void CVPC::SetConditional( const char *pString, bool bSet )
  108. {
  109. VPCStatus( false, "Set Conditional: $%s = %s", pString, ( bSet ? "1" : "0" ) );
  110. conditional_t *pConditional = FindOrCreateConditional( pString, true, CONDITIONAL_CUSTOM );
  111. if ( !pConditional )
  112. {
  113. VPCError( "Failed to find or create $%s conditional", pString );
  114. }
  115. pConditional->m_bDefined = bSet;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Returns true if string has a conditional of the specified type
  119. //-----------------------------------------------------------------------------
  120. bool CVPC::ConditionHasDefinedType( const char* pCondition, conditionalType_e type )
  121. {
  122. char symbol[MAX_SYSTOKENCHARS];
  123. for ( int i=0; i<m_Conditionals.Count(); i++ )
  124. {
  125. if ( m_Conditionals[i].type != type )
  126. continue;
  127. sprintf( symbol, "$%s", m_Conditionals[i].name.String() );
  128. if ( V_stristr( pCondition, symbol ) )
  129. {
  130. // a define of expected type occurs in the conditional expression
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Callback for expression evaluator.
  138. //-----------------------------------------------------------------------------
  139. bool CVPC::ResolveConditionalSymbol( const char *pSymbol )
  140. {
  141. int offset = 0;
  142. if ( !V_stricmp( pSymbol, "$0" ) || !V_stricmp( pSymbol, "0" ) )
  143. {
  144. return false;
  145. }
  146. else if ( !V_stricmp( pSymbol, "$1" ) || !V_stricmp( pSymbol, "1" ) )
  147. {
  148. return true;
  149. }
  150. if ( pSymbol[0] == '$' )
  151. {
  152. offset = 1;
  153. }
  154. conditional_t *pConditional = FindOrCreateConditional( (char*)pSymbol+offset, false, CONDITIONAL_NULL );
  155. if ( pConditional )
  156. {
  157. // game conditionals only resolve true when they are 'defined' and 'active'
  158. // only one game conditional is expected to be active at a time
  159. if ( pConditional->type == CONDITIONAL_GAME )
  160. {
  161. if ( !pConditional->m_bDefined )
  162. {
  163. return false;
  164. }
  165. pConditional->m_bGameConditionActive;
  166. }
  167. // all other type of conditions are gated by their 'defined' state
  168. return pConditional->m_bDefined;
  169. }
  170. // unknown conditional, defaults to false
  171. return false;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Callback for expression evaluator.
  175. //-----------------------------------------------------------------------------
  176. static bool ResolveSymbol( const char *pSymbol )
  177. {
  178. return g_pVPC->ResolveConditionalSymbol( pSymbol );
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Callback for expression evaluator.
  182. //-----------------------------------------------------------------------------
  183. static void SymbolSyntaxError( const char *pReason )
  184. {
  185. // invoke internal syntax error hndling which spews script stack as well
  186. g_pVPC->VPCSyntaxError( pReason );
  187. }
  188. //-----------------------------------------------------------------------------
  189. //-----------------------------------------------------------------------------
  190. bool CVPC::EvaluateConditionalExpression( const char *pExpression )
  191. {
  192. char conditionalBuffer[MAX_SYSTOKENCHARS];
  193. ResolveMacrosInString( pExpression, conditionalBuffer, sizeof( conditionalBuffer ) );
  194. if ( !conditionalBuffer[0] )
  195. {
  196. // empty string, same as not having a conditional
  197. return true;
  198. }
  199. bool bResult = false;
  200. CExpressionEvaluator ExpressionHandler;
  201. bool bValid = ExpressionHandler.Evaluate( bResult, conditionalBuffer, ::ResolveSymbol, ::SymbolSyntaxError );
  202. if ( !bValid )
  203. {
  204. g_pVPC->VPCSyntaxError( "VPC Conditional Evaluation Error" );
  205. }
  206. return bResult;
  207. }