diff --git a/src/engine.cpp b/src/engine.cpp index 8e5584c7..3c957507 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -70,7 +70,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths) auto resdir = paths->getResources(); - std::vector roots {resdir}; + std::vector> roots {}; resPaths = std::make_unique(resdir, roots); try { loadAssets(); @@ -241,9 +241,9 @@ void Engine::loadContent() { names = manager.assembly(names); contentPacks = manager.getAll(names); - std::vector resRoots; + std::vector> resRoots; for (auto& pack : contentPacks) { - resRoots.push_back(pack.folder); + resRoots.push_back({pack.id, pack.folder}); ContentLoader loader(&pack); loader.load(contentBuilder); diff --git a/src/files/engine_paths.cpp b/src/files/engine_paths.cpp index 59975a25..bd5f8fdf 100644 --- a/src/files/engine_paths.cpp +++ b/src/files/engine_paths.cpp @@ -4,6 +4,7 @@ #include #include +#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 roots) +ResPaths::ResPaths(fs::path mainRoot, std::vector> 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 ResPaths::listdir(const std::string& folderName) const { std::vector 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)) { diff --git a/src/files/engine_paths.h b/src/files/engine_paths.h index 7efef2e6..f6d5dd3a 100644 --- a/src/files/engine_paths.h +++ b/src/files/engine_paths.h @@ -42,12 +42,15 @@ public: class ResPaths { fs::path mainRoot; - std::vector roots; + std::vector> roots; public: - ResPaths(fs::path mainRoot, - std::vector roots); + ResPaths( + fs::path mainRoot, + std::vector> roots + ); fs::path find(const std::string& filename) const; + std::string findRaw(const std::string& filename) const; std::vector listdir(const std::string& folder) const; const fs::path& getMainRoot() const; diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 96839aa1..22772874 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -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) {