SWF_ScriptVar.cpp (12038B)
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 #pragma hdrstop 29 #include "../idlib/precompiled.h" 30 31 extern idCVar swf_debugShowAddress; 32 33 /* 34 ======================== 35 idSWFScriptVar::idSWFScriptVar 36 ======================== 37 */ 38 idSWFScriptVar::idSWFScriptVar( const idSWFScriptVar & other ) { 39 type = other.type; 40 value = other.value; 41 if ( other.type == SWF_VAR_STRING ) { 42 other.value.string->AddRef(); 43 } else if ( other.type == SWF_VAR_OBJECT ) { 44 other.value.object->AddRef(); 45 } else if ( other.type == SWF_VAR_FUNCTION ) { 46 other.value.function->AddRef(); 47 } 48 } 49 50 /* 51 ======================== 52 idSWFScriptVar::operator= 53 ======================== 54 */ 55 idSWFScriptVar & idSWFScriptVar::operator=( const idSWFScriptVar & other ) { 56 if ( this != &other ) { 57 Free(); 58 type = other.type; 59 value = other.value; 60 if ( other.type == SWF_VAR_STRING ) { 61 other.value.string->AddRef(); 62 } else if ( other.type == SWF_VAR_OBJECT ) { 63 other.value.object->AddRef(); 64 } else if ( other.type == SWF_VAR_FUNCTION ) { 65 other.value.function->AddRef(); 66 } 67 } 68 return *this; 69 } 70 71 /* 72 ======================== 73 idSWFScriptVar::~idSWFScriptVar 74 ======================== 75 */ 76 idSWFScriptVar::~idSWFScriptVar() { 77 Free(); 78 } 79 80 /* 81 ======================== 82 idSWFScriptVar::Free 83 ======================== 84 */ 85 void idSWFScriptVar::Free() { 86 if ( type == SWF_VAR_STRING ) { 87 value.string->Release(); 88 } else if ( type == SWF_VAR_OBJECT ) { 89 value.object->Release(); 90 } else if ( type == SWF_VAR_FUNCTION ) { 91 value.function->Release(); 92 } 93 value.string = NULL; 94 value.function = NULL; 95 value.object = NULL; 96 type = SWF_VAR_UNDEF; 97 } 98 99 /* 100 ======================== 101 idSWFScriptVar::SetObject 102 ======================== 103 */ 104 void idSWFScriptVar::SetObject( idSWFScriptObject * o ) { 105 Free(); 106 if ( o == NULL ) { 107 type = SWF_VAR_NULL; 108 } else { 109 type = SWF_VAR_OBJECT; 110 value.object = o; 111 o->AddRef(); 112 } 113 } 114 115 /* 116 ======================== 117 idSWFScriptVar::SetFunction 118 ======================== 119 */ 120 void idSWFScriptVar::SetFunction( idSWFScriptFunction * f ) { 121 Free(); 122 if ( f == NULL ) { 123 type = SWF_VAR_NULL; 124 } else { 125 type = SWF_VAR_FUNCTION; 126 value.function = f; 127 f->AddRef(); 128 } 129 } 130 131 /* 132 ======================== 133 idSWFScriptVar::StrictEquals 134 ======================== 135 */ 136 bool idSWFScriptVar::StrictEquals( const idSWFScriptVar & other ) { 137 if ( type != other.type ) { 138 return false; 139 } 140 switch ( type ) { 141 case SWF_VAR_STRINGID: return ( value.i == other.value.i ); 142 case SWF_VAR_STRING: return ( *value.string == *other.value.string ); 143 case SWF_VAR_FLOAT: return ( value.f == other.value.f ); 144 case SWF_VAR_BOOL: return ( value.b == other.value.b ); 145 case SWF_VAR_INTEGER: return ( value.i == other.value.i ); 146 case SWF_VAR_NULL: return true; 147 case SWF_VAR_UNDEF: return true; 148 case SWF_VAR_OBJECT: return ( value.object == other.value.object ); 149 case SWF_VAR_FUNCTION: return ( value.function == other.value.function ); 150 default: assert( false ); return false; 151 } 152 } 153 154 /* 155 ======================== 156 idSWFScriptVar::AbstractEquals 157 ======================== 158 */ 159 bool idSWFScriptVar::AbstractEquals( const idSWFScriptVar & other ) { 160 if ( type == other.type ) { 161 switch ( type ) { 162 case SWF_VAR_STRINGID: return ( value.i == other.value.i ); 163 case SWF_VAR_STRING: return ( *value.string == *other.value.string ); 164 case SWF_VAR_FLOAT: return ( value.f == other.value.f ); 165 case SWF_VAR_BOOL: return ( value.b == other.value.b ); 166 case SWF_VAR_INTEGER: return ( value.i == other.value.i ); 167 case SWF_VAR_NULL: return true; 168 case SWF_VAR_UNDEF: return true; 169 case SWF_VAR_OBJECT: return ( value.object == other.value.object ); 170 case SWF_VAR_FUNCTION: return ( value.function == other.value.function ); 171 default: assert( false ); return false; 172 } 173 } 174 switch ( type ) { 175 case SWF_VAR_STRINGID: return ToString() == other.ToString(); 176 case SWF_VAR_STRING: 177 switch ( other.type ) { 178 case SWF_VAR_STRINGID: return *value.string == other.ToString(); 179 case SWF_VAR_FLOAT: return ToFloat() == other.value.f; 180 case SWF_VAR_BOOL: return ToBool() == other.value.b; 181 case SWF_VAR_INTEGER: return ToInteger() == other.value.i; 182 case SWF_VAR_OBJECT: return *value.string == other.ToString(); 183 default: return false; 184 } 185 case SWF_VAR_FLOAT: return ( other.ToFloat() == value.f ); 186 case SWF_VAR_BOOL: return ( other.ToBool() == value.b ); 187 case SWF_VAR_INTEGER: return ( other.ToInteger() == value.i ); 188 case SWF_VAR_NULL: return ( other.type == SWF_VAR_UNDEF ); 189 case SWF_VAR_UNDEF: return ( other.type == SWF_VAR_NULL ); 190 case SWF_VAR_OBJECT: 191 switch ( other.type ) { 192 case SWF_VAR_STRING: return ToString() == *other.value.string; 193 case SWF_VAR_FLOAT: return ToFloat() == other.value.f; 194 case SWF_VAR_BOOL: return ToBool() == other.value.b; 195 case SWF_VAR_INTEGER: return ToInteger() == other.value.i; 196 default: return false; 197 } 198 case SWF_VAR_FUNCTION: return false; 199 default: assert( false ); return false; 200 } 201 } 202 203 /* 204 ======================== 205 idSWFScriptVar::ToString 206 ======================== 207 */ 208 idStr idSWFScriptVar::ToString() const { 209 switch ( type ) { 210 case SWF_VAR_STRINGID: return idStrId( value.i ).GetLocalizedString(); 211 case SWF_VAR_STRING: return *value.string; 212 213 case SWF_VAR_FLOAT: return va( "%g", value.f ); 214 case SWF_VAR_BOOL: return value.b ? "true" : "false"; 215 case SWF_VAR_INTEGER: return va( "%i", value.i ); 216 217 case SWF_VAR_NULL: return "[null]"; 218 case SWF_VAR_UNDEF: return "[undefined]"; 219 case SWF_VAR_OBJECT: return value.object->DefaultValue( true ).ToString(); 220 case SWF_VAR_FUNCTION: 221 if ( swf_debugShowAddress.GetBool() ) { 222 return va( "[function:%p]", value.function ); 223 } else { 224 return "[function]"; 225 } 226 default: assert( false ); return ""; 227 } 228 } 229 230 /* 231 ======================== 232 idSWFScriptVar::ToFloat 233 ======================== 234 */ 235 float idSWFScriptVar::ToFloat() const { 236 switch ( type ) { 237 case SWF_VAR_STRING: return atof( *value.string ); 238 239 case SWF_VAR_FLOAT: return value.f; 240 case SWF_VAR_BOOL: return (float)value.b; 241 case SWF_VAR_INTEGER: return (float)value.i; 242 243 case SWF_VAR_OBJECT: return value.object->DefaultValue( false ).ToFloat(); 244 245 case SWF_VAR_FUNCTION: 246 case SWF_VAR_NULL: 247 case SWF_VAR_UNDEF: return 0.0f; 248 default: assert( false ); return 0.0f; 249 } 250 } 251 252 /* 253 ======================== 254 idSWFScriptVar::ToBool 255 ======================== 256 */ 257 bool idSWFScriptVar::ToBool() const { 258 switch ( type ) { 259 case SWF_VAR_STRING: return ( value.string->Icmp( "true" ) == 0 || value.string->Icmp( "1" ) == 0 ); 260 261 case SWF_VAR_FLOAT: return ( value.f != 0.0f ); 262 case SWF_VAR_BOOL: return value.b; 263 case SWF_VAR_INTEGER: return value.i != 0; 264 265 case SWF_VAR_OBJECT: return value.object->DefaultValue( false ).ToBool(); 266 267 case SWF_VAR_FUNCTION: 268 case SWF_VAR_NULL: 269 case SWF_VAR_UNDEF: return false; 270 default: assert( false ); return false; 271 } 272 } 273 274 /* 275 ======================== 276 idSWFScriptVar::ToInteger 277 ======================== 278 */ 279 int32 idSWFScriptVar::ToInteger() const { 280 switch ( type ) { 281 case SWF_VAR_STRING: return atoi( *value.string ); 282 283 case SWF_VAR_FLOAT: return idMath::Ftoi( value.f ); 284 285 case SWF_VAR_BOOL: return value.b ? 1 : 0; 286 case SWF_VAR_INTEGER: return value.i; 287 288 case SWF_VAR_OBJECT: return value.object->DefaultValue( false ).ToInteger(); 289 290 case SWF_VAR_FUNCTION: 291 case SWF_VAR_NULL: 292 case SWF_VAR_UNDEF: return 0; 293 default: assert( false ); return 0; 294 } 295 } 296 297 /* 298 ======================== 299 idSWFScriptVar::ToSprite 300 ======================== 301 */ 302 idSWFSpriteInstance * idSWFScriptVar::ToSprite() { 303 if ( IsObject() && value.object != NULL ) { 304 return value.object->GetSprite(); 305 } 306 307 return NULL; 308 } 309 310 /* 311 ======================== 312 idSWFScriptVar::ToText 313 ======================== 314 */ 315 idSWFTextInstance * idSWFScriptVar::ToText() { 316 if ( IsObject() && value.object != NULL ) { 317 return value.object->GetText(); 318 } 319 320 return NULL; 321 } 322 323 /* 324 ======================== 325 idSWFScriptVar::GetNestedVar 326 ======================== 327 */ 328 idSWFScriptVar idSWFScriptVar::GetNestedVar( const char * arg1, const char * arg2, const char * arg3, const char * arg4, const char * arg5, const char * arg6 ) { 329 if ( !IsObject() ) { 330 return idSWFScriptVar(); 331 } 332 333 return GetObject()->GetNestedVar( arg1, arg2, arg3, arg4, arg5, arg6 ); 334 } 335 336 /* 337 ======================== 338 idSWFScriptVar::GetNestedObj 339 ======================== 340 */ 341 idSWFScriptObject * idSWFScriptVar::GetNestedObj( const char * arg1, const char * arg2, const char * arg3, const char * arg4, const char * arg5, const char * arg6 ) { 342 if ( !IsObject() ) { 343 return NULL; 344 } 345 346 return GetObject()->GetNestedObj( arg1, arg2, arg3, arg4, arg5, arg6 ); 347 } 348 349 /* 350 ======================== 351 idSWFScriptVar::GetNestedSprite 352 ======================== 353 */ 354 idSWFSpriteInstance * idSWFScriptVar::GetNestedSprite( const char * arg1, const char * arg2, const char * arg3, const char * arg4, const char * arg5, const char * arg6 ) { 355 if ( !IsObject() ) { 356 return NULL; 357 } 358 359 return GetObject()->GetNestedSprite( arg1, arg2, arg3, arg4, arg5, arg6 ); 360 } 361 362 /* 363 ======================== 364 idSWFScriptVar::GetNestedSprite 365 ======================== 366 */ 367 idSWFTextInstance * idSWFScriptVar::GetNestedText( const char * arg1, const char * arg2, const char * arg3, const char * arg4, const char * arg5, const char * arg6 ) { 368 if ( !IsObject() ) { 369 return NULL; 370 } 371 372 return GetObject()->GetNestedText( arg1, arg2, arg3, arg4, arg5, arg6 ); 373 } 374 375 /* 376 ======================== 377 idSWFScriptVar::TypeOf 378 ======================== 379 */ 380 const char * idSWFScriptVar::TypeOf() const { 381 switch ( type ) { 382 case SWF_VAR_STRINGID: return "stringid"; 383 case SWF_VAR_STRING: return "string"; 384 385 case SWF_VAR_FLOAT: return "number"; 386 case SWF_VAR_BOOL: return "boolean"; 387 case SWF_VAR_INTEGER: return "number"; 388 389 case SWF_VAR_OBJECT: 390 if ( value.object->GetSprite() != NULL ) { 391 return "movieclip"; 392 } else if ( value.object->GetText() != NULL ) { 393 return "text"; 394 } else { 395 return "object"; 396 } 397 398 case SWF_VAR_FUNCTION: return "function"; 399 case SWF_VAR_NULL: return "null"; 400 case SWF_VAR_UNDEF: return "undefined"; 401 default: assert( false ); return ""; 402 } 403 } 404 405 /* 406 ======================== 407 idSWFScriptVar::PrintToConsole 408 ======================== 409 */ 410 void idSWFScriptVar::PrintToConsole() const { 411 idLib::Printf( "Object type: %s\n", TypeOf() ); 412 413 if ( IsObject() ) { 414 GetObject()->PrintToConsole(); 415 } else if ( IsNumeric() ) { 416 idLib::Printf( "%d\n", ToInteger() ); 417 } else if ( IsString() ) { 418 idLib::Printf( "%s\n", ToString().c_str() ); 419 } else { 420 idLib::Printf( "unknown\n" ); 421 } 422 }