SWF_ScriptObject.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_SCRIPTOBJECT_H__
  21. #define __SWF_SCRIPTOBJECT_H__
  22. class idSWFSpriteInstance;
  23. /*
  24. ========================
  25. This is the base class for script variables which are implemented in code
  26. ========================
  27. */
  28. class idSWFScriptNativeVariable {
  29. public:
  30. virtual bool IsReadOnly() { return false; }
  31. virtual void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) = 0;
  32. virtual idSWFScriptVar Get( class idSWFScriptObject * object ) = 0;
  33. };
  34. #define SWF_NATIVE_VAR_DECLARE( x ) \
  35. class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable { \
  36. public: \
  37. void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ); \
  38. idSWFScriptVar Get( class idSWFScriptObject * object ); \
  39. } swfScriptVar_##x;
  40. #define SWF_NATIVE_VAR_DECLARE_READONLY( x ) \
  41. class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable { \
  42. public: \
  43. bool IsReadOnly() { return true; } \
  44. void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) { assert( false ); } \
  45. idSWFScriptVar Get( class idSWFScriptObject * object ); \
  46. } swfScriptVar_##x;
  47. /*
  48. ========================
  49. This is a helper class for quickly setting up native variables which need access to a parent class
  50. ========================
  51. */
  52. template< typename T >
  53. class idSWFScriptNativeVariable_Nested : public idSWFScriptNativeVariable {
  54. public:
  55. idSWFScriptNativeVariable_Nested() : pThis( NULL ) { }
  56. idSWFScriptNativeVariable_Nested * Bind( T * p ) { pThis = p; return this; }
  57. virtual void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) = 0;
  58. virtual idSWFScriptVar Get( class idSWFScriptObject * object ) = 0;
  59. protected:
  60. T * pThis;
  61. };
  62. #define SWF_NATIVE_VAR_DECLARE_NESTED( x, y ) \
  63. class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable_Nested<y> { \
  64. public: \
  65. void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ); \
  66. idSWFScriptVar Get( class idSWFScriptObject * object ); \
  67. } swfScriptVar_##x;
  68. #define SWF_NATIVE_VAR_DECLARE_NESTED_READONLY( x, y, z ) \
  69. class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable_Nested<y> { \
  70. public: \
  71. bool IsReadOnly() { return true; } \
  72. void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) { assert( false ); } \
  73. idSWFScriptVar Get( class idSWFScriptObject * object ) { return pThis->z; } \
  74. } swfScriptVar_##x;
  75. /*
  76. ========================
  77. An object in an action script is a collection of variables. functions are also variables.
  78. ========================
  79. */
  80. class idSWFScriptObject {
  81. public:
  82. idSWFScriptObject();
  83. virtual ~idSWFScriptObject();
  84. static idSWFScriptObject * Alloc();
  85. void AddRef();
  86. void Release();
  87. void SetNoAutoDelete( bool b ) { noAutoDelete = b; }
  88. void Clear();
  89. void MakeArray();
  90. void SetSprite( idSWFSpriteInstance * s ) { objectType = SWF_OBJECT_SPRITE; data.sprite = s; }
  91. idSWFSpriteInstance * GetSprite() { return ( objectType == SWF_OBJECT_SPRITE ) ? data.sprite : NULL; }
  92. void SetText( idSWFTextInstance * t ) { objectType = SWF_OBJECT_TEXT; data.text = t; }
  93. idSWFTextInstance * GetText() { return ( objectType == SWF_OBJECT_TEXT ) ? data.text : NULL; }
  94. // Also accessible via __proto__ property
  95. idSWFScriptObject * GetPrototype() { return prototype; }
  96. void SetPrototype( idSWFScriptObject *_prototype ) { assert( prototype == NULL ); prototype = _prototype; prototype->AddRef(); }
  97. idSWFScriptVar Get( int index );
  98. idSWFScriptVar Get( const char * name );
  99. idSWFSpriteInstance * GetSprite( int index );
  100. idSWFSpriteInstance * GetSprite( const char * name );
  101. idSWFScriptObject * GetObject( int index );
  102. idSWFScriptObject * GetObject( const char * name );
  103. idSWFTextInstance * GetText( int index );
  104. idSWFTextInstance * GetText( const char * name );
  105. void Set( int index, const idSWFScriptVar & value );
  106. void Set( const char * name, const idSWFScriptVar & value );
  107. void SetNative( const char * name, idSWFScriptNativeVariable * native );
  108. bool HasProperty( const char * name );
  109. bool HasValidProperty( const char * name );
  110. idSWFScriptVar DefaultValue( bool stringHint );
  111. // This is to implement for-in (fixme: respect DONTENUM flag)
  112. int NumVariables() { return variables.Num(); }
  113. const char * EnumVariable( int i ) { return variables[i].name; }
  114. idSWFScriptVar GetNestedVar( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  115. idSWFScriptObject * GetNestedObj( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  116. idSWFSpriteInstance * GetNestedSprite( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  117. idSWFTextInstance * GetNestedText( const char * arg1, const char * arg2 = NULL, const char * arg3 = NULL, const char * arg4 = NULL, const char * arg5 = NULL, const char * arg6 = NULL );
  118. void PrintToConsole() const;
  119. private:
  120. int refCount;
  121. bool noAutoDelete;
  122. enum swfNamedVarFlags_t {
  123. SWF_VAR_FLAG_NONE = 0,
  124. SWF_VAR_FLAG_READONLY = BIT(1),
  125. SWF_VAR_FLAG_DONTENUM = BIT(2)
  126. };
  127. struct swfNamedVar_t {
  128. swfNamedVar_t() : native( NULL ) { }
  129. ~swfNamedVar_t();
  130. swfNamedVar_t & operator=( const swfNamedVar_t & other );
  131. int index;
  132. int hashNext;
  133. idStr name;
  134. idSWFScriptVar value;
  135. idSWFScriptNativeVariable * native;
  136. int flags;
  137. };
  138. idList< swfNamedVar_t, TAG_SWF > variables;
  139. static const int VARIABLE_HASH_BUCKETS = 16;
  140. int variablesHash[VARIABLE_HASH_BUCKETS];
  141. idSWFScriptObject * prototype;
  142. enum swfObjectType_t {
  143. SWF_OBJECT_OBJECT,
  144. SWF_OBJECT_ARRAY,
  145. SWF_OBJECT_SPRITE,
  146. SWF_OBJECT_TEXT
  147. } objectType;
  148. union swfObjectData_t {
  149. idSWFSpriteInstance * sprite; // only valid if objectType == SWF_OBJECT_SPRITE
  150. idSWFTextInstance * text; // only valid if objectType == SWF_OBJECT_TEXT
  151. } data;
  152. swfNamedVar_t * GetVariable( int index, bool create );
  153. swfNamedVar_t * GetVariable( const char * name, bool create );
  154. };
  155. #endif // !__SWF_SCRIPTOBJECT_H__