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 */
/*--------------------------------------------------------------------------------*/
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<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteOnTrigger"
);
csScriptsExecuteFixedUpdate = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
@ -360,17 +362,17 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"RemoveAllScriptsImmediately"
);
csScriptsSerialise = dotNet.GetFunctionPtr<CsScriptSerialiseFuncPtr>
/*csScriptsSerialise = dotNet.GetFunctionPtr<CsScriptSerialiseFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScripts"
);
csScriptsSerialiseJson = dotNet.GetFunctionPtr<CsScriptSerialiseJsonFuncPtr>
csScriptsSerialiseYaml = dotNet.GetFunctionPtr<CsScriptSerialiseYamlFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScriptsJson"
"SerialiseScriptsYaml"
);
csScriptDeserialise = dotNet.GetFunctionPtr<CsScriptDeserialiseFuncPtr>
(
@ -378,12 +380,18 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"DeserialiseScript"
);
csScriptDeserialiseYaml = dotNet.GetFunctionPtr<CsScriptSerialiseYamlFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"SerialiseScriptsYaml"
);
csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".Editor",
"RenderScriptsInInspector"
);
);*/
}
void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath)

View File

@ -29,12 +29,9 @@ namespace SHADE
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors & Destructors */
/* Constructor */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Default Constructor
/// </summary>
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.
/// </summary>
void Init();
static void Init();
/// <summary>
/// Loads the managed script assembly. Ensure this is only called after
/// UnloadScriptAssembly() has been called.
/// </summary>
void UnloadScriptAssembly();
static void UnloadScriptAssembly();
/// <summary>
/// Unloads the managed script assembly.
/// Take note that this will clear all existing scripts, ensure that the scene
/// is saved before doing so.
/// </summary>
void LoadScriptAssembly();
static void LoadScriptAssembly();
/// <summary>
/// Reloads the managed script assembly.
/// Take note that this will clear all existing scripts, ensure that the scene
/// is saved before doing so.
/// </summary>
void ReloadScriptAssembly();
static void ReloadScriptAssembly();
/// <summary>
/// Executes the FixedUpdate()s of the PlushieScripts that are attached to
/// Entities.
/// </summary>
void ExecuteFixedUpdates();
/// <summary>
/// Executes the OnTrigger() family of functions of the PlushieScripts that are
/// attached to Entities.
/// </summary>
void ExecuteOnTrigger();
static void ExecuteFixedUpdates();
/// <summary>
/// Shuts down the DotNetRuntime.
/// </summary>
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.
/// </returns>
bool AddScript(const SHEntity& entity, const std::string_view& scriptName) const;
static bool AddScript(const SHEntity& entity, const std::string_view& scriptName);
/// <summary>
/// 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.
/// </summary>
/// <param name="entity">The entity to remove the scripts from.</param>
void RemoveAllScripts(const SHEntity& entity) const;
static void RemoveAllScripts(const SHEntity& entity);
/// <summary>
/// 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.
/// </param>
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
/// <returns>
/// String that represents the set of scripts attached to the specified Entity.
/// </returns>
std::string SerialiseScripts(const SHEntity& entity) const;
static std::string SerialiseScripts(const SHEntity& entity);
/// <summary>
/// Loads the specified JSON string and creates a Script for the specified Entity
/// based on the specified JSON string.
/// </summary>
/// <param name="entity">The Entity to deserialise a Script on to.</param>
/// <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>
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.
/// </summary>
/// <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 */
@ -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
/// <summary>
/// Loads all the function pointers to CLR code that we need to execute.
/// </summary>
void loadFunctions();
static void loadFunctions();
/// <summary>
/// Reads the file via the specified path that represents a build log of error
/// and warning messages.

View File

@ -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;

View File

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