1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-09 12:22:34 +01:00
openrw/rwcore/platform/FileIndex.hpp

105 lines
3.1 KiB
C++
Raw Normal View History

#ifndef _LIBRW_FILEINDEX_HPP_
#define _LIBRW_FILEINDEX_HPP_
2019-01-03 12:59:57 +01:00
#include <filesystem>
#include <unordered_map>
#include <memory>
#include <loaders/LoaderIMG.hpp>
#include <rw/forward.hpp>
class FileIndex {
public:
/**
* @brief normalizeString Normalize a file path
* @param filePath the path to normalize
* @return normalized file path
*/
static std::string normalizeFilePath(const std::string &filePath);
/**
* @brief indexDirectory index all files at path
* @param path the path to index
*
* This is used to build the mapping of lower-case file paths to the
* true case on the file system for platforms where this is an issue.
*/
2019-01-03 12:59:57 +01:00
void indexTree(const std::filesystem::path &path);
/**
* @brief findFilePath finds disk path for a game data file
* @param filePath the path to find
* @return The file path as it exists on disk
* @throws if this FileIndex has not indexed the path
*/
2019-01-03 12:59:57 +01:00
std::filesystem::path findFilePath(const std::string &filePath) const;
/**
* @brief openFileRaw Opens a raw file on the disk
* @param filePath the path to open
* @return FileHandle to the file
* @throws if this FileIndex has not indexed the path
*/
FileContentsInfo openFileRaw(const std::string &filePath) const;
/**
* Adds the files contained within the given Archive file to the
* file index.
* @param filePath path to the archive
* @throws if this FileIndex has not indexed the archive itself
*/
void indexArchive(const std::string &filePath);
/**
* Returns a FileHandle for the file if it can be found in the
* file index, otherwise an empty FileHandle is returned.
* @param filePath name of the file to open
* @return FileHandle to the file, nullptr if this FileINdexed has not indexed the path
*/
FileContentsInfo openFile(const std::string &filePath);
private:
/**
* @brief Type of the indexed data.
*/
enum IndexedDataType {
/// Is a file on disk
FILE,
/// Is a member of an archive
ARCHIVE,
};
/**
* @brief All information of indexed data.
*/
struct IndexedData {
/// Type of indexed data.
IndexedDataType type;
/// Path of indexed data.
std::string path;
/// Extra data of assets (FIXME: use c++17 std::variant or std::option)
std::string assetData;
};
/**
* @brief indexedData_ A mapping from filepath (relative to game data path) to an IndexedData item.
*/
std::unordered_map<std::string, IndexedData> indexedData_;
/**
* @brief getIndexedDataAt Get IndexedData for filePath
* @param filePath the file path to get the IndexedData for
* @return IndexedData pointer if this FileIndex has indexed the filePath
* @throws If this FileIndex has not indexed filePath
*/
const IndexedData *getIndexedDataAt(const std::string &filePath) const;
/**
* @brief loaders_ Maps .img filepaths to its respective loader
*/
std::unordered_map<std::string, LoaderIMG> loaders_;
};
#endif