refactor io/engine_paths

This commit is contained in:
MihailRis 2025-03-18 19:15:49 +03:00
parent 611c6fa444
commit dbe9ca5efe
4 changed files with 51 additions and 34 deletions

View File

@ -346,15 +346,20 @@ void Engine::loadContent() {
names.push_back(pack.id);
}
ContentBuilder contentBuilder;
corecontent::setup(*input, contentBuilder);
paths.setContentPacks(&contentPacks);
PacksManager manager = createPacksManager(paths.getCurrentWorldFolder());
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);
// Setup filesystem entry points
@ -443,7 +448,12 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
}
void Engine::setLanguage(std::string locale) {
langs::setup("res:", std::move(locale), resPaths->collectRoots());
langs::setup(
"res:",
std::move(locale),
resPaths ? resPaths->collectRoots()
: std::vector<io::path> {{"core", "res:"}}
);
}
void Engine::onWorldOpen(std::unique_ptr<Level> level, int64_t localPlayer) {

View File

@ -2,7 +2,6 @@
#include <algorithm>
#include <array>
#include <filesystem>
#include <sstream>
#include <stack>
#include "typedefs.hpp"
@ -192,15 +191,15 @@ void EnginePaths::unmount(const std::string& name) {
mounted.erase(found);
}
std::string EnginePaths::createWriteablePackDevice(const std::string& name) {
const auto& found = writeablePacks.find(name);
if (found != writeablePacks.end()) {
std::string EnginePaths::createWriteableDevice(const std::string& name) {
const auto& found = writeables.find(name);
if (found != writeables.end()) {
return found->second;
}
io::path folder;
for (const auto& pack : *contentPacks) {
if (pack.id == name) {
folder = pack.folder;
for (const auto& point : entryPoints) {
if (point.name == name) {
folder = point.path;
break;
}
}
@ -209,29 +208,33 @@ std::string EnginePaths::createWriteablePackDevice(const std::string& name) {
}
auto entryPoint = std::string("W.") + generate_random_base64<6>();
io::create_subdevice(entryPoint, folder.entryPoint(), folder.pathPart());
writeablePacks[name] = entryPoint;
writeables[name] = entryPoint;
return entryPoint;
}
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
void EnginePaths::cleanup() {
// Remove previous content entry-points
for (const auto& id : contentEntryPoints) {
for (const auto& [id, _] : entryPoints) {
io::remove_device(id);
}
for (const auto& [_, entryPoint] : writeablePacks) {
for (const auto& [_, entryPoint] : writeables) {
io::remove_device(entryPoint);
}
for (const auto& entryPoint : mounted) {
io::remove_device(entryPoint);
}
contentEntryPoints.clear();
this->contentPacks = contentPacks;
// Create content devices
for (const auto& pack : *contentPacks) {
auto parent = pack.folder.entryPoint();
io::create_subdevice(pack.id, parent, pack.folder);
contentEntryPoints.push_back(pack.id);
entryPoints.clear();
}
void EnginePaths::setEntryPoints(std::vector<PathsRoot> entryPoints) {
cleanup();
// Create sub-devices
for (const auto& point : entryPoints) {
auto parent = point.path.entryPoint();
io::create_subdevice(point.name, parent, point.path);
}
this->entryPoints = std::move(entryPoints);
}
std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view path) {

View File

@ -9,7 +9,15 @@
#include "io.hpp"
#include "data/dv.hpp"
#include "content/ContentPack.hpp"
struct PathsRoot {
std::string name;
io::path path;
PathsRoot(std::string name, io::path path)
: name(std::move(name)), path(std::move(path)) {
}
};
class EnginePaths {
public:
@ -37,9 +45,9 @@ public:
std::string mount(const io::path& file);
void unmount(const std::string& name);
std::string createWriteablePackDevice(const std::string& name);
std::string createWriteableDevice(const std::string& name);
void setContentPacks(std::vector<ContentPack>* contentPacks);
void setEntryPoints(std::vector<PathsRoot> entryPoints);
std::vector<io::path> scanForWorlds() const;
@ -51,15 +59,11 @@ private:
std::filesystem::path resourcesFolder {"res"};
io::path currentWorldFolder;
std::optional<std::filesystem::path> scriptFolder;
std::vector<ContentPack>* contentPacks = nullptr;
std::vector<std::string> contentEntryPoints;
std::unordered_map<std::string, std::string> writeablePacks;
std::vector<PathsRoot> entryPoints;
std::unordered_map<std::string, std::string> writeables;
std::vector<std::string> mounted;
};
struct PathsRoot {
std::string name;
io::path path;
void cleanup();
};
class ResPaths {

View File

@ -258,7 +258,7 @@ static int l_pack_request_writeable(lua::State* L) {
auto str = langs::get(L"Grant %{0} pack modification permission?");
util::replaceAll(str, L"%{0}", util::str2wstr_utf8(packid));
guiutil::confirm(*engine, str, [packid, handler]() {
handler({engine->getPaths().createWriteablePackDevice(packid)});
handler({engine->getPaths().createWriteableDevice(packid)});
engine->getGUI().getMenu()->reset();
});
return 0;