World content packs list
This commit is contained in:
parent
7f8935a93c
commit
082599e78c
@ -1,6 +1,7 @@
|
|||||||
# Menu
|
# Menu
|
||||||
menu.missing-content=Missing Content!
|
menu.missing-content=Missing Content!
|
||||||
world.convert-request=Content indices have changed! Convert world files?
|
world.convert-request=Content indices have changed! Convert world files?
|
||||||
|
error.pack-not-found=Could not to find pack
|
||||||
|
|
||||||
# Bindings
|
# Bindings
|
||||||
movement.forward=Forward
|
movement.forward=Forward
|
||||||
|
|||||||
@ -6,6 +6,8 @@ Cancel=Отмена
|
|||||||
Back=Назад
|
Back=Назад
|
||||||
Continue=Продолжить
|
Continue=Продолжить
|
||||||
|
|
||||||
|
error.pack-not-found=Не удалось найти пакет
|
||||||
|
|
||||||
# Меню
|
# Меню
|
||||||
menu.New World=Новый Мир
|
menu.New World=Новый Мир
|
||||||
menu.Quit=Выход
|
menu.Quit=Выход
|
||||||
|
|||||||
@ -6,6 +6,8 @@ Cancel=Отмена
|
|||||||
Back = Обратно
|
Back = Обратно
|
||||||
Continue=Продолжить
|
Continue=Продолжить
|
||||||
|
|
||||||
|
error.pack-not-found=Не удалось найти пакет
|
||||||
|
|
||||||
# Меню
|
# Меню
|
||||||
menu.New World = Новый Мир
|
menu.New World = Новый Мир
|
||||||
menu.Quit=Выход
|
menu.Quit=Выход
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
#include "ContentPack.h"
|
#include "ContentPack.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "../files/files.h"
|
|
||||||
#include "../coders/json.h"
|
#include "../coders/json.h"
|
||||||
|
#include "../files/files.h"
|
||||||
|
#include "../files/engine_paths.h"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
@ -44,3 +46,21 @@ void ContentPack::scan(fs::path rootfolder,
|
|||||||
packs.push_back(read(folder));
|
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 std::runtime_error("could not to find pack '"+name+"'");
|
||||||
|
}
|
||||||
|
return folder;
|
||||||
|
}
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
class EnginePaths;
|
||||||
|
|
||||||
struct ContentPack {
|
struct ContentPack {
|
||||||
std::string id = "none";
|
std::string id = "none";
|
||||||
std::string title = "untitled";
|
std::string title = "untitled";
|
||||||
@ -19,6 +21,8 @@ struct ContentPack {
|
|||||||
static ContentPack read(std::filesystem::path folder);
|
static ContentPack read(std::filesystem::path folder);
|
||||||
static void scan(std::filesystem::path folder,
|
static void scan(std::filesystem::path folder,
|
||||||
std::vector<ContentPack>& packs);
|
std::vector<ContentPack>& packs);
|
||||||
|
static std::vector<std::string> worldPacksList(std::filesystem::path folder);
|
||||||
|
static std::filesystem::path findPack(const EnginePaths* paths, std::string name);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONTENT_CONTENT_PACK_H_
|
#endif // CONTENT_CONTENT_PACK_H_
|
||||||
|
|||||||
@ -7,15 +7,10 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "../coders/json.h"
|
#include "../coders/json.h"
|
||||||
|
|
||||||
using std::ios;
|
namespace fs = std::filesystem;
|
||||||
using std::string;
|
|
||||||
using std::unique_ptr;
|
|
||||||
using std::ifstream;
|
|
||||||
using std::ofstream;
|
|
||||||
using std::filesystem::path;
|
|
||||||
|
|
||||||
bool files::write_bytes(path filename, const char* data, size_t size) {
|
bool files::write_bytes(fs::path filename, const char* data, size_t size) {
|
||||||
ofstream output(filename, ios::binary);
|
std::ofstream output(filename, std::ios::binary);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return false;
|
return false;
|
||||||
output.write(data, size);
|
output.write(data, size);
|
||||||
@ -23,8 +18,8 @@ bool files::write_bytes(path filename, const char* data, size_t size) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint files::append_bytes(path filename, const char* data, size_t size) {
|
uint files::append_bytes(fs::path filename, const char* data, size_t size) {
|
||||||
ofstream output(filename, ios::binary | ios::app);
|
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return 0;
|
return 0;
|
||||||
uint position = output.tellp();
|
uint position = output.tellp();
|
||||||
@ -33,8 +28,8 @@ uint files::append_bytes(path filename, const char* data, size_t size) {
|
|||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool files::read(path filename, char* data, size_t size) {
|
bool files::read(fs::path filename, char* data, size_t size) {
|
||||||
ifstream output(filename, ios::binary);
|
std::ifstream output(filename, std::ios::binary);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return false;
|
return false;
|
||||||
output.read(data, size);
|
output.read(data, size);
|
||||||
@ -42,31 +37,32 @@ bool files::read(path filename, char* data, size_t size) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* files::read_bytes(path filename, size_t& length) {
|
char* files::read_bytes(fs::path filename, size_t& length) {
|
||||||
ifstream input(filename, ios::binary);
|
std::ifstream input(filename, std::ios::binary);
|
||||||
if (!input.is_open())
|
if (!input.is_open())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
input.seekg(0, std::ios_base::end);
|
input.seekg(0, std::ios_base::end);
|
||||||
length = input.tellg();
|
length = input.tellg();
|
||||||
input.seekg(0, std::ios_base::beg);
|
input.seekg(0, std::ios_base::beg);
|
||||||
|
|
||||||
unique_ptr<char> data(new char[length]);
|
std::unique_ptr<char> data(new char[length]);
|
||||||
input.read(data.get(), length);
|
input.read(data.get(), length);
|
||||||
input.close();
|
input.close();
|
||||||
return data.release();
|
return data.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string files::read_string(path filename) {
|
std::string files::read_string(fs::path filename) {
|
||||||
size_t size;
|
size_t size;
|
||||||
unique_ptr<char> chars (read_bytes(filename, size));
|
std::unique_ptr<char> chars (read_bytes(filename, size));
|
||||||
if (chars == nullptr) {
|
if (chars == nullptr) {
|
||||||
throw std::runtime_error("could not to load file '"+filename.string()+"'");
|
throw std::runtime_error("could not to load file '"+
|
||||||
|
filename.string()+"'");
|
||||||
}
|
}
|
||||||
return string(chars.get(), size);
|
return std::string(chars.get(), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool files::write_string(path filename, const string content) {
|
bool files::write_string(fs::path filename, const std::string content) {
|
||||||
ofstream file(filename);
|
std::ofstream file(filename);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -74,8 +70,8 @@ bool files::write_string(path filename, const string content) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
json::JObject* files::read_json(path file) {
|
json::JObject* files::read_json(fs::path file) {
|
||||||
string text = files::read_string(file);
|
std::string text = files::read_string(file);
|
||||||
try {
|
try {
|
||||||
return json::parse(file.string(), text);
|
return json::parse(file.string(), text);
|
||||||
} catch (const parsing_error& error) {
|
} catch (const parsing_error& error) {
|
||||||
@ -83,3 +79,20 @@ json::JObject* files::read_json(path file) {
|
|||||||
throw std::runtime_error("could not to parse "+file.string());
|
throw std::runtime_error("could not to parse "+file.string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> files::read_list(std::filesystem::path filename) {
|
||||||
|
std::ifstream file(filename);
|
||||||
|
if (!file) {
|
||||||
|
throw std::runtime_error("could not to open file "+filename.u8string());
|
||||||
|
}
|
||||||
|
std::vector<std::string> lines;
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
if (line.length() == 0)
|
||||||
|
continue;
|
||||||
|
if (line[0] == '#')
|
||||||
|
continue;
|
||||||
|
lines.push_back(line);
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#define FILES_FILES_H_
|
#define FILES_FILES_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
|
||||||
@ -17,6 +18,7 @@ namespace files {
|
|||||||
extern std::string read_string(std::filesystem::path filename);
|
extern std::string read_string(std::filesystem::path filename);
|
||||||
extern bool write_string(std::filesystem::path filename, const std::string content);
|
extern bool write_string(std::filesystem::path filename, const std::string content);
|
||||||
extern json::JObject* read_json(std::filesystem::path file);
|
extern json::JObject* read_json(std::filesystem::path file);
|
||||||
|
extern std::vector<std::string> read_list(std::filesystem::path file);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* FILES_FILES_H_ */
|
#endif /* FILES_FILES_H_ */
|
||||||
@ -159,15 +159,27 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
|
|||||||
button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
|
button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
|
||||||
button->listenAction([=](GUI* gui) {
|
button->listenAction([=](GUI* gui) {
|
||||||
// TODO: complete and move somewhere
|
// TODO: complete and move somewhere
|
||||||
|
|
||||||
|
auto folder = paths->getWorldsFolder()/u8path(name);
|
||||||
auto resdir = engine->getPaths()->getResources();
|
auto resdir = engine->getPaths()->getResources();
|
||||||
auto& packs = engine->getContentPacks();
|
auto& packs = engine->getContentPacks();
|
||||||
packs.clear();
|
packs.clear();
|
||||||
packs.push_back(ContentPack::read(resdir/path("content/base")));
|
auto packnames = ContentPack::worldPacksList(folder);
|
||||||
|
for (auto name : packnames) {
|
||||||
|
path packfolder;
|
||||||
|
try {
|
||||||
|
packfolder = ContentPack::findPack(paths, name);
|
||||||
|
} catch (std::runtime_error& error) {
|
||||||
|
guiutil::alert(gui, langs::get(L"error.pack-not-found")+
|
||||||
|
L": "+util::str2wstr_utf8(name));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
packs.push_back(ContentPack::read(packfolder));
|
||||||
|
}
|
||||||
engine->loadContent();
|
engine->loadContent();
|
||||||
|
|
||||||
auto* content = engine->getContent();
|
auto* content = engine->getContent();
|
||||||
auto& settings = engine->getSettings();
|
auto& settings = engine->getSettings();
|
||||||
auto folder = paths->getWorldsFolder()/u8path(name);
|
|
||||||
std::filesystem::create_directories(folder);
|
std::filesystem::create_directories(folder);
|
||||||
ContentLUT* lut = World::checkIndices(folder, content);
|
ContentLUT* lut = World::checkIndices(folder, content);
|
||||||
if (lut) {
|
if (lut) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user