2017-10-30 23:44:40 +01:00
|
|
|
#ifndef _LIBRW_FILEINDEX_HPP_
|
|
|
|
#define _LIBRW_FILEINDEX_HPP_
|
2015-02-26 04:57:28 +01:00
|
|
|
|
2019-01-03 12:59:57 +01:00
|
|
|
#include <filesystem>
|
2018-06-21 23:41:10 +02:00
|
|
|
#include <unordered_map>
|
2020-05-15 17:35:57 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <loaders/LoaderIMG.hpp>
|
|
|
|
#include <rw/forward.hpp>
|
|
|
|
|
2016-08-04 02:50:41 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
class FileIndex {
|
2018-06-21 23:41:10 +02:00
|
|
|
public:
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
2018-06-21 23:41:10 +02:00
|
|
|
* @brief normalizeString Normalize a file path
|
|
|
|
* @param filePath the path to normalize
|
|
|
|
* @return normalized file path
|
2016-09-09 22:13:21 +02:00
|
|
|
*/
|
2018-06-21 23:41:10 +02:00
|
|
|
static std::string normalizeFilePath(const std::string &filePath);
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
2018-06-21 23:41:10 +02:00
|
|
|
* @brief indexDirectory index all files at path
|
|
|
|
* @param path the path to index
|
2016-09-09 22:13:21 +02:00
|
|
|
*
|
|
|
|
* 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);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief findFilePath finds disk path for a game data file
|
2018-06-21 23:41:10 +02:00
|
|
|
* @param filePath the path to find
|
2016-09-09 22:13:21 +02:00
|
|
|
* @return The file path as it exists on disk
|
2018-06-21 23:41:10 +02:00
|
|
|
* @throws if this FileIndex has not indexed the path
|
2016-09-09 22:13:21 +02:00
|
|
|
*/
|
2019-01-03 12:59:57 +01:00
|
|
|
std::filesystem::path findFilePath(const std::string &filePath) const;
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
2018-06-21 23:41:10 +02:00
|
|
|
* @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
|
2016-09-09 22:13:21 +02:00
|
|
|
*/
|
2018-07-06 22:05:43 +02:00
|
|
|
FileContentsInfo openFileRaw(const std::string &filePath) const;
|
2016-08-31 21:36:29 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
|
|
|
* Adds the files contained within the given Archive file to the
|
|
|
|
* file index.
|
2018-06-21 23:41:10 +02:00
|
|
|
* @param filePath path to the archive
|
|
|
|
* @throws if this FileIndex has not indexed the archive itself
|
2016-09-09 22:13:21 +02:00
|
|
|
*/
|
2018-06-21 23:41:10 +02:00
|
|
|
void indexArchive(const std::string &filePath);
|
2015-02-26 04:57:28 +01:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
|
|
|
* Returns a FileHandle for the file if it can be found in the
|
|
|
|
* file index, otherwise an empty FileHandle is returned.
|
2018-06-21 23:41:10 +02:00
|
|
|
* @param filePath name of the file to open
|
|
|
|
* @return FileHandle to the file, nullptr if this FileINdexed has not indexed the path
|
2016-09-09 22:13:21 +02:00
|
|
|
*/
|
2018-07-06 22:05:43 +02:00
|
|
|
FileContentsInfo openFile(const std::string &filePath);
|
2015-02-26 04:57:28 +01:00
|
|
|
|
|
|
|
private:
|
2018-06-21 23:41:10 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
2020-05-15 17:35:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief loaders_ Maps .img filepaths to its respective loader
|
|
|
|
*/
|
|
|
|
std::unordered_map<std::string, LoaderIMG> loaders_;
|
2016-08-28 02:31:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|