2016-03-01 15:47:10 +00:00
/*
* * dobjtype . cpp
* * Implements the type information class
* *
* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2017-04-17 11:33:19 +00:00
* * Copyright 1998 - 2016 Randy Heit
* * Copyright 2005 - 2016 Christoph Oelckers
2016-03-01 15:47:10 +00:00
* * All rights reserved .
* *
* * Redistribution and use in source and binary forms , with or without
* * modification , are permitted provided that the following conditions
* * are met :
* *
* * 1. Redistributions of source code must retain the above copyright
* * notice , this list of conditions and the following disclaimer .
* * 2. Redistributions in binary form must reproduce the above copyright
* * notice , this list of conditions and the following disclaimer in the
* * documentation and / or other materials provided with the distribution .
* * 3. The name of the author may not be used to endorse or promote products
* * derived from this software without specific prior written permission .
* *
* * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ` ` AS IS ' ' AND ANY EXPRESS OR
* * IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
* * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
* * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
* * INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
* * NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* * DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* * THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* * THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* *
*/
// HEADER FILES ------------------------------------------------------------
# include <float.h>
# include <limits>
# include "dobject.h"
# include "i_system.h"
2016-09-20 16:27:47 +00:00
# include "serializer.h"
2016-03-01 15:47:10 +00:00
# include "actor.h"
# include "templates.h"
# include "autosegs.h"
# include "v_text.h"
# include "a_pickups.h"
# include "d_player.h"
2016-09-20 16:27:47 +00:00
# include "doomerrors.h"
2016-03-01 15:47:10 +00:00
# include "fragglescript/t_fs.h"
2016-11-30 12:36:13 +00:00
# include "a_keys.h"
2017-04-12 23:12:04 +00:00
# include "vm.h"
# include "types.h"
2016-03-01 15:47:10 +00:00
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
2016-06-08 08:56:11 +00:00
EXTERN_CVAR ( Bool , strictdecorate ) ;
2016-03-01 15:47:10 +00:00
// PUBLIC DATA DEFINITIONS -------------------------------------------------
2017-02-08 19:37:22 +00:00
FMemArena ClassDataAllocator ( 32768 ) ; // use this for all static class data that can be released in bulk when the type system is shut down.
2017-01-23 18:09:36 +00:00
2016-03-01 15:47:10 +00:00
TArray < PClass * > PClass : : AllClasses ;
2017-04-12 20:46:49 +00:00
TMap < FName , PClass * > PClass : : ClassMap ;
2017-01-31 12:41:23 +00:00
TArray < VMFunction * * > PClass : : FunctionPtrList ;
2016-03-01 15:47:10 +00:00
bool PClass : : bShutdown ;
2017-01-13 12:51:47 +00:00
bool PClass : : bVMOperational ;
2016-03-01 15:47:10 +00:00
2017-04-13 16:59:45 +00:00
// Originally this was just a bogus pointer, but with the VM performing a read barrier on every object pointer write
// that does not work anymore. WP_NOCHANGE needs to point to a vaild object to work as intended.
// This Object does not need to be garbage collected, though, but it needs to provide the proper structure so that the
// GC can process it.
AWeapon * WP_NOCHANGE ;
DEFINE_GLOBAL ( WP_NOCHANGE ) ;
2016-03-01 15:47:10 +00:00
2017-04-12 20:46:49 +00:00
2016-03-01 15:47:10 +00:00
// PRIVATE DATA DEFINITIONS ------------------------------------------------
2017-02-07 13:48:27 +00:00
// A harmless non-nullptr FlatPointer for classes without pointers.
2016-03-01 15:47:10 +00:00
static const size_t TheEnd = ~ ( size_t ) 0 ;
2016-04-03 04:10:43 +00:00
//==========================================================================
//
// PClass :: WriteValue
//
// Similar to PStruct's version, except it also needs to traverse parent
// classes.
//
//==========================================================================
2016-09-20 16:27:47 +00:00
static void RecurseWriteFields ( const PClass * type , FSerializer & ar , const void * addr )
2016-04-03 04:10:43 +00:00
{
2017-02-07 13:48:27 +00:00
if ( type ! = nullptr )
2016-04-03 04:10:43 +00:00
{
RecurseWriteFields ( type - > ParentClass , ar , addr ) ;
2016-11-22 18:20:31 +00:00
// Don't write this part if it has no non-transient variables
2016-04-03 04:10:43 +00:00
for ( unsigned i = 0 ; i < type - > Fields . Size ( ) ; + + i )
{
2017-03-01 19:09:50 +00:00
if ( ! ( type - > Fields [ i ] - > Flags & ( VARF_Transient | VARF_Meta ) ) )
2016-04-03 04:10:43 +00:00
{
// Tag this section with the class it came from in case
// a more-derived class has variables that shadow a less-
// derived class. Whether or not that is a language feature
// that will actually be allowed remains to be seen.
2016-09-20 16:27:47 +00:00
FString key ;
key . Format ( " class:%s " , type - > TypeName . GetChars ( ) ) ;
if ( ar . BeginObject ( key . GetChars ( ) ) )
{
2017-04-12 20:46:49 +00:00
type - > VMType - > Symbols . WriteFields ( ar , addr ) ;
2016-09-20 16:27:47 +00:00
ar . EndObject ( ) ;
}
2016-04-03 04:10:43 +00:00
break ;
}
}
}
}
2016-09-20 16:27:47 +00:00
// Same as WriteValue, but does not create a new object in the serializer
// This is so that user variables do not contain unnecessary subblocks.
void PClass : : WriteAllFields ( FSerializer & ar , const void * addr ) const
2016-04-03 04:10:43 +00:00
{
RecurseWriteFields ( this , ar , addr ) ;
}
//==========================================================================
//
2017-04-12 15:21:13 +00:00
// PClass :: ReadAllFields
2016-04-03 04:10:43 +00:00
//
//==========================================================================
2016-09-20 16:27:47 +00:00
bool PClass : : ReadAllFields ( FSerializer & ar , void * addr ) const
{
bool readsomething = false ;
bool foundsomething = false ;
const char * key ;
- removed the sequential processing of JSON objects because the benefit is too small.
After testing with a savegame on ZDCMP2 which is probably the largest map in existence, timing both methods resulted in a speed difference of less than 40 ms (70 vs 110 ms for reading all sectory, linedefs, sidedefs and objects).
This compares to an overall restoration time, including reloading the level, precaching all textures and setting everything up, of approx. 1.2 s, meaning an increase of 3% of the entire reloading time.
That's simply not worth all the negative side effects that may happen with a method that highly depends on proper code construction.
On the other hand, using random access means that a savegame version change is only needed now when the semantics of a field change, but not if some get added or deleted.
- do not I_Error out in the serializer unless caused by a programming error.
It is better to let the serializer finish, collect all the errors and I_Error out when the game is known to be in a stable enough state to allow unwinding.
2016-09-23 12:04:05 +00:00
key = ar . GetKey ( ) ;
if ( strcmp ( key , " classtype " ) )
{
// this does not represent a DObject
Printf ( TEXTCOLOR_RED " trying to read user variables but got a non-object (first key is '%s') " , key ) ;
ar . mErrors + + ;
return false ;
}
2016-09-20 16:27:47 +00:00
while ( ( key = ar . GetKey ( ) ) )
2016-04-03 04:10:43 +00:00
{
2016-09-20 16:27:47 +00:00
if ( strncmp ( key , " class: " , 6 ) )
{
- removed the sequential processing of JSON objects because the benefit is too small.
After testing with a savegame on ZDCMP2 which is probably the largest map in existence, timing both methods resulted in a speed difference of less than 40 ms (70 vs 110 ms for reading all sectory, linedefs, sidedefs and objects).
This compares to an overall restoration time, including reloading the level, precaching all textures and setting everything up, of approx. 1.2 s, meaning an increase of 3% of the entire reloading time.
That's simply not worth all the negative side effects that may happen with a method that highly depends on proper code construction.
On the other hand, using random access means that a savegame version change is only needed now when the semantics of a field change, but not if some get added or deleted.
- do not I_Error out in the serializer unless caused by a programming error.
It is better to let the serializer finish, collect all the errors and I_Error out when the game is known to be in a stable enough state to allow unwinding.
2016-09-23 12:04:05 +00:00
// We have read all user variable blocks.
2016-09-20 16:27:47 +00:00
break ;
}
foundsomething = true ;
PClass * type = PClass : : FindClass ( key + 6 ) ;
if ( type ! = nullptr )
2016-04-03 04:10:43 +00:00
{
// Only read it if the type is related to this one.
2017-04-12 20:46:49 +00:00
if ( IsDescendantOf ( type ) )
2016-04-03 04:10:43 +00:00
{
- removed the sequential processing of JSON objects because the benefit is too small.
After testing with a savegame on ZDCMP2 which is probably the largest map in existence, timing both methods resulted in a speed difference of less than 40 ms (70 vs 110 ms for reading all sectory, linedefs, sidedefs and objects).
This compares to an overall restoration time, including reloading the level, precaching all textures and setting everything up, of approx. 1.2 s, meaning an increase of 3% of the entire reloading time.
That's simply not worth all the negative side effects that may happen with a method that highly depends on proper code construction.
On the other hand, using random access means that a savegame version change is only needed now when the semantics of a field change, but not if some get added or deleted.
- do not I_Error out in the serializer unless caused by a programming error.
It is better to let the serializer finish, collect all the errors and I_Error out when the game is known to be in a stable enough state to allow unwinding.
2016-09-23 12:04:05 +00:00
if ( ar . BeginObject ( nullptr ) )
{
2017-04-12 20:46:49 +00:00
readsomething | = type - > VMType - > Symbols . ReadFields ( ar , addr , type - > TypeName . GetChars ( ) ) ;
- removed the sequential processing of JSON objects because the benefit is too small.
After testing with a savegame on ZDCMP2 which is probably the largest map in existence, timing both methods resulted in a speed difference of less than 40 ms (70 vs 110 ms for reading all sectory, linedefs, sidedefs and objects).
This compares to an overall restoration time, including reloading the level, precaching all textures and setting everything up, of approx. 1.2 s, meaning an increase of 3% of the entire reloading time.
That's simply not worth all the negative side effects that may happen with a method that highly depends on proper code construction.
On the other hand, using random access means that a savegame version change is only needed now when the semantics of a field change, but not if some get added or deleted.
- do not I_Error out in the serializer unless caused by a programming error.
It is better to let the serializer finish, collect all the errors and I_Error out when the game is known to be in a stable enough state to allow unwinding.
2016-09-23 12:04:05 +00:00
ar . EndObject ( ) ;
}
2016-04-03 04:10:43 +00:00
}
else
{
2016-08-28 07:55:04 +00:00
DPrintf ( DMSG_ERROR , " Unknown superclass %s of class %s \n " ,
2016-04-03 04:10:43 +00:00
type - > TypeName . GetChars ( ) , TypeName . GetChars ( ) ) ;
}
}
2016-09-20 16:27:47 +00:00
else
{
DPrintf ( DMSG_ERROR , " Unknown superclass %s of class %s \n " ,
key + 6 , TypeName . GetChars ( ) ) ;
}
2016-04-03 04:10:43 +00:00
}
2016-09-20 16:27:47 +00:00
return readsomething | | ! foundsomething ;
2016-04-03 04:10:43 +00:00
}
2016-03-01 15:47:10 +00:00
//==========================================================================
//
// cregcmp
//
// Sorter to keep built-in types in a deterministic order. (Needed?)
//
//==========================================================================
2016-04-11 08:46:30 +00:00
static int cregcmp ( const void * a , const void * b ) NO_SANITIZE
2016-03-01 15:47:10 +00:00
{
const PClass * class1 = * ( const PClass * * ) a ;
const PClass * class2 = * ( const PClass * * ) b ;
return strcmp ( class1 - > TypeName , class2 - > TypeName ) ;
}
//==========================================================================
//
// PClass :: StaticInit STATIC
//
// Creates class metadata for all built-in types.
//
//==========================================================================
void PClass : : StaticInit ( )
{
atterm ( StaticShutdown ) ;
2017-01-23 18:09:36 +00:00
Namespaces . GlobalNamespace = Namespaces . NewNamespace ( 0 ) ;
2016-03-01 15:47:10 +00:00
FAutoSegIterator probe ( CRegHead , CRegTail ) ;
2017-02-07 13:48:27 +00:00
while ( * + + probe ! = nullptr )
2016-03-01 15:47:10 +00:00
{
( ( ClassReg * ) * probe ) - > RegisterClass ( ) ;
}
// Keep built-in classes in consistant order. I did this before, though
// I'm not sure if this is really necessary to maintain any sort of sync.
qsort ( & AllClasses [ 0 ] , AllClasses . Size ( ) , sizeof ( AllClasses [ 0 ] ) , cregcmp ) ;
2016-10-08 20:16:10 +00:00
2017-04-13 16:59:45 +00:00
// WP_NOCHANGE must point to a valid object, although it does not need to be a weapon.
// A simple DObject is enough to give the GC the ability to deal with it, if subjected to it.
2017-04-14 11:31:58 +00:00
WP_NOCHANGE = ( AWeapon * ) Create < DObject > ( ) ;
2017-04-13 16:59:45 +00:00
WP_NOCHANGE - > Release ( ) ;
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
// PClass :: StaticShutdown STATIC
//
2017-02-07 13:48:27 +00:00
// Frees all static class data.
2016-03-01 15:47:10 +00:00
//
//==========================================================================
void PClass : : StaticShutdown ( )
{
2017-04-13 16:59:45 +00:00
if ( WP_NOCHANGE ! = nullptr )
{
delete WP_NOCHANGE ;
}
2017-01-31 12:41:23 +00:00
// delete all variables containing pointers to script functions.
for ( auto p : FunctionPtrList )
{
* p = nullptr ;
}
FunctionPtrList . Clear ( ) ;
2017-02-08 10:13:41 +00:00
VMFunction : : DeleteAll ( ) ;
2017-01-12 21:49:18 +00:00
2017-02-07 13:48:27 +00:00
// Make a full garbage collection here so that all destroyed but uncollected higher level objects
// that still exist are properly taken down before the low level data is deleted.
2017-01-12 21:49:18 +00:00
GC : : FullGC ( ) ;
// From this point onward no scripts may be called anymore because the data needed by the VM is getting deleted now.
// This flags DObject::Destroy not to call any scripted OnDestroy methods anymore.
2017-01-13 12:51:47 +00:00
bVMOperational = false ;
2017-01-12 21:49:18 +00:00
2017-02-09 00:34:07 +00:00
// PendingWeapon must be cleared manually because it is not subjected to the GC if it contains WP_NOCHANGE, which is just RUNTIME_CLASS(AWWeapon).
// But that will get cleared here, confusing the GC if the value is left in.
for ( auto & p : players )
{
p . PendingWeapon = nullptr ;
}
2017-04-14 17:40:50 +00:00
Namespaces . ReleaseSymbols ( ) ;
2017-02-09 00:34:07 +00:00
2017-04-12 20:57:14 +00:00
// This must be done in two steps because the native classes are not ordered by inheritance,
// so all meta data must be gone before deleting the actual class objects.
for ( auto cls : AllClasses ) cls - > DestroyMeta ( cls - > Meta ) ;
for ( auto cls : AllClasses ) delete cls ;
2017-01-12 21:49:18 +00:00
// Unless something went wrong, anything left here should be class and type objects only, which do not own any scripts.
2017-02-08 21:43:20 +00:00
bShutdown = true ;
2017-01-12 21:49:18 +00:00
TypeTable . Clear ( ) ;
2017-02-08 19:37:22 +00:00
ClassDataAllocator . FreeAllBlocks ( ) ;
2016-03-01 15:47:10 +00:00
AllClasses . Clear ( ) ;
PClassActor : : AllActorClasses . Clear ( ) ;
2017-04-13 10:47:41 +00:00
ClassMap . Clear ( ) ;
2016-03-01 15:47:10 +00:00
FAutoSegIterator probe ( CRegHead , CRegTail ) ;
2016-11-12 09:17:57 +00:00
while ( * + + probe ! = nullptr )
2016-03-01 15:47:10 +00:00
{
2016-11-12 09:17:57 +00:00
auto cr = ( ( ClassReg * ) * probe ) ;
cr - > MyClass = nullptr ;
2016-03-01 15:47:10 +00:00
}
2017-01-12 21:49:18 +00:00
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
// PClass Constructor
//
//==========================================================================
PClass : : PClass ( )
{
PClass : : AllClasses . Push ( this ) ;
}
//==========================================================================
//
// PClass Destructor
//
//==========================================================================
PClass : : ~ PClass ( )
{
2017-02-07 13:48:27 +00:00
if ( Defaults ! = nullptr )
2016-03-01 15:47:10 +00:00
{
M_Free ( Defaults ) ;
2017-02-07 13:48:27 +00:00
Defaults = nullptr ;
2016-03-01 15:47:10 +00:00
}
2017-02-27 22:28:19 +00:00
if ( Meta ! = nullptr )
{
M_Free ( Meta ) ;
Meta = nullptr ;
}
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
// ClassReg :: RegisterClass
//
// Create metadata describing the built-in class this struct is intended
// for.
//
//==========================================================================
PClass * ClassReg : : RegisterClass ( )
{
// Skip classes that have already been registered
2017-02-07 13:48:27 +00:00
if ( MyClass ! = nullptr )
2016-03-01 15:47:10 +00:00
{
return MyClass ;
}
// Add type to list
2017-04-12 20:46:49 +00:00
PClass * cls = new PClass ;
2016-03-01 15:47:10 +00:00
SetupClass ( cls ) ;
cls - > InsertIntoHash ( ) ;
2016-11-06 10:28:01 +00:00
if ( ParentType ! = nullptr )
2016-03-01 15:47:10 +00:00
{
cls - > ParentClass = ParentType - > RegisterClass ( ) ;
}
return cls ;
}
//==========================================================================
//
// ClassReg :: SetupClass
//
// Copies the class-defining parameters from a ClassReg to the Class object
// created for it.
//
//==========================================================================
void ClassReg : : SetupClass ( PClass * cls )
{
2017-02-07 13:48:27 +00:00
assert ( MyClass = = nullptr ) ;
2016-03-01 15:47:10 +00:00
MyClass = cls ;
cls - > TypeName = FName ( Name + 1 ) ;
cls - > Size = SizeOf ;
cls - > Pointers = Pointers ;
cls - > ConstructNative = ConstructNative ;
}
//==========================================================================
//
// PClass :: InsertIntoHash
//
// Add class to the type table.
//
//==========================================================================
void PClass : : InsertIntoHash ( )
{
2017-04-12 20:46:49 +00:00
auto k = ClassMap . CheckKey ( TypeName ) ;
if ( k ! = nullptr )
2016-03-01 15:47:10 +00:00
{ // This type has already been inserted
2016-10-07 13:28:25 +00:00
I_Error ( " Tried to register class '%s' more than once. \n " , TypeName . GetChars ( ) ) ;
2016-03-01 15:47:10 +00:00
}
else
{
2017-04-12 20:46:49 +00:00
ClassMap [ TypeName ] = this ;
}
if ( IsDescendantOf ( RUNTIME_CLASS ( AActor ) ) )
{
PClassActor : : AllActorClasses . Push ( static_cast < PClassActor * > ( this ) ) ;
2016-03-01 15:47:10 +00:00
}
}
2016-04-03 04:10:43 +00:00
//==========================================================================
//
// PClass :: FindParentClass
//
// Finds a parent class that matches the given name, including itself.
//
//==========================================================================
const PClass * PClass : : FindParentClass ( FName name ) const
{
2017-02-07 13:48:27 +00:00
for ( const PClass * type = this ; type ! = nullptr ; type = type - > ParentClass )
2016-04-03 04:10:43 +00:00
{
if ( type - > TypeName = = name )
{
return type ;
}
}
2017-02-07 13:48:27 +00:00
return nullptr ;
2016-04-03 04:10:43 +00:00
}
2016-03-01 15:47:10 +00:00
//==========================================================================
//
// PClass :: FindClass
//
// Find a type, passed the name as a name.
//
//==========================================================================
PClass * PClass : : FindClass ( FName zaname )
{
if ( zaname = = NAME_None )
{
2017-02-07 13:48:27 +00:00
return nullptr ;
2016-03-01 15:47:10 +00:00
}
2017-04-12 20:46:49 +00:00
auto k = ClassMap . CheckKey ( zaname ) ;
return k ? * k : nullptr ;
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
// PClass :: CreateNew
//
// Create a new object that this class represents
//
//==========================================================================
2017-02-27 22:28:19 +00:00
DObject * PClass : : CreateNew ( )
2016-03-01 15:47:10 +00:00
{
2017-03-08 17:47:52 +00:00
uint8_t * mem = ( uint8_t * ) M_Malloc ( Size ) ;
2017-02-07 13:48:27 +00:00
assert ( mem ! = nullptr ) ;
2016-03-01 15:47:10 +00:00
// Set this object's defaults before constructing it.
2017-02-07 13:48:27 +00:00
if ( Defaults ! = nullptr )
2016-03-01 15:47:10 +00:00
memcpy ( mem , Defaults , Size ) ;
else
memset ( mem , 0 , Size ) ;
2017-02-12 13:04:48 +00:00
if ( ConstructNative = = nullptr )
{
2017-03-09 16:21:37 +00:00
M_Free ( mem ) ;
2017-02-12 13:04:48 +00:00
I_Error ( " Attempt to instantiate abstract class %s. " , TypeName . GetChars ( ) ) ;
}
2016-03-01 15:47:10 +00:00
ConstructNative ( mem ) ;
( ( DObject * ) mem ) - > SetClass ( const_cast < PClass * > ( this ) ) ;
2017-02-27 22:28:19 +00:00
InitializeSpecials ( mem , Defaults , & PClass : : SpecialInits ) ;
2016-03-01 15:47:10 +00:00
return ( DObject * ) mem ;
}
2016-04-03 22:45:04 +00:00
//==========================================================================
//
// PClass :: InitializeSpecials
//
2016-04-04 19:57:43 +00:00
// Initialize special fields (e.g. strings) of a newly-created instance.
2016-04-03 22:45:04 +00:00
//
//==========================================================================
2017-02-27 22:28:19 +00:00
void PClass : : InitializeSpecials ( void * addr , void * defaults , TArray < FTypeAndOffset > PClass : : * Inits )
2016-04-03 22:45:04 +00:00
{
2016-04-04 19:57:43 +00:00
// Once we reach a native class, we can stop going up the family tree,
// since native classes handle initialization natively.
2017-02-27 23:45:16 +00:00
if ( ( ! bRuntimeClass & & Inits = = & PClass : : SpecialInits ) | | ParentClass = = nullptr )
2016-04-03 22:45:04 +00:00
{
2016-04-04 19:57:43 +00:00
return ;
2016-04-03 22:45:04 +00:00
}
2017-02-27 22:28:19 +00:00
ParentClass - > InitializeSpecials ( addr , defaults , Inits ) ;
for ( auto tao : ( this - > * Inits ) )
2016-04-03 22:45:04 +00:00
{
2017-01-15 15:55:30 +00:00
tao . first - > InitializeValue ( ( char * ) addr + tao . second , defaults = = nullptr ? nullptr : ( ( char * ) defaults ) + tao . second ) ;
2016-04-03 22:45:04 +00:00
}
}
//==========================================================================
//
// PClass :: DestroySpecials
//
2016-04-04 19:57:43 +00:00
// Destroy special fields (e.g. strings) of an instance that is about to be
// deleted.
//
2016-04-03 22:45:04 +00:00
//==========================================================================
2017-02-27 22:28:19 +00:00
void PClass : : DestroySpecials ( void * addr )
2016-04-03 22:45:04 +00:00
{
2016-04-04 19:57:43 +00:00
if ( ! bRuntimeClass )
2016-04-03 22:45:04 +00:00
{
2016-04-04 19:57:43 +00:00
return ;
2016-04-03 22:45:04 +00:00
}
2017-02-07 13:48:27 +00:00
assert ( ParentClass ! = nullptr ) ;
2016-04-04 19:57:43 +00:00
ParentClass - > DestroySpecials ( addr ) ;
2016-04-03 22:45:04 +00:00
for ( auto tao : SpecialInits )
{
2017-03-08 17:47:52 +00:00
tao . first - > DestroyValue ( ( uint8_t * ) addr + tao . second ) ;
2016-04-03 22:45:04 +00:00
}
}
2016-04-04 19:57:43 +00:00
2017-04-11 19:48:41 +00:00
//==========================================================================
//
// PClass :: DestroyMeta
//
// Same for meta data
//
//==========================================================================
void PClass : : DestroyMeta ( void * addr )
{
if ( ParentClass ! = nullptr ) ParentClass - > DestroyMeta ( addr ) ;
for ( auto tao : MetaInits )
{
tao . first - > DestroyValue ( ( uint8_t * ) addr + tao . second ) ;
}
}
2016-03-01 15:47:10 +00:00
//==========================================================================
//
// PClass :: Derive
//
// Copies inheritable values into the derived class and other miscellaneous setup.
//
//==========================================================================
2016-11-11 19:05:07 +00:00
void PClass : : Derive ( PClass * newclass , FName name )
2016-03-01 15:47:10 +00:00
{
2016-11-11 19:05:07 +00:00
newclass - > bRuntimeClass = true ;
2016-03-01 15:47:10 +00:00
newclass - > ParentClass = this ;
newclass - > ConstructNative = ConstructNative ;
2016-11-11 19:05:07 +00:00
newclass - > TypeName = name ;
2017-03-05 16:58:55 +00:00
newclass - > MetaSize = MetaSize ;
2016-11-11 13:40:32 +00:00
}
2016-03-01 15:47:10 +00:00
2016-11-11 13:40:32 +00:00
//==========================================================================
//
// PClassActor :: InitializeNativeDefaults
//
//==========================================================================
void PClass : : InitializeDefaults ( )
{
2017-04-12 08:20:58 +00:00
if ( IsDescendantOf ( RUNTIME_CLASS ( AActor ) ) )
2017-01-29 17:23:09 +00:00
{
2017-02-07 13:48:27 +00:00
assert ( Defaults = = nullptr ) ;
2017-03-08 17:47:52 +00:00
Defaults = ( uint8_t * ) M_Malloc ( Size ) ;
2017-01-29 17:23:09 +00:00
// run the constructor on the defaults to set the vtbl pointer which is needed to run class-aware functions on them.
// Temporarily setting bSerialOverride prevents linking into the thinker chains.
auto s = DThinker : : bSerialOverride ;
DThinker : : bSerialOverride = true ;
ConstructNative ( Defaults ) ;
DThinker : : bSerialOverride = s ;
// We must unlink the defaults from the class list because it's just a static block of data to the engine.
DObject * optr = ( DObject * ) Defaults ;
GC : : Root = optr - > ObjNext ;
optr - > ObjNext = nullptr ;
optr - > SetClass ( this ) ;
// Copy the defaults from the parent but leave the DObject part alone because it contains important data.
2017-02-07 13:48:27 +00:00
if ( ParentClass - > Defaults ! = nullptr )
2016-11-11 13:40:32 +00:00
{
2017-01-29 17:23:09 +00:00
memcpy ( Defaults + sizeof ( DObject ) , ParentClass - > Defaults + sizeof ( DObject ) , ParentClass - > Size - sizeof ( DObject ) ) ;
if ( Size > ParentClass - > Size )
{
memset ( Defaults + ParentClass - > Size , 0 , Size - ParentClass - > Size ) ;
}
}
else
{
memset ( Defaults + sizeof ( DObject ) , 0 , Size - sizeof ( DObject ) ) ;
2016-11-11 13:40:32 +00:00
}
2017-02-27 22:28:19 +00:00
2017-02-27 23:45:16 +00:00
assert ( MetaSize > = ParentClass - > MetaSize ) ;
2017-02-27 22:28:19 +00:00
if ( MetaSize ! = 0 )
{
2017-03-08 17:47:52 +00:00
Meta = ( uint8_t * ) M_Malloc ( MetaSize ) ;
2017-02-27 23:45:16 +00:00
// Copy the defaults from the parent but leave the DObject part alone because it contains important data.
if ( ParentClass - > Meta ! = nullptr )
{
memcpy ( Meta , ParentClass - > Meta , ParentClass - > MetaSize ) ;
if ( MetaSize > ParentClass - > MetaSize )
{
memset ( Meta + ParentClass - > MetaSize , 0 , MetaSize - ParentClass - > MetaSize ) ;
}
}
else
{
memset ( Meta , 0 , MetaSize ) ;
}
if ( MetaSize > 0 ) memcpy ( Meta , ParentClass - > Meta , ParentClass - > MetaSize ) ;
else memset ( Meta , 0 , MetaSize ) ;
2017-02-27 22:28:19 +00:00
}
2016-03-01 15:47:10 +00:00
}
2017-04-12 20:46:49 +00:00
if ( VMType ! = nullptr ) // purely internal classes have no symbol table
2016-11-11 13:40:32 +00:00
{
2017-04-12 20:46:49 +00:00
if ( bRuntimeClass )
{
// Copy parent values from the parent defaults.
assert ( ParentClass ! = nullptr ) ;
if ( Defaults ! = nullptr ) ParentClass - > InitializeSpecials ( Defaults , ParentClass - > Defaults , & PClass : : SpecialInits ) ;
for ( const PField * field : Fields )
{
if ( ! ( field - > Flags & VARF_Native ) & & ! ( field - > Flags & VARF_Meta ) )
{
field - > Type - > SetDefaultValue ( Defaults , unsigned ( field - > Offset ) , & SpecialInits ) ;
}
}
}
if ( Meta ! = nullptr ) ParentClass - > InitializeSpecials ( Meta , ParentClass - > Meta , & PClass : : MetaInits ) ;
2017-02-12 13:04:48 +00:00
for ( const PField * field : Fields )
2016-11-11 13:40:32 +00:00
{
2017-04-12 20:46:49 +00:00
if ( ! ( field - > Flags & VARF_Native ) & & ( field - > Flags & VARF_Meta ) )
2016-11-11 13:40:32 +00:00
{
2017-04-12 20:46:49 +00:00
field - > Type - > SetDefaultValue ( Meta , unsigned ( field - > Offset ) , & MetaInits ) ;
2016-11-11 13:40:32 +00:00
}
2017-02-27 23:45:16 +00:00
}
}
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
// PClass :: CreateDerivedClass
//
// Create a new class based on an existing class
//
//==========================================================================
PClass * PClass : : CreateDerivedClass ( FName name , unsigned int size )
{
2017-02-08 18:52:33 +00:00
assert ( size > = Size ) ;
2016-03-01 15:47:10 +00:00
PClass * type ;
bool notnew ;
2017-02-08 18:52:33 +00:00
const PClass * existclass = FindClass ( name ) ;
2016-03-01 15:47:10 +00:00
2017-02-23 18:03:31 +00:00
if ( existclass ! = nullptr )
2016-03-01 15:47:10 +00:00
{
2017-02-23 18:03:31 +00:00
// This is a placeholder so fill it in
if ( existclass - > Size = = TentativeClass )
2016-03-01 15:47:10 +00:00
{
2017-02-23 18:03:31 +00:00
type = const_cast < PClass * > ( existclass ) ;
if ( ! IsDescendantOf ( type - > ParentClass ) )
{
I_Error ( " %s must inherit from %s but doesn't. " , name . GetChars ( ) , type - > ParentClass - > TypeName . GetChars ( ) ) ;
}
DPrintf ( DMSG_SPAMMY , " Defining placeholder class %s \n " , name . GetChars ( ) ) ;
notnew = true ;
}
else
{
// a different class with the same name already exists. Let the calling code deal with this.
return nullptr ;
2016-03-01 15:47:10 +00:00
}
}
else
{
2017-04-12 20:46:49 +00:00
type = new PClass ;
2016-03-01 15:47:10 +00:00
notnew = false ;
}
2017-02-08 18:52:33 +00:00
type - > TypeName = name ;
type - > bRuntimeClass = true ;
2016-11-11 19:05:07 +00:00
Derive ( type , name ) ;
2016-11-12 12:09:19 +00:00
type - > Size = size ;
if ( size ! = TentativeClass )
{
2017-04-12 20:46:49 +00:00
NewClassType ( type ) ;
2016-11-12 12:09:19 +00:00
type - > InitializeDefaults ( ) ;
type - > Virtuals = Virtuals ;
}
2017-02-08 18:52:33 +00:00
else
2017-04-12 20:46:49 +00:00
type - > bOptional = false ;
2017-02-08 18:52:33 +00:00
2016-03-01 15:47:10 +00:00
if ( ! notnew )
{
type - > InsertIntoHash ( ) ;
}
return type ;
}
//==========================================================================
2016-04-03 21:18:59 +00:00
//
// PClass :: AddField
//
//==========================================================================
2017-03-08 17:50:37 +00:00
PField * PClass : : AddField ( FName name , PType * type , uint32_t flags )
2016-04-03 21:18:59 +00:00
{
2017-04-12 15:21:13 +00:00
PField * field ;
2017-02-27 22:28:19 +00:00
if ( ! ( flags & VARF_Meta ) )
{
unsigned oldsize = Size ;
2017-04-12 20:46:49 +00:00
field = VMType - > Symbols . AddField ( name , type , flags , Size ) ;
2016-10-09 23:18:47 +00:00
2017-02-27 22:28:19 +00:00
// Only initialize the defaults if they have already been created.
// For ZScript this is not the case, it will first define all fields before
// setting up any defaults for any class.
if ( field ! = nullptr & & ! ( flags & VARF_Native ) & & Defaults ! = nullptr )
{
2017-03-08 17:47:52 +00:00
Defaults = ( uint8_t * ) M_Realloc ( Defaults , Size ) ;
2017-02-27 22:28:19 +00:00
memset ( Defaults + oldsize , 0 , Size - oldsize ) ;
}
}
else
2016-10-09 23:18:47 +00:00
{
2017-04-12 15:21:13 +00:00
// Same as above, but a different data storage.
2017-02-27 22:28:19 +00:00
unsigned oldsize = MetaSize ;
2017-04-12 20:46:49 +00:00
field = VMType - > Symbols . AddField ( name , type , flags , MetaSize ) ;
2017-02-27 22:28:19 +00:00
2017-02-28 13:44:18 +00:00
if ( field ! = nullptr & & ! ( flags & VARF_Native ) & & Meta ! = nullptr )
2017-02-27 22:28:19 +00:00
{
2017-03-08 17:47:52 +00:00
Meta = ( uint8_t * ) M_Realloc ( Meta , MetaSize ) ;
2017-02-27 22:28:19 +00:00
memset ( Meta + oldsize , 0 , MetaSize - oldsize ) ;
}
2016-04-03 21:18:59 +00:00
}
2017-04-12 15:21:13 +00:00
if ( field ! = nullptr ) Fields . Push ( field ) ;
return field ;
}
2016-04-03 21:18:59 +00:00
//==========================================================================
2016-03-01 15:47:10 +00:00
//
// PClass :: FindClassTentative
//
// Like FindClass but creates a placeholder if no class is found.
2016-11-11 19:05:07 +00:00
// This will be filled in when the actual class is constructed.
2016-03-01 15:47:10 +00:00
//
//==========================================================================
2016-11-12 12:09:19 +00:00
PClass * PClass : : FindClassTentative ( FName name )
2016-03-01 15:47:10 +00:00
{
if ( name = = NAME_None )
{
2017-02-07 13:48:27 +00:00
return nullptr ;
2016-03-01 15:47:10 +00:00
}
2017-04-12 20:46:49 +00:00
PClass * found = FindClass ( name ) ;
if ( found ! = nullptr ) return found ;
2016-03-01 15:47:10 +00:00
2017-04-12 20:46:49 +00:00
PClass * type = new PClass ;
2016-08-28 07:55:04 +00:00
DPrintf ( DMSG_SPAMMY , " Creating placeholder class %s : %s \n " , name . GetChars ( ) , TypeName . GetChars ( ) ) ;
2016-03-01 15:47:10 +00:00
2016-11-11 19:05:07 +00:00
Derive ( type , name ) ;
2016-03-01 15:47:10 +00:00
type - > Size = TentativeClass ;
2017-04-12 20:46:49 +00:00
2017-04-13 09:03:45 +00:00
type - > InsertIntoHash ( ) ;
2016-03-01 15:47:10 +00:00
return type ;
}
2016-11-11 19:05:07 +00:00
//==========================================================================
//
// PClass :: FindVirtualIndex
//
// Compares a prototype with the existing list of virtual functions
// and returns an index if something matching is found.
//
//==========================================================================
int PClass : : FindVirtualIndex ( FName name , PPrototype * proto )
{
for ( unsigned i = 0 ; i < Virtuals . Size ( ) ; i + + )
{
if ( Virtuals [ i ] - > Name = = name )
{
auto vproto = Virtuals [ i ] - > Proto ;
if ( vproto - > ReturnTypes . Size ( ) ! = proto - > ReturnTypes . Size ( ) | |
vproto - > ArgumentTypes . Size ( ) ! = proto - > ArgumentTypes . Size ( ) )
{
continue ; // number of parameters does not match, so it's incompatible
}
bool fail = false ;
// The first argument is self and will mismatch so just skip it.
for ( unsigned a = 1 ; a < proto - > ArgumentTypes . Size ( ) ; a + + )
{
if ( proto - > ArgumentTypes [ a ] ! = vproto - > ArgumentTypes [ a ] )
{
fail = true ;
break ;
}
}
if ( fail ) continue ;
for ( unsigned a = 0 ; a < proto - > ReturnTypes . Size ( ) ; a + + )
{
if ( proto - > ReturnTypes [ a ] ! = vproto - > ReturnTypes [ a ] )
{
fail = true ;
break ;
}
}
if ( ! fail ) return i ;
}
}
return - 1 ;
}
2017-04-12 11:08:41 +00:00
PSymbol * PClass : : FindSymbol ( FName symname , bool searchparents ) const
{
2017-04-12 20:46:49 +00:00
if ( VMType = = nullptr ) return nullptr ;
return VMType - > Symbols . FindSymbol ( symname , searchparents ) ;
2017-04-12 11:08:41 +00:00
}
2016-03-01 15:47:10 +00:00
//==========================================================================
//
// PClass :: BuildFlatPointers
//
// Create the FlatPointers array, if it doesn't exist already.
// It comprises all the Pointers from superclasses plus this class's own
// Pointers. If this class does not define any new Pointers, then
// FlatPointers will be set to the same array as the super class.
//
//==========================================================================
void PClass : : BuildFlatPointers ( )
{
2017-02-07 13:48:27 +00:00
if ( FlatPointers ! = nullptr )
2016-03-01 15:47:10 +00:00
{ // Already built: Do nothing.
return ;
}
2017-02-07 13:48:27 +00:00
else if ( ParentClass = = nullptr )
2016-11-17 21:21:08 +00:00
{ // No parent (i.e. DObject: FlatPointers is the same as Pointers.
2017-02-07 13:48:27 +00:00
if ( Pointers = = nullptr )
{ // No pointers: Make FlatPointers a harmless non-nullptr.
2016-03-01 15:47:10 +00:00
FlatPointers = & TheEnd ;
}
else
{
FlatPointers = Pointers ;
}
}
else
{
ParentClass - > BuildFlatPointers ( ) ;
2016-11-17 21:21:08 +00:00
TArray < size_t > ScriptPointers ;
// Collect all pointers in scripted fields. These are not part of the Pointers list.
for ( auto field : Fields )
{
if ( ! ( field - > Flags & VARF_Native ) )
{
field - > Type - > SetPointer ( Defaults , unsigned ( field - > Offset ) , & ScriptPointers ) ;
}
}
if ( Pointers = = nullptr & & ScriptPointers . Size ( ) = = 0 )
2016-03-01 15:47:10 +00:00
{ // No new pointers: Just use the same FlatPointers as the parent.
FlatPointers = ParentClass - > FlatPointers ;
}
else
{ // New pointers: Create a new FlatPointers array and add them.
int numPointers , numSuperPointers ;
2016-11-17 21:21:08 +00:00
if ( Pointers ! = nullptr )
{
// Count pointers defined by this class.
for ( numPointers = 0 ; Pointers [ numPointers ] ! = ~ ( size_t ) 0 ; numPointers + + )
{
}
}
else numPointers = 0 ;
2016-03-01 15:47:10 +00:00
// Count pointers defined by superclasses.
for ( numSuperPointers = 0 ; ParentClass - > FlatPointers [ numSuperPointers ] ! = ~ ( size_t ) 0 ; numSuperPointers + + )
{ }
// Concatenate them into a new array
2017-02-08 19:37:22 +00:00
size_t * flat = ( size_t * ) ClassDataAllocator . Alloc ( sizeof ( size_t ) * ( numPointers + numSuperPointers + ScriptPointers . Size ( ) + 1 ) ) ;
2016-03-01 15:47:10 +00:00
if ( numSuperPointers > 0 )
{
memcpy ( flat , ParentClass - > FlatPointers , sizeof ( size_t ) * numSuperPointers ) ;
}
2016-11-17 21:21:08 +00:00
if ( numPointers > 0 )
{
memcpy ( flat + numSuperPointers , Pointers , sizeof ( size_t ) * numPointers ) ;
}
if ( ScriptPointers . Size ( ) > 0 )
{
memcpy ( flat + numSuperPointers + numPointers , & ScriptPointers [ 0 ] , sizeof ( size_t ) * ScriptPointers . Size ( ) ) ;
}
flat [ numSuperPointers + numPointers + ScriptPointers . Size ( ) ] = ~ ( size_t ) 0 ;
2016-03-01 15:47:10 +00:00
FlatPointers = flat ;
}
}
}
2017-02-07 13:48:27 +00:00
//==========================================================================
//
// PClass :: BuildArrayPointers
//
// same as above, but creates a list to dynamic object arrays
//
//==========================================================================
void PClass : : BuildArrayPointers ( )
{
if ( ArrayPointers ! = nullptr )
{ // Already built: Do nothing.
return ;
}
else if ( ParentClass = = nullptr )
{ // No parent (i.e. DObject: FlatPointers is the same as Pointers.
ArrayPointers = & TheEnd ;
}
else
{
ParentClass - > BuildArrayPointers ( ) ;
TArray < size_t > ScriptPointers ;
// Collect all arrays to pointers in scripted fields.
for ( auto field : Fields )
{
if ( ! ( field - > Flags & VARF_Native ) )
{
field - > Type - > SetPointerArray ( Defaults , unsigned ( field - > Offset ) , & ScriptPointers ) ;
}
}
if ( ScriptPointers . Size ( ) = = 0 )
{ // No new pointers: Just use the same ArrayPointers as the parent.
ArrayPointers = ParentClass - > ArrayPointers ;
}
else
{ // New pointers: Create a new FlatPointers array and add them.
int numSuperPointers ;
// Count pointers defined by superclasses.
for ( numSuperPointers = 0 ; ParentClass - > ArrayPointers [ numSuperPointers ] ! = ~ ( size_t ) 0 ; numSuperPointers + + )
{
}
// Concatenate them into a new array
2017-02-08 19:37:22 +00:00
size_t * flat = ( size_t * ) ClassDataAllocator . Alloc ( sizeof ( size_t ) * ( numSuperPointers + ScriptPointers . Size ( ) + 1 ) ) ;
2017-02-07 13:48:27 +00:00
if ( numSuperPointers > 0 )
{
memcpy ( flat , ParentClass - > ArrayPointers , sizeof ( size_t ) * numSuperPointers ) ;
}
if ( ScriptPointers . Size ( ) > 0 )
{
memcpy ( flat + numSuperPointers , & ScriptPointers [ 0 ] , sizeof ( size_t ) * ScriptPointers . Size ( ) ) ;
}
flat [ numSuperPointers + ScriptPointers . Size ( ) ] = ~ ( size_t ) 0 ;
ArrayPointers = flat ;
}
}
}
2016-03-01 15:47:10 +00:00
//==========================================================================
//
// PClass :: NativeClass
//
2016-10-08 11:34:37 +00:00
// Finds the native type underlying this class.
2016-03-01 15:47:10 +00:00
//
//==========================================================================
const PClass * PClass : : NativeClass ( ) const
{
const PClass * cls = this ;
while ( cls & & cls - > bRuntimeClass )
cls = cls - > ParentClass ;
return cls ;
}
2016-11-28 13:39:25 +00:00
VMFunction * PClass : : FindFunction ( FName clsname , FName funcname )
{
2017-02-13 16:45:03 +00:00
auto cls = PClass : : FindClass ( clsname ) ;
2016-11-28 13:39:25 +00:00
if ( ! cls ) return nullptr ;
2017-04-12 11:08:41 +00:00
auto func = dyn_cast < PFunction > ( cls - > FindSymbol ( funcname , true ) ) ;
2016-11-28 13:39:25 +00:00
if ( ! func ) return nullptr ;
return func - > Variants [ 0 ] . Implementation ;
}
2017-01-31 12:41:23 +00:00
void PClass : : FindFunction ( VMFunction * * pptr , FName clsname , FName funcname )
{
2017-02-13 16:45:03 +00:00
auto cls = PClass : : FindClass ( clsname ) ;
2017-01-31 12:41:23 +00:00
if ( ! cls ) return ;
2017-04-12 11:08:41 +00:00
auto func = dyn_cast < PFunction > ( cls - > FindSymbol ( funcname , true ) ) ;
2017-01-31 12:41:23 +00:00
if ( ! func ) return ;
* pptr = func - > Variants [ 0 ] . Implementation ;
FunctionPtrList . Push ( pptr ) ;
}
2017-04-12 23:12:04 +00:00
unsigned GetVirtualIndex ( PClass * cls , const char * funcname )
2016-03-01 15:47:10 +00:00
{
2017-04-12 23:12:04 +00:00
// Look up the virtual function index in the defining class because this may have gotten overloaded in subclasses with something different than a virtual override.
auto sym = dyn_cast < PFunction > ( cls - > FindSymbol ( funcname , false ) ) ;
assert ( sym ! = nullptr ) ;
auto VIndex = sym - > Variants [ 0 ] . Implementation - > VirtualIndex ;
return VIndex ;
2016-03-01 15:47:10 +00:00
}