2017-10-30 23:44:40 +01:00
|
|
|
#include "platform/FileIndex.hpp"
|
|
|
|
|
2015-02-26 04:57:28 +01:00
|
|
|
#include <algorithm>
|
2018-06-21 23:41:10 +02:00
|
|
|
#include <cctype>
|
2016-09-09 22:13:21 +02:00
|
|
|
#include <fstream>
|
2018-06-21 23:41:10 +02:00
|
|
|
#include <sstream>
|
2017-10-30 23:44:40 +01:00
|
|
|
|
2018-08-05 18:28:59 +02:00
|
|
|
#include "platform/FileHandle.hpp"
|
2018-06-21 23:41:10 +02:00
|
|
|
#include "rw/debug.hpp"
|
|
|
|
|
|
|
|
std::string FileIndex::normalizeFilePath(const std::string &filePath) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
std::transform(filePath.cbegin(), filePath.cend(), std::ostreambuf_iterator<char>(oss), [](char c) {
|
|
|
|
if (c == '\\') {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
return static_cast<char>(std::tolower(c));
|
|
|
|
});
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:59:57 +01:00
|
|
|
void FileIndex::indexTree(const std::filesystem::path &path) {
|
2018-06-21 23:41:10 +02:00
|
|
|
// Remove the trailing "/" or "/." from base_path. Boost 1.66 and c++17 have different lexically_relative behavior.
|
2019-01-03 12:59:57 +01:00
|
|
|
std::filesystem::path basePath = (path / ".").lexically_normal();
|
2018-06-21 23:41:10 +02:00
|
|
|
basePath = basePath.parent_path();
|
2016-08-04 02:50:41 +02:00
|
|
|
|
2019-01-03 12:59:57 +01:00
|
|
|
for (const std::filesystem::path &path :
|
|
|
|
std::filesystem::recursive_directory_iterator(basePath)) {
|
|
|
|
if (!std::filesystem::is_regular_file(path)) {
|
2017-10-29 23:50:59 +01:00
|
|
|
continue;
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2018-06-21 23:41:10 +02:00
|
|
|
auto relPath = path.lexically_relative(basePath);
|
|
|
|
std::string relPathName = normalizeFilePath(relPath.string());
|
|
|
|
indexedData_[relPathName] = {IndexedDataType::FILE, path.string(), ""};
|
2017-10-29 23:50:59 +01:00
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
auto filename = normalizeFilePath(path.filename().string());
|
|
|
|
indexedData_[filename] = {IndexedDataType::FILE, path.string(), ""};
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2016-08-04 02:50:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
const FileIndex::IndexedData *FileIndex::getIndexedDataAt(const std::string &filePath) const {
|
|
|
|
auto normPath = normalizeFilePath(filePath);
|
|
|
|
return &indexedData_.at(normPath);
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:59:57 +01:00
|
|
|
std::filesystem::path FileIndex::findFilePath(const std::string &filePath) const {
|
2018-06-21 23:41:10 +02:00
|
|
|
return getIndexedDataAt(filePath)->path;
|
|
|
|
}
|
|
|
|
|
2018-07-06 22:05:43 +02:00
|
|
|
FileContentsInfo FileIndex::openFileRaw(const std::string &filePath) const {
|
2018-06-21 23:41:10 +02:00
|
|
|
const auto *indexData = getIndexedDataAt(filePath);
|
|
|
|
std::ifstream dfile(indexData->path, std::ios::binary);
|
2016-09-09 22:13:21 +02:00
|
|
|
if (!dfile.is_open()) {
|
2018-06-21 23:41:10 +02:00
|
|
|
throw std::runtime_error("Unable to open file: " + filePath);
|
|
|
|
}
|
|
|
|
|
2018-07-13 00:28:01 +02:00
|
|
|
#ifdef RW_DEBUG
|
2018-06-21 23:41:10 +02:00
|
|
|
if (indexData->type != IndexedDataType::FILE) {
|
|
|
|
RW_MESSAGE("Reading raw data from archive \"" << filePath << "\"");
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2018-06-21 23:41:10 +02:00
|
|
|
#endif
|
2016-08-31 21:36:29 +02:00
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
dfile.seekg(0, std::ios::end);
|
2016-09-09 22:13:21 +02:00
|
|
|
auto length = dfile.tellg();
|
|
|
|
dfile.seekg(0);
|
2018-08-05 18:28:59 +02:00
|
|
|
auto data = std::make_unique<char[]>(length);
|
|
|
|
dfile.read(data.get(), length);
|
2016-08-31 21:36:29 +02:00
|
|
|
|
2018-08-05 18:28:59 +02:00
|
|
|
return {std::move(data), static_cast<size_t>(length)};
|
2016-08-31 21:36:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
void FileIndex::indexArchive(const std::string &archive) {
|
2019-01-03 12:59:57 +01:00
|
|
|
std::filesystem::path path = findFilePath(archive);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
LoaderIMG& img = loaders_[path.string()];
|
2018-06-21 23:41:10 +02:00
|
|
|
if (!img.load(path.string())) {
|
|
|
|
throw std::runtime_error("Failed to load IMG archive: " + path.string());
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < img.getAssetCount(); ++i) {
|
2018-06-21 23:41:10 +02:00
|
|
|
auto &asset = img.getAssetInfoByIndex(i);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
if (asset.size == 0) continue;
|
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
std::string assetName = normalizeFilePath(asset.name);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
indexedData_[assetName] = {IndexedDataType::ARCHIVE, path.string(), asset.name};
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2015-02-26 04:57:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 22:05:43 +02:00
|
|
|
FileContentsInfo FileIndex::openFile(const std::string &filePath) {
|
2018-06-21 23:41:10 +02:00
|
|
|
auto cleanFilePath = normalizeFilePath(filePath);
|
|
|
|
auto indexedDataPos = indexedData_.find(cleanFilePath);
|
2018-07-06 22:05:43 +02:00
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
if (indexedDataPos == indexedData_.end()) {
|
2018-07-06 22:05:43 +02:00
|
|
|
return {nullptr, 0};
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
const auto &indexedData = indexedDataPos->second;
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-08-05 18:28:59 +02:00
|
|
|
std::unique_ptr<char[]> data = nullptr;
|
2016-09-09 22:13:21 +02:00
|
|
|
size_t length = 0;
|
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
if (indexedData.type == IndexedDataType::ARCHIVE) {
|
2020-05-15 17:35:57 +02:00
|
|
|
auto loaderPos = loaders_.find(indexedData.path);
|
|
|
|
if (loaderPos == loaders_.end()) {
|
|
|
|
throw std::runtime_error("IMG archive not indexed: " + indexedData.path);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
auto& loader = loaderPos->second;
|
2016-09-09 22:13:21 +02:00
|
|
|
LoaderIMGFile file;
|
2019-01-03 12:59:57 +01:00
|
|
|
auto filename = std::filesystem::path(indexedData.assetData).filename().string();
|
2020-05-15 17:35:57 +02:00
|
|
|
if (loader.findAssetInfo(filename, file)) {
|
2016-09-09 22:13:21 +02:00
|
|
|
length = file.size * 2048;
|
2020-05-15 17:35:57 +02:00
|
|
|
data = loader.loadToMemory(filename);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-21 23:41:10 +02:00
|
|
|
std::ifstream dfile(indexedData.path, std::ios::binary);
|
2016-09-09 22:13:21 +02:00
|
|
|
if (!dfile.is_open()) {
|
2018-06-21 23:41:10 +02:00
|
|
|
throw std::runtime_error("Unable to open file: " + indexedData.path);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 23:41:10 +02:00
|
|
|
dfile.seekg(0, std::ios::end);
|
2016-09-09 22:13:21 +02:00
|
|
|
length = dfile.tellg();
|
|
|
|
dfile.seekg(0);
|
2018-08-05 18:28:59 +02:00
|
|
|
data = std::make_unique<char[]>(length);
|
|
|
|
dfile.read(data.get(), length);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 18:28:59 +02:00
|
|
|
return {std::move(data), length};
|
2015-02-26 04:57:28 +01:00
|
|
|
}
|