PacksManager added to engine

This commit is contained in:
MihailRis
2024-04-08 13:09:38 +03:00
parent 3655464b6f
commit f9d391e05a
9 changed files with 93 additions and 67 deletions
-29
View File
@@ -113,21 +113,6 @@ void ContentPack::scanFolder(
} }
} }
void ContentPack::scan(
fs::path rootfolder,
EnginePaths* paths,
std::vector<ContentPack>& packs
) {
scanFolder(paths->getResources()/fs::path("content"), packs);
scanFolder(paths->getUserfiles()/fs::path("content"), packs);
scanFolder(rootfolder, packs);
}
void ContentPack::scan(EnginePaths* paths,
std::vector<ContentPack>& packs) {
scan(paths->getWorldFolder()/fs::path("content"), paths, packs);
}
std::vector<std::string> ContentPack::worldPacksList(fs::path folder) { std::vector<std::string> ContentPack::worldPacksList(fs::path folder) {
fs::path listfile = folder / fs::path("packs.list"); fs::path listfile = folder / fs::path("packs.list");
if (!fs::is_regular_file(listfile)) { if (!fs::is_regular_file(listfile)) {
@@ -154,20 +139,6 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std:
return folder; return folder;
} }
void ContentPack::readPacks(const EnginePaths* paths,
std::vector<ContentPack>& packs,
const std::vector<std::string>& packnames,
fs::path worldDir) {
for (const auto& name : packnames) {
fs::path packfolder = ContentPack::findPack(paths, worldDir, name);
if (!fs::is_directory(packfolder)) {
throw contentpack_error(name, packfolder,
"could not to find pack '"+name+"'");
}
packs.push_back(ContentPack::read(packfolder));
}
}
ContentPackRuntime::ContentPackRuntime( ContentPackRuntime::ContentPackRuntime(
ContentPack info, ContentPack info,
std::unique_ptr<scripting::Environment> env std::unique_ptr<scripting::Environment> env
-17
View File
@@ -64,16 +64,6 @@ struct ContentPack {
std::vector<ContentPack>& packs std::vector<ContentPack>& packs
); );
static void scan(
fs::path folder,
EnginePaths* paths,
std::vector<ContentPack>& packs
);
static void scan(
EnginePaths* paths,
std::vector<ContentPack>& packs
);
static std::vector<std::string> worldPacksList(fs::path folder); static std::vector<std::string> worldPacksList(fs::path folder);
static fs::path findPack( static fs::path findPack(
@@ -81,13 +71,6 @@ struct ContentPack {
fs::path worldDir, fs::path worldDir,
std::string name std::string name
); );
static void readPacks(
const EnginePaths* paths,
std::vector<ContentPack>& packs,
const std::vector<std::string>& names,
fs::path worldDir
);
}; };
struct ContentPackStats { struct ContentPackStats {
+21 -9
View File
@@ -24,7 +24,7 @@ void PacksManager::scan() {
} }
} }
std::vector<std::string> PacksManager::getAllNames() { std::vector<std::string> PacksManager::getAllNames() const {
std::vector<std::string> names; std::vector<std::string> names;
for (auto& entry : packs) { for (auto& entry : packs) {
names.push_back(entry.first); names.push_back(entry.first);
@@ -32,8 +32,20 @@ std::vector<std::string> PacksManager::getAllNames() {
return names; return names;
} }
static contentpack_error on_circular_dependency(std::queue<ContentPack*>& queue) { std::vector<ContentPack> PacksManager::getAll(const std::vector<std::string>& names) const {
ContentPack* lastPack = queue.back(); std::vector<ContentPack> packsList;
for (auto& name : names) {
auto found = packs.find(name);
if (found == packs.end()) {
throw contentpack_error(name, fs::path(""), "pack not found");
}
packsList.push_back(found->second);
}
return packsList;
}
static contentpack_error on_circular_dependency(std::queue<const ContentPack*>& queue) {
const ContentPack* lastPack = queue.back();
// circular dependency // circular dependency
std::stringstream ss; std::stringstream ss;
ss << "circular dependency: " << lastPack->id; ss << "circular dependency: " << lastPack->id;
@@ -55,11 +67,11 @@ static contentpack_error on_circular_dependency(std::queue<ContentPack*>& queue)
/// @return true if all dependencies are already added or not found (optional/weak) /// @return true if all dependencies are already added or not found (optional/weak)
/// @throws contentpack_error if required dependency is not found /// @throws contentpack_error if required dependency is not found
static bool resolve_dependencies ( static bool resolve_dependencies (
ContentPack* pack, const ContentPack* pack,
std::unordered_map<std::string, ContentPack>& packs, const std::unordered_map<std::string, ContentPack>& packs,
std::vector<std::string>& allNames, std::vector<std::string>& allNames,
std::vector<std::string>& added, std::vector<std::string>& added,
std::queue<ContentPack*>& queue, std::queue<const ContentPack*>& queue,
bool resolveWeaks bool resolveWeaks
) { ) {
bool satisfied = true; bool satisfied = true;
@@ -91,11 +103,11 @@ static bool resolve_dependencies (
return satisfied; return satisfied;
} }
std::vector<std::string> PacksManager::assembly(const std::vector<std::string>& names) { std::vector<std::string> PacksManager::assembly(const std::vector<std::string>& names) const {
std::vector<std::string> allNames = names; std::vector<std::string> allNames = names;
std::vector<std::string> added; std::vector<std::string> added;
std::queue<ContentPack*> queue; std::queue<const ContentPack*> queue;
std::queue<ContentPack*> queue2; std::queue<const ContentPack*> queue2;
for (auto& name : names) { for (auto& name : names) {
auto found = packs.find(name); auto found = packs.find(name);
+7 -2
View File
@@ -23,14 +23,19 @@ public:
void scan(); void scan();
/// @brief Get all found packs /// @brief Get all found packs
std::vector<std::string> getAllNames(); std::vector<std::string> getAllNames() const;
/// @brief Get packs by names (id)
/// @param names pack names
/// @throws contentpack_error if pack not found
std::vector<ContentPack> getAll(const std::vector<std::string>& names) const;
/// @brief Resolve all dependencies and fix packs order /// @brief Resolve all dependencies and fix packs order
/// @param names required packs (method can add extra packs) /// @param names required packs (method can add extra packs)
/// @return resulting ordered vector of pack names /// @return resulting ordered vector of pack names
/// @throws contentpack_error if required dependency not found or /// @throws contentpack_error if required dependency not found or
/// circular dependency detected /// circular dependency detected
std::vector<std::string> assembly(const std::vector<std::string>& names); std::vector<std::string> assembly(const std::vector<std::string>& names) const;
}; };
#endif // CONTENT_PACKS_MANAGER_H_ #endif // CONTENT_PACKS_MANAGER_H_
+35 -4
View File
@@ -11,6 +11,7 @@
#include "content/Content.h" #include "content/Content.h"
#include "content/ContentLoader.h" #include "content/ContentLoader.h"
#include "content/ContentPack.h" #include "content/ContentPack.h"
#include "content/PacksManager.h"
#include "core_defs.h" #include "core_defs.h"
#include "files/engine_paths.h" #include "files/engine_paths.h"
#include "files/files.h" #include "files/files.h"
@@ -27,6 +28,7 @@
#include "graphics/ui/elements/containers.h" #include "graphics/ui/elements/containers.h"
#include "logic/scripting/scripting.h" #include "logic/scripting/scripting.h"
#include "util/platform.h" #include "util/platform.h"
#include "util/listutil.h"
#include "voxels/DefaultWorldGenerator.h" #include "voxels/DefaultWorldGenerator.h"
#include "voxels/FlatWorldGenerator.h" #include "voxels/FlatWorldGenerator.h"
#include "window/Camera.h" #include "window/Camera.h"
@@ -227,6 +229,22 @@ void Engine::loadContent() {
corecontent::setup(&contentBuilder); corecontent::setup(&contentBuilder);
paths->setContentPacks(&contentPacks); paths->setContentPacks(&contentPacks);
std::vector<std::string> names;
for (auto& pack : contentPacks) {
names.push_back(pack.id);
}
// ---------------------------------------------
PacksManager manager;
manager.setSources({
paths->getWorldFolder()/fs::path("content"),
paths->getUserfiles()/fs::path("content"),
paths->getResources()/fs::path("content")
});
manager.scan();
auto allnames = manager.getAllNames();
// ---------------------------------------------
std::vector<fs::path> resRoots; std::vector<fs::path> resRoots;
std::vector<ContentPack> srcPacks = contentPacks; std::vector<ContentPack> srcPacks = contentPacks;
contentPacks.clear(); contentPacks.clear();
@@ -281,15 +299,28 @@ void Engine::loadContent() {
void Engine::loadWorldContent(const fs::path& folder) { void Engine::loadWorldContent(const fs::path& folder) {
contentPacks.clear(); contentPacks.clear();
auto packNames = ContentPack::worldPacksList(folder); auto packNames = ContentPack::worldPacksList(folder);
ContentPack::readPacks(paths, contentPacks, packNames, folder); PacksManager manager;
manager.setSources({
folder/fs::path("content"),
paths->getUserfiles()/fs::path("content"),
paths->getResources()/fs::path("content")
});
manager.scan();
contentPacks = manager.getAll(manager.assembly(packNames));
paths->setWorldFolder(folder); paths->setWorldFolder(folder);
loadContent(); loadContent();
} }
void Engine::loadAllPacks() { void Engine::loadAllPacks() {
auto resdir = paths->getResources(); PacksManager manager;
contentPacks.clear(); manager.setSources({
ContentPack::scan(paths, contentPacks); paths->getWorldFolder()/fs::path("content"),
paths->getUserfiles()/fs::path("content"),
paths->getResources()/fs::path("content")
});
manager.scan();
auto allnames = manager.getAllNames();
contentPacks = manager.getAll(manager.assembly(allnames));
} }
double Engine::getDelta() const { double Engine::getDelta() const {
+1
View File
@@ -3,6 +3,7 @@
#include "../../engine.h" #include "../../engine.h"
#include "../../files/WorldFiles.h" #include "../../files/WorldFiles.h"
#include "../../content/PacksManager.h"
#include "../../graphics/ui/elements/containers.h" #include "../../graphics/ui/elements/containers.h"
#include "../../graphics/ui/elements/controls.h" #include "../../graphics/ui/elements/controls.h"
#include "../../graphics/ui/gui_util.h" #include "../../graphics/ui/gui_util.h"
+11 -2
View File
@@ -2,6 +2,7 @@
#include "menu_commons.h" #include "menu_commons.h"
#include "../../coders/png.h" #include "../../coders/png.h"
#include "../../content/PacksManager.h"
#include "../../content/ContentLUT.h" #include "../../content/ContentLUT.h"
#include "../../engine.h" #include "../../engine.h"
#include "../../files/WorldFiles.h" #include "../../files/WorldFiles.h"
@@ -181,8 +182,16 @@ void create_content_panel(Engine* engine, LevelController* controller) {
auto menu = engine->getGUI()->getMenu(); auto menu = engine->getGUI()->getMenu();
auto mainPanel = menus::create_page(engine, "content", 550, 0.0f, 5); auto mainPanel = menus::create_page(engine, "content", 550, 0.0f, 5);
std::vector<ContentPack> scanned; auto paths = engine->getPaths();
ContentPack::scan(engine->getPaths(), scanned); PacksManager manager;
manager.setSources({
paths->getWorldFolder()/fs::path("content"),
paths->getUserfiles()/fs::path("content"),
paths->getResources()/fs::path("content")
});
manager.scan();
std::vector<ContentPack> scanned = manager.getAll(manager.getAllNames());
for (const auto& pack : engine->getContentPacks()) { for (const auto& pack : engine->getContentPacks()) {
for (size_t i = 0; i < scanned.size(); i++) { for (size_t i = 0; i < scanned.size(); i++) {
if (scanned[i].id == pack.id) { if (scanned[i].id == pack.id) {
+17
View File
@@ -0,0 +1,17 @@
#include "listutil.h"
#include "../util/stringutil.h"
#include <sstream>
std::string util::to_string(const std::vector<std::string>& vec) {
std::stringstream ss;
ss << "[";
for (size_t i = 0; i < vec.size(); i++) {
ss << util::quote(vec.at(i));
if (i < vec.size()-1) {
ss << ", ";
}
}
ss << "]";
return ss.str();
}
+1 -4
View File
@@ -11,10 +11,7 @@ namespace util {
return std::find(vec.begin(), vec.end(), value) != vec.end(); return std::find(vec.begin(), vec.end(), value) != vec.end();
} }
template<class T> std::string to_string(const std::vector<std::string>& vec);
bool contains(const std::queue<T>& queue, const T& value) {
return std::find(queue.begin(), queue.end(), value) != queue.end();
}
} }
#endif // UTIL_LISTUTIL_H_ #endif // UTIL_LISTUTIL_H_