Merge pull request #306 from MihailRis/isolated-lua-states
Add isolated lua states
This commit is contained in:
@@ -4,7 +4,7 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches: [ "main" ]
|
branches: [ "main" ]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "main" ]
|
branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches: [ "main", "release-**"]
|
branches: [ "main", "release-**"]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "main" ]
|
branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-dmg:
|
build-dmg:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches: [ "main" ]
|
branches: [ "main" ]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "main" ]
|
branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-windows:
|
build-windows:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "lua_util.hpp"
|
#include "../lua_util.hpp"
|
||||||
|
|
||||||
/// Definitions can be found in local .cpp files
|
/// Definitions can be found in local .cpp files
|
||||||
/// having same names as declarations
|
/// having same names as declarations
|
||||||
+1
-1
@@ -7,7 +7,7 @@
|
|||||||
#include "world/generator/VoxelFragment.hpp"
|
#include "world/generator/VoxelFragment.hpp"
|
||||||
#include "content/ContentLoader.hpp"
|
#include "content/ContentLoader.hpp"
|
||||||
#include "engine.hpp"
|
#include "engine.hpp"
|
||||||
#include "lua_custom_types.hpp"
|
#include "../lua_custom_types.hpp"
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "api_lua.hpp"
|
#include "libs/api_lua.hpp"
|
||||||
#include "lua_custom_types.hpp"
|
#include "lua_custom_types.hpp"
|
||||||
|
|
||||||
static debug::Logger logger("lua-state");
|
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(
|
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)) {
|
if (getglobal(L, libname)) {
|
||||||
for (uint i = 0; funcs[i]; i++) {
|
for (uint i = 0; funcs[i]; i++) {
|
||||||
@@ -28,49 +28,52 @@ static void remove_lib_funcs(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void create_libs(lua::State* L) {
|
[[nodiscard]] scriptenv lua::create_environment(State* L) {
|
||||||
openlib(L, "audio", audiolib);
|
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, "block", blocklib);
|
||||||
openlib(L, "console", consolelib);
|
|
||||||
openlib(L, "core", corelib);
|
openlib(L, "core", corelib);
|
||||||
openlib(L, "file", filelib);
|
openlib(L, "file", filelib);
|
||||||
openlib(L, "gui", guilib);
|
|
||||||
openlib(L, "input", inputlib);
|
|
||||||
openlib(L, "inventory", inventorylib);
|
|
||||||
openlib(L, "generation", generationlib);
|
openlib(L, "generation", generationlib);
|
||||||
openlib(L, "item", itemlib);
|
openlib(L, "item", itemlib);
|
||||||
openlib(L, "json", jsonlib);
|
openlib(L, "json", jsonlib);
|
||||||
openlib(L, "mat4", mat4lib);
|
openlib(L, "mat4", mat4lib);
|
||||||
openlib(L, "pack", packlib);
|
openlib(L, "pack", packlib);
|
||||||
openlib(L, "player", playerlib);
|
|
||||||
openlib(L, "quat", quatlib);
|
openlib(L, "quat", quatlib);
|
||||||
openlib(L, "time", timelib);
|
openlib(L, "time", timelib);
|
||||||
openlib(L, "toml", tomllib);
|
openlib(L, "toml", tomllib);
|
||||||
openlib(L, "vec2", vec2lib);
|
openlib(L, "vec2", vec2lib);
|
||||||
openlib(L, "vec3", vec3lib);
|
openlib(L, "vec3", vec3lib);
|
||||||
openlib(L, "vec4", vec4lib);
|
openlib(L, "vec4", vec4lib);
|
||||||
openlib(L, "world", worldlib);
|
|
||||||
|
|
||||||
openlib(L, "entities", entitylib);
|
if (stateType == StateType::BASE) {
|
||||||
openlib(L, "cameras", cameralib);
|
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, "entities", entitylib);
|
||||||
openlib(L, "__skeleton", skeletonlib);
|
openlib(L, "cameras", cameralib);
|
||||||
openlib(L, "__rigidbody", rigidbodylib);
|
|
||||||
openlib(L, "__transform", transformlib);
|
// components
|
||||||
|
openlib(L, "__skeleton", skeletonlib);
|
||||||
|
openlib(L, "__rigidbody", rigidbodylib);
|
||||||
|
openlib(L, "__transform", transformlib);
|
||||||
|
}
|
||||||
|
|
||||||
addfunc(L, "print", lua::wrap<l_print>);
|
addfunc(L, "print", lua::wrap<l_print>);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua::initialize() {
|
void lua::init_state(State* L, StateType stateType) {
|
||||||
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;
|
|
||||||
// Allowed standard libraries
|
// Allowed standard libraries
|
||||||
pop(L, luaopen_base(L));
|
pop(L, luaopen_base(L));
|
||||||
pop(L, luaopen_math(L));
|
pop(L, luaopen_math(L));
|
||||||
@@ -83,7 +86,7 @@ void lua::initialize() {
|
|||||||
const char* removed_os[] {
|
const char* removed_os[] {
|
||||||
"execute", "exit", "remove", "rename", "setlocale", "tmpname", nullptr};
|
"execute", "exit", "remove", "rename", "setlocale", "tmpname", nullptr};
|
||||||
remove_lib_funcs(L, "os", removed_os);
|
remove_lib_funcs(L, "os", removed_os);
|
||||||
create_libs(L);
|
create_libs(L, stateType);
|
||||||
|
|
||||||
pushglobals(L);
|
pushglobals(L);
|
||||||
setglobal(L, env_name(0));
|
setglobal(L, env_name(0));
|
||||||
@@ -101,12 +104,19 @@ void lua::initialize() {
|
|||||||
newusertype<LuaVoxelStructure>(L);
|
newusertype<LuaVoxelStructure>(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lua::initialize() {
|
||||||
|
logger.info() << LUA_VERSION;
|
||||||
|
logger.info() << LUAJIT_VERSION;
|
||||||
|
|
||||||
|
main_thread = create_state(StateType::BASE);
|
||||||
|
}
|
||||||
|
|
||||||
void lua::finalize() {
|
void lua::finalize() {
|
||||||
lua_close(main_thread);
|
lua::close(main_thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool lua::emit_event(
|
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");
|
getglobal(L, "events");
|
||||||
getfield(L, "emit");
|
getfield(L, "emit");
|
||||||
@@ -117,6 +127,15 @@ bool lua::emit_event(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::State* lua::get_main_thread() {
|
State* lua::get_main_state() {
|
||||||
return main_thread;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,13 +8,22 @@
|
|||||||
#include "lua_util.hpp"
|
#include "lua_util.hpp"
|
||||||
|
|
||||||
namespace lua {
|
namespace lua {
|
||||||
|
enum class StateType {
|
||||||
|
BASE,
|
||||||
|
GENERATOR,
|
||||||
|
};
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
void finalize();
|
void finalize();
|
||||||
|
|
||||||
bool emit_event(
|
bool emit_event(
|
||||||
lua::State*,
|
State*,
|
||||||
const std::string& name,
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "api_lua.hpp"
|
#include "libs/api_lua.hpp"
|
||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
|
|
||||||
static debug::Logger logger("lua-debug");
|
static debug::Logger logger("lua-debug");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "api_lua.hpp"
|
#include "libs/api_lua.hpp"
|
||||||
|
|
||||||
/// @brief Modified version of luaB_print from lbaselib.c
|
/// @brief Modified version of luaB_print from lbaselib.c
|
||||||
int l_print(lua::State* L) {
|
int l_print(lua::State* L) {
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ int lua::create_environment(State* L, int parent) {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua::removeEnvironment(State* L, int id) {
|
void lua::remove_environment(State* L, int id) {
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -546,9 +546,13 @@ namespace lua {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int create_environment(lua::State*, int parent);
|
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*);
|
void dump_stack(lua::State*);
|
||||||
|
|
||||||
|
inline void close(lua::State* L) {
|
||||||
|
lua_close(L);
|
||||||
|
}
|
||||||
|
|
||||||
inline void addfunc(
|
inline void addfunc(
|
||||||
lua::State* L, const std::string& name, lua_CFunction func
|
lua::State* L, const std::string& name, lua_CFunction func
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ void scripting::load_script(const fs::path& name, bool throwable) {
|
|||||||
auto paths = scripting::engine->getPaths();
|
auto paths = scripting::engine->getPaths();
|
||||||
fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name;
|
fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name;
|
||||||
std::string src = files::read_string(file);
|
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());
|
lua::loadbuffer(L, 0, src, file.u8string());
|
||||||
if (throwable) {
|
if (throwable) {
|
||||||
lua::call(L, 0, 0);
|
lua::call(L, 0, 0);
|
||||||
@@ -57,7 +57,7 @@ int scripting::load_script(
|
|||||||
) {
|
) {
|
||||||
std::string src = files::read_string(file);
|
std::string src = files::read_string(file);
|
||||||
logger.info() << "script (" << type << ") " << file.u8string();
|
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) {
|
void scripting::initialize(Engine* engine) {
|
||||||
@@ -73,19 +73,10 @@ void scripting::initialize(Engine* engine) {
|
|||||||
return std::make_shared<int>(0);
|
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(
|
[[nodiscard]] scriptenv scripting::create_pack_environment(
|
||||||
const ContentPack& pack
|
const ContentPack& pack
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
int id = lua::create_environment(L, 0);
|
int id = lua::create_environment(L, 0);
|
||||||
lua::pushenv(L, id);
|
lua::pushenv(L, id);
|
||||||
lua::pushvalue(L, -1);
|
lua::pushvalue(L, -1);
|
||||||
@@ -94,7 +85,7 @@ void scripting::initialize(Engine* engine) {
|
|||||||
lua::setfield(L, "PACK_ID");
|
lua::setfield(L, "PACK_ID");
|
||||||
lua::pop(L);
|
lua::pop(L);
|
||||||
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
||||||
lua::removeEnvironment(L, *id);
|
lua::remove_environment(L, *id);
|
||||||
delete id;
|
delete id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -102,7 +93,7 @@ void scripting::initialize(Engine* engine) {
|
|||||||
[[nodiscard]] scriptenv scripting::create_doc_environment(
|
[[nodiscard]] scriptenv scripting::create_doc_environment(
|
||||||
const scriptenv& parent, const std::string& name
|
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);
|
int id = lua::create_environment(L, *parent);
|
||||||
lua::pushenv(L, id);
|
lua::pushenv(L, id);
|
||||||
lua::pushvalue(L, -1);
|
lua::pushvalue(L, -1);
|
||||||
@@ -121,7 +112,7 @@ void scripting::initialize(Engine* engine) {
|
|||||||
}
|
}
|
||||||
lua::pop(L);
|
lua::pop(L);
|
||||||
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
||||||
lua::removeEnvironment(L, *id);
|
lua::remove_environment(L, *id);
|
||||||
delete id;
|
delete id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -129,7 +120,7 @@ void scripting::initialize(Engine* engine) {
|
|||||||
[[nodiscard]] static scriptenv create_component_environment(
|
[[nodiscard]] static scriptenv create_component_environment(
|
||||||
const scriptenv& parent, int entityIdx, const std::string& name
|
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);
|
int id = lua::create_environment(L, *parent);
|
||||||
|
|
||||||
lua::pushvalue(L, entityIdx);
|
lua::pushvalue(L, entityIdx);
|
||||||
@@ -151,13 +142,13 @@ void scripting::initialize(Engine* engine) {
|
|||||||
lua::pop(L);
|
lua::pop(L);
|
||||||
|
|
||||||
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
||||||
lua::removeEnvironment(L, *id);
|
lua::remove_environment(L, *id);
|
||||||
delete id;
|
delete id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::process_post_runnables() {
|
void scripting::process_post_runnables() {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
if (lua::getglobal(L, "__process_post_runnables")) {
|
if (lua::getglobal(L, "__process_post_runnables")) {
|
||||||
lua::call_nothrow(L, 0);
|
lua::call_nothrow(L, 0);
|
||||||
}
|
}
|
||||||
@@ -171,28 +162,28 @@ void scripting::on_world_load(LevelController* controller) {
|
|||||||
scripting::controller = controller;
|
scripting::controller = controller;
|
||||||
load_script("world.lua", false);
|
load_script("world.lua", false);
|
||||||
|
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldopen");
|
lua::emit_event(L, pack.id + ".worldopen");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_tick() {
|
void scripting::on_world_tick() {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldtick");
|
lua::emit_event(L, pack.id + ".worldtick");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_save() {
|
void scripting::on_world_save() {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldsave");
|
lua::emit_event(L, pack.id + ".worldsave");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_quit() {
|
void scripting::on_world_quit() {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
lua::emit_event(L, pack.id + ".worldquit");
|
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) {
|
void scripting::on_blocks_tick(const Block& block, int tps) {
|
||||||
std::string name = block.name + ".blockstick";
|
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);
|
return lua::pushinteger(L, tps);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::update_block(const Block& block, int x, int y, int z) {
|
void scripting::update_block(const Block& block, int x, int y, int z) {
|
||||||
std::string name = block.name + ".update";
|
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));
|
return lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::random_update_block(const Block& block, int x, int y, int z) {
|
void scripting::random_update_block(const Block& block, int x, int y, int z) {
|
||||||
std::string name = block.name + ".randupdate";
|
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));
|
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
|
Player* player, const Block& block, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
std::string name = block.name + ".placed";
|
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::pushivec_stack(L, glm::ivec3(x, y, z));
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
return 4;
|
return 4;
|
||||||
@@ -254,7 +245,7 @@ void scripting::on_block_placed(
|
|||||||
for (auto& [packid, pack] : content->getPacks()) {
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
if (pack->worldfuncsset.onblockplaced) {
|
if (pack->worldfuncsset.onblockplaced) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
packid + ".blockplaced",
|
packid + ".blockplaced",
|
||||||
world_event_args
|
world_event_args
|
||||||
);
|
);
|
||||||
@@ -268,7 +259,7 @@ void scripting::on_block_broken(
|
|||||||
if (block.rt.funcsset.onbroken) {
|
if (block.rt.funcsset.onbroken) {
|
||||||
std::string name = block.name + ".broken";
|
std::string name = block.name + ".broken";
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
name,
|
name,
|
||||||
[x, y, z, player](auto L) {
|
[x, y, z, player](auto L) {
|
||||||
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
||||||
@@ -286,7 +277,7 @@ void scripting::on_block_broken(
|
|||||||
for (auto& [packid, pack] : content->getPacks()) {
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
if (pack->worldfuncsset.onblockbroken) {
|
if (pack->worldfuncsset.onblockbroken) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
packid + ".blockbroken",
|
packid + ".blockbroken",
|
||||||
world_event_args
|
world_event_args
|
||||||
);
|
);
|
||||||
@@ -298,7 +289,7 @@ bool scripting::on_block_interact(
|
|||||||
Player* player, const Block& block, glm::ivec3 pos
|
Player* player, const Block& block, glm::ivec3 pos
|
||||||
) {
|
) {
|
||||||
std::string name = block.name + ".interact";
|
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::pushivec_stack(L, pos);
|
||||||
lua::pushinteger(L, player->getId());
|
lua::pushinteger(L, player->getId());
|
||||||
return 4;
|
return 4;
|
||||||
@@ -308,7 +299,7 @@ bool scripting::on_block_interact(
|
|||||||
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
||||||
std::string name = item.name + ".use";
|
std::string name = item.name + ".use";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
name,
|
name,
|
||||||
[player](lua::State* L) { return lua::pushinteger(L, player->getId()); }
|
[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";
|
std::string name = item.name + ".useon";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
name,
|
name,
|
||||||
[ipos, normal, player](auto L) {
|
[ipos, normal, player](auto L) {
|
||||||
lua::pushivec_stack(L, ipos);
|
lua::pushivec_stack(L, ipos);
|
||||||
@@ -335,7 +326,7 @@ bool scripting::on_item_break_block(
|
|||||||
) {
|
) {
|
||||||
std::string name = item.name + ".blockbreakby";
|
std::string name = item.name + ".blockbreakby";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
name,
|
name,
|
||||||
[x, y, z, player](auto L) {
|
[x, y, z, player](auto L) {
|
||||||
lua::pushivec_stack(L, glm::ivec3(x, y, z));
|
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(
|
dv::value scripting::get_component_value(
|
||||||
const scriptenv& env, const std::string& name
|
const scriptenv& env, const std::string& name
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
lua::pushenv(L, *env);
|
lua::pushenv(L, *env);
|
||||||
if (lua::getfield(L, name)) {
|
if (lua::getfield(L, name)) {
|
||||||
return lua::tovalue(L, -1);
|
return lua::tovalue(L, -1);
|
||||||
@@ -363,7 +354,7 @@ void scripting::on_entity_spawn(
|
|||||||
const dv::value& args,
|
const dv::value& args,
|
||||||
const dv::value& saved
|
const dv::value& saved
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
lua::requireglobal(L, STDCOMP);
|
lua::requireglobal(L, STDCOMP);
|
||||||
if (lua::getfield(L, "new_Entity")) {
|
if (lua::getfield(L, "new_Entity")) {
|
||||||
lua::pushinteger(L, eid);
|
lua::pushinteger(L, eid);
|
||||||
@@ -431,7 +422,7 @@ static void process_entity_callback(
|
|||||||
const std::string& name,
|
const std::string& name,
|
||||||
std::function<int(lua::State*)> args
|
std::function<int(lua::State*)> args
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
lua::pushenv(L, *env);
|
lua::pushenv(L, *env);
|
||||||
if (lua::getfield(L, name)) {
|
if (lua::getfield(L, name)) {
|
||||||
if (args) {
|
if (args) {
|
||||||
@@ -461,7 +452,7 @@ void scripting::on_entity_despawn(const Entity& entity) {
|
|||||||
process_entity_callback(
|
process_entity_callback(
|
||||||
entity, "on_despawn", &entity_funcs_set::on_despawn, nullptr
|
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::get_from(L, "stdcomp", "remove_Entity", true);
|
||||||
lua::pushinteger(L, entity.getUID());
|
lua::pushinteger(L, entity.getUID());
|
||||||
lua::call(L, 1, 0);
|
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) {
|
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::get_from(L, STDCOMP, "update", true);
|
||||||
lua::pushinteger(L, tps);
|
lua::pushinteger(L, tps);
|
||||||
lua::pushinteger(L, parts);
|
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) {
|
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::get_from(L, STDCOMP, "render", true);
|
||||||
lua::pushnumber(L, delta);
|
lua::pushnumber(L, delta);
|
||||||
lua::call_nothrow(L, 1, 0);
|
lua::call_nothrow(L, 1, 0);
|
||||||
@@ -584,7 +575,7 @@ void scripting::on_ui_open(
|
|||||||
auto argsptr =
|
auto argsptr =
|
||||||
std::make_shared<std::vector<dv::value>>(std::move(args));
|
std::make_shared<std::vector<dv::value>>(std::move(args));
|
||||||
std::string name = layout->getId() + ".open";
|
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) {
|
for (const auto& value : *argsptr) {
|
||||||
lua::pushvalue(L, value);
|
lua::pushvalue(L, value);
|
||||||
}
|
}
|
||||||
@@ -596,7 +587,7 @@ void scripting::on_ui_progress(
|
|||||||
UiDocument* layout, int workDone, int workTotal
|
UiDocument* layout, int workDone, int workTotal
|
||||||
) {
|
) {
|
||||||
std::string name = layout->getId() + ".progress";
|
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, workDone);
|
||||||
lua::pushinteger(L, workTotal);
|
lua::pushinteger(L, workTotal);
|
||||||
return 2;
|
return 2;
|
||||||
@@ -605,7 +596,7 @@ void scripting::on_ui_progress(
|
|||||||
|
|
||||||
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
|
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
|
||||||
std::string name = layout->getId() + ".close";
|
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);
|
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(
|
bool scripting::register_event(
|
||||||
int env, const std::string& name, const std::string& id
|
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) {
|
if (lua::pushenv(L, env) == 0) {
|
||||||
lua::pushglobals(L);
|
lua::pushglobals(L);
|
||||||
}
|
}
|
||||||
@@ -635,7 +626,7 @@ bool scripting::register_event(
|
|||||||
}
|
}
|
||||||
|
|
||||||
int scripting::get_values_on_stack() {
|
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(
|
void scripting::load_block_script(
|
||||||
@@ -645,7 +636,7 @@ void scripting::load_block_script(
|
|||||||
block_funcs_set& funcsset
|
block_funcs_set& funcsset
|
||||||
) {
|
) {
|
||||||
int env = *senv;
|
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.init = register_event(env, "init", prefix + ".init");
|
||||||
funcsset.update = register_event(env, "on_update", prefix + ".update");
|
funcsset.update = register_event(env, "on_update", prefix + ".update");
|
||||||
funcsset.randupdate =
|
funcsset.randupdate =
|
||||||
@@ -665,7 +656,7 @@ void scripting::load_item_script(
|
|||||||
item_funcs_set& funcsset
|
item_funcs_set& funcsset
|
||||||
) {
|
) {
|
||||||
int env = *senv;
|
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.init = register_event(env, "init", prefix + ".init");
|
||||||
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
|
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
|
||||||
funcsset.on_use_on_block =
|
funcsset.on_use_on_block =
|
||||||
@@ -677,7 +668,7 @@ void scripting::load_item_script(
|
|||||||
void scripting::load_entity_component(
|
void scripting::load_entity_component(
|
||||||
const std::string& name, const fs::path& file
|
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);
|
std::string src = files::read_string(file);
|
||||||
logger.info() << "script (component) " << file.u8string();
|
logger.info() << "script (component) " << file.u8string();
|
||||||
lua::loadbuffer(L, 0, src, "C!" + name);
|
lua::loadbuffer(L, 0, src, "C!" + name);
|
||||||
@@ -691,7 +682,7 @@ void scripting::load_world_script(
|
|||||||
world_funcs_set& funcsset
|
world_funcs_set& funcsset
|
||||||
) {
|
) {
|
||||||
int env = *senv;
|
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, "init", prefix + ".init");
|
||||||
register_event(env, "on_world_open", prefix + ".worldopen");
|
register_event(env, "on_world_open", prefix + ".worldopen");
|
||||||
register_event(env, "on_world_tick", prefix + ".worldtick");
|
register_event(env, "on_world_tick", prefix + ".worldtick");
|
||||||
@@ -710,7 +701,7 @@ void scripting::load_layout_script(
|
|||||||
uidocscript& script
|
uidocscript& script
|
||||||
) {
|
) {
|
||||||
int env = *senv;
|
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.onopen = register_event(env, "on_open", prefix + ".open");
|
||||||
script.onprogress =
|
script.onprogress =
|
||||||
register_event(env, "on_progress", prefix + ".progress");
|
register_event(env, "on_progress", prefix + ".progress");
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ namespace scripting {
|
|||||||
|
|
||||||
scriptenv get_root_environment();
|
scriptenv get_root_environment();
|
||||||
scriptenv create_pack_environment(const ContentPack& pack);
|
scriptenv create_pack_environment(const ContentPack& pack);
|
||||||
scriptenv create_environment();
|
|
||||||
scriptenv create_doc_environment(
|
scriptenv create_doc_environment(
|
||||||
const scriptenv& parent, const std::string& name
|
const scriptenv& parent, const std::string& name
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ static debug::Logger logger("scripting_func");
|
|||||||
runnable scripting::create_runnable(
|
runnable scripting::create_runnable(
|
||||||
const scriptenv& env, const std::string& src, const std::string& file
|
const scriptenv& env, const std::string& src, const std::string& file
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
try {
|
try {
|
||||||
lua::loadbuffer(L, *env, src, file);
|
lua::loadbuffer(L, *env, src, file);
|
||||||
return lua::create_runnable(L);
|
return lua::create_runnable(L);
|
||||||
@@ -25,7 +25,7 @@ runnable scripting::create_runnable(
|
|||||||
static lua::State* process_callback(
|
static lua::State* process_callback(
|
||||||
const scriptenv& env, const std::string& src, const std::string& file
|
const scriptenv& env, const std::string& src, const std::string& file
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
try {
|
try {
|
||||||
if (lua::eval(L, *env, src, file) != 0) {
|
if (lua::eval(L, *env, src, file) != 0) {
|
||||||
return L;
|
return L;
|
||||||
@@ -163,7 +163,7 @@ vec2supplier scripting::create_vec2_supplier(
|
|||||||
value_to_string_func scripting::create_tostring(
|
value_to_string_func scripting::create_tostring(
|
||||||
const scriptenv& env, const std::string& src, const std::string& file
|
const scriptenv& env, const std::string& src, const std::string& file
|
||||||
) {
|
) {
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_state();
|
||||||
try {
|
try {
|
||||||
lua::loadbuffer(L, *env, src, file);
|
lua::loadbuffer(L, *env, src, file);
|
||||||
lua::call(L, 0, 1);
|
lua::call(L, 0, 1);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "files/files.hpp"
|
#include "files/files.hpp"
|
||||||
#include "frontend/hud.hpp"
|
#include "frontend/hud.hpp"
|
||||||
#include "objects/Player.hpp"
|
#include "objects/Player.hpp"
|
||||||
#include "lua/api_lua.hpp"
|
#include "lua/libs/api_lua.hpp"
|
||||||
#include "lua/lua_engine.hpp"
|
#include "lua/lua_engine.hpp"
|
||||||
#include "scripting.hpp"
|
#include "scripting.hpp"
|
||||||
|
|
||||||
@@ -17,11 +17,11 @@ Hud* scripting::hud = nullptr;
|
|||||||
|
|
||||||
void scripting::on_frontend_init(Hud* hud) {
|
void scripting::on_frontend_init(Hud* hud) {
|
||||||
scripting::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()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
pack.id + ".hudopen",
|
pack.id + ".hudopen",
|
||||||
[&](lua::State* L) {
|
[&](lua::State* L) {
|
||||||
return lua::pushinteger(L, hud->getPlayer()->getId());
|
return lua::pushinteger(L, hud->getPlayer()->getId());
|
||||||
@@ -33,7 +33,7 @@ void scripting::on_frontend_init(Hud* hud) {
|
|||||||
void scripting::on_frontend_render() {
|
void scripting::on_frontend_render() {
|
||||||
for (auto& pack : engine->getContentPacks()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
pack.id + ".hudrender",
|
pack.id + ".hudrender",
|
||||||
[&](lua::State* L) { return 0; }
|
[&](lua::State* L) { return 0; }
|
||||||
);
|
);
|
||||||
@@ -43,7 +43,7 @@ void scripting::on_frontend_render() {
|
|||||||
void scripting::on_frontend_close() {
|
void scripting::on_frontend_close() {
|
||||||
for (auto& pack : engine->getContentPacks()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_state(),
|
||||||
pack.id + ".hudclose",
|
pack.id + ".hudclose",
|
||||||
[&](lua::State* L) {
|
[&](lua::State* L) {
|
||||||
return lua::pushinteger(L, hud->getPlayer()->getId());
|
return lua::pushinteger(L, hud->getPlayer()->getId());
|
||||||
@@ -60,7 +60,7 @@ void scripting::load_hud_script(
|
|||||||
std::string src = files::read_string(file);
|
std::string src = files::read_string(file);
|
||||||
logger.info() << "loading script " << file.u8string();
|
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, "init", packid + ".init");
|
||||||
register_event(env, "on_hud_open", packid + ".hudopen");
|
register_event(env, "on_hud_open", packid + ".hudopen");
|
||||||
|
|||||||
@@ -13,35 +13,45 @@
|
|||||||
#include "data/dv.hpp"
|
#include "data/dv.hpp"
|
||||||
#include "world/generator/GeneratorDef.hpp"
|
#include "world/generator/GeneratorDef.hpp"
|
||||||
#include "util/timeutil.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 {
|
class LuaGeneratorScript : public GeneratorScript {
|
||||||
|
State* L;
|
||||||
const GeneratorDef& def;
|
const GeneratorDef& def;
|
||||||
scriptenv env;
|
scriptenv env;
|
||||||
public:
|
public:
|
||||||
LuaGeneratorScript(
|
LuaGeneratorScript(State* L, const GeneratorDef& def, scriptenv env)
|
||||||
const GeneratorDef& def,
|
: L(L), def(def), env(std::move(env)) {
|
||||||
scriptenv env)
|
}
|
||||||
: def(def),
|
|
||||||
env(std::move(env))
|
virtual ~LuaGeneratorScript() {
|
||||||
{}
|
env.reset();
|
||||||
|
if (L != get_main_state()) {
|
||||||
|
close(L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<Heightmap> generateHeightmap(
|
std::shared_ptr<Heightmap> generateHeightmap(
|
||||||
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd
|
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd
|
||||||
) override {
|
) override {
|
||||||
auto L = lua::get_main_thread();
|
pushenv(L, *env);
|
||||||
lua::pushenv(L, *env);
|
if (getfield(L, "generate_heightmap")) {
|
||||||
if (lua::getfield(L, "generate_heightmap")) {
|
pushivec_stack(L, offset);
|
||||||
lua::pushivec_stack(L, offset);
|
pushivec_stack(L, size);
|
||||||
lua::pushivec_stack(L, size);
|
pushinteger(L, seed);
|
||||||
lua::pushinteger(L, seed);
|
pushinteger(L, bpd);
|
||||||
lua::pushinteger(L, bpd);
|
if (call_nothrow(L, 6)) {
|
||||||
if (lua::call_nothrow(L, 6)) {
|
auto map = touserdata<LuaHeightmap>(L, -1)->getHeightmap();
|
||||||
auto map = lua::touserdata<lua::LuaHeightmap>(L, -1)->getHeightmap();
|
pop(L, 2);
|
||||||
lua::pop(L, 2);
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
return std::make_shared<Heightmap>(size.x, size.y);
|
return std::make_shared<Heightmap>(size.x, size.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,23 +61,22 @@ public:
|
|||||||
std::vector<std::shared_ptr<Heightmap>> maps;
|
std::vector<std::shared_ptr<Heightmap>> maps;
|
||||||
|
|
||||||
uint biomeParameters = def.biomeParameters;
|
uint biomeParameters = def.biomeParameters;
|
||||||
auto L = lua::get_main_thread();
|
pushenv(L, *env);
|
||||||
lua::pushenv(L, *env);
|
if (getfield(L, "generate_biome_parameters")) {
|
||||||
if (lua::getfield(L, "generate_biome_parameters")) {
|
pushivec_stack(L, offset);
|
||||||
lua::pushivec_stack(L, offset);
|
pushivec_stack(L, size);
|
||||||
lua::pushivec_stack(L, size);
|
pushinteger(L, seed);
|
||||||
lua::pushinteger(L, seed);
|
pushinteger(L, bpd);
|
||||||
lua::pushinteger(L, bpd);
|
if (call_nothrow(L, 6, biomeParameters)) {
|
||||||
if (lua::call_nothrow(L, 6, biomeParameters)) {
|
|
||||||
for (int i = biomeParameters-1; i >= 0; i--) {
|
for (int i = biomeParameters-1; i >= 0; i--) {
|
||||||
maps.push_back(
|
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;
|
return maps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
for (uint i = 0; i < biomeParameters; i++) {
|
for (uint i = 0; i < biomeParameters; i++) {
|
||||||
maps.push_back(std::make_shared<Heightmap>(size.x, size.y));
|
maps.push_back(std::make_shared<Heightmap>(size.x, size.y));
|
||||||
}
|
}
|
||||||
@@ -80,45 +89,46 @@ public:
|
|||||||
) override {
|
) override {
|
||||||
std::vector<StructurePlacement> placements;
|
std::vector<StructurePlacement> placements;
|
||||||
|
|
||||||
auto L = lua::get_main_thread();
|
stackguard _(L);
|
||||||
lua::stackguard _(L);
|
pushenv(L, *env);
|
||||||
lua::pushenv(L, *env);
|
if (getfield(L, "place_structures")) {
|
||||||
if (lua::getfield(L, "place_structures")) {
|
pushivec_stack(L, offset);
|
||||||
lua::pushivec_stack(L, offset);
|
pushivec_stack(L, size);
|
||||||
lua::pushivec_stack(L, size);
|
pushinteger(L, seed);
|
||||||
lua::pushinteger(L, seed);
|
newuserdata<LuaHeightmap>(L, heightmap);
|
||||||
lua::newuserdata<lua::LuaHeightmap>(L, heightmap);
|
pushinteger(L, chunkHeight);
|
||||||
lua::pushinteger(L, chunkHeight);
|
if (call_nothrow(L, 7, 1)) {
|
||||||
if (lua::call_nothrow(L, 7, 1)) {
|
int len = objlen(L, -1);
|
||||||
int len = lua::objlen(L, -1);
|
|
||||||
for (int i = 1; i <= len; i++) {
|
for (int i = 1; i <= len; i++) {
|
||||||
lua::rawgeti(L, i);
|
rawgeti(L, i);
|
||||||
|
|
||||||
lua::rawgeti(L, 1);
|
rawgeti(L, 1);
|
||||||
int structIndex = 0;
|
int structIndex = 0;
|
||||||
if (lua::isstring(L, -1)) {
|
if (isstring(L, -1)) {
|
||||||
const auto& found = def.structuresIndices.find(lua::require_string(L, -1));
|
const auto& found = def.structuresIndices.find(
|
||||||
|
require_string(L, -1)
|
||||||
|
);
|
||||||
if (found != def.structuresIndices.end()) {
|
if (found != def.structuresIndices.end()) {
|
||||||
structIndex = found->second;
|
structIndex = found->second;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
structIndex = lua::tointeger(L, -1);
|
structIndex = tointeger(L, -1);
|
||||||
}
|
}
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
|
|
||||||
lua::rawgeti(L, 2);
|
rawgeti(L, 2);
|
||||||
glm::ivec3 pos = lua::tovec3(L, -1);
|
glm::ivec3 pos = tovec3(L, -1);
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
|
|
||||||
lua::rawgeti(L, 3);
|
rawgeti(L, 3);
|
||||||
int rotation = lua::tointeger(L, -1) & 0b11;
|
int rotation = tointeger(L, -1) & 0b11;
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
|
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
|
|
||||||
placements.emplace_back(structIndex, pos, rotation);
|
placements.emplace_back(structIndex, pos, rotation);
|
||||||
}
|
}
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return placements;
|
return placements;
|
||||||
@@ -128,27 +138,26 @@ public:
|
|||||||
std::unique_ptr<GeneratorScript> scripting::load_generator(
|
std::unique_ptr<GeneratorScript> scripting::load_generator(
|
||||||
const GeneratorDef& def, const fs::path& file, const std::string& dirPath
|
const GeneratorDef& def, const fs::path& file, const std::string& dirPath
|
||||||
) {
|
) {
|
||||||
auto env = create_environment();
|
auto L = create_state(StateType::GENERATOR);
|
||||||
auto L = lua::get_main_thread();
|
auto env = create_environment(L);
|
||||||
lua::stackguard _(L);
|
stackguard _(L);
|
||||||
|
|
||||||
lua::pushenv(L, *env);
|
pushenv(L, *env);
|
||||||
lua::pushstring(L, dirPath);
|
pushstring(L, dirPath);
|
||||||
lua::setfield(L, "__DIR__");
|
setfield(L, "__DIR__");
|
||||||
lua::pushstring(L, dirPath + "/script.lua");
|
pushstring(L, dirPath + "/script.lua");
|
||||||
lua::setfield(L, "__FILE__");
|
setfield(L, "__FILE__");
|
||||||
|
|
||||||
lua::pop(L);
|
pop(L);
|
||||||
|
|
||||||
if (fs::exists(file)) {
|
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 {
|
} else {
|
||||||
// Use default (empty) script
|
// 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>(
|
return std::make_unique<LuaGeneratorScript>(L, def, std::move(env));
|
||||||
def,
|
|
||||||
std::move(env)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user