fixed worlds list scanning
This commit is contained in:
@@ -28,8 +28,6 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
regfile::regfile(fs::path filename) : file(filename) {
|
regfile::regfile(fs::path filename) : file(filename) {
|
||||||
if (file.length() < REGION_HEADER_SIZE)
|
if (file.length() < REGION_HEADER_SIZE)
|
||||||
throw std::runtime_error("incomplete region file header");
|
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];
|
return sizes[z * REGION_SIZE + x];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* WorldFiles::WORLD_FILE = "world.json";
|
||||||
|
|
||||||
WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
|
WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
|
||||||
: directory(directory),
|
: directory(directory),
|
||||||
generatorTestMode(settings.generatorTestMode),
|
generatorTestMode(settings.generatorTestMode),
|
||||||
@@ -248,7 +248,7 @@ fs::path WorldFiles::getPlayerFile() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fs::path WorldFiles::getWorldFile() const {
|
fs::path WorldFiles::getWorldFile() const {
|
||||||
return directory/fs::path("world.json");
|
return directory/fs::path(WORLD_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path WorldFiles::getIndicesFile() const {
|
fs::path WorldFiles::getIndicesFile() const {
|
||||||
|
|||||||
@@ -146,6 +146,8 @@ public:
|
|||||||
void write(const World* world, const Content* content);
|
void write(const World* world, const Content* content);
|
||||||
void writePacks(const World* world);
|
void writePacks(const World* world);
|
||||||
void writeIndices(const ContentIndices* indices);
|
void writeIndices(const ContentIndices* indices);
|
||||||
|
|
||||||
|
static const char* WORLD_FILE;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* FILES_WORLDFILES_H_ */
|
#endif /* FILES_WORLDFILES_H_ */
|
||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
#include "WorldFiles.h"
|
||||||
|
|
||||||
#define SCREENSHOTS_FOLDER "screenshots"
|
#define SCREENSHOTS_FOLDER "screenshots"
|
||||||
|
|
||||||
@@ -41,6 +43,27 @@ fs::path EnginePaths::getWorldsFolder() {
|
|||||||
return userfiles/fs::path("worlds");
|
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) {
|
bool EnginePaths::isWorldNameUsed(std::string name) {
|
||||||
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
|
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ public:
|
|||||||
void setResources(fs::path folder);
|
void setResources(fs::path folder);
|
||||||
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
||||||
|
|
||||||
|
std::vector<fs::path> scanForWorlds();
|
||||||
|
|
||||||
fs::path resolve(std::string path);
|
fs::path resolve(std::string path);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+44
-40
@@ -153,7 +153,7 @@ void open_world(std::string name, Engine* engine) {
|
|||||||
auto folder = paths->getWorldsFolder()/fs::u8path(name);
|
auto folder = paths->getWorldsFolder()/fs::u8path(name);
|
||||||
try {
|
try {
|
||||||
engine->loadWorldContent(folder);
|
engine->loadWorldContent(folder);
|
||||||
} catch (contentpack_error& error) {
|
} catch (const contentpack_error& error) {
|
||||||
// could not to find or read pack
|
// 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")+
|
||||||
@@ -178,8 +178,15 @@ void open_world(std::string name, Engine* engine) {
|
|||||||
show_convert_request(engine, content, lut, folder);
|
show_convert_request(engine, content, lut, folder);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Level* level = World::load(folder, settings, content, packs);
|
try {
|
||||||
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
|
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);
|
panel->maxLength(400);
|
||||||
|
|
||||||
auto paths = engine->getPaths();
|
auto paths = engine->getPaths();
|
||||||
|
|
||||||
|
auto folders = paths->scanForWorlds();
|
||||||
|
|
||||||
fs::path worldsFolder = paths->getWorldsFolder();
|
fs::path worldsFolder = paths->getWorldsFolder();
|
||||||
if (fs::is_directory(worldsFolder)) {
|
for (auto folder : folders) {
|
||||||
for (auto entry : fs::directory_iterator(worldsFolder)) {
|
auto name = folder.filename().u8string();
|
||||||
if (!entry.is_directory()) {
|
auto namews = util::str2wstr_utf8(name);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
auto folder = entry.path();
|
|
||||||
auto name = folder.filename().u8string();
|
|
||||||
auto namews = util::str2wstr_utf8(name);
|
|
||||||
|
|
||||||
auto btn = std::make_shared<RichButton>(vec2(390, 46));
|
auto btn = std::make_shared<RichButton>(vec2(390, 46));
|
||||||
btn->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
|
btn->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
|
||||||
btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
||||||
|
|
||||||
auto label = std::make_shared<Label>(namews);
|
auto label = std::make_shared<Label>(namews);
|
||||||
label->setInteractive(false);
|
label->setInteractive(false);
|
||||||
btn->add(label, vec2(8, 8));
|
btn->add(label, vec2(8, 8));
|
||||||
btn->listenAction([=](GUI*) {
|
btn->listenAction([=](GUI*) {
|
||||||
open_world(name, engine);
|
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));
|
panel->add(btn);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return panel;
|
return panel;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user