refactor: move and rename EnginePaths

This commit is contained in:
MihailRis
2025-11-09 20:13:59 +03:00
parent 3b583d4dd6
commit 17ffa73ce7
30 changed files with 57 additions and 63 deletions
+9 -8
View File
@@ -37,6 +37,7 @@
#include "Mainloop.hpp"
#include "ServerMainloop.hpp"
#include "WindowControl.hpp"
#include "EnginePaths.hpp"
#include <iostream>
#include <assert.h>
@@ -61,7 +62,7 @@ Engine& Engine::getInstance() {
void Engine::onContentLoad() {
editor->loadTools();
langs::setup(langs::get_current(), paths.resPaths.collectRoots());
langs::setup(langs::get_current(), paths->resPaths.collectRoots());
if (isHeadless()) {
return;
@@ -130,7 +131,7 @@ void Engine::initialize(CoreParameters coreParameters) {
if (params.projectFolder.empty()) {
params.projectFolder = params.resFolder;
}
paths.prepare(params);
paths = std::make_unique<EnginePaths>(params);
loadProject();
editor = std::make_unique<devtools::Editor>(*this);
@@ -161,7 +162,7 @@ void Engine::initialize(CoreParameters coreParameters) {
langs::locale_by_envlocale(platform::detect_locale())
);
}
content = std::make_unique<ContentControl>(*project, paths, *input, [this]() {
content = std::make_unique<ContentControl>(*project, *paths, *input, [this]() {
onContentLoad();
});
scripting::initialize(this);
@@ -170,7 +171,7 @@ void Engine::initialize(CoreParameters coreParameters) {
gui->setPageLoader(scripting::create_page_loader());
}
keepAlive(settings.ui.language.observe([this](auto lang) {
langs::setup(lang, paths.resPaths.collectRoots());
langs::setup(lang, paths->resPaths.collectRoots());
}, true));
project->loadProjectStartScript();
@@ -345,12 +346,12 @@ void Engine::setLevelConsumer(OnWorldOpen levelConsumer) {
void Engine::loadAssets() {
logger.info() << "loading assets";
Shader::preprocessor->setPaths(&paths.resPaths);
Shader::preprocessor->setPaths(&paths->resPaths);
auto content = this->content->get();
auto new_assets = std::make_unique<Assets>();
AssetsLoader loader(*this, *new_assets, paths.resPaths);
AssetsLoader loader(*this, *new_assets, paths->resPaths);
AssetsLoader::addDefaults(loader, content);
// no need
@@ -426,11 +427,11 @@ Assets* Engine::getAssets() {
}
EnginePaths& Engine::getPaths() {
return paths;
return *paths;
}
ResPaths& Engine::getResPaths() {
return paths.resPaths;
return paths->resPaths;
}
std::shared_ptr<Screen> Engine::getScreen() {
+3 -3
View File
@@ -4,7 +4,6 @@
#include "PostRunnables.hpp"
#include "Time.hpp"
#include "delegates.hpp"
#include "io/engine_paths.hpp"
#include "settings.hpp"
#include "typedefs.hpp"
#include "util/ObjectsKeeper.hpp"
@@ -15,8 +14,10 @@
class Assets;
class ContentControl;
class EngineController;
class EnginePaths;
class Input;
class Level;
class ResPaths;
class Screen;
class SettingsHandler;
class Window;
@@ -50,8 +51,7 @@ using OnWorldOpen = std::function<void(std::unique_ptr<Level>, int64_t)>;
class Engine : public util::ObjectsKeeper {
CoreParameters params;
EngineSettings settings;
EnginePaths paths;
std::unique_ptr<EnginePaths> paths;
std::unique_ptr<Project> project;
std::unique_ptr<SettingsHandler> settingsHandler;
std::unique_ptr<Assets> assets;
+339
View File
@@ -0,0 +1,339 @@
#include "EnginePaths.hpp"
#include "debug/Logger.hpp"
#include "io/devices/StdfsDevice.hpp"
#include "io/devices/ZipFileDevice.hpp"
#include "maths/util.hpp"
#include "typedefs.hpp"
#include "util/platform.hpp"
#include "util/random.hpp"
#include "util/stringutil.hpp"
#include "world/files/WorldFiles.hpp"
#include <algorithm>
#include <array>
#include <chrono>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <utility>
namespace fs = std::filesystem;
static std::random_device random_device;
static inline io::path SCREENSHOTS_FOLDER = "user:screenshots";
static inline io::path CONTENT_FOLDER = "user:content";
static inline io::path WORLDS_FOLDER = "user:worlds";
static debug::Logger logger("engine-paths");
template<int n>
static std::string generate_random_base64() {
auto randomEngine = util::seeded_random_engine(random_device);
static std::uniform_int_distribution<integer_t> dist(0, 0xFF);
ubyte bytes[n];
for (size_t i = 0; i < n; i++) {
bytes[i] = dist(randomEngine);
}
return util::base64_urlsafe_encode(bytes, n);
}
EnginePaths::EnginePaths(CoreParameters& params)
: resourcesFolder(params.resFolder),
userFilesFolder(params.userFolder),
projectFolder(params.projectFolder) {
if (!params.scriptFile.empty()) {
scriptFolder = params.scriptFile.parent_path();
io::set_device("script", std::make_shared<io::StdfsDevice>(*scriptFolder));
}
io::set_device("res", std::make_shared<io::StdfsDevice>(resourcesFolder, false));
io::set_device("user", std::make_shared<io::StdfsDevice>(userFilesFolder));
io::set_device("project", std::make_shared<io::StdfsDevice>(projectFolder));
if (!io::is_directory("res:")) {
throw std::runtime_error(
resourcesFolder.string() + " is not a directory"
);
}
logger.info() << "executable path: " << platform::get_executable_path().string();
logger.info() << "resources folder: " << fs::canonical(resourcesFolder).u8string();
logger.info() << "user files folder: " << fs::canonical(userFilesFolder).u8string();
logger.info() << "project folder: " << fs::canonical(projectFolder).u8string();
if (!io::is_directory(CONTENT_FOLDER)) {
io::create_directories(CONTENT_FOLDER);
}
io::create_subdevice("core", "res", "");
io::create_subdevice("export", "user", "export");
io::create_subdevice("config", "user", "config");
}
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) const {
auto folder = SCREENSHOTS_FOLDER;
if (!io::is_directory(folder)) {
io::create_directories(folder);
}
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
const char* format = "%Y-%m-%d_%H-%M-%S";
std::stringstream ss;
ss << std::put_time(&tm, format);
std::string datetimestr = ss.str();
auto file = folder / ("screenshot-" + datetimestr + "." + ext);
uint index = 0;
while (io::exists(file)) {
file = folder / ("screenshot-" + datetimestr + "-" +
std::to_string(index) + "." + ext);
index++;
}
return file;
}
io::path EnginePaths::getWorldsFolder() const {
return WORLDS_FOLDER;
}
io::path EnginePaths::getWorldFolderByName(const std::string& name) {
return getWorldsFolder() / name;
}
std::vector<io::path> EnginePaths::scanForWorlds() const {
std::vector<io::path> folders;
auto folder = getWorldsFolder();
if (!io::is_directory(folder)) return folders;
for (const auto& worldFolder : io::directory_iterator(folder)) {
if (!io::is_directory(worldFolder)) {
continue;
}
auto worldFile = worldFolder / WorldFiles::WORLD_FILE;
if (!io::is_regular_file(worldFile)) {
continue;
}
folders.push_back(worldFolder);
}
std::sort(
folders.begin(),
folders.end(),
[](io::path a, io::path b) {
a = a / WorldFiles::WORLD_FILE;
b = b / WorldFiles::WORLD_FILE;
return fs::last_write_time(io::resolve(a)) >
fs::last_write_time(io::resolve(b));
}
);
return folders;
}
void EnginePaths::setCurrentWorldFolder(io::path folder) {
if (folder.empty()) {
io::remove_device("world");
} else {
io::create_subdevice("world", "user", folder);
}
this->currentWorldFolder = std::move(folder);
}
std::string EnginePaths::mount(const io::path& file) {
if (file.extension() == ".zip") {
auto stream = io::read(file);
auto device = std::make_unique<io::ZipFileDevice>(
std::move(stream), [file]() { return io::read(file); }
);
std::string name;
do {
name = std::string("M.") + generate_random_base64<6>();
} while (std::find(mounted.begin(), mounted.end(), name) != mounted.end());
io::set_device(name, std::move(device));
mounted.push_back(name);
return name;
}
throw std::runtime_error("unable to mount " + file.string());
}
void EnginePaths::unmount(const std::string& name) {
const auto& found = std::find(mounted.begin(), mounted.end(), name);
if (found == mounted.end()) {
throw std::runtime_error(name + " is not mounted");
}
io::remove_device(name);
mounted.erase(found);
}
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& point : entryPoints) {
if (point.name == name) {
folder = point.path;
break;
}
}
if (name == "core") {
folder = "res:";
}
if (folder.emptyOrInvalid()) {
throw std::runtime_error("pack not found");
}
auto entryPoint = std::string("W.") + generate_random_base64<6>();
io::create_subdevice(entryPoint, folder.entryPoint(), folder.pathPart());
writeables[name] = entryPoint;
return entryPoint;
}
void EnginePaths::cleanup() {
// Remove previous content entry-points
for (const auto& [id, _] : entryPoints) {
io::remove_device(id);
}
for (const auto& [_, entryPoint] : writeables) {
io::remove_device(entryPoint);
}
for (const auto& entryPoint : mounted) {
io::remove_device(entryPoint);
}
entryPoints.clear();
writeables.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) {
size_t separator = path.find(':');
if (separator == std::string::npos) {
return {"", std::string(path)};
}
auto prefix = std::string(path.substr(0, separator));
auto filename = std::string(path.substr(separator + 1));
return {prefix, filename};
}
ResPaths::ResPaths(std::vector<PathsRoot> roots)
: roots(std::move(roots)) {
}
io::path ResPaths::find(const std::string& filename) const {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
auto file = root.path / filename;
if (io::exists(file)) {
return file;
}
}
return io::path("res:") / filename;
}
std::string ResPaths::findRaw(const std::string& filename) const {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
if (io::exists(root.path / filename)) {
return root.name + ":" + filename;
}
}
throw std::runtime_error("could not to find file " + util::quote(filename));
}
std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) const {
std::vector<std::string> entries;
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
auto folder = root.path / folderName;
if (!io::is_directory(folder)) continue;
for (const auto& file : io::directory_iterator(folder)) {
entries.emplace_back(root.name + ":" + folderName + "/" + file.name());
}
}
return entries;
}
std::vector<io::path> ResPaths::listdir(
const std::string& folderName
) const {
std::vector<io::path> entries;
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
io::path folder = root.path / folderName;
if (!io::is_directory(folder)) continue;
for (const auto& entry : io::directory_iterator(folder)) {
entries.push_back(entry);
}
}
return entries;
}
dv::value ResPaths::readCombinedList(const std::string& filename) const {
dv::value list = dv::list();
for (const auto& root : roots) {
auto path = root.path / filename;
if (!io::exists(path)) {
continue;
}
try {
auto value = io::read_object(path);
if (!value.isList()) {
logger.warning() << "reading combined list " << root.name << ":"
<< filename << " is not a list (skipped)";
continue;
}
for (const auto& elem : value) {
list.add(elem);
}
} catch (const std::runtime_error& err) {
logger.warning() << "reading combined list " << root.name << ":"
<< filename << ": " << err.what();
}
}
return list;
}
dv::value ResPaths::readCombinedObject(const std::string& filename, bool deep) const {
dv::value object = dv::object();
for (const auto& root : roots) {
auto path = root.path / filename;
if (!io::exists(path)) {
continue;
}
try {
auto value = io::read_object(path);
if (!value.isObject()) {
logger.warning()
<< "reading combined object " << root.name << ": "
<< filename << " is not an object (skipped)";
}
object.merge(std::move(value), deep);
} catch (const std::runtime_error& err) {
logger.warning() << "reading combined object " << root.name << ":"
<< filename << ": " << err.what();
}
}
return object;
}
std::vector<io::path> ResPaths::collectRoots() {
std::vector<io::path> collected;
collected.reserve(roots.size());
for (const auto& root : roots) {
collected.emplace_back(root.path);
}
return collected;
}
+82
View File
@@ -0,0 +1,82 @@
#pragma once
#include "io/io.hpp"
#include "data/dv.hpp"
#include "CoreParameters.hpp"
#include <unordered_map>
#include <optional>
#include <string>
#include <vector>
#include <tuple>
struct PathsRoot {
std::string name;
io::path path;
PathsRoot(std::string name, io::path path)
: name(std::move(name)), path(std::move(path)) {
}
};
class ResPaths {
public:
ResPaths() = default;
ResPaths(std::vector<PathsRoot> roots);
io::path find(const std::string& filename) const;
std::string findRaw(const std::string& filename) const;
std::vector<io::path> listdir(const std::string& folder) const;
std::vector<std::string> listdirRaw(const std::string& folder) const;
/// @brief Read all found list versions from all packs and combine into a
/// single list. Invalid versions will be skipped with logging a warning
/// @param file *.json file path relative to entry point
dv::value readCombinedList(const std::string& file) const;
dv::value readCombinedObject(const std::string& file, bool deep=false) const;
std::vector<io::path> collectRoots();
private:
std::vector<PathsRoot> roots;
};
class EnginePaths {
public:
ResPaths resPaths;
EnginePaths(CoreParameters& params);
io::path getWorldFolderByName(const std::string& name);
io::path getWorldsFolder() const;
void setCurrentWorldFolder(io::path folder);
io::path getNewScreenshotFile(const std::string& ext) const;
std::string mount(const io::path& file);
void unmount(const std::string& name);
std::string createWriteableDevice(const std::string& name);
void setEntryPoints(std::vector<PathsRoot> entryPoints);
std::vector<io::path> scanForWorlds() const;
static std::tuple<std::string, std::string> parsePath(std::string_view view);
static inline io::path CONFIG_DEFAULTS = "config/defaults.toml";
static inline io::path CONTROLS_FILE = "user:controls.toml";
static inline io::path SETTINGS_FILE = "user:settings.toml";
private:
std::filesystem::path resourcesFolder;
std::filesystem::path userFilesFolder;
std::filesystem::path projectFolder;
io::path currentWorldFolder;
std::optional<std::filesystem::path> scriptFolder;
std::vector<PathsRoot> entryPoints;
std::unordered_map<std::string, std::string> writeables;
std::vector<std::string> mounted;
void cleanup();
};
+1
View File
@@ -1,6 +1,7 @@
#include "ServerMainloop.hpp"
#include "Engine.hpp"
#include "EnginePaths.hpp"
#include "logic/scripting/scripting.hpp"
#include "logic/LevelController.hpp"
#include "interfaces/Process.hpp"
+1
View File
@@ -1,6 +1,7 @@
#include "WindowControl.hpp"
#include "Engine.hpp"
#include "engine/EnginePaths.hpp"
#include "devtools/Project.hpp"
#include "coders/imageio.hpp"
#include "window/Window.hpp"