initial import, >900commits predate this

This commit is contained in:
2020-09-29 13:47:50 +02:00
commit e74154ccee
352 changed files with 108120 additions and 0 deletions

View File

@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.2)
project(filesystem_service CXX)
add_library(filesystem_service
src/mm/path_utils.hpp
src/mm/services/filesystem.hpp
src/mm/services/filesystem.cpp
src/mm/fs_const_archiver.hpp
src/mm/fs_const_archiver.cpp
)
target_include_directories(filesystem_service PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(filesystem_service
engine
logger
entt
nlohmann_json::nlohmann_json
physfs-static # TODO: fix this
std_utils
)
if(NOT MM_HEADLESS)
#if android
#target_link_libraries(filesystem_service SDL)
#endif
if(EMSCRIPTEN)
set_target_properties(filesystem_service PROPERTIES COMPILE_FLAGS "-s USE_SDL=2")
set_target_properties(filesystem_service PROPERTIES LINK_FLAGS "-s USE_SDL=2")
else()
#if not android or emscripten
target_include_directories(filesystem_service PUBLIC "${SDL2_INCLUDE_DIR}")
target_link_libraries(filesystem_service ${SDL2_LIBRARY})
#endif
endif()
endif()
if (BUILD_TESTING)
add_subdirectory(test)
endif()

View 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

View 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 {};

View 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

View 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

View 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

View File

@ -0,0 +1,14 @@
add_executable(filesystem_service_test
filesystem_tests.cpp
res/test.zip.h
)
target_include_directories(filesystem_service_test PRIVATE ".")
target_link_libraries(filesystem_service_test
filesystem_service
gtest_main
)
add_test(NAME filesystem_service_test COMMAND filesystem_service_test)

View File

@ -0,0 +1,307 @@
#include <gtest/gtest.h>
#include <mm/services/filesystem.hpp>
//#include <physfs.h>
#include <nlohmann/json.hpp>
#include <mm/fs_const_archiver.hpp>
#include <mm/logger.hpp>
#include "res/test.zip.h" // zip in memory
char* argv0; // HACK
TEST(fs_service_system, general) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
ASSERT_FALSE(fs_ss_ptr->open("i_do_not_exist.txt"));
ASSERT_FALSE(fs_ss_ptr->exists("test_file.txt"));
ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
ASSERT_TRUE(fs_ss_ptr->exists("test_file.txt"));
{
MM::Services::FilesystemService::fs_file_t file = fs_ss_ptr->open("test_file.txt", MM::Services::FilesystemService::FOPEN_t::READ);
ASSERT_TRUE(file) << "File opened";
ASSERT_FALSE(fs_ss_ptr->eof(file));
ASSERT_EQ(fs_ss_ptr->tell(file), 0) << "position at start of file";
char buffer[9] = {0};
ASSERT_EQ(fs_ss_ptr->read(file, buffer, 8), 8) << "file length == 8";
ASSERT_STREQ(buffer, "test :D\n");
ASSERT_TRUE(fs_ss_ptr->eof(file));
ASSERT_EQ(fs_ss_ptr->tell(file), 8) << "position at end of file, in this case 8";
ASSERT_TRUE(fs_ss_ptr->close(file));
}
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, fopen_eof_tell) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
MM::Services::FilesystemService::fs_file_t file = fs_ss_ptr->open("test_file.txt", MM::Services::FilesystemService::FOPEN_t::READ);
ASSERT_TRUE(file) << "File opened";
auto& fs = *fs_ss_ptr;
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 0) << "position at start of file";
ASSERT_TRUE(fs.seek(file, 3));
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 3);
ASSERT_FALSE(fs.seek(file, 10)) << "seek past end";
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 3);
ASSERT_TRUE(fs.close(file));
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, fopen_w_a_write) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
const char* filename = "test2_write_file.txt";
// write
{
if (fs.exists(filename)) {
PHYSFS_delete(filename); // TODO: add wrapper
std::cout << "######deleted existing file" << std::endl;
}
auto file = fs.open(filename, MM::Services::FilesystemService::FOPEN_t::WRITE);
ASSERT_TRUE(file) << "File opened";
//ASSERT_TRUE(MM::fs_service_system::eof(file)); actually false
ASSERT_EQ(fs.tell(file), 0) << "position at start of file";
ASSERT_TRUE(fs.seek(file, 10)) << "seek past end";
ASSERT_EQ(fs.tell(file), 10);
const char data[4] {'a', 's', 'd', 'f'};
ASSERT_EQ(fs.write(file, data, 4), 4);
ASSERT_EQ(fs.tell(file), 14);
fs.flush(file);
ASSERT_TRUE(fs.close(file));
}
// append
{
auto file = fs.open(filename, MM::Services::FilesystemService::FOPEN_t::APPEND);
ASSERT_TRUE(file) << "File opened";
//ASSERT_TRUE(MM::fs_service_system::eof(file)); actually false
ASSERT_EQ(fs.tell(file), 14) << "position at start of file";
const char data[4] {'j', 'k', 'l', 'o'};
ASSERT_EQ(fs.write(file, data, 4), 4);
ASSERT_EQ(fs.tell(file), 18);
fs.flush(file);
ASSERT_TRUE(fs.close(file));
}
// read
{
auto file = fs.open(filename, MM::Services::FilesystemService::FOPEN_t::READ);
ASSERT_TRUE(file) << "File opened";
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 0) << "position at start of file";
// fail
{
const char data[4] {'j', 'k', 'l', 'o'};
ASSERT_FALSE(fs.write(file, data, 4) == 4);
}
ASSERT_TRUE(fs.seek(file, 10));
char buffer[9] = {0};
ASSERT_EQ(fs.read(file, buffer, 8), 8);
ASSERT_STREQ(buffer, "asdfjklo");
ASSERT_TRUE(fs.eof(file));
ASSERT_TRUE(fs.close(file));
}
engine.disableService<MM::Services::FilesystemService>();
}
// TODO: readString
// TODO: fwrite ?
TEST(fs_service_system, read_json) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
{
auto j = fs.readJson("does_not_exist.json");
ASSERT_TRUE(j.empty());
}
ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
{
auto j1 = fs.readJson("wall_concrete-1_se_0.2.json");
//SPDLOG_INFO("dump: \n{}", j1.dump(4));
ASSERT_FALSE(j1.empty());
ASSERT_EQ(j1["type"], "tileset");
}
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, read_const_json) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
{
auto j = fs.readJson("does_not_exist.json");
ASSERT_TRUE(j.empty());
}
FS_CONST_MOUNT_FILE("json/file1.json",
R"({
"answer": {
"everything": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"nothing": null,
"object": {
"currency": "USD",
"value": 42.99
},
"pi": 3.141
})")
FS_CONST_MOUNT_FILE("/json/file2.json",
R"({
"answer": 42
})")
{
auto j1 = fs.readJson("/json/file1.json");
//std::cout << "dump: \n" << std::setw(4) << j1 << std::endl;
ASSERT_FALSE(j1.empty());
ASSERT_EQ(j1["happy"], true);
}
{
auto j2 = fs.readJson("json/file2.json");
//std::cout << "dump: \n" << std::setw(4) << j2 << std::endl;
ASSERT_FALSE(j2.empty());
ASSERT_EQ(j2["answer"], 42);
}
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, write_json) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
//ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
// taken from json README.md
nlohmann::json j = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
ASSERT_TRUE(fs.writeJson("write_test.json", j, 4));
engine.disableService<MM::Services::FilesystemService>();
}
//FS_CONST_MOUNT_FILE_STATIC("static_test_file.txt",
//R"(test text.)")
int main(int argc, char** argv) {
argv0 = argv[0];
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Binary file not shown.

View File

@ -0,0 +1,64 @@
unsigned char test_zip[] = {
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0xfa, 0x64,
0x42, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x00, 0x0d, 0x00, 0x20, 0x00, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66,
0x69, 0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x0d, 0x00, 0x07,
0x89, 0x81, 0x55, 0x5c, 0x20, 0x0b, 0x87, 0x5c, 0x89, 0x81, 0x55, 0x5c,
0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8,
0x03, 0x00, 0x00, 0x2b, 0x49, 0x2d, 0x2e, 0x51, 0xb0, 0x72, 0xe1, 0x02,
0x00, 0x50, 0x4b, 0x07, 0x08, 0x46, 0xb6, 0x5c, 0xe8, 0x0a, 0x00, 0x00,
0x00, 0x08, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08,
0x00, 0x08, 0x00, 0xe1, 0xbc, 0x7e, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xb8, 0x05, 0x00, 0x00, 0x1b, 0x00, 0x20, 0x00, 0x77,
0x61, 0x6c, 0x6c, 0x5f, 0x63, 0x6f, 0x6e, 0x63, 0x72, 0x65, 0x74, 0x65,
0x2d, 0x31, 0x5f, 0x73, 0x65, 0x5f, 0x30, 0x2e, 0x32, 0x2e, 0x6a, 0x73,
0x6f, 0x6e, 0x55, 0x54, 0x0d, 0x00, 0x07, 0x06, 0xf0, 0x9f, 0x5c, 0x06,
0xf0, 0x9f, 0x5c, 0x06, 0xf0, 0x9f, 0x5c, 0x75, 0x78, 0x0b, 0x00, 0x01,
0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x95, 0x94,
0x61, 0x8b, 0x84, 0x20, 0x10, 0x86, 0xbf, 0xf7, 0x2b, 0xc2, 0xcf, 0xb5,
0xa4, 0xd6, 0xc2, 0xf5, 0x57, 0x8e, 0x25, 0xa4, 0xa4, 0x84, 0xb2, 0x48,
0xf7, 0x96, 0x63, 0xd9, 0xff, 0x7e, 0x93, 0x9d, 0x1e, 0x0b, 0x67, 0x69,
0x04, 0xc9, 0x8c, 0x3e, 0xbe, 0x33, 0x0d, 0xef, 0x33, 0x45, 0xed, 0x3c,
0xde, 0x27, 0xa9, 0x50, 0x8d, 0x71, 0x96, 0xa4, 0x48, 0x4c, 0xac, 0xe7,
0xa8, 0x46, 0x0f, 0x36, 0x8e, 0x4d, 0x3b, 0xcb, 0x76, 0xe5, 0x9a, 0xe7,
0xb8, 0x51, 0xbc, 0x29, 0x2e, 0xe4, 0xb2, 0xc8, 0x1e, 0xb9, 0x6d, 0x03,
0x17, 0xfd, 0xa0, 0xe1, 0xe4, 0xb5, 0x70, 0xb1, 0x87, 0xe8, 0xf4, 0x80,
0x6a, 0x5a, 0x91, 0x2d, 0x34, 0xb1, 0xb5, 0x17, 0x12, 0xd5, 0x26, 0x2f,
0xd9, 0xe4, 0x27, 0x1b, 0xaa, 0x5a, 0x58, 0x2b, 0xe0, 0x86, 0x7d, 0xbf,
0xe6, 0xeb, 0xca, 0xc4, 0x26, 0xed, 0x33, 0x49, 0x7f, 0x9f, 0xa7, 0x5b,
0x39, 0x9e, 0x9a, 0x47, 0xd1, 0x6d, 0xc7, 0x5d, 0x42, 0x8b, 0x11, 0x12,
0xb4, 0x74, 0xa1, 0xd7, 0xcd, 0xf0, 0x20, 0xdc, 0xce, 0x77, 0x09, 0x8a,
0xab, 0xca, 0x06, 0xba, 0x2f, 0xbe, 0x2a, 0x31, 0x83, 0x46, 0x84, 0xa1,
0x3e, 0x8a, 0x6c, 0xc2, 0x16, 0x47, 0x89, 0x8d, 0x78, 0x85, 0xc0, 0xf5,
0x35, 0x7e, 0x13, 0xb0, 0x2b, 0x87, 0xfd, 0x45, 0x96, 0xee, 0x6f, 0x8e,
0x6f, 0x7f, 0x72, 0xb2, 0xd4, 0xcb, 0x21, 0x47, 0x9c, 0x1c, 0xc3, 0x27,
0x8c, 0x83, 0xfd, 0x20, 0x43, 0x09, 0x07, 0xd1, 0xff, 0x41, 0x96, 0x12,
0x0e, 0xfa, 0x38, 0x03, 0x05, 0xf7, 0xa8, 0x38, 0xac, 0x2d, 0xa2, 0x4b,
0xc4, 0xd3, 0x25, 0x8b, 0x89, 0x41, 0x1d, 0xf4, 0x29, 0xae, 0xe3, 0xa4,
0x3c, 0x25, 0x85, 0x76, 0x8a, 0x1e, 0xff, 0xbc, 0x88, 0xf2, 0xa8, 0x47,
0x54, 0x11, 0x39, 0x05, 0xb4, 0x3a, 0x9b, 0xcb, 0xd0, 0xda, 0xca, 0x83,
0x36, 0xb9, 0x49, 0x08, 0x44, 0xf9, 0x45, 0xc5, 0x92, 0xae, 0xa7, 0xa3,
0xf9, 0x86, 0x72, 0xc6, 0x64, 0x7d, 0x73, 0x77, 0x9b, 0xef, 0x65, 0x73,
0x36, 0x63, 0x3a, 0x5c, 0x1b, 0x4b, 0x72, 0x36, 0x05, 0x2e, 0x95, 0xbc,
0x7e, 0x00, 0x50, 0x4b, 0x07, 0x08, 0x20, 0xba, 0x28, 0x91, 0x30, 0x01,
0x00, 0x00, 0xb8, 0x05, 0x00, 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03,
0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0xfa, 0x64, 0x42, 0x4e, 0x46, 0xb6,
0x5c, 0xe8, 0x0a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0d, 0x00,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x81,
0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x69, 0x6c,
0x65, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x0d, 0x00, 0x07, 0x89, 0x81,
0x55, 0x5c, 0x20, 0x0b, 0x87, 0x5c, 0x89, 0x81, 0x55, 0x5c, 0x75, 0x78,
0x0b, 0x00, 0x01, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8, 0x03, 0x00,
0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x08, 0x00, 0x08,
0x00, 0xe1, 0xbc, 0x7e, 0x4e, 0x20, 0xba, 0x28, 0x91, 0x30, 0x01, 0x00,
0x00, 0xb8, 0x05, 0x00, 0x00, 0x1b, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x81, 0x65, 0x00, 0x00, 0x00, 0x77,
0x61, 0x6c, 0x6c, 0x5f, 0x63, 0x6f, 0x6e, 0x63, 0x72, 0x65, 0x74, 0x65,
0x2d, 0x31, 0x5f, 0x73, 0x65, 0x5f, 0x30, 0x2e, 0x32, 0x2e, 0x6a, 0x73,
0x6f, 0x6e, 0x55, 0x54, 0x0d, 0x00, 0x07, 0x06, 0xf0, 0x9f, 0x5c, 0x06,
0xf0, 0x9f, 0x5c, 0x06, 0xf0, 0x9f, 0x5c, 0x75, 0x78, 0x0b, 0x00, 0x01,
0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x50, 0x4b,
0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0xc4, 0x00,
0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x00
};
unsigned int test_zip_len = 728;

View File

@ -0,0 +1 @@
test :D

View File

@ -0,0 +1,80 @@
{ "columns":11,
"image":"wall_concrete-1_se_0.2.png",
"imageheight":160,
"imagewidth":352,
"margin":0,
"name":"wall_concrete-1_se_0.2",
"spacing":0,
"terrains":[
{
"name":"solid",
"tile":34
}],
"tilecount":55,
"tiledversion":"1.2.3",
"tileheight":32,
"tiles":[
{
"id":1,
"terrain":[0, 0, 0, -1]
},
{
"id":2,
"terrain":[0, 0, -1, 0]
},
{
"id":12,
"terrain":[0, -1, 0, 0]
},
{
"id":13,
"terrain":[-1, 0, 0, 0]
},
{
"id":19,
"terrain":[-1, 0, 0, -1]
},
{
"id":20,
"terrain":[0, -1, -1, 0]
},
{
"id":22,
"terrain":[-1, -1, -1, 0]
},
{
"id":23,
"terrain":[-1, -1, 0, 0]
},
{
"id":24,
"terrain":[-1, -1, 0, -1]
},
{
"id":33,
"terrain":[-1, 0, -1, 0]
},
{
"id":34,
"terrain":[0, 0, 0, 0]
},
{
"id":35,
"terrain":[0, -1, 0, -1]
},
{
"id":44,
"terrain":[-1, 0, -1, -1]
},
{
"id":45,
"terrain":[0, 0, -1, -1]
},
{
"id":46,
"terrain":[0, -1, -1, -1]
}],
"tilewidth":32,
"type":"tileset",
"version":1.2
}