diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 02690def..44c8af76 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -28,8 +28,6 @@ #include #include -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 { diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 9f44f99c..ef23c024 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -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_ */ \ No newline at end of file diff --git a/src/files/engine_paths.cpp b/src/files/engine_paths.cpp index bab9059c..8096fedc 100644 --- a/src/files/engine_paths.cpp +++ b/src/files/engine_paths.cpp @@ -2,7 +2,9 @@ #include #include + #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 EnginePaths::scanForWorlds() { + std::vector 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)); } diff --git a/src/files/engine_paths.h b/src/files/engine_paths.h index 4a25161f..d8506f70 100644 --- a/src/files/engine_paths.h +++ b/src/files/engine_paths.h @@ -25,6 +25,8 @@ public: void setResources(fs::path folder); void setContentPacks(std::vector* contentPacks); + std::vector scanForWorlds(); + fs::path resolve(std::string path); }; diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index ef4e9c76..5ea5ded8 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -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(engine, level)); + try { + Level* level = World::load(folder, settings, content, packs); + engine->setScreen(std::make_shared(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(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(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