Converted SHScriptEngine into a SHSystem

This commit is contained in:
Kah Wei 2022-09-16 16:37:50 +08:00
parent 941f4a135f
commit 0f63ee10d0
3 changed files with 43 additions and 65 deletions

View File

@ -37,7 +37,7 @@ namespace Sandbox
SHADE::SHSystemManager::CreateSystem<SHADE::SHGraphicsSystem>();
SHADE::SHGraphicsSystem* graphicsSystem = static_cast<SHADE::SHGraphicsSystem*>(SHADE::SHSystemManager::GetSystem<SHADE::SHGraphicsSystem>());
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHGraphicsSystem, SHADE::SHGraphicsSystem::SHGraphicsSystemRoutine>(1);
SHADE::SHSystemManager::CreateSystem<SHADE::SHScriptEngine>();
graphicsSystem->SetWindow(&window);
SDL_CreateWindowFrom(window.GetHWND());
@ -47,9 +47,6 @@ namespace Sandbox
//SHADE::SHEditor::Initialize(window.GetHWND());
#else
#endif
// Set up scripting
SHADE::SHScriptEngine::Init();
}
void SBApplication::Update(void)
@ -76,7 +73,6 @@ namespace Sandbox
void SBApplication::Exit(void)
{
SHADE::SHScriptEngine::Exit();
SHADE::SHSystemManager::Exit();
SDL_DestroyWindow(sdlWindow);
#ifdef SHEDITOR

View File

@ -26,25 +26,6 @@ namespace SHADE
/* Static Definitions */
/*--------------------------------------------------------------------------------*/
const std::string SHScriptEngine::DEFAULT_CSHARP_NAMESPACE = std::string("SHADE");
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 */
@ -137,7 +118,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*---------------------------------------------------------------------------------*/
std::string SHScriptEngine::SerialiseScripts(const SHEntity& entity)
std::string SHScriptEngine::SerialiseScripts(const SHEntity& entity) const
{
// Create buffer needed to store serialised script data
constexpr int BUFFER_SIZE = 10240;
@ -162,7 +143,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*---------------------------------------------------------------------------------*/
void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml)
void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml) const
{
csScriptDeserialise(entity.GetEID(), yaml.c_str());
}
@ -170,7 +151,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Script Editor Functions */
/*---------------------------------------------------------------------------------*/
void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity)
void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity) const
{
csEditorRenderScripts(entity.GetEID());
}
@ -178,7 +159,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Static Utility Functions */
/*---------------------------------------------------------------------------------*/
bool SHScriptEngine::BuildScriptAssembly(bool debug)
bool SHScriptEngine::BuildScriptAssembly(bool debug) const
{
constexpr std::string_view BUILD_LOG_PATH = "../Build.log";
@ -230,7 +211,7 @@ namespace SHADE
return BUILD_SUCCESS;
}
void SHScriptEngine::GenerateScriptsCsProjFile(const std::filesystem::path& path)
void SHScriptEngine::GenerateScriptsCsProjFile(const std::filesystem::path& path) const
{
// Sample
static std::string_view FILE_CONTENTS =

View File

@ -18,6 +18,7 @@ of DigiPen Institute of Technology is prohibited.
#include "SHDotNetRuntime.h"
#include "ECS_Base/SHECSMacros.h"
#include "ECS_Base/Entity/SHEntity.h"
#include "ECS_Base/System/SHSystem.h"
#include "SH_API.h"
namespace SHADE
@ -26,13 +27,13 @@ namespace SHADE
/// Manages initialisation of the DotNetRuntime and interfacing with CLR code written
/// and executed on .NET.
/// </summary>
class SH_API SHScriptEngine
class SH_API SHScriptEngine : public SHSystem
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructor */
/*-----------------------------------------------------------------------------*/
SHScriptEngine() = delete;
SHScriptEngine() = default;
/*-----------------------------------------------------------------------------*/
/* Lifecycle Functions */
@ -41,33 +42,33 @@ namespace SHADE
/// Initialises the DotNetRuntime and retrieves function pointers to all
/// functions on the CLR used to interface with the engine.
/// </summary>
static void Init();
void Init() override;
/// <summary>
/// Loads the managed script assembly. Ensure this is only called after
/// UnloadScriptAssembly() has been called.
/// </summary>
static void UnloadScriptAssembly();
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>
static void LoadScriptAssembly();
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>
static void ReloadScriptAssembly();
void ReloadScriptAssembly();
/// <summary>
/// Executes the FixedUpdate()s of the Scripts that are attached to
/// Entities.
/// </summary>
static void ExecuteFixedUpdates();
void ExecuteFixedUpdates();
/// <summary>
/// Shuts down the DotNetRuntime.
/// </summary>
static void Exit();
void Exit() override;
/*-----------------------------------------------------------------------------*/
/* Script Manipulation Functions */
@ -84,14 +85,14 @@ namespace SHADE
/// True if successfully added. False otherwise with the error logged to the
/// console.
/// </returns>
static bool AddScript(const SHEntity& entity, const std::string_view& scriptName);
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 Scripts
/// attached.
/// </summary>
/// <param name="entity">The entity to remove the scripts from.</param>
static void RemoveAllScripts(const SHEntity& entity);
void RemoveAllScripts(const SHEntity& entity);
/// <summary>
/// Removes all Scripts attached to the specified Entity. Unlike
/// RemoveAllScripts(), this removes all the scripts immediately.
@ -103,7 +104,7 @@ namespace SHADE
/// Whether or not to call OnDestroy on the scripts. This is ignored if not in
/// play mode.
/// </param>
static void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy);
void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy);
/*-----------------------------------------------------------------------------*/
/* Script Serialisation Functions */
@ -116,7 +117,7 @@ namespace SHADE
/// <returns>
/// String that represents the set of scripts attached to the specified Entity.
/// </returns>
static std::string SerialiseScripts(const SHEntity& entity);
std::string SerialiseScripts(const SHEntity& entity) const;
/// <summary>
/// Loads the specified JSON string and creates a Script for the specified Entity
/// based on the specified JSON string.
@ -125,7 +126,7 @@ namespace SHADE
/// <param name="yaml">
/// The YAML string that represents the Script to load into the Entity.
/// </param>
static void DeserialiseScript(const SHEntity& entity, const std::string& yaml);
void DeserialiseScript(const SHEntity& entity, const std::string& yaml) const;
/*-----------------------------------------------------------------------------*/
/* Script Editor Functions */
@ -138,7 +139,7 @@ namespace SHADE
/// rendering code.
/// </summary>
/// <param name="entity">The Entity to render the Scripts of.</param>
static void RenderScriptsInInspector(const SHEntity& entity);
void RenderScriptsInInspector(const SHEntity& entity) const;
/*-----------------------------------------------------------------------------*/
/* Static Utility Functions */
@ -153,12 +154,12 @@ namespace SHADE
/// can be debugged.
/// </param>
/// <returns>Whether or not the build succeeded.</returns>
static bool BuildScriptAssembly(bool debug = false);
bool BuildScriptAssembly(bool debug = false) const;
/// <summary>
/// Generates a .csproj file for editing and compiling the C# scripts.
/// </summary>
/// <param name="path">File path to the generated file.</param>
static void GenerateScriptsCsProjFile(const std::filesystem::path& path);
void GenerateScriptsCsProjFile(const std::filesystem::path& path) const;
private:
/*-----------------------------------------------------------------------------*/
@ -183,29 +184,29 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
static SHDotNetRuntime dotNet;
SHDotNetRuntime dotNet { false };
// Function Pointers to CLR Code
// - Engine Lifecycle
static CsFuncPtr csEngineInit;
static CsFuncPtr csEngineLoadScripts;
static CsFuncPtr csEngineUnloadScripts;
static CsFuncPtr csEngineReloadScripts;
static CsFuncPtr csEngineExit;
CsFuncPtr csEngineInit = nullptr;
CsFuncPtr csEngineLoadScripts = nullptr;
CsFuncPtr csEngineUnloadScripts = nullptr;
CsFuncPtr csEngineReloadScripts = nullptr;
CsFuncPtr csEngineExit = nullptr;
// - Scripts Store
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;
CsFuncPtr csScriptsFrameSetUp = 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;
CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptDeserialiseYaml = nullptr;
// - Editor
static CsScriptEditorFuncPtr csEditorRenderScripts;
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
// Delegates
/*ECS::EntityEvent::Delegate onEntityCreate;
ECS::EntityEvent::Delegate onEntityDestroy;*/
@ -216,7 +217,7 @@ namespace SHADE
/// <summary>
/// Loads all the function pointers to CLR code that we need to execute.
/// </summary>
static void loadFunctions();
void loadFunctions();
/// <summary>
/// Reads the file via the specified path that represents a build log of error
/// and warning messages.