Completed script serialization in YAML

This commit is contained in:
Kah Wei 2022-10-20 11:08:20 +08:00
parent 4bc91283c8
commit 166a036142
7 changed files with 117 additions and 42 deletions

View File

@ -128,7 +128,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Script Serialisation Functions */ /* Script Serialisation Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
bool SHScriptEngine::DeserialiseScript(EntityID entity, YAML::Node& scriptsNode) const bool SHScriptEngine::DeserialiseScripts(EntityID entity, const YAML::Node& scriptsNode) const
{ {
return csScriptsDeserialiseYaml(entity, &scriptsNode); return csScriptsDeserialiseYaml(entity, &scriptsNode);
} }
@ -374,7 +374,7 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScripts" "SerialiseScripts"
); );
csScriptsDeserialiseYaml = dotNet.GetFunctionPtr<CsScriptSerialiseYamlFuncPtr> csScriptsDeserialiseYaml = dotNet.GetFunctionPtr<CsScriptDeserialiseYamlFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",

View File

@ -161,7 +161,7 @@ namespace SHADE
/// YAML Node that contains the serialised script data. /// YAML Node that contains the serialised script data.
/// </param> /// </param>
/// <return>True if successfully deserialised.</return> /// <return>True if successfully deserialised.</return>
bool DeserialiseScript(EntityID entity, YAML::Node& scriptsNode) const; bool DeserialiseScripts(EntityID entity, const YAML::Node& scriptsNode) const;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Script Editor Functions */ /* Script Editor Functions */
@ -211,13 +211,13 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
using CsFuncPtr = void(*)(void); using CsFuncPtr = void(*)(void);
using CsScriptManipFuncPtr = bool(*)(EntityID, const char*); using CsScriptManipFuncPtr = bool(*)(EntityID, const char*);
using CsScriptBasicFuncPtr = void(*)(EntityID); using CsScriptBasicFuncPtr = void(*)(EntityID);
using CsScriptOptionalFuncPtr = void(*)(EntityID, bool); using CsScriptOptionalFuncPtr = void(*)(EntityID, bool);
using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*); using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*);
using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*); using CsScriptDeserialiseYamlFuncPtr = bool(*)(EntityID, const void*);
using CsScriptEditorFuncPtr = void(*)(EntityID); using CsScriptEditorFuncPtr = void(*)(EntityID);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Constants */ /* Constants */
@ -231,29 +231,29 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
SHDotNetRuntime dotNet { false }; SHDotNetRuntime dotNet { false };
// Function Pointers to CLR Code // Function Pointers to CLR Code
// - Engine Lifecycle // - Engine Lifecycle
CsFuncPtr csEngineInit = nullptr; CsFuncPtr csEngineInit = nullptr;
CsFuncPtr csEngineLoadScripts = nullptr; CsFuncPtr csEngineLoadScripts = nullptr;
CsFuncPtr csEngineUnloadScripts = nullptr; CsFuncPtr csEngineUnloadScripts = nullptr;
CsFuncPtr csEngineReloadScripts = nullptr; CsFuncPtr csEngineReloadScripts = nullptr;
CsFuncPtr csEngineExit = nullptr; CsFuncPtr csEngineExit = nullptr;
// - Scripts Store // - Scripts Store
CsFuncPtr csScriptsFrameSetUp = nullptr; CsFuncPtr csScriptsFrameSetUp = nullptr;
CsFuncPtr csScriptsExecuteFixedUpdate = nullptr; CsFuncPtr csScriptsExecuteFixedUpdate = nullptr;
CsFuncPtr csScriptsExecuteUpdate = nullptr; CsFuncPtr csScriptsExecuteUpdate = nullptr;
CsFuncPtr csScriptsExecuteLateUpdate = nullptr; CsFuncPtr csScriptsExecuteLateUpdate = nullptr;
CsFuncPtr csScriptsFrameCleanUp = nullptr; CsFuncPtr csScriptsFrameCleanUp = nullptr;
CsScriptManipFuncPtr csScriptsAdd = nullptr; CsScriptManipFuncPtr csScriptsAdd = nullptr;
CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr; CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr;
CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr; CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml = nullptr; CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr; CsScriptDeserialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr;
// - Editor // - Editor
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
CsFuncPtr csEditorUndo = nullptr; CsFuncPtr csEditorUndo = nullptr;
CsFuncPtr csEditorRedo = nullptr; CsFuncPtr csEditorRedo = nullptr;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Event Handler Functions */ /* Event Handler Functions */

