fixed worlds list scanning
This commit is contained in:
parent
a4a5aef422
commit
1c99dfa180
@ -28,8 +28,6 @@
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
regfile::regfile(fs::path filename) : file(filename) {
|
||||
if (file.length() < REGION_HEADER_SIZE)
|
||||
throw std::runtime_error("incomplete region file header");
|
||||
@ -90,6 +88,8 @@ uint WorldRegion::getChunkDataSize(uint x, uint z) {
|
||||
return sizes[z * REGION_SIZE + x];
|
||||
}
|
||||
|
||||
const char* WorldFiles::WORLD_FILE = "world.json";
|
||||
|
||||
WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
|
||||
: directory(directory),
|
||||
generatorTestMode(settings.generatorTestMode),
|
||||
@ -248,7 +248,7 @@ fs::path WorldFiles::getPlayerFile() const {
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getWorldFile() const {
|
||||
return directory/fs::path("world.json");
|
||||
return directory/fs::path(WORLD_FILE);
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getIndicesFile() const {
|
||||
|
||||
@ -146,6 +146,8 @@ public:
|
||||
void write(const World* world, const Content* content);
|
||||
void writePacks(const World* world);
|
||||
void writeIndices(const ContentIndices* indices);
|
||||
|
||||
static const char* WORLD_FILE;
|
||||
};
|
||||
|
||||
#endif /* FILES_WORLDFILES_H_ */
|
||||
@ -2,7 +2,9 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <sstream>
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "WorldFiles.h"
|
||||
|
||||
#define SCREENSHOTS_FOLDER "screenshots"
|
||||
|
||||
@ -41,6 +43,27 @@ fs::path EnginePaths::getWorldsFolder() {
|
||||
return userfiles/fs::path("worlds");
|
||||
}
|
||||
|
||||
std::vector<fs::path> EnginePaths::scanForWorlds() {
|
||||
std::vector<fs::path> folders;
|
||||
|
||||
fs::path folder = getWorldsFolder();
|
||||
if (!fs::is_directory(folder))
|
||||
return folders;
|
||||
|
||||
for (auto entry : fs::directory_iterator(folder)) {
|
||||
if (!entry.is_directory()) {
|
||||
continue;
|
||||
}
|
||||
fs::path worldFolder = entry.path();
|
||||
fs::path worldFile = worldFolder/fs::path(WorldFiles::WORLD_FILE);
|
||||
if (!fs::is_regular_file(worldFile)) {
|
||||
continue;
|
||||
}
|
||||
folders.push_back(worldFolder);
|
||||
}
|
||||
return folders;
|
||||
}
|
||||
|
||||
bool EnginePaths::isWorldNameUsed(std::string name) {
|
||||
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
|
||||
}
|
||||
|
||||
@ -25,6 +25,8 @@ public:
|
||||
void setResources(fs::path folder);
|
||||
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
||||
|
||||
std::vector<fs::path> scanForWorlds();
|
||||
|
||||
fs::path resolve(std::string path);
|
||||
};
|
||||
|
||||
|
||||
@ -153,7 +153,7 @@ void open_world(std::string name, Engine* engine) {
|
||||
auto folder = paths->getWorldsFolder()/fs::u8path(name);
|
||||
try {
|
||||
engine->loadWorldContent(folder);
|
||||
} catch (contentpack_error& error) {
|
||||
} catch (const contentpack_error& error) {
|
||||
// could not to find or read pack
|
||||
guiutil::alert(engine->getGUI(),
|
||||
langs::get(L"error.pack-not-found")+
|
||||
@ -178,8 +178,15 @@ void open_world(std::string name, Engine* engine) {
|
||||
show_convert_request(engine, content, lut, folder);
|
||||
}
|
||||
} else {
|
||||
Level* level = World::load(folder, settings, content, packs);
|
||||
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
|
||||
try {
|
||||
Level* level = World::load(folder, settings, content, packs);
|
||||
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
|
||||
} catch (const world_load_error& error) {
|
||||
guiutil::alert(engine->getGUI(),
|
||||
langs::get(L"Error")+
|
||||
L": "+util::str2wstr_utf8(error.what()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,48 +196,45 @@ Panel* create_worlds_panel(Engine* engine) {
|
||||
panel->maxLength(400);
|
||||
|
||||
auto paths = engine->getPaths();
|
||||
|
||||
auto folders = paths->scanForWorlds();
|
||||
|
||||
fs::path worldsFolder = paths->getWorldsFolder();
|
||||
if (fs::is_directory(worldsFolder)) {
|
||||
for (auto entry : fs::directory_iterator(worldsFolder)) {
|
||||
if (!entry.is_directory()) {
|
||||
continue;
|
||||
}
|
||||
auto folder = entry.path();
|
||||
auto name = folder.filename().u8string();
|
||||
auto namews = util::str2wstr_utf8(name);
|
||||
for (auto folder : folders) {
|
||||
auto name = folder.filename().u8string();
|
||||
auto namews = util::str2wstr_utf8(name);
|
||||
|
||||
auto btn = std::make_shared<RichButton>(vec2(390, 46));
|
||||
btn->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
|
||||
btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
||||
auto btn = std::make_shared<RichButton>(vec2(390, 46));
|
||||
btn->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
|
||||
btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
||||
|
||||
auto label = std::make_shared<Label>(namews);
|
||||
label->setInteractive(false);
|
||||
btn->add(label, vec2(8, 8));
|
||||
btn->listenAction([=](GUI*) {
|
||||
open_world(name, engine);
|
||||
auto label = std::make_shared<Label>(namews);
|
||||
label->setInteractive(false);
|
||||
btn->add(label, vec2(8, 8));
|
||||
btn->listenAction([=](GUI*) {
|
||||
open_world(name, engine);
|
||||
});
|
||||
|
||||
auto image = std::make_shared<Image>("gui/delete_icon", vec2(32, 32));
|
||||
image->color(vec4(1, 1, 1, 0.5f));
|
||||
|
||||
auto delbtn = std::make_shared<Button>(image, vec4(2));
|
||||
delbtn->color(vec4(0.0f));
|
||||
delbtn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
||||
|
||||
btn->add(delbtn, vec2(330, 3));
|
||||
|
||||
delbtn->listenAction([=](GUI* gui) {
|
||||
guiutil::confirm(gui, langs::get(L"delete-confirm", L"world")+
|
||||
L" ("+util::str2wstr_utf8(folder.u8string())+L")", [=]()
|
||||
{
|
||||
std::cout << "deleting " << folder.u8string() << std::endl;
|
||||
fs::remove_all(folder);
|
||||
menus::refresh_menus(engine, gui->getMenu());
|
||||
});
|
||||
});
|
||||
|
||||
auto image = std::make_shared<Image>("gui/delete_icon", vec2(32, 32));
|
||||
image->color(vec4(1, 1, 1, 0.5f));
|
||||
|
||||
auto delbtn = std::make_shared<Button>(image, vec4(2));
|
||||
delbtn->color(vec4(0.0f));
|
||||
delbtn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
||||
|
||||
btn->add(delbtn, vec2(330, 3));
|
||||
|
||||
delbtn->listenAction([=](GUI* gui) {
|
||||
guiutil::confirm(gui, langs::get(L"delete-confirm", L"world")+
|
||||
L" ("+util::str2wstr_utf8(folder.u8string())+L")", [=]()
|
||||
{
|
||||
std::cout << "deleting " << folder.u8string() << std::endl;
|
||||
fs::remove_all(folder);
|
||||
menus::refresh_menus(engine, gui->getMenu());
|
||||
});
|
||||
});
|
||||
|
||||
panel->add(btn);
|
||||
}
|
||||
panel->add(btn);
|
||||
}
|
||||
return panel;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user