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.

265 lines
5.8 KiB

  1. // This contains stubs that emulate the HL2 engine for places where
  2. // the response rules expect to find it.
  3. // temporarily make unicode go away as we deal with Valve types
  4. #ifdef _UNICODE
  5. #define PUT_UNICODE_BACK
  6. #undef _UNICODE
  7. #endif
  8. #include "platform.h"
  9. #include "wchartypes.h"
  10. #include <ctype.h>
  11. struct datamap_t;
  12. template <typename T> datamap_t *DataMapInit(T *);
  13. #include "appframework/appframework.h"
  14. #include "filesystem.h"
  15. #include "vstdlib/random.h"
  16. #include "icommandline.h"
  17. #include "responserules/response_types.h"
  18. #include "../../responserules/runtime/response_types_internal.h"
  19. #include "response_system.h"
  20. #include "cli_appsystem_unmanaged_wrapper.h"
  21. #include "cli_appsystem_adapter.h"
  22. #include "characterset.h"
  23. #ifdef PUT_UNICODE_BACK
  24. #define _UNICODE
  25. #undef PUT_UNICODE_BACK
  26. #endif
  27. class CLI_SourceEngineEmulator;
  28. const char *COM_Parse (const char *data);
  29. byte *UTIL_LoadFileForMe( const char *filename, int *pLength, IFileSystem *filesystem );
  30. int TestRandomNumberGeneration( int bottom, int top )
  31. {
  32. return ResponseRules::IEngineEmulator::Get()->GetRandomStream()->RandomInt(bottom,top);
  33. }
  34. const char *TestFileSystemHook( )
  35. {
  36. // return ResponseRules::IEngineEmulator::Get()->GetFilesystem() ? "present" : "absent" ;
  37. return ResponseRules::IEngineEmulator::Get()->GetFilesystem()->IsSteam() ? "steam" : "not steam";
  38. }
  39. class CLI_SourceEngineEmulator : public ResponseRules::IEngineEmulator
  40. {
  41. public:
  42. /// Given an input text buffer data pointer, parses a single token into the variable token and returns the new
  43. /// reading position
  44. virtual const char *ParseFile( const char *data, char *token, int maxlen );
  45. /// Return a pointer to an IFileSystem we can use to read and process scripts.
  46. virtual IFileSystem *GetFilesystem();
  47. /// Return a pointer to an instance of an IUniformRandomStream
  48. virtual IUniformRandomStream *GetRandomStream() ;
  49. /// Return a pointer to a tier0 ICommandLine
  50. virtual ICommandLine *GetCommandLine();
  51. /// Emulates the server's UTIL_LoadFileForMe
  52. virtual byte *LoadFileForMe( const char *filename, int *pLength );
  53. /// Emulates the server's UTIL_FreeFile
  54. virtual void FreeFile( byte *buffer );
  55. CLI_SourceEngineEmulator();
  56. virtual ~CLI_SourceEngineEmulator();
  57. void LocalInit();
  58. // protected:
  59. };
  60. CLI_SourceEngineEmulator g_EngineEmulator;
  61. CLI_SourceEngineEmulator::CLI_SourceEngineEmulator()
  62. {
  63. LocalInit();
  64. }
  65. CLI_SourceEngineEmulator::~CLI_SourceEngineEmulator()
  66. {
  67. }
  68. ResponseRules::IEngineEmulator *ResponseRules::IEngineEmulator::s_pSingleton = &g_EngineEmulator;
  69. /// Return a pointer to an IFileSystem we can use to read and process scripts.
  70. IFileSystem *CLI_SourceEngineEmulator::GetFilesystem()
  71. {
  72. return AppSystemWrapper_Unmanaged::Get()->GetFilesytem();
  73. }
  74. /// Return a pointer to an instance of an IUniformRandomStream
  75. IUniformRandomStream *CLI_SourceEngineEmulator::GetRandomStream()
  76. {
  77. return AppSystemWrapper_Unmanaged::Get()->GetRandomStream();
  78. }
  79. /// Return a pointer to a tier0 ICommandLine
  80. ICommandLine *CLI_SourceEngineEmulator::GetCommandLine()
  81. {
  82. return AppSystemWrapper_Unmanaged::Get()->GetCommandLine();
  83. }
  84. /// Emulates the server's UTIL_LoadFileForMe
  85. byte *CLI_SourceEngineEmulator::LoadFileForMe( const char *filename, int *pLength )
  86. {
  87. return UTIL_LoadFileForMe( filename, pLength, GetFilesystem() );
  88. }
  89. /// Emulates the server's UTIL_FreeFile
  90. void CLI_SourceEngineEmulator::FreeFile( byte *buffer )
  91. {
  92. GetFilesystem()->FreeOptimalReadBuffer( buffer );
  93. }
  94. /*
  95. ===================================
  96. STUFF COPIED FROM SOURCE ENGINE
  97. ===================================
  98. */
  99. // wordbreak parsing set
  100. static characterset_t g_BreakSet, g_BreakSetIncludingColons;
  101. bool com_ignorecolons = false;
  102. #define COM_TOKEN_MAX_LENGTH 1024
  103. char com_token[COM_TOKEN_MAX_LENGTH] = {0} ;
  104. /// Given an input text buffer data pointer, parses a single token into the variable token and returns the new
  105. /// reading position
  106. const char *CLI_SourceEngineEmulator::ParseFile( const char *data, char *token, int maxlen )
  107. {
  108. Assert( data );
  109. const char *return_data = COM_Parse(data);
  110. Q_strncpy(token, com_token, maxlen);
  111. return return_data;
  112. }
  113. /*
  114. ==============
  115. COM_Parse (from Quake engine)
  116. Parse a token out of a string
  117. ==============
  118. */
  119. static
  120. const char *COM_Parse (const char *data)
  121. {
  122. unsigned char c;
  123. int len;
  124. characterset_t *breaks;
  125. breaks = &g_BreakSetIncludingColons;
  126. if ( com_ignorecolons )
  127. breaks = &g_BreakSet;
  128. len = 0;
  129. com_token[0] = 0;
  130. if (!data)
  131. return NULL;
  132. // skip whitespace
  133. skipwhite:
  134. while ( (c = *data) <= ' ')
  135. {
  136. if (c == 0)
  137. return NULL; // end of file;
  138. data++;
  139. }
  140. // skip // comments
  141. if (c=='/' && data[1] == '/')
  142. {
  143. while (*data && *data != '\n')
  144. data++;
  145. goto skipwhite;
  146. }
  147. // handle quoted strings specially
  148. if (c == '\"')
  149. {
  150. data++;
  151. while (1)
  152. {
  153. c = *data++;
  154. if (c=='\"' || !c)
  155. {
  156. com_token[len] = 0;
  157. return data;
  158. }
  159. com_token[len] = c;
  160. len++;
  161. }
  162. }
  163. // parse single characters
  164. if ( IN_CHARACTERSET( *breaks, c ) )
  165. {
  166. com_token[len] = c;
  167. len++;
  168. com_token[len] = 0;
  169. return data+1;
  170. }
  171. // parse a regular word
  172. do
  173. {
  174. com_token[len] = c;
  175. data++;
  176. len++;
  177. c = *data;
  178. if ( IN_CHARACTERSET( *breaks, c ) )
  179. break;
  180. } while (c>32);
  181. com_token[len] = 0;
  182. return data;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Purpose:
  186. // Input : *filename -
  187. // *pLength -
  188. // Output : byte
  189. //-----------------------------------------------------------------------------
  190. static byte *UTIL_LoadFileForMe( const char *filename, int *pLength, IFileSystem *filesystem )
  191. {
  192. void *buffer = NULL;
  193. int length = filesystem->ReadFileEx( filename, "GAME", &buffer, true, true );
  194. if ( pLength )
  195. {
  196. *pLength = length;
  197. }
  198. return (byte *)buffer;
  199. }
  200. void CLI_SourceEngineEmulator::LocalInit()
  201. {
  202. CharacterSetBuild( &g_BreakSet, "{}()'" );
  203. CharacterSetBuild( &g_BreakSetIncludingColons, "{}()':" );
  204. }