refactor: Engine, EnginePaths, move CoreParameters to new header
This commit is contained in:
parent
01bf474a2b
commit
3b583d4dd6
15
src/engine/CoreParameters.hpp
Normal file
15
src/engine/CoreParameters.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
struct CoreParameters {
|
||||
bool headless = false;
|
||||
bool testMode = false;
|
||||
std::filesystem::path resFolder = "res";
|
||||
std::filesystem::path userFolder = ".";
|
||||
std::filesystem::path scriptFile;
|
||||
std::filesystem::path projectFolder;
|
||||
std::string debugServerString;
|
||||
int tps = 20;
|
||||
};
|
||||
@ -16,6 +16,7 @@
|
||||
#include "content/ContentControl.hpp"
|
||||
#include "core_defs.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "io/settings_io.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "frontend/menu.hpp"
|
||||
#include "frontend/screens/Screen.hpp"
|
||||
@ -129,13 +130,7 @@ void Engine::initialize(CoreParameters coreParameters) {
|
||||
if (params.projectFolder.empty()) {
|
||||
params.projectFolder = params.resFolder;
|
||||
}
|
||||
paths.setResourcesFolder(params.resFolder);
|
||||
paths.setUserFilesFolder(params.userFolder);
|
||||
paths.setProjectFolder(params.projectFolder);
|
||||
if (!params.scriptFile.empty()) {
|
||||
paths.setScriptFolder(params.scriptFile.parent_path());
|
||||
}
|
||||
paths.prepare();
|
||||
paths.prepare(params);
|
||||
loadProject();
|
||||
|
||||
editor = std::make_unique<devtools::Editor>(*this);
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "delegates.hpp"
|
||||
#include "typedefs.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
#include "io/engine_paths.hpp"
|
||||
#include "io/settings_io.hpp"
|
||||
#include "util/ObjectsKeeper.hpp"
|
||||
#include "CoreParameters.hpp"
|
||||
#include "PostRunnables.hpp"
|
||||
#include "Time.hpp"
|
||||
#include "delegates.hpp"
|
||||
#include "io/engine_paths.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "typedefs.hpp"
|
||||
#include "util/ObjectsKeeper.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Window;
|
||||
class WindowControl;
|
||||
class Assets;
|
||||
class Level;
|
||||
class Screen;
|
||||
class ContentControl;
|
||||
class EngineController;
|
||||
class Input;
|
||||
class Level;
|
||||
class Screen;
|
||||
class SettingsHandler;
|
||||
class Window;
|
||||
class WindowControl;
|
||||
struct Project;
|
||||
|
||||
namespace gui {
|
||||
@ -45,17 +45,6 @@ public:
|
||||
initialize_error(const std::string& message) : std::runtime_error(message) {}
|
||||
};
|
||||
|
||||
struct CoreParameters {
|
||||
bool headless = false;
|
||||
bool testMode = false;
|
||||
std::filesystem::path resFolder = "res";
|
||||
std::filesystem::path userFolder = ".";
|
||||
std::filesystem::path scriptFile;
|
||||
std::filesystem::path projectFolder;
|
||||
std::string debugServerString;
|
||||
int tps = 20;
|
||||
};
|
||||
|
||||
using OnWorldOpen = std::function<void(std::unique_ptr<Level>, int64_t)>;
|
||||
|
||||
class Engine : public util::ObjectsKeeper {
|
||||
|
||||
@ -1,44 +1,56 @@
|
||||
#include "engine_paths.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 "typedefs.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#include "io/devices/StdfsDevice.hpp"
|
||||
#include "io/devices/ZipFileDevice.hpp"
|
||||
#include "world/files/WorldFiles.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include "maths/util.hpp"
|
||||
|
||||
template<int n>
|
||||
static std::string generate_random_base64() {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
auto seed = now.time_since_epoch().count();
|
||||
|
||||
util::PseudoRandom random(seed); // fixme: replace with safe random
|
||||
ubyte bytes[n];
|
||||
random.rand(bytes, n);
|
||||
return util::base64_urlsafe_encode(bytes, n);
|
||||
}
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static debug::Logger logger("engine-paths");
|
||||
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";
|
||||
|
||||
void EnginePaths::prepare() {
|
||||
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);
|
||||
}
|
||||
|
||||
void EnginePaths::prepare(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(
|
||||
@ -59,11 +71,11 @@ void EnginePaths::prepare() {
|
||||
io::create_subdevice("config", "user", "config");
|
||||
}
|
||||
|
||||
const std::filesystem::path& EnginePaths::getUserFilesFolder() const {
|
||||
const fs::path& EnginePaths::getUserFilesFolder() const {
|
||||
return userFilesFolder;
|
||||
}
|
||||
|
||||
const std::filesystem::path& EnginePaths::getResourcesFolder() const {
|
||||
const fs::path& EnginePaths::getResourcesFolder() const {
|
||||
return resourcesFolder;
|
||||
}
|
||||
|
||||
@ -132,24 +144,6 @@ std::vector<io::path> EnginePaths::scanForWorlds() const {
|
||||
return folders;
|
||||
}
|
||||
|
||||
void EnginePaths::setUserFilesFolder(std::filesystem::path folder) {
|
||||
this->userFilesFolder = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setResourcesFolder(std::filesystem::path folder) {
|
||||
this->resourcesFolder = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setScriptFolder(std::filesystem::path folder) {
|
||||
io::set_device("script", std::make_shared<io::StdfsDevice>(folder));
|
||||
this->scriptFolder = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setProjectFolder(std::filesystem::path folder) {
|
||||
io::set_device("project", std::make_shared<io::StdfsDevice>(folder));
|
||||
this->projectFolder = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setCurrentWorldFolder(io::path folder) {
|
||||
if (folder.empty()) {
|
||||
io::remove_device("world");
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "io.hpp"
|
||||
#include "data/dv.hpp"
|
||||
#include "engine/CoreParameters.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <stdexcept>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
|
||||
#include "io.hpp"
|
||||
#include "data/dv.hpp"
|
||||
|
||||
struct PathsRoot {
|
||||
std::string name;
|
||||
io::path path;
|
||||
@ -46,18 +46,11 @@ class EnginePaths {
|
||||
public:
|
||||
ResPaths resPaths;
|
||||
|
||||
void prepare();
|
||||
void prepare(CoreParameters& params);
|
||||
|
||||
void setUserFilesFolder(std::filesystem::path folder);
|
||||
const std::filesystem::path& getUserFilesFolder() const;
|
||||
|
||||
void setResourcesFolder(std::filesystem::path folder);
|
||||
const std::filesystem::path& getResourcesFolder() const;
|
||||
|
||||
void setScriptFolder(std::filesystem::path folder);
|
||||
|
||||
void setProjectFolder(std::filesystem::path folder);
|
||||
|
||||
io::path getWorldFolderByName(const std::string& name);
|
||||
io::path getWorldsFolder() const;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user