MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / CreateInstance

Method CreateInstance

Source/Engine/Content/JsonAsset.cpp:457–524  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

455}
456
457bool JsonAsset::CreateInstance()
458{
459 ScopeLock lock(Locker);
460 if (Instance)
461 return false;
462 PROFILE_MEM(ContentAssets);
463
464 // Try to scripting type for this data
465 const StringAsANSI<> dataTypeNameAnsi(DataTypeName.Get(), DataTypeName.Length());
466 const auto typeHandle = Scripting::FindScriptingType(StringAnsiView(dataTypeNameAnsi.Get(), DataTypeName.Length()));
467 if (typeHandle)
468 {
469 auto& type = typeHandle.GetType();
470
471 // Ensure that object can deserialized
472 const ScriptingType::InterfaceImplementation* interface = type.GetInterface(ISerializable::TypeInitializer);
473 if (!interface)
474 {
475 LOG(Warning, "Cannot deserialize {0} from Json Asset because it doesn't implement ISerializable interface.", type.ToString());
476 return false;
477 }
478 auto modifier = Cache::ISerializeModifier.Get();
479 modifier->EngineBuild = DataEngineBuild;
480
481 // Create object
482 switch (type.Type)
483 {
484 case ScriptingTypes::Class:
485 case ScriptingTypes::Structure:
486 {
487 const auto instance = Allocator::Allocate(type.Size);
488 if (!instance)
489 return true;
490 Instance = instance;
491 if (type.Type == ScriptingTypes::Class)
492 {
493 _dtor = type.Class.Dtor;
494 type.Class.Ctor(instance);
495 }
496 else
497 {
498 _dtor = type.Struct.Dtor;
499 type.Struct.Ctor(instance);
500 }
501
502 // Deserialize object
503 ((ISerializable*)((byte*)instance + interface->VTableOffset))->Deserialize(*Data, modifier.Value);
504 break;
505 }
506 case ScriptingTypes::Script:
507 {
508 const ScriptingObjectSpawnParams params(Guid::New(), typeHandle);
509 const auto instance = type.Script.Spawn(params);
510 if (!instance)
511 return true;
512 Instance = instance;
513 _dtor = nullptr;
514

Callers 15

InitStructureMethod · 0.45
ReadVariantMethod · 0.45
ConvertToMethod · 0.45
GetInstanceFunction · 0.45
ProcessGroupPackingMethod · 0.45
AddChildControlMethod · 0.45
DeserializeMethod · 0.45
ConvertToMethod · 0.45
AddChildMethod · 0.45
InitCustomNodeMethod · 0.45
ifFunction · 0.45
ConfigureMethod · 0.45

Calls 10

FindScriptingTypeFunction · 0.85
GetInterfaceMethod · 0.80
StringAnsiViewClass · 0.50
NewFunction · 0.50
GetMethod · 0.45
LengthMethod · 0.45
GetTypeMethod · 0.45
ToStringMethod · 0.45
DeserializeMethod · 0.45
SpawnMethod · 0.45

Tested by

no test coverage detected