fix hud libraries not removing after on_hud_close & fix segfault on illegal access to hud functions

This commit is contained in:
MihailRis 2025-02-11 16:16:15 +03:00
parent b1740bf7eb
commit 0eeea9dde6
5 changed files with 63 additions and 49 deletions

View File

@ -1,9 +1,9 @@
#include "libhud.hpp"
#include <glm/glm.hpp>
#include <iostream>
#include "assets/Assets.hpp"
#include "content/Content.hpp"
#include "engine/Engine.hpp"
#include "frontend/UiDocument.hpp"
#include "frontend/hud.hpp"
#include "graphics/ui/elements/InventoryView.hpp"
@ -16,11 +16,7 @@
#include "voxels/voxel.hpp"
#include "voxels/blocks_agent.hpp"
#include "world/Level.hpp"
#include "api_lua.hpp"
namespace scripting {
extern Hud* hud;
}
using namespace scripting;
static int l_open_inventory(lua::State*) {
@ -176,22 +172,22 @@ static int l_set_allow_pause(lua::State* L) {
}
const luaL_Reg hudlib[] = {
{"open_inventory", lua::wrap<l_open_inventory>},
{"close_inventory", lua::wrap<l_close_inventory>},
{"open", lua::wrap<l_open>},
{"open_block", lua::wrap<l_open_block>},
{"open_permanent", lua::wrap<l_open_permanent>},
{"show_overlay", lua::wrap<l_show_overlay>},
{"get_block_inventory", lua::wrap<l_get_block_inventory>},
{"close", lua::wrap<l_close>},
{"pause", lua::wrap<l_pause>},
{"resume", lua::wrap<l_resume>},
{"is_paused", lua::wrap<l_is_paused>},
{"is_inventory_open", lua::wrap<l_is_inventory_open>},
{"get_player", lua::wrap<l_get_player>},
{"_is_content_access", lua::wrap<l_is_content_access>},
{"_set_content_access", lua::wrap<l_set_content_access>},
{"_set_debug_cheats", lua::wrap<l_set_debug_cheats>},
{"set_allow_pause", lua::wrap<l_set_allow_pause>},
{"open_inventory", wrap_hud<l_open_inventory>},
{"close_inventory", wrap_hud<l_close_inventory>},
{"open", wrap_hud<l_open>},
{"open_block", wrap_hud<l_open_block>},
{"open_permanent", wrap_hud<l_open_permanent>},
{"show_overlay", wrap_hud<l_show_overlay>},
{"get_block_inventory", wrap_hud<l_get_block_inventory>},
{"close", wrap_hud<l_close>},
{"pause", wrap_hud<l_pause>},
{"resume", wrap_hud<l_resume>},
{"is_paused", wrap_hud<l_is_paused>},
{"is_inventory_open", wrap_hud<l_is_inventory_open>},
{"get_player", wrap_hud<l_get_player>},
{"_is_content_access", wrap_hud<l_is_content_access>},
{"_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>},
{NULL, NULL}
};

View File

@ -0,0 +1,17 @@
#pragma once
#include "api_lua.hpp"
#include "logic/scripting/scripting_hud.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "engine/Engine.hpp"
template <lua_CFunction func>
inline int wrap_hud(lua_State* L) {
if (scripting::hud == nullptr) {
return luaL_error(
L, "renderer is not initialized yet, see hud.lua on_hud_open event"
);
}
return lua::wrap<func>(L);
}

View File

@ -1,11 +1,8 @@
#include "api_lua.hpp"
#include "libhud.hpp"
#include "logic/scripting/scripting_hud.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/ParticlesRenderer.hpp"
#include "graphics/render/Emitter.hpp"
#include "assets/assets_util.hpp"
#include "engine/Engine.hpp"
using namespace scripting;
@ -81,10 +78,10 @@ static int l_is_alive(lua::State* L) {
}
const luaL_Reg particleslib[] = {
{"emit", lua::wrap<l_emit>},
{"stop", lua::wrap<l_stop>},
{"is_alive", lua::wrap<l_is_alive>},
{"get_origin", lua::wrap<l_get_origin>},
{"set_origin", lua::wrap<l_set_origin>},
{"emit", wrap_hud<l_emit>},
{"stop", wrap_hud<l_stop>},
{"is_alive", wrap_hud<l_is_alive>},
{"get_origin", wrap_hud<l_get_origin>},
{"set_origin", wrap_hud<l_set_origin>},
{NULL, NULL}
};

View File

@ -1,10 +1,7 @@
#include "api_lua.hpp"
#include "libhud.hpp"
#include "logic/scripting/scripting_hud.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/TextsRenderer.hpp"
#include "graphics/render/TextNote.hpp"
#include "engine/Engine.hpp"
using namespace scripting;
@ -99,17 +96,17 @@ static int l_set_rotation(lua::State* L) {
}
const luaL_Reg text3dlib[] = {
{"show", lua::wrap<l_show>},
{"hide", lua::wrap<l_hide>},
{"get_text", lua::wrap<l_get_text>},
{"set_text", lua::wrap<l_set_text>},
{"get_pos", lua::wrap<l_get_pos>},
{"set_pos", lua::wrap<l_set_pos>},
{"get_axis_x", lua::wrap<l_get_axis_x>},
{"set_axis_x", lua::wrap<l_set_axis_x>},
{"get_axis_y", lua::wrap<l_get_axis_y>},
{"set_axis_y", lua::wrap<l_set_axis_y>},
{"set_rotation", lua::wrap<l_set_rotation>},
{"update_settings", lua::wrap<l_update_settings>},
{"show", wrap_hud<l_show>},
{"hide", wrap_hud<l_hide>},
{"get_text", wrap_hud<l_get_text>},
{"set_text", wrap_hud<l_set_text>},
{"get_pos", wrap_hud<l_get_pos>},
{"set_pos", wrap_hud<l_set_pos>},
{"get_axis_x", wrap_hud<l_get_axis_x>},
{"set_axis_x", wrap_hud<l_set_axis_x>},
{"get_axis_y", wrap_hud<l_get_axis_y>},
{"set_axis_y", wrap_hud<l_set_axis_y>},
{"set_rotation", wrap_hud<l_set_rotation>},
{"update_settings", wrap_hud<l_update_settings>},
{NULL, NULL}
};

View File

@ -65,15 +65,22 @@ void scripting::on_frontend_render() {
}
void scripting::on_frontend_close() {
auto L = lua::get_main_state();
for (auto& pack : engine->getAllContentPacks()) {
lua::emit_event(
lua::get_main_state(),
L,
pack.id + ":.hudclose",
[&](lua::State* L) {
return lua::pushinteger(L, hud->getPlayer()->getId());
}
);
}
lua::pushnil(L);
lua::setglobal(L, "hud");
lua::pushnil(L);
lua::setglobal(L, "gfx");
scripting::renderer = nullptr;
scripting::hud = nullptr;
}