Converted SHScriptEngine to static and replaced json references with yaml

This commit is contained in:
Kah Wei 2022-09-13 20:30:32 +08:00
parent cfa12eb865
commit 0c28199acf
4 changed files with 79 additions and 84 deletions

View File

@ -26,12 +26,25 @@ namespace SHADE
/* Static Definitions */ /* Static Definitions */
/*--------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------*/
const std::string SHScriptEngine::DEFAULT_CSHARP_NAMESPACE = std::string(DEFAULT_CSHARP_LIB_NAME); const std::string SHScriptEngine::DEFAULT_CSHARP_NAMESPACE = std::string(DEFAULT_CSHARP_LIB_NAME);
SHDotNetRuntime SHScriptEngine::dotNet { false };
/*---------------------------------------------------------------------------------*/ SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineInit = nullptr;
/* Constructors/Destructors */ SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineLoadScripts = nullptr;
/*---------------------------------------------------------------------------------*/ SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineUnloadScripts = nullptr;
SHScriptEngine::SHScriptEngine() 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 */ /* Lifecycle Functions */
@ -85,11 +98,6 @@ namespace SHADE
csScriptsExecuteFixedUpdate(); csScriptsExecuteFixedUpdate();
} }
void SHScriptEngine::ExecuteOnTrigger()
{
csScriptsExecuteOnTrigger();
}
void SHScriptEngine::Exit() void SHScriptEngine::Exit()
{ {
// Do not allow deinitialization if not initialised // Do not allow deinitialization if not initialised
@ -113,15 +121,15 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Script Manipulation Functions */ /* 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()); return csScriptsAdd(entity.GetEID(), scriptName.data());
} }
void SHScriptEngine::RemoveAllScripts(const SHEntity& entity) const void SHScriptEngine::RemoveAllScripts(const SHEntity& entity)
{ {
csScriptsRemoveAll(entity.GetEID()); csScriptsRemoveAll(entity.GetEID());
} }
void SHScriptEngine::RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy) const void SHScriptEngine::RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy)
{ {
csScriptsRemoveAllImmediately(entity.GetEID(), callOnDestroy); csScriptsRemoveAllImmediately(entity.GetEID(), callOnDestroy);
} }
@ -129,7 +137,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */ /* 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 // Create buffer needed to store serialised script data
constexpr int BUFFER_SIZE = 10240; constexpr int BUFFER_SIZE = 10240;
@ -154,7 +162,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */ /* 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()); csScriptDeserialise(entity.GetEID(), yaml.c_str());
} }
@ -162,7 +170,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Script Editor Functions */ /* Script Editor Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity) const void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity)
{ {
csEditorRenderScripts(entity.GetEID()); csEditorRenderScripts(entity.GetEID());
} }
@ -312,12 +320,6 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"FrameSetUp" "FrameSetUp"
); );
csScriptsExecuteOnTrigger = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteOnTrigger"
);
csScriptsExecuteFixedUpdate = dotNet.GetFunctionPtr<CsFuncPtr> csScriptsExecuteFixedUpdate = dotNet.GetFunctionPtr<CsFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
@ -360,17 +362,17 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"RemoveAllScriptsImmediately" "RemoveAllScriptsImmediately"
); );
csScriptsSerialise = dotNet.GetFunctionPtr<CsScriptSerialiseFuncPtr> /*csScriptsSerialise = dotNet.GetFunctionPtr<CsScriptSerialiseFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScripts" "SerialiseScripts"
); );
csScriptsSerialiseJson = dotNet.GetFunctionPtr<CsScriptSerialiseJsonFuncPtr> csScriptsSerialiseYaml = dotNet.GetFunctionPtr<CsScriptSerialiseYamlFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScriptsJson" "SerialiseScriptsYaml"
); );
csScriptDeserialise = dotNet.GetFunctionPtr<CsScriptDeserialiseFuncPtr> csScriptDeserialise = dotNet.GetFunctionPtr<CsScriptDeserialiseFuncPtr>
( (
@ -378,12 +380,18 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"DeserialiseScript" "DeserialiseScript"
); );
csScriptDeserialiseYaml = dotNet.GetFunctionPtr<CsScriptSerialiseYamlFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScriptsYaml"
);
csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr> csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".Editor", DEFAULT_CSHARP_NAMESPACE + ".Editor",
"RenderScriptsInInspector" "RenderScriptsInInspector"
); );*/
} }
void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath)

