Paste will expire never.
- #include "stdafx.h"
- #include <assert.h>
- #include <string.h> // strstr
- #include <stdio.h> // sprintf
- #include "scriptreloader.h"
- #include "scriptstring.h"
- #include "scriptarray.h"
- using namespace std;
- BEGIN_AS_NAMESPACE
- void CScriptReloaderVariable::Store(void *ref, int _typeId)
- {
- is_init = true;
- setType( _typeId );
- ptr = ref;
- if( type_id & asTYPEID_OBJHANDLE )
- {
- handle_ptr = *(void**)ref;
- }
- else if( type_id & asTYPEID_SCRIPTOBJECT )
- {
- asIScriptObject *obj = (asIScriptObject *)ref;
- asIObjectType *type = obj->GetObjectType();
- setType( type->GetTypeId() );
- // Store childs
- for(int i =0; i < type->GetPropertyCount(); i++ )
- {
- int child_id;
- const char *child_name;
- type->GetProperty( i, &child_name, &child_id );
- childs.push_back( CScriptReloaderVariable( this, child_name, obj->GetAddressOfProperty( i ), child_id ) );
- }
- }
- else
- {
- int size = reloader->engine->GetSizeOfPrimitiveType( type_id );
- if( size == 0 )
- {
- if( type_id_str== "string" )
- {
- CScriptString *txt_ref = ((CScriptString*)ref);
- txt_val = txt_ref->buffer;
- }
- else if( type_id_str == "array" )
- {
- CScriptArray *array = (CScriptArray*)ref;
- for( int i =0; i < array->GetSize(); i++ )
- childs.push_back( CScriptReloaderVariable( this, "", array->At( i ), array->GetElementTypeId() ) );
- }
- else if( getType() )
- {
- size = getType()->GetSize();
- }
- }
- if( size )
- {
- mem.resize( size );
- memcpy(&mem[0], ref, size);
- }
- }
- }
- void CScriptReloaderVariable::Restore(void *ref)
- {
- if( !this || !is_init || !ref )
- return;
- restore_ptr = ref;
- if( type_id & asTYPEID_OBJHANDLE )
- {
- // if need create objects
- if( childs.size() == 1 )
- {
- asIObjectType *type = childs[0].getType();
- void *new_obejct = reloader->engine->CreateScriptObject( type->GetTypeId() );
- childs[0].Restore( new_obejct );
- }
- }
- else if( type_id & asTYPEID_SCRIPTOBJECT )
- {
- asIScriptObject *obj = (asIScriptObject *)ref;
- asIObjectType *type = getType();
- // Retrieve children s
- for( int i =0; i < type->GetPropertyCount() ; i++ )
- {
- const char *name_property;
- type->GetProperty(i, &name_property );
- child( name_property )->Restore( obj->GetAddressOfProperty( i ) );
- }
- }
- else
- {
- if( mem.size() )
- memcpy( ref, &mem[0], mem.size());
- else
- {
- if( type_id_str == "string" )
- {
- CScriptString *ref_txt = ((CScriptString*)ref);
- ref_txt->buffer = txt_val;
- }
- else if( type_id_str == "array" )
- {
- CScriptArray *array = (CScriptArray*)ref;
- array->Resize( childs.size() );
- for( size_t i =0; i < childs.size(); ++i )
- childs[i].Restore( array->At(i) );
- }
- }
- }
- }
- void CScriptReloaderVariable::canselDublicate( CScriptReloaderVariable *from )
- {
- std::vector<void*> ptrs;
- from->childsPtr( &ptrs );
- for( size_t i=0; i < ptrs.size(); ++i )
- {
- CScriptReloaderVariable *find = reloader->root.findByPtrInCreated( ptrs[i] );
- while( find )
- {
- // cancel create object
- find->childs.clear();
- // Find next link to this ptr
- find = reloader->root.findByPtrInCreated( ptrs[i] );
- }
- }
- }
- void CScriptReloaderVariable::StoreHandle()
- {
- // Find to
- if( handle_ptr )
- {
- CScriptReloaderVariable *handle_to = reloader->root.findByPtr( handle_ptr );
- // if handle is not found in global space...
- if( handle_to == nullptr )
- {
- asIObjectType *type = getType();
- CScriptReloaderVariable need_create = CScriptReloaderVariable( this, name, handle_ptr, type->GetTypeId() );
- canselDublicate( &need_create );
- childs.push_back( need_create );
- }
- }
- // Childs...
- for( size_t i=0; i < childs.size(); ++i )
- childs[i].StoreHandle();
- }
- void CScriptReloaderVariable::RestoreHandle()
- {
- CScriptReloaderVariable *handle_to = reloader->root.findByPtr( handle_ptr );
- // Restore handle!
- if( restore_ptr && handle_to && handle_to->restore_ptr )
- *(void**)restore_ptr = handle_to->restore_ptr;
- // Childs...
- for( size_t i=0; i < childs.size(); ++i )
- childs[i].RestoreHandle();
- }
- void CScriptReloaderVariable::setType( int _typeId )
- {
- type_id = _typeId;
- asIObjectType *type = reloader->engine->GetObjectTypeById( type_id );
- if( type )
- type_id_str = type->GetName();
- }
- asIObjectType *CScriptReloaderVariable::getType()
- {
- if( !type_id_str.empty() )
- {
- int new_type_id = reloader->mod->GetTypeIdByDecl( type_id_str.c_str() );
- return reloader->engine->GetObjectTypeById( new_type_id );
- }
- return nullptr;
- }
- END_AS_NAMESPACE