cleanup ContentControl
This commit is contained in:
parent
f99d909f34
commit
3486cbd4c9
@ -30,7 +30,13 @@ ContentControl::ContentControl(
|
||||
: paths(paths),
|
||||
input(input),
|
||||
postContent(std::move(postContent)),
|
||||
basePacks(io::read_list("res:config/builtins.list")) {
|
||||
basePacks(io::read_list("res:config/builtins.list")),
|
||||
manager(std::make_unique<PacksManager>()) {
|
||||
manager->setSources({
|
||||
"world:content",
|
||||
"user:content",
|
||||
"res:content",
|
||||
});
|
||||
}
|
||||
|
||||
ContentControl::~ContentControl() = default;
|
||||
@ -57,29 +63,22 @@ void ContentControl::resetContent() {
|
||||
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)) {
|
||||
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);
|
||||
contentPacks = manager->getAll(basePacks);
|
||||
|
||||
postContent();
|
||||
}
|
||||
|
||||
void ContentControl::loadContent(const std::vector<std::string>& names) {
|
||||
PacksManager manager;
|
||||
manager.setSources(getDefaultSources());
|
||||
manager.scan();
|
||||
contentPacks = manager.getAll(manager.assemble(names));
|
||||
manager->scan();
|
||||
contentPacks = manager->getAll(manager->assemble(names));
|
||||
loadContent();
|
||||
}
|
||||
|
||||
@ -90,12 +89,9 @@ void ContentControl::loadContent() {
|
||||
for (auto& pack : contentPacks) {
|
||||
names.push_back(pack.id);
|
||||
}
|
||||
|
||||
PacksManager manager;
|
||||
manager.setSources(getDefaultSources());
|
||||
manager.scan();
|
||||
names = manager.assemble(names);
|
||||
contentPacks = manager.getAll(names);
|
||||
manager->scan();
|
||||
names = manager->assemble(names);
|
||||
contentPacks = manager->getAll(names);
|
||||
|
||||
std::vector<PathsRoot> entryPoints;
|
||||
for (auto& pack : contentPacks) {
|
||||
@ -130,14 +126,6 @@ void ContentControl::loadContent() {
|
||||
postContent();
|
||||
}
|
||||
|
||||
std::vector<io::path> ContentControl::getDefaultSources() {
|
||||
return {
|
||||
"world:content",
|
||||
"user:content",
|
||||
"res:content",
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<ContentPack>& ContentControl::getContentPacks() {
|
||||
return contentPacks;
|
||||
}
|
||||
@ -147,3 +135,8 @@ std::vector<ContentPack> ContentControl::getAllContentPacks() {
|
||||
packs.insert(packs.begin(), ContentPack::createCore(paths));
|
||||
return packs;
|
||||
}
|
||||
|
||||
PacksManager& ContentControl::scan() {
|
||||
manager->scan();
|
||||
return *manager;
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include "ContentPack.hpp"
|
||||
|
||||
class Content;
|
||||
struct ContentPack;
|
||||
class PacksManager;
|
||||
class EnginePaths;
|
||||
class Input;
|
||||
|
||||
@ -36,14 +36,16 @@ public:
|
||||
|
||||
void loadContent();
|
||||
|
||||
std::vector<io::path> getDefaultSources();
|
||||
std::vector<ContentPack>& getContentPacks();
|
||||
std::vector<ContentPack> getAllContentPacks();
|
||||
|
||||
PacksManager& scan();
|
||||
private:
|
||||
EnginePaths& paths;
|
||||
Input& input;
|
||||
std::unique_ptr<Content> content;
|
||||
std::function<void()> postContent;
|
||||
std::vector<std::string> basePacks;
|
||||
std::unique_ptr<PacksManager> manager;
|
||||
std::vector<ContentPack> contentPacks;
|
||||
};
|
||||
|
||||
@ -15,12 +15,13 @@ namespace fs = std::filesystem;
|
||||
|
||||
ContentPack ContentPack::createCore(const EnginePaths& paths) {
|
||||
return ContentPack {
|
||||
"core", "Core", ENGINE_VERSION_STRING, "", "", "res:", "res:", {}
|
||||
"core", "Core", ENGINE_VERSION_STRING, "", "", "res:", {}
|
||||
};
|
||||
}
|
||||
|
||||
const std::vector<std::string> ContentPack::RESERVED_NAMES = {
|
||||
"res", "abs", "local", "core", "user", "world", "none", "null"};
|
||||
"res", "abs", "local", "core", "user", "world", "none", "null"
|
||||
};
|
||||
|
||||
contentpack_error::contentpack_error(
|
||||
std::string packId, io::path folder, const std::string& message
|
||||
@ -70,7 +71,7 @@ static void checkContentPackId(const std::string& id, const io::path& folder) {
|
||||
}
|
||||
}
|
||||
|
||||
ContentPack ContentPack::read(const std::string& path, const io::path& folder) {
|
||||
ContentPack ContentPack::read(const io::path& folder) {
|
||||
auto root = io::read_json(folder / PACKAGE_FILENAME);
|
||||
ContentPack pack;
|
||||
root.at("id").get(pack.id);
|
||||
@ -90,7 +91,6 @@ ContentPack ContentPack::read(const std::string& path, const io::path& folder) {
|
||||
root.at("description").get(pack.description);
|
||||
root.at("source").get(pack.source);
|
||||
pack.folder = folder;
|
||||
pack.path = path;
|
||||
|
||||
if (auto found = root.at("dependencies")) {
|
||||
const auto& dependencies = *found;
|
||||
@ -133,7 +133,7 @@ void ContentPack::scanFolder(
|
||||
if (!io::is_directory(packFolder)) continue;
|
||||
if (!is_pack(packFolder)) continue;
|
||||
try {
|
||||
packs.push_back(read(packFolder.string(), packFolder));
|
||||
packs.push_back(read(packFolder));
|
||||
} catch (const contentpack_error& err) {
|
||||
std::cerr << "package.json error at " << err.getFolder().string();
|
||||
std::cerr << ": " << err.what() << std::endl;
|
||||
|
||||
@ -43,7 +43,6 @@ struct ContentPack {
|
||||
std::string creator = "";
|
||||
std::string description = "no description";
|
||||
io::path folder;
|
||||
std::string path;
|
||||
std::vector<DependencyPack> dependencies;
|
||||
std::string source = "";
|
||||
|
||||
@ -58,7 +57,7 @@ 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 io::path& folder);
|
||||
|
||||
static void scanFolder(
|
||||
const io::path& folder, std::vector<ContentPack>& packs
|
||||
|
||||
@ -133,16 +133,14 @@ void Engine::initialize(CoreParameters coreParameters) {
|
||||
|
||||
bool langNotSet = settings.ui.language.get() == "auto";
|
||||
if (langNotSet) {
|
||||
settings.ui.language.set(langs::locale_by_envlocale(
|
||||
platform::detect_locale(),
|
||||
"res:"
|
||||
));
|
||||
settings.ui.language.set(
|
||||
langs::locale_by_envlocale(platform::detect_locale())
|
||||
);
|
||||
}
|
||||
content = std::make_unique<ContentControl>(paths, *input, [this]() {
|
||||
langs::setup("res:", langs::get_current(), paths.resPaths.collectRoots());
|
||||
langs::setup(langs::get_current(), paths.resPaths.collectRoots());
|
||||
if (!isHeadless()) {
|
||||
loadAssets();
|
||||
onAssetsLoaded();
|
||||
}
|
||||
});
|
||||
scripting::initialize(this);
|
||||
@ -150,7 +148,7 @@ void Engine::initialize(CoreParameters coreParameters) {
|
||||
gui->setPageLoader(scripting::create_page_loader());
|
||||
}
|
||||
keepAlive(settings.ui.language.observe([this](auto lang) {
|
||||
setLanguage(lang);
|
||||
langs::setup(lang, paths.resPaths.collectRoots());
|
||||
}, true));
|
||||
}
|
||||
|
||||
@ -179,11 +177,6 @@ void Engine::loadControls() {
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::onAssetsLoaded() {
|
||||
assets->setup();
|
||||
gui->onAssetsLoad(assets.get());
|
||||
}
|
||||
|
||||
void Engine::updateHotkeys() {
|
||||
if (input->jpressed(keycode::F2)) {
|
||||
saveScreenshot();
|
||||
@ -319,6 +312,9 @@ void Engine::loadAssets() {
|
||||
if (content) {
|
||||
ModelsGenerator::prepare(*content, *assets);
|
||||
}
|
||||
|
||||
assets->setup();
|
||||
gui->onAssetsLoad(assets.get());
|
||||
}
|
||||
|
||||
void Engine::setScreen(std::shared_ptr<Screen> screen) {
|
||||
@ -328,10 +324,6 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
|
||||
this->screen = std::move(screen);
|
||||
}
|
||||
|
||||
void Engine::setLanguage(std::string locale) {
|
||||
langs::setup("res:", std::move(locale), paths.resPaths.collectRoots());
|
||||
}
|
||||
|
||||
void Engine::onWorldOpen(std::unique_ptr<Level> level, int64_t localPlayer) {
|
||||
logger.info() << "world open";
|
||||
levelConsumer(std::move(level), localPlayer);
|
||||
|
||||
@ -17,12 +17,8 @@ class Window;
|
||||
class Assets;
|
||||
class Level;
|
||||
class Screen;
|
||||
class EnginePaths;
|
||||
class ResPaths;
|
||||
class ContentControl;
|
||||
class EngineController;
|
||||
class SettingsHandler;
|
||||
struct EngineSettings;
|
||||
class Input;
|
||||
|
||||
namespace gui {
|
||||
@ -96,9 +92,6 @@ public:
|
||||
void updateFrontend();
|
||||
void renderFrame();
|
||||
void nextFrame();
|
||||
|
||||
/// @brief Called after assets loading when all engine systems are initialized
|
||||
void onAssetsLoaded();
|
||||
|
||||
/// @brief Set screen (scene).
|
||||
/// nullptr may be used to delete previous screen before creating new one,
|
||||
@ -106,10 +99,6 @@ public:
|
||||
/// @param screen nullable screen
|
||||
void setScreen(std::shared_ptr<Screen> screen);
|
||||
|
||||
/// @brief Change locale to specified
|
||||
/// @param locale isolanguage_ISOCOUNTRY (example: en_US)
|
||||
void setLanguage(std::string locale);
|
||||
|
||||
/// @brief Get active assets storage instance
|
||||
Assets* getAssets();
|
||||
|
||||
|
||||
@ -63,8 +63,8 @@ namespace {
|
||||
};
|
||||
}
|
||||
|
||||
static void load_locales_info(const io::path& resdir, std::string& fallback) {
|
||||
auto file = resdir / langs::TEXTS_FOLDER / "langs.json";
|
||||
static void load_locales_info(std::string& fallback) {
|
||||
auto file = io::path("res:") / langs::TEXTS_FOLDER / "langs.json";
|
||||
auto root = io::read_json(file);
|
||||
|
||||
::locales_info.clear();
|
||||
@ -89,13 +89,12 @@ static void load_locales_info(const io::path& resdir, std::string& fallback) {
|
||||
}
|
||||
|
||||
static void load(
|
||||
const io::path& resdir,
|
||||
const std::string& locale,
|
||||
const std::vector<io::path>& roots,
|
||||
Lang& lang
|
||||
) {
|
||||
io::path filename = io::path(TEXTS_FOLDER) / (locale + LANG_FILE_EXT);
|
||||
io::path core_file = resdir / filename;
|
||||
io::path core_file = io::path("res:") / filename;
|
||||
|
||||
if (io::is_regular_file(core_file)) {
|
||||
std::string text = io::read_string(core_file);
|
||||
@ -112,15 +111,14 @@ static void load(
|
||||
}
|
||||
}
|
||||
static void load(
|
||||
const io::path& resdir,
|
||||
const std::string& locale,
|
||||
const std::string& fallback,
|
||||
const std::vector<io::path>& roots
|
||||
) {
|
||||
auto lang = std::make_unique<Lang>(locale);
|
||||
load(resdir, fallback, roots, *lang.get());
|
||||
load(fallback, roots, *lang.get());
|
||||
if (locale != fallback) {
|
||||
load(resdir, locale, roots, *lang.get());
|
||||
load(locale, roots, *lang.get());
|
||||
}
|
||||
current = std::move(lang);
|
||||
}
|
||||
@ -136,10 +134,10 @@ const std::unordered_map<std::string, LocaleInfo>& langs::get_locales_info() {
|
||||
return ::locales_info;
|
||||
}
|
||||
|
||||
std::string langs::locale_by_envlocale(const std::string& envlocale, const io::path& resdir){
|
||||
std::string langs::locale_by_envlocale(const std::string& envlocale){
|
||||
std::string fallback = FALLBACK_DEFAULT;
|
||||
if (locales_info.size() == 0) {
|
||||
load_locales_info(resdir, fallback);
|
||||
load_locales_info(fallback);
|
||||
}
|
||||
if (locales_info.find(envlocale) != locales_info.end()) {
|
||||
logger.info() << "locale " << envlocale << " is automatically selected";
|
||||
@ -158,16 +156,15 @@ std::string langs::locale_by_envlocale(const std::string& envlocale, const io::p
|
||||
}
|
||||
|
||||
void langs::setup(
|
||||
const io::path& resdir,
|
||||
std::string locale,
|
||||
const std::vector<io::path>& roots
|
||||
) {
|
||||
std::string fallback = langs::FALLBACK_DEFAULT;
|
||||
load_locales_info(resdir, fallback);
|
||||
if (::locales_info.find(locale) == ::locales_info.end()) {
|
||||
load_locales_info(fallback);
|
||||
if (locales_info.find(locale) == locales_info.end()) {
|
||||
locale = fallback;
|
||||
}
|
||||
load(resdir, locale, fallback, roots);
|
||||
load(locale, fallback, roots);
|
||||
}
|
||||
|
||||
const std::wstring& langs::get(const std::wstring& key) {
|
||||
|
||||
@ -39,9 +39,7 @@ namespace langs {
|
||||
std::string name;
|
||||
};
|
||||
|
||||
std::string locale_by_envlocale(
|
||||
const std::string& envlocale, const io::path& resdir
|
||||
);
|
||||
std::string locale_by_envlocale(const std::string& envlocale);
|
||||
|
||||
const std::string& get_current();
|
||||
const std::unordered_map<std::string, LocaleInfo>& get_locales_info();
|
||||
@ -51,9 +49,7 @@ namespace langs {
|
||||
const std::wstring& key, const std::wstring& context
|
||||
);
|
||||
|
||||
void setup(
|
||||
const io::path& resdir,
|
||||
std::string locale,
|
||||
const std::vector<io::path>& roots
|
||||
);
|
||||
/// @brief Change locale to specified
|
||||
/// @param locale isolanguage_ISOCOUNTRY (example: en_US)
|
||||
void setup(std::string locale, const std::vector<io::path>& roots);
|
||||
}
|
||||
|
||||
@ -324,12 +324,10 @@ void EngineController::reconfigPacks(
|
||||
}
|
||||
}
|
||||
|
||||
runnable removeFunc = [this, controller, packsToAdd, packsToRemove]() {
|
||||
runnable removeFunc = [this, controller, packsToAdd, packsToRemove, &contentControl]() {
|
||||
auto& manager = contentControl.scan();
|
||||
if (controller == nullptr) {
|
||||
try {
|
||||
PacksManager manager;
|
||||
manager.setSources(engine.getContentControl().getDefaultSources());
|
||||
manager.scan();
|
||||
auto names = PacksManager::getNames(
|
||||
engine.getContentControl().getContentPacks()
|
||||
);
|
||||
@ -352,10 +350,6 @@ void EngineController::reconfigPacks(
|
||||
auto& wfile = *world->wfile;
|
||||
controller->saveWorld();
|
||||
|
||||
PacksManager manager;
|
||||
manager.setSources(engine.getContentControl().getDefaultSources());
|
||||
manager.scan();
|
||||
|
||||
auto names = PacksManager::getNames(world->getPacks());
|
||||
for (const auto& id : packsToAdd) {
|
||||
names.push_back(id);
|
||||
|
||||
@ -45,10 +45,7 @@ static int l_pack_get_installed(lua::State* L) {
|
||||
|
||||
/// @brief pack.get_available() -> array<string>
|
||||
static int l_pack_get_available(lua::State* L) {
|
||||
PacksManager manager;
|
||||
manager.setSources(content_control->getDefaultSources());
|
||||
manager.scan();
|
||||
|
||||
auto& manager = content_control->scan();
|
||||
const auto& installed = content_control->getContentPacks();
|
||||
for (auto& pack : installed) {
|
||||
manager.exclude(pack.id);
|
||||
@ -83,7 +80,7 @@ static int l_pack_get_info(
|
||||
lua::pushstring(L, pack.version);
|
||||
lua::setfield(L, "version");
|
||||
|
||||
lua::pushstring(L, pack.path);
|
||||
lua::pushstring(L, pack.folder.string());
|
||||
lua::setfield(L, "path");
|
||||
|
||||
if (!engine->isHeadless()) {
|
||||
@ -147,9 +144,7 @@ static int pack_get_infos(lua::State* L) {
|
||||
}
|
||||
}
|
||||
if (!ids.empty()) {
|
||||
PacksManager manager;
|
||||
manager.setSources(content_control->getDefaultSources());
|
||||
manager.scan();
|
||||
auto& manager = content_control->scan();
|
||||
auto vec =
|
||||
manager.getAll(std::vector<std::string>(ids.begin(), ids.end()));
|
||||
for (const auto& pack : vec) {
|
||||
@ -185,13 +180,7 @@ static int l_pack_get_info(lua::State* L) {
|
||||
return pack.id == packid;
|
||||
});
|
||||
if (found == packs.end()) {
|
||||
io::path worldFolder;
|
||||
if (level) {
|
||||
worldFolder = level->getWorld()->wfile->getFolder();
|
||||
}
|
||||
PacksManager manager;
|
||||
manager.setSources(content_control->getDefaultSources());
|
||||
manager.scan();
|
||||
auto& manager = content_control->scan();
|
||||
auto vec = manager.getAll({packid});
|
||||
if (!vec.empty()) {
|
||||
return l_pack_get_info(L, vec[0], content);
|
||||
@ -223,13 +212,7 @@ static int l_pack_assemble(lua::State* L) {
|
||||
ids.push_back(lua::require_string(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
io::path worldFolder;
|
||||
if (level) {
|
||||
worldFolder = level->getWorld()->wfile->getFolder();
|
||||
}
|
||||
PacksManager manager;
|
||||
manager.setSources(content_control->getDefaultSources());
|
||||
manager.scan();
|
||||
auto& manager = content_control->scan();
|
||||
try {
|
||||
ids = manager.assemble(ids);
|
||||
} catch (const contentpack_error& err) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user