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

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) {
fs::path listfile = folder / fs::path("packs.list");
if (!fs::is_regular_file(listfile)) {
@ -154,20 +139,6 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std:
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(
ContentPack info,
std::unique_ptr<scripting::Environment> env

View File

@ -63,16 +63,6 @@ struct ContentPack {
fs::path folder,
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);
@ -81,13 +71,6 @@ struct ContentPack {
fs::path worldDir,
std::string name
);
static void readPacks(
const EnginePaths* paths,
std::vector<ContentPack>& packs,
const std::vector<std::string>& names,
fs::path worldDir
);
};
struct ContentPackStats {

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;
for (auto& entry : packs) {
names.push_back(entry.first);
@ -32,8 +32,20 @@ std::vector<std::string> PacksManager::getAllNames() {
return names;
}
static contentpack_error on_circular_dependency(std::queue<ContentPack*>& queue) {
ContentPack* lastPack = queue.back();
std::vector<ContentPack> PacksManager::getAll(const std::vector<std::string>& names) const {
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
std::stringstream ss;
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)
/// @throws contentpack_error if required dependency is not found
static bool resolve_dependencies (
ContentPack* pack,
std::unordered_map<std::string, ContentPack>& packs,
const ContentPack* pack,
const std::unordered_map<std::string, ContentPack>& packs,
std::vector<std::string>& allNames,
std::vector<std::string>& added,
std::queue<ContentPack*>& queue,
std::queue<const ContentPack*>& queue,
bool resolveWeaks
) {
bool satisfied = true;
@ -91,11 +103,11 @@ static bool resolve_dependencies (
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> added;
std::queue<ContentPack*> queue;
std::queue<ContentPack*> queue2;
std::queue<const ContentPack*> queue;
std::queue<const ContentPack*> queue2;
for (auto& name : names) {
auto found = packs.find(name);

View File

@ -23,14 +23,19 @@ public:
void scan();
/// @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
/// @param names required packs (method can add extra packs)
/// @return resulting ordered vector of pack names
/// @throws contentpack_error if required dependency not found or
/// 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_

View File

@ -11,6 +11,7 @@
#include "content/Content.h"
#include "content/ContentLoader.h"
#include "content/ContentPack.h"
#include "content/PacksManager.h"
#include "core_defs.h"
#include "files/engine_paths.h"
#include "files/files.h"
@ -27,6 +28,7 @@
#include "graphics/ui/elements/containers.h"
#include "logic/scripting/scripting.h"
#include "util/platform.h"
#include "util/listutil.h"
#include "voxels/DefaultWorldGenerator.h"
#include "voxels/FlatWorldGenerator.h"
#include "window/Camera.h"
@ -227,6 +229,22 @@ void Engine::loadContent() {
corecontent::setup(&contentBuilder);
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<ContentPack> srcPacks = contentPacks;
contentPacks.clear();
@ -281,15 +299,28 @@ void Engine::loadContent() {
void Engine::loadWorldContent(const fs::path& folder) {
contentPacks.clear();
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);
loadContent();
}
void Engine::loadAllPacks() {
auto resdir = paths->getResources();
contentPacks.clear();
ContentPack::scan(paths, contentPacks);
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();
contentPacks = manager.getAll(manager.assembly(allnames));
}
double Engine::getDelta() const {

View File

@ -3,6 +3,7 @@
#include "../../engine.h"
#include "../../files/WorldFiles.h"
#include "../../content/PacksManager.h"
#include "../../graphics/ui/elements/containers.h"
#include "../../graphics/ui/elements/controls.h"
#include "../../graphics/ui/gui_util.h"

View File

@ -2,6 +2,7 @@
#include "menu_commons.h"
#include "../../coders/png.h"
#include "../../content/PacksManager.h"
#include "../../content/ContentLUT.h"
#include "../../engine.h"
#include "../../files/WorldFiles.h"
@ -181,8 +182,16 @@ void create_content_panel(Engine* engine, LevelController* controller) {
auto menu = engine->getGUI()->getMenu();
auto mainPanel = menus::create_page(engine, "content", 550, 0.0f, 5);
std::vector<ContentPack> scanned;
ContentPack::scan(engine->getPaths(), scanned);
auto paths = engine->getPaths();
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 (size_t i = 0; i < scanned.size(); i++) {
if (scanned[i].id == pack.id) {

17
src/util/listutil.cpp Normal file
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();
}

View File

@ -11,10 +11,7 @@ namespace util {
return std::find(vec.begin(), vec.end(), value) != vec.end();
}
template<class T>
bool contains(const std::queue<T>& queue, const T& value) {
return std::find(queue.begin(), queue.end(), value) != queue.end();
}
std::string to_string(const std::vector<std::string>& vec);
}
#endif // UTIL_LISTUTIL_H_