View File

@ -79,6 +79,10 @@ namespace SHADE
} }
} }
} }
// Deserialise scripts
if (node[ScriptsNode])
SHSystemManager::GetSystem<SHScriptEngine>()->DeserialiseScripts(eid, node[ScriptsNode]);
} }
void SHSerialization::DeserializeSceneFromFile(std::filesystem::path const& path) void SHSerialization::DeserializeSceneFromFile(std::filesystem::path const& path)
@ -186,9 +190,6 @@ namespace SHADE
SHSystemManager::GetSystem<SHScriptEngine>()->SerialiseScripts(eid, scripts); SHSystemManager::GetSystem<SHScriptEngine>()->SerialiseScripts(eid, scripts);
node[ScriptsNode] = scripts; node[ScriptsNode] = scripts;
//YAML::Emitter emitter;
//emitter << node;
//SHLOG_TRACE(emitter.c_str())
return node; return node;
} }

View File

@ -481,14 +481,14 @@ namespace SHADE
// Check if yamlNode is valid // Check if yamlNode is valid
if (yamlNode == nullptr) if (yamlNode == nullptr)
{ {
Debug::LogWarning("Attempted to serialise scripts with an invalid YAML Node! Skipping."); Debug::LogWarning("[ScriptStore] Attempted to serialise scripts with an invalid YAML Node! Skipping.");
return false; return false;
} }
// Check if entity exists, otherwise nothing // Check if entity exists, otherwise nothing
if (!EntityUtils::IsValid(entity)) if (!EntityUtils::IsValid(entity))
{ {
Debug::LogWarning("Attempted to serialise scripts for an invalid Entity! Skipping."); Debug::LogWarning("[ScriptStore] Attempted to serialise scripts for an invalid Entity! Skipping.");
return false; return false;
} }
@ -518,14 +518,14 @@ namespace SHADE
// Check if yamlNode is valid // Check if yamlNode is valid
if (yamlNode == nullptr) if (yamlNode == nullptr)
{ {
Debug::LogWarning("Attempted to deserialise scripts with an invalid YAML Node! Skipping."); Debug::LogWarning("[ScriptStore] Attempted to deserialise scripts with an invalid YAML Node! Skipping.");
return false; return false;
} }
// Check if entity exists, otherwise nothing // Check if entity exists, otherwise nothing
if (!EntityUtils::IsValid(entity)) if (!EntityUtils::IsValid(entity))
{ {
Debug::LogWarning("Attempted to deserialise scripts for an invalid Entity! Skipping."); Debug::LogWarning("[ScriptStore] Attempted to deserialise scripts for an invalid Entity! Skipping.");
return false; return false;
} }
@ -533,17 +533,27 @@ namespace SHADE
for (YAML::Node& node : *yamlNode) for (YAML::Node& node : *yamlNode)
{ {
// Get the name of the script // Get the name of the script
const std::string typeName = ""; // TODO if (!node["Type"])
{
Debug::LogWarning("[ScriptStore] Script with no type detected, skipping.");
continue;
}
System::String^ typeName = Convert::ToCLI(node["Type"].as<std::string>());
// Create // Create
Script^ script; Script^ script;
if (AddScriptViaNameWithRef(entity, Convert::ToCLI(typeName), script)) if (AddScriptViaNameWithRef(entity, typeName, script))
{ {
// Copy the data in // Copy the data in
ReflectionUtilities::Deserialise(script, node); ReflectionUtilities::Deserialise(script, node);
return true; }
else
{
Debug::LogWarning("[ScriptStore] Script with unloaded type detected, skipping.");
} }
} }
return true;
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
return false; return false;

View File

@ -27,7 +27,8 @@ namespace SHADE
{ {
if (fieldInfo->FieldType == FieldType::typeid) if (fieldInfo->FieldType == FieldType::typeid)
{ {
fieldNode = 0.0;//static_cast<double>(safe_cast<FieldType>(fieldInfo->GetValue(object))); const FieldType VALUE = safe_cast<FieldType>(fieldInfo->GetValue(object));
fieldNode = static_cast<FieldType>(VALUE);
return true; return true;
} }
@ -37,7 +38,7 @@ namespace SHADE
template<typename FieldType> template<typename FieldType>
bool ReflectionUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) bool ReflectionUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node)
{ {
return fieldAssignYaml<FieldType, FieldType>(fieldInfo, object, node); return fieldAssignYaml<FieldType, ToNativeType_T<FieldType>>(fieldInfo, object, node);
} }
template<typename FieldType, typename CastType> template<typename FieldType, typename CastType>

View File

@ -63,7 +63,6 @@ namespace SHADE
/// <param name="object">The object to copy deserialised data into.</param> /// <param name="object">The object to copy deserialised data into.</param>
static void Deserialise(System::Object^ object, YAML::Node& yamlNode); static void Deserialise(System::Object^ object, YAML::Node& yamlNode);
private: private:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Serialization Helper Functions */ /* Serialization Helper Functions */

View File

@ -91,4 +91,68 @@ namespace SHADE
/// <returns>Managed copy of a native std::string.</returns> /// <returns>Managed copy of a native std::string.</returns>
static System::String^ ToCLI(const std::string& str); static System::String^ ToCLI(const std::string& str);
}; };
/// <summary>
/// Type Transformer for managed types to native types.
/// </summary>
/// <typeparam name="ManagedType">
/// Managed type to get the native type for.
/// </typeparam>
template<typename ManagedType>
struct ToNativeType
{
public:
using Value = void;
};
template<> struct ToNativeType<System::Int16> { using Value = int16_t; };
template<> struct ToNativeType<System::Int32> { using Value = int32_t; };
template<> struct ToNativeType<System::Int64> { using Value = int64_t; };
template<> struct ToNativeType<System::UInt16> { using Value = uint16_t; };
template<> struct ToNativeType<System::UInt32> { using Value = uint32_t; };
template<> struct ToNativeType<System::UInt64> { using Value = uint64_t; };
template<> struct ToNativeType<System::Byte> { using Value = int8_t; };
template<> struct ToNativeType<bool> { using Value = bool; };
template<> struct ToNativeType<double> { using Value = double; };
template<> struct ToNativeType<float> { using Value = float; };
/// <summary>
/// Alias for ToNativeType::Value
/// </summary>
/// <typeparam name="ManagedType">
/// Managed type to get the native type for.
/// </typeparam>
template<typename ManagedType>
using ToNativeType_T = typename ToNativeType<ManagedType>::Value;
/// <summary>
/// Type Transformer for native types to managed types.
/// </summary>
/// <typeparam name="ManagedType">
/// Managed type to get the native type for.
/// </typeparam>
template<typename NativeType>
struct ToManagedType
{
public:
using Value = void;
};
template<> struct ToManagedType<int8_t> { using Value = System::Byte; };
template<> struct ToManagedType<int16_t> { using Value = System::Int16; };
template<> struct ToManagedType<int32_t> { using Value = System::Int32; };
template<> struct ToManagedType<int64_t> { using Value = System::Int64; };
template<> struct ToManagedType<uint16_t> { using Value = System::UInt16; };
template<> struct ToManagedType<uint32_t> { using Value = System::UInt32; };
template<> struct ToManagedType<uint64_t> { using Value = System::UInt64; };
template<> struct ToManagedType<bool> { using Value = bool; };
template<> struct ToManagedType<double> { using Value = double; };
template<> struct ToManagedType<float> { using Value = float; };
/// <summary>
/// Alias for ToManagedType::Value
/// </summary>
/// <typeparam name="ManagedType">
/// Managed type to get the native type for.
/// </typeparam>
template<typename NativeType>
using ToManagedType_T = typename ToManagedType<NativeType>::Value;
} }