Merge pull request #306 from MihailRis/isolated-lua-states

Add isolated lua states
This commit is contained in:
MihailRis 2024-10-06 21:54:14 +03:00 committed by GitHub
commit cd952e7216
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
41 changed files with 199 additions and 168 deletions

View File

@ -4,7 +4,7 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge
jobs:
build:

View File

@ -4,7 +4,7 @@ on:
push:
branches: [ "main", "release-**"]
pull_request:
branches: [ "main" ]
branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge
jobs:
build-dmg:

View File

@ -4,7 +4,7 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge
jobs:
build-windows:

View File

@ -3,7 +3,7 @@
#include <exception>
#include <string>
#include "lua_util.hpp"
#include "../lua_util.hpp"
/// Definitions can be found in local .cpp files
/// having same names as declarations

View File

@ -7,7 +7,7 @@
#include "world/generator/VoxelFragment.hpp"
#include "content/ContentLoader.hpp"
#include "engine.hpp"
#include "lua_custom_types.hpp"
#include "../lua_custom_types.hpp"
using namespace scripting;

View File

@ -5,7 +5,7 @@
#include "debug/Logger.hpp"
#include "util/stringutil.hpp"
#include "api_lua.hpp"
#include "libs/api_lua.hpp"
#include "lua_custom_types.hpp"
static debug::Logger logger("lua-state");
@ -17,7 +17,7 @@ luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
}
static void remove_lib_funcs(
lua::State* L, const char* libname, const char* funcs[]
State* L, const char* libname, const char* funcs[]
) {
if (getglobal(L, libname)) {
for (uint i = 0; funcs[i]; i++) {
@ -28,49 +28,52 @@ static void remove_lib_funcs(
}
}
static void create_libs(lua::State* L) {
openlib(L, "audio", audiolib);
[[nodiscard]] scriptenv lua::create_environment(State* L) {
int id = lua::create_environment(L, 0);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
lua::remove_environment(L, *id);
delete id;
});
}
static void create_libs(State* L, StateType stateType) {
openlib(L, "block", blocklib);
openlib(L, "console", consolelib);
openlib(L, "core", corelib);
openlib(L, "file", filelib);
openlib(L, "gui", guilib);
openlib(L, "input", inputlib);
openlib(L, "inventory", inventorylib);
openlib(L, "generation", generationlib);
openlib(L, "item", itemlib);
openlib(L, "json", jsonlib);
openlib(L, "mat4", mat4lib);
openlib(L, "pack", packlib);
openlib(L, "player", playerlib);
openlib(L, "quat", quatlib);
openlib(L, "time", timelib);
openlib(L, "toml", tomllib);
openlib(L, "vec2", vec2lib);
openlib(L, "vec3", vec3lib);
openlib(L, "vec4", vec4lib);
openlib(L, "world", worldlib);
openlib(L, "entities", entitylib);
openlib(L, "cameras", cameralib);
if (stateType == StateType::BASE) {
openlib(L, "gui", guilib);
openlib(L, "input", inputlib);
openlib(L, "inventory", inventorylib);
openlib(L, "world", worldlib);
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "player", playerlib);
// components
openlib(L, "__skeleton", skeletonlib);
openlib(L, "__rigidbody", rigidbodylib);
openlib(L, "__transform", transformlib);
openlib(L, "entities", entitylib);
openlib(L, "cameras", cameralib);
// components
openlib(L, "__skeleton", skeletonlib);
openlib(L, "__rigidbody", rigidbodylib);
openlib(L, "__transform", transformlib);
}
addfunc(L, "print", lua::wrap<l_print>);
}
void lua::initialize() {
logger.info() << LUA_VERSION;
logger.info() << LUAJIT_VERSION;
auto L = luaL_newstate();
if (L == nullptr) {
throw luaerror("could not to initialize Lua");
}
main_thread = L;
void lua::init_state(State* L, StateType stateType) {
// Allowed standard libraries
pop(L, luaopen_base(L));
pop(L, luaopen_math(L));
@ -83,7 +86,7 @@ void lua::initialize() {
const char* removed_os[] {
"execute", "exit", "remove", "rename", "setlocale", "tmpname", nullptr};
remove_lib_funcs(L, "os", removed_os);
create_libs(L);
create_libs(L, stateType);
pushglobals(L);
setglobal(L, env_name(0));
@ -101,12 +104,19 @@ void lua::initialize() {
newusertype<LuaVoxelStructure>(L);
}
void lua::initialize() {
logger.info() << LUA_VERSION;
logger.info() << LUAJIT_VERSION;
main_thread = create_state(StateType::BASE);
}
void lua::finalize() {
lua_close(main_thread);
lua::close(main_thread);
}
bool lua::emit_event(
lua::State* L, const std::string& name, std::function<int(lua::State*)> args
State* L, const std::string& name, std::function<int(State*)> args
) {
getglobal(L, "events");
getfield(L, "emit");
@ -117,6 +127,15 @@ bool lua::emit_event(
return result;
}
lua::State* lua::get_main_thread() {
State* lua::get_main_state() {
return main_thread;
}
State* lua::create_state(StateType stateType) {
auto L = luaL_newstate();
if (L == nullptr) {
throw luaerror("could not initialize Lua state");
}
init_state(L, stateType);
return L;
}

View File

@ -8,13 +8,22 @@
#include "lua_util.hpp"
namespace lua {
enum class StateType {
BASE,
GENERATOR,
};
void initialize();
void finalize();
bool emit_event(
lua::State*,
State*,
const std::string& name,
std::function<int(lua::State*)> args = [](auto*) { return 0; }
std::function<int(State*)> args = [](auto*) { return 0; }
);
lua::State* get_main_thread();
State* get_main_state();
State* create_state(StateType stateType);
[[nodiscard]] scriptenv create_environment(State* L);
void init_state(State* L, StateType stateType);
}

View File

@ -1,6 +1,6 @@
#include <iostream>
#include "api_lua.hpp"
#include "libs/api_lua.hpp"
#include "debug/Logger.hpp"
static debug::Logger logger("lua-debug");

View File

@ -1,6 +1,6 @@
#include <iostream>
#include "api_lua.hpp"
#include "libs/api_lua.hpp"
/// @brief Modified version of luaB_print from lbaselib.c
int l_print(lua::State* L) {

View File

@ -277,7 +277,7 @@ int lua::create_environment(State* L, int parent) {
return id;
}
void lua::removeEnvironment(State* L, int id) {
void lua::remove_environment(State* L, int id) {
if (id == 0) {
return;
}

View File

@ -546,9 +546,13 @@ namespace lua {
return 0;
}
int create_environment(lua::State*, int parent);
void removeEnvironment(lua::State*, int id);
void remove_environment(lua::State*, int id);
void dump_stack(lua::State*);
inline void close(lua::State* L) {
lua_close(L);
}
inline void addfunc(
lua::State* L, const std::string& name, lua_CFunction func
) {

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) {
@ -73,19 +73,10 @@ void scripting::initialize(Engine* engine) {
return std::make_shared<int>(0);
}
[[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;
});
}
[[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 +85,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 +93,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 +112,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 +120,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 +142,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 +162,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 +208,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 +231,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 +245,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 +259,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 +277,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 +289,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 +299,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 +310,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 +326,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 +339,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 +354,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 +422,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 +452,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 +552,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 +562,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 +575,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 +587,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 +596,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 +604,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 +626,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 +636,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 +656,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 +668,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 +682,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 +701,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");

View File

@ -52,7 +52,6 @@ namespace scripting {
scriptenv get_root_environment();
scriptenv create_pack_environment(const ContentPack& pack);
scriptenv create_environment();
scriptenv create_doc_environment(
const scriptenv& parent, const std::string& name
);

View File

@ -12,7 +12,7 @@ static debug::Logger logger("scripting_func");
runnable scripting::create_runnable(
const scriptenv& env, const std::string& src, const std::string& file
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
try {
lua::loadbuffer(L, *env, src, file);
return lua::create_runnable(L);
@ -25,7 +25,7 @@ runnable scripting::create_runnable(
static lua::State* process_callback(
const scriptenv& env, const std::string& src, const std::string& file
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
try {
if (lua::eval(L, *env, src, file) != 0) {
return L;
@ -163,7 +163,7 @@ vec2supplier scripting::create_vec2_supplier(
value_to_string_func scripting::create_tostring(
const scriptenv& env, const std::string& src, const std::string& file
) {
auto L = lua::get_main_thread();
auto L = lua::get_main_state();
try {
lua::loadbuffer(L, *env, src, file);
lua::call(L, 0, 1);

View File

@ -5,7 +5,7 @@
#include "files/files.hpp"
#include "frontend/hud.hpp"
#include "objects/Player.hpp"
#include "lua/api_lua.hpp"
#include "lua/libs/api_lua.hpp"
#include "lua/lua_engine.hpp"
#include "scripting.hpp"
@ -17,11 +17,11 @@ Hud* scripting::hud = nullptr;
void scripting::on_frontend_init(Hud* hud) {
scripting::hud = hud;
lua::openlib(lua::get_main_thread(), "hud", hudlib);
lua::openlib(lua::get_main_state(), "hud", hudlib);
for (auto& pack : engine->getContentPacks()) {
lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
pack.id + ".hudopen",
[&](lua::State* L) {
return lua::pushinteger(L, hud->getPlayer()->getId());
@ -33,7 +33,7 @@ void scripting::on_frontend_init(Hud* hud) {
void scripting::on_frontend_render() {
for (auto& pack : engine->getContentPacks()) {
lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
pack.id + ".hudrender",
[&](lua::State* L) { return 0; }
);
@ -43,7 +43,7 @@ void scripting::on_frontend_render() {
void scripting::on_frontend_close() {
for (auto& pack : engine->getContentPacks()) {
lua::emit_event(
lua::get_main_thread(),
lua::get_main_state(),
pack.id + ".hudclose",
[&](lua::State* L) {
return lua::pushinteger(L, hud->getPlayer()->getId());
@ -60,7 +60,7 @@ void scripting::load_hud_script(
std::string src = files::read_string(file);
logger.info() << "loading script " << file.u8string();
lua::execute(lua::get_main_thread(), env, src, file.u8string());
lua::execute(lua::get_main_state(), env, src, file.u8string());
register_event(env, "init", packid + ".init");
register_event(env, "on_hud_open", packid + ".hudopen");

View File

@ -13,35 +13,45 @@
#include "data/dv.hpp"
#include "world/generator/GeneratorDef.hpp"
#include "util/timeutil.hpp"
#include "files/files.hpp"
#include "debug/Logger.hpp"
using namespace lua;
static debug::Logger logger("generator-scripting");
class LuaGeneratorScript : public GeneratorScript {
State* L;
const GeneratorDef& def;
scriptenv env;
public:
LuaGeneratorScript(
const GeneratorDef& def,
scriptenv env)
: def(def),
env(std::move(env))
{}
LuaGeneratorScript(State* L, const GeneratorDef& def, scriptenv env)
: L(L), def(def), env(std::move(env)) {
}
virtual ~LuaGeneratorScript() {
env.reset();
if (L != get_main_state()) {
close(L);
}
}
std::shared_ptr<Heightmap> generateHeightmap(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd
) override {
auto L = lua::get_main_thread();
lua::pushenv(L, *env);
if (lua::getfield(L, "generate_heightmap")) {
lua::pushivec_stack(L, offset);
lua::pushivec_stack(L, size);
lua::pushinteger(L, seed);
lua::pushinteger(L, bpd);
if (lua::call_nothrow(L, 6)) {
auto map = lua::touserdata<lua::LuaHeightmap>(L, -1)->getHeightmap();
lua::pop(L, 2);
pushenv(L, *env);
if (getfield(L, "generate_heightmap")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
pushinteger(L, bpd);
if (call_nothrow(L, 6)) {
auto map = touserdata<LuaHeightmap>(L, -1)->getHeightmap();
pop(L, 2);
return map;
}
}
lua::pop(L);
pop(L);
return std::make_shared<Heightmap>(size.x, size.y);
}
@ -51,23 +61,22 @@ public:
std::vector<std::shared_ptr<Heightmap>> maps;
uint biomeParameters = def.biomeParameters;
auto L = lua::get_main_thread();
lua::pushenv(L, *env);
if (lua::getfield(L, "generate_biome_parameters")) {
lua::pushivec_stack(L, offset);
lua::pushivec_stack(L, size);
lua::pushinteger(L, seed);
lua::pushinteger(L, bpd);
if (lua::call_nothrow(L, 6, biomeParameters)) {
pushenv(L, *env);
if (getfield(L, "generate_biome_parameters")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
pushinteger(L, bpd);
if (call_nothrow(L, 6, biomeParameters)) {
for (int i = biomeParameters-1; i >= 0; i--) {
maps.push_back(
lua::touserdata<lua::LuaHeightmap>(L, -1-i)->getHeightmap());
touserdata<LuaHeightmap>(L, -1-i)->getHeightmap());
}
lua::pop(L, 1+biomeParameters);
pop(L, 1+biomeParameters);
return maps;
}
}
lua::pop(L);
pop(L);
for (uint i = 0; i < biomeParameters; i++) {
maps.push_back(std::make_shared<Heightmap>(size.x, size.y));
}
@ -80,45 +89,46 @@ public:
) override {
std::vector<StructurePlacement> placements;
auto L = lua::get_main_thread();
lua::stackguard _(L);
lua::pushenv(L, *env);
if (lua::getfield(L, "place_structures")) {
lua::pushivec_stack(L, offset);
lua::pushivec_stack(L, size);
lua::pushinteger(L, seed);
lua::newuserdata<lua::LuaHeightmap>(L, heightmap);
lua::pushinteger(L, chunkHeight);
if (lua::call_nothrow(L, 7, 1)) {
int len = lua::objlen(L, -1);
stackguard _(L);
pushenv(L, *env);
if (getfield(L, "place_structures")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
newuserdata<LuaHeightmap>(L, heightmap);
pushinteger(L, chunkHeight);
if (call_nothrow(L, 7, 1)) {
int len = objlen(L, -1);
for (int i = 1; i <= len; i++) {
lua::rawgeti(L, i);
rawgeti(L, i);
lua::rawgeti(L, 1);
rawgeti(L, 1);
int structIndex = 0;
if (lua::isstring(L, -1)) {
const auto& found = def.structuresIndices.find(lua::require_string(L, -1));
if (isstring(L, -1)) {
const auto& found = def.structuresIndices.find(
require_string(L, -1)
);
if (found != def.structuresIndices.end()) {
structIndex = found->second;
}
} else {
structIndex = lua::tointeger(L, -1);
structIndex = tointeger(L, -1);
}
lua::pop(L);
pop(L);
lua::rawgeti(L, 2);
glm::ivec3 pos = lua::tovec3(L, -1);
lua::pop(L);
rawgeti(L, 2);
glm::ivec3 pos = tovec3(L, -1);
pop(L);
lua::rawgeti(L, 3);
int rotation = lua::tointeger(L, -1) & 0b11;
lua::pop(L);
rawgeti(L, 3);
int rotation = tointeger(L, -1) & 0b11;
pop(L);
lua::pop(L);
pop(L);
placements.emplace_back(structIndex, pos, rotation);
}
lua::pop(L);
pop(L);
}
}
return placements;
@ -128,27 +138,26 @@ public:
std::unique_ptr<GeneratorScript> scripting::load_generator(
const GeneratorDef& def, const fs::path& file, const std::string& dirPath
) {
auto env = create_environment();
auto L = lua::get_main_thread();
lua::stackguard _(L);
auto L = create_state(StateType::GENERATOR);
auto env = create_environment(L);
stackguard _(L);
lua::pushenv(L, *env);
lua::pushstring(L, dirPath);
lua::setfield(L, "__DIR__");
lua::pushstring(L, dirPath + "/script.lua");
lua::setfield(L, "__FILE__");
pushenv(L, *env);
pushstring(L, dirPath);
setfield(L, "__DIR__");
pushstring(L, dirPath + "/script.lua");
setfield(L, "__FILE__");
lua::pop(L);
pop(L);
if (fs::exists(file)) {
lua::pop(L, load_script(*env, "generator", file));
std::string src = files::read_string(file);
logger.info() << "script (generator) " << file.u8string();
pop(L, execute(L, *env, src, file.u8string()));
} else {
// Use default (empty) script
lua::pop(L, lua::execute(lua::get_main_thread(), *env, "", "<empty>"));
pop(L, execute(L, *env, "", "<empty>"));
}
return std::make_unique<LuaGeneratorScript>(
def,
std::move(env)
);
return std::make_unique<LuaGeneratorScript>(L, def, std::move(env));
}