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 "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
{

View File

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

View File

@ -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<std::string, Ref<ScriptClass>> 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();

View File

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

View File

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