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}};
+8 -8
View File
@@ -71,15 +71,15 @@ namespace lua {
};
static_assert(!std::is_abstract<LuaHeightmap>());
class LuaVoxelStructure : public Userdata {
std::shared_ptr<VoxelFragment> structure;
class LuaVoxelFragment : public Userdata {
std::shared_ptr<VoxelFragment> fragment;
public:
LuaVoxelStructure(std::shared_ptr<VoxelFragment> structure);
LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment);
virtual ~LuaVoxelStructure();
virtual ~LuaVoxelFragment();
std::shared_ptr<VoxelFragment> getStructure() const {
return structure;
std::shared_ptr<VoxelFragment> getFragment() const {
return fragment;
}
const std::string& getTypeName() const override {
@@ -87,7 +87,7 @@ namespace lua {
}
static int createMetatable(lua::State*);
inline static std::string TYPENAME = "VoxelStructure";
inline static std::string TYPENAME = "VoxelFragment";
};
static_assert(!std::is_abstract<LuaVoxelStructure>());
static_assert(!std::is_abstract<LuaVoxelFragment>());
}
+1 -1
View File
@@ -103,7 +103,7 @@ void lua::init_state(State* L, StateType stateType) {
newusertype<LuaBytearray>(L);
newusertype<LuaHeightmap>(L);
newusertype<LuaVoxelStructure>(L);
newusertype<LuaVoxelFragment>(L);
}
void lua::initialize(const EnginePaths& paths) {
@@ -7,20 +7,37 @@
using namespace lua;
LuaVoxelStructure::LuaVoxelStructure(std::shared_ptr<VoxelFragment> structure)
: structure(std::move(structure)) {}
LuaVoxelFragment::LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment)
: fragment(std::move(fragment)) {}
LuaVoxelStructure::~LuaVoxelStructure() {
LuaVoxelFragment::~LuaVoxelFragment() {
}
static int l_meta_tostring(lua::State* L) {
return pushstring(L, "VoxelStructure(0x" + util::tohex(
return pushstring(L, "VoxelFragment(0x" + util::tohex(
reinterpret_cast<uint64_t>(topointer(L, 1)))+")");
}
int LuaVoxelStructure::createMetatable(lua::State* L) {
createtable(L, 0, 1);
static int l_meta_index(lua::State* L) {
auto fragment = touserdata<LuaVoxelFragment>(L, 1);
if (fragment == nullptr) {
return 0;
}
if (isstring(L, 2)) {
auto fieldname = tostring(L, 2);
if (!std::strcmp(fieldname, "size")) {
return pushivec(L, fragment->getFragment()->getSize());
}
}
return 0;
}
int LuaVoxelFragment::createMetatable(lua::State* L) {
createtable(L, 0, 2);
pushcfunction(L, lua::wrap<l_meta_tostring>);
setfield(L, "__tostring");
pushcfunction(L, lua::wrap<l_meta_index>);
setfield(L, "__index");
return 1;
}