From 170e6b86ea551a740ff1f1a2255cc00a117df0a6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 20 Dec 2023 23:22:48 +0300 Subject: [PATCH] Minor refactor --- src/content/ContentPack.cpp | 9 +++++++++ src/content/ContentPack.h | 3 +++ src/frontend/menu.cpp | 24 +++++++++--------------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index ca6dbdd9f..7a39dfbe3 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -78,3 +78,12 @@ fs::path ContentPack::findPack(const EnginePaths* paths, std::string name) { } return folder; } + +void ContentPack::readPacks(const EnginePaths* paths, + std::vector& packs, + const std::vector& packnames) { + for (const auto& name : packnames) { + fs::path packfolder = ContentPack::findPack(paths, name); + packs.push_back(ContentPack::read(packfolder)); + } +} diff --git a/src/content/ContentPack.h b/src/content/ContentPack.h index 37e85ee8f..4ae38cba8 100644 --- a/src/content/ContentPack.h +++ b/src/content/ContentPack.h @@ -36,6 +36,9 @@ struct ContentPack { std::vector& packs); static std::vector worldPacksList(std::filesystem::path folder); static std::filesystem::path findPack(const EnginePaths* paths, std::string name); + static void readPacks(const EnginePaths* paths, + std::vector& packs, + const std::vector& names); }; #endif // CONTENT_CONTENT_PACK_H_ diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 41486f725..67c7157c6 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -147,25 +147,19 @@ void create_languages_panel(Engine* engine, PagesControl* menu) { } void open_world(std::string name, Engine* engine) { - // TODO: complete and move somewhere auto paths = engine->getPaths(); - auto resdir = paths->getResources(); auto folder = paths->getWorldsFolder()/fs::u8path(name); - auto& packs = engine->getContentPacks(); packs.clear(); - auto packnames = ContentPack::worldPacksList(folder); - for (auto name : packnames) { - try { - fs::path packfolder = ContentPack::findPack(paths, name); - packs.push_back(ContentPack::read(packfolder)); - } catch (contentpack_error& error) { - // could not to find or read pack - guiutil::alert(engine->getGUI(), - langs::get(L"error.pack-not-found")+ - L": "+util::str2wstr_utf8(error.getPackId())); - return; - } + try { + auto packNames = ContentPack::worldPacksList(folder); + ContentPack::readPacks(paths, packs, packNames); + } catch (contentpack_error& error) { + // could not to find or read pack + guiutil::alert(engine->getGUI(), + langs::get(L"error.pack-not-found")+ + L": "+util::str2wstr_utf8(error.getPackId())); + return; } engine->loadContent();