ResPaths.findRaw

This commit is contained in:
MihailRis 2024-04-19 14:22:14 +03:00
parent bedcc438f4
commit 0e69db13be
4 changed files with 29 additions and 26 deletions

View File

@ -70,7 +70,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
auto resdir = paths->getResources();
std::vector<fs::path> roots {resdir};
std::vector<std::pair<std::string, fs::path>> roots {};
resPaths = std::make_unique<ResPaths>(resdir, roots);
try {
loadAssets();
@ -241,9 +241,9 @@ void Engine::loadContent() {
names = manager.assembly(names);
contentPacks = manager.getAll(names);
std::vector<fs::path> resRoots;
std::vector<std::pair<std::string, fs::path>> resRoots;
for (auto& pack : contentPacks) {
resRoots.push_back(pack.folder);
resRoots.push_back({pack.id, pack.folder});
ContentLoader loader(&pack);
loader.load(contentBuilder);

View File

@ -4,6 +4,7 @@
#include <sstream>
#include <filesystem>
#include "../util/stringutil.h"
#include "../typedefs.h"
#include "WorldFiles.h"
@ -152,14 +153,14 @@ fs::path EnginePaths::resolve(std::string path) {
throw files_access_error("unknown entry point '"+prefix+"'");
}
ResPaths::ResPaths(fs::path mainRoot, std::vector<fs::path> roots)
ResPaths::ResPaths(fs::path mainRoot, std::vector<std::pair<std::string, fs::path>> roots)
: mainRoot(mainRoot), roots(roots) {
}
fs::path ResPaths::find(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
fs::path file = root / fs::u8path(filename);
fs::path file = root.second / fs::u8path(filename);
if (fs::exists(file)) {
return file;
}
@ -167,11 +168,25 @@ fs::path ResPaths::find(const std::string& filename) const {
return mainRoot / fs::u8path(filename);
}
std::string ResPaths::findRaw(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
if (fs::exists(root.second / fs::path(filename))) {
return root.first+":"+filename;
}
}
auto resDir = mainRoot;
if (fs::exists(resDir / fs::path(filename))) {
return "core:"+filename;
}
throw std::runtime_error("could not to find file "+util::quote(filename));
}
std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
std::vector<fs::path> entries;
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
fs::path folder = root / fs::u8path(folderName);
fs::path folder = root.second / fs::u8path(folderName);
if (!fs::is_directory(folder))
continue;
for (const auto& entry : fs::directory_iterator(folder)) {

View File

@ -42,12 +42,15 @@ public:
class ResPaths {
fs::path mainRoot;
std::vector<fs::path> roots;
std::vector<std::pair<std::string, fs::path>> roots;
public:
ResPaths(fs::path mainRoot,
std::vector<fs::path> roots);
ResPaths(
fs::path mainRoot,
std::vector<std::pair<std::string, fs::path>> roots
);
fs::path find(const std::string& filename) const;
std::string findRaw(const std::string& filename) const;
std::vector<fs::path> listdir(const std::string& folder) const;
const fs::path& getMainRoot() const;

View File

@ -19,25 +19,10 @@ static fs::path resolve_path(lua_State* L, const std::string& path) {
}
}
// TODO: move to ResPaths
static int l_file_find(lua_State* L) {
std::string path = lua_tostring(L, 1);
auto& packs = scripting::engine->getContentPacks();
for (int i = packs.size()-1; i >= 0; i--) {
auto& pack = packs[i];
if (fs::exists(pack.folder / fs::path(path))) {
lua_pushstring(L, (pack.id+":"+path).c_str());
return 1;
}
}
auto resDir = scripting::engine->getResPaths()->getMainRoot();
if (fs::exists(resDir / fs::path(path))) {
lua_pushstring(L, ("core:"+path).c_str());
return 1;
}
luaL_error(L, "file not found %q", path.c_str());
return 0;
lua_pushstring(L, scripting::engine->getResPaths()->findRaw(path).c_str());
return 1;
}
static int l_file_resolve(lua_State* L) {