fix: lua coroutines support

This commit is contained in:
MihailRis
2024-06-11 01:52:28 +03:00
parent 590f9757c0
commit 7973a9c32b
15 changed files with 707 additions and 740 deletions
+93 -91
View File
@@ -17,6 +17,7 @@
#include "../../voxels/Block.hpp"
#include "../../world/Level.hpp"
#include "lua/LuaState.hpp"
#include "lua/lua_util.hpp"
#include <iostream>
#include <stdexcept>
@@ -37,12 +38,12 @@ const ContentIndices* scripting::indices = nullptr;
BlocksController* scripting::blocks = nullptr;
LevelController* scripting::controller = nullptr;
void load_script(const fs::path& name) {
static void load_script(const fs::path& name) {
auto paths = scripting::engine->getPaths();
fs::path file = paths->getResources()/fs::path("scripts")/name;
std::string src = files::read_string(file);
state->execute(0, src, file.u8string());
state->execute(state->getMainThread(), 0, src, file.u8string());
}
void scripting::initialize(Engine* engine) {
@@ -59,46 +60,49 @@ scriptenv scripting::get_root_environment() {
}
scriptenv scripting::create_pack_environment(const ContentPack& pack) {
int id = state->createEnvironment(0);
state->pushenv(id);
state->pushvalue(-1);
state->setfield("PACK_ENV");
state->pushstring(pack.id);
state->setfield("PACK_ID");
state->pop();
auto L = state->getMainThread();
int id = state->createEnvironment(L, 0);
state->pushenv(L, id);
lua::pushvalue(L, -1);
lua::setfield(L, "PACK_ENV");
lua::pushstring(L, pack.id);
lua::setfield(L, "PACK_ID");
lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) {
state->removeEnvironment(*id);
state->removeEnvironment(L, *id);
delete id;
});
}
scriptenv scripting::create_doc_environment(const scriptenv& parent, const std::string& name) {
int id = state->createEnvironment(*parent);
state->pushenv(id);
state->pushvalue(-1);
state->setfield("DOC_ENV");
state->pushstring(name.c_str());
state->setfield("DOC_NAME");
auto L = state->getMainThread();
int id = state->createEnvironment(L, *parent);
state->pushenv(L, id);
lua_pushvalue(L, -1);
lua::setfield(L, "DOC_ENV");
lua::pushstring(L, name);
lua::setfield(L, "DOC_NAME");
if (state->getglobal("Document")) {
if (state->getfield("new")) {
state->pushstring(name.c_str());
if (state->callNoThrow(1)) {
state->setfield("document", -3);
if (lua::getglobal(L, "Document")) {
if (lua::getfield(L, "new")) {
lua::pushstring(L, name);
if (lua::callNoThrow(L, 1)) {
lua::setfield(L, "document", -3);
}
}
state->pop();
lua::pop(L);
}
state->pop();
lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) {
state->removeEnvironment(*id);
state->removeEnvironment(L, *id);
delete id;
});
}
void scripting::process_post_runnables() {
if (state->getglobal("__process_post_runnables")) {
state->callNoThrow(0);
auto L = state->getMainThread();
if (lua::getglobal(L, "__process_post_runnables")) {
lua::callNoThrow(L, 0);
}
}
@@ -110,38 +114,42 @@ void scripting::on_world_load(LevelController* controller) {
scripting::controller = controller;
load_script("world.lua");
auto L = state->getMainThread();
for (auto& pack : scripting::engine->getContentPacks()) {
state->emit_event(pack.id + ".worldopen");
state->emitEvent(L, pack.id + ".worldopen");
}
}
void scripting::on_world_tick() {
auto L = state->getMainThread();
for (auto& pack : scripting::engine->getContentPacks()) {
state->emit_event(pack.id + ".worldtick");
state->emitEvent(L, pack.id + ".worldtick");
}
}
void scripting::on_world_save() {
auto L = state->getMainThread();
for (auto& pack : scripting::engine->getContentPacks()) {
state->emit_event(pack.id + ".worldsave");
state->emitEvent(L, pack.id + ".worldsave");
}
}
void scripting::on_world_quit() {
auto L = state->getMainThread();
for (auto& pack : scripting::engine->getContentPacks()) {
state->emit_event(pack.id + ".worldquit");
state->emitEvent(L, pack.id + ".worldquit");
}
state->getglobal("pack");
lua::getglobal(L, "pack");
for (auto& pack : scripting::engine->getContentPacks()) {
state->getfield("unload");
state->pushstring(pack.id);
state->callNoThrow(1);
lua::getfield(L, "unload");
lua::pushstring(L, pack.id);
lua::callNoThrow(L, 1);
}
state->pop();
lua::pop(L);
if (state->getglobal("__scripts_cleanup")) {
state->callNoThrow(0);
if (lua::getglobal(L, "__scripts_cleanup")) {
lua::callNoThrow(L, 0);
}
scripting::level = nullptr;
scripting::content = nullptr;
@@ -152,77 +160,73 @@ void scripting::on_world_quit() {
void scripting::on_blocks_tick(const Block* block, int tps) {
std::string name = block->name + ".blockstick";
state->emit_event(name, [tps] (lua::LuaState* state) {
state->pushinteger(tps);
return 1;
state->emitEvent(state->getMainThread(), name, [tps] (lua_State* 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";
state->emit_event(name, [x, y, z] (lua::LuaState* state) {
state->pushivec3(x, y, z);
return 3;
state->emitEvent(state->getMainThread(), name, [x, y, z] (lua_State* L) {
return lua::pushivec3(L, x, y, z);
});
}
void scripting::random_update_block(const Block* block, int x, int y, int z) {
std::string name = block->name + ".randupdate";
state->emit_event(name, [x, y, z] (lua::LuaState* state) {
state->pushivec3(x, y, z);
return 3;
state->emitEvent(state->getMainThread(), name, [x, y, z] (lua_State* L) {
return lua::pushivec3(L, x, y, z);
});
}
void scripting::on_block_placed(Player* player, const Block* block, int x, int y, int z) {
std::string name = block->name + ".placed";
state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
state->pushivec3(x, y, z);
state->pushinteger(player->getId());
return 4;
state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) {
lua::pushivec3(L, x, y, z);
lua::pushinteger(L, player->getId());
return 4;
});
}
void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) {
std::string name = block->name + ".broken";
state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
state->pushivec3(x, y, z);
state->pushinteger(player->getId());
state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) {
lua::pushivec3(L, x, y, z);
lua::pushinteger(L, player->getId());
return 4;
});
}
bool scripting::on_block_interact(Player* player, const Block* block, glm::ivec3 pos) {
std::string name = block->name + ".interact";
return state->emit_event(name, [pos, player] (lua::LuaState* state) {
state->pushivec3(pos.x, pos.y, pos.z);
state->pushinteger(player->getId());
return state->emitEvent(state->getMainThread(), name, [pos, player] (lua_State* L) {
lua::pushivec3(L, pos.x, pos.y, pos.z);
lua::pushinteger(L, player->getId());
return 4;
});
}
bool scripting::on_item_use(Player* player, const ItemDef* item) {
std::string name = item->name + ".use";
return state->emit_event(name, [player] (lua::LuaState* state) {
state->pushinteger(player->getId());
return 1;
return state->emitEvent(state->getMainThread(), name, [player] (lua_State* L) {
return lua::pushinteger(L, player->getId());
});
}
bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) {
std::string name = item->name + ".useon";
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
state->pushivec3(x, y, z);
state->pushinteger(player->getId());
return state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) {
lua::pushivec3(L, x, y, z);
lua::pushinteger(L, player->getId());
return 4;
});
}
bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) {
std::string name = item->name + ".blockbreakby";
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
state->pushivec3(x, y, z);
state->pushinteger(player->getId());
return state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) {
lua::pushivec3(L, x, y, z);
lua::pushinteger(L, player->getId());
return 4;
});
}
@@ -233,9 +237,9 @@ void scripting::on_ui_open(
) {
auto argsptr = std::make_shared<std::vector<dynamic::Value>>(std::move(args));
std::string name = layout->getId() + ".open";
state->emit_event(name, [=] (lua::LuaState* state) {
state->emitEvent(state->getMainThread(), name, [=] (lua_State* L) {
for (const auto& value : *argsptr) {
state->pushvalue(value);
lua::pushvalue(L, value);
}
return argsptr->size();
});
@@ -243,37 +247,37 @@ void scripting::on_ui_open(
void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) {
std::string name = layout->getId() + ".progress";
state->emit_event(name, [=] (lua::LuaState* state) {
state->pushinteger(workDone);
state->pushinteger(workTotal);
state->emitEvent(state->getMainThread(), name, [=] (lua_State* L) {
lua::pushinteger(L, workDone);
lua::pushinteger(L, workTotal);
return 2;
});
}
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
std::string name = layout->getId() + ".close";
state->emit_event(name, [inventory] (lua::LuaState* state) {
state->pushinteger(inventory == nullptr ? 0 : inventory->getId());
return 1;
state->emitEvent(state->getMainThread(), name, [inventory] (lua_State* L) {
return lua::pushinteger(L, inventory ? inventory->getId() : 0);
});
}
bool scripting::register_event(int env, const std::string& name, const std::string& id) {
if (state->pushenv(env) == 0) {
state->pushglobals();
auto L = state->getMainThread();
if (state->pushenv(L, env) == 0) {
lua::pushglobals(L);
}
if (state->getfield(name)) {
state->pop();
state->getglobal("events");
state->getfield("on");
state->pushstring(id);
state->getfield(name, -4);
state->callNoThrow(2);
state->pop();
if (lua::getfield(L, name)) {
lua::pop(L);
lua::getglobal(L, "events");
lua::getfield(L, "on");
lua::pushstring(L, id);
lua::getfield(L, name, -4);
lua::callNoThrow(L, 2);
lua::pop(L);
// remove previous name
state->pushnil();
state->setfield(name);
lua::pushnil(L);
lua::setfield(L, name);
return true;
}
return false;
@@ -283,7 +287,7 @@ void scripting::load_block_script(const scriptenv& senv, const std::string& pref
int env = *senv;
std::string src = files::read_string(file);
logger.info() << "script (block) " << file.u8string();
state->execute(env, src, file.u8string());
state->execute(state->getMainThread(), env, src, file.u8string());
funcsset.init = register_event(env, "init", prefix+".init");
funcsset.update = register_event(env, "on_update", prefix+".update");
funcsset.randupdate = register_event(env, "on_random_update", prefix+".randupdate");
@@ -297,7 +301,7 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi
int env = *senv;
std::string src = files::read_string(file);
logger.info() << "script (item) " << file.u8string();
state->execute(env, src, file.u8string());
state->execute(state->getMainThread(), env, src, file.u8string());
funcsset.init = register_event(env, "init", prefix+".init");
funcsset.on_use = register_event(env, "on_use", prefix+".use");
@@ -311,8 +315,7 @@ void scripting::load_world_script(const scriptenv& senv, const std::string& pref
std::string src = files::read_string(file);
logger.info() << "loading world script for " << prefix;
state->loadbuffer(env, src, file.u8string());
state->callNoThrow(0);
state->execute(state->getMainThread(), env, src, file.u8string());
register_event(env, "init", prefix+".init");
register_event(env, "on_world_open", prefix+".worldopen");
@@ -327,8 +330,7 @@ void scripting::load_layout_script(const scriptenv& senv, const std::string& pre
std::string src = files::read_string(file);
logger.info() << "loading script " << file.u8string();
state->loadbuffer(env, src, file.u8string());
state->callNoThrow(0);
state->execute(state->getMainThread(), env, src, file.u8string());
script.onopen = register_event(env, "on_open", prefix+".open");
script.onprogress = register_event(env, "on_progress", prefix+".progress");
script.onclose = register_event(env, "on_close", prefix+".close");