libcmo21/LibCmo/CK2/CKFileReader.cpp

417 lines
13 KiB
C++
Raw Normal View History

2023-02-25 17:39:39 +08:00
#include "CKFile.hpp"
#include "CKGlobals.hpp"
2023-02-26 21:48:03 +08:00
#include "CKStateChunk.hpp"
2023-02-28 11:35:54 +08:00
#include "CKObjects.hpp"
2023-02-26 21:48:03 +08:00
#include "VxMemoryMappedFile.hpp"
#include "CKMinContext.hpp"
2023-02-25 17:39:39 +08:00
#include <memory>
2023-02-26 21:48:03 +08:00
namespace LibCmo::CK2 {
2023-02-25 17:39:39 +08:00
/*
* NOTE:
* We onlt support read Virtools file with FileVersion >= 7.
* The file with FileVersion < 7 is older than NeMo 1.0 (CK 1.1).
* No need to support them.
*/
2023-03-03 11:06:26 +08:00
CKERROR CKFile::ShallowLoad(CKSTRING u8_filename, CKFileDocument** out_doc) {
2023-02-25 22:58:28 +08:00
// preset value
*out_doc = nullptr;
// check file and open memory
if (u8_filename == nullptr) return CKERROR::CKERR_INVALIDPARAMETER;
2023-02-26 21:48:03 +08:00
std::unique_ptr<VxMath::VxMemoryMappedFile> mappedFile(new(std::nothrow) VxMath::VxMemoryMappedFile(u8_filename));
2023-02-25 22:58:28 +08:00
if (mappedFile == nullptr) {
2023-02-25 17:39:39 +08:00
this->m_MinCtx->Printf("Out of memory when creating Memory File.");
return CKERROR::CKERR_OUTOFMEMORY;
}
2023-02-25 22:58:28 +08:00
if (!mappedFile->IsValid()) {
this->m_MinCtx->Printf("Fail to create Memory File for \"%s\".", u8_filename);
2023-02-25 17:39:39 +08:00
return CKERROR::CKERR_INVALIDFILE;
}
// create document
2023-03-03 11:06:26 +08:00
std::unique_ptr<CKFileDocument> doc(new(std::nothrow) CKFileDocument());
2023-02-25 22:58:28 +08:00
if (doc == nullptr) return CKERROR::CKERR_OUTOFMEMORY;
2023-02-25 17:39:39 +08:00
// create buffer and start loading
2023-02-25 22:58:28 +08:00
std::unique_ptr<CKBufferParser> parser(new(std::nothrow) CKBufferParser(mappedFile->GetBase(), mappedFile->GetFileSize(), false));
CKERROR err = this->ReadFileHeader(parser.get(), doc.get());
2023-02-25 17:39:39 +08:00
if (err != CKERROR::CKERR_OK) return err;
2023-02-25 22:58:28 +08:00
err = this->ReadFileData(parser.get(), doc.get());
2023-02-25 17:39:39 +08:00
if (err != CKERROR::CKERR_OK) return err;
2023-02-25 22:58:28 +08:00
// unbind document and assign it
*out_doc = doc.release();
// other data will be free automatically
2023-02-25 17:39:39 +08:00
return CKERROR::CKERR_OK;
}
2023-03-03 11:06:26 +08:00
CKERROR CKFile::ReadFileHeader(CKBufferParser* ParserPtr, CKFileDocument* doc) {
2023-02-25 17:39:39 +08:00
std::unique_ptr<CKBufferParser> parser(new(std::nothrow) CKBufferParser(ParserPtr->GetBase(), ParserPtr->GetSize(), false));
2023-02-25 22:58:28 +08:00
if (parser == nullptr) return CKERROR::CKERR_OUTOFMEMORY;
parser->SetCursor(ParserPtr->GetCursor());
std::string name_conv;
2023-02-25 17:39:39 +08:00
// ========== read header ==========
// check header size
if (parser->GetSize() < sizeof(CKRawFileInfo)) return CKERROR::CKERR_INVALIDFILE;
2023-02-26 21:48:03 +08:00
if (std::memcmp(parser->GetPtr(), "Nemo Fi", sizeof(CKRawFileInfo::NeMo))) return CKERROR::CKERR_INVALIDFILE;
2023-02-25 17:39:39 +08:00
// read header
CKRawFileInfo rawHeader;
parser->Read(&rawHeader, sizeof(CKRawFileInfo));
// ========== header checker ==========
// check zero flag?
if (rawHeader.Zero) return CKERROR::CKERR_INVALIDFILE;
// check file version
if (rawHeader.FileVersion > 9 || rawHeader.FileVersion < 7) return CKERROR::CKERR_OBSOLETEVIRTOOLS;
// force reset too big product ver?
if (rawHeader.ProductVersion >= 12u) {
rawHeader.ProductVersion = 0u;
2023-02-28 14:04:38 +08:00
rawHeader.ProductBuild = 0x01010000u;
2023-02-25 17:39:39 +08:00
}
// ========== assign value ==========
2023-02-25 22:58:28 +08:00
doc->m_FileInfo.ProductVersion = rawHeader.ProductVersion;
doc->m_FileInfo.ProductBuild = rawHeader.ProductBuild;
doc->m_FileInfo.FileWriteMode = static_cast<CK_FILE_WRITEMODE>(rawHeader.FileWriteMode);
doc->m_FileInfo.CKVersion = rawHeader.CKVersion;
doc->m_FileInfo.FileVersion = rawHeader.FileVersion;
doc->m_FileInfo.FileSize = parser->GetSize();
doc->m_FileInfo.ManagerCount = rawHeader.ManagerCount;
doc->m_FileInfo.ObjectCount = rawHeader.ObjectCount;
doc->m_FileInfo.MaxIDSaved = rawHeader.MaxIDSaved;
doc->m_FileInfo.Hdr1PackSize = rawHeader.FileVersion >= 8 ? rawHeader.Hdr1PackSize : 0u;
doc->m_FileInfo.Hdr1UnPackSize = rawHeader.FileVersion >= 8 ? rawHeader.Hdr1UnPackSize : 0u;
doc->m_FileInfo.DataPackSize = rawHeader.DataPackSize;
doc->m_FileInfo.DataUnPackSize = rawHeader.DataUnPackSize;
doc->m_FileInfo.Crc = rawHeader.Crc;
2023-02-25 17:39:39 +08:00
// ========== crc and body unpacker ==========
2023-02-25 22:58:28 +08:00
if (doc->m_FileInfo.FileVersion >= 8) {
2023-02-25 17:39:39 +08:00
// crc checker for file ver >= 8
// reset crc field of header
rawHeader.Crc = 0u;
// compute crc
CKDWORD gotten_crc = CKComputeDataCRC(&rawHeader, sizeof(CKRawFileInfo), 0u);
parser->SetCursor(sizeof(CKRawFileInfo));
2023-02-25 22:58:28 +08:00
gotten_crc = CKComputeDataCRC(parser->GetPtr(), doc->m_FileInfo.Hdr1PackSize, gotten_crc);
parser->MoveCursor(doc->m_FileInfo.Hdr1PackSize);
gotten_crc = CKComputeDataCRC(parser->GetPtr(), doc->m_FileInfo.DataPackSize, gotten_crc);
2023-02-25 17:39:39 +08:00
2023-02-25 22:58:28 +08:00
if (gotten_crc != doc->m_FileInfo.Crc) {
this->m_MinCtx->Printf("Virtools file CRC error.");
return CKERROR::CKERR_FILECRCERROR;
}
2023-02-25 17:39:39 +08:00
// reset cursor
parser->SetCursor(sizeof(CKRawFileInfo));
// compare size to decide wheher use compress feature
2023-02-25 22:58:28 +08:00
void* decomp_buffer = CKUnPackData(doc->m_FileInfo.Hdr1UnPackSize, parser->GetPtr(), doc->m_FileInfo.Hdr1PackSize);
2023-02-25 17:39:39 +08:00
if (decomp_buffer != nullptr) {
2023-02-25 22:58:28 +08:00
parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.Hdr1UnPackSize, true));
2023-02-25 17:39:39 +08:00
if (parser == nullptr) {
2023-03-01 15:51:56 +08:00
delete[] reinterpret_cast<char*>(decomp_buffer);
2023-02-25 17:39:39 +08:00
return CKERROR::CKERR_OUTOFMEMORY;
}
}
}
// ========== object list read ==========
// file ver >= 7 have this features
2023-02-25 22:58:28 +08:00
{
2023-02-25 17:39:39 +08:00
// apply max id saved
2023-02-25 22:58:28 +08:00
doc->m_SaveIDMax = doc->m_FileInfo.MaxIDSaved;
2023-02-25 17:39:39 +08:00
// resize
2023-02-25 22:58:28 +08:00
doc->m_FileObjects.resize(doc->m_FileInfo.ObjectCount);
2023-02-25 17:39:39 +08:00
// read data
2023-02-25 22:58:28 +08:00
for (auto& fileobj : doc->m_FileObjects) {
2023-02-25 17:39:39 +08:00
// read basic fields
parser->Read(&(fileobj.ObjectId), sizeof(CK_ID));
parser->Read(&(fileobj.ObjectCid), sizeof(CK_CLASSID));
parser->Read(&(fileobj.FileIndex), sizeof(CKDWORD));
CKDWORD namelen;
parser->Read(&namelen, sizeof(CKDWORD));
if (namelen != 0) {
2023-02-25 22:58:28 +08:00
name_conv.resize(namelen);
parser->Read(name_conv.data(), namelen);
2023-02-26 21:48:03 +08:00
m_MinCtx->GetUtf8String(name_conv, fileobj.Name);
2023-02-25 17:39:39 +08:00
}
}
}
// ========== dep list read ==========
// file ver >= 8 have this feature
2023-02-25 22:58:28 +08:00
if (doc->m_FileInfo.FileVersion >= 8) {
2023-02-25 17:39:39 +08:00
// get size and resize
CKDWORD depSize;
parser->Read(&depSize, sizeof(CKDWORD));
2023-02-25 22:58:28 +08:00
doc->m_PluginDep.resize(depSize);
2023-02-25 17:39:39 +08:00
CKDWORD guid_size;
2023-02-25 22:58:28 +08:00
for (auto& dep : doc->m_PluginDep) {
2023-02-25 17:39:39 +08:00
// read category
parser->Read(&(dep.m_PluginCategory), sizeof(CK_PLUGIN_TYPE));
// get size and resize
parser->Read(&guid_size, sizeof(CKDWORD));
dep.m_Guids.resize(guid_size);
// read data
if (guid_size != 0) {
parser->Read(dep.m_Guids.data(), sizeof(CKGUID) * guid_size);
}
}
}
// ========== included file list read ==========
// file ver >= 8 have this feature
2023-02-25 22:58:28 +08:00
if (doc->m_FileInfo.FileVersion >= 8) {
2023-02-25 17:39:39 +08:00
// MARK: i don't knwo what is this!
int32_t hasIncludedFile;
parser->Read(&hasIncludedFile, sizeof(int32_t));
if (hasIncludedFile > 0) {
// read included file size and resize
CKDWORD includedFileCount;
parser->Read(&includedFileCount, sizeof(CKDWORD));
2023-02-25 22:58:28 +08:00
doc->m_IncludedFiles.resize(includedFileCount);
2023-02-25 17:39:39 +08:00
hasIncludedFile -= 4;
}
2023-02-25 22:58:28 +08:00
// MARK: backward pos
// backward with 0?
parser->MoveCursor(hasIncludedFile);
}
// ========== sync main parser ==========
if (doc->m_FileInfo.FileVersion >= 8) {
// file ver >= 8, use header offset
// because it have compress feature
ParserPtr->SetCursor(doc->m_FileInfo.Hdr1PackSize + sizeof(CKRawFileInfo));
} else {
// otherwise, sync with current parser.
ParserPtr->SetCursor(parser->GetCursor());
2023-02-25 17:39:39 +08:00
}
return CKERROR::CKERR_OK;
}
2023-03-03 11:06:26 +08:00
CKERROR CKFile::ReadFileData(CKBufferParser* ParserPtr, CKFileDocument* doc) {
2023-02-25 22:58:28 +08:00
std::unique_ptr<CKBufferParser> parser(new(std::nothrow) CKBufferParser(ParserPtr->GetBase(), ParserPtr->GetSize(), false));
if (parser == nullptr) return CKERROR::CKERR_OUTOFMEMORY;
parser->SetCursor(ParserPtr->GetCursor());
std::string name_conv;
// ========== compress feature process ==========
if (EnumsHelper::FlagEnumHas(doc->m_FileInfo.FileWriteMode, CK_FILE_WRITEMODE::CKFILE_CHUNKCOMPRESSED_OLD) ||
EnumsHelper::FlagEnumHas(doc->m_FileInfo.FileWriteMode, CK_FILE_WRITEMODE::CKFILE_WHOLECOMPRESSED)) {
void* decomp_buffer = CKUnPackData(doc->m_FileInfo.DataUnPackSize, parser->GetPtr(), doc->m_FileInfo.DataPackSize);
if (decomp_buffer != nullptr) {
parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.DataUnPackSize, true));
if (parser == nullptr) {
2023-03-01 15:51:56 +08:00
delete[] reinterpret_cast<char*>(decomp_buffer);
2023-02-25 22:58:28 +08:00
return CKERROR::CKERR_OUTOFMEMORY;
}
}
}
// ========== old file crc and obj list read ==========
// only file ver < 8 run this
if (doc->m_FileInfo.FileVersion < 8) {
// check crc
CKDWORD gotten_crc = CKComputeDataCRC(
parser->GetPtr(),
parser->GetSize() - parser->GetCursor(),
0u
);
if (gotten_crc != doc->m_FileInfo.Crc) {
this->m_MinCtx->Printf("Virtools file CRC error.");
return CKERROR::CKERR_FILECRCERROR;
}
// MARK: why read again? especially for file ver == 7.
// get save id max
parser->Read(&doc->m_SaveIDMax, sizeof(int32_t));
// get object count and resize
parser->Read(&doc->m_FileInfo.ObjectCount, sizeof(CKDWORD));
if (doc->m_FileObjects.empty()) {
doc->m_FileObjects.resize(doc->m_FileInfo.ObjectCount);
}
}
// ========== manager read ==========
// only file ver >= 6 have this
if (doc->m_FileInfo.ManagerCount != 0) {
doc->m_FileManagersData.resize(doc->m_FileInfo.ManagerCount);
CKDWORD stateChunkLen = 0u;
bool stateChkParseSuccess = false;
for (auto& mgr : doc->m_FileManagersData) {
// read guid
parser->Read(&(mgr.Manager), sizeof(CKGUID));
// read statechunk len
parser->Read(&stateChunkLen, sizeof(CKDWORD));
// check len
if (stateChunkLen == 0) {
mgr.Data = nullptr;
continue;
}
// read statechunk
mgr.Data = new(std::nothrow) CKStateChunk(doc, this->m_MinCtx);
2023-02-25 22:58:28 +08:00
if (mgr.Data != nullptr) {
stateChkParseSuccess = mgr.Data->ConvertFromBuffer(parser->GetPtr());
if (!stateChkParseSuccess) {
delete mgr.Data;
mgr.Data = nullptr;
}
}
parser->MoveCursor(stateChunkLen);
}
}
// ========== object read ==========
// only works file version >= 4. < 4 section has been removed.
if (doc->m_FileInfo.ObjectCount != 0) {
// new file reader section
CKDWORD stateChunkLen = 0u;
bool stateChkParseSuccess = false;
for (auto& obj : doc->m_FileObjects) {
// get statechunk len
parser->Read(&stateChunkLen, sizeof(CKDWORD));
// check state chunk len
if (stateChunkLen == 0) {
obj.Data = nullptr;
continue;
}
// read state chunk
obj.Data = new(std::nothrow) CKStateChunk(doc, this->m_MinCtx);
2023-02-25 22:58:28 +08:00
if (obj.Data != nullptr) {
stateChkParseSuccess = obj.Data->ConvertFromBuffer(parser->GetPtr());
if (!stateChkParseSuccess) {
delete obj.Data;
obj.Data = nullptr;
}
}
parser->MoveCursor(stateChunkLen);
}
}
// ========== included file get ==========
// before reading, we need switch back to original parser.
// and skip data chunk size
parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(ParserPtr->GetBase(), ParserPtr->GetSize(), false));
parser->MoveCursor(doc->m_FileInfo.DataPackSize);
// then we can read it.
if (doc->m_IncludedFiles.size() != 0) {
for (auto& file : doc->m_IncludedFiles) {
// get file name length and resize it
CKDWORD filenamelen = 0u;
parser->Read(&filenamelen, sizeof(CKDWORD));
name_conv.resize(filenamelen);
// read filename
if (filenamelen != 0) {
parser->Read(name_conv.data(), filenamelen);
2023-02-26 21:48:03 +08:00
m_MinCtx->GetUtf8String(name_conv, file);
2023-02-25 22:58:28 +08:00
}
// read file body length
CKDWORD filebodylen = 0u;
parser->Read(&filebodylen, sizeof(CKDWORD));
// read file body
FILE* fp = m_MinCtx->OpenTempFile(file.c_str(), false);
if (fp != nullptr) {
2023-02-26 21:48:03 +08:00
StreamHelper::CopyStream(parser->GetPtr(), fp, filebodylen);
2023-02-25 22:58:28 +08:00
fclose(fp);
}
// move to next
parser->MoveCursor(filebodylen);
}
}
return CKERROR::CKERR_OK;
2023-02-25 17:39:39 +08:00
}
2023-03-03 11:06:26 +08:00
CKERROR CKFile::DeepLoad(CKSTRING u8_filename, CKFileDocument** out_doc) {
2023-02-28 11:35:54 +08:00
// ========== prepare work ==========
// preset value
*out_doc = nullptr;
2023-03-03 11:06:26 +08:00
CKERROR err = CKERROR::CKERR_OK;
2023-02-28 11:35:54 +08:00
// get shallow document first
2023-03-03 11:06:26 +08:00
CKFileDocument* rawShallowDoc = nullptr;
err = this->ShallowLoad(u8_filename, &rawShallowDoc);
2023-02-28 11:35:54 +08:00
if (rawShallowDoc == nullptr) return err;
2023-03-03 11:06:26 +08:00
// use unique ptr wrap it as a deep doc
std::unique_ptr<CKFileDocument> deepDoc(rawShallowDoc);
2023-02-28 11:35:54 +08:00
if (err != CKERROR::CKERR_OK) return err;
// ========== create object first ==========
2023-03-03 11:06:26 +08:00
for (auto& obj : deepDoc->m_FileObjects) {
2023-02-28 11:35:54 +08:00
// todo: skip CK_LEVEL
// todo: resolve references
if (obj.Data == nullptr) continue;
// create object and assign created obj ckid
2023-03-03 11:06:26 +08:00
obj.ObjPtr = m_MinCtx->CreateCKObject(obj.ObjectId, obj.ObjectCid, obj.Name.c_str());
if (obj.ObjPtr == nullptr) {
obj.CreatedObject = 0u;
} else {
obj.CreatedObject = obj.ObjPtr->m_ID;
}
2023-02-28 11:35:54 +08:00
}
// ========== CKStateChunk remap ==========
// todo: remap
// todo: CK_LEVEL special proc
// ========== consume Managers ==========
// todo...
// ========== analyze objects CKStateChunk ==========
2023-03-03 11:06:26 +08:00
for (auto& obj : deepDoc->m_FileObjects) {
if (obj.Data == nullptr || obj.ObjPtr == nullptr) continue;
2023-02-28 11:35:54 +08:00
// todo: special treat for CK_LEVEL
// try parsing data
2023-02-28 14:04:38 +08:00
obj.Data->StartRead();
2023-03-05 22:31:11 +08:00
bool success = obj.ObjPtr->Load(obj.Data, deepDoc.get());
2023-02-28 14:04:38 +08:00
obj.Data->StopRead();
2023-03-05 22:31:11 +08:00
if (success) {
2023-03-03 11:06:26 +08:00
// if success, clear CKStateChunk*
delete obj.Data;
obj.Data = nullptr;
2023-03-05 22:31:11 +08:00
} else {
// if failed, delete it
m_MinCtx->DestroyCKObject(obj.ObjectId);
obj.ObjPtr = nullptr;
obj.CreatedObject = 0u;
2023-02-28 11:35:54 +08:00
}
}
// ========== finalize work ==========
// detach and return
*out_doc = deepDoc.release();
2023-02-26 13:57:32 +08:00
return CKERROR::CKERR_OK;
}
2023-02-25 17:39:39 +08:00
}