diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index d40605e4..6653284d 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -26,12 +26,25 @@ namespace SHADE /* Static Definitions */ /*--------------------------------------------------------------------------------*/ const std::string SHScriptEngine::DEFAULT_CSHARP_NAMESPACE = std::string(DEFAULT_CSHARP_LIB_NAME); - - /*---------------------------------------------------------------------------------*/ - /* Constructors/Destructors */ - /*---------------------------------------------------------------------------------*/ - SHScriptEngine::SHScriptEngine() - {} + SHDotNetRuntime SHScriptEngine::dotNet { false }; + SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineInit = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineLoadScripts = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineUnloadScripts = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineReloadScripts = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineExit = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsFrameSetUp = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsExecuteFixedUpdate = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsExecuteUpdate = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsExecuteLateUpdate = nullptr; + SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsFrameCleanUp = nullptr; + SHScriptEngine::CsScriptManipFuncPtr SHScriptEngine::csScriptsAdd = nullptr; + SHScriptEngine::CsScriptBasicFuncPtr SHScriptEngine::csScriptsRemoveAll = nullptr; + SHScriptEngine::CsScriptOptionalFuncPtr SHScriptEngine::csScriptsRemoveAllImmediately = nullptr; + SHScriptEngine::CsScriptSerialiseFuncPtr SHScriptEngine::csScriptsSerialise = nullptr; + SHScriptEngine::CsScriptDeserialiseFuncPtr SHScriptEngine::csScriptDeserialise = nullptr; + SHScriptEngine::CsScriptSerialiseYamlFuncPtr SHScriptEngine::csScriptsSerialiseYaml = nullptr; + SHScriptEngine::CsScriptSerialiseYamlFuncPtr SHScriptEngine::csScriptDeserialiseYaml = nullptr; + SHScriptEngine::CsScriptEditorFuncPtr SHScriptEngine::csEditorRenderScripts = nullptr; /*---------------------------------------------------------------------------------*/ /* Lifecycle Functions */ @@ -85,11 +98,6 @@ namespace SHADE csScriptsExecuteFixedUpdate(); } - void SHScriptEngine::ExecuteOnTrigger() - { - csScriptsExecuteOnTrigger(); - } - void SHScriptEngine::Exit() { // Do not allow deinitialization if not initialised @@ -113,15 +121,15 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Script Manipulation Functions */ /*---------------------------------------------------------------------------------*/ - bool SHScriptEngine::AddScript(const SHEntity& entity, const std::string_view& scriptName) const + bool SHScriptEngine::AddScript(const SHEntity& entity, const std::string_view& scriptName) { return csScriptsAdd(entity.GetEID(), scriptName.data()); } - void SHScriptEngine::RemoveAllScripts(const SHEntity& entity) const + void SHScriptEngine::RemoveAllScripts(const SHEntity& entity) { csScriptsRemoveAll(entity.GetEID()); } - void SHScriptEngine::RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy) const + void SHScriptEngine::RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy) { csScriptsRemoveAllImmediately(entity.GetEID(), callOnDestroy); } @@ -129,7 +137,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Script Serialisation Functions */ /*---------------------------------------------------------------------------------*/ - std::string SHScriptEngine::SerialiseScripts(const SHEntity& entity) const + std::string SHScriptEngine::SerialiseScripts(const SHEntity& entity) { // Create buffer needed to store serialised script data constexpr int BUFFER_SIZE = 10240; @@ -154,7 +162,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Script Serialisation Functions */ /*---------------------------------------------------------------------------------*/ - void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml) const + void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml) { csScriptDeserialise(entity.GetEID(), yaml.c_str()); } @@ -162,7 +170,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Script Editor Functions */ /*---------------------------------------------------------------------------------*/ - void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity) const + void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity) { csEditorRenderScripts(entity.GetEID()); } @@ -312,12 +320,6 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", "FrameSetUp" ); - csScriptsExecuteOnTrigger = dotNet.GetFunctionPtr - ( - DEFAULT_CSHARP_LIB_NAME, - DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", - "ExecuteOnTrigger" - ); csScriptsExecuteFixedUpdate = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, @@ -360,17 +362,17 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", "RemoveAllScriptsImmediately" ); - csScriptsSerialise = dotNet.GetFunctionPtr + /*csScriptsSerialise = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", "SerialiseScripts" ); - csScriptsSerialiseJson = dotNet.GetFunctionPtr + csScriptsSerialiseYaml = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", - "SerialiseScriptsJson" + "SerialiseScriptsYaml" ); csScriptDeserialise = dotNet.GetFunctionPtr ( @@ -378,12 +380,18 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", "DeserialiseScript" ); + csScriptDeserialiseYaml = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", + "SerialiseScriptsYaml" + ); csEditorRenderScripts = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_NAMESPACE + ".Editor", "RenderScriptsInInspector" - ); + );*/ } void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 91533c0f..a375c864 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -29,13 +29,10 @@ namespace SHADE { public: /*-----------------------------------------------------------------------------*/ - /* Constructors & Destructors */ + /* Constructor */ /*-----------------------------------------------------------------------------*/ - /// - /// Default Constructor - /// - SHScriptEngine(); - + SHScriptEngine() = delete; + /*-----------------------------------------------------------------------------*/ /* Lifecycle Functions */ /*-----------------------------------------------------------------------------*/ @@ -43,38 +40,33 @@ namespace SHADE /// Initialises the DotNetRuntime and retrieves function pointers to all /// functions on the CLR used to interface with the engine. /// - void Init(); + static void Init(); /// /// Loads the managed script assembly. Ensure this is only called after /// UnloadScriptAssembly() has been called. /// - void UnloadScriptAssembly(); + static void UnloadScriptAssembly(); /// /// Unloads the managed script assembly. /// Take note that this will clear all existing scripts, ensure that the scene /// is saved before doing so. /// - void LoadScriptAssembly(); + static void LoadScriptAssembly(); /// /// Reloads the managed script assembly. /// Take note that this will clear all existing scripts, ensure that the scene /// is saved before doing so. /// - void ReloadScriptAssembly(); + static void ReloadScriptAssembly(); /// /// Executes the FixedUpdate()s of the PlushieScripts that are attached to /// Entities. /// - void ExecuteFixedUpdates(); - /// - /// Executes the OnTrigger() family of functions of the PlushieScripts that are - /// attached to Entities. - /// - void ExecuteOnTrigger(); + static void ExecuteFixedUpdates(); /// /// Shuts down the DotNetRuntime. /// - void Exit(); + static void Exit(); /*-----------------------------------------------------------------------------*/ /* Script Manipulation Functions */ @@ -91,14 +83,14 @@ namespace SHADE /// True if successfully added. False otherwise with the error logged to the /// console. /// - bool AddScript(const SHEntity& entity, const std::string_view& scriptName) const; + static bool AddScript(const SHEntity& entity, const std::string_view& scriptName); /// /// Removes all Scripts attached to the specified Entity. Does not do anything /// if the specified Entity is invalid or does not have any PlushieScripts /// attached. /// /// The entity to remove the scripts from. - void RemoveAllScripts(const SHEntity& entity) const; + static void RemoveAllScripts(const SHEntity& entity); /// /// Removes all Scripts attached to the specified Entity. Unlike /// RemoveAllScripts(), this removes all the scripts immediately. @@ -110,7 +102,7 @@ namespace SHADE /// Whether or not to call OnDestroy on the scripts. This is ignored if not in /// play mode. /// - void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy) const; + static void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy); /*-----------------------------------------------------------------------------*/ /* Script Serialisation Functions */ @@ -123,16 +115,16 @@ namespace SHADE /// /// String that represents the set of scripts attached to the specified Entity. /// - std::string SerialiseScripts(const SHEntity& entity) const; + static std::string SerialiseScripts(const SHEntity& entity); /// /// Loads the specified JSON string and creates a Script for the specified Entity /// based on the specified JSON string. /// /// The Entity to deserialise a Script on to. /// - /// The JSON string that represents the Script to load into the Entity. + /// The YAML string that represents the Script to load into the Entity. /// - void DeserialiseScript(const SHEntity& entity, const std::string& yaml) const; + static void DeserialiseScript(const SHEntity& entity, const std::string& yaml); /*-----------------------------------------------------------------------------*/ /* Script Editor Functions */ @@ -145,7 +137,7 @@ namespace SHADE /// rendering code. /// /// The Entity to render the PlushieScripts of. - void RenderScriptsInInspector(const SHEntity& entity) const; + static void RenderScriptsInInspector(const SHEntity& entity); /*-----------------------------------------------------------------------------*/ /* Static Utility Functions */ @@ -177,7 +169,7 @@ namespace SHADE using CsScriptOptionalFuncPtr = void(*)(EntityID, bool); using CsScriptSerialiseFuncPtr = bool(*)(EntityID, char*, int); using CsScriptDeserialiseFuncPtr = bool(*)(EntityID, const char*); - using CsScriptSerialiseJsonFuncPtr = bool(*)(EntityID, void*); + using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*); using CsScriptEditorFuncPtr = void(*)(EntityID); /*-----------------------------------------------------------------------------*/ @@ -190,30 +182,29 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Data Members */ /*-----------------------------------------------------------------------------*/ - SHDotNetRuntime dotNet {false}; + static SHDotNetRuntime dotNet; // Function Pointers to CLR Code - // - Engine Init - CsFuncPtr csEngineInit = nullptr; - CsFuncPtr csEngineLoadScripts = nullptr; - CsFuncPtr csEngineUnloadScripts = nullptr; - CsFuncPtr csEngineReloadScripts = nullptr; - CsFuncPtr csEngineExit = nullptr; + // - Engine Lifecycle + static CsFuncPtr csEngineInit; + static CsFuncPtr csEngineLoadScripts; + static CsFuncPtr csEngineUnloadScripts; + static CsFuncPtr csEngineReloadScripts; + static CsFuncPtr csEngineExit; // - Scripts Store - CsFuncPtr csScriptsFrameSetUp = nullptr; - CsFuncPtr csScriptsExecuteOnTrigger = nullptr; - CsFuncPtr csScriptsExecuteFixedUpdate = nullptr; - CsFuncPtr csScriptsExecuteUpdate = nullptr; - CsFuncPtr csScriptsExecuteLateUpdate = nullptr; - CsFuncPtr csScriptsFrameCleanUp = nullptr; - CsScriptManipFuncPtr csScriptsAdd = nullptr; - CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr; - CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr; - CsScriptSerialiseFuncPtr csScriptsSerialise = nullptr; - CsScriptDeserialiseFuncPtr csScriptDeserialise = nullptr; - CsScriptSerialiseJsonFuncPtr csScriptsSerialiseJson = nullptr; - CsScriptSerialiseJsonFuncPtr csScriptDeserialiseJson = nullptr; + static CsFuncPtr csScriptsFrameSetUp; + static CsFuncPtr csScriptsExecuteFixedUpdate; + static CsFuncPtr csScriptsExecuteUpdate; + static CsFuncPtr csScriptsExecuteLateUpdate; + static CsFuncPtr csScriptsFrameCleanUp; + static CsScriptManipFuncPtr csScriptsAdd; + static CsScriptBasicFuncPtr csScriptsRemoveAll; + static CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately; + static CsScriptSerialiseFuncPtr csScriptsSerialise; + static CsScriptDeserialiseFuncPtr csScriptDeserialise; + static CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml; + static CsScriptSerialiseYamlFuncPtr csScriptDeserialiseYaml; // - Editor - CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; + static CsScriptEditorFuncPtr csEditorRenderScripts; // Delegates /*ECS::EntityEvent::Delegate onEntityCreate; ECS::EntityEvent::Delegate onEntityDestroy;*/ @@ -224,7 +215,7 @@ namespace SHADE /// /// Loads all the function pointers to CLR code that we need to execute. /// - void loadFunctions(); + static void loadFunctions(); /// /// Reads the file via the specified path that represents a build log of error /// and warning messages. diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 79a75bda..9a9eff54 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -526,7 +526,7 @@ namespace SHADE return false; } - bool ScriptStore::DeserialiseScript(Entity entity, System::String^ json) + bool ScriptStore::DeserialiseScript(Entity entity, System::String^ yaml) { SAFE_NATIVE_CALL_BEGIN // Check if entity exists, otherwise nothing @@ -534,13 +534,13 @@ namespace SHADE return false; // Get the name of the script - const int FIRST_QUOTE = json->IndexOf('\"'); - const int FIRST_COLON = json->IndexOf(':'); + const int FIRST_QUOTE = yaml->IndexOf('\"'); + const int FIRST_COLON = yaml->IndexOf(':'); if (FIRST_QUOTE < 0 || FIRST_COLON < 0) // No script name, it's invalid return false; const int SCRIPT_NAME_START = FIRST_QUOTE + 1; const int SCRIPT_NAME_END = FIRST_COLON - 1; - System::String^ typeName = json->Substring(SCRIPT_NAME_START, SCRIPT_NAME_END - SCRIPT_NAME_START); + System::String^ typeName = yaml->Substring(SCRIPT_NAME_START, SCRIPT_NAME_END - SCRIPT_NAME_START); // Create the script Script^ script; diff --git a/SHADE_Managed/src/Scripts/ScriptStore.hxx b/SHADE_Managed/src/Scripts/ScriptStore.hxx index 798c4cb6..cc0c1db5 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.hxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.hxx @@ -232,10 +232,6 @@ namespace SHADE /// Executes LateUpdate() for all scripts. /// static void ExecuteLateUpdate(); - /// - /// Executes OnTrigger functions for all scripts. - /// - static void ExecuteOnTrigger(); /*-----------------------------------------------------------------------------*/ /* Serialisation Functions */ @@ -268,11 +264,11 @@ namespace SHADE /// /// The Entity to attach the deserialised Scripts to. /// - /// + /// /// JSON string that describes the Script to serialise. /// /// - static bool DeserialiseScript(Entity entity, System::String^ json); + static bool DeserialiseScript(Entity entity, System::String^ yaml); private: /*-----------------------------------------------------------------------------*/