From 055d6d7d0b088b48ece1a80420d87e9c424f1193 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 10 Jul 2023 14:19:56 -0400 Subject: [PATCH] Clean up --- Editor/Editor.cpp | 3 +- .../Scene/Components/WrenScriptComponent.h | 4 +- Nuake/src/Scripting/Modules/SceneModule.h | 2 +- Nuake/src/Scripting/ScriptingEngine.cpp | 5 +-- Nuake/src/Scripting/WrenScript.cpp | 41 +++++++++---------- Nuake/src/Scripting/WrenScript.h | 21 +++++----- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index f530c6df..c1d33b85 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -28,6 +27,8 @@ #include "src/Misc/GizmoDrawer.h" #include "src/Windows/FileSystemUI.h" +#include + const std::string WindowTitle = "Nuake Editor"; int main(int argc, char* argv[]) diff --git a/Nuake/src/Scene/Components/WrenScriptComponent.h b/Nuake/src/Scene/Components/WrenScriptComponent.h index 6ed39012..d5f069e2 100644 --- a/Nuake/src/Scene/Components/WrenScriptComponent.h +++ b/Nuake/src/Scene/Components/WrenScriptComponent.h @@ -1,8 +1,10 @@ #pragma once #include "src/Scripting/WrenScript.h" #include "src/Resource/Serializable.h" +#include "src/Core/FileSystem.h" -namespace Nuake { +namespace Nuake +{ class WrenScriptComponent { public: diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index b6f3b907..2e0087d7 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -115,7 +115,7 @@ namespace Nuake { if (ent.HasComponent()) { - wrenSetSlotHandle(vm, 0, ent.GetComponent().mWrenScript->m_Instance); + wrenSetSlotHandle(vm, 0, ent.GetComponent().mWrenScript->GetWrenInstanceHandle()); } } diff --git a/Nuake/src/Scripting/ScriptingEngine.cpp b/Nuake/src/Scripting/ScriptingEngine.cpp index e21af9a3..1c286233 100644 --- a/Nuake/src/Scripting/ScriptingEngine.cpp +++ b/Nuake/src/Scripting/ScriptingEngine.cpp @@ -2,7 +2,6 @@ #include "WrenScript.h" #include "src/Core/FileSystem.h" -#include "src/Scripting/VM.h" #include "src/Scripting/Modules/ScriptModule.h" #include "src/Scripting/Modules/EngineModule.h" #include "src/Scripting/Modules/SceneModule.h" @@ -11,7 +10,7 @@ #include "src/Scripting/Modules/PhysicsModule.h" namespace Nuake { - Ref ScriptingEngine::m_VM; + WrenVM* ScriptingEngine::m_WrenVM; std::map> ScriptingEngine::m_Scripts; std::map> ScriptingEngine::Modules; @@ -163,8 +162,6 @@ namespace Nuake { config.errorFn = &errorFn; config.writeFn = &writeFn; config.bindForeignMethodFn = &bindForeignMethod; - - m_VM = CreateRef(); m_WrenVM = wrenNewVM(&config); Logger::Log("Registing Scripting Modules"); diff --git a/Nuake/src/Scripting/WrenScript.cpp b/Nuake/src/Scripting/WrenScript.cpp index 5583b917..aadb1d8a 100644 --- a/Nuake/src/Scripting/WrenScript.cpp +++ b/Nuake/src/Scripting/WrenScript.cpp @@ -8,10 +8,10 @@ namespace Nuake { - WrenScript::WrenScript(Ref file, bool isEntity) + WrenScript::WrenScript(Ref file, bool isEntityScript) { mFile = file; - IsEntity = isEntity; + m_IsEntityScript = isEntityScript; ParseModules(); } @@ -32,45 +32,42 @@ namespace Nuake if (s == "class" && i + 1 < splits.size() && !hasFoundModule) { std::string moduleFound = splits[i + 1]; - mModules.push_back(moduleFound); + m_Modules.push_back(moduleFound); hasFoundModule = true; } } } - // Close the file MyReadFile.close(); } - std::vector WrenScript::GetModules() + std::vector WrenScript::GetModules() const { - return mModules; + return m_Modules; } void WrenScript::Build(unsigned int moduleId, bool isEntity) { WrenVM* vm = ScriptingEngine::GetWrenVM(); - std::string relativePath = mFile->GetRelativePath(); + const std::string& relativePath = mFile->GetRelativePath(); // Can't import twice the same script, otherwise gives a compile error. if (!ScriptingEngine::IsScriptImported(relativePath)) { - std::string source = "import \"" + relativePath + "\" for " + GetModules()[moduleId]; + const std::string& source = "import \"" + relativePath + "\" for " + GetModules()[moduleId]; WrenInterpretResult result = wrenInterpret(vm, "main", source.c_str()); - - Logger::Log("Wren result: " + std::to_string(result)); if (result == WREN_RESULT_SUCCESS) { - Logger::Log("Wren result success: " + std::to_string(result)); - CompiledSuccesfully = true; + Logger::Log("[ScriptingEngine] Compiled succesfully: " + std::to_string(result)); + m_HasCompiledSuccesfully = true; ScriptingEngine::ImportScript(relativePath); } else { - Logger::Log("Wren result failed: " + std::to_string(result)); - CompiledSuccesfully = false; + Logger::Log("[ScriptingEngine] Compiled failed: " + std::to_string(result)); + m_HasCompiledSuccesfully = false; return; } } @@ -96,12 +93,12 @@ namespace Nuake if (isEntity) this->m_SetEntityIDHandle = wrenMakeCallHandle(vm, "SetEntityId(_)"); - CompiledSuccesfully = true; + m_HasCompiledSuccesfully = true; } void WrenScript::CallInit() { - if (!CompiledSuccesfully) return; + if (!m_HasCompiledSuccesfully) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); wrenSetSlotHandle(vm, 0, this->m_Instance); @@ -110,7 +107,7 @@ namespace Nuake void WrenScript::CallUpdate(float timestep) { - if (!CompiledSuccesfully) return; + if (!m_HasCompiledSuccesfully) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); wrenEnsureSlots(vm, 2); @@ -121,7 +118,7 @@ namespace Nuake void WrenScript::CallFixedUpdate(float timestep) { - if (!CompiledSuccesfully) return; + if (!m_HasCompiledSuccesfully) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); wrenEnsureSlots(vm, 2); @@ -132,7 +129,7 @@ namespace Nuake void WrenScript::CallExit() { - if (!CompiledSuccesfully || !m_Instance) + if (!m_HasCompiledSuccesfully || !m_Instance) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); @@ -143,7 +140,7 @@ namespace Nuake void WrenScript::RegisterMethod(const std::string& signature) { - if (!CompiledSuccesfully) + if (!m_HasCompiledSuccesfully) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); @@ -153,7 +150,7 @@ namespace Nuake void WrenScript::CallMethod(const std::string& signature) { - if (!CompiledSuccesfully) + if (!m_HasCompiledSuccesfully) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); @@ -169,7 +166,7 @@ namespace Nuake void WrenScript::SetScriptableEntityID(int id) { - if (!CompiledSuccesfully) + if (!m_HasCompiledSuccesfully) return; WrenVM* vm = ScriptingEngine::GetWrenVM(); diff --git a/Nuake/src/Scripting/WrenScript.h b/Nuake/src/Scripting/WrenScript.h index 9daf9a7a..7a5c5009 100644 --- a/Nuake/src/Scripting/WrenScript.h +++ b/Nuake/src/Scripting/WrenScript.h @@ -12,11 +12,10 @@ namespace Nuake class WrenScript { private: - bool CompiledSuccesfully; - bool IsEntity = false; - std::vector mModules; + bool m_HasCompiledSuccesfully; + bool m_IsEntityScript = false; + std::vector m_Modules; - public: Ref mFile; std::map methods; @@ -27,12 +26,11 @@ namespace Nuake WrenHandle* m_OnExitHandle; WrenHandle* m_SetEntityIDHandle; - // Building - WrenScript(Ref file, bool isEntity); - + public: + WrenScript(Ref file, bool isEntityScript); void ParseModules(); - std::vector GetModules(); - void Build(unsigned int moduleId, bool isEntity = false); + + void Build(unsigned int moduleId, bool isEntityScript = false); // Method calls void CallInit(); @@ -44,7 +42,10 @@ namespace Nuake void CallMethod(const std::string& signature); void SetScriptableEntityID(int id); + bool HasCompiledSuccesfully() const { return m_HasCompiledSuccesfully; } - bool HasCompiledSuccesfully() { return CompiledSuccesfully; } + std::vector GetModules() const; + + WrenHandle* GetWrenInstanceHandle() const { return m_Instance; } }; }