add file.read_combined_list(...)
This commit is contained in:
parent
f20a0dfae0
commit
e590d06bb0
@ -303,21 +303,28 @@ void Engine::loadContent() {
|
||||
names = manager.assembly(names);
|
||||
contentPacks = manager.getAll(names);
|
||||
|
||||
std::vector<PathsRoot> resRoots;
|
||||
{
|
||||
auto pack = ContentPack::createCore(paths);
|
||||
resRoots.push_back({"core", pack.folder});
|
||||
ContentLoader(&pack, contentBuilder).load();
|
||||
load_configs(pack.folder);
|
||||
}
|
||||
auto corePack = ContentPack::createCore(paths);
|
||||
|
||||
// Setup filesystem entry points
|
||||
std::vector<PathsRoot> resRoots {
|
||||
{"core", corePack.folder}
|
||||
};
|
||||
for (auto& pack : contentPacks) {
|
||||
resRoots.push_back({pack.id, pack.folder});
|
||||
}
|
||||
resPaths = std::make_unique<ResPaths>(resdir, resRoots);
|
||||
|
||||
// Load content
|
||||
{
|
||||
ContentLoader(&corePack, contentBuilder).load();
|
||||
load_configs(corePack.folder);
|
||||
}
|
||||
for (auto& pack : contentPacks) {
|
||||
ContentLoader(&pack, contentBuilder).load();
|
||||
load_configs(pack.folder);
|
||||
}
|
||||
|
||||
content = contentBuilder.build();
|
||||
resPaths = std::make_unique<ResPaths>(resdir, resRoots);
|
||||
|
||||
langs::setup(resdir, langs::current->getId(), contentPacks);
|
||||
loadAssets();
|
||||
|
||||
@ -10,6 +10,9 @@
|
||||
#include <utility>
|
||||
|
||||
#include "WorldFiles.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
static debug::Logger logger("engine-paths");
|
||||
|
||||
|
||||
/// @brief ENUM for accessing folder and file names
|
||||
@ -258,6 +261,31 @@ std::vector<std::filesystem::path> ResPaths::listdir(
|
||||
return entries;
|
||||
}
|
||||
|
||||
dv::value ResPaths::readCombinedList(const std::string& filename) {
|
||||
dv::value list = dv::list();
|
||||
for (const auto& root : roots) {
|
||||
auto path = root.path / fs::u8path(filename);
|
||||
if (!fs::exists(path)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
auto value = files::read_object(path);
|
||||
if (!value.isList()) {
|
||||
logger.warning() << "reading combined list " << root.name << ":"
|
||||
<< filename << " is not a list (skipped)";
|
||||
continue;
|
||||
}
|
||||
for (const auto& elem : value) {
|
||||
list.add(elem);
|
||||
}
|
||||
} catch (const std::runtime_error& err) {
|
||||
logger.warning() << "reading combined list " << root.name << ":"
|
||||
<< filename << ": " << err.what();
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
const std::filesystem::path& ResPaths::getMainRoot() const {
|
||||
return mainRoot;
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
|
||||
#include "data/dv.hpp"
|
||||
#include "content/ContentPack.hpp"
|
||||
|
||||
|
||||
@ -63,6 +64,11 @@ public:
|
||||
std::vector<std::filesystem::path> listdir(const std::string& folder) const;
|
||||
std::vector<std::string> listdirRaw(const std::string& folder) const;
|
||||
|
||||
/// @brief Read all found list versions from all packs and combine into a
|
||||
/// single list. Invalid versions will be skipped with logging a warning
|
||||
/// @param file *.json file path relative to entry point
|
||||
dv::value readCombinedList(const std::string& file);
|
||||
|
||||
const std::filesystem::path& getMainRoot() const;
|
||||
|
||||
private:
|
||||
|
||||
@ -63,8 +63,13 @@ namespace files {
|
||||
/// @brief Read JSON or BJSON file
|
||||
/// @param file *.json or *.bjson file
|
||||
dv::value read_json(const fs::path& file);
|
||||
|
||||
dv::value read_binary_json(const fs::path& file);
|
||||
|
||||
/// @brief Read TOML file
|
||||
/// @param file *.toml file
|
||||
dv::value read_toml(const fs::path& file);
|
||||
|
||||
std::vector<std::string> read_list(const fs::path& file);
|
||||
|
||||
bool is_data_file(const fs::path& file);
|
||||
|
||||
@ -247,6 +247,14 @@ static int l_file_gzip_decompress(lua::State* L) {
|
||||
}
|
||||
}
|
||||
|
||||
static int l_file_read_combined_list(lua::State* L) {
|
||||
std::string path = lua::require_string(L, 1);
|
||||
if (path.find(':') != std::string::npos) {
|
||||
throw std::runtime_error("entry point must not be specified");
|
||||
}
|
||||
return lua::pushvalue(L, engine->getResPaths()->readCombinedList(path));
|
||||
}
|
||||
|
||||
const luaL_Reg filelib[] = {
|
||||
{"exists", lua::wrap<l_file_exists>},
|
||||
{"find", lua::wrap<l_file_find>},
|
||||
@ -265,4 +273,5 @@ const luaL_Reg filelib[] = {
|
||||
{"write", lua::wrap<l_file_write>},
|
||||
{"gzip_compress", lua::wrap<l_file_gzip_compress>},
|
||||
{"gzip_decompress", lua::wrap<l_file_gzip_decompress>},
|
||||
{"read_combined_list", lua::wrap<l_file_read_combined_list>},
|
||||
{NULL, NULL}};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user