SWF_ScriptFunction.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 __SWF_SCRIPTFUNCTION_H__
  21. #define __SWF_SCRIPTFUNCTION_H__
  22. /*
  23. ========================
  24. Interface for calling functions from script
  25. ========================
  26. */
  27. class idSWFScriptFunction {
  28. public:
  29. virtual ~idSWFScriptFunction() {};
  30. virtual idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ){ return idSWFScriptVar(); }; // this should never be hit
  31. virtual void AddRef(){};
  32. virtual void Release(){};
  33. virtual idSWFScriptObject *GetPrototype() { return NULL; }
  34. virtual void SetPrototype( idSWFScriptObject * _object ) { }
  35. };
  36. /*
  37. ========================
  38. Interface for calling functions from script, implemented statically
  39. ========================
  40. */
  41. class idSWFScriptFunction_Static : public idSWFScriptFunction {
  42. public:
  43. idSWFScriptFunction_Static() { }
  44. virtual void AddRef() { }
  45. virtual void Release() { }
  46. };
  47. /*
  48. ========================
  49. Interface for calling functions from script, implemented natively on a nested class object
  50. ========================
  51. */
  52. template< typename T >
  53. class idSWFScriptFunction_Nested : public idSWFScriptFunction {
  54. protected:
  55. T * pThis;
  56. public:
  57. idSWFScriptFunction_Nested() : pThis( NULL ) { }
  58. idSWFScriptFunction * Bind( T * _pThis ) { pThis = _pThis; return this; }
  59. virtual void AddRef() { }
  60. virtual void Release() { }
  61. };
  62. /*
  63. ========================
  64. Interface for calling functions from script, with reference counting
  65. NOTE: The ref count starts at 0 here because it assumes you do an AddRef on the allocated
  66. object. The proper way is to start it at 1 and force the caller to Release, but that's
  67. really kind of a pain in the ass. It was made to be used like this:
  68. object->Set( "myFunction", new idSWFScriptFunction_MyFunction() );
  69. ========================
  70. */
  71. class idSWFScriptFunction_RefCounted : public idSWFScriptFunction {
  72. public:
  73. idSWFScriptFunction_RefCounted() : refCount( 0 ) { }
  74. void AddRef() { refCount++; }
  75. void Release() { if ( --refCount <= 0 ) { delete this; } }
  76. private:
  77. int refCount;
  78. };
  79. /*
  80. ========================
  81. Action Scripts can define a pool of constants then push values from that pool
  82. The documentation isn't very clear on the scope of these things, but from what
  83. I've gathered by testing, pool is per-function and copied into the function
  84. whenever that function is declared.
  85. ========================
  86. */
  87. class idSWFConstantPool {
  88. public:
  89. idSWFConstantPool();
  90. ~idSWFConstantPool() { Clear(); }
  91. void Clear();
  92. void Copy( const idSWFConstantPool & other );
  93. idSWFScriptString * Get( int n ) { return pool[n]; }
  94. void Append( idSWFScriptString * s ) { pool.Append( s ); }
  95. private:
  96. idList< idSWFScriptString *, TAG_SWF > pool;
  97. };
  98. /*
  99. ========================
  100. The idSWFStack class is just a helper routine for treating an idList like a stack
  101. ========================
  102. */
  103. class idSWFStack : public idList< idSWFScriptVar > {
  104. public:
  105. idSWFScriptVar & A() { return operator[]( Num() - 1 ); }
  106. idSWFScriptVar & B() { return operator[]( Num() - 2 ); }
  107. idSWFScriptVar & C() { return operator[]( Num() - 3 ); }
  108. idSWFScriptVar & D() { return operator[]( Num() - 4 ); }
  109. void Pop( int n ) { SetNum( Num() - n ); }
  110. };
  111. /*
  112. ========================
  113. idSWFScriptFunction_Script is a script function that's implemented in action script
  114. ========================
  115. */
  116. class idSWFScriptFunction_Script : public idSWFScriptFunction {
  117. public:
  118. idSWFScriptFunction_Script() : refCount( 1 ), flags( 0 ), prototype( NULL ), data( NULL ), length( 0 ), defaultSprite( NULL ) { registers.SetNum( 4 ); }
  119. virtual ~idSWFScriptFunction_Script();
  120. static idSWFScriptFunction_Script * Alloc() { return new (TAG_SWF) idSWFScriptFunction_Script; }
  121. void AddRef() { refCount++; }
  122. void Release() { if ( --refCount == 0 ) { delete this; } }
  123. // This could all be passed to Alloc (and was at one time) but in some places it's far more convenient to specify each separately
  124. void SetFlags( uint16 _flags ) { flags = _flags; }
  125. void SetData( const byte * _data, uint32 _length ) { data = _data; length = _length; }
  126. void SetScope( idList<idSWFScriptObject *> & scope );
  127. void SetConstants( const idSWFConstantPool & _constants ) { constants.Copy( _constants ); }
  128. void SetDefaultSprite( idSWFSpriteInstance * _sprite ) { defaultSprite = _sprite; }
  129. void AllocRegisters( int numRegs ) { registers.SetNum( numRegs ); }
  130. void AllocParameters( int numParms ) { parameters.SetNum( numParms ); }
  131. void SetParameter( uint8 n, uint8 r, const char * name ) { parameters[n].reg = r; parameters[n].name = name; }
  132. idSWFScriptObject * GetPrototype() { return prototype; }
  133. void SetPrototype( idSWFScriptObject * _prototype ) { _prototype->AddRef(); assert( prototype == NULL ); prototype = _prototype; }
  134. virtual idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms );
  135. private:
  136. idSWFScriptVar Run( idSWFScriptObject * thisObject, idSWFStack & stack, idSWFBitStream & bitstream );
  137. private:
  138. int refCount;
  139. uint16 flags;
  140. const byte * data;
  141. uint32 length;
  142. idSWFScriptObject * prototype;
  143. idSWFSpriteInstance * defaultSprite; // some actions have an implicit sprite they work off of (e.g. Action_GotoFrame outside of object scope)
  144. idList< idSWFScriptObject *, TAG_SWF > scope;
  145. idSWFConstantPool constants;
  146. idList< idSWFScriptVar, TAG_SWF > registers;
  147. struct parmInfo_t {
  148. const char * name;
  149. uint8 reg;
  150. };
  151. idList< parmInfo_t, TAG_SWF > parameters;
  152. };
  153. #endif // !__SWF_SCRIPTFUNCTION_H__