move generator script execution to an isolated Lua state

This commit is contained in:
MihailRis
2024-10-06 18:23:33 +03:00
parent 091805a16e
commit 73d96fd4f7
8 changed files with 101 additions and 75 deletions
+41 -46
View File
@@ -43,7 +43,7 @@ void scripting::load_script(const fs::path& name, bool throwable) {
auto paths = scripting::engine->getPaths();
fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name;
std::string src = files::read_string(file);
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::loadbuffer(L, 0, src, file.u8string());
if (throwable) {
lua::call(L, 0, 0);
@@ -57,7 +57,7 @@ int scripting::load_script(
) {
std::string src = files::read_string(file);
logger.info() << "script (" << type << ") " << file.u8string();
return lua::execute(lua::get_main_thread(), env, src, file.u8string());
return lua::execute(lua::get_main_state(), env, src, file.u8string());
}
void scripting::initialize(Engine* engine) {
@@ -74,18 +74,13 @@ void scripting::initialize(Engine* engine) {
}
[[nodiscard]] scriptenv scripting::create_environment() {
auto L = lua::get_main_thread();
int id = lua::create_environment(L, 0);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
lua::removeEnvironment(L, *id);
delete id;
});
return lua::create_environment(lua::get_main_state());
}
[[nodiscard]] scriptenv scripting::create_pack_environment(
const ContentPack& pack
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
int id = lua::create_environment(L, 0);
lua::pushenv(L, id);
lua::pushvalue(L, -1);
@@ -94,7 +89,7 @@ void scripting::initialize(Engine* engine) {
lua::setfield(L, "PACK_ID");
lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
lua::removeEnvironment(L, *id);
lua::remove_environment(L, *id);
delete id;
});
}
@@ -102,7 +97,7 @@ void scripting::initialize(Engine* engine) {
[[nodiscard]] scriptenv scripting::create_doc_environment(
const scriptenv& parent, const std::string& name
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
int id = lua::create_environment(L, *parent);
lua::pushenv(L, id);
lua::pushvalue(L, -1);
@@ -121,7 +116,7 @@ void scripting::initialize(Engine* engine) {
}
lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
lua::removeEnvironment(L, *id);
lua::remove_environment(L, *id);
delete id;
});
}
@@ -129,7 +124,7 @@ void scripting::initialize(Engine* engine) {
[[nodiscard]] static scriptenv create_component_environment(
const scriptenv& parent, int entityIdx, const std::string& name
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
int id = lua::create_environment(L, *parent);
lua::pushvalue(L, entityIdx);
@@ -151,13 +146,13 @@ void scripting::initialize(Engine* engine) {
lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
lua::removeEnvironment(L, *id);
lua::remove_environment(L, *id);
delete id;
});
}
void scripting::process_post_runnables() {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
if (lua::getglobal(L, "__process_post_runnables")) {
lua::call_nothrow(L, 0);
}
@@ -171,28 +166,28 @@ void scripting::on_world_load(LevelController* controller) {
scripting::controller = controller;
load_script("world.lua", false);
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
for (auto& pack : scripting::engine->getContentPacks()) {
lua::emit_event(L, pack.id + ".worldopen");
}
}
void scripting::on_world_tick() {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
for (auto& pack : scripting::engine->getContentPacks()) {
lua::emit_event(L, pack.id + ".worldtick");
}
}
void scripting::on_world_save() {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
for (auto& pack : scripting::engine->getContentPacks()) {
lua::emit_event(L, pack.id + ".worldsave");
}
}
void scripting::on_world_quit() {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
for (auto& pack : scripting::engine->getContentPacks()) {
lua::emit_event(L, pack.id + ".worldquit");
}
@@ -217,21 +212,21 @@ void scripting::on_world_quit() {
void scripting::on_blocks_tick(const Block& block, int tps) {
std::string name = block.name + ".blockstick";
lua::emit_event(lua::get_main_thread(), name, [tps](auto L) {
lua::emit_event(lua::get_main_state(), name, [tps](auto L) {
return lua::pushinteger(L, tps);
});
}
void scripting::update_block(const Block& block, int x, int y, int z) {
std::string name = block.name + ".update";
lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) {
lua::emit_event(lua::get_main_state(), name, [x, y, z](auto L) {
return lua::pushivec_stack(L, glm::ivec3(x, y, z));
});
}
void scripting::random_update_block(const Block& block, int x, int y, int z) {
std::string name = block.name + ".randupdate";
lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) {
lua::emit_event(lua::get_main_state(), name, [x, y, z](auto L) {
return lua::pushivec_stack(L, glm::ivec3(x, y, z));
});
}
@@ -240,7 +235,7 @@ void scripting::on_block_placed(
Player* player, const Block& block, int x, int y, int z
) {
std::string name = block.name + ".placed";
lua::emit_event(lua::get_main_thread(), name, [x, y, z, player](auto L) {
lua::emit_event(lua::get_main_state(), name, [x, y, z, player](auto L) {
lua::pushivec_stack(L, glm::ivec3(x, y, z));
lua::pushinteger(L, player ? player->getId() : -1);
return 4;
@@ -254,7 +249,7 @@ void scripting::on_block_placed(
for (auto& [packid, pack] : content->getPacks()) {
if (pack->worldfuncsset.onblockplaced) {
lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
packid + ".blockplaced",
world_event_args
);
@@ -268,7 +263,7 @@ void scripting::on_block_broken(
if (block.rt.funcsset.onbroken) {
std::string name = block.name + ".broken";
lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
name,
[x, y, z, player](auto L) {
lua::pushivec_stack(L, glm::ivec3(x, y, z));
@@ -286,7 +281,7 @@ void scripting::on_block_broken(
for (auto& [packid, pack] : content->getPacks()) {
if (pack->worldfuncsset.onblockbroken) {
lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
packid + ".blockbroken",
world_event_args
);
@@ -298,7 +293,7 @@ bool scripting::on_block_interact(
Player* player, const Block& block, glm::ivec3 pos
) {
std::string name = block.name + ".interact";
return lua::emit_event(lua::get_main_thread(), name, [pos, player](auto L) {
return lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) {
lua::pushivec_stack(L, pos);
lua::pushinteger(L, player->getId());
return 4;
@@ -308,7 +303,7 @@ bool scripting::on_block_interact(
bool scripting::on_item_use(Player* player, const ItemDef& item) {
std::string name = item.name + ".use";
return lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
name,
[player](lua::State* L) { return lua::pushinteger(L, player->getId()); }
);
@@ -319,7 +314,7 @@ bool scripting::on_item_use_on_block(
) {
std::string name = item.name + ".useon";
return lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
name,
[ipos, normal, player](auto L) {
lua::pushivec_stack(L, ipos);
@@ -335,7 +330,7 @@ bool scripting::on_item_break_block(
) {
std::string name = item.name + ".blockbreakby";
return lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
name,
[x, y, z, player](auto L) {
lua::pushivec_stack(L, glm::ivec3(x, y, z));
@@ -348,7 +343,7 @@ bool scripting::on_item_break_block(
dv::value scripting::get_component_value(
const scriptenv& env, const std::string& name
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::pushenv(L, *env);
if (lua::getfield(L, name)) {
return lua::tovalue(L, -1);
@@ -363,7 +358,7 @@ void scripting::on_entity_spawn(
const dv::value& args,
const dv::value& saved
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::requireglobal(L, STDCOMP);
if (lua::getfield(L, "new_Entity")) {
lua::pushinteger(L, eid);
@@ -431,7 +426,7 @@ static void process_entity_callback(
const std::string& name,
std::function<int(lua::State*)> args
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::pushenv(L, *env);
if (lua::getfield(L, name)) {
if (args) {
@@ -461,7 +456,7 @@ void scripting::on_entity_despawn(const Entity& entity) {
process_entity_callback(
entity, "on_despawn", &entity_funcs_set::on_despawn, nullptr
);
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::get_from(L, "stdcomp", "remove_Entity", true);
lua::pushinteger(L, entity.getUID());
lua::call(L, 1, 0);
@@ -561,7 +556,7 @@ void scripting::on_entity_used(const Entity& entity, Player* player) {
}
void scripting::on_entities_update(int tps, int parts, int part) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::get_from(L, STDCOMP, "update", true);
lua::pushinteger(L, tps);
lua::pushinteger(L, parts);
@@ -571,7 +566,7 @@ void scripting::on_entities_update(int tps, int parts, int part) {
}
void scripting::on_entities_render(float delta) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
lua::get_from(L, STDCOMP, "render", true);
lua::pushnumber(L, delta);
lua::call_nothrow(L, 1, 0);
@@ -584,7 +579,7 @@ void scripting::on_ui_open(
auto argsptr =
std::make_shared<std::vector<dv::value>>(std::move(args));
std::string name = layout->getId() + ".open";
lua::emit_event(lua::get_main_thread(), name, [=](auto L) {
lua::emit_event(lua::get_main_state(), name, [=](auto L) {
for (const auto& value : *argsptr) {
lua::pushvalue(L, value);
}
@@ -596,7 +591,7 @@ void scripting::on_ui_progress(
UiDocument* layout, int workDone, int workTotal
) {
std::string name = layout->getId() + ".progress";
lua::emit_event(lua::get_main_thread(), name, [=](auto L) {
lua::emit_event(lua::get_main_state(), name, [=](auto L) {
lua::pushinteger(L, workDone);
lua::pushinteger(L, workTotal);
return 2;
@@ -605,7 +600,7 @@ void scripting::on_ui_progress(
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
std::string name = layout->getId() + ".close";
lua::emit_event(lua::get_main_thread(), name, [inventory](auto L) {
lua::emit_event(lua::get_main_state(), name, [inventory](auto L) {
return lua::pushinteger(L, inventory ? inventory->getId() : 0);
});
}
@@ -613,7 +608,7 @@ void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
bool scripting::register_event(
int env, const std::string& name, const std::string& id
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
if (lua::pushenv(L, env) == 0) {
lua::pushglobals(L);
}
@@ -635,7 +630,7 @@ bool scripting::register_event(
}
int scripting::get_values_on_stack() {
return lua::gettop(lua::get_main_thread());
return lua::gettop(lua::get_main_state());
}
void scripting::load_block_script(
@@ -645,7 +640,7 @@ void scripting::load_block_script(
block_funcs_set& funcsset
) {
int env = *senv;
lua::pop(lua::get_main_thread(), load_script(env, "block", file));
lua::pop(lua::get_main_state(), load_script(env, "block", file));
funcsset.init = register_event(env, "init", prefix + ".init");
funcsset.update = register_event(env, "on_update", prefix + ".update");
funcsset.randupdate =
@@ -665,7 +660,7 @@ void scripting::load_item_script(
item_funcs_set& funcsset
) {
int env = *senv;
lua::pop(lua::get_main_thread(), load_script(env, "item", file));
lua::pop(lua::get_main_state(), load_script(env, "item", file));
funcsset.init = register_event(env, "init", prefix + ".init");
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
funcsset.on_use_on_block =
@@ -677,7 +672,7 @@ void scripting::load_item_script(
void scripting::load_entity_component(
const std::string& name, const fs::path& file
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
std::string src = files::read_string(file);
logger.info() << "script (component) " << file.u8string();
lua::loadbuffer(L, 0, src, "C!" + name);
@@ -691,7 +686,7 @@ void scripting::load_world_script(
world_funcs_set& funcsset
) {
int env = *senv;
lua::pop(lua::get_main_thread(), load_script(env, "world", file));
lua::pop(lua::get_main_state(), load_script(env, "world", file));
register_event(env, "init", prefix + ".init");
register_event(env, "on_world_open", prefix + ".worldopen");
register_event(env, "on_world_tick", prefix + ".worldtick");
@@ -710,7 +705,7 @@ void scripting::load_layout_script(
uidocscript& script
) {
int env = *senv;
lua::pop(lua::get_main_thread(), load_script(env, "layout", file));
lua::pop(lua::get_main_state(), load_script(env, "layout", file));
script.onopen = register_event(env, "on_open", prefix + ".open");
script.onprogress =
register_event(env, "on_progress", prefix + ".progress");