feat: reloading hud scripts

This commit is contained in:
MihailRis
2025-03-15 21:12:19 +03:00
parent f9998f0a93
commit 6fb14ee0a4
5 changed files with 36 additions and 14 deletions
+18
View File
@@ -171,6 +171,23 @@ static int l_set_allow_pause(lua::State* L) {
return 0;
}
static int l_reload_script(lua::State* L) {
auto packid = lua::require_string(L, 1);
if (content == nullptr) {
throw std::runtime_error("content is not initialized");
}
auto& writeableContent = *engine->getWriteableContent();
auto pack = writeableContent.getPackRuntime(packid);
const auto& info = pack->getInfo();
scripting::load_hud_script(
pack->getEnvironment(),
packid,
info.folder / "scripts/hud.lua",
pack->getId() + ":scripts/hud.lua"
);
return 0;
}
const luaL_Reg hudlib[] = {
{"open_inventory", wrap_hud<l_open_inventory>},
{"close_inventory", wrap_hud<l_close_inventory>},
@@ -189,5 +206,6 @@ const luaL_Reg hudlib[] = {
{"_set_content_access", wrap_hud<l_set_content_access>},
{"_set_debug_cheats", wrap_hud<l_set_debug_cheats>},
{"set_allow_pause", wrap_hud<l_set_allow_pause>},
{"reload_script", wrap_hud<l_reload_script>},
{NULL, NULL}
};
+13 -13
View File
@@ -806,21 +806,21 @@ bool scripting::register_event(
if (lua::pushenv(L, env) == 0) {
lua::pushglobals(L);
}
if (lua::getfield(L, name)) {
lua::pop(L);
lua::getglobal(L, "events");
lua::getfield(L, "reset");
lua::pushstring(L, id);
lua::getfield(L, name, -4);
lua::call_nothrow(L, 2);
lua::pop(L);
// remove previous name
bool success = true;
lua::getglobal(L, "events");
lua::getfield(L, "reset");
lua::pushstring(L, id);
if (!lua::getfield(L, name, -4)) {
success = false;
lua::pushnil(L);
lua::setfield(L, name);
return true;
}
return false;
lua::call_nothrow(L, 2);
lua::pop(L);
// remove previous name
lua::pushnil(L);
lua::setfield(L, name);
return success;
}
int scripting::get_values_on_stack() {