Added Script Serialization and Deserialization #102

Merged
Pycorax merged 5 commits from SP3-6-c-scripting into main 2022-10-20 11:19:10 +08:00
3 changed files with 13 additions and 6 deletions
Showing only changes of commit cfed342f9c - Show all commits

View File

@ -497,7 +497,7 @@ namespace SHADE
return true; return true;
// Serialise each script // Serialise each script
//yamlNode->SetStyle(YAML::EmitterStyle::Block); yamlNode->SetStyle(YAML::EmitterStyle::Block);
System::Collections::Generic::List<Script^>^ scriptList = scripts[entity]; System::Collections::Generic::List<Script^>^ scriptList = scripts[entity];
for each (Script^ script in scriptList) for each (Script^ script in scriptList)
{ {

View File

@ -38,6 +38,11 @@ if (iter != jsonValue.MemberEnd()) \
vec.MEMBER = iter->value.GetDouble(); \ vec.MEMBER = iter->value.GetDouble(); \
} \ } \
/*-------------------------------------------------------------------------------------*/
/* File-Level Constants */
/*-------------------------------------------------------------------------------------*/
static const std::string_view SCRIPT_TYPE_YAMLTAG = "Type";
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
/* Function Definitions */ /* Function Definitions */
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
@ -61,12 +66,14 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Serialisation Functions */ /* Serialisation Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void ReflectionUtilities::Serialise(System::Object^ object, YAML::Node& yamlNode) void ReflectionUtilities::Serialise(System::Object^ object, YAML::Node& scriptListNode)
{ {
using namespace System::Reflection; using namespace System::Reflection;
// Create YAML object // Create YAML object
YAML::Node scriptNode; YAML::Node scriptNode;
scriptNode.SetStyle(YAML::EmitterStyle::Block);
scriptNode[SCRIPT_TYPE_YAMLTAG] = Convert::ToNative(object->GetType()->FullName);
// Get all fields // Get all fields
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = GetInstanceFields(object); System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = GetInstanceFields(object);
@ -80,7 +87,7 @@ namespace SHADE
writeFieldIntoYaml(field, object, scriptNode); writeFieldIntoYaml(field, object, scriptNode);
} }
yamlNode[Convert::ToNative(object->GetType()->FullName)] = scriptNode; scriptListNode.push_back(scriptNode);
} }
void ReflectionUtilities::Deserialise(Object^ object, YAML::Node& yamlNode) void ReflectionUtilities::Deserialise(Object^ object, YAML::Node& yamlNode)
{ {
@ -120,8 +127,6 @@ namespace SHADE
{ {
// Field YAML Node // Field YAML Node
YAML::Node fieldNode; YAML::Node fieldNode;
fieldNode.SetStyle(YAML::EmitterStyle::Block);
fieldNode["Name"] = Convert::ToNative(fieldInfo->Name);
// Retrieve string for the YAML // Retrieve string for the YAML
if (fieldInsertYaml<System::Int16> (fieldInfo, object, fieldNode) || if (fieldInsertYaml<System::Int16> (fieldInfo, object, fieldNode) ||
@ -172,7 +177,7 @@ namespace SHADE
} }
// Store the field into YAML // Store the field into YAML
yamlNode.push_back(fieldNode); yamlNode[Convert::ToNative(fieldInfo->Name)] = fieldNode;
} }
void ReflectionUtilities::writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node) void ReflectionUtilities::writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node)

View File

@ -64,6 +64,8 @@ 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:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Serialization Helper Functions */ /* Serialization Helper Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/