update voxel fragments lua api & replace structure.save command with fragment.save & add 'export' entry point

This commit is contained in:
MihailRis
2024-10-14 19:39:58 +03:00
parent cc6891dde8
commit fd2bd15658
9 changed files with 117 additions and 40 deletions
+27 -20
View File
@@ -11,24 +11,30 @@
using namespace scripting;
static int l_save_structure(lua::State* L) {
auto pointA = lua::tovec<3>(L, 1);
auto pointB = lua::tovec<3>(L, 2);
auto filename = lua::require_string(L, 3);
if (!files::is_valid_name(filename)) {
throw std::runtime_error("invalid file name");
}
bool saveEntities = lua::toboolean(L, 4);
auto structure = VoxelFragment::create(level, pointA, pointB, saveEntities);
auto map = structure->serialize();
auto bytes = json::to_binary(map);
files::write_bytes(fs::u8path(filename), bytes.data(), bytes.size());
static int l_save_fragment(lua::State* L) {
auto paths = engine->getPaths();
auto fragment = lua::touserdata<lua::LuaVoxelFragment>(L, 1);
auto file = paths->resolve(lua::require_string(L, 2), true);
auto map = fragment->getFragment()->serialize();
auto bytes = json::to_binary(map, true);
files::write_bytes(file, bytes.data(), bytes.size());
return 0;
}
static int l_load_structure(lua::State* L) {
static int l_create_fragment(lua::State* L) {
auto pointA = lua::tovec<3>(L, 1);
auto pointB = lua::tovec<3>(L, 2);
bool crop = lua::toboolean(L, 3);
bool saveEntities = lua::toboolean(L, 4);
auto fragment =
VoxelFragment::create(level, pointA, pointB, crop, saveEntities);
return lua::newuserdata<lua::LuaVoxelFragment>(
L, std::shared_ptr<VoxelFragment>(std::move(fragment))
);
}
static int l_load_fragment(lua::State* L) {
auto paths = engine->getPaths();
auto [prefix, filename] = EnginePaths::parsePath(lua::require_string(L, 1));
@@ -38,9 +44,9 @@ static int l_load_structure(lua::State* L) {
}
auto map = files::read_binary_json(path);
auto structure = std::make_shared<VoxelFragment>();
structure->deserialize(map);
return lua::newuserdata<lua::LuaVoxelStructure>(L, std::move(structure));
auto fragment = std::make_shared<VoxelFragment>();
fragment->deserialize(map);
return lua::newuserdata<lua::LuaVoxelFragment>(L, std::move(fragment));
}
/// @brief Get a list of all world generators
@@ -72,8 +78,9 @@ static int l_get_default_generator(lua::State* L) {
}
const luaL_Reg generationlib[] = {
{"save_structure", lua::wrap<l_save_structure>},
{"load_structure", lua::wrap<l_load_structure>},
{"create_fragment", lua::wrap<l_create_fragment>},
{"save_fragment", lua::wrap<l_save_fragment>},
{"load_fragment", lua::wrap<l_load_fragment>},
{"get_generators", lua::wrap<l_get_generators>},
{"get_default_generator", lua::wrap<l_get_default_generator>},
{NULL, NULL}};