SWF_ScriptObject.h (7964B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 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. 23 24 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. 25 26 =========================================================================== 27 */ 28 #ifndef __SWF_SCRIPTOBJECT_H__ 29 #define __SWF_SCRIPTOBJECT_H__ 30 31 class idSWFSpriteInstance; 32 33 /* 34 ======================== 35 This is the base class for script variables which are implemented in code 36 ======================== 37 */ 38 class idSWFScriptNativeVariable { 39 public: 40 virtual bool IsReadOnly() { return false; } 41 virtual void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) = 0; 42 virtual idSWFScriptVar Get( class idSWFScriptObject * object ) = 0; 43 }; 44 45 #define SWF_NATIVE_VAR_DECLARE( x ) \ 46 class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable { \ 47 public: \ 48 void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ); \ 49 idSWFScriptVar Get( class idSWFScriptObject * object ); \ 50 } swfScriptVar_##x; 51 52 #define SWF_NATIVE_VAR_DECLARE_READONLY( x ) \ 53 class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable { \ 54 public: \ 55 bool IsReadOnly() { return true; } \ 56 void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) { assert( false ); } \ 57 idSWFScriptVar Get( class idSWFScriptObject * object ); \ 58 } swfScriptVar_##x; 59 60 /* 61 ======================== 62 This is a helper class for quickly setting up native variables which need access to a parent class 63 ======================== 64 */ 65 template< typename T > 66 class idSWFScriptNativeVariable_Nested : public idSWFScriptNativeVariable { 67 public: 68 idSWFScriptNativeVariable_Nested() : pThis( NULL ) { } 69 idSWFScriptNativeVariable_Nested * Bind( T * p ) { pThis = p; return this; } 70 virtual void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) = 0; 71 virtual idSWFScriptVar Get( class idSWFScriptObject * object ) = 0; 72 protected: 73 T * pThis; 74 }; 75 76 #define SWF_NATIVE_VAR_DECLARE_NESTED( x, y ) \ 77 class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable_Nested<y> { \ 78 public: \ 79 void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ); \ 80 idSWFScriptVar Get( class idSWFScriptObject * object ); \ 81 } swfScriptVar_##x; 82 83 #define SWF_NATIVE_VAR_DECLARE_NESTED_READONLY( x, y, z ) \ 84 class idSWFScriptNativeVar_##x : public idSWFScriptNativeVariable_Nested<y> { \ 85 public: \ 86 bool IsReadOnly() { return true; } \ 87 void Set( class idSWFScriptObject * object, const idSWFScriptVar & value ) { assert( false ); } \ 88 idSWFScriptVar Get( class idSWFScriptObject * object ) { return pThis->z; } \ 89 } swfScriptVar_##x; 90 91 /* 92 ======================== 93 An object in an action script is a collection of variables. functions are also variables. 94 ======================== 95 */ 96 class idSWFScriptObject { 97 public: 98 idSWFScriptObject(); 99 virtual ~idSWFScriptObject(); 100 101 static idSWFScriptObject * Alloc(); 102 void AddRef(); 103 void Release(); 104 void SetNoAutoDelete( bool b ) { noAutoDelete = b; } 105 106 void Clear(); 107 108 void MakeArray(); 109 110 void SetSprite( idSWFSpriteInstance * s ) { objectType = SWF_OBJECT_SPRITE; data.sprite = s; } 111 idSWFSpriteInstance * GetSprite() { return ( objectType == SWF_OBJECT_SPRITE ) ? data.sprite : NULL; } 112 113 void SetText( idSWFTextInstance * t ) { objectType = SWF_OBJECT_TEXT; data.text = t; } 114 idSWFTextInstance * GetText() { return ( objectType == SWF_OBJECT_TEXT ) ? data.text : NULL; } 115 116 // Also accessible via __proto__ property 117 idSWFScriptObject * GetPrototype() { return prototype; } 118 void SetPrototype( idSWFScriptObject *_prototype ) { assert( prototype == NULL ); prototype = _prototype; prototype->AddRef(); } 119 idSWFScriptVar Get( int index ); 120 idSWFScriptVar Get( const char * name ); 121 idSWFSpriteInstance * GetSprite( int index ); 122 idSWFSpriteInstance * GetSprite( const char * name ); 123 idSWFScriptObject * GetObject( int index ); 124 idSWFScriptObject * GetObject( const char * name ); 125 idSWFTextInstance * GetText( int index ); 126 idSWFTextInstance * GetText( const char * name ); 127 void Set( int index, const idSWFScriptVar & value ); 128 void Set( const char * name, const idSWFScriptVar & value ); 129 void SetNative( const char * name, idSWFScriptNativeVariable * native ); 130 bool HasProperty( const char * name ); 131 bool HasValidProperty( const char * name ); 132 idSWFScriptVar DefaultValue( bool stringHint ); 133 134 // This is to implement for-in (fixme: respect DONTENUM flag) 135 int NumVariables() { return variables.Num(); } 136 const char * EnumVariable( int i ) { return variables[i].name; } 137 138 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 ); 139 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 ); 140 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 ); 141 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 ); 142 143 void PrintToConsole() const; 144 145 private: 146 int refCount; 147 bool noAutoDelete; 148 149 enum swfNamedVarFlags_t { 150 SWF_VAR_FLAG_NONE = 0, 151 SWF_VAR_FLAG_READONLY = BIT(1), 152 SWF_VAR_FLAG_DONTENUM = BIT(2) 153 }; 154 struct swfNamedVar_t { 155 swfNamedVar_t() : native( NULL ) { } 156 ~swfNamedVar_t(); 157 swfNamedVar_t & operator=( const swfNamedVar_t & other ); 158 159 int index; 160 int hashNext; 161 idStr name; 162 idSWFScriptVar value; 163 idSWFScriptNativeVariable * native; 164 int flags; 165 }; 166 idList< swfNamedVar_t, TAG_SWF > variables; 167 168 static const int VARIABLE_HASH_BUCKETS = 16; 169 int variablesHash[VARIABLE_HASH_BUCKETS]; 170 171 idSWFScriptObject * prototype; 172 173 enum swfObjectType_t { 174 SWF_OBJECT_OBJECT, 175 SWF_OBJECT_ARRAY, 176 SWF_OBJECT_SPRITE, 177 SWF_OBJECT_TEXT 178 } objectType; 179 180 union swfObjectData_t { 181 idSWFSpriteInstance * sprite; // only valid if objectType == SWF_OBJECT_SPRITE 182 idSWFTextInstance * text; // only valid if objectType == SWF_OBJECT_TEXT 183 } data; 184 185 swfNamedVar_t * GetVariable( int index, bool create ); 186 swfNamedVar_t * GetVariable( const char * name, bool create ); 187 }; 188 189 #endif // !__SWF_SCRIPTOBJECT_H__