add Assemble reloading

This commit is contained in:
2025-10-26 13:40:19 +08:00
parent e8dec2e2d3
commit 477d6b9ffe
5 changed files with 43 additions and 3 deletions

View File

@ -14,6 +14,7 @@
#include <Hazel/Utils/PlatformUtils.h> #include <Hazel/Utils/PlatformUtils.h>
#include "imgui_internal.h" #include "imgui_internal.h"
#include "Hazel/Scripting/ScriptEngine.h"
namespace Hazel namespace Hazel
@ -280,8 +281,18 @@ namespace Hazel
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S"))
SaveSceneAs(); SaveSceneAs();
if (ImGui::BeginMenu("Script"))
{
if (ImGui::MenuItem("Reload Script", "Ctrl+R"))
ScriptEngine::ReloadAssemble();
ImGui::EndMenu();
}
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Exit")) { Hazel::Application::Get().Close(); } if (ImGui::MenuItem("Exit"))
{
Application::Get().Close();
}
ImGui::EndMenu(); ImGui::EndMenu();
} }
@ -636,6 +647,8 @@ namespace Hazel
NewScene(); NewScene();
else if (Input::IsKeyPressed(KeyCode::D)) else if (Input::IsKeyPressed(KeyCode::D))
OnDuplicateEntity(); OnDuplicateEntity();
else if (Input::IsKeyPressed(KeyCode::R))
ScriptEngine::ReloadAssemble();
} }
} else } else
{ {

View File

@ -26,6 +26,8 @@ namespace Hazel
typedef enum class KeyCode typedef enum class KeyCode
{ {
NONE = 0,
A = 4, A = 4,
B = 5, B = 5,
C = 6, C = 6,

View File

@ -136,6 +136,10 @@ namespace Hazel
MonoAssembly* AppAssembly = nullptr; MonoAssembly* AppAssembly = nullptr;
MonoImage* AppAssemblyImage = nullptr; MonoImage* AppAssemblyImage = nullptr;
std::filesystem::path AppAssemblyFilePath;
std::filesystem::path CoreAssemblyFilePath;
ScriptClass EntityClass; ScriptClass EntityClass;
std::unordered_map<std::string, Ref<ScriptClass>> EntityClasses; std::unordered_map<std::string, Ref<ScriptClass>> EntityClasses;
@ -154,14 +158,14 @@ namespace Hazel
{ {
s_Data = new ScriptEngineData(); s_Data = new ScriptEngineData();
InitMono(); InitMono();
ScriptGlue::RegisterFunctions();
LoadAssemble("Resources/Scripts/Hazel-ScriptCore.dll"); LoadAssemble("Resources/Scripts/Hazel-ScriptCore.dll");
LoadAppAssemble("Resources/Scripts/Sandbox.dll"); LoadAppAssemble("Resources/Scripts/Sandbox.dll");
LoadAssemblyClass(); LoadAssemblyClass();
Utils::PrintAssemblyTypes(s_Data->CoreAssembly); Utils::PrintAssemblyTypes(s_Data->CoreAssembly);
ScriptGlue::RegisterComponents(); ScriptGlue::RegisterComponents();
ScriptGlue::RegisterFunctions();
// -------------test--------------- // -------------test---------------
// 1.create an object (and run constructor) // 1.create an object (and run constructor)
@ -213,6 +217,7 @@ namespace Hazel
mono_domain_set(s_Data->AppDomain, true); mono_domain_set(s_Data->AppDomain, true);
// TODO: move this later // TODO: move this later
s_Data->CoreAssemblyFilePath = assemblePath;
s_Data->CoreAssembly = Utils::LoadMonoAssembly(assemblePath); s_Data->CoreAssembly = Utils::LoadMonoAssembly(assemblePath);
s_Data->CoreAssemblyImage = mono_assembly_get_image(s_Data->CoreAssembly); s_Data->CoreAssemblyImage = mono_assembly_get_image(s_Data->CoreAssembly);
@ -222,12 +227,29 @@ namespace Hazel
void ScriptEngine::LoadAppAssemble(const std::filesystem::path& assemblePath) void ScriptEngine::LoadAppAssemble(const std::filesystem::path& assemblePath)
{ {
// TODO: move this later // TODO: move this later
s_Data->AppAssemblyFilePath = assemblePath;
s_Data->AppAssembly = Utils::LoadMonoAssembly(assemblePath); s_Data->AppAssembly = Utils::LoadMonoAssembly(assemblePath);
s_Data->AppAssemblyImage = mono_assembly_get_image(s_Data->AppAssembly); s_Data->AppAssemblyImage = mono_assembly_get_image(s_Data->AppAssembly);
// Utils::PrintAssemblyTypes(s_Data->CoreAssembly); // 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() void ScriptEngine::LoadAssemblyClass()
{ {
s_Data->EntityClasses.clear(); s_Data->EntityClasses.clear();

View File

@ -148,6 +148,8 @@ namespace Hazel
static void LoadAssemble(const std::filesystem::path& assemblePath); static void LoadAssemble(const std::filesystem::path& assemblePath);
static void LoadAppAssemble(const std::filesystem::path& assemblePath); static void LoadAppAssemble(const std::filesystem::path& assemblePath);
static void ReloadAssemble();
static void LoadAssemblyClass(); static void LoadAssemblyClass();
static void OnRuntimeStart(Scene* scene); static void OnRuntimeStart(Scene* scene);

View File

@ -159,6 +159,7 @@ namespace Hazel
void ScriptGlue::RegisterComponents() void ScriptGlue::RegisterComponents()
{ {
s_EntityHasComponentFuncs.clear();
RegisterComponent(AllComponents{}); RegisterComponent(AllComponents{});
} }