Extra2D/include/platform/file_module.h

180 lines
3.7 KiB
C
Raw Permalink Normal View History

#pragma once
#include <module/module.h>
#include <module/module_registry.h>
#include <string>
#include <types/base/types.h>
#include <vector>
namespace extra2d {
/**
* @brief
*/
struct FileInfo {
std::string path;
std::string name;
bool isDir = false;
int64 size = 0;
};
/**
* @brief
*/
struct FileData {
bool ok = false;
std::vector<uint8> data;
std::string error;
operator bool() const { return ok; }
const uint8 *ptr() const { return data.data(); }
size_t size() const { return data.size(); }
};
/**
* @brief
*
*
* Context
*/
class FileModule : public Module {
// 优先级为 2系统级模块在 TimerModule (优先级 4) 之前初始化
E2D_REGISTER_MODULE(FileModule, "File", 2)
public:
FileModule();
~FileModule() override;
// 禁止拷贝
FileModule(const FileModule &) = delete;
FileModule &operator=(const FileModule &) = delete;
// 允许移动
FileModule(FileModule &&) noexcept;
FileModule &operator=(FileModule &&) noexcept;
// Module 接口实现
bool init() override;
void shutdown() override;
/**
* @brief
*/
bool exists(const std::string &path) const;
/**
* @brief
*/
bool isDir(const std::string &path) const;
/**
* @brief
*/
FileData read(const std::string &path) const;
/**
* @brief
*/
std::string readString(const std::string &path) const;
/**
* @brief
*/
bool write(const std::string &path, const void *data, size_t size) const;
/**
* @brief
*/
bool writeString(const std::string &path, const std::string &content) const;
/**
* @brief
*/
bool append(const std::string &path, const void *data, size_t size) const;
/**
* @brief
*/
bool remove(const std::string &path) const;
/**
* @brief
*/
bool mkdir(const std::string &path) const;
/**
* @brief
*/
std::vector<FileInfo> listDir(const std::string &path) const;
/**
* @brief
*/
int64 fileSize(const std::string &path) const;
/**
* @brief
*/
std::string ext(const std::string &path) const;
/**
* @brief
*/
std::string fileName(const std::string &path) const;
/**
* @brief
*/
std::string dirName(const std::string &path) const;
/**
* @brief
*/
std::string join(const std::string &a, const std::string &b) const;
/**
* @brief
*/
std::string writableDir() const;
/**
* @brief
*/
void setAssetRoot(const std::string &root) { assetRoot_ = root; }
/**
* @brief
*/
const std::string &assetRoot() const { return assetRoot_; }
/**
* @brief
*/
std::string assetPath(const std::string &relPath) const;
/**
* @brief RomFS
* @param path
* @return "romfs:/"
*/
bool isRomfsPath(const std::string &path) const;
/**
* @brief RomFS Switch
* @param root RomFS "romfs:/"
*/
void setRomfsRoot(const std::string &root) { romfsRoot_ = root; }
/**
* @brief RomFS
*/
const std::string &romfsRoot() const { return romfsRoot_; }
private:
std::string assetRoot_;
std::string writableDir_;
std::string romfsRoot_ = "romfs:/";
};
} // namespace extra2d