mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2025-06-19 19:26:36 +02:00
initial import, >900commits predate this
This commit is contained in:
247
framework/filesystem/src/mm/fs_const_archiver.cpp
Normal file
247
framework/filesystem/src/mm/fs_const_archiver.cpp
Normal file
@ -0,0 +1,247 @@
|
||||
#include "./fs_const_archiver.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <mm/logger.hpp>
|
||||
#define LOG_CRIT(...) __LOG_CRIT( "Filesystem", __VA_ARGS__)
|
||||
#define LOG_ERROR(...) __LOG_ERROR("Filesystem", __VA_ARGS__)
|
||||
#define LOG_WARN(...) __LOG_WARN( "Filesystem", __VA_ARGS__)
|
||||
#define LOG_INFO(...) __LOG_INFO( "Filesystem", __VA_ARGS__)
|
||||
#define LOG_DEBUG(...) __LOG_DEBUG("Filesystem", __VA_ARGS__)
|
||||
#define LOG_TRACE(...) __LOG_TRACE("Filesystem", __VA_ARGS__)
|
||||
|
||||
namespace MM {
|
||||
|
||||
std::unordered_map<std::string, std::pair<uint8_t*, size_t>> FSConstArchiver::_storage {};
|
||||
|
||||
void* FSConstArchiver::PH_openArchive(PHYSFS_Io* io, const char* name, int forWrite, int* claimed) {
|
||||
(void)io;
|
||||
(void)name;
|
||||
// TODO: test name
|
||||
|
||||
if (forWrite) {
|
||||
//PHYSFS_setErrorCode(PHYSFS_ERR_READ_ONLY);
|
||||
*claimed = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
*claimed = 1;
|
||||
|
||||
return (void*)1; // non zero
|
||||
}
|
||||
|
||||
PHYSFS_EnumerateCallbackResult FSConstArchiver::PH_enumerate(void* opaque, const char* dirname, PHYSFS_EnumerateCallback cb, const char* origdir, void* callbackdata) {
|
||||
(void)opaque;
|
||||
(void)dirname;
|
||||
(void)cb;
|
||||
(void)origdir;
|
||||
(void)callbackdata;
|
||||
|
||||
// "files" just not enumeratable
|
||||
//return PHYSFS_ENUM_STOP;
|
||||
return PHYSFS_ENUM_OK;
|
||||
}
|
||||
|
||||
PHYSFS_Io* FSConstArchiver::PH_openRead(void* opaque, const char* fnm) {
|
||||
(void)opaque;
|
||||
|
||||
if (!_storage.count(fnm)) {
|
||||
PHYSFS_setErrorCode(PHYSFS_ERR_NOT_FOUND);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return createIO(fnm);
|
||||
}
|
||||
|
||||
PHYSFS_Io* FSConstArchiver::PH_openWrite(void* opaque, const char* filename) {
|
||||
(void)opaque;
|
||||
(void)filename;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PHYSFS_Io* FSConstArchiver::PH_openAppend(void* opaque, const char* filename) {
|
||||
(void)opaque;
|
||||
(void)filename;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int FSConstArchiver::PH_remove(void* opaque, const char* filename) {
|
||||
(void)opaque;
|
||||
(void)filename;
|
||||
|
||||
// TODO: support this?
|
||||
|
||||
PHYSFS_setErrorCode(PHYSFS_ERR_READ_ONLY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FSConstArchiver::PH_mkdir(void* opaque, const char* filename) {
|
||||
(void)opaque;
|
||||
(void)filename;
|
||||
|
||||
PHYSFS_setErrorCode(PHYSFS_ERR_READ_ONLY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FSConstArchiver::PH_stat(void* opaque, const char* fn, PHYSFS_Stat* stat) {
|
||||
(void)opaque;
|
||||
|
||||
if (pathIsDir(fn)) {
|
||||
stat->filesize = 0;
|
||||
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
||||
} else if (_storage.count(fn)) {
|
||||
stat->filesize = _storage[fn].second;
|
||||
stat->filetype = PHYSFS_FILETYPE_REGULAR;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
stat->accesstime = -1;
|
||||
stat->createtime = -1;
|
||||
stat->modtime = -1;
|
||||
stat->readonly = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void FSConstArchiver::PH_closeArchive(void* opaque) {
|
||||
(void)opaque;
|
||||
}
|
||||
|
||||
PHYSFS_Io* FSConstArchiver::createIO(const char* filename, PHYSFS_uint64 pos) {
|
||||
if (!_storage.count(filename)) {
|
||||
//LOGCA(std::string("error: path '") + filename + "' alredy used!");
|
||||
LOG_ERROR("path '{}' already used!", filename);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//LOGCA(std::string("###creating io for ") + filename);
|
||||
LOG_TRACE("creating io for {}", filename);
|
||||
|
||||
struct io_internal {
|
||||
std::string filename;
|
||||
PHYSFS_uint64 pos = 0;
|
||||
uint8_t* data = nullptr;
|
||||
size_t data_size = 0;
|
||||
};
|
||||
|
||||
auto io = new PHYSFS_Io;
|
||||
|
||||
|
||||
io->version = 0;
|
||||
io->write = nullptr;
|
||||
io->flush = nullptr;
|
||||
|
||||
io->opaque = new io_internal{
|
||||
filename,
|
||||
pos,
|
||||
_storage[filename].first,
|
||||
_storage[filename].second
|
||||
};
|
||||
io->destroy = [](PHYSFS_Io* io) {
|
||||
delete (io_internal*)io->opaque;
|
||||
delete io;
|
||||
};
|
||||
|
||||
io->tell = [](PHYSFS_Io* io) -> PHYSFS_sint64 { return ((io_internal*)io->opaque)->pos; };
|
||||
io->seek = [](PHYSFS_Io* io, PHYSFS_uint64 offset) -> int {
|
||||
auto* inter = (io_internal*)io->opaque;
|
||||
if (offset > inter->data_size)
|
||||
return 0; // error, past end
|
||||
inter->pos = offset;
|
||||
return 1;
|
||||
};
|
||||
io->length = [](PHYSFS_Io* io) -> PHYSFS_sint64 { return ((io_internal*)io->opaque)->data_size; };
|
||||
io->duplicate = [](PHYSFS_Io* io) -> PHYSFS_Io* {
|
||||
if (!io)
|
||||
return nullptr;
|
||||
|
||||
auto* inter = (io_internal*)io->opaque;
|
||||
auto* dup = createIO(inter->filename.c_str(), inter->pos);
|
||||
return dup;
|
||||
};
|
||||
io->read = [](PHYSFS_Io* io, void* buf, PHYSFS_uint64 len) -> PHYSFS_sint64 {
|
||||
if (!io)
|
||||
return -1;
|
||||
|
||||
auto* inter = (io_internal*)io->opaque;
|
||||
if (inter->data_size == inter->pos)
|
||||
return 0; // EOF
|
||||
|
||||
PHYSFS_sint64 bytes_to_read = 0;
|
||||
if (inter->pos + len >= inter->data_size) {
|
||||
bytes_to_read = inter->data_size - inter->pos; // remaining data
|
||||
} else {
|
||||
bytes_to_read = len;
|
||||
}
|
||||
|
||||
memcpy(buf, inter->data + inter->pos, bytes_to_read);
|
||||
inter->pos += bytes_to_read;
|
||||
return bytes_to_read;
|
||||
};
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
bool FSConstArchiver::pathIsDir(const char* path) {
|
||||
std::string_view pstr {path};
|
||||
//for (auto&[str, data] : _storage) {
|
||||
for (auto& it : _storage) {
|
||||
// stats_with c++20 <.<
|
||||
|
||||
if (it.first.compare(0, pstr.size(), pstr) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const PHYSFS_Archiver* FSConstArchiver::getArchiverStruct(void) {
|
||||
static const PHYSFS_Archiver a {
|
||||
0, // version
|
||||
{ // info
|
||||
"MEM", // ext
|
||||
"in const memory 'archiver'", // desc
|
||||
"Green", // author
|
||||
"", // url
|
||||
0 // sym
|
||||
},
|
||||
&PH_openArchive,
|
||||
&PH_enumerate,
|
||||
&PH_openRead,
|
||||
&PH_openWrite,
|
||||
&PH_openAppend,
|
||||
&PH_remove,
|
||||
&PH_mkdir,
|
||||
&PH_stat,
|
||||
&PH_closeArchive
|
||||
};
|
||||
|
||||
return &a;
|
||||
}
|
||||
|
||||
void FSConstArchiver::addFile(const char* path, uint8_t* data, size_t data_size) {
|
||||
if (!path)
|
||||
return;
|
||||
|
||||
// remove leading / es
|
||||
while (path[0] == '/') {
|
||||
path++;
|
||||
|
||||
if (path[0] == '\0')
|
||||
return;
|
||||
}
|
||||
|
||||
if (_storage.count(path)) {
|
||||
//LOGCA(std::string("error: '") + path + "' already in path");
|
||||
LOG_TRACE("'{}' already in path, overriding...", path);
|
||||
}
|
||||
|
||||
_storage[path] = {data, data_size};
|
||||
}
|
||||
|
||||
} // MM
|
||||
|
42
framework/filesystem/src/mm/fs_const_archiver.hpp
Normal file
42
framework/filesystem/src/mm/fs_const_archiver.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <mm/services/filesystem.hpp>
|
||||
|
||||
#include <physfs.h>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace MM {
|
||||
class FSConstArchiver {
|
||||
private:
|
||||
static std::unordered_map<std::string, std::pair<uint8_t*, size_t>> _storage;
|
||||
|
||||
private:
|
||||
// archiver interface
|
||||
static void* PH_openArchive(PHYSFS_Io* io, const char* name, int forWrite, int* claimed);
|
||||
static PHYSFS_EnumerateCallbackResult PH_enumerate(void* opaque, const char* dirname, PHYSFS_EnumerateCallback cb, const char* origdir, void* callbackdata);
|
||||
static PHYSFS_Io* PH_openRead(void* opaque, const char* fnm);
|
||||
static PHYSFS_Io* PH_openWrite(void* opaque, const char* filename);
|
||||
static PHYSFS_Io* PH_openAppend(void* opaque, const char* filename);
|
||||
static int PH_remove(void* opaque, const char* filename);
|
||||
static int PH_mkdir(void* opaque, const char* filename);
|
||||
static int PH_stat(void* opaque, const char* fn, PHYSFS_Stat* stat);
|
||||
static void PH_closeArchive(void* opaque);
|
||||
|
||||
static PHYSFS_Io* createIO(const char* filename, PHYSFS_uint64 pos = 0);
|
||||
|
||||
static bool pathIsDir(const char* path);
|
||||
|
||||
public:
|
||||
static const PHYSFS_Archiver* getArchiverStruct(void);
|
||||
|
||||
// the archiver is not responsible for memory, you need to keep it around.
|
||||
static void addFile(const char* path, uint8_t* data, size_t data_size);
|
||||
};
|
||||
} // MM
|
||||
|
||||
#define FS_CONST_MOUNT_FILE(path, x) MM::FSConstArchiver::addFile(path, (uint8_t*)x, sizeof x);
|
||||
#define FS_CONST_MOUNT_FILE_S(path, x, size) MM::FSConstArchiver::addFile(path, x, size);
|
||||
|
||||
//#define FS_CONST_MOUNT_FILE_STATIC(path, x) struct __internal_fs_const_struct_t { __internal_fs_const_struct_t(void) { FS_CONST_MOUNT_FILE(path, x) } }; static __internal_fs_const_struct_t __internal_fs_const_struct {};
|
||||
|
49
framework/filesystem/src/mm/path_utils.hpp
Normal file
49
framework/filesystem/src/mm/path_utils.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
// TODO: test
|
||||
|
||||
#include <mm/string_view_split.hpp>
|
||||
|
||||
// TODO: make proper namespace
|
||||
namespace MM {
|
||||
|
||||
// removes './'s and resolves '../'
|
||||
// returns false if too many ..
|
||||
inline bool path_shorten(std::string& path) {
|
||||
auto splits = std_utils::split(path, "/");
|
||||
std::vector<std::string_view> new_splits;
|
||||
|
||||
for (size_t i = 0; i < splits.size(); i++) {
|
||||
if (splits[i] == "..") {
|
||||
if (new_splits.empty())
|
||||
return false;
|
||||
|
||||
new_splits.pop_back();
|
||||
} else if (splits[i] == ".") {
|
||||
// skip
|
||||
} else {
|
||||
new_splits.emplace_back(splits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::string new_path;
|
||||
for (auto& s : new_splits) {
|
||||
new_path += "/";
|
||||
new_path += s;
|
||||
}
|
||||
path = new_path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// remove the file name from the path
|
||||
inline std::string base_path(std::string_view path) {
|
||||
auto pos = path.find_last_of("/");
|
||||
if (pos == path.npos)
|
||||
return std::string(path); // TODO: make sure there is a '/'
|
||||
|
||||
return std::string(path.substr(0, pos+1));
|
||||
}
|
||||
|
||||
} // MM
|
||||
|
354
framework/filesystem/src/mm/services/filesystem.cpp
Normal file
354
framework/filesystem/src/mm/services/filesystem.cpp
Normal file
@ -0,0 +1,354 @@
|
||||
#include "./filesystem.hpp"
|
||||
|
||||
#include "../fs_const_archiver.hpp"
|
||||
|
||||
#include <physfs.h>
|
||||
|
||||
#include <mm/path_utils.hpp>
|
||||
|
||||
#ifndef MM_HEADLESS
|
||||
#include <SDL.h>
|
||||
#endif
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <mm/logger.hpp>
|
||||
#define LOG_CRIT(...) __LOG_CRIT( "Filesystem", __VA_ARGS__)
|
||||
#define LOG_ERROR(...) __LOG_ERROR("Filesystem", __VA_ARGS__)
|
||||
#define LOG_WARN(...) __LOG_WARN( "Filesystem", __VA_ARGS__)
|
||||
#define LOG_INFO(...) __LOG_INFO( "Filesystem", __VA_ARGS__)
|
||||
#define LOG_DEBUG(...) __LOG_DEBUG("Filesystem", __VA_ARGS__)
|
||||
#define LOG_TRACE(...) __LOG_TRACE("Filesystem", __VA_ARGS__)
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
bool FilesystemService::enable(Engine&) {
|
||||
if (PHYSFS_isInit()) {
|
||||
LOG_ERROR("physfs already initialized!!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PHYSFS_init(_argv0)) {
|
||||
LOG_ERROR("error initializing physfs: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
return false;
|
||||
}
|
||||
|
||||
#if !defined(MM_HEADLESS) && !defined(EMSCRIPTEN)
|
||||
char* pref_path = SDL_GetPrefPath("made_of_jelly", _app_name);
|
||||
#else
|
||||
// org only respected on windows
|
||||
const char* pref_path = PHYSFS_getPrefDir("made_of_jelly", _app_name); // TODO: make this point to the right dir
|
||||
#endif
|
||||
if (!pref_path) {
|
||||
LOG_ERROR("error getting pref path");
|
||||
return false; // TODO: defaulting to base path?
|
||||
} else {
|
||||
if (!PHYSFS_setWriteDir(pref_path)) {
|
||||
LOG_ERROR("error setting physfs write dir: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
//return false;
|
||||
}
|
||||
|
||||
//LOGFS(std::string("write dir set to: ") + PHYSFS_getWriteDir());
|
||||
LOG_INFO("write dir set to: {}", PHYSFS_getWriteDir());
|
||||
|
||||
if (!PHYSFS_mount(pref_path, NULL, 0)) {
|
||||
LOG_ERROR("mounting physfs write dir: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
//return false;
|
||||
}
|
||||
|
||||
#if !defined(MM_HEADLESS) && !defined(EMSCRIPTEN)
|
||||
SDL_free(pref_path);
|
||||
#endif
|
||||
}
|
||||
|
||||
// add base path to search tree
|
||||
if (_try_mount_base) {
|
||||
if (!PHYSFS_mount(PHYSFS_getBaseDir(), NULL, 0)) {
|
||||
LOG_ERROR("mounting physfs base dir: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
}
|
||||
|
||||
// mount self (exec) :P
|
||||
if (_try_mount_self) {
|
||||
if (PHYSFS_mount(_argv0, "/", 1)) {
|
||||
LOG_INFO("mounted self!!");
|
||||
}
|
||||
}
|
||||
|
||||
// const archiver
|
||||
if (!PHYSFS_registerArchiver(FSConstArchiver::getArchiverStruct())) {
|
||||
LOG_ERROR("error registering const archiver: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
|
||||
// fake mount, to add mount const archiver
|
||||
if (!PHYSFS_mountMemory((void*)1, 1, nullptr, "main.mem", "/", 1)) { // should trigger
|
||||
LOG_ERROR("error mounting const archiver: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
|
||||
for (const auto&[archive, mount_point, append] : _try_mount_list) {
|
||||
if (!PHYSFS_mount(archive.c_str(), mount_point.c_str(), append?1:0)) {
|
||||
LOG_ERROR("mounting physfs userdefined archive: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FilesystemService::disable(Engine&) {
|
||||
if (!PHYSFS_deinit()) {
|
||||
LOG_ERROR("error deinitializing physfs: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
}
|
||||
|
||||
FilesystemService::FilesystemService(void) {
|
||||
MM::Logger::initSectionLogger("Filesystem");
|
||||
}
|
||||
|
||||
FilesystemService::FilesystemService(
|
||||
const char* argv0,
|
||||
const char* app_name,
|
||||
const bool try_mount_self,
|
||||
const bool try_mount_base,
|
||||
const std::vector<std::tuple<std::string, std::string, bool>>& try_mount_list
|
||||
) : _argv0(argv0), _app_name(app_name), _try_mount_self(try_mount_self), _try_mount_base(try_mount_base) {
|
||||
MM::Logger::initSectionLogger("Filesystem");
|
||||
|
||||
//_try_mount_list = try_mount_list;
|
||||
for (const auto& it : try_mount_list) {
|
||||
_try_mount_list.emplace_back(it);
|
||||
}
|
||||
}
|
||||
|
||||
FilesystemService::~FilesystemService(void) {
|
||||
}
|
||||
|
||||
bool FilesystemService::exists(const char* filepath) const {
|
||||
return PHYSFS_exists(filepath);
|
||||
}
|
||||
|
||||
bool FilesystemService::isFile(const char* filepath) const {
|
||||
if (!PHYSFS_exists(filepath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PHYSFS_Stat stat;
|
||||
|
||||
if (PHYSFS_stat(filepath, &stat) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: y other tho
|
||||
return stat.filetype == PHYSFS_FileType::PHYSFS_FILETYPE_REGULAR || stat.filetype == PHYSFS_FileType::PHYSFS_FILETYPE_OTHER;
|
||||
}
|
||||
|
||||
bool FilesystemService::isDir(const char* filepath) const {
|
||||
if (!PHYSFS_exists(filepath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PHYSFS_Stat stat;
|
||||
|
||||
if (PHYSFS_stat(filepath, &stat) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return stat.filetype == PHYSFS_FileType::PHYSFS_FILETYPE_DIRECTORY;
|
||||
}
|
||||
|
||||
void FilesystemService::forEachIn(const char* filepath, std::function<bool(const char*)> fn) const {
|
||||
char** rc = PHYSFS_enumerateFiles(filepath);
|
||||
for (char** i = rc; *i != NULL; i++) {
|
||||
if (!fn(*i)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
PHYSFS_freeList(rc);
|
||||
}
|
||||
|
||||
FilesystemService::fs_file_t FilesystemService::open(const char* filepath, FilesystemService::FOPEN_t t) const {
|
||||
if (!filepath) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PHYSFS_File* phys_file = nullptr;
|
||||
|
||||
switch (t) {
|
||||
case FOPEN_t::READ:
|
||||
LOG_TRACE("opening '{}' in read mode", filepath);
|
||||
phys_file = PHYSFS_openRead(filepath);
|
||||
break;
|
||||
case FOPEN_t::WRITE:
|
||||
LOG_TRACE("opening '{}' in write mode", filepath);
|
||||
if (std::string_view(filepath).find('/') != std::string_view::npos) {// bc dirs might not exist
|
||||
auto tmp_dir_path = MM::base_path(filepath);
|
||||
PHYSFS_mkdir(tmp_dir_path.c_str());
|
||||
}
|
||||
phys_file = PHYSFS_openWrite(filepath);
|
||||
break;
|
||||
case FOPEN_t::APPEND:
|
||||
LOG_TRACE("opening '{}' in append mode", filepath);
|
||||
if (std::string_view(filepath).find('/') != std::string_view::npos) {// bc dirs might not exist
|
||||
auto tmp_dir_path = MM::base_path(filepath);
|
||||
PHYSFS_mkdir(tmp_dir_path.c_str());
|
||||
}
|
||||
phys_file = PHYSFS_openAppend(filepath);
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("invalid fopen mode {} for '{}'", t, filepath);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!phys_file) {
|
||||
LOG_ERROR("error while opening '{}' : {}", filepath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
|
||||
return phys_file;
|
||||
}
|
||||
|
||||
bool FilesystemService::close(fs_file_t file) const {
|
||||
if (!PHYSFS_close(file)) {
|
||||
//LOGFS_physfs("error while closing file ");
|
||||
// ignore. windoof for some reason allways fails with "permission denied" (for closing read handles)
|
||||
LOG_TRACE("error while closing file : {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FilesystemService::eof(fs_file_t file) const {
|
||||
return PHYSFS_eof(file);
|
||||
}
|
||||
|
||||
int64_t FilesystemService::tell(fs_file_t file) const {
|
||||
int64_t ret = PHYSFS_tell(file);
|
||||
if (ret < 0) {
|
||||
LOG_ERROR("error while determining file position (tell()) {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool FilesystemService::seek(fs_file_t file, uint64_t pos) const {
|
||||
if (!PHYSFS_seek(file, pos)) {
|
||||
LOG_ERROR("error while seeking in file: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t FilesystemService::read(fs_file_t file, void* buffer, uint64_t size) const {
|
||||
auto r = PHYSFS_readBytes(file, buffer, size);
|
||||
|
||||
if (r < 0) {
|
||||
LOG_ERROR("error while reading file: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
uint64_t FilesystemService::write(fs_file_t file, const void* buffer, uint64_t size) const {
|
||||
uint64_t r = static_cast<uint64_t>(PHYSFS_writeBytes(file, buffer, size));
|
||||
|
||||
// TODO: handle partial writes differently
|
||||
if (r != size) {
|
||||
LOG_ERROR("error while writing file: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void FilesystemService::flush(fs_file_t file) const {
|
||||
if (!PHYSFS_flush(file)) {
|
||||
LOG_ERROR("error flushing ..?: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
|
||||
}
|
||||
}
|
||||
|
||||
int64_t FilesystemService::length(fs_file_t file) const {
|
||||
return PHYSFS_fileLength(file);
|
||||
}
|
||||
|
||||
int64_t FilesystemService::readString(fs_file_t file, std::string& string) const {
|
||||
char buffer[256] = {};
|
||||
|
||||
int64_t read_bytes = 0;
|
||||
|
||||
while (int64_t r = read(file, buffer, 256)) {
|
||||
if (r < 0) {
|
||||
return r;
|
||||
}
|
||||
|
||||
string.append(buffer, static_cast<size_t>(r));
|
||||
read_bytes += r;
|
||||
}
|
||||
|
||||
return read_bytes;
|
||||
}
|
||||
|
||||
nlohmann::json FilesystemService::readJson(fs_file_t file) const {
|
||||
if (!file)
|
||||
return {};
|
||||
|
||||
seek(file, 0);
|
||||
|
||||
std::string buffer;
|
||||
readString(file, buffer);
|
||||
|
||||
return nlohmann::json::parse(buffer);
|
||||
}
|
||||
|
||||
nlohmann::json FilesystemService::readJson(const char* filepath) const {
|
||||
if (!exists(filepath)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto h = open(filepath, READ);
|
||||
auto r = readJson(h);
|
||||
close(h);
|
||||
return r;
|
||||
}
|
||||
|
||||
bool FilesystemService::writeJson(fs_file_t file, nlohmann::json& j, const int indent, const char indent_char) const {
|
||||
if (!file) {
|
||||
LOG_ERROR("writing json to invalid file");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto s = j.dump(indent, indent_char);
|
||||
|
||||
auto r = write(file, s.c_str(), s.size());
|
||||
|
||||
return r == s.size();
|
||||
}
|
||||
|
||||
bool FilesystemService::writeJson(const char* filepath, nlohmann::json& j, const int indent, const char indent_char) const {
|
||||
auto h = open(filepath, WRITE);
|
||||
auto r = writeJson(h, j, indent, indent_char);
|
||||
close(h);
|
||||
return r;
|
||||
}
|
||||
|
||||
bool FilesystemService::writeJson(fs_file_t file, nlohmann::ordered_json& j, const int indent, const char indent_char) const {
|
||||
if (!file) {
|
||||
LOG_ERROR("writing json to invalid file");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto s = j.dump(indent, indent_char);
|
||||
|
||||
auto r = write(file, s.c_str(), s.size());
|
||||
|
||||
return r == s.size();
|
||||
}
|
||||
|
||||
bool FilesystemService::writeJson(const char* filepath, nlohmann::ordered_json& j, const int indent, const char indent_char) const {
|
||||
auto h = open(filepath, WRITE);
|
||||
auto r = writeJson(h, j, indent, indent_char);
|
||||
close(h);
|
||||
return r;
|
||||
}
|
||||
|
||||
} // MM::Services
|
||||
|
89
framework/filesystem/src/mm/services/filesystem.hpp
Normal file
89
framework/filesystem/src/mm/services/filesystem.hpp
Normal file
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
//#include <physfs.h>
|
||||
//fwd
|
||||
typedef struct PHYSFS_File PHYSFS_File;
|
||||
typedef struct PHYSFS_Stat PHYSFS_Stat;
|
||||
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
class FilesystemService : public Service {
|
||||
public:
|
||||
using fs_file_t = PHYSFS_File*;
|
||||
|
||||
|
||||
public:
|
||||
bool enable(Engine&) override;
|
||||
void disable(Engine&) override;
|
||||
|
||||
const char* name(void) override { return "Filesystem"; }
|
||||
|
||||
private:
|
||||
// setup data
|
||||
const char* _argv0 = nullptr;
|
||||
const char* _app_name = "default_app";
|
||||
const bool _try_mount_self = true;
|
||||
const bool _try_mount_base = true;
|
||||
// TODO: write dir
|
||||
std::vector<std::tuple<std::string, std::string, bool>> _try_mount_list;
|
||||
|
||||
public:
|
||||
FilesystemService(void);
|
||||
FilesystemService(
|
||||
const char* argv0 = nullptr,
|
||||
const char* app_name = "default_app",
|
||||
const bool try_mount_self = true,
|
||||
const bool try_mount_base = true,
|
||||
const std::vector<std::tuple<std::string, std::string, bool>>& try_mount_list = {}
|
||||
);
|
||||
|
||||
~FilesystemService(void);
|
||||
|
||||
bool exists(const char* filepath) const;
|
||||
bool isFile(const char* filepath) const;
|
||||
bool isDir(const char* filepath) const;
|
||||
|
||||
void forEachIn(const char* filepath, std::function<bool(const char*)> fn) const;
|
||||
|
||||
enum FOPEN_t { READ, WRITE, APPEND };
|
||||
// opens file, expects path and mode
|
||||
fs_file_t open(const char* filepath, FOPEN_t = READ) const;
|
||||
|
||||
bool close(fs_file_t file) const;
|
||||
bool eof(fs_file_t file) const;
|
||||
int64_t tell(fs_file_t file) const;
|
||||
bool seek(fs_file_t file, uint64_t pos) const;
|
||||
|
||||
// read from file into buffer max size bytes, returns read bytes
|
||||
int64_t read(fs_file_t file, void* buffer, uint64_t size) const;
|
||||
|
||||
// writes from buffer to file, returns written bytes
|
||||
uint64_t write(fs_file_t file, const void* buffer, uint64_t size) const;
|
||||
|
||||
void flush(fs_file_t file) const;
|
||||
|
||||
int64_t length(fs_file_t file) const;
|
||||
|
||||
// reads in (remaining) file to string, and returns read bytes
|
||||
int64_t readString(fs_file_t file, std::string& string) const;
|
||||
|
||||
// json
|
||||
nlohmann::json readJson(fs_file_t file) const;
|
||||
nlohmann::json readJson(const char* filepath) const;
|
||||
|
||||
bool writeJson(fs_file_t file, nlohmann::json& j, const int indent = -1, const char indent_char = ' ') const;
|
||||
bool writeJson(const char* filepath, nlohmann::json& j, const int indent = -1, const char indent_char = ' ') const;
|
||||
|
||||
bool writeJson(fs_file_t file, nlohmann::ordered_json& j, const int indent = -1, const char indent_char = ' ') const;
|
||||
bool writeJson(const char* filepath, nlohmann::ordered_json& j, const int indent = -1, const char indent_char = ' ') const;
|
||||
};
|
||||
|
||||
} // namespace MM::Services
|
||||
|
Reference in New Issue
Block a user