lua: file library
This commit is contained in:
+51
-23
@@ -6,37 +6,35 @@
|
||||
|
||||
#define SCREENSHOTS_FOLDER "screenshots"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
fs::path EnginePaths::getUserfiles() const {
|
||||
return userfiles;
|
||||
return userfiles;
|
||||
}
|
||||
|
||||
fs::path EnginePaths::getResources() const {
|
||||
return resources;
|
||||
return resources;
|
||||
}
|
||||
|
||||
fs::path EnginePaths::getScreenshotFile(std::string ext) {
|
||||
fs::path folder = userfiles/fs::path(SCREENSHOTS_FOLDER);
|
||||
if (!fs::is_directory(folder)) {
|
||||
fs::create_directory(folder);
|
||||
}
|
||||
fs::path folder = userfiles/fs::path(SCREENSHOTS_FOLDER);
|
||||
if (!fs::is_directory(folder)) {
|
||||
fs::create_directory(folder);
|
||||
}
|
||||
|
||||
auto t = std::time(nullptr);
|
||||
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();
|
||||
const char* format = "%Y-%m-%d_%H-%M-%S";
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(&tm, format);
|
||||
std::string datetimestr = ss.str();
|
||||
|
||||
fs::path filename = folder/fs::path("screenshot-"+datetimestr+"."+ext);
|
||||
uint index = 0;
|
||||
while (fs::exists(filename)) {
|
||||
filename = folder/fs::path("screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext);
|
||||
index++;
|
||||
}
|
||||
return filename;
|
||||
fs::path filename = folder/fs::path("screenshot-"+datetimestr+"."+ext);
|
||||
uint index = 0;
|
||||
while (fs::exists(filename)) {
|
||||
filename = folder/fs::path("screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext);
|
||||
index++;
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
fs::path EnginePaths::getWorldsFolder() {
|
||||
@@ -44,15 +42,45 @@ fs::path EnginePaths::getWorldsFolder() {
|
||||
}
|
||||
|
||||
bool EnginePaths::isWorldNameUsed(std::string name) {
|
||||
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
|
||||
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
|
||||
}
|
||||
|
||||
void EnginePaths::setUserfiles(fs::path folder) {
|
||||
this->userfiles = folder;
|
||||
this->userfiles = folder;
|
||||
}
|
||||
|
||||
void EnginePaths::setResources(fs::path folder) {
|
||||
this->resources = folder;
|
||||
this->resources = folder;
|
||||
}
|
||||
|
||||
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
||||
this->contentPacks = contentPacks;
|
||||
}
|
||||
|
||||
fs::path EnginePaths::resolve(std::string path) {
|
||||
size_t separator = path.find(':');
|
||||
if (separator == std::string::npos) {
|
||||
return fs::path(path);
|
||||
}
|
||||
std::string prefix = path.substr(0, separator);
|
||||
std::string filename = path.substr(separator+1);
|
||||
|
||||
if (prefix == "res" || prefix == "core") {
|
||||
return resources/fs::path(filename);
|
||||
}
|
||||
|
||||
if (prefix == "user") {
|
||||
return userfiles/fs::path(filename);
|
||||
}
|
||||
|
||||
if (contentPacks) {
|
||||
for (auto& pack : *contentPacks) {
|
||||
if (pack.id == prefix) {
|
||||
return pack.folder/fs::path(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
return fs::path("./"+filename);
|
||||
}
|
||||
|
||||
ResPaths::ResPaths(fs::path mainRoot, std::vector<fs::path> roots)
|
||||
|
||||
+22
-14
@@ -5,30 +5,38 @@
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
#include "../content/ContentPack.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class EnginePaths {
|
||||
std::filesystem::path userfiles {"."};
|
||||
std::filesystem::path resources {"res"};
|
||||
fs::path userfiles {"."};
|
||||
fs::path resources {"res"};
|
||||
std::vector<ContentPack>* contentPacks = nullptr;
|
||||
public:
|
||||
std::filesystem::path getUserfiles() const;
|
||||
std::filesystem::path getResources() const;
|
||||
fs::path getUserfiles() const;
|
||||
fs::path getResources() const;
|
||||
|
||||
std::filesystem::path getScreenshotFile(std::string ext);
|
||||
std::filesystem::path getWorldsFolder();
|
||||
fs::path getScreenshotFile(std::string ext);
|
||||
fs::path getWorldsFolder();
|
||||
bool isWorldNameUsed(std::string name);
|
||||
|
||||
void setUserfiles(std::filesystem::path folder);
|
||||
void setResources(std::filesystem::path folder);
|
||||
void setUserfiles(fs::path folder);
|
||||
void setResources(fs::path folder);
|
||||
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
||||
|
||||
fs::path resolve(std::string path);
|
||||
};
|
||||
|
||||
class ResPaths {
|
||||
std::filesystem::path mainRoot;
|
||||
std::vector<std::filesystem::path> roots;
|
||||
fs::path mainRoot;
|
||||
std::vector<fs::path> roots;
|
||||
public:
|
||||
ResPaths(std::filesystem::path mainRoot,
|
||||
std::vector<std::filesystem::path> roots);
|
||||
ResPaths(fs::path mainRoot,
|
||||
std::vector<fs::path> roots);
|
||||
|
||||
std::filesystem::path find(const std::string& filename) const;
|
||||
std::vector<std::filesystem::path> listdir(const std::string& folder) const;
|
||||
fs::path find(const std::string& filename) const;
|
||||
std::vector<fs::path> listdir(const std::string& folder) const;
|
||||
};
|
||||
|
||||
#endif // FILES_ENGINE_PATHS_H_
|
||||
Reference in New Issue
Block a user