Script_Program.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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_PROGRAM_H__
  21. #define __SCRIPT_PROGRAM_H__
  22. class idScriptObject;
  23. class idEventDef;
  24. class idVarDef;
  25. class idTypeDef;
  26. class idEntity;
  27. class idThread;
  28. class idSaveGame;
  29. class idRestoreGame;
  30. #define MAX_STRING_LEN 128
  31. #define MAX_GLOBALS 296608 // in bytes
  32. #define MAX_STRINGS 1024
  33. #define MAX_FUNCS 3584
  34. #define MAX_STATEMENTS 131072 // statement_t - 18 bytes last I checked
  35. typedef enum {
  36. ev_error = -1, ev_void, ev_scriptevent, ev_namespace, ev_string, ev_float, ev_vector, ev_entity, ev_field, ev_function, ev_virtualfunction, ev_pointer, ev_object, ev_jumpoffset, ev_argsize, ev_boolean
  37. } etype_t;
  38. class function_t {
  39. public:
  40. function_t();
  41. size_t Allocated() const;
  42. void SetName( const char *name );
  43. const char *Name() const;
  44. void Clear();
  45. private:
  46. idStr name;
  47. public:
  48. const idEventDef *eventdef;
  49. idVarDef *def;
  50. const idTypeDef *type;
  51. int firstStatement;
  52. int numStatements;
  53. int parmTotal;
  54. int locals; // total ints of parms + locals
  55. int filenum; // source file defined in
  56. idList<int, TAG_SCRIPT> parmSize;
  57. };
  58. typedef union eval_s {
  59. const char *stringPtr;
  60. float _float;
  61. float vector[ 3 ];
  62. function_t *function;
  63. int _int;
  64. int entity;
  65. } eval_t;
  66. /***********************************************************************
  67. idTypeDef
  68. Contains type information for variables and functions.
  69. ***********************************************************************/
  70. class idTypeDef {
  71. private:
  72. etype_t type;
  73. idStr name;
  74. int size;
  75. // function types are more complex
  76. idTypeDef *auxType; // return type
  77. idList<idTypeDef *, TAG_SCRIPT> parmTypes;
  78. idStrList parmNames;
  79. idList<const function_t *, TAG_SCRIPT> functions;
  80. public:
  81. idVarDef *def; // a def that points to this type
  82. idTypeDef( const idTypeDef &other );
  83. idTypeDef( etype_t etype, idVarDef *edef, const char *ename, int esize, idTypeDef *aux );
  84. void operator=( const idTypeDef& other );
  85. size_t Allocated() const;
  86. bool Inherits( const idTypeDef *basetype ) const;
  87. bool MatchesType( const idTypeDef &matchtype ) const;
  88. bool MatchesVirtualFunction( const idTypeDef &matchfunc ) const;
  89. void AddFunctionParm( idTypeDef *parmtype, const char *name );
  90. void AddField( idTypeDef *fieldtype, const char *name );
  91. void SetName( const char *newname );
  92. const char *Name() const;
  93. etype_t Type() const;
  94. int Size() const;
  95. idTypeDef *SuperClass() const;
  96. idTypeDef *ReturnType() const;
  97. void SetReturnType( idTypeDef *type );
  98. idTypeDef *FieldType() const;
  99. void SetFieldType( idTypeDef *type );
  100. idTypeDef *PointerType() const;
  101. void SetPointerType( idTypeDef *type );
  102. int NumParameters() const;
  103. idTypeDef *GetParmType( int parmNumber ) const;
  104. const char *GetParmName( int parmNumber ) const;
  105. int NumFunctions() const;
  106. int GetFunctionNumber( const function_t *func ) const;
  107. const function_t *GetFunction( int funcNumber ) const;
  108. void AddFunction( const function_t *func );
  109. };
  110. /***********************************************************************
  111. idScriptObject
  112. In-game representation of objects in scripts. Use the idScriptVariable template
  113. (below) to access variables.
  114. ***********************************************************************/
  115. class idScriptObject {
  116. private:
  117. idTypeDef *type;
  118. public:
  119. byte *data;
  120. idScriptObject();
  121. ~idScriptObject();
  122. void Save( idSaveGame *savefile ) const; // archives object for save game file
  123. void Restore( idRestoreGame *savefile ); // unarchives object from save game file
  124. void Free();
  125. bool SetType( const char *typeName );
  126. void ClearObject();
  127. bool HasObject() const;
  128. idTypeDef *GetTypeDef() const;
  129. const char *GetTypeName() const;
  130. const function_t *GetConstructor() const;
  131. const function_t *GetDestructor() const;
  132. const function_t *GetFunction( const char *name ) const;
  133. byte *GetVariable( const char *name, etype_t etype ) const;
  134. };
  135. /***********************************************************************
  136. idScriptVariable
  137. Helper template that handles looking up script variables stored in objects.
  138. If the specified variable doesn't exist, or is the wrong data type, idScriptVariable
  139. will cause an error.
  140. ***********************************************************************/
  141. template<class type, etype_t etype, class returnType>
  142. class idScriptVariable {
  143. private:
  144. type *data;
  145. public:
  146. idScriptVariable();
  147. bool IsLinked() const;
  148. void Unlink();
  149. void LinkTo( idScriptObject &obj, const char *name );
  150. idScriptVariable &operator=( const returnType &value );
  151. operator returnType() const;
  152. };
  153. template<class type, etype_t etype, class returnType>
  154. ID_INLINE idScriptVariable<type, etype, returnType>::idScriptVariable() {
  155. data = NULL;
  156. }
  157. template<class type, etype_t etype, class returnType>
  158. ID_INLINE bool idScriptVariable<type, etype, returnType>::IsLinked() const {
  159. return ( data != NULL );
  160. }
  161. template<class type, etype_t etype, class returnType>
  162. ID_INLINE void idScriptVariable<type, etype, returnType>::Unlink() {
  163. data = NULL;
  164. }
  165. template<class type, etype_t etype, class returnType>
  166. ID_INLINE void idScriptVariable<type, etype, returnType>::LinkTo( idScriptObject &obj, const char *name ) {
  167. data = ( type * )obj.GetVariable( name, etype );
  168. if ( !data ) {
  169. gameError( "Missing '%s' field in script object '%s'", name, obj.GetTypeName() );
  170. }
  171. }
  172. template<class type, etype_t etype, class returnType>
  173. ID_INLINE idScriptVariable<type, etype, returnType> &idScriptVariable<type, etype, returnType>::operator=( const returnType &value ) {
  174. // check if we attempt to access the object before it's been linked
  175. assert( data );
  176. // make sure we don't crash if we don't have a pointer
  177. if ( data ) {
  178. *data = ( type )value;
  179. }
  180. return *this;
  181. }
  182. template<class type, etype_t etype, class returnType>
  183. ID_INLINE idScriptVariable<type, etype, returnType>::operator returnType() const {
  184. // check if we attempt to access the object before it's been linked
  185. assert( data );
  186. // make sure we don't crash if we don't have a pointer
  187. if ( data ) {
  188. return ( const returnType )*data;
  189. } else {
  190. // reasonably safe value
  191. return ( const returnType )0;
  192. }
  193. }
  194. /***********************************************************************
  195. Script object variable access template instantiations
  196. These objects will automatically handle looking up of the current value
  197. of a variable in a script object. They can be stored as part of a class
  198. for up-to-date values of the variable, or can be used in functions to
  199. sample the data for non-dynamic values.
  200. ***********************************************************************/
  201. typedef idScriptVariable<int, ev_boolean, int> idScriptBool;
  202. typedef idScriptVariable<float, ev_float, float> idScriptFloat;
  203. typedef idScriptVariable<float, ev_float, int> idScriptInt;
  204. typedef idScriptVariable<idVec3, ev_vector, idVec3> idScriptVector;
  205. typedef idScriptVariable<idStr, ev_string, const char *> idScriptString;
  206. /***********************************************************************
  207. idCompileError
  208. Causes the compiler to exit out of compiling the current function and
  209. display an error message with line and file info.
  210. ***********************************************************************/
  211. class idCompileError : public idException {
  212. public:
  213. idCompileError( const char *text ) : idException( text ) {}
  214. };
  215. /***********************************************************************
  216. idVarDef
  217. Define the name, type, and location of variables, functions, and objects
  218. defined in script.
  219. ***********************************************************************/
  220. typedef union varEval_s {
  221. idScriptObject **objectPtrPtr;
  222. char *stringPtr;
  223. float *floatPtr;
  224. idVec3 *vectorPtr;
  225. function_t *functionPtr;
  226. int *intPtr;
  227. byte *bytePtr;
  228. int *entityNumberPtr;
  229. int virtualFunction;
  230. int jumpOffset;
  231. int stackOffset; // offset in stack for local variables
  232. int argSize;
  233. varEval_s *evalPtr;
  234. int ptrOffset;
  235. } varEval_t;
  236. class idVarDefName;
  237. class idVarDef {
  238. friend class idVarDefName;
  239. public:
  240. int num;
  241. varEval_t value;
  242. idVarDef * scope; // function, namespace, or object the var was defined in
  243. int numUsers; // number of users if this is a constant
  244. typedef enum {
  245. uninitialized, initializedVariable, initializedConstant, stackVariable
  246. } initialized_t;
  247. initialized_t initialized;
  248. public:
  249. idVarDef( idTypeDef *typeptr = NULL );
  250. ~idVarDef();
  251. const char * Name() const;
  252. const char * GlobalName() const;
  253. void SetTypeDef( idTypeDef *_type ) { typeDef = _type; }
  254. idTypeDef * TypeDef() const { return typeDef; }
  255. etype_t Type() const { return ( typeDef != NULL ) ? typeDef->Type() : ev_void; }
  256. int DepthOfScope( const idVarDef *otherScope ) const;
  257. void SetFunction( function_t *func );
  258. void SetObject( idScriptObject *object );
  259. void SetValue( const eval_t &value, bool constant );
  260. void SetString( const char *string, bool constant );
  261. idVarDef * Next() const { return next; } // next var def with same name
  262. void PrintInfo( idFile *file, int instructionPointer ) const;
  263. private:
  264. idTypeDef * typeDef;
  265. idVarDefName * name; // name of this var
  266. idVarDef * next; // next var with the same name
  267. };
  268. /***********************************************************************
  269. idVarDefName
  270. ***********************************************************************/
  271. class idVarDefName {
  272. public:
  273. idVarDefName() { defs = NULL; }
  274. idVarDefName( const char *n ) { name = n; defs = NULL; }
  275. const char * Name() const { return name; }
  276. idVarDef * GetDefs() const { return defs; }
  277. void AddDef( idVarDef *def );
  278. void RemoveDef( idVarDef *def );
  279. private:
  280. idStr name;
  281. idVarDef * defs;
  282. };
  283. /***********************************************************************
  284. Variable and type defintions
  285. ***********************************************************************/
  286. extern idTypeDef type_void;
  287. extern idTypeDef type_scriptevent;
  288. extern idTypeDef type_namespace;
  289. extern idTypeDef type_string;
  290. extern idTypeDef type_float;
  291. extern idTypeDef type_vector;
  292. extern idTypeDef type_entity;
  293. extern idTypeDef type_field;
  294. extern idTypeDef type_function;
  295. extern idTypeDef type_virtualfunction;
  296. extern idTypeDef type_pointer;
  297. extern idTypeDef type_object;
  298. extern idTypeDef type_jumpoffset; // only used for jump opcodes
  299. extern idTypeDef type_argsize; // only used for function call and thread opcodes
  300. extern idTypeDef type_boolean;
  301. extern idVarDef def_void;
  302. extern idVarDef def_scriptevent;
  303. extern idVarDef def_namespace;
  304. extern idVarDef def_string;
  305. extern idVarDef def_float;
  306. extern idVarDef def_vector;
  307. extern idVarDef def_entity;
  308. extern idVarDef def_field;
  309. extern idVarDef def_function;
  310. extern idVarDef def_virtualfunction;
  311. extern idVarDef def_pointer;
  312. extern idVarDef def_object;
  313. extern idVarDef def_jumpoffset; // only used for jump opcodes
  314. extern idVarDef def_argsize; // only used for function call and thread opcodes
  315. extern idVarDef def_boolean;
  316. typedef struct statement_s {
  317. unsigned short op;
  318. idVarDef *a;
  319. idVarDef *b;
  320. idVarDef *c;
  321. unsigned short linenumber;
  322. unsigned short file;
  323. } statement_t;
  324. /***********************************************************************
  325. idProgram
  326. Handles compiling and storage of script data. Multiple idProgram objects
  327. would represent seperate programs with no knowledge of each other. Scripts
  328. meant to access shared data and functions should all be compiled by a
  329. single idProgram.
  330. ***********************************************************************/
  331. class idProgram {
  332. private:
  333. idStrList fileList;
  334. idStr filename;
  335. int filenum;
  336. int numVariables;
  337. byte variables[ MAX_GLOBALS ];
  338. idStaticList<byte,MAX_GLOBALS> variableDefaults;
  339. idStaticList<function_t,MAX_FUNCS> functions;
  340. idStaticList<statement_t,MAX_STATEMENTS> statements;
  341. idList<idTypeDef *, TAG_SCRIPT> types;
  342. idHashIndex typesHash;
  343. idList<idVarDefName *, TAG_SCRIPT> varDefNames;
  344. idHashIndex varDefNameHash;
  345. idList<idVarDef *, TAG_SCRIPT> varDefs;
  346. idVarDef *sysDef;
  347. int top_functions;
  348. int top_statements;
  349. int top_types;
  350. int top_defs;
  351. int top_files;
  352. void CompileStats();
  353. public:
  354. idVarDef *returnDef;
  355. idVarDef *returnStringDef;
  356. idProgram();
  357. ~idProgram();
  358. // save games
  359. void Save( idSaveGame *savefile ) const;
  360. bool Restore( idRestoreGame *savefile );
  361. int CalculateChecksum() const; // Used to insure program code has not
  362. // changed between savegames
  363. void Startup( const char *defaultScript );
  364. void Restart();
  365. bool CompileText( const char *source, const char *text, bool console );
  366. const function_t *CompileFunction( const char *functionName, const char *text );
  367. void CompileFile( const char *filename );
  368. void BeginCompilation();
  369. void FinishCompilation();
  370. void DisassembleStatement( idFile *file, int instructionPointer ) const;
  371. void Disassemble() const;
  372. void FreeData();
  373. const char *GetFilename( int num );
  374. int GetFilenum( const char *name );
  375. int GetLineNumberForStatement( int index );
  376. const char *GetFilenameForStatement( int index );
  377. idTypeDef *AllocType( idTypeDef &type );
  378. idTypeDef *AllocType( etype_t etype, idVarDef *edef, const char *ename, int esize, idTypeDef *aux );
  379. idTypeDef *GetType( idTypeDef &type, bool allocate );
  380. idTypeDef *FindType( const char *name );
  381. idVarDef *AllocDef( idTypeDef *type, const char *name, idVarDef *scope, bool constant );
  382. idVarDef *GetDef( const idTypeDef *type, const char *name, const idVarDef *scope ) const;
  383. void FreeDef( idVarDef *d, const idVarDef *scope );
  384. idVarDef *FindFreeResultDef( idTypeDef *type, const char *name, idVarDef *scope, const idVarDef *a, const idVarDef *b );
  385. idVarDef *GetDefList( const char *name ) const;
  386. void AddDefToNameList( idVarDef *def, const char *name );
  387. function_t *FindFunction( const char *name ) const; // returns NULL if function not found
  388. function_t *FindFunction( const char *name, const idTypeDef *type ) const; // returns NULL if function not found
  389. function_t &AllocFunction( idVarDef *def );
  390. function_t *GetFunction( int index );
  391. int GetFunctionIndex( const function_t *func );
  392. void SetEntity( const char *name, idEntity *ent );
  393. statement_t *AllocStatement();
  394. statement_t &GetStatement( int index );
  395. int NumStatements() { return statements.Num(); }
  396. int GetReturnedInteger();
  397. void ReturnFloat( float value );
  398. void ReturnInteger( int value );
  399. void ReturnVector( idVec3 const &vec );
  400. void ReturnString( const char *string );
  401. void ReturnEntity( idEntity *ent );
  402. int NumFilenames() { return fileList.Num( ); }
  403. };
  404. /*
  405. ================
  406. idProgram::GetStatement
  407. ================
  408. */
  409. ID_INLINE statement_t &idProgram::GetStatement( int index ) {
  410. return statements[ index ];
  411. }
  412. /*
  413. ================
  414. idProgram::GetFunction
  415. ================
  416. */
  417. ID_INLINE function_t *idProgram::GetFunction( int index ) {
  418. return &functions[ index ];
  419. }
  420. /*
  421. ================
  422. idProgram::GetFunctionIndex
  423. ================
  424. */
  425. ID_INLINE int idProgram::GetFunctionIndex( const function_t *func ) {
  426. return func - &functions[0];
  427. }
  428. /*
  429. ================
  430. idProgram::GetReturnedInteger
  431. ================
  432. */
  433. ID_INLINE int idProgram::GetReturnedInteger() {
  434. return *returnDef->value.intPtr;
  435. }
  436. /*
  437. ================
  438. idProgram::ReturnFloat
  439. ================
  440. */
  441. ID_INLINE void idProgram::ReturnFloat( float value ) {
  442. *returnDef->value.floatPtr = value;
  443. }
  444. /*
  445. ================
  446. idProgram::ReturnInteger
  447. ================
  448. */
  449. ID_INLINE void idProgram::ReturnInteger( int value ) {
  450. *returnDef->value.intPtr = value;
  451. }
  452. /*
  453. ================
  454. idProgram::ReturnVector
  455. ================
  456. */
  457. ID_INLINE void idProgram::ReturnVector( idVec3 const &vec ) {
  458. *returnDef->value.vectorPtr = vec;
  459. }
  460. /*
  461. ================
  462. idProgram::ReturnString
  463. ================
  464. */
  465. ID_INLINE void idProgram::ReturnString( const char *string ) {
  466. idStr::Copynz( returnStringDef->value.stringPtr, string, MAX_STRING_LEN );
  467. }
  468. /*
  469. ================
  470. idProgram::GetFilename
  471. ================
  472. */
  473. ID_INLINE const char *idProgram::GetFilename( int num ) {
  474. return fileList[ num ];
  475. }
  476. /*
  477. ================
  478. idProgram::GetLineNumberForStatement
  479. ================
  480. */
  481. ID_INLINE int idProgram::GetLineNumberForStatement( int index ) {
  482. return statements[ index ].linenumber;
  483. }
  484. /*
  485. ================
  486. idProgram::GetFilenameForStatement
  487. ================
  488. */
  489. ID_INLINE const char *idProgram::GetFilenameForStatement( int index ) {
  490. return GetFilename( statements[ index ].file );
  491. }
  492. #endif /* !__SCRIPT_PROGRAM_H__ */