Merge pull request #284 from alexei-zebra/refactor-engine_paths
Refactor engine_paths
This commit is contained in:
@@ -128,11 +128,11 @@ fs::path ContentPack::findPack(
|
|||||||
if (fs::is_directory(folder)) {
|
if (fs::is_directory(folder)) {
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
folder = paths->getUserfiles() / fs::path("content") / fs::path(name);
|
folder = paths->getUserFilesFolder() / fs::path("content") / fs::path(name);
|
||||||
if (fs::is_directory(folder)) {
|
if (fs::is_directory(folder)) {
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
return paths->getResources() / fs::path("content") / fs::path(name);
|
return paths->getResourcesFolder() / fs::path("content") / fs::path(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentPackRuntime::ContentPackRuntime(ContentPack info, scriptenv env)
|
ContentPackRuntime::ContentPackRuntime(ContentPack info, scriptenv env)
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
|
|||||||
ItemDef& item = builder->items.create("core:empty");
|
ItemDef& item = builder->items.create("core:empty");
|
||||||
item.iconType = item_icon_type::none;
|
item.iconType = item_icon_type::none;
|
||||||
|
|
||||||
auto bindsFile = paths->getResources()/fs::path("bindings.toml");
|
auto bindsFile = paths->getResourcesFolder()/fs::path("bindings.toml");
|
||||||
if (fs::is_regular_file(bindsFile)) {
|
if (fs::is_regular_file(bindsFile)) {
|
||||||
Events::loadBindings(
|
Events::loadBindings(
|
||||||
bindsFile.u8string(), files::read_string(bindsFile)
|
bindsFile.u8string(), files::read_string(bindsFile)
|
||||||
|
|||||||
+14
-14
@@ -83,7 +83,7 @@ Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, Engin
|
|||||||
paths->prepare();
|
paths->prepare();
|
||||||
loadSettings();
|
loadSettings();
|
||||||
|
|
||||||
auto resdir = paths->getResources();
|
auto resdir = paths->getResourcesFolder();
|
||||||
|
|
||||||
controller = std::make_unique<EngineController>(this);
|
controller = std::make_unique<EngineController>(this);
|
||||||
if (Window::initialize(&this->settings.display)){
|
if (Window::initialize(&this->settings.display)){
|
||||||
@@ -105,7 +105,7 @@ Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, Engin
|
|||||||
if (settings.ui.language.get() == "auto") {
|
if (settings.ui.language.get() == "auto") {
|
||||||
settings.ui.language.set(langs::locale_by_envlocale(
|
settings.ui.language.set(langs::locale_by_envlocale(
|
||||||
platform::detect_locale(),
|
platform::detect_locale(),
|
||||||
paths->getResources()
|
paths->getResourcesFolder()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if (ENGINE_DEBUG_BUILD) {
|
if (ENGINE_DEBUG_BUILD) {
|
||||||
@@ -162,7 +162,7 @@ void Engine::updateHotkeys() {
|
|||||||
void Engine::saveScreenshot() {
|
void Engine::saveScreenshot() {
|
||||||
auto image = Window::takeScreenshot();
|
auto image = Window::takeScreenshot();
|
||||||
image->flipY();
|
image->flipY();
|
||||||
fs::path filename = paths->getScreenshotFile("png");
|
fs::path filename = paths->getNewScreenshotFile("png");
|
||||||
imageio::write(filename.string(), image.get());
|
imageio::write(filename.string(), image.get());
|
||||||
logger.info() << "saved screenshot as " << filename.u8string();
|
logger.info() << "saved screenshot as " << filename.u8string();
|
||||||
}
|
}
|
||||||
@@ -252,8 +252,8 @@ PacksManager Engine::createPacksManager(const fs::path& worldFolder) {
|
|||||||
PacksManager manager;
|
PacksManager manager;
|
||||||
manager.setSources({
|
manager.setSources({
|
||||||
worldFolder/fs::path("content"),
|
worldFolder/fs::path("content"),
|
||||||
paths->getUserfiles()/fs::path("content"),
|
paths->getUserFilesFolder()/fs::path("content"),
|
||||||
paths->getResources()/fs::path("content")
|
paths->getResourcesFolder()/fs::path("content")
|
||||||
});
|
});
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
@@ -296,7 +296,7 @@ static void load_configs(const fs::path& root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::loadContent() {
|
void Engine::loadContent() {
|
||||||
auto resdir = paths->getResources();
|
auto resdir = paths->getResourcesFolder();
|
||||||
|
|
||||||
std::vector<std::string> names;
|
std::vector<std::string> names;
|
||||||
for (auto& pack : contentPacks) {
|
for (auto& pack : contentPacks) {
|
||||||
@@ -307,7 +307,7 @@ void Engine::loadContent() {
|
|||||||
corecontent::setup(paths, &contentBuilder);
|
corecontent::setup(paths, &contentBuilder);
|
||||||
|
|
||||||
paths->setContentPacks(&contentPacks);
|
paths->setContentPacks(&contentPacks);
|
||||||
PacksManager manager = createPacksManager(paths->getWorldFolder());
|
PacksManager manager = createPacksManager(paths->getCurrentWorldFolder());
|
||||||
manager.scan();
|
manager.scan();
|
||||||
names = manager.assembly(names);
|
names = manager.assembly(names);
|
||||||
contentPacks = manager.getAll(names);
|
contentPacks = manager.getAll(names);
|
||||||
@@ -318,7 +318,7 @@ void Engine::loadContent() {
|
|||||||
ContentLoader(&pack, contentBuilder).load();
|
ContentLoader(&pack, contentBuilder).load();
|
||||||
load_configs(pack.folder);
|
load_configs(pack.folder);
|
||||||
}
|
}
|
||||||
load_configs(paths->getResources());
|
load_configs(paths->getResourcesFolder());
|
||||||
|
|
||||||
content = contentBuilder.build();
|
content = contentBuilder.build();
|
||||||
resPaths = std::make_unique<ResPaths>(resdir, resRoots);
|
resPaths = std::make_unique<ResPaths>(resdir, resRoots);
|
||||||
@@ -329,7 +329,7 @@ void Engine::loadContent() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::resetContent() {
|
void Engine::resetContent() {
|
||||||
auto resdir = paths->getResources();
|
auto resdir = paths->getResourcesFolder();
|
||||||
resPaths = std::make_unique<ResPaths>(resdir, std::vector<PathsRoot>());
|
resPaths = std::make_unique<ResPaths>(resdir, std::vector<PathsRoot>());
|
||||||
contentPacks.clear();
|
contentPacks.clear();
|
||||||
content.reset();
|
content.reset();
|
||||||
@@ -349,17 +349,17 @@ void Engine::loadWorldContent(const fs::path& folder) {
|
|||||||
PacksManager manager;
|
PacksManager manager;
|
||||||
manager.setSources({
|
manager.setSources({
|
||||||
folder/fs::path("content"),
|
folder/fs::path("content"),
|
||||||
paths->getUserfiles()/fs::path("content"),
|
paths->getUserFilesFolder()/fs::path("content"),
|
||||||
paths->getResources()/fs::path("content")
|
paths->getResourcesFolder()/fs::path("content")
|
||||||
});
|
});
|
||||||
manager.scan();
|
manager.scan();
|
||||||
contentPacks = manager.getAll(manager.assembly(packNames));
|
contentPacks = manager.getAll(manager.assembly(packNames));
|
||||||
paths->setWorldFolder(folder);
|
paths->setCurrentWorldFolder(folder);
|
||||||
loadContent();
|
loadContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::loadAllPacks() {
|
void Engine::loadAllPacks() {
|
||||||
PacksManager manager = createPacksManager(paths->getWorldFolder());
|
PacksManager manager = createPacksManager(paths->getCurrentWorldFolder());
|
||||||
manager.scan();
|
manager.scan();
|
||||||
auto allnames = manager.getAllNames();
|
auto allnames = manager.getAllNames();
|
||||||
contentPacks = manager.getAll(manager.assembly(allnames));
|
contentPacks = manager.getAll(manager.assembly(allnames));
|
||||||
@@ -376,7 +376,7 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::setLanguage(std::string locale) {
|
void Engine::setLanguage(std::string locale) {
|
||||||
langs::setup(paths->getResources(), std::move(locale), contentPacks);
|
langs::setup(paths->getResourcesFolder(), std::move(locale), contentPacks);
|
||||||
gui->getMenu()->setPageLoader(menus::create_page_loader(this));
|
gui->getMenu()->setPageLoader(menus::create_page_loader(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+99
-79
@@ -5,33 +5,80 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include <typedefs.hpp>
|
#include <typedefs.hpp>
|
||||||
#include <util/stringutil.hpp>
|
#include <util/stringutil.hpp>
|
||||||
#include "WorldFiles.hpp"
|
#include "WorldFiles.hpp"
|
||||||
|
|
||||||
const fs::path SCREENSHOTS_FOLDER {"screenshots"};
|
/**
|
||||||
const fs::path CONTENT_FOLDER {"content"};
|
* @brief ENUM for accessing folder and file names
|
||||||
const fs::path CONTROLS_FILE {"controls.toml"};
|
*/
|
||||||
const fs::path SETTINGS_FILE {"settings.toml"};
|
enum F_F_NAME{
|
||||||
|
SCREENSHOTS_FOLDER,
|
||||||
|
CONTENT_FOLDER,
|
||||||
|
CONTROLS_FILE,
|
||||||
|
SETTINGS_FILE,
|
||||||
|
|
||||||
|
COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief array for get file or folder name by enum `F_F_NAME`
|
||||||
|
*
|
||||||
|
* example:
|
||||||
|
* `std::filesystem::path settings = f_f_names[SETTINGS_FILE];`
|
||||||
|
*/
|
||||||
|
static std::array<std::string, F_F_NAME::COUNT> f_f_names{
|
||||||
|
"screenshots",
|
||||||
|
"content",
|
||||||
|
"controls.toml",
|
||||||
|
"settings.toml"
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::filesystem::path toCanonic(std::filesystem::path path) {
|
||||||
|
std::stack<std::string> parts;
|
||||||
|
path = path.lexically_normal();
|
||||||
|
do {
|
||||||
|
parts.push(path.filename().u8string());
|
||||||
|
path = path.parent_path();
|
||||||
|
} while (!path.empty());
|
||||||
|
|
||||||
|
path = fs::u8path("");
|
||||||
|
|
||||||
|
while (!parts.empty()) {
|
||||||
|
const std::string part = parts.top();
|
||||||
|
parts.pop();
|
||||||
|
if (part == ".") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (part == "..") {
|
||||||
|
throw files_access_error("entry point reached");
|
||||||
|
}
|
||||||
|
|
||||||
|
path = path / std::filesystem::path(part);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void EnginePaths::prepare() {
|
void EnginePaths::prepare() {
|
||||||
fs::path contentFolder = userfiles / fs::path(CONTENT_FOLDER);
|
std::filesystem::path contentFolder = userFilesFolder / std::filesystem::path(f_f_names[CONTENT_FOLDER]);
|
||||||
if (!fs::is_directory(contentFolder)) {
|
if (!fs::is_directory(contentFolder)) {
|
||||||
fs::create_directories(contentFolder);
|
fs::create_directories(contentFolder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getUserfiles() const {
|
std::filesystem::path EnginePaths::getUserFilesFolder() const {
|
||||||
return userfiles;
|
return userFilesFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getResources() const {
|
std::filesystem::path EnginePaths::getResourcesFolder() const {
|
||||||
return resources;
|
return resourcesFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getScreenshotFile(const std::string& ext) {
|
std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
|
||||||
fs::path folder = userfiles / fs::path(SCREENSHOTS_FOLDER);
|
std::filesystem::path folder = userFilesFolder / std::filesystem::path(f_f_names[SCREENSHOTS_FOLDER]);
|
||||||
if (!fs::is_directory(folder)) {
|
if (!fs::is_directory(folder)) {
|
||||||
fs::create_directory(folder);
|
fs::create_directory(folder);
|
||||||
}
|
}
|
||||||
@@ -44,7 +91,7 @@ fs::path EnginePaths::getScreenshotFile(const std::string& ext) {
|
|||||||
ss << std::put_time(&tm, format);
|
ss << std::put_time(&tm, format);
|
||||||
std::string datetimestr = ss.str();
|
std::string datetimestr = ss.str();
|
||||||
|
|
||||||
fs::path filename =
|
std::filesystem::path filename =
|
||||||
folder / fs::u8path("screenshot-" + datetimestr + "." + ext);
|
folder / fs::u8path("screenshot-" + datetimestr + "." + ext);
|
||||||
uint index = 0;
|
uint index = 0;
|
||||||
while (fs::exists(filename)) {
|
while (fs::exists(filename)) {
|
||||||
@@ -57,44 +104,44 @@ fs::path EnginePaths::getScreenshotFile(const std::string& ext) {
|
|||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getWorldsFolder() {
|
std::filesystem::path EnginePaths::getWorldsFolder() {
|
||||||
return userfiles / fs::path("worlds");
|
return userFilesFolder / std::filesystem::path("worlds");
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getWorldFolder() {
|
std::filesystem::path EnginePaths::getCurrentWorldFolder() {
|
||||||
return worldFolder;
|
return currentWorldFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getWorldFolder(const std::string& name) {
|
std::filesystem::path EnginePaths::getWorldFolderByName(const std::string& name) {
|
||||||
return getWorldsFolder() / fs::path(name);
|
return getWorldsFolder() / std::filesystem::path(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getControlsFile() {
|
std::filesystem::path EnginePaths::getControlsFile() {
|
||||||
return userfiles / fs::path(CONTROLS_FILE);
|
return userFilesFolder / std::filesystem::path(f_f_names[CONTROLS_FILE]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::getSettingsFile() {
|
std::filesystem::path EnginePaths::getSettingsFile() {
|
||||||
return userfiles / fs::path(SETTINGS_FILE);
|
return userFilesFolder / std::filesystem::path(f_f_names[SETTINGS_FILE]);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<fs::path> EnginePaths::scanForWorlds() {
|
std::vector<std::filesystem::path> EnginePaths::scanForWorlds() {
|
||||||
std::vector<fs::path> folders;
|
std::vector<std::filesystem::path> folders;
|
||||||
|
|
||||||
fs::path folder = getWorldsFolder();
|
std::filesystem::path folder = getWorldsFolder();
|
||||||
if (!fs::is_directory(folder)) return folders;
|
if (!fs::is_directory(folder)) return folders;
|
||||||
|
|
||||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
if (!entry.is_directory()) {
|
if (!entry.is_directory()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const fs::path& worldFolder = entry.path();
|
const std::filesystem::path& worldFolder = entry.path();
|
||||||
fs::path worldFile = worldFolder / fs::u8path(WorldFiles::WORLD_FILE);
|
std::filesystem::path worldFile = worldFolder / fs::u8path(WorldFiles::WORLD_FILE);
|
||||||
if (!fs::is_regular_file(worldFile)) {
|
if (!fs::is_regular_file(worldFile)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
folders.push_back(worldFolder);
|
folders.push_back(worldFolder);
|
||||||
}
|
}
|
||||||
std::sort(folders.begin(), folders.end(), [](fs::path a, fs::path b) {
|
std::sort(folders.begin(), folders.end(), [](std::filesystem::path a, std::filesystem::path b) {
|
||||||
a = a / fs::u8path(WorldFiles::WORLD_FILE);
|
a = a / fs::u8path(WorldFiles::WORLD_FILE);
|
||||||
b = b / fs::u8path(WorldFiles::WORLD_FILE);
|
b = b / fs::u8path(WorldFiles::WORLD_FILE);
|
||||||
return fs::last_write_time(a) > fs::last_write_time(b);
|
return fs::last_write_time(a) > fs::last_write_time(b);
|
||||||
@@ -102,51 +149,23 @@ std::vector<fs::path> EnginePaths::scanForWorlds() {
|
|||||||
return folders;
|
return folders;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EnginePaths::isWorldNameUsed(const std::string& name) {
|
void EnginePaths::setUserFilesFolder(std::filesystem::path folder) {
|
||||||
return fs::exists(EnginePaths::getWorldsFolder() / fs::u8path(name));
|
this->userFilesFolder = std::move(folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnginePaths::setUserfiles(fs::path folder) {
|
void EnginePaths::setResourcesFolder(std::filesystem::path folder) {
|
||||||
this->userfiles = std::move(folder);
|
this->resourcesFolder = std::move(folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnginePaths::setResources(fs::path folder) {
|
void EnginePaths::setCurrentWorldFolder(std::filesystem::path folder) {
|
||||||
this->resources = std::move(folder);
|
this->currentWorldFolder = std::move(folder);
|
||||||
}
|
|
||||||
|
|
||||||
void EnginePaths::setWorldFolder(fs::path folder) {
|
|
||||||
this->worldFolder = std::move(folder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
||||||
this->contentPacks = contentPacks;
|
this->contentPacks = contentPacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fs::path toCanonic(fs::path path) {
|
std::filesystem::path EnginePaths::resolve(const std::string& path, bool throwErr) {
|
||||||
std::stack<std::string> parts;
|
|
||||||
path = path.lexically_normal();
|
|
||||||
while (true) {
|
|
||||||
parts.push(path.filename().u8string());
|
|
||||||
path = path.parent_path();
|
|
||||||
if (path.empty()) break;
|
|
||||||
}
|
|
||||||
path = fs::u8path("");
|
|
||||||
while (!parts.empty()) {
|
|
||||||
const std::string part = parts.top();
|
|
||||||
parts.pop();
|
|
||||||
if (part == ".") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (part == "..") {
|
|
||||||
throw files_access_error("entry point reached");
|
|
||||||
}
|
|
||||||
|
|
||||||
path = path / fs::path(part);
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path EnginePaths::resolve(const std::string& path, bool throwErr) {
|
|
||||||
size_t separator = path.find(':');
|
size_t separator = path.find(':');
|
||||||
if (separator == std::string::npos) {
|
if (separator == std::string::npos) {
|
||||||
throw files_access_error("no entry point specified");
|
throw files_access_error("no entry point specified");
|
||||||
@@ -156,13 +175,13 @@ fs::path EnginePaths::resolve(const std::string& path, bool throwErr) {
|
|||||||
filename = toCanonic(fs::u8path(filename)).u8string();
|
filename = toCanonic(fs::u8path(filename)).u8string();
|
||||||
|
|
||||||
if (prefix == "res" || prefix == "core") {
|
if (prefix == "res" || prefix == "core") {
|
||||||
return resources / fs::u8path(filename);
|
return resourcesFolder / fs::u8path(filename);
|
||||||
}
|
}
|
||||||
if (prefix == "user") {
|
if (prefix == "user") {
|
||||||
return userfiles / fs::u8path(filename);
|
return userFilesFolder / fs::u8path(filename);
|
||||||
}
|
}
|
||||||
if (prefix == "world") {
|
if (prefix == "world") {
|
||||||
return worldFolder / fs::u8path(filename);
|
return currentWorldFolder / fs::u8path(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentPacks) {
|
if (contentPacks) {
|
||||||
@@ -175,17 +194,18 @@ fs::path EnginePaths::resolve(const std::string& path, bool throwErr) {
|
|||||||
if (throwErr) {
|
if (throwErr) {
|
||||||
throw files_access_error("unknown entry point '" + prefix + "'");
|
throw files_access_error("unknown entry point '" + prefix + "'");
|
||||||
}
|
}
|
||||||
return fs::path(filename);
|
return std::filesystem::path(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
ResPaths::ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots)
|
|
||||||
|
ResPaths::ResPaths(std::filesystem::path mainRoot, std::vector<PathsRoot> roots)
|
||||||
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
|
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path ResPaths::find(const std::string& filename) const {
|
std::filesystem::path ResPaths::find(const std::string& filename) const {
|
||||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
fs::path file = root.path / fs::u8path(filename);
|
std::filesystem::path file = root.path / fs::u8path(filename);
|
||||||
if (fs::exists(file)) {
|
if (fs::exists(file)) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@@ -196,12 +216,12 @@ fs::path ResPaths::find(const std::string& filename) const {
|
|||||||
std::string ResPaths::findRaw(const std::string& filename) const {
|
std::string ResPaths::findRaw(const std::string& filename) const {
|
||||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
if (fs::exists(root.path / fs::path(filename))) {
|
if (fs::exists(root.path / std::filesystem::path(filename))) {
|
||||||
return root.name + ":" + filename;
|
return root.name + ":" + filename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto resDir = mainRoot;
|
auto resDir = mainRoot;
|
||||||
if (fs::exists(resDir / fs::path(filename))) {
|
if (fs::exists(resDir / std::filesystem::path(filename))) {
|
||||||
return "core:" + filename;
|
return "core:" + filename;
|
||||||
}
|
}
|
||||||
throw std::runtime_error("could not to find file " + util::quote(filename));
|
throw std::runtime_error("could not to find file " + util::quote(filename));
|
||||||
@@ -212,7 +232,7 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName
|
|||||||
std::vector<std::string> entries;
|
std::vector<std::string> entries;
|
||||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
fs::path folder = root.path / fs::u8path(folderName);
|
std::filesystem::path folder = root.path / fs::u8path(folderName);
|
||||||
if (!fs::is_directory(folder)) continue;
|
if (!fs::is_directory(folder)) continue;
|
||||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
auto name = entry.path().filename().u8string();
|
auto name = entry.path().filename().u8string();
|
||||||
@@ -220,7 +240,7 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
fs::path folder = mainRoot / fs::u8path(folderName);
|
std::filesystem::path folder = mainRoot / fs::u8path(folderName);
|
||||||
if (!fs::is_directory(folder)) return entries;
|
if (!fs::is_directory(folder)) return entries;
|
||||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
auto name = entry.path().filename().u8string();
|
auto name = entry.path().filename().u8string();
|
||||||
@@ -230,18 +250,18 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
|
std::vector<std::filesystem::path> ResPaths::listdir(const std::string& folderName) const {
|
||||||
std::vector<fs::path> entries;
|
std::vector<std::filesystem::path> entries;
|
||||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
fs::path folder = root.path / fs::u8path(folderName);
|
std::filesystem::path folder = root.path / fs::u8path(folderName);
|
||||||
if (!fs::is_directory(folder)) continue;
|
if (!fs::is_directory(folder)) continue;
|
||||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
entries.push_back(entry.path());
|
entries.push_back(entry.path());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
fs::path folder = mainRoot / fs::u8path(folderName);
|
std::filesystem::path folder = mainRoot / fs::u8path(folderName);
|
||||||
if (!fs::is_directory(folder)) return entries;
|
if (!fs::is_directory(folder)) return entries;
|
||||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
entries.push_back(entry.path());
|
entries.push_back(entry.path());
|
||||||
@@ -250,6 +270,6 @@ std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fs::path& ResPaths::getMainRoot() const {
|
const std::filesystem::path& ResPaths::getMainRoot() const {
|
||||||
return mainRoot;
|
return mainRoot;
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-26
@@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include <content/ContentPack.hpp>
|
#include <content/ContentPack.hpp>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
class files_access_error : public std::runtime_error {
|
class files_access_error : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
@@ -17,51 +16,57 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
class EnginePaths {
|
class EnginePaths {
|
||||||
fs::path userfiles {"."};
|
|
||||||
fs::path resources {"res"};
|
|
||||||
fs::path worldFolder;
|
|
||||||
std::vector<ContentPack>* contentPacks = nullptr;
|
|
||||||
public:
|
public:
|
||||||
void prepare();
|
void prepare();
|
||||||
|
|
||||||
fs::path getUserfiles() const;
|
void setUserFilesFolder(std::filesystem::path folder);
|
||||||
fs::path getResources() const;
|
std::filesystem::path getUserFilesFolder() const;
|
||||||
|
|
||||||
fs::path getScreenshotFile(const std::string& ext);
|
void setResourcesFolder(std::filesystem::path folder);
|
||||||
fs::path getWorldsFolder();
|
std::filesystem::path getResourcesFolder() const;
|
||||||
fs::path getWorldFolder();
|
|
||||||
fs::path getWorldFolder(const std::string& name);
|
std::filesystem::path getWorldsFolder();
|
||||||
fs::path getControlsFile();
|
std::filesystem::path getWorldFolderByName(const std::string& name);
|
||||||
fs::path getSettingsFile();
|
|
||||||
bool isWorldNameUsed(const std::string& name);
|
void setCurrentWorldFolder(std::filesystem::path folder);
|
||||||
|
std::filesystem::path getCurrentWorldFolder();
|
||||||
|
|
||||||
|
std::filesystem::path getNewScreenshotFile(const std::string& ext);
|
||||||
|
std::filesystem::path getControlsFile();
|
||||||
|
std::filesystem::path getSettingsFile();
|
||||||
|
|
||||||
void setUserfiles(fs::path folder);
|
|
||||||
void setResources(fs::path folder);
|
|
||||||
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
||||||
void setWorldFolder(fs::path folder);
|
|
||||||
|
|
||||||
std::vector<fs::path> scanForWorlds();
|
std::vector<std::filesystem::path> scanForWorlds();
|
||||||
|
|
||||||
fs::path resolve(const std::string& path, bool throwErr = true);
|
std::filesystem::path resolve(const std::string& path, bool throwErr = true);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::filesystem::path userFilesFolder {"."};
|
||||||
|
std::filesystem::path resourcesFolder {"res"};
|
||||||
|
std::filesystem::path currentWorldFolder;
|
||||||
|
std::vector<ContentPack>* contentPacks = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PathsRoot {
|
struct PathsRoot {
|
||||||
std::string name;
|
std::string name;
|
||||||
fs::path path;
|
std::filesystem::path path;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ResPaths {
|
class ResPaths {
|
||||||
fs::path mainRoot;
|
|
||||||
std::vector<PathsRoot> roots;
|
|
||||||
public:
|
public:
|
||||||
ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots);
|
ResPaths(std::filesystem::path mainRoot, std::vector<PathsRoot> roots);
|
||||||
|
|
||||||
fs::path find(const std::string& filename) const;
|
std::filesystem::path find(const std::string& filename) const;
|
||||||
std::string findRaw(const std::string& filename) const;
|
std::string findRaw(const std::string& filename) const;
|
||||||
std::vector<fs::path> listdir(const std::string& folder) const;
|
std::vector<std::filesystem::path> listdir(const std::string& folder) const;
|
||||||
std::vector<std::string> listdirRaw(const std::string& folder) const;
|
std::vector<std::string> listdirRaw(const std::string& folder) const;
|
||||||
|
|
||||||
const fs::path& getMainRoot() const;
|
const std::filesystem::path& getMainRoot() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::filesystem::path mainRoot;
|
||||||
|
std::vector<PathsRoot> roots;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FILES_ENGINE_PATHS_HPP_
|
#endif // FILES_ENGINE_PATHS_HPP_
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ LevelScreen::~LevelScreen() {
|
|||||||
saveWorldPreview();
|
saveWorldPreview();
|
||||||
scripting::on_frontend_close();
|
scripting::on_frontend_close();
|
||||||
controller->onWorldQuit();
|
controller->onWorldQuit();
|
||||||
engine->getPaths()->setWorldFolder(fs::path());
|
engine->getPaths()->setCurrentWorldFolder(fs::path());
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::saveWorldPreview() {
|
void LevelScreen::saveWorldPreview() {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ EngineController::EngineController(Engine* engine) : engine(engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EngineController::deleteWorld(const std::string& name) {
|
void EngineController::deleteWorld(const std::string& name) {
|
||||||
fs::path folder = engine->getPaths()->getWorldFolder(name);
|
fs::path folder = engine->getPaths()->getWorldFolderByName(name);
|
||||||
guiutil::confirm(
|
guiutil::confirm(
|
||||||
engine->getGUI(),
|
engine->getGUI(),
|
||||||
langs::get(L"delete-confirm", L"world") + L" (" +
|
langs::get(L"delete-confirm", L"world") + L" (" +
|
||||||
@@ -189,7 +189,7 @@ void EngineController::createWorld(
|
|||||||
|
|
||||||
if (!menus::call(engine, [this, paths, folder]() {
|
if (!menus::call(engine, [this, paths, folder]() {
|
||||||
engine->loadContent();
|
engine->loadContent();
|
||||||
paths->setWorldFolder(folder);
|
paths->setCurrentWorldFolder(folder);
|
||||||
})) {
|
})) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ using namespace scripting;
|
|||||||
static int l_pack_get_folder(lua::State* L) {
|
static int l_pack_get_folder(lua::State* L) {
|
||||||
std::string packName = lua::tostring(L, 1);
|
std::string packName = lua::tostring(L, 1);
|
||||||
if (packName == "core") {
|
if (packName == "core") {
|
||||||
auto folder = engine->getPaths()->getResources().u8string() + "/";
|
auto folder = engine->getPaths()->getResourcesFolder().u8string() + "/";
|
||||||
return lua::pushstring(L, folder);
|
return lua::pushstring(L, folder);
|
||||||
}
|
}
|
||||||
for (auto& pack : engine->getContentPacks()) {
|
for (auto& pack : engine->getContentPacks()) {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ static int l_world_get_seed(lua::State* L) {
|
|||||||
|
|
||||||
static int l_world_exists(lua::State* L) {
|
static int l_world_exists(lua::State* L) {
|
||||||
auto name = lua::require_string(L, 1);
|
auto name = lua::require_string(L, 1);
|
||||||
auto worldsDir = engine->getPaths()->getWorldFolder(name);
|
auto worldsDir = engine->getPaths()->getWorldFolderByName(name);
|
||||||
return lua::pushboolean(L, fs::is_directory(worldsDir));
|
return lua::pushboolean(L, fs::is_directory(worldsDir));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ LevelController* scripting::controller = nullptr;
|
|||||||
|
|
||||||
static void load_script(const fs::path& name, bool throwable) {
|
static void load_script(const fs::path& name, bool throwable) {
|
||||||
auto paths = scripting::engine->getPaths();
|
auto paths = scripting::engine->getPaths();
|
||||||
fs::path file = paths->getResources() / fs::path("scripts") / name;
|
fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name;
|
||||||
|
|
||||||
std::string src = files::read_string(file);
|
std::string src = files::read_string(file);
|
||||||
auto L = lua::get_main_thread();
|
auto L = lua::get_main_thread();
|
||||||
|
|||||||
@@ -48,14 +48,14 @@ bool perform_keyword(
|
|||||||
if (!fs::is_directory(fs::path(token))) {
|
if (!fs::is_directory(fs::path(token))) {
|
||||||
throw std::runtime_error(token + " is not a directory");
|
throw std::runtime_error(token + " is not a directory");
|
||||||
}
|
}
|
||||||
paths.setResources(fs::path(token));
|
paths.setResourcesFolder(fs::path(token));
|
||||||
std::cout << "resources folder: " << token << std::endl;
|
std::cout << "resources folder: " << token << std::endl;
|
||||||
} else if (keyword == "--dir") {
|
} else if (keyword == "--dir") {
|
||||||
auto token = reader.next();
|
auto token = reader.next();
|
||||||
if (!fs::is_directory(fs::path(token))) {
|
if (!fs::is_directory(fs::path(token))) {
|
||||||
fs::create_directories(fs::path(token));
|
fs::create_directories(fs::path(token));
|
||||||
}
|
}
|
||||||
paths.setUserfiles(fs::path(token));
|
paths.setUserFilesFolder(fs::path(token));
|
||||||
std::cout << "userfiles folder: " << token << std::endl;
|
std::cout << "userfiles folder: " << token << std::endl;
|
||||||
} else if (keyword == "--help" || keyword == "-h") {
|
} else if (keyword == "--help" || keyword == "-h") {
|
||||||
std::cout << "VoxelEngine command-line arguments:" << std::endl;
|
std::cout << "VoxelEngine command-line arguments:" << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user