Frostbite2D/Fostbite2D/include/fostbite2D/utils/asset.h

398 lines
11 KiB
C
Raw Normal View History

#pragma once
#include <filesystem>
#include <fostbite2D/types/type_alias.h>
#include <optional>
#include <string>
#include <vector>
namespace frostbite2D {
namespace fs = std::filesystem;
/**
* @brief
*/
struct FileInfo {
std::string name; ///< 文件名(包含扩展名)
std::string extension; ///< 文件扩展名(包含点,如 ".txt"
std::string fullPath; ///< 完整路径
uint64 size = 0; ///< 文件大小(字节)
bool isDirectory = false; ///< 是否为目录
bool isRegularFile = false; ///< 是否为普通文件
bool exists = false; ///< 是否存在
};
/**
* @brief
*
*
*
* UTF-8
*
* @example
* auto& asset = Asset::get();
* asset.setWorkingDirectory("D:/游戏/资源");
* std::string content;
* asset.readTextFile("配置/设置.json", content);
*/
class Asset {
public:
/**
* @brief
* @return
*/
static Asset &get();
Asset(const Asset &) = delete;
Asset &operator=(const Asset &) = delete;
// ---------------------------------------------------------------------------
// 文件读写
// ---------------------------------------------------------------------------
/**
* @brief
* @param path
* @param outContent
* @return true
*/
bool readTextFile(const std::string &path, std::string &outContent);
/**
* @brief
* @param path
* @param content
* @param append
* @return true
*/
bool writeTextFile(const std::string &path, const std::string &content,
bool append = false);
/**
* @brief
* @param path
* @param outData
* @return true
*/
bool readBinaryFile(const std::string &path, std::vector<uint8> &outData);
/**
* @brief
* @param path
* @param data
* @param append
* @return true
*/
bool writeBinaryFile(const std::string &path, const std::vector<uint8> &data,
bool append = false);
/**
* @brief 便
* @param path
* @return std::nullopt
*/
std::optional<std::string> readFileToString(const std::string &path);
/**
* @brief 便
* @param path
* @return std::nullopt
*/
std::optional<std::vector<uint8>> readFileToBytes(const std::string &path);
// ---------------------------------------------------------------------------
// 文件/目录检查
// ---------------------------------------------------------------------------
/**
* @brief
* @param path
* @return true
*/
bool exists(const std::string &path) const;
/**
* @brief
* @param path
* @return true
*/
bool isDirectory(const std::string &path) const;
/**
* @brief
* @param path
* @return true
*/
bool isRegularFile(const std::string &path) const;
/**
* @brief
* @param path
* @return true
*/
bool isEmpty(const std::string &path) const;
// ---------------------------------------------------------------------------
// 目录操作
// ---------------------------------------------------------------------------
/**
* @brief
* @param path
* @return true
*/
bool createDirectory(const std::string &path);
/**
* @brief
* @param path
* @return true
*/
bool createDirectories(const std::string &path);
/**
* @brief
* @param path
* @return true
*/
bool removeFile(const std::string &path);
/**
* @brief
* @param path
* @return true
*/
bool removeDirectory(const std::string &path);
// ---------------------------------------------------------------------------
// 文件操作
// ---------------------------------------------------------------------------
/**
* @brief
* @param from
* @param to
* @param overwrite
* @return true
*/
bool copyFile(const std::string &from, const std::string &to,
bool overwrite = false);
/**
* @brief
* @param from
* @param to
* @return true
*/
bool moveFile(const std::string &from, const std::string &to);
/**
* @brief
* @param oldPath
* @param newPath
* @return true
*/
bool rename(const std::string &oldPath, const std::string &newPath);
// ---------------------------------------------------------------------------
// 目录遍历
// ---------------------------------------------------------------------------
/**
* @brief
* @param directoryPath
* @param recursive
* @return
*/
std::vector<std::string> listFiles(const std::string &directoryPath,
bool recursive = false);
/**
* @brief
* @param directoryPath
* @param recursive
* @return
*/
std::vector<std::string> listDirectories(const std::string &directoryPath,
bool recursive = false);
/**
* @brief
* @param directoryPath
* @param recursive
* @return
*/
std::vector<std::string> listAll(const std::string &directoryPath,
bool recursive = false);
/**
* @brief
* @param directoryPath
* @param extension ".png"
* @param recursive
* @return
*/
std::vector<std::string>
listFilesWithExtension(const std::string &directoryPath,
const std::string &extension, bool recursive = false);
// ---------------------------------------------------------------------------
// 文件信息
// ---------------------------------------------------------------------------
/**
* @brief
* @param path
* @return
*/
FileInfo getFileInfo(const std::string &path);
/**
* @brief
* @param path
* @return
*/
uint64 getFileSize(const std::string &path) const;
// ---------------------------------------------------------------------------
// 路径处理
// ---------------------------------------------------------------------------
/**
* @brief
* @param path
* @return
*/
std::string getFileName(const std::string &path) const;
/**
* @brief
* @param path
* @return
*/
std::string getFileNameWithoutExtension(const std::string &path) const;
/**
* @brief
* @param path
* @return ".txt"
*/
std::string getExtension(const std::string &path) const;
/**
* @brief
* @param path
* @return
*/
std::string getParentPath(const std::string &path) const;
/**
* @brief
* @param path
* @return
*/
std::string getAbsolutePath(const std::string &path) const;
/**
* @brief
* @param path
* @return
*/
std::string getCanonicalPath(const std::string &path) const;
/**
* @brief
* @param left
* @param right
* @return
*/
std::string combinePath(const std::string &left,
const std::string &right) const;
/**
* @brief
* @param path
* @return
*/
std::string normalizePath(const std::string &path) const;
// ---------------------------------------------------------------------------
// 工作目录
// ---------------------------------------------------------------------------
/**
* @brief
* @return
*/
std::string getCurrentPath() const;
/**
* @brief
* @param path
* @return true
*/
bool setCurrentPath(const std::string &path);
/**
* @brief
*
*
*
* @param path
*/
void setWorkingDirectory(const std::string &path);
/**
* @brief
* @return
*/
const std::string &getWorkingDirectory() const;
/**
* @brief
* @param relativePath
* @return
*/
std::string resolvePath(const std::string &relativePath) const;
// ---------------------------------------------------------------------------
// 资源根目录
// ---------------------------------------------------------------------------
/**
* @brief
* @param root
*/
void setAssetRoot(const std::string &root);
/**
* @brief
* @return
*/
const std::string &getAssetRoot() const;
/**
* @brief
*
* //
*
* @param relativePath
* @return
*/
std::string resolveAssetPath(const std::string &relativePath) const;
private:
Asset() = default;
~Asset() = default;
fs::path toPath(const std::string &path) const;
std::string fromPath(const fs::path &path) const;
std::string resolveFullPath(const std::string &path) const;
std::string workingDirectory_;
std::string assetRoot_;
};
} // namespace frostbite2D