Minor refactor

This commit is contained in:
MihailRis
2023-12-20 21:21:59 +03:00
parent 3ad903449e
commit e0aee76f41
4 changed files with 36 additions and 9 deletions
+17 -3
View File
@@ -1,7 +1,6 @@
#include "ContentPack.h" #include "ContentPack.h"
#include <iostream> #include <iostream>
#include <stdexcept>
#include "../coders/json.h" #include "../coders/json.h"
#include "../files/files.h" #include "../files/files.h"
@@ -12,6 +11,20 @@ namespace fs = std::filesystem;
const std::string ContentPack::PACKAGE_FILENAME = "package.json"; const std::string ContentPack::PACKAGE_FILENAME = "package.json";
const std::string ContentPack::CONTENT_FILENAME = "content.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 { std::filesystem::path ContentPack::getContentFile() const {
return folder/fs::path(CONTENT_FILENAME); return folder/fs::path(CONTENT_FILENAME);
} }
@@ -28,7 +41,7 @@ ContentPack ContentPack::read(std::filesystem::path folder) {
root->str("version", pack.version); root->str("version", pack.version);
pack.folder = folder; pack.folder = folder;
if (pack.id == "none") if (pack.id == "none")
throw std::runtime_error("content-pack id is none: "+folder.u8string()); throw contentpack_error(pack.id, folder, "content-pack id is none");
return pack; return pack;
} }
@@ -60,7 +73,8 @@ std::vector<std::string> ContentPack::worldPacksList(fs::path folder) {
fs::path ContentPack::findPack(const EnginePaths* paths, std::string name) { fs::path ContentPack::findPack(const EnginePaths* paths, std::string name) {
auto folder = paths->getResources() / fs::path("content") / fs::path(name); auto folder = paths->getResources() / fs::path("content") / fs::path(name);
if (!fs::is_directory(folder)) { if (!fs::is_directory(folder)) {
throw std::runtime_error("could not to find pack '"+name+"'"); throw contentpack_error(name, folder,
"could not to find pack '"+name+"'");
} }
return folder; return folder;
} }
+13
View File
@@ -3,10 +3,23 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <stdexcept>
#include <filesystem> #include <filesystem>
class EnginePaths; class EnginePaths;
class contentpack_error : public std::runtime_error {
std::string packId;
std::filesystem::path folder;
public:
contentpack_error(std::string packId,
std::filesystem::path folder,
std::string message);
std::string getPackId() const;
std::filesystem::path getFolder() const;
};
struct ContentPack { struct ContentPack {
std::string id = "none"; std::string id = "none";
std::string title = "untitled"; std::string title = "untitled";
-1
View File
@@ -203,5 +203,4 @@ void Engine::loadAllPacks() {
auto resdir = paths->getResources(); auto resdir = paths->getResources();
contentPacks.clear(); contentPacks.clear();
ContentPack::scan(resdir/fs::path("content"), contentPacks); ContentPack::scan(resdir/fs::path("content"), contentPacks);
loadContent();
} }
+6 -5
View File
@@ -156,16 +156,16 @@ void open_world(std::string name, Engine* engine) {
packs.clear(); packs.clear();
auto packnames = ContentPack::worldPacksList(folder); auto packnames = ContentPack::worldPacksList(folder);
for (auto name : packnames) { for (auto name : packnames) {
fs::path packfolder;
try { try {
packfolder = ContentPack::findPack(paths, name); fs::path packfolder = ContentPack::findPack(paths, name);
} catch (std::runtime_error& error) { packs.push_back(ContentPack::read(packfolder));
} catch (contentpack_error& error) {
// could not to find or read pack
guiutil::alert(engine->getGUI(), guiutil::alert(engine->getGUI(),
langs::get(L"error.pack-not-found")+ langs::get(L"error.pack-not-found")+
L": "+util::str2wstr_utf8(name)); L": "+util::str2wstr_utf8(error.getPackId()));
return; return;
} }
packs.push_back(ContentPack::read(packfolder));
} }
engine->loadContent(); engine->loadContent();
@@ -291,6 +291,7 @@ void create_new_world_panel(Engine* engine, PagesControl* menu) {
fs::create_directories(folder); fs::create_directories(folder);
engine->loadAllPacks(); engine->loadAllPacks();
engine->loadContent();
Level* level = World::create(nameutf8, Level* level = World::create(nameutf8,
folder, folder,
seed, seed,