Script_Interpreter.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SCRIPT_INTERPRETER_H__
  21. #define __SCRIPT_INTERPRETER_H__
  22. #define MAX_STACK_DEPTH 64
  23. #define LOCALSTACK_SIZE 49152 //bc 6144
  24. typedef struct prstack_s {
  25. int s;
  26. const function_t *f;
  27. int stackbase;
  28. } prstack_t;
  29. class idInterpreter {
  30. private:
  31. prstack_t callStack[ MAX_STACK_DEPTH ];
  32. int callStackDepth;
  33. int maxStackDepth;
  34. byte localstack[ LOCALSTACK_SIZE ];
  35. int localstackUsed;
  36. int localstackBase;
  37. int maxLocalstackUsed;
  38. const function_t *currentFunction;
  39. int instructionPointer;
  40. int popParms;
  41. const idEventDef *multiFrameEvent;
  42. idEntity *eventEntity;
  43. idThread *thread;
  44. void PopParms( int numParms );
  45. void PushString( const char *string );
  46. void Push( int value );
  47. const char *FloatToString( float value );
  48. void AppendString( idVarDef *def, const char *from );
  49. void SetString( idVarDef *def, const char *from );
  50. const char *GetString( idVarDef *def );
  51. varEval_t GetVariable( idVarDef *def );
  52. idEntity *GetEntity( int entnum ) const;
  53. idScriptObject *GetScriptObject( int entnum ) const;
  54. void NextInstruction( int position );
  55. void LeaveFunction( idVarDef *returnDef );
  56. void CallEvent( const function_t *func, int argsize );
  57. void CallSysEvent( const function_t *func, int argsize );
  58. public:
  59. bool doneProcessing;
  60. bool threadDying;
  61. bool terminateOnExit;
  62. bool debug;
  63. idInterpreter();
  64. // save games
  65. void Save( idSaveGame *savefile ) const; // archives object for save game file
  66. void Restore( idRestoreGame *savefile ); // unarchives object from save game file
  67. void SetThread( idThread *pThread );
  68. void StackTrace( void ) const;
  69. int CurrentLine( void ) const;
  70. const char *CurrentFile( void ) const;
  71. void Error( char *fmt, ... ) const id_attribute((format(printf,2,3)));
  72. void Warning( char *fmt, ... ) const id_attribute((format(printf,2,3)));
  73. void DisplayInfo( void ) const;
  74. bool BeginMultiFrameEvent( idEntity *ent, const idEventDef *event );
  75. void EndMultiFrameEvent( idEntity *ent, const idEventDef *event );
  76. bool MultiFrameEventInProgress( void ) const;
  77. void ThreadCall( idInterpreter *source, const function_t *func, int args );
  78. void EnterFunction( const function_t *func, bool clearStack );
  79. void EnterObjectFunction( idEntity *self, const function_t *func, bool clearStack );
  80. //BC
  81. //from DarkMod
  82. bool EnterFunctionVarArgVN(const function_t *func, bool clearStack, const char *fmt, va_list args);
  83. bool Execute( void );
  84. void Reset( void );
  85. bool GetRegisterValue( const char *name, idStr &out, int scopeDepth );
  86. int GetCallstackDepth( void ) const;
  87. const prstack_t *GetCallstack( void ) const;
  88. const function_t *GetCurrentFunction( void ) const;
  89. idThread *GetThread( void ) const;
  90. };
  91. /*
  92. ====================
  93. idInterpreter::PopParms
  94. ====================
  95. */
  96. ID_INLINE void idInterpreter::PopParms( int numParms ) {
  97. // pop our parms off the stack
  98. if ( localstackUsed < numParms ) {
  99. //BC TODO: figure out why passing args from code->script causes this error.
  100. //Error( "locals stack underflow\n" );
  101. //gameLocal.Printf("locals stack underflow. localstackUsed: %d numParms: %d\n", localstackUsed, numParms);
  102. }
  103. localstackUsed -= numParms;
  104. }
  105. /*
  106. ====================
  107. idInterpreter::Push
  108. ====================
  109. */
  110. ID_INLINE void idInterpreter::Push( int value ) {
  111. if ( localstackUsed + sizeof( int ) > LOCALSTACK_SIZE ) {
  112. Error( "Push: locals stack overflow\n" );
  113. }
  114. *( int * )&localstack[ localstackUsed ] = value;
  115. localstackUsed += sizeof( int );
  116. }
  117. /*
  118. ====================
  119. idInterpreter::PushString
  120. ====================
  121. */
  122. ID_INLINE void idInterpreter::PushString( const char *string ) {
  123. if ( localstackUsed + MAX_STRING_LEN > LOCALSTACK_SIZE ) {
  124. Error( "PushString: locals stack overflow\n" );
  125. }
  126. idStr::Copynz( ( char * )&localstack[ localstackUsed ], string, MAX_STRING_LEN );
  127. localstackUsed += MAX_STRING_LEN;
  128. }
  129. /*
  130. ====================
  131. idInterpreter::FloatToString
  132. ====================
  133. */
  134. ID_INLINE const char *idInterpreter::FloatToString( float value ) {
  135. static char text[ 32 ];
  136. if ( value == ( float )( int )value ) {
  137. sprintf( text, "%d", ( int )value );
  138. } else {
  139. sprintf( text, "%f", value );
  140. }
  141. return text;
  142. }
  143. /*
  144. ====================
  145. idInterpreter::AppendString
  146. ====================
  147. */
  148. ID_INLINE void idInterpreter::AppendString( idVarDef *def, const char *from ) {
  149. if ( def->initialized == idVarDef::stackVariable ) {
  150. idStr::Append( ( char * )&localstack[ localstackBase + def->value.stackOffset ], MAX_STRING_LEN, from );
  151. } else {
  152. idStr::Append( def->value.stringPtr, MAX_STRING_LEN, from );
  153. }
  154. }
  155. /*
  156. ====================
  157. idInterpreter::SetString
  158. ====================
  159. */
  160. ID_INLINE void idInterpreter::SetString( idVarDef *def, const char *from ) {
  161. if ( def->initialized == idVarDef::stackVariable ) {
  162. idStr::Copynz( ( char * )&localstack[ localstackBase + def->value.stackOffset ], from, MAX_STRING_LEN );
  163. } else {
  164. idStr::Copynz( def->value.stringPtr, from, MAX_STRING_LEN );
  165. }
  166. }
  167. /*
  168. ====================
  169. idInterpreter::GetString
  170. ====================
  171. */
  172. ID_INLINE const char *idInterpreter::GetString( idVarDef *def ) {
  173. if ( def->initialized == idVarDef::stackVariable ) {
  174. return ( char * )&localstack[ localstackBase + def->value.stackOffset ];
  175. } else {
  176. return def->value.stringPtr;
  177. }
  178. }
  179. /*
  180. ====================
  181. idInterpreter::GetVariable
  182. ====================
  183. */
  184. ID_INLINE varEval_t idInterpreter::GetVariable( idVarDef *def ) {
  185. if ( def->initialized == idVarDef::stackVariable ) {
  186. varEval_t val;
  187. val.intPtr = ( int * )&localstack[ localstackBase + def->value.stackOffset ];
  188. return val;
  189. } else {
  190. return def->value;
  191. }
  192. }
  193. /*
  194. ================
  195. idInterpreter::GetEntity
  196. ================
  197. */
  198. ID_INLINE idEntity *idInterpreter::GetEntity( int entnum ) const{
  199. assert( entnum <= MAX_GENTITIES );
  200. if ( ( entnum > 0 ) && ( entnum <= MAX_GENTITIES ) ) {
  201. return gameLocal.entities[ entnum - 1 ];
  202. }
  203. return NULL;
  204. }
  205. /*
  206. ================
  207. idInterpreter::GetScriptObject
  208. ================
  209. */
  210. ID_INLINE idScriptObject *idInterpreter::GetScriptObject( int entnum ) const {
  211. idEntity *ent;
  212. assert( entnum <= MAX_GENTITIES );
  213. if ( ( entnum > 0 ) && ( entnum <= MAX_GENTITIES ) ) {
  214. ent = gameLocal.entities[ entnum - 1 ];
  215. if ( ent && ent->scriptObject.data ) {
  216. return &ent->scriptObject;
  217. }
  218. }
  219. return NULL;
  220. }
  221. /*
  222. ====================
  223. idInterpreter::NextInstruction
  224. ====================
  225. */
  226. ID_INLINE void idInterpreter::NextInstruction( int position ) {
  227. // Before we execute an instruction, we increment instructionPointer,
  228. // therefore we need to compensate for that here.
  229. instructionPointer = position - 1;
  230. }
  231. #endif /* !__SCRIPT_INTERPRETER_H__ */