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

View File

@ -161,7 +161,7 @@ namespace SHADE
/// YAML Node that contains the serialised script data.
/// </param>
/// <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 */
@ -216,7 +216,7 @@ namespace SHADE
using CsScriptBasicFuncPtr = void(*)(EntityID);
using CsScriptOptionalFuncPtr = void(*)(EntityID, bool);
using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*);
using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*);
using CsScriptDeserialiseYamlFuncPtr = bool(*)(EntityID, const void*);
using CsScriptEditorFuncPtr = void(*)(EntityID);
/*-----------------------------------------------------------------------------*/
@ -249,7 +249,7 @@ namespace SHADE
CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr;
CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr;
CsScriptDeserialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr;
// - Editor
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
CsFuncPtr csEditorUndo = nullptr;

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)
@ -186,9 +190,6 @@ namespace SHADE
SHSystemManager::GetSystem<SHScriptEngine>()->SerialiseScripts(eid, scripts);
node[ScriptsNode] = scripts;
//YAML::Emitter emitter;
//emitter << node;
//SHLOG_TRACE(emitter.c_str())
return node;
}

View File

@ -481,14 +481,14 @@ namespace SHADE
// Check if yamlNode is valid
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;
}
// Check if entity exists, otherwise nothing
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;
}
@ -518,14 +518,14 @@ namespace SHADE
// Check if yamlNode is valid
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;
}
// Check if entity exists, otherwise nothing
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;
}
@ -533,17 +533,27 @@ namespace SHADE
for (YAML::Node& node : *yamlNode)
{
// 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
Script^ script;
if (AddScriptViaNameWithRef(entity, Convert::ToCLI(typeName), script))
if (AddScriptViaNameWithRef(entity, typeName, script))
{
// Copy the data in
ReflectionUtilities::Deserialise(script, node);
}
else
{
Debug::LogWarning("[ScriptStore] Script with unloaded type detected, skipping.");
}
}
return true;
}
}
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
return false;

View File

@ -27,7 +27,8 @@ namespace SHADE
{
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;
}
@ -37,7 +38,7 @@ namespace SHADE
template<typename FieldType>
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>

View File

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

View File

@ -91,4 +91,68 @@ namespace SHADE
/// <returns>Managed copy of a native std::string.</returns>
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;
}