doom3-bfg/neo/swf/SWF_ScriptObject.cpp

697 lines
15 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
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.
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.
===========================================================================
*/
#pragma hdrstop
#include "../idlib/precompiled.h"
idCVar swf_debugShowAddress( "swf_debugShowAddress", "0", CVAR_BOOL, "shows addresses along with object types when they are serialized" );
/*
========================
idSWFScriptObject::swfNamedVar_t::~swfNamedVar_t
========================
*/
idSWFScriptObject::swfNamedVar_t::~swfNamedVar_t()
{
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFScriptObject::swfNamedVar_t::operator=
========================
*/
idSWFScriptObject::swfNamedVar_t& idSWFScriptObject::swfNamedVar_t::operator=( const swfNamedVar_t& other )
{
if( &other != this )
{
2012-11-26 18:58:24 +00:00
index = other.index;
name = other.name;
hashNext = other.hashNext;
value = other.value;
native = other.native;
flags = other.flags;
}
return *this;
}
/*
========================
idSWFScriptObject::idSWFScriptObject
========================
*/
idSWFScriptObject::idSWFScriptObject() : prototype( NULL ), refCount( 1 ), noAutoDelete( false ), objectType( SWF_OBJECT_OBJECT )
{
2012-11-26 18:58:24 +00:00
data.sprite = NULL;
data.text = NULL;
Clear();
refCount = 1;
}
/*
========================
idSWFScriptObject::~idSWFScriptObject
========================
*/
idSWFScriptObject::~idSWFScriptObject()
{
if( prototype != NULL )
{
2012-11-26 18:58:24 +00:00
prototype->Release();
}
}
/*
========================
idSWFScriptObject::Alloc
========================
*/
idSWFScriptObject* idSWFScriptObject::Alloc()
{
return new( TAG_SWF ) idSWFScriptObject;
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFScriptObject::AddRef
========================
*/
void idSWFScriptObject::AddRef()
{
2012-11-26 18:58:24 +00:00
refCount++;
}
/*
========================
idSWFScriptObject::Release
========================
*/
void idSWFScriptObject::Release()
{
if( --refCount == 0 && !noAutoDelete )
{
2012-11-26 18:58:24 +00:00
delete this;
}
}
/*
========================
idSWFScriptObject::Clear
========================
*/
void idSWFScriptObject::Clear()
{
2012-11-26 18:58:24 +00:00
variables.Clear();
for( int i = 0; i < VARIABLE_HASH_BUCKETS; i++ )
{
2012-11-26 18:58:24 +00:00
variablesHash[i] = -1;
}
}
/*
========================
idSWFScriptObject::HasProperty
========================
*/
bool idSWFScriptObject::HasProperty( const char* name )
{
2012-11-26 18:58:24 +00:00
return ( GetVariable( name, false ) != NULL );
}
/*
========================
idSWFScriptObject::HasValidProperty
========================
*/
bool idSWFScriptObject::HasValidProperty( const char* name )
{
idSWFScriptObject::swfNamedVar_t* const variable = GetVariable( name, false );
if( variable == NULL )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( variable->native != NULL )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar nv = variable->native->Get( this );
if( nv.IsNULL() || nv.IsUndefined() )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else
{
if( variable->value.IsNULL() || variable->value.IsUndefined() )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
return true;
}
/*
========================
idSWFScriptObject::Get
========================
*/
idSWFScriptVar idSWFScriptObject::Get( const char* name )
{
swfNamedVar_t* variable = GetVariable( name, false );
if( variable == NULL )
{
2012-11-26 18:58:24 +00:00
return idSWFScriptVar();
}
else
{
if( variable->native )
{
2012-11-26 18:58:24 +00:00
return variable->native->Get( this );
}
else
{
2012-11-26 18:58:24 +00:00
return variable->value;
}
}
}
/*
========================
idSWFScriptObject::Get
========================
*/
idSWFScriptVar idSWFScriptObject::Get( int index )
{
swfNamedVar_t* variable = GetVariable( index, false );
if( variable == NULL )
{
2012-11-26 18:58:24 +00:00
return idSWFScriptVar();
}
else
{
if( variable->native )
{
2012-11-26 18:58:24 +00:00
return variable->native->Get( this );
}
else
{
2012-11-26 18:58:24 +00:00
return variable->value;
}
}
}
/*
========================
idSWFScriptObject::GetSprite
========================
*/
idSWFSpriteInstance* idSWFScriptObject::GetSprite( int index )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = Get( index );
return var.ToSprite();
}
/*
========================
idSWFScriptObject::GetSprite
========================
*/
idSWFSpriteInstance* idSWFScriptObject::GetSprite( const char* name )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = Get( name );
return var.ToSprite();
}
/*
========================
idSWFScriptObject::GetObject
========================
*/
idSWFScriptObject* idSWFScriptObject::GetObject( int index )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = Get( index );
if( var.IsObject() )
{
2012-11-26 18:58:24 +00:00
return var.GetObject();
}
return NULL;
}
/*
========================
idSWFScriptObject::GetObject
========================
*/
idSWFScriptObject* idSWFScriptObject::GetObject( const char* name )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = Get( name );
if( var.IsObject() )
{
2012-11-26 18:58:24 +00:00
return var.GetObject();
}
return NULL;
}
/*
========================
idSWFScriptObject::GetText
========================
*/
idSWFTextInstance* idSWFScriptObject::GetText( int index )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = Get( index );
if( var.IsObject() )
{
2012-11-26 18:58:24 +00:00
return var.GetObject()->GetText();
}
return NULL;
}
/*
========================
idSWFScriptObject::GetText
========================
*/
idSWFTextInstance* idSWFScriptObject::GetText( const char* name )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = Get( name );
if( var.IsObject() )
{
2012-11-26 18:58:24 +00:00
return var.GetObject()->GetText();
}
return NULL;
}
/*
========================
idSWFScriptObject::Set
========================
*/
void idSWFScriptObject::Set( const char* name, const idSWFScriptVar& value )
{
if( objectType == SWF_OBJECT_ARRAY )
{
if( idStr::Cmp( name, "length" ) == 0 )
{
2012-11-26 18:58:24 +00:00
int newLength = value.ToInteger();
for( int i = 0; i < variables.Num(); i++ )
{
if( variables[i].index >= newLength )
{
2012-11-26 18:58:24 +00:00
variables.RemoveIndexFast( i );
i--;
}
}
// rebuild the hash table
for( int i = 0; i < VARIABLE_HASH_BUCKETS; i++ )
{
2012-11-26 18:58:24 +00:00
variablesHash[i] = -1;
}
for( int i = 0; i < variables.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
int hash = idStr::Hash( variables[i].name.c_str() ) & ( VARIABLE_HASH_BUCKETS - 1 );
variables[i].hashNext = variablesHash[hash];
variablesHash[hash] = i;
}
}
else
{
2012-11-26 18:58:24 +00:00
int iName = atoi( name );
if( iName > 0 || ( iName == 0 && idStr::Cmp( name, "0" ) == 0 ) )
{
swfNamedVar_t* lengthVar = GetVariable( "length", true );
if( lengthVar->value.ToInteger() <= iName )
{
2012-11-26 18:58:24 +00:00
lengthVar->value = idSWFScriptVar( iName + 1 );
}
}
}
}
swfNamedVar_t* variable = GetVariable( name, true );
if( variable->native )
{
2012-11-26 18:58:24 +00:00
variable->native->Set( this, value );
}
else if( ( variable->flags & SWF_VAR_FLAG_READONLY ) == 0 )
{
2012-11-26 18:58:24 +00:00
variable->value = value;
}
}
/*
========================
idSWFScriptObject::Set
========================
*/
void idSWFScriptObject::Set( int index, const idSWFScriptVar& value )
{
if( index < 0 )
{
2012-11-26 18:58:24 +00:00
extern idCVar swf_debug;
if( swf_debug.GetBool() )
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "SWF: Trying to set a negative array index.\n" );
}
return;
}
if( objectType == SWF_OBJECT_ARRAY )
{
swfNamedVar_t* lengthVar = GetVariable( "length", true );
if( lengthVar->value.ToInteger() <= index )
{
2012-11-26 18:58:24 +00:00
lengthVar->value = idSWFScriptVar( index + 1 );
}
}
swfNamedVar_t* variable = GetVariable( index, true );
if( variable->native )
{
2012-11-26 18:58:24 +00:00
variable->native->Set( this, value );
}
else if( ( variable->flags & SWF_VAR_FLAG_READONLY ) == 0 )
{
2012-11-26 18:58:24 +00:00
variable->value = value;
}
}
/*
========================
idSWFScriptObject::SetNative
========================
*/
void idSWFScriptObject::SetNative( const char* name, idSWFScriptNativeVariable* native )
{
swfNamedVar_t* variable = GetVariable( name, true );
2012-11-26 18:58:24 +00:00
variable->flags = SWF_VAR_FLAG_DONTENUM;
variable->native = native;
if( native->IsReadOnly() )
{
2012-11-26 18:58:24 +00:00
variable->flags |= SWF_VAR_FLAG_READONLY;
}
}
/*
========================
idSWFScriptObject::DefaultValue
========================
*/
idSWFScriptVar idSWFScriptObject::DefaultValue( bool stringHint )
{
const char* methods[2] = { "toString", "valueOf" };
if( !stringHint )
{
2012-11-26 18:58:24 +00:00
SwapValues( methods[0], methods[1] );
}
for( int i = 0; i < 2; i++ )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar method = Get( methods[i] );
if( method.IsFunction() )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar value = method.GetFunction()->Call( this, idSWFParmList() );
if( !value.IsObject() && !value.IsFunction() )
{
2012-11-26 18:58:24 +00:00
return value;
}
}
}
switch( objectType )
{
case SWF_OBJECT_OBJECT:
if( swf_debugShowAddress.GetBool() )
{
return idSWFScriptVar( va( "[object:%p]", this ) );
}
else
{
return idSWFScriptVar( "[object]" );
}
case SWF_OBJECT_ARRAY:
if( swf_debugShowAddress.GetBool() )
{
return idSWFScriptVar( va( "[array:%p]", this ) );
}
else
{
return idSWFScriptVar( "[array]" );
}
case SWF_OBJECT_SPRITE:
if( data.sprite != NULL )
{
if( data.sprite->parent == NULL )
{
return idSWFScriptVar( "[_root]" );
}
else
{
return idSWFScriptVar( va( "[%s]", data.sprite->GetName() ) );
}
}
else
{
return idSWFScriptVar( "[NULL]" );
}
case SWF_OBJECT_TEXT:
if( swf_debugShowAddress.GetBool() )
{
return idSWFScriptVar( va( "[edittext:%p]", this ) );
}
else
{
return idSWFScriptVar( "[edittext]" );
2012-11-26 18:58:24 +00:00
}
}
return idSWFScriptVar( "[unknown]" );
}
/*
========================
idSWFScriptObject::GetVariable
========================
*/
idSWFScriptObject::swfNamedVar_t* idSWFScriptObject::GetVariable( int index, bool create )
{
for( int i = 0; i < variables.Num(); i++ )
{
if( variables[i].index == index )
{
2012-11-26 18:58:24 +00:00
return &variables[i];
}
}
if( create )
{
swfNamedVar_t* variable = &variables.Alloc();
2012-11-26 18:58:24 +00:00
variable->flags = SWF_VAR_FLAG_NONE;
variable->index = index;
variable->name = va( "%d", index );
variable->native = NULL;
int hash = idStr::Hash( variable->name ) & ( VARIABLE_HASH_BUCKETS - 1 );
variable->hashNext = variablesHash[hash];
variablesHash[hash] = variables.Num() - 1;
return variable;
}
return NULL;
}
/*
========================
idSWFScriptObject::GetVariable
========================
*/
idSWFScriptObject::swfNamedVar_t* idSWFScriptObject::GetVariable( const char* name, bool create )
{
2012-11-26 18:58:24 +00:00
int hash = idStr::Hash( name ) & ( VARIABLE_HASH_BUCKETS - 1 );
for( int i = variablesHash[hash]; i >= 0; i = variables[i].hashNext )
{
if( variables[i].name == name )
{
2012-11-26 18:58:24 +00:00
return &variables[i];
}
}
if( prototype != NULL )
{
swfNamedVar_t* variable = prototype->GetVariable( name, false );
if( ( variable != NULL ) && ( variable->native || !create ) )
{
2012-11-26 18:58:24 +00:00
// If the variable is native, we want to pull it from the prototype even if we're going to set it
return variable;
}
}
if( create )
{
swfNamedVar_t* variable = &variables.Alloc();
2012-11-26 18:58:24 +00:00
variable->flags = SWF_VAR_FLAG_NONE;
variable->index = atoi( name );
if( variable->index == 0 && idStr::Cmp( name, "0" ) != 0 )
{
2012-11-26 18:58:24 +00:00
variable->index = -1;
}
variable->name = name;
variable->native = NULL;
variable->hashNext = variablesHash[hash];
variablesHash[hash] = variables.Num() - 1;
return variable;
}
return NULL;
}
/*
========================
idSWFScriptObject::MakeArray
========================
*/
void idSWFScriptObject::MakeArray()
{
2012-11-26 18:58:24 +00:00
objectType = SWF_OBJECT_ARRAY;
swfNamedVar_t* variable = GetVariable( "length", true );
2012-11-26 18:58:24 +00:00
variable->value = idSWFScriptVar( 0 );
variable->flags = SWF_VAR_FLAG_DONTENUM;
}
/*
========================
idSWFScriptObject::GetNestedVar
========================
*/
idSWFScriptVar idSWFScriptObject::GetNestedVar( const char* arg1, const char* arg2, const char* arg3, const char* arg4, const char* arg5, const char* arg6 )
{
const char* const args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
const int numArgs = sizeof( args ) / sizeof( const char* );
idStaticList< const char*, numArgs > vars;
for( int i = 0; i < numArgs && args[ i ] != NULL; ++i )
{
2012-11-26 18:58:24 +00:00
vars.Append( args[ i ] );
}
idSWFScriptObject* baseObject = this;
2012-11-26 18:58:24 +00:00
idSWFScriptVar retVal;
for( int i = 0; i < vars.Num(); ++i )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = baseObject->Get( vars[ i ] );
2012-11-26 18:58:24 +00:00
// when at the end of object path just use the latest value as result
if( i == vars.Num() - 1 )
{
2012-11-26 18:58:24 +00:00
retVal = var;
break;
}
2012-11-26 18:58:24 +00:00
// encountered variable in path that wasn't an object
if( !var.IsObject() )
{
2012-11-26 18:58:24 +00:00
retVal = idSWFScriptVar();
break;
}
2012-11-26 18:58:24 +00:00
baseObject = var.GetObject();
}
2012-11-26 18:58:24 +00:00
return retVal;
}
/*
========================
idSWFScriptObject::GetNestedObj
========================
*/
idSWFScriptObject* idSWFScriptObject::GetNestedObj( const char* arg1, const char* arg2, const char* arg3, const char* arg4, const char* arg5, const char* arg6 )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = GetNestedVar( arg1, arg2, arg3, arg4, arg5, arg6 );
if( !var.IsObject() )
{
2012-11-26 18:58:24 +00:00
return NULL;
}
2012-11-26 18:58:24 +00:00
return var.GetObject();
}
/*
========================
idSWFScriptObject::GetNestedSprite
========================
*/
idSWFSpriteInstance* idSWFScriptObject::GetNestedSprite( const char* arg1, const char* arg2, const char* arg3, const char* arg4, const char* arg5, const char* arg6 )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = GetNestedVar( arg1, arg2, arg3, arg4, arg5, arg6 );
return var.ToSprite();
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFScriptObject::GetNestedText
========================
*/
idSWFTextInstance* idSWFScriptObject::GetNestedText( const char* arg1, const char* arg2, const char* arg3, const char* arg4, const char* arg5, const char* arg6 )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = GetNestedVar( arg1, arg2, arg3, arg4, arg5, arg6 );
return var.ToText();
2012-11-26 18:58:24 +00:00
}
/*
========================
idSWFScriptObject::PrintToConsole
========================
*/
void idSWFScriptObject::PrintToConsole() const
{
if( variables.Num() > 0 )
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "%d subelements:\n", variables.Num() );
int maxVarLength = 0;
for( int i = 0; i < variables.Num(); ++i )
{
const idSWFScriptObject::swfNamedVar_t& nv = variables[ i ];
2012-11-26 18:58:24 +00:00
const int nameLength = idStr::Length( nv.name );
if( maxVarLength < nameLength )
{
2012-11-26 18:58:24 +00:00
maxVarLength = nameLength;
}
}
2012-11-26 18:58:24 +00:00
maxVarLength += 2; // a little extra padding
const char* const fmt = va( "%%-%ds %%-10s %%-s\n", maxVarLength );
2012-11-26 18:58:24 +00:00
idLib::Printf( fmt, "Name", "Type", "Value" );
idLib::Printf( "------------------------------------------------------------\n" );
for( int i = 0; i < variables.Num(); ++i )
{
const idSWFScriptObject::swfNamedVar_t& nv = variables[ i ];
2012-11-26 18:58:24 +00:00
idLib::Printf( fmt, nv.name.c_str(), nv.value.TypeOf(),
nv.value.ToString().c_str() );
2012-11-26 18:58:24 +00:00
}
}
else
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "No subelements\n" );
}
}