#include "ContentPack.h" #include #include #include "../coders/json.h" #include "../files/files.h" #include "../files/engine_paths.h" namespace fs = std::filesystem; const std::string ContentPack::PACKAGE_FILENAME = "package.json"; const std::string ContentPack::CONTENT_FILENAME = "content.json"; std::filesystem::path ContentPack::getContentFile() const { return folder/fs::path(CONTENT_FILENAME); } bool ContentPack::is_pack(std::filesystem::path folder) { return fs::is_regular_file(folder/fs::path(PACKAGE_FILENAME)); } ContentPack ContentPack::read(std::filesystem::path folder) { auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME)); ContentPack pack; root->str("id", pack.id); root->str("title", pack.title); root->str("version", pack.version); pack.folder = folder; if (pack.id == "none") throw std::runtime_error("content-pack id is none: "+folder.u8string()); return pack; } void ContentPack::scan(fs::path rootfolder, std::vector& packs) { if (!fs::is_directory(rootfolder)) { return; } for (auto entry : fs::directory_iterator(rootfolder)) { fs::path folder = entry.path(); if (!fs::is_directory(folder)) continue; if (!is_pack(folder)) continue; packs.push_back(read(folder)); } } std::vector ContentPack::worldPacksList(fs::path folder) { fs::path listfile = folder / fs::path("packs.list"); if (!fs::is_regular_file(listfile)) { std::cerr << "warning: packs.list not found (will be created)"; std::cerr << std::endl; files::write_string(listfile, "# autogenerated, do not modify\nbase\n"); } return files::read_list(listfile); } fs::path ContentPack::findPack(const EnginePaths* paths, std::string name) { auto folder = paths->getResources() / fs::path("content") / fs::path(name); if (!fs::is_directory(folder)) { throw std::runtime_error("could not to find pack '"+name+"'"); } return folder; }