mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
89 lines
2.8 KiB
C++
89 lines
2.8 KiB
C++
#include "ContentPack.h"
|
|
|
|
#include <iostream>
|
|
|
|
#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";
|
|
|
|
contentpack_error::contentpack_error(
|
|
std::string packId,
|
|
std::filesystem::path folder,
|
|
std::string message)
|
|
: std::runtime_error(message), packId(packId), folder(folder) {
|
|
}
|
|
|
|
std::string contentpack_error::getPackId() const {
|
|
return packId;
|
|
}
|
|
std::filesystem::path contentpack_error::getFolder() const {
|
|
return folder;
|
|
}
|
|
|
|
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 contentpack_error(pack.id, folder, "content-pack id is none");
|
|
return pack;
|
|
}
|
|
|
|
void ContentPack::scan(fs::path rootfolder,
|
|
std::vector<ContentPack>& 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<std::string> 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 contentpack_error(name, folder,
|
|
"could not to find pack '"+name+"'");
|
|
}
|
|
return folder;
|
|
}
|
|
|
|
void ContentPack::readPacks(const EnginePaths* paths,
|
|
std::vector<ContentPack>& packs,
|
|
const std::vector<std::string>& packnames) {
|
|
for (const auto& name : packnames) {
|
|
fs::path packfolder = ContentPack::findPack(paths, name);
|
|
packs.push_back(ContentPack::read(packfolder));
|
|
}
|
|
}
|