2016-08-28 02:31:05 +02:00
|
|
|
#ifndef RWENGINE_FILEINDEX_HPP
|
|
|
|
#define RWENGINE_FILEINDEX_HPP
|
2017-10-29 23:50:59 +01:00
|
|
|
#include <platform/FileHandle.hpp>
|
|
|
|
#include <rw/filesystem.hpp>
|
2015-02-26 04:57:28 +01:00
|
|
|
|
|
|
|
#include <map>
|
2016-09-09 22:13:21 +02:00
|
|
|
#include <string>
|
2016-08-28 17:23:34 +02:00
|
|
|
#include <unordered_map>
|
2015-02-26 04:57:28 +01:00
|
|
|
|
2016-08-04 02:50:41 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
class FileIndex {
|
2016-08-28 17:23:34 +02:00
|
|
|
private:
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
|
|
|
* Mapping type (lower case name) => (on disk name)
|
|
|
|
*/
|
2017-10-29 23:50:59 +01:00
|
|
|
using FileSystemMap = std::unordered_map<rwfs::path, rwfs::path>;
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2017-10-29 23:50:59 +01:00
|
|
|
rwfs::path gamedatapath_;
|
2016-09-09 22:13:21 +02:00
|
|
|
FileSystemMap filesystemfiles_;
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2015-02-26 04:57:28 +01:00
|
|
|
public:
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
|
|
|
* @brief indexDirectory finds the true case for each file in the tree
|
|
|
|
* @param base_path
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2017-10-29 23:50:59 +01:00
|
|
|
void indexGameDirectory(const rwfs::path& base_path);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief findFilePath finds disk path for a game data file
|
|
|
|
* @param path
|
|
|
|
* @return The file path as it exists on disk
|
|
|
|
*/
|
2017-10-29 23:50:59 +01:00
|
|
|
rwfs::path findFilePath(std::string path) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto backslash = std::string::npos;
|
|
|
|
while ((backslash = path.find("\\")) != std::string::npos) {
|
|
|
|
path.replace(backslash, 1, "/");
|
|
|
|
}
|
|
|
|
auto realpath = gamedatapath_ / path;
|
|
|
|
std::string name = realpath.string();
|
|
|
|
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
2015-02-26 04:57:28 +01:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
return filesystemfiles_[name];
|
|
|
|
}
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
|
|
|
* @brief openFilePath opens a file on the disk
|
|
|
|
* @param file_path
|
|
|
|
* @return A handle for the file on the disk
|
|
|
|
*/
|
|
|
|
FileHandle openFilePath(const std::string& file_path);
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
struct IndexData {
|
|
|
|
/// Lowercase identifying filename
|
|
|
|
std::string filename;
|
|
|
|
/// Original filename
|
|
|
|
std::string originalName;
|
|
|
|
/// Containing directory
|
|
|
|
std::string directory;
|
|
|
|
/// The archive filename (if applicable)
|
|
|
|
std::string archive;
|
|
|
|
};
|
2016-08-28 17:23:34 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/**
|
|
|
|
* Adds the files contained within the given directory tree to the
|
|
|
|
* file index.
|
|
|
|
*/
|
2017-11-02 04:11:00 +01:00
|
|
|
void indexTree(const rwfs::path& root);
|
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.
|
|
|
|
*/
|
|
|
|
void indexArchive(const std::string& archive);
|
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.
|
|
|
|
*/
|
|
|
|
FileHandle openFile(const std::string& filename);
|
2015-02-26 04:57:28 +01:00
|
|
|
|
|
|
|
private:
|
2016-09-09 22:13:21 +02:00
|
|
|
std::map<std::string, IndexData> files;
|
2016-08-28 02:31:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|