XPlor/libs/fastfile/360/fastfile_cod6_360.cpp

110 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "fastfile_cod6_360.h"
#include "zonefile_cod6_360.h"
#include "compression.h"
#include "encryption.h"
#include "utils.h"
#include <QFile>
#include <QDebug>
FastFile_COD6_360::FastFile_COD6_360()
: FastFile() {
SetCompany(COMPANY_INFINITY_WARD);
SetType(FILETYPE_FAST_FILE);
SetSignage(SIGNAGE_UNSIGNED);
SetMagic(0);
SetVersion(0);
SetPlatform("360");
SetGame("COD6");
}
FastFile_COD6_360::FastFile_COD6_360(const QByteArray& aData)
: FastFile_COD6_360() {
if (!aData.isEmpty()) {
Load(aData);
}
}
FastFile_COD6_360::FastFile_COD6_360(const QString aFilePath)
: FastFile_COD6_360() {
if (!aFilePath.isEmpty()) {
Load(aFilePath);
}
}
FastFile_COD6_360::~FastFile_COD6_360() {
}
QByteArray FastFile_COD6_360::GetBinaryData() {
return QByteArray();
}
bool FastFile_COD6_360::Load(const QString aFilePath) {
if (aFilePath.isEmpty()) {
return false;
}
// Check fastfile can be read
QFile *file = new QFile(aFilePath);
if (!file->open(QIODevice::ReadOnly)) {
qDebug() << QString("Error: Failed to open FastFile: %1!").arg(aFilePath);
return false;
}
// Decompress fastfile and close
const QString fastFileStem = aFilePath.section("/", -1, -1);
SetStem(fastFileStem);
if (!Load(file->readAll())) {
qDebug() << "Error: Failed to load fastfile: " << fastFileStem;
return false;
}
file->close();
// Open zone file after decompressing ff and writing
return true;
}
bool FastFile_COD6_360::Load(const QByteArray aData) {
const qint64 zlibOffset = Compression::FindZlibOffset(aData);
if (zlibOffset == -1)
{
qWarning() << "Z-Lib stream not found";
return false;
}
QByteArray compressed = aData.mid(zlibOffset);
// 2. Try plain decompression first ------------------------------
QByteArray decompressed = Compression::DecompressZLIB(compressed);
// 3. If that failed or looks too small, try stripping hash blocks
if (decompressed.isEmpty() || decompressed.size() < 1024)
{
QByteArray stripped = Compression::StripHashBlocks(compressed);
QByteArray retry = Compression::DecompressZLIB(stripped);
if (!retry.isEmpty())
decompressed.swap(retry);
}
if (decompressed.isEmpty())
{
qWarning() << "Unable to decompress fast-file";
return false;
}
// Optional keep a copy on disk for quick inspection
Utils::ExportData(GetBaseStem() + ".zone", decompressed);
// 4. Forward to zone-file loader --------------------------------
auto zoneFile = std::make_shared<ZoneFile_COD6_360>();
zoneFile->SetStem(GetStem());
zoneFile->Load(decompressed);
SetZoneFile(zoneFile);
return true;
}