View File

@ -29,12 +29,9 @@ namespace SHADE
{ {
public: public:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Constructors & Destructors */ /* Constructor */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/// <summary> SHScriptEngine() = delete;
/// Default Constructor
/// </summary>
SHScriptEngine();
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Lifecycle Functions */ /* Lifecycle Functions */
@ -43,38 +40,33 @@ namespace SHADE
/// Initialises the DotNetRuntime and retrieves function pointers to all /// Initialises the DotNetRuntime and retrieves function pointers to all
/// functions on the CLR used to interface with the engine. /// functions on the CLR used to interface with the engine.
/// </summary> /// </summary>
void Init(); static void Init();
/// <summary> /// <summary>
/// Loads the managed script assembly. Ensure this is only called after /// Loads the managed script assembly. Ensure this is only called after
/// UnloadScriptAssembly() has been called. /// UnloadScriptAssembly() has been called.
/// </summary> /// </summary>
void UnloadScriptAssembly(); static void UnloadScriptAssembly();
/// <summary> /// <summary>
/// Unloads the managed script assembly. /// Unloads the managed script assembly.
/// Take note that this will clear all existing scripts, ensure that the scene /// Take note that this will clear all existing scripts, ensure that the scene
/// is saved before doing so. /// is saved before doing so.
/// </summary> /// </summary>
void LoadScriptAssembly(); static void LoadScriptAssembly();
/// <summary> /// <summary>
/// Reloads the managed script assembly. /// Reloads the managed script assembly.
/// Take note that this will clear all existing scripts, ensure that the scene /// Take note that this will clear all existing scripts, ensure that the scene
/// is saved before doing so. /// is saved before doing so.
/// </summary> /// </summary>
void ReloadScriptAssembly(); static void ReloadScriptAssembly();
/// <summary> /// <summary>
/// Executes the FixedUpdate()s of the PlushieScripts that are attached to /// Executes the FixedUpdate()s of the PlushieScripts that are attached to
/// Entities. /// Entities.
/// </summary> /// </summary>
void ExecuteFixedUpdates(); static void ExecuteFixedUpdates();
/// <summary>
/// Executes the OnTrigger() family of functions of the PlushieScripts that are
/// attached to Entities.
/// </summary>
void ExecuteOnTrigger();
/// <summary> /// <summary>
/// Shuts down the DotNetRuntime. /// Shuts down the DotNetRuntime.
/// </summary> /// </summary>
void Exit(); static void Exit();
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Script Manipulation Functions */ /* Script Manipulation Functions */
@ -91,14 +83,14 @@ namespace SHADE
/// True if successfully added. False otherwise with the error logged to the /// True if successfully added. False otherwise with the error logged to the
/// console. /// console.
/// </returns> /// </returns>
bool AddScript(const SHEntity& entity, const std::string_view& scriptName) const; static bool AddScript(const SHEntity& entity, const std::string_view& scriptName);
/// <summary> /// <summary>
/// Removes all Scripts attached to the specified Entity. Does not do anything /// Removes all Scripts attached to the specified Entity. Does not do anything
/// if the specified Entity is invalid or does not have any PlushieScripts /// if the specified Entity is invalid or does not have any PlushieScripts
/// attached. /// attached.
/// </summary> /// </summary>
/// <param name="entity">The entity to remove the scripts from.</param> /// <param name="entity">The entity to remove the scripts from.</param>
void RemoveAllScripts(const SHEntity& entity) const; static void RemoveAllScripts(const SHEntity& entity);
/// <summary> /// <summary>
/// Removes all Scripts attached to the specified Entity. Unlike /// Removes all Scripts attached to the specified Entity. Unlike
/// RemoveAllScripts(), this removes all the scripts immediately. /// 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 /// Whether or not to call OnDestroy on the scripts. This is ignored if not in
/// play mode. /// play mode.
/// </param> /// </param>
void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy) const; static void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Script Serialisation Functions */ /* Script Serialisation Functions */
@ -123,16 +115,16 @@ namespace SHADE
/// <returns> /// <returns>
/// String that represents the set of scripts attached to the specified Entity. /// String that represents the set of scripts attached to the specified Entity.
/// </returns> /// </returns>
std::string SerialiseScripts(const SHEntity& entity) const; static std::string SerialiseScripts(const SHEntity& entity);
/// <summary> /// <summary>
/// Loads the specified JSON string and creates a Script for the specified Entity /// Loads the specified JSON string and creates a Script for the specified Entity
/// based on the specified JSON string. /// based on the specified JSON string.
/// </summary> /// </summary>
/// <param name="entity">The Entity to deserialise a Script on to.</param> /// <param name="entity">The Entity to deserialise a Script on to.</param>
/// <param name="yaml"> /// <param name="yaml">
/// The JSON string that represents the Script to load into the Entity. /// The YAML string that represents the Script to load into the Entity.
/// </param> /// </param>
void DeserialiseScript(const SHEntity& entity, const std::string& yaml) const; static void DeserialiseScript(const SHEntity& entity, const std::string& yaml);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Script Editor Functions */ /* Script Editor Functions */
@ -145,7 +137,7 @@ namespace SHADE
/// rendering code. /// rendering code.
/// </summary> /// </summary>
/// <param name="entity">The Entity to render the PlushieScripts of.</param> /// <param name="entity">The Entity to render the PlushieScripts of.</param>
void RenderScriptsInInspector(const SHEntity& entity) const; static void RenderScriptsInInspector(const SHEntity& entity);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Static Utility Functions */ /* Static Utility Functions */
@ -177,7 +169,7 @@ namespace SHADE
using CsScriptOptionalFuncPtr = void(*)(EntityID, bool); using CsScriptOptionalFuncPtr = void(*)(EntityID, bool);
using CsScriptSerialiseFuncPtr = bool(*)(EntityID, char*, int); using CsScriptSerialiseFuncPtr = bool(*)(EntityID, char*, int);
using CsScriptDeserialiseFuncPtr = bool(*)(EntityID, const char*); using CsScriptDeserialiseFuncPtr = bool(*)(EntityID, const char*);
using CsScriptSerialiseJsonFuncPtr = bool(*)(EntityID, void*); using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*);
using CsScriptEditorFuncPtr = void(*)(EntityID); using CsScriptEditorFuncPtr = void(*)(EntityID);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -190,30 +182,29 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
SHDotNetRuntime dotNet {false}; static SHDotNetRuntime dotNet;
// Function Pointers to CLR Code // Function Pointers to CLR Code
// - Engine Init // - Engine Lifecycle
CsFuncPtr csEngineInit = nullptr; static CsFuncPtr csEngineInit;
CsFuncPtr csEngineLoadScripts = nullptr; static CsFuncPtr csEngineLoadScripts;
CsFuncPtr csEngineUnloadScripts = nullptr; static CsFuncPtr csEngineUnloadScripts;
CsFuncPtr csEngineReloadScripts = nullptr; static CsFuncPtr csEngineReloadScripts;
CsFuncPtr csEngineExit = nullptr; static CsFuncPtr csEngineExit;
// - Scripts Store // - Scripts Store
CsFuncPtr csScriptsFrameSetUp = nullptr; static CsFuncPtr csScriptsFrameSetUp;
CsFuncPtr csScriptsExecuteOnTrigger = nullptr; static CsFuncPtr csScriptsExecuteFixedUpdate;
CsFuncPtr csScriptsExecuteFixedUpdate = nullptr; static CsFuncPtr csScriptsExecuteUpdate;
CsFuncPtr csScriptsExecuteUpdate = nullptr; static CsFuncPtr csScriptsExecuteLateUpdate;
CsFuncPtr csScriptsExecuteLateUpdate = nullptr; static CsFuncPtr csScriptsFrameCleanUp;
CsFuncPtr csScriptsFrameCleanUp = nullptr; static CsScriptManipFuncPtr csScriptsAdd;
CsScriptManipFuncPtr csScriptsAdd = nullptr; static CsScriptBasicFuncPtr csScriptsRemoveAll;
CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr; static CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately;
CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr; static CsScriptSerialiseFuncPtr csScriptsSerialise;
CsScriptSerialiseFuncPtr csScriptsSerialise = nullptr; static CsScriptDeserialiseFuncPtr csScriptDeserialise;
CsScriptDeserialiseFuncPtr csScriptDeserialise = nullptr; static CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml;
CsScriptSerialiseJsonFuncPtr csScriptsSerialiseJson = nullptr; static CsScriptSerialiseYamlFuncPtr csScriptDeserialiseYaml;
CsScriptSerialiseJsonFuncPtr csScriptDeserialiseJson = nullptr;
// - Editor // - Editor
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; static CsScriptEditorFuncPtr csEditorRenderScripts;
// Delegates // Delegates
/*ECS::EntityEvent::Delegate onEntityCreate; /*ECS::EntityEvent::Delegate onEntityCreate;
ECS::EntityEvent::Delegate onEntityDestroy;*/ ECS::EntityEvent::Delegate onEntityDestroy;*/
@ -224,7 +215,7 @@ namespace SHADE
/// <summary> /// <summary>
/// Loads all the function pointers to CLR code that we need to execute. /// Loads all the function pointers to CLR code that we need to execute.
/// </summary> /// </summary>
void loadFunctions(); static void loadFunctions();
/// <summary> /// <summary>
/// Reads the file via the specified path that represents a build log of error /// Reads the file via the specified path that represents a build log of error
/// and warning messages. /// and warning messages.

View File

@ -526,7 +526,7 @@ namespace SHADE
return false; return false;
} }
bool ScriptStore::DeserialiseScript(Entity entity, System::String^ json) bool ScriptStore::DeserialiseScript(Entity entity, System::String^ yaml)
{ {
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN
// Check if entity exists, otherwise nothing // Check if entity exists, otherwise nothing
@ -534,13 +534,13 @@ namespace SHADE
return false; return false;
// Get the name of the script // Get the name of the script
const int FIRST_QUOTE = json->IndexOf('\"'); const int FIRST_QUOTE = yaml->IndexOf('\"');
const int FIRST_COLON = json->IndexOf(':'); const int FIRST_COLON = yaml->IndexOf(':');
if (FIRST_QUOTE < 0 || FIRST_COLON < 0) // No script name, it's invalid if (FIRST_QUOTE < 0 || FIRST_COLON < 0) // No script name, it's invalid
return false; return false;
const int SCRIPT_NAME_START = FIRST_QUOTE + 1; const int SCRIPT_NAME_START = FIRST_QUOTE + 1;
const int SCRIPT_NAME_END = FIRST_COLON - 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 // Create the script
Script^ script; Script^ script;

View File

@ -232,10 +232,6 @@ namespace SHADE
/// Executes LateUpdate() for all scripts. /// Executes LateUpdate() for all scripts.
/// </summary> /// </summary>
static void ExecuteLateUpdate(); static void ExecuteLateUpdate();
/// <summary>
/// Executes OnTrigger functions for all scripts.
/// </summary>
static void ExecuteOnTrigger();
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Serialisation Functions */ /* Serialisation Functions */
@ -268,11 +264,11 @@ namespace SHADE
/// <param name="entity"> /// <param name="entity">
/// The Entity to attach the deserialised Scripts to. /// The Entity to attach the deserialised Scripts to.
/// </param> /// </param>
/// <param name="json"> /// <param name="yaml">
/// JSON string that describes the Script to serialise. /// JSON string that describes the Script to serialise.
/// </param> /// </param>
/// <returns></returns> /// <returns></returns>
static bool DeserialiseScript(Entity entity, System::String^ json); static bool DeserialiseScript(Entity entity, System::String^ yaml);
private: private:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/