add EnginePaths::parsePath & update generation.load_structure

This commit is contained in:
MihailRis 2024-09-21 01:24:38 +03:00
parent 4f882a3ca3
commit aa1fb26ea5
4 changed files with 18 additions and 7 deletions

View File

@ -1,4 +1,4 @@
-- use for engine development tests
-- must be empty in release
-- must not be modified by content-packs
print(generation.load_structure("core:generators/default.files/tree0.vox"))
print(generation.load_structure("core:default.files/tree0"))

View File

@ -162,15 +162,23 @@ void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
this->contentPacks = contentPacks;
}
std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view path) {
size_t separator = path.find(':');
if (separator == std::string::npos) {
return {"", std::string(path)};
}
auto prefix = std::string(path.substr(0, separator));
auto filename = std::string(path.substr(separator + 1));
return {prefix, filename};
}
std::filesystem::path EnginePaths::resolve(
const std::string& path, bool throwErr
) {
size_t separator = path.find(':');
if (separator == std::string::npos) {
auto [prefix, filename] = EnginePaths::parsePath(path);
if (prefix.empty()) {
throw files_access_error("no entry point specified");
}
std::string prefix = path.substr(0, separator);
std::string filename = path.substr(separator + 1);
filename = toCanonic(fs::u8path(filename)).u8string();
if (prefix == "res" || prefix == "core") {

View File

@ -4,6 +4,7 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <tuple>
#include "content/ContentPack.hpp"
@ -40,6 +41,7 @@ public:
std::filesystem::path resolve(const std::string& path, bool throwErr = true);
static std::tuple<std::string, std::string> parsePath(std::string_view view);
private:
std::filesystem::path userFilesFolder {"."};
std::filesystem::path resourcesFolder {"res"};

View File

@ -29,8 +29,9 @@ static int l_save_structure(lua::State* L) {
static int l_load_structure(lua::State* L) {
auto paths = engine->getPaths();
auto filename = lua::require_string(L, 1);
auto path = paths->resolve(filename);
auto [prefix, filename] = EnginePaths::parsePath(lua::require_string(L, 1));
auto path = paths->resolve(prefix+":generators/"+filename+".vox");
if (!std::filesystem::exists(path)) {
throw std::runtime_error("file "+path.u8string()+" does not exist");
}