format engine_paths.cpp

This commit is contained in:
MihailRis
2024-08-06 19:21:25 +03:00
parent da2c307444
commit 6c165badf5
+39 -41
View File
@@ -1,20 +1,19 @@
#include "engine_paths.hpp" #include "engine_paths.hpp"
#include <algorithm> #include <algorithm>
#include <array>
#include <filesystem> #include <filesystem>
#include <sstream> #include <sstream>
#include <stack> #include <stack>
#include <utility>
#include <array>
#include <typedefs.hpp> #include <typedefs.hpp>
#include <util/stringutil.hpp> #include <util/stringutil.hpp>
#include <utility>
#include "WorldFiles.hpp" #include "WorldFiles.hpp"
/**
* @brief ENUM for accessing folder and file names /// @brief ENUM for accessing folder and file names
*/ enum F_F_NAME {
enum F_F_NAME{
SCREENSHOTS_FOLDER, SCREENSHOTS_FOLDER,
CONTENT_FOLDER, CONTENT_FOLDER,
CONTROLS_FILE, CONTROLS_FILE,
@@ -23,18 +22,11 @@ enum F_F_NAME{
COUNT COUNT
}; };
/** /// @brief array for get file or folder name by enum `F_F_NAME`
* @brief array for get file or folder name by enum `F_F_NAME` /// @example:
* /// std::filesystem::path settings = f_f_names[SETTINGS_FILE];
* example: static std::array<std::string, F_F_NAME::COUNT> f_f_names {
* `std::filesystem::path settings = f_f_names[SETTINGS_FILE];` "screenshots", "content", "controls.toml", "settings.toml"};
*/
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) { static std::filesystem::path toCanonic(std::filesystem::path path) {
std::stack<std::string> parts; std::stack<std::string> parts;
@@ -61,9 +53,9 @@ static std::filesystem::path toCanonic(std::filesystem::path path) {
return path; return path;
} }
void EnginePaths::prepare() { void EnginePaths::prepare() {
std::filesystem::path contentFolder = userFilesFolder / std::filesystem::path(f_f_names[CONTENT_FOLDER]); auto 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);
} }
@@ -78,7 +70,8 @@ std::filesystem::path EnginePaths::getResourcesFolder() const {
} }
std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext) { std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
std::filesystem::path folder = userFilesFolder / std::filesystem::path(f_f_names[SCREENSHOTS_FOLDER]); auto 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);
} }
@@ -91,8 +84,7 @@ std::filesystem::path EnginePaths::getNewScreenshotFile(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();
std::filesystem::path filename = auto 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)) {
filename = folder / fs::u8path( filename = folder / fs::u8path(
@@ -127,25 +119,29 @@ std::filesystem::path EnginePaths::getSettingsFile() {
std::vector<std::filesystem::path> EnginePaths::scanForWorlds() { std::vector<std::filesystem::path> EnginePaths::scanForWorlds() {
std::vector<std::filesystem::path> folders; std::vector<std::filesystem::path> folders;
std::filesystem::path folder = getWorldsFolder(); auto 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 std::filesystem::path& worldFolder = entry.path(); const auto& worldFolder = entry.path();
std::filesystem::path worldFile = worldFolder / fs::u8path(WorldFiles::WORLD_FILE); auto 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(), [](std::filesystem::path a, std::filesystem::path b) { std::sort(
a = a / fs::u8path(WorldFiles::WORLD_FILE); folders.begin(),
b = b / fs::u8path(WorldFiles::WORLD_FILE); folders.end(),
return fs::last_write_time(a) > fs::last_write_time(b); [](std::filesystem::path a, std::filesystem::path b) {
}); a = a / fs::u8path(WorldFiles::WORLD_FILE);
b = b / fs::u8path(WorldFiles::WORLD_FILE);
return fs::last_write_time(a) > fs::last_write_time(b);
}
);
return folders; return folders;
} }
@@ -165,7 +161,9 @@ void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
this->contentPacks = contentPacks; this->contentPacks = contentPacks;
} }
std::filesystem::path EnginePaths::resolve(const std::string& path, bool throwErr) { std::filesystem::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");
@@ -197,7 +195,6 @@ std::filesystem::path EnginePaths::resolve(const std::string& path, bool throwEr
return std::filesystem::path(filename); return std::filesystem::path(filename);
} }
ResPaths::ResPaths(std::filesystem::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)) {
} }
@@ -205,7 +202,7 @@ ResPaths::ResPaths(std::filesystem::path mainRoot, std::vector<PathsRoot> roots)
std::filesystem::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];
std::filesystem::path file = root.path / fs::u8path(filename); auto file = root.path / fs::u8path(filename);
if (fs::exists(file)) { if (fs::exists(file)) {
return file; return file;
} }
@@ -227,12 +224,11 @@ std::string ResPaths::findRaw(const std::string& filename) const {
throw std::runtime_error("could not to find file " + util::quote(filename)); throw std::runtime_error("could not to find file " + util::quote(filename));
} }
std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) const {
) const {
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];
std::filesystem::path folder = root.path / fs::u8path(folderName); auto 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();
@@ -240,7 +236,7 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName
} }
} }
{ {
std::filesystem::path folder = mainRoot / fs::u8path(folderName); auto 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();
@@ -250,7 +246,9 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName
return entries; return entries;
} }
std::vector<std::filesystem::path> ResPaths::listdir(const std::string& folderName) const { std::vector<std::filesystem::path> ResPaths::listdir(
const std::string& folderName
) const {
std::vector<std::filesystem::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];
@@ -261,7 +259,7 @@ std::vector<std::filesystem::path> ResPaths::listdir(const std::string& folderNa
} }
} }
{ {
std::filesystem::path folder = mainRoot / fs::u8path(folderName); auto 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());