Added C# Assets System and Serialization of Script Enabled State #247

Merged
Pycorax merged 11 commits from SP3-6-c-scripting into main 2022-11-22 18:59:32 +08:00
4 changed files with 46 additions and 3 deletions
Showing only changes of commit 6afcaa9263 - Show all commits

View File

@ -258,6 +258,14 @@ namespace SHADE
Script::Script() Script::Script()
: OnGizmosDrawOverriden { false } : OnGizmosDrawOverriden { false }
{} {}
/*---------------------------------------------------------------------------------*/
/* Manipulation Functions */
/*---------------------------------------------------------------------------------*/
void Script::SetEnabledWithoutEvents(bool enable)
{
enabled = enable;
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Virtual "All-Time" Lifecycle Functions */ /* Virtual "All-Time" Lifecycle Functions */

View File

@ -326,6 +326,15 @@ namespace SHADE
/// <param name="collision">Information on the collision event.</param> /// <param name="collision">Information on the collision event.</param>
void OnTriggerExit(CollisionInfo collision); void OnTriggerExit(CollisionInfo collision);
/*-----------------------------------------------------------------------------*/
/* Manipulation Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Function to set the enabled state of this script without triggering events.
/// </summary>
/// <param name="enable">Whether to enable or disable the script.</param>
void SetEnabledWithoutEvents(bool enable);
protected: protected:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Constructors */ /* Constructors */

View File

@ -744,7 +744,7 @@ namespace SHADE
for (YAML::Node& node : *yamlNode) for (YAML::Node& node : *yamlNode)
{ {
// Get the name of the script // Get the name of the script
if (!node["Type"]) if (!node["Type"].IsDefined())
{ {
Debug::LogWarning("[ScriptStore] Script with no type detected, skipping."); Debug::LogWarning("[ScriptStore] Script with no type detected, skipping.");
continue; continue;

View File

@ -21,11 +21,13 @@ of DigiPen Institute of Technology is prohibited.
#include "Assets/FontAsset.hxx" #include "Assets/FontAsset.hxx"
#include "Assets/MaterialAsset.hxx" #include "Assets/MaterialAsset.hxx"
#include "Assets/MeshAsset.hxx" #include "Assets/MeshAsset.hxx"
#include "Scripts/Script.hxx"
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
/* File-Level Constants */ /* File-Level Constants */
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
static const std::string_view SCRIPT_TYPE_YAMLTAG = "Type"; static const std::string_view SCRIPT_TYPE_YAMLTAG = "Type";
static const std::string_view SCRIPT_ENABLED_YAMLTAG = "Enabled";
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
/* Function Definitions */ /* Function Definitions */
@ -39,10 +41,19 @@ namespace SHADE
{ {
using namespace System::Reflection; using namespace System::Reflection;
// Obtain script
Script^ script = safe_cast<Script^>(object);
if (script == nullptr)
{
Debug::LogWarning("[SerialisationUtilities] Attempted to serialise an object that is not a script!");
return;
}
// Create YAML object // Create YAML object
YAML::Node scriptNode; YAML::Node scriptNode;
scriptNode.SetStyle(YAML::EmitterStyle::Block); scriptNode.SetStyle(YAML::EmitterStyle::Block);
scriptNode[SCRIPT_TYPE_YAMLTAG.data()] = Convert::ToNative(object->GetType()->FullName); scriptNode[SCRIPT_TYPE_YAMLTAG.data()] = Convert::ToNative(object->GetType()->FullName);
scriptNode[SCRIPT_ENABLED_YAMLTAG.data()] = script->Enabled;
// Get all fields // Get all fields
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = ReflectionUtilities::GetInstanceFields(object); System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = ReflectionUtilities::GetInstanceFields(object);
@ -72,7 +83,7 @@ namespace SHADE
{ {
using namespace System::Reflection; using namespace System::Reflection;
// Load the YAML // Error Checking
if (!yamlNode.IsMap()) if (!yamlNode.IsMap())
{ {
// Invalid // Invalid
@ -83,6 +94,21 @@ namespace SHADE
); );
return; return;
} }
// Get the script
Script^ script = safe_cast<Script^>(object);
if (script == nullptr)
{
Debug::LogWarning("[SerialisationUtilities] Attempted to deserialise an object that is not a script!");
return;
}
// Set enabled state
if (yamlNode[SCRIPT_ENABLED_YAMLTAG.data()].IsDefined())
{
script->SetEnabledWithoutEvents(yamlNode[SCRIPT_ENABLED_YAMLTAG.data()].as<bool>());
}
// Get all fields // Get all fields
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = ReflectionUtilities::GetInstanceFields(object); System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = ReflectionUtilities::GetInstanceFields(object);
for each (FieldInfo^ field in fields) for each (FieldInfo^ field in fields)
@ -95,7 +121,7 @@ namespace SHADE
// Deserialise // Deserialise
const std::string FIELD_NAME = Convert::ToNative(field->Name); const std::string FIELD_NAME = Convert::ToNative(field->Name);
if (yamlNode[FIELD_NAME]) if (yamlNode[FIELD_NAME].IsDefined())
{ {
writeYamlIntoField(field, object, yamlNode[FIELD_NAME]); writeYamlIntoField(field, object, yamlNode[FIELD_NAME]);
} }