2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-12-07 17:29:47 +00:00
|
|
|
Copyright (C) 2012 Robert Beckebans
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +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
|
2012-12-22 15:18:19 +00:00
|
|
|
#include "precompiled.h"
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "../Game_local.h"
|
|
|
|
|
|
|
|
// simple types. function types are dynamically allocated
|
|
|
|
idTypeDef type_void( ev_void, &def_void, "void", 0, NULL );
|
2012-12-07 17:29:47 +00:00
|
|
|
|
|
|
|
// RB: 64 bit fixes, changed all pointer types to intptr_t
|
|
|
|
idTypeDef type_scriptevent( ev_scriptevent, &def_scriptevent, "scriptevent", sizeof( intptr_t ), NULL );
|
|
|
|
idTypeDef type_namespace( ev_namespace, &def_namespace, "namespace", sizeof( intptr_t ), NULL );
|
2012-11-26 18:58:24 +00:00
|
|
|
idTypeDef type_string( ev_string, &def_string, "string", MAX_STRING_LEN, NULL );
|
2012-12-07 17:29:47 +00:00
|
|
|
idTypeDef type_float( ev_float, &def_float, "float", sizeof( intptr_t ), NULL );
|
|
|
|
idTypeDef type_vector( ev_vector, &def_vector, "vector", E_EVENT_SIZEOF_VEC, NULL );
|
|
|
|
idTypeDef type_entity( ev_entity, &def_entity, "entity", sizeof( intptr_t ), NULL ); // stored as entity number pointer
|
|
|
|
idTypeDef type_field( ev_field, &def_field, "field", sizeof( intptr_t ), NULL );
|
|
|
|
idTypeDef type_function( ev_function, &def_function, "function", sizeof( intptr_t ), &type_void );
|
|
|
|
idTypeDef type_virtualfunction( ev_virtualfunction, &def_virtualfunction, "virtual function", sizeof( intptr_t ), NULL );
|
|
|
|
idTypeDef type_pointer( ev_pointer, &def_pointer, "pointer", sizeof( intptr_t ), NULL );
|
|
|
|
idTypeDef type_object( ev_object, &def_object, "object", sizeof( intptr_t ), NULL ); // stored as entity number pointer
|
|
|
|
idTypeDef type_jumpoffset( ev_jumpoffset, &def_jumpoffset, "<jump>", sizeof( intptr_t ), NULL ); // only used for jump opcodes
|
|
|
|
idTypeDef type_argsize( ev_argsize, &def_argsize, "<argsize>", sizeof( intptr_t ), NULL ); // only used for function call and thread opcodes
|
|
|
|
idTypeDef type_boolean( ev_boolean, &def_boolean, "boolean", sizeof( intptr_t ), NULL );
|
|
|
|
// RB end
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
idVarDef def_void( &type_void );
|
|
|
|
idVarDef def_scriptevent( &type_scriptevent );
|
|
|
|
idVarDef def_namespace( &type_namespace );
|
|
|
|
idVarDef def_string( &type_string );
|
|
|
|
idVarDef def_float( &type_float );
|
|
|
|
idVarDef def_vector( &type_vector );
|
|
|
|
idVarDef def_entity( &type_entity );
|
|
|
|
idVarDef def_field( &type_field );
|
|
|
|
idVarDef def_function( &type_function );
|
|
|
|
idVarDef def_virtualfunction( &type_virtualfunction );
|
|
|
|
idVarDef def_pointer( &type_pointer );
|
|
|
|
idVarDef def_object( &type_object );
|
|
|
|
idVarDef def_jumpoffset( &type_jumpoffset ); // only used for jump opcodes
|
|
|
|
idVarDef def_argsize( &type_argsize );
|
|
|
|
idVarDef def_boolean( &type_boolean );
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
function_t
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
function_t::function_t
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
function_t::function_t()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
function_t::Allocated
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
size_t function_t::Allocated() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return name.Allocated() + parmSize.Allocated();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
function_t::SetName
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void function_t::SetName( const char* name )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
this->name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
function_t::Name
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* function_t::Name() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
function_t::Clear
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void function_t::Clear()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
eventdef = NULL;
|
|
|
|
def = NULL;
|
|
|
|
type = NULL;
|
|
|
|
firstStatement = 0;
|
|
|
|
numStatements = 0;
|
|
|
|
parmTotal = 0;
|
|
|
|
locals = 0;
|
|
|
|
filenum = 0;
|
|
|
|
name.Clear();
|
|
|
|
parmSize.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
idTypeDef
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::idTypeDef
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef::idTypeDef( etype_t etype, idVarDef* edef, const char* ename, int esize, idTypeDef* aux )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
name = ename;
|
|
|
|
type = etype;
|
|
|
|
def = edef;
|
|
|
|
size = esize;
|
|
|
|
auxType = aux;
|
|
|
|
|
|
|
|
parmTypes.SetGranularity( 1 );
|
|
|
|
parmNames.SetGranularity( 1 );
|
|
|
|
functions.SetGranularity( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::idTypeDef
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef::idTypeDef( const idTypeDef& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::operator=
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::operator=( const idTypeDef& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
type = other.type;
|
|
|
|
def = other.def;
|
|
|
|
name = other.name;
|
|
|
|
size = other.size;
|
|
|
|
auxType = other.auxType;
|
|
|
|
parmTypes = other.parmTypes;
|
|
|
|
parmNames = other.parmNames;
|
|
|
|
functions = other.functions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::Allocated
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
size_t idTypeDef::Allocated() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size_t memsize;
|
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
memsize = name.Allocated() + parmTypes.Allocated() + parmNames.Allocated() + functions.Allocated();
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < parmTypes.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
memsize += parmNames[ i ].Allocated();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return memsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::Inherits
|
|
|
|
|
|
|
|
Returns true if basetype is an ancestor of this type.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idTypeDef::Inherits( const idTypeDef* basetype ) const
|
|
|
|
{
|
|
|
|
idTypeDef* superType;
|
|
|
|
|
|
|
|
if( type != ev_object )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( this == basetype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( superType = auxType; superType != NULL; superType = superType->auxType )
|
|
|
|
{
|
|
|
|
if( superType == basetype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::MatchesType
|
|
|
|
|
|
|
|
Returns true if both types' base types and parameters match
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idTypeDef::MatchesType( const idTypeDef& matchtype ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( this == &matchtype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ( type != matchtype.type ) || ( auxType != matchtype.auxType ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( parmTypes.Num() != matchtype.parmTypes.Num() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < matchtype.parmTypes.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( parmTypes[ i ] != matchtype.parmTypes[ i ] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::MatchesVirtualFunction
|
|
|
|
|
|
|
|
Returns true if both functions' base types and parameters match
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idTypeDef::MatchesVirtualFunction( const idTypeDef& matchfunc ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( this == &matchfunc )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ( type != matchfunc.type ) || ( auxType != matchfunc.auxType ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( parmTypes.Num() != matchfunc.parmTypes.Num() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( parmTypes.Num() > 0 )
|
|
|
|
{
|
|
|
|
if( !parmTypes[ 0 ]->Inherits( matchfunc.parmTypes[ 0 ] ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 1; i < matchfunc.parmTypes.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( parmTypes[ i ] != matchfunc.parmTypes[ i ] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::AddFunctionParm
|
|
|
|
|
|
|
|
Adds a new parameter for a function type.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::AddFunctionParm( idTypeDef* parmtype, const char* name )
|
|
|
|
{
|
|
|
|
if( type != ev_function )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::AddFunctionParm : tried to add parameter on non-function type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::AddFunctionParm : tried to add parameter on non-function type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
parmTypes.Append( parmtype );
|
2012-11-28 15:47:07 +00:00
|
|
|
idStr& parmName = parmNames.Alloc();
|
2012-11-26 18:58:24 +00:00
|
|
|
parmName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::AddField
|
|
|
|
|
|
|
|
Adds a new field to an object type.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::AddField( idTypeDef* fieldtype, const char* name )
|
|
|
|
{
|
|
|
|
if( type != ev_object )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::AddField : tried to add field to non-object type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::AddField : tried to add field to non-object type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
parmTypes.Append( fieldtype );
|
2012-11-28 15:47:07 +00:00
|
|
|
idStr& parmName = parmNames.Alloc();
|
2012-11-26 18:58:24 +00:00
|
|
|
parmName = name;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( fieldtype->FieldType()->Inherits( &type_object ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size += type_object.Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size += fieldtype->FieldType()->Size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::SetName
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::SetName( const char* newname )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
name = newname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::Name
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idTypeDef::Name() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::Type
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
etype_t idTypeDef::Type() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::Size
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTypeDef::Size() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::SuperClass
|
|
|
|
|
|
|
|
If type is an object, then returns the object's superclass
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idTypeDef::SuperClass() const
|
|
|
|
{
|
|
|
|
if( type != ev_object )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::SuperClass : tried to get superclass of a non-object type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::SuperClass : tried to get superclass of a non-object type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return auxType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::ReturnType
|
|
|
|
|
|
|
|
If type is a function, then returns the function's return type
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idTypeDef::ReturnType() const
|
|
|
|
{
|
|
|
|
if( type != ev_function )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::ReturnType: tried to get return type on non-function type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::ReturnType: tried to get return type on non-function type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return auxType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::SetReturnType
|
|
|
|
|
|
|
|
If type is a function, then sets the function's return type
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::SetReturnType( idTypeDef* returntype )
|
|
|
|
{
|
|
|
|
if( type != ev_function )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::SetReturnType: tried to set return type on non-function type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::SetReturnType: tried to set return type on non-function type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
auxType = returntype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::FieldType
|
|
|
|
|
|
|
|
If type is a field, then returns it's type
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idTypeDef::FieldType() const
|
|
|
|
{
|
|
|
|
if( type != ev_field )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::FieldType: tried to get field type on non-field type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::FieldType: tried to get field type on non-field type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return auxType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::SetFieldType
|
|
|
|
|
|
|
|
If type is a field, then sets the function's return type
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::SetFieldType( idTypeDef* fieldtype )
|
|
|
|
{
|
|
|
|
if( type != ev_field )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::SetFieldType: tried to set return type on non-function type" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::SetFieldType: tried to set return type on non-function type" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
auxType = fieldtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::PointerType
|
|
|
|
|
|
|
|
If type is a pointer, then returns the type it points to
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idTypeDef::PointerType() const
|
|
|
|
{
|
|
|
|
if( type != ev_pointer )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::PointerType: tried to get pointer type on non-pointer" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::PointerType: tried to get pointer type on non-pointer" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return auxType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::SetPointerType
|
|
|
|
|
|
|
|
If type is a pointer, then sets the pointer's type
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::SetPointerType( idTypeDef* pointertype )
|
|
|
|
{
|
|
|
|
if( type != ev_pointer )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( "idTypeDef::SetPointerType: tried to set type on non-pointer" );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "idTypeDef::SetPointerType: tried to set type on non-pointer" );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
auxType = pointertype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::NumParameters
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTypeDef::NumParameters() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return parmTypes.Num();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::GetParmType
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idTypeDef::GetParmType( int parmNumber ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( parmNumber >= 0 );
|
|
|
|
assert( parmNumber < parmTypes.Num() );
|
|
|
|
return parmTypes[ parmNumber ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::GetParmName
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idTypeDef::GetParmName( int parmNumber ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( parmNumber >= 0 );
|
|
|
|
assert( parmNumber < parmTypes.Num() );
|
|
|
|
return parmNames[ parmNumber ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::NumFunctions
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTypeDef::NumFunctions() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return functions.Num();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::GetFunctionNumber
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTypeDef::GetFunctionNumber( const function_t* func ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < functions.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( functions[ i ] == func )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::GetFunction
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const function_t* idTypeDef::GetFunction( int funcNumber ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( funcNumber >= 0 );
|
|
|
|
assert( funcNumber < functions.Num() );
|
|
|
|
return functions[ funcNumber ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idTypeDef::AddFunction
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTypeDef::AddFunction( const function_t* func )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < functions.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( !strcmp( functions[ i ]->def->Name(), func->def->Name() ) )
|
|
|
|
{
|
|
|
|
if( func->def->TypeDef()->MatchesVirtualFunction( *functions[ i ]->def->TypeDef() ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
functions[ i ] = func;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
functions.Append( func );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
idVarDef
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idVarDef::idVarDef()
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef::idVarDef( idTypeDef* typeptr )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
typeDef = typeptr;
|
|
|
|
num = 0;
|
|
|
|
scope = NULL;
|
|
|
|
numUsers = 0;
|
|
|
|
initialized = idVarDef::uninitialized;
|
|
|
|
memset( &value, 0, sizeof( value ) );
|
|
|
|
name = NULL;
|
|
|
|
next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::~idVarDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef::~idVarDef()
|
|
|
|
{
|
|
|
|
if( name )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
name->RemoveDef( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::Name
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idVarDef::Name() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return name->Name();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::GlobalName
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idVarDef::GlobalName() const
|
|
|
|
{
|
|
|
|
if( scope != &def_namespace )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return va( "%s::%s", scope->GlobalName(), name->Name() );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return name->Name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::DepthOfScope
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idVarDef::DepthOfScope( const idVarDef* otherScope ) const
|
|
|
|
{
|
|
|
|
const idVarDef* def;
|
2012-11-26 18:58:24 +00:00
|
|
|
int depth;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
depth = 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( def = otherScope; def != NULL; def = def->scope )
|
|
|
|
{
|
|
|
|
if( def == scope )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
depth++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::SetFunction
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDef::SetFunction( function_t* func )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( typeDef );
|
|
|
|
initialized = initializedConstant;
|
|
|
|
assert( typeDef->Type() == ev_function );
|
|
|
|
value.functionPtr = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::SetObject
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDef::SetObject( idScriptObject* object )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( typeDef );
|
|
|
|
initialized = initialized;
|
|
|
|
assert( typeDef->Inherits( &type_object ) );
|
|
|
|
*value.objectPtrPtr = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::SetValue
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDef::SetValue( const eval_t& _value, bool constant )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( typeDef );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( constant )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
initialized = initializedConstant;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
initialized = initializedVariable;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
switch( typeDef->Type() )
|
|
|
|
{
|
|
|
|
case ev_pointer :
|
|
|
|
case ev_boolean :
|
|
|
|
case ev_field :
|
|
|
|
*value.intPtr = _value._int;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_jumpoffset :
|
|
|
|
value.jumpOffset = _value._int;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_argsize :
|
|
|
|
value.argSize = _value._int;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_entity :
|
|
|
|
*value.entityNumberPtr = _value.entity;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_string :
|
|
|
|
idStr::Copynz( value.stringPtr, _value.stringPtr, MAX_STRING_LEN );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_float :
|
|
|
|
*value.floatPtr = _value._float;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_vector :
|
|
|
|
value.vectorPtr->x = _value.vector[ 0 ];
|
|
|
|
value.vectorPtr->y = _value.vector[ 1 ];
|
|
|
|
value.vectorPtr->z = _value.vector[ 2 ];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_function :
|
|
|
|
value.functionPtr = _value.function;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_virtualfunction :
|
|
|
|
value.virtualFunction = _value._int;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_object :
|
|
|
|
*value.entityNumberPtr = _value.entity;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-28 15:47:07 +00:00
|
|
|
throw idCompileError( va( "weird type on '%s'", Name() ) );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "weird type on '%s'", Name() );
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::SetString
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDef::SetString( const char* string, bool constant )
|
|
|
|
{
|
|
|
|
if( constant )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
initialized = initializedConstant;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
initialized = initializedVariable;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( typeDef && ( typeDef->Type() == ev_string ) );
|
|
|
|
idStr::Copynz( value.stringPtr, string, MAX_STRING_LEN );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDef::PrintInfo
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDef::PrintInfo( idFile* file, int instructionPointer ) const
|
|
|
|
{
|
|
|
|
statement_t* jumpst;
|
2012-11-26 18:58:24 +00:00
|
|
|
int jumpto;
|
|
|
|
etype_t etype;
|
|
|
|
int i;
|
|
|
|
int len;
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* ch;
|
|
|
|
|
|
|
|
if( initialized == initializedConstant )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "const " );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
etype = typeDef->Type();
|
2012-11-28 15:47:07 +00:00
|
|
|
switch( etype )
|
|
|
|
{
|
|
|
|
case ev_jumpoffset :
|
|
|
|
jumpto = instructionPointer + value.jumpOffset;
|
|
|
|
jumpst = &gameLocal.program.GetStatement( jumpto );
|
|
|
|
file->Printf( "address %d [%s(%d)]", jumpto, gameLocal.program.GetFilename( jumpst->file ), jumpst->linenumber );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_function :
|
|
|
|
if( value.functionPtr->eventdef )
|
|
|
|
{
|
|
|
|
file->Printf( "event %s", GlobalName() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file->Printf( "function %s", GlobalName() );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_field :
|
|
|
|
file->Printf( "field %d", value.ptrOffset );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_argsize:
|
|
|
|
file->Printf( "args %d", value.argSize );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
file->Printf( "%s ", typeDef->Name() );
|
|
|
|
if( initialized == initializedConstant )
|
|
|
|
{
|
|
|
|
switch( etype )
|
|
|
|
{
|
|
|
|
case ev_string :
|
|
|
|
file->Printf( "\"" );
|
|
|
|
len = strlen( value.stringPtr );
|
|
|
|
ch = value.stringPtr;
|
|
|
|
for( i = 0; i < len; i++, ch++ )
|
|
|
|
{
|
|
|
|
if( idStr::CharIsPrintable( *ch ) )
|
|
|
|
{
|
|
|
|
file->Printf( "%c", *ch );
|
|
|
|
}
|
|
|
|
else if( *ch == '\n' )
|
|
|
|
{
|
|
|
|
file->Printf( "\\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file->Printf( "\\x%.2x", static_cast<int>( *ch ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file->Printf( "\"" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_vector :
|
|
|
|
file->Printf( "'%s'", value.vectorPtr->ToString() );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_float :
|
|
|
|
file->Printf( "%f", *value.floatPtr );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ev_virtualfunction :
|
|
|
|
file->Printf( "vtable[ %d ]", value.virtualFunction );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
file->Printf( "%d", *value.intPtr );
|
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( initialized == stackVariable )
|
|
|
|
{
|
|
|
|
file->Printf( "stack[%d]", value.stackOffset );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file->Printf( "global[%d]", num );
|
|
|
|
}
|
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
idVarDef
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDefName::AddDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDefName::AddDef( idVarDef* def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( def->next == NULL );
|
|
|
|
def->name = this;
|
|
|
|
def->next = defs;
|
|
|
|
defs = def;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idVarDefName::RemoveDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idVarDefName::RemoveDef( idVarDef* def )
|
|
|
|
{
|
|
|
|
if( defs == def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
defs = def->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( idVarDef* d = defs; d->next != NULL; d = d->next )
|
|
|
|
{
|
|
|
|
if( d->next == def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
d->next = def->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
def->next = NULL;
|
|
|
|
def->name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
idScriptObject
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::idScriptObject
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idScriptObject::idScriptObject()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
data = NULL;
|
|
|
|
type = &type_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::~idScriptObject
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idScriptObject::~idScriptObject()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::Free
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idScriptObject::Free()
|
|
|
|
{
|
|
|
|
if( data )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( data );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
data = NULL;
|
|
|
|
type = &type_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idScriptObject::Save
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idScriptObject::Save( idSaveGame* savefile ) const
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB: 64 bit fix, changed size_t to int
|
|
|
|
int size;
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( type == &type_object && data == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// Write empty string for uninitialized object
|
|
|
|
savefile->WriteString( "" );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->WriteString( type->Name() );
|
|
|
|
size = type->Size();
|
|
|
|
savefile->WriteInt( size );
|
|
|
|
savefile->Write( data, size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idScriptObject::Restore
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idScriptObject::Restore( idRestoreGame* savefile )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr typeName;
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB: 64 bit fix, changed size_t to int
|
2012-11-26 18:58:24 +00:00
|
|
|
int size;
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadString( typeName );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// Empty string signals uninitialized object
|
2012-11-28 15:47:07 +00:00
|
|
|
if( typeName.Length() == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !SetType( typeName ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->Error( "idScriptObject::Restore: failed to restore object of type '%s'.", typeName.c_str() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB: 64 bit fix, changed size_t to int
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadInt( size );
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
if( size != type->Size() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->Error( "idScriptObject::Restore: size of object '%s' doesn't match size in save game.", typeName.c_str() );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->Read( data, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::SetType
|
|
|
|
|
|
|
|
Allocates an object and initializes memory.
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idScriptObject::SetType( const char* typeName )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size_t size;
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* newtype;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// lookup the type
|
|
|
|
newtype = gameLocal.program.FindType( typeName );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// only allocate memory if the object type changes
|
2012-11-28 15:47:07 +00:00
|
|
|
if( newtype != type )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Free();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !newtype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Warning( "idScriptObject::SetType: Unknown type '%s'", typeName );
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !newtype->Inherits( &type_object ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Warning( "idScriptObject::SetType: Can't create object of type '%s'. Must be an object type.", newtype->Name() );
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// set the type
|
|
|
|
type = newtype;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// allocate the memory
|
|
|
|
size = type->Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
data = ( byte* )Mem_Alloc( size, TAG_SCRIPT );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// init object memory
|
|
|
|
ClearObject();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::ClearObject
|
|
|
|
|
|
|
|
Resets the memory for the script object without changing its type.
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idScriptObject::ClearObject()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
size_t size;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( type != &type_object )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// init object memory
|
|
|
|
size = type->Size();
|
|
|
|
memset( data, 0, size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::HasObject
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idScriptObject::HasObject() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return ( type != &type_object );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::GetTypeDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idScriptObject::GetTypeDef() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::GetTypeName
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idScriptObject::GetTypeName() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return type->Name();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::GetConstructor
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const function_t* idScriptObject::GetConstructor() const
|
|
|
|
{
|
|
|
|
const function_t* func;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
func = GetFunction( "init" );
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::GetDestructor
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const function_t* idScriptObject::GetDestructor() const
|
|
|
|
{
|
|
|
|
const function_t* func;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
func = GetFunction( "destroy" );
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::GetFunction
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const function_t* idScriptObject::GetFunction( const char* name ) const
|
|
|
|
{
|
|
|
|
const function_t* func;
|
|
|
|
|
|
|
|
if( type == &type_object )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
func = gameLocal.program.FindFunction( name, type );
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idScriptObject::GetVariable
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
byte* idScriptObject::GetVariable( const char* name, etype_t etype ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
int pos;
|
2012-11-28 15:47:07 +00:00
|
|
|
const idTypeDef* t = type;
|
|
|
|
const idTypeDef* parm;
|
|
|
|
|
|
|
|
if( t == &type_object || t == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( t->SuperClass() != &type_object )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pos = t->SuperClass()->Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pos = 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < t->NumParameters(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
parm = t->GetParmType( i );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !strcmp( t->GetParmName( i ), name ) )
|
|
|
|
{
|
|
|
|
if( etype != parm->FieldType()->Type() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &data[ pos ];
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( parm->FieldType()->Inherits( &type_object ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pos += type_object.Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pos += parm->FieldType()->Size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t = t->SuperClass();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
while( t != NULL && ( t != &type_object ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
idProgram
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::AllocType
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idProgram::AllocType( idTypeDef& type )
|
|
|
|
{
|
|
|
|
idTypeDef* newtype = new( TAG_SCRIPT ) idTypeDef( type );
|
2012-11-26 18:58:24 +00:00
|
|
|
typesHash.Add( idStr::Hash( type.Name() ), types.Append( newtype ) );
|
|
|
|
return newtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::AllocType
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idProgram::AllocType( etype_t etype, idVarDef* edef, const char* ename, int esize, idTypeDef* aux )
|
|
|
|
{
|
|
|
|
idTypeDef* newtype = new( TAG_SCRIPT ) idTypeDef( etype, edef, ename, esize, aux );
|
2012-11-26 18:58:24 +00:00
|
|
|
typesHash.Add( idStr::Hash( ename ), types.Append( newtype ) );
|
|
|
|
return newtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::GetType
|
|
|
|
|
|
|
|
Returns a preexisting complex type that matches the parm, or allocates
|
|
|
|
a new one and copies it out.
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idProgram::GetType( idTypeDef& type, bool allocate )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = typesHash.First( idStr::Hash( type.Name() ) ); i != -1; i = typesHash.Next( i ) )
|
|
|
|
{
|
|
|
|
if( types[ i ]->MatchesType( type ) && !strcmp( types[ i ]->Name(), type.Name() ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return types[ i ];
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !allocate )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// allocate a new one
|
|
|
|
return AllocType( type );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::FindType
|
|
|
|
|
|
|
|
Returns a preexisting complex type that matches the name, or returns NULL if not found
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idTypeDef* idProgram::FindType( const char* name )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = typesHash.First( idStr::Hash( name ) ); i != -1; i = typesHash.Next( i ) )
|
|
|
|
{
|
|
|
|
idTypeDef* check = types[ i ];
|
|
|
|
if( !strcmp( check->Name(), name ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return check;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::GetDefList
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* idProgram::GetDefList( const char* name ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, hash;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
hash = varDefNameHash.GenerateKey( name, true );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = varDefNameHash.First( hash ); i != -1; i = varDefNameHash.Next( i ) )
|
|
|
|
{
|
|
|
|
if( idStr::Cmp( varDefNames[i]->Name(), name ) == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return varDefNames[i]->GetDefs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::AddDefToNameList
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::AddDefToNameList( idVarDef* def, const char* name )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, hash;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
hash = varDefNameHash.GenerateKey( name, true );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = varDefNameHash.First( hash ); i != -1; i = varDefNameHash.Next( i ) )
|
|
|
|
{
|
|
|
|
if( idStr::Cmp( varDefNames[i]->Name(), name ) == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( i == -1 )
|
|
|
|
{
|
|
|
|
i = varDefNames.Append( new( TAG_SCRIPT ) idVarDefName( name ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
varDefNameHash.Add( hash, i );
|
|
|
|
}
|
|
|
|
varDefNames[i]->AddDef( def );
|
|
|
|
}
|
|
|
|
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB: moved from AllocDef
|
|
|
|
byte* idProgram::ReserveDefMemory( int size )
|
|
|
|
{
|
|
|
|
byte* mem = &variables[ numVariables ];
|
|
|
|
numVariables += size;
|
|
|
|
if( numVariables > sizeof( variables ) )
|
|
|
|
{
|
|
|
|
#if defined(USE_EXCEPTIONS)
|
|
|
|
throw idCompileError( va( "Exceeded global memory size (%zd bytes)", sizeof( variables ) ) );
|
|
|
|
#else
|
|
|
|
gameLocal.Error( "Exceeded global memory size (%zd bytes)", sizeof( variables ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( mem, 0, size );
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
// RB end
|
|
|
|
|
|
|
|
// RB: moved from AllocDef
|
|
|
|
idVarDef* idProgram::AllocVarDef( idTypeDef* type, const char* name, idVarDef* scope )
|
|
|
|
{
|
|
|
|
// allocate a new def
|
|
|
|
idVarDef* def = new( TAG_SCRIPT ) idVarDef( type );
|
|
|
|
def->scope = scope;
|
|
|
|
def->numUsers = 1;
|
|
|
|
def->num = varDefs.Append( def );
|
|
|
|
|
|
|
|
// add the def to the list with defs with this name and set the name pointer
|
|
|
|
AddDefToNameList( def, name );
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
// RB end
|
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::AllocDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* idProgram::AllocDef( idTypeDef* type, const char* name, idVarDef* scope, bool constant )
|
|
|
|
{
|
|
|
|
idVarDef* def;
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr element;
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* def_x;
|
|
|
|
idVarDef* def_y;
|
|
|
|
idVarDef* def_z;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// allocate a new def
|
2012-12-07 17:29:47 +00:00
|
|
|
def = AllocVarDef( type, name, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ( type->Type() == ev_vector ) || ( ( type->Type() == ev_field ) && ( type->FieldType()->Type() == ev_vector ) ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
|
|
|
// vector
|
|
|
|
//
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !strcmp( name, RESULT_STRING ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// <RESULT> vector defs don't need the _x, _y and _z components
|
|
|
|
assert( scope->Type() == ev_function );
|
|
|
|
def->value.stackOffset = scope->value.functionPtr->locals;
|
|
|
|
def->initialized = idVarDef::stackVariable;
|
|
|
|
scope->value.functionPtr->locals += type->Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( scope->TypeDef()->Inherits( &type_object ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idTypeDef newtype( ev_field, NULL, "float field", 0, &type_float );
|
2012-12-07 17:29:47 +00:00
|
|
|
|
|
|
|
// RB: changed local type to ftype
|
|
|
|
idTypeDef* ftype = GetType( newtype, true );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// set the value to the variable's position in the object
|
|
|
|
def->value.ptrOffset = scope->TypeDef()->Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// make automatic defs for the vectors elements
|
|
|
|
// origin can be accessed as origin_x, origin_y, and origin_z
|
|
|
|
sprintf( element, "%s_x", def->Name() );
|
2012-12-07 17:29:47 +00:00
|
|
|
def_x = AllocDef( ftype, element, scope, constant );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( element, "%s_y", def->Name() );
|
2012-12-07 17:29:47 +00:00
|
|
|
def_y = AllocDef( ftype, element, scope, constant );
|
|
|
|
def_y->value.ptrOffset = def_x->value.ptrOffset + sizeof( float );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( element, "%s_z", def->Name() );
|
2012-12-07 17:29:47 +00:00
|
|
|
def_z = AllocDef( ftype, element, scope, constant );
|
|
|
|
def_z->value.ptrOffset = def_y->value.ptrOffset + sizeof( float );
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB: from dhewm3
|
|
|
|
idTypeDef newtype( ev_float, &def_float, "vector float", 0, NULL );
|
|
|
|
idTypeDef* ftype = GetType( newtype, true );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// make automatic defs for the vectors elements
|
|
|
|
// origin can be accessed as origin_x, origin_y, and origin_z
|
|
|
|
sprintf( element, "%s_x", def->Name() );
|
2012-12-07 17:29:47 +00:00
|
|
|
def_x = AllocVarDef( ftype, element, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( element, "%s_y", def->Name() );
|
2012-12-07 17:29:47 +00:00
|
|
|
def_y = AllocVarDef( ftype, element, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( element, "%s_z", def->Name() );
|
2012-12-07 17:29:47 +00:00
|
|
|
def_z = AllocVarDef( ftype, element, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 17:29:47 +00:00
|
|
|
// get the memory for the full vector and point the _x, _y and _z
|
|
|
|
// defs at the vector member offsets
|
|
|
|
if( scope->Type() == ev_function )
|
|
|
|
{
|
|
|
|
// vector on stack
|
|
|
|
def->value.stackOffset = scope->value.functionPtr->locals;
|
|
|
|
def->initialized = idVarDef::stackVariable;
|
|
|
|
scope->value.functionPtr->locals += type->Size();
|
|
|
|
|
|
|
|
def_x->value.stackOffset = def->value.stackOffset;
|
|
|
|
def_y->value.stackOffset = def_x->value.stackOffset + sizeof( float );
|
|
|
|
def_z->value.stackOffset = def_y->value.stackOffset + sizeof( float );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// global vector
|
|
|
|
def->value.bytePtr = ReserveDefMemory( type->Size() );
|
|
|
|
def_x->value.bytePtr = def->value.bytePtr;
|
|
|
|
def_y->value.bytePtr = def_x->value.bytePtr + sizeof( float );
|
|
|
|
def_z->value.bytePtr = def_y->value.bytePtr + sizeof( float );
|
|
|
|
}
|
|
|
|
|
|
|
|
def_x->initialized = def->initialized;
|
|
|
|
def_y->initialized = def->initialized;
|
|
|
|
def_z->initialized = def->initialized;
|
|
|
|
// RB end
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( scope->TypeDef()->Inherits( &type_object ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
|
|
|
// object variable
|
|
|
|
//
|
|
|
|
// set the value to the variable's position in the object
|
|
|
|
def->value.ptrOffset = scope->TypeDef()->Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( scope->Type() == ev_function )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
|
|
|
// stack variable
|
|
|
|
//
|
|
|
|
// since we don't know how many local variables there are,
|
|
|
|
// we have to have them go backwards on the stack
|
|
|
|
def->value.stackOffset = scope->value.functionPtr->locals;
|
|
|
|
def->initialized = idVarDef::stackVariable;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( type->Inherits( &type_object ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// objects only have their entity number on the stack, not the entire object
|
|
|
|
scope->value.functionPtr->locals += type_object.Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
scope->value.functionPtr->locals += type->Size();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
|
|
|
// global variable
|
|
|
|
//
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB begin
|
|
|
|
def->value.bytePtr = ReserveDefMemory( def->TypeDef()->Size() );
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 17:29:47 +00:00
|
|
|
//memset( def->value.bytePtr, 0, def->TypeDef()->Size() );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::GetDef
|
|
|
|
|
|
|
|
If type is NULL, it will match any type
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* idProgram::GetDef( const idTypeDef* type, const char* name, const idVarDef* scope ) const
|
|
|
|
{
|
|
|
|
idVarDef* def;
|
|
|
|
idVarDef* bestDef;
|
2012-11-26 18:58:24 +00:00
|
|
|
int bestDepth;
|
|
|
|
int depth;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
bestDepth = 0;
|
|
|
|
bestDef = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( def = GetDefList( name ); def != NULL; def = def->Next() )
|
|
|
|
{
|
|
|
|
if( def->scope->Type() == ev_namespace )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth = def->DepthOfScope( scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !depth )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// not in the same namespace
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( def->scope != scope )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// in a different function
|
|
|
|
continue;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth = 1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !bestDef || ( depth < bestDepth ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
bestDepth = depth;
|
|
|
|
bestDef = def;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// see if the name is already in use for another type
|
2012-11-28 15:47:07 +00:00
|
|
|
if( bestDef && type && ( bestDef->TypeDef() != type ) )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( va( "Type mismatch on redeclaration of %s", name ) );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "Type mismatch on redeclaration of %s", name );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return bestDef;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::FreeDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::FreeDef( idVarDef* def, const idVarDef* scope )
|
|
|
|
{
|
|
|
|
idVarDef* e;
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( def->Type() == ev_vector )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr name;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( name, "%s_x", def->Name() );
|
|
|
|
e = GetDef( NULL, name, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( e )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeDef( e, scope );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( name, "%s_y", def->Name() );
|
|
|
|
e = GetDef( NULL, name, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( e )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeDef( e, scope );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
sprintf( name, "%s_z", def->Name() );
|
|
|
|
e = GetDef( NULL, name, scope );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( e )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeDef( e, scope );
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
varDefs.RemoveIndex( def->num );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = def->num; i < varDefs.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
varDefs[ i ]->num = i;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
delete def;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
idProgram::FindFreeResultDef
|
|
|
|
============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* idProgram::FindFreeResultDef( idTypeDef* type, const char* name, idVarDef* scope, const idVarDef* a, const idVarDef* b )
|
|
|
|
{
|
|
|
|
idVarDef* def;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
for( def = GetDefList( name ); def != NULL; def = def->Next() )
|
|
|
|
{
|
|
|
|
if( def == a || def == b )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( def->TypeDef() != type )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( def->scope != scope )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( def->numUsers <= 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return AllocDef( type, name, scope, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::FindFunction
|
|
|
|
|
|
|
|
Searches for the specified function in the currently loaded script. A full namespace should be
|
|
|
|
specified if not in the global namespace.
|
|
|
|
|
|
|
|
Returns 0 if function not found.
|
|
|
|
Returns >0 if function found.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
function_t* idProgram::FindFunction( const char* name ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int start;
|
|
|
|
int pos;
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* namespaceDef;
|
|
|
|
idVarDef* def;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( name );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr fullname = name;
|
|
|
|
start = 0;
|
|
|
|
namespaceDef = &def_namespace;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pos = fullname.Find( "::", true, start );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pos < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr namespaceName = fullname.Mid( start, pos - start );
|
|
|
|
def = GetDef( NULL, namespaceName, namespaceDef );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// couldn't find namespace
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
namespaceDef = def;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// skip past the ::
|
|
|
|
start = pos + 2;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
while( def->Type() == ev_namespace );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr funcName = fullname.Right( fullname.Length() - start );
|
|
|
|
def = GetDef( NULL, funcName, namespaceDef );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// couldn't find function
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ( def->Type() == ev_function ) && ( def->value.functionPtr->eventdef == NULL ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return def->value.functionPtr;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// is not a function, or is an eventdef
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::FindFunction
|
|
|
|
|
|
|
|
Searches for the specified object function in the currently loaded script.
|
|
|
|
|
|
|
|
Returns 0 if function not found.
|
|
|
|
Returns >0 if function found.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
function_t* idProgram::FindFunction( const char* name, const idTypeDef* type ) const
|
|
|
|
{
|
|
|
|
const idVarDef* tdef;
|
|
|
|
const idVarDef* def;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// look for the function
|
|
|
|
def = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( tdef = type->def; tdef != &def_object; tdef = tdef->TypeDef()->SuperClass()->def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
def = GetDef( NULL, name, tdef );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( def )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return def->value.functionPtr;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::AllocFunction
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
function_t& idProgram::AllocFunction( idVarDef* def )
|
|
|
|
{
|
|
|
|
if( functions.Num() >= functions.Max() )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( va( "Exceeded maximum allowed number of functions (%d)", functions.Max() ) );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "Exceeded maximum allowed number of functions (%d)", functions.Max() );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// fill in the dfunction
|
2012-11-28 15:47:07 +00:00
|
|
|
function_t& func = *functions.Alloc();
|
2012-11-26 18:58:24 +00:00
|
|
|
func.eventdef = NULL;
|
|
|
|
func.def = def;
|
|
|
|
func.type = def->TypeDef();
|
|
|
|
func.firstStatement = 0;
|
|
|
|
func.numStatements = 0;
|
|
|
|
func.parmTotal = 0;
|
|
|
|
func.locals = 0;
|
|
|
|
func.filenum = filenum;
|
|
|
|
func.parmSize.SetGranularity( 1 );
|
|
|
|
func.SetName( def->GlobalName() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
def->SetFunction( &func );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::SetEntity
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::SetEntity( const char* name, idEntity* ent )
|
|
|
|
{
|
|
|
|
idVarDef* def;
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr defName( "$" );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
defName += name;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
def = GetDef( &type_entity, defName, &def_namespace );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( def != NULL && ( def->initialized != idVarDef::stackVariable ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// 0 is reserved for NULL entity
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !ent )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*def->value.entityNumberPtr = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*def->value.entityNumberPtr = ent->entityNumber + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::AllocStatement
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
statement_t* idProgram::AllocStatement()
|
|
|
|
{
|
|
|
|
if( statements.Num() >= statements.Max() )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( va( "Exceeded maximum allowed number of statements (%d)", statements.Max() ) );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "Exceeded maximum allowed number of statements (%d)", statements.Max() );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
return statements.Alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
idProgram::BeginCompilation
|
|
|
|
|
|
|
|
called before compiling a batch of files, clears the pr struct
|
|
|
|
==============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::BeginCompilation()
|
|
|
|
{
|
|
|
|
statement_t* statement;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeData();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-28 15:47:07 +00:00
|
|
|
try
|
2012-12-07 17:29:47 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// make the first statement a return for a "NULL" function
|
|
|
|
statement = AllocStatement();
|
|
|
|
statement->linenumber = 0;
|
|
|
|
statement->file = 0;
|
|
|
|
statement->op = OP_RETURN;
|
|
|
|
statement->a = NULL;
|
|
|
|
statement->b = NULL;
|
|
|
|
statement->c = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// define NULL
|
|
|
|
//AllocDef( &type_void, "<NULL>", &def_namespace, true );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// define the return def
|
|
|
|
returnDef = AllocDef( &type_vector, "<RETURN>", &def_namespace, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// define the return def for strings
|
|
|
|
returnStringDef = AllocDef( &type_string, "<RETURN>", &def_namespace, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// define the sys object
|
|
|
|
sysDef = AllocDef( &type_void, "sys", &def_namespace, true );
|
|
|
|
}
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-28 15:47:07 +00:00
|
|
|
catch( idCompileError& err )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Error( "%s", err.GetError() );
|
|
|
|
}
|
2012-12-07 17:29:47 +00:00
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
idProgram::DisassembleStatement
|
|
|
|
==============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::DisassembleStatement( idFile* file, int instructionPointer ) const
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
// RB: added const
|
|
|
|
const opcode_t* op;
|
|
|
|
// RB end
|
2012-11-28 15:47:07 +00:00
|
|
|
const statement_t* statement;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
statement = &statements[ instructionPointer ];
|
|
|
|
op = &idCompiler::opcodes[ statement->op ];
|
|
|
|
file->Printf( "%20s(%d):\t%6d: %15s\t", fileList[ statement->file ].c_str(), statement->linenumber, instructionPointer, op->opname );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( statement->a )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "\ta: " );
|
|
|
|
statement->a->PrintInfo( file, instructionPointer );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( statement->b )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "\tb: " );
|
|
|
|
statement->b->PrintInfo( file, instructionPointer );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( statement->c )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "\tc: " );
|
|
|
|
statement->c->PrintInfo( file, instructionPointer );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
idProgram::Disassemble
|
|
|
|
==============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::Disassemble() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
int instructionPointer;
|
2012-11-28 15:47:07 +00:00
|
|
|
const function_t* func;
|
|
|
|
idFile* file;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
file = fileSystem->OpenFileByMode( "script/disasm.txt", FS_WRITE );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < functions.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
func = &functions[ i ];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( func->eventdef )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// skip eventdefs
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "\nfunction %s() %d stack used, %d parms, %d locals {\n", func->Name(), func->locals, func->parmTotal, func->locals - func->parmTotal );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( instructionPointer = 0; instructionPointer < func->numStatements; instructionPointer++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
DisassembleStatement( file, func->firstStatement + instructionPointer );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
file->Printf( "}\n" );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
fileSystem->CloseFile( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
idProgram::FinishCompilation
|
|
|
|
|
|
|
|
Called after all files are compiled to check for errors
|
|
|
|
==============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::FinishCompilation()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
top_functions = functions.Num();
|
|
|
|
top_statements = statements.Num();
|
|
|
|
top_types = types.Num();
|
|
|
|
top_defs = varDefs.Num();
|
|
|
|
top_files = fileList.Num();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
variableDefaults.Clear();
|
|
|
|
variableDefaults.SetNum( numVariables );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < numVariables; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
variableDefaults[ i ] = variables[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
idProgram::CompileStats
|
|
|
|
|
|
|
|
called after all files are compiled to report memory usage.
|
|
|
|
==============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::CompileStats()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int memused;
|
|
|
|
int memallocated;
|
|
|
|
int numdefs;
|
|
|
|
int stringspace;
|
|
|
|
int funcMem;
|
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Printf( "---------- Compile stats ----------\n" );
|
|
|
|
gameLocal.DPrintf( "Files loaded:\n" );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
stringspace = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < fileList.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.DPrintf( " %s\n", fileList[ i ].c_str() );
|
|
|
|
stringspace += fileList[ i ].Allocated();
|
|
|
|
}
|
|
|
|
stringspace += fileList.Size();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numdefs = varDefs.Num();
|
|
|
|
memused = varDefs.Num() * sizeof( idVarDef );
|
|
|
|
memused += types.Num() * sizeof( idTypeDef );
|
|
|
|
memused += stringspace;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < types.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
memused += types[ i ]->Allocated();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
funcMem = functions.MemoryUsed();
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < functions.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
funcMem += functions[ i ].Allocated();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
memallocated = funcMem + memused + sizeof( idProgram );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
memused += statements.MemoryUsed();
|
|
|
|
memused += functions.MemoryUsed(); // name and filename of functions are shared, so no need to include them
|
|
|
|
memused += sizeof( variables );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Printf( "\nMemory usage:\n" );
|
|
|
|
gameLocal.Printf( " Strings: %d, %d bytes\n", fileList.Num(), stringspace );
|
|
|
|
gameLocal.Printf( " Statements: %d, %d bytes\n", statements.Num(), statements.MemoryUsed() );
|
|
|
|
gameLocal.Printf( " Functions: %d, %d bytes\n", functions.Num(), funcMem );
|
|
|
|
gameLocal.Printf( " Variables: %d bytes\n", numVariables );
|
|
|
|
gameLocal.Printf( " Mem used: %d bytes\n", memused );
|
|
|
|
gameLocal.Printf( " Static data: %d bytes\n", sizeof( idProgram ) );
|
|
|
|
gameLocal.Printf( " Allocated: %d bytes\n", memallocated );
|
|
|
|
gameLocal.Printf( " Thread size: %d bytes\n\n", sizeof( idThread ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::CompileText
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idProgram::CompileText( const char* source, const char* text, bool console )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idCompiler compiler;
|
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
idVarDef* def;
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr ospath;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// use a full os path for GetFilenum since it calls OSPathToRelativePath to convert filenames from the parser
|
|
|
|
ospath = fileSystem->RelativePathToOSPath( source );
|
|
|
|
filenum = GetFilenum( ospath );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-28 15:47:07 +00:00
|
|
|
try
|
2012-12-07 17:29:47 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
compiler.CompileFile( text, filename, console );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// check to make sure all functions prototyped have code
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < varDefs.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
def = varDefs[ i ];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( def->Type() == ev_function ) && ( ( def->scope->Type() == ev_namespace ) || def->scope->TypeDef()->Inherits( &type_object ) ) )
|
|
|
|
{
|
|
|
|
if( !def->value.functionPtr->eventdef && !def->value.functionPtr->firstStatement )
|
|
|
|
{
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-26 18:58:24 +00:00
|
|
|
throw idCompileError( va( "function %s was not defined\n", def->GlobalName() ) );
|
2012-12-07 17:29:47 +00:00
|
|
|
#else
|
|
|
|
gameLocal.Error( "function %s was not defined\n", def->GlobalName() );
|
|
|
|
#endif
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 17:29:47 +00:00
|
|
|
#if defined(USE_EXCEPTIONS)
|
2012-11-28 15:47:07 +00:00
|
|
|
catch( idCompileError& err )
|
|
|
|
{
|
|
|
|
if( console )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Printf( "%s\n", err.GetError() );
|
|
|
|
return false;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Error( "%s\n", err.GetError() );
|
|
|
|
}
|
|
|
|
};
|
2012-12-07 17:29:47 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !console )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
CompileStats();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::CompileFunction
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const function_t* idProgram::CompileFunction( const char* functionName, const char* text )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
bool result;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
result = CompileText( functionName, text, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( g_disasm.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Disassemble();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !result )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Error( "Compile failed." );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return FindFunction( functionName );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::CompileFile
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::CompileFile( const char* filename )
|
|
|
|
{
|
|
|
|
char* src;
|
2012-11-26 18:58:24 +00:00
|
|
|
bool result;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( fileSystem->ReadFile( filename, ( void** )&src, NULL ) < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Error( "Couldn't load %s\n", filename );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
result = CompileText( filename, src, false );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
fileSystem->FreeFile( src );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( g_disasm.GetBool() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Disassemble();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !result )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Error( "Compile failed in file %s.", filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::FreeData
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::FreeData()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// free the defs
|
|
|
|
varDefs.DeleteContents( true );
|
|
|
|
varDefNames.DeleteContents( true );
|
|
|
|
varDefNameHash.Free();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
returnDef = NULL;
|
|
|
|
returnStringDef = NULL;
|
|
|
|
sysDef = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// free any special types we've created
|
|
|
|
types.DeleteContents( true );
|
|
|
|
typesHash.Free();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
filenum = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numVariables = 0;
|
|
|
|
memset( variables, 0, sizeof( variables ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// clear all the strings in the functions so that it doesn't look like we're leaking memory.
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < functions.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
functions[ i ].Clear();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
filename.Clear();
|
|
|
|
fileList.Clear();
|
|
|
|
statements.Clear();
|
|
|
|
functions.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
top_functions = 0;
|
|
|
|
top_statements = 0;
|
|
|
|
top_types = 0;
|
|
|
|
top_defs = 0;
|
|
|
|
top_files = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
filename = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::Startup
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::Startup( const char* defaultScript )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
gameLocal.Printf( "Initializing scripts\n" );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// make sure all data is freed up
|
|
|
|
idThread::Restart();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// get ready for loading scripts
|
|
|
|
BeginCompilation();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// load the default script
|
2012-11-28 15:47:07 +00:00
|
|
|
if( defaultScript && *defaultScript )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
CompileFile( defaultScript );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FinishCompilation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::Save
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::Save( idSaveGame* savefile ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
int currentFileNum = top_files;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
savefile->WriteInt( ( fileList.Num() - currentFileNum ) );
|
|
|
|
while( currentFileNum < fileList.Num() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->WriteString( fileList[ currentFileNum ] );
|
|
|
|
currentFileNum++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < variableDefaults.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( variables[i] != variableDefaults[i] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->WriteInt( i );
|
|
|
|
savefile->WriteByte( variables[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Mark the end of the diff with default variables with -1
|
|
|
|
savefile->WriteInt( -1 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->WriteInt( numVariables );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = variableDefaults.Num(); i < numVariables; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->WriteByte( variables[i] );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int checksum = CalculateChecksum();
|
|
|
|
savefile->WriteInt( checksum );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::Restore
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idProgram::Restore( idRestoreGame* savefile )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, num, index;
|
|
|
|
bool result = true;
|
|
|
|
idStr scriptname;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadInt( num );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < num; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadString( scriptname );
|
|
|
|
CompileFile( scriptname );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadInt( index );
|
2012-11-28 15:47:07 +00:00
|
|
|
while( index >= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadByte( variables[index] );
|
|
|
|
savefile->ReadInt( index );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadInt( num );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = variableDefaults.Num(); i < num; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadByte( variables[i] );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int saved_checksum, checksum;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savefile->ReadInt( saved_checksum );
|
|
|
|
checksum = CalculateChecksum();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( saved_checksum != checksum )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
result = false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::CalculateChecksum
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idProgram::CalculateChecksum() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, result;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
unsigned short op;
|
|
|
|
int a;
|
|
|
|
int b;
|
|
|
|
int c;
|
|
|
|
unsigned short linenumber;
|
|
|
|
unsigned short file;
|
|
|
|
} statementBlock_t;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
statementBlock_t* statementList = new( TAG_SCRIPT ) statementBlock_t[ statements.Num() ];
|
|
|
|
|
|
|
|
memset( statementList, 0, ( sizeof( statementBlock_t ) * statements.Num() ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// Copy info into new list, using the variable numbers instead of a pointer to the variable
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < statements.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].op = statements[i].op;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( statements[i].a )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].a = statements[i].a->num;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].a = -1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( statements[i].b )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].b = statements[i].b->num;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].b = -1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( statements[i].c )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].c = statements[i].c->num;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].c = -1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
statementList[i].linenumber = statements[i].linenumber;
|
|
|
|
statementList[i].file = statements[i].file;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
result = MD4_BlockChecksum( statementList, ( sizeof( statementBlock_t ) * statements.Num() ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
delete [] statementList;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
idProgram::Restart
|
|
|
|
|
|
|
|
Restores all variables to their initial value
|
|
|
|
==============
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::Restart()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idThread::Restart();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
|
|
|
// since there may have been a script loaded by the map or the user may
|
|
|
|
// have typed "script" from the console, free up any types and vardefs that
|
|
|
|
// have been allocated after the initial startup
|
|
|
|
//
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = top_types; i < types.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
delete types[ i ];
|
|
|
|
}
|
|
|
|
types.SetNum( top_types );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
typesHash.Free();
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < types.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
typesHash.Add( idStr::Hash( types[i]->Name() ), i );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = top_defs; i < varDefs.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
delete varDefs[ i ];
|
|
|
|
}
|
|
|
|
varDefs.SetNum( top_defs );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = top_functions; i < functions.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
functions[ i ].Clear();
|
|
|
|
}
|
|
|
|
functions.SetNum( top_functions );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
statements.SetNum( top_statements );
|
|
|
|
fileList.SetNum( top_files );
|
|
|
|
filename.Clear();
|
|
|
|
|
|
|
|
// reset the variables to their default values
|
|
|
|
numVariables = variableDefaults.Num();
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < numVariables; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
variables[ i ] = variableDefaults[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::GetFilenum
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idProgram::GetFilenum( const char* name )
|
|
|
|
{
|
|
|
|
if( filename == name )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return filenum;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr strippedName;
|
|
|
|
strippedName = fileSystem->OSPathToRelativePath( name );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !strippedName.Length() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// not off the base path so just use the full path
|
|
|
|
filenum = fileList.AddUnique( name );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
filenum = fileList.AddUnique( strippedName );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// save the unstripped name so that we don't have to strip the incoming name every time we call GetFilenum
|
|
|
|
filename = name;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return filenum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::idProgram
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idProgram::idProgram()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
varDefs.SetGranularity( 256 );
|
|
|
|
varDefNames.SetGranularity( 256 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::~idProgram
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idProgram::~idProgram()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idProgram::ReturnEntity
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idProgram::ReturnEntity( idEntity* ent )
|
|
|
|
{
|
|
|
|
if( ent )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*returnDef->value.entityNumberPtr = ent->entityNumber + 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*returnDef->value.entityNumberPtr = 0;
|
|
|
|
}
|
|
|
|
}
|