2020-05-15 17:35:57 +02:00
|
|
|
#include <loaders/LoaderIMG.hpp>
|
2013-07-02 08:40:43 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
|
|
|
#include <algorithm>
|
2017-10-30 23:44:40 +01:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
#include <rw/debug.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr size_t kAssetRecordSize{2048};
|
|
|
|
|
|
|
|
void to_lowercase_inplace(char* name) {
|
|
|
|
size_t len = std::strlen(name);
|
|
|
|
|
|
|
|
std::transform(
|
|
|
|
name, name+len, name,
|
|
|
|
[](char ch) -> char { return std::tolower(ch); }
|
|
|
|
);
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
}
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2017-11-02 05:01:00 +01:00
|
|
|
bool LoaderIMG::load(const rwfs::path& filepath) {
|
2020-05-15 17:35:57 +02:00
|
|
|
assert(m_archive.empty());
|
|
|
|
m_archive = filepath;
|
|
|
|
|
2017-11-02 05:01:00 +01:00
|
|
|
auto dirPath = filepath;
|
|
|
|
dirPath.replace_extension(".dir");
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
std::ifstream file(dirPath.string(), std::ios::binary);
|
|
|
|
if (!file.is_open()) {
|
|
|
|
RW_ERROR("Failed to open " + dirPath.string());
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
file.seekg(0, std::ios::end);
|
|
|
|
auto fileSize = file.tellg();
|
|
|
|
file.seekg(0);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
std::size_t expectedCount = fileSize / sizeof(LoaderIMGFile);
|
|
|
|
m_assets.resize(expectedCount);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
file.read(reinterpret_cast<char*>(m_assets.data()), expectedCount * sizeof(LoaderIMGFile));
|
|
|
|
|
|
|
|
if (file.fail() || file.gcount() != fileSize) {\
|
|
|
|
m_assets.resize(file.gcount() / sizeof(LoaderIMGFile));
|
|
|
|
RW_ERROR("Error reading records in IMG archive");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& asset : m_assets) {
|
|
|
|
to_lowercase_inplace(asset.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto imgPath = filepath;
|
|
|
|
imgPath.replace_extension(".img");
|
|
|
|
|
|
|
|
m_archive_stream.open(imgPath.string(), std::ios::binary);
|
|
|
|
if (!m_archive_stream.is_open()) {
|
|
|
|
RW_ERROR("Failed to open " << imgPath.string());
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2020-05-15 17:35:57 +02:00
|
|
|
|
|
|
|
return true;
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the information of a asset in the examining archive
|
2016-09-09 22:13:21 +02:00
|
|
|
bool LoaderIMG::findAssetInfo(const std::string& assetname,
|
|
|
|
LoaderIMGFile& out) {
|
2020-05-15 17:35:57 +02:00
|
|
|
for (const auto& asset : m_assets) {
|
|
|
|
if (assetname.compare(asset.name) == 0) {
|
2018-01-26 16:47:07 +01:00
|
|
|
out = asset;
|
2016-09-09 22:13:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 17:35:57 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
return false;
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 18:28:59 +02:00
|
|
|
std::unique_ptr<char[]> LoaderIMG::loadToMemory(const std::string& assetname) {
|
2020-05-15 17:35:57 +02:00
|
|
|
if (!m_archive_stream.is_open()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
LoaderIMGFile assetInfo;
|
|
|
|
bool found = findAssetInfo(assetname, assetInfo);
|
|
|
|
|
|
|
|
if (!found) {
|
2017-10-30 23:44:40 +01:00
|
|
|
RW_ERROR("Asset '" << assetname << "' not found!");
|
2016-09-09 22:13:21 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
std::streamsize asset_size = assetInfo.size * kAssetRecordSize;
|
|
|
|
auto raw_data = std::make_unique<char[]>(asset_size);
|
|
|
|
m_archive_stream.seekg(assetInfo.offset * kAssetRecordSize);
|
|
|
|
m_archive_stream.read(raw_data.get(), asset_size);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
if (m_archive_stream.gcount() != asset_size) {
|
|
|
|
RW_ERROR("Error reading asset " << assetInfo.name);
|
|
|
|
}
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
return raw_data;
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes the contents of assetname to filename
|
2016-09-09 22:13:21 +02:00
|
|
|
bool LoaderIMG::saveAsset(const std::string& assetname,
|
|
|
|
const std::string& filename) {
|
2018-08-05 18:28:59 +02:00
|
|
|
auto raw_data = loadToMemory(assetname);
|
2020-05-15 17:35:57 +02:00
|
|
|
if (!raw_data) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
LoaderIMGFile asset;
|
|
|
|
if (!findAssetInfo(assetname, asset)) {
|
2016-09-09 22:13:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
std::ofstream dump_file(filename, std::ios::binary);
|
|
|
|
if (!dump_file.is_open()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_file.write(raw_data.get(), kAssetRecordSize * asset.size);
|
|
|
|
RW_MESSAGE("Saved " << assetname << " to disk with filename " << filename);
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2020-05-15 17:35:57 +02:00
|
|
|
return true;
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|