refactor: add ContentControl class
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
#include "ContentControl.hpp"
|
||||
|
||||
#include "io/io.hpp"
|
||||
#include "io/engine_paths.hpp"
|
||||
#include "coders/toml.hpp"
|
||||
#include "Content.hpp"
|
||||
#include "ContentPack.hpp"
|
||||
#include "ContentBuilder.hpp"
|
||||
#include "ContentLoader.hpp"
|
||||
#include "PacksManager.hpp"
|
||||
#include "objects/rigging.hpp"
|
||||
#include "logic/scripting/scripting.hpp"
|
||||
#include "window/input.hpp"
|
||||
#include "core_defs.hpp"
|
||||
|
||||
static void load_configs(Input& input, const io::path& root) {
|
||||
auto configFolder = root / "config";
|
||||
auto bindsFile = configFolder / "bindings.toml";
|
||||
if (io::is_regular_file(bindsFile)) {
|
||||
input.getBindings().read(
|
||||
toml::parse(bindsFile.string(), io::read_string(bindsFile)),
|
||||
BindType::BIND
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
ContentControl::ContentControl(std::function<void()> postContent)
|
||||
: postContent(std::move(postContent)) {
|
||||
basePacks = io::read_list("res:config/builtins.list");
|
||||
}
|
||||
|
||||
ContentControl::~ContentControl() = default;
|
||||
|
||||
Content* ContentControl::get() {
|
||||
return content.get();
|
||||
}
|
||||
|
||||
std::vector<std::string>& ContentControl::getBasePacks() {
|
||||
return basePacks;
|
||||
}
|
||||
|
||||
void ContentControl::resetContent(
|
||||
EnginePaths& paths, Input& input, std::vector<ContentPack>& contentPacks
|
||||
) {
|
||||
scripting::cleanup();
|
||||
std::vector<PathsRoot> resRoots;
|
||||
{
|
||||
auto pack = ContentPack::createCore(paths);
|
||||
resRoots.push_back({"core", pack.folder});
|
||||
load_configs(input, pack.folder);
|
||||
}
|
||||
PacksManager manager;
|
||||
manager.setSources({
|
||||
"user:content",
|
||||
"res:content",
|
||||
});
|
||||
manager.scan();
|
||||
for (const auto& pack : manager.getAll(basePacks)) {
|
||||
resRoots.push_back({pack.id, pack.folder});
|
||||
}
|
||||
paths.resPaths = ResPaths(resRoots);
|
||||
content.reset();
|
||||
|
||||
contentPacks.clear();
|
||||
contentPacks = manager.getAll(basePacks);
|
||||
|
||||
postContent();
|
||||
}
|
||||
|
||||
void ContentControl::loadContent(
|
||||
EnginePaths& paths,
|
||||
Input& input,
|
||||
std::vector<ContentPack>& packs,
|
||||
const std::vector<std::string>& names
|
||||
) {
|
||||
PacksManager manager;
|
||||
manager.setSources(getDefaultSources());
|
||||
manager.scan();
|
||||
packs = manager.getAll(manager.assemble(names));
|
||||
loadContent(paths, input, packs);
|
||||
}
|
||||
|
||||
void ContentControl::loadContent(
|
||||
EnginePaths& paths,
|
||||
Input& input,
|
||||
std::vector<ContentPack>& contentPacks
|
||||
) {
|
||||
scripting::cleanup();
|
||||
|
||||
std::vector<std::string> names;
|
||||
for (auto& pack : contentPacks) {
|
||||
names.push_back(pack.id);
|
||||
}
|
||||
|
||||
PacksManager manager;
|
||||
manager.setSources(getDefaultSources());
|
||||
manager.scan();
|
||||
names = manager.assemble(names);
|
||||
contentPacks = manager.getAll(names);
|
||||
|
||||
std::vector<PathsRoot> entryPoints;
|
||||
for (auto& pack : contentPacks) {
|
||||
entryPoints.emplace_back(pack.id, pack.folder);
|
||||
}
|
||||
paths.setEntryPoints(std::move(entryPoints));
|
||||
|
||||
ContentBuilder contentBuilder;
|
||||
corecontent::setup(input, contentBuilder);
|
||||
|
||||
auto corePack = ContentPack::createCore(paths);
|
||||
|
||||
auto allPacks = contentPacks;
|
||||
allPacks.insert(allPacks.begin(), corePack);
|
||||
|
||||
// Setup filesystem entry points
|
||||
std::vector<PathsRoot> resRoots;
|
||||
for (auto& pack : allPacks) {
|
||||
resRoots.push_back({pack.id, pack.folder});
|
||||
}
|
||||
paths.resPaths = ResPaths(resRoots);
|
||||
// Load content
|
||||
for (auto& pack : allPacks) {
|
||||
ContentLoader(&pack, contentBuilder, paths.resPaths).load();
|
||||
load_configs(input, pack.folder);
|
||||
}
|
||||
content = contentBuilder.build();
|
||||
scripting::on_content_load(content.get());
|
||||
|
||||
ContentLoader::loadScripts(*content);
|
||||
|
||||
postContent();
|
||||
}
|
||||
|
||||
std::vector<io::path> ContentControl::getDefaultSources() {
|
||||
return {
|
||||
"world:content",
|
||||
"user:content",
|
||||
"res:content",
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
class Content;
|
||||
struct ContentPack;
|
||||
class EnginePaths;
|
||||
class Input;
|
||||
|
||||
namespace io {
|
||||
class path;
|
||||
}
|
||||
|
||||
class ContentControl {
|
||||
public:
|
||||
ContentControl(std::function<void()> postContent);
|
||||
~ContentControl();
|
||||
|
||||
Content* get();
|
||||
|
||||
std::vector<std::string>& getBasePacks();
|
||||
|
||||
void resetContent(
|
||||
EnginePaths& paths, Input& input, std::vector<ContentPack>& packs
|
||||
);
|
||||
|
||||
void loadContent(
|
||||
EnginePaths& paths,
|
||||
Input& input,
|
||||
std::vector<ContentPack>& packs,
|
||||
const std::vector<std::string>& names
|
||||
);
|
||||
|
||||
void loadContent(
|
||||
EnginePaths& paths,
|
||||
Input& input,
|
||||
std::vector<ContentPack>& packs
|
||||
);
|
||||
|
||||
std::vector<io::path> getDefaultSources();
|
||||
private:
|
||||
std::unique_ptr<Content> content;
|
||||
std::vector<std::string> basePacks;
|
||||
std::function<void()> postContent;
|
||||
};
|
||||
@@ -124,7 +124,7 @@ ContentPack ContentPack::read(const std::string& path, const io::path& folder) {
|
||||
}
|
||||
|
||||
void ContentPack::scanFolder(
|
||||
const std::string& path, const io::path& folder, std::vector<ContentPack>& packs
|
||||
const io::path& folder, std::vector<ContentPack>& packs
|
||||
) {
|
||||
if (!io::is_directory(folder)) {
|
||||
return;
|
||||
@@ -133,9 +133,7 @@ void ContentPack::scanFolder(
|
||||
if (!io::is_directory(packFolder)) continue;
|
||||
if (!is_pack(packFolder)) continue;
|
||||
try {
|
||||
packs.push_back(
|
||||
read(path + "/" + packFolder.name(), packFolder)
|
||||
);
|
||||
packs.push_back(read(packFolder.string(), packFolder));
|
||||
} catch (const contentpack_error& err) {
|
||||
std::cerr << "package.json error at " << err.getFolder().string();
|
||||
std::cerr << ": " << err.what() << std::endl;
|
||||
|
||||
@@ -58,14 +58,10 @@ struct ContentPack {
|
||||
static const std::vector<std::string> RESERVED_NAMES;
|
||||
|
||||
static bool is_pack(const io::path& folder);
|
||||
static ContentPack read(
|
||||
const std::string& path, const io::path& folder
|
||||
);
|
||||
static ContentPack read(const std::string& path, const io::path& folder);
|
||||
|
||||
static void scanFolder(
|
||||
const std::string& path,
|
||||
const io::path& folder,
|
||||
std::vector<ContentPack>& packs
|
||||
const io::path& folder, std::vector<ContentPack>& packs
|
||||
);
|
||||
|
||||
static std::vector<std::string> worldPacksList(
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
PacksManager::PacksManager() = default;
|
||||
|
||||
void PacksManager::setSources(std::vector<std::pair<std::string, io::path>> sources) {
|
||||
void PacksManager::setSources(std::vector<io::path> sources) {
|
||||
this->sources = std::move(sources);
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ void PacksManager::scan() {
|
||||
packs.clear();
|
||||
|
||||
std::vector<ContentPack> packsList;
|
||||
for (auto& [path, folder] : sources) {
|
||||
ContentPack::scanFolder(path, folder, packsList);
|
||||
for (auto& folder : sources) {
|
||||
ContentPack::scanFolder(folder, packsList);
|
||||
for (auto& pack : packsList) {
|
||||
packs.try_emplace(pack.id, pack);
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
|
||||
class PacksManager {
|
||||
std::unordered_map<std::string, ContentPack> packs;
|
||||
std::vector<std::pair<std::string, io::path>> sources;
|
||||
std::vector<io::path> sources;
|
||||
public:
|
||||
PacksManager();
|
||||
|
||||
/// @brief Set content packs sources (search folders)
|
||||
void setSources(std::vector<std::pair<std::string, io::path>> sources);
|
||||
void setSources(std::vector<io::path> sources);
|
||||
|
||||
/// @brief Scan sources and collect all found packs excluding duplication.
|
||||
/// Scanning order depends on sources order
|
||||
|
||||
Reference in New Issue
Block a user