SWF_ScriptVar.cpp 12 KB

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