diff --git a/Editor/src/Editor/EditorLayer.cpp b/Editor/src/Editor/EditorLayer.cpp index 943f542..107c8b8 100644 --- a/Editor/src/Editor/EditorLayer.cpp +++ b/Editor/src/Editor/EditorLayer.cpp @@ -14,6 +14,7 @@ #include #include "imgui_internal.h" +#include "Hazel/Scripting/ScriptEngine.h" namespace Hazel @@ -280,8 +281,18 @@ namespace Hazel if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) SaveSceneAs(); + if (ImGui::BeginMenu("Script")) + { + if (ImGui::MenuItem("Reload Script", "Ctrl+R")) + ScriptEngine::ReloadAssemble(); + ImGui::EndMenu(); + } + ImGui::Separator(); - if (ImGui::MenuItem("Exit")) { Hazel::Application::Get().Close(); } + if (ImGui::MenuItem("Exit")) + { + Application::Get().Close(); + } ImGui::EndMenu(); } @@ -636,6 +647,8 @@ namespace Hazel NewScene(); else if (Input::IsKeyPressed(KeyCode::D)) OnDuplicateEntity(); + else if (Input::IsKeyPressed(KeyCode::R)) + ScriptEngine::ReloadAssemble(); } } else { diff --git a/Hazel/src/Hazel/Core/KeyCodes.h b/Hazel/src/Hazel/Core/KeyCodes.h index 8bae8f0..a2f2512 100644 --- a/Hazel/src/Hazel/Core/KeyCodes.h +++ b/Hazel/src/Hazel/Core/KeyCodes.h @@ -26,6 +26,8 @@ namespace Hazel typedef enum class KeyCode { + NONE = 0, + A = 4, B = 5, C = 6, diff --git a/Hazel/src/Hazel/Scripting/ScriptEngine.cpp b/Hazel/src/Hazel/Scripting/ScriptEngine.cpp index 7d21f6c..dab96bf 100644 --- a/Hazel/src/Hazel/Scripting/ScriptEngine.cpp +++ b/Hazel/src/Hazel/Scripting/ScriptEngine.cpp @@ -136,6 +136,10 @@ namespace Hazel MonoAssembly* AppAssembly = nullptr; MonoImage* AppAssemblyImage = nullptr; + std::filesystem::path AppAssemblyFilePath; + std::filesystem::path CoreAssemblyFilePath; + + ScriptClass EntityClass; std::unordered_map> EntityClasses; @@ -154,14 +158,14 @@ namespace Hazel { s_Data = new ScriptEngineData(); InitMono(); + ScriptGlue::RegisterFunctions(); + LoadAssemble("Resources/Scripts/Hazel-ScriptCore.dll"); LoadAppAssemble("Resources/Scripts/Sandbox.dll"); LoadAssemblyClass(); Utils::PrintAssemblyTypes(s_Data->CoreAssembly); ScriptGlue::RegisterComponents(); - ScriptGlue::RegisterFunctions(); - // -------------test--------------- // 1.create an object (and run constructor) @@ -213,6 +217,7 @@ namespace Hazel mono_domain_set(s_Data->AppDomain, true); // TODO: move this later + s_Data->CoreAssemblyFilePath = assemblePath; s_Data->CoreAssembly = Utils::LoadMonoAssembly(assemblePath); s_Data->CoreAssemblyImage = mono_assembly_get_image(s_Data->CoreAssembly); @@ -222,12 +227,29 @@ namespace Hazel void ScriptEngine::LoadAppAssemble(const std::filesystem::path& assemblePath) { // TODO: move this later + s_Data->AppAssemblyFilePath = assemblePath; s_Data->AppAssembly = Utils::LoadMonoAssembly(assemblePath); s_Data->AppAssemblyImage = mono_assembly_get_image(s_Data->AppAssembly); // Utils::PrintAssemblyTypes(s_Data->CoreAssembly); } + void ScriptEngine::ReloadAssemble() + { + HZ_CORE_DEBUG("Reloading script..."); + mono_domain_set(mono_get_root_domain(), false); + mono_domain_unload(s_Data->AppDomain); + + LoadAssemble(s_Data->CoreAssemblyFilePath); + LoadAppAssemble(s_Data->AppAssemblyFilePath); + LoadAssemblyClass(); + + ScriptGlue::RegisterComponents(); + + s_Data->EntityClass = ScriptClass("Hazel", "Entity", true); + HZ_CORE_DEBUG("script Reloaded!"); + } + void ScriptEngine::LoadAssemblyClass() { s_Data->EntityClasses.clear(); diff --git a/Hazel/src/Hazel/Scripting/ScriptEngine.h b/Hazel/src/Hazel/Scripting/ScriptEngine.h index ab8dbd1..e24c799 100644 --- a/Hazel/src/Hazel/Scripting/ScriptEngine.h +++ b/Hazel/src/Hazel/Scripting/ScriptEngine.h @@ -148,6 +148,8 @@ namespace Hazel static void LoadAssemble(const std::filesystem::path& assemblePath); static void LoadAppAssemble(const std::filesystem::path& assemblePath); + static void ReloadAssemble(); + static void LoadAssemblyClass(); static void OnRuntimeStart(Scene* scene); diff --git a/Hazel/src/Hazel/Scripting/ScriptGlue.cpp b/Hazel/src/Hazel/Scripting/ScriptGlue.cpp index fbb2f01..578fd4c 100644 --- a/Hazel/src/Hazel/Scripting/ScriptGlue.cpp +++ b/Hazel/src/Hazel/Scripting/ScriptGlue.cpp @@ -159,6 +159,7 @@ namespace Hazel void ScriptGlue::RegisterComponents() { + s_EntityHasComponentFuncs.clear(); RegisterComponent(AllComponents{}); }