refactor (4/?)

This commit is contained in:
2023-08-25 21:57:22 +08:00
parent c8c2c6e20b
commit 4a787610f1
17 changed files with 323 additions and 265 deletions

View File

@ -14,45 +14,34 @@ namespace LibCmo::CK2 {
* No need to support them.
*/
CKERROR CKFile::ShallowLoad(CKSTRING u8_filename) {
// preset value
*out_doc = nullptr;
CKERROR CKFileReader::ShallowLoad(CKSTRING u8_filename) {
// check document status
if (this->m_Done) LIBPANIC("Can not load multiple times for single CKFileReader.")
// check file and open memory
if (u8_filename == nullptr) return CKERROR::CKERR_INVALIDPARAMETER;
std::unique_ptr<VxMath::VxMemoryMappedFile> mappedFile(new(std::nothrow) VxMath::VxMemoryMappedFile(u8_filename));
if (mappedFile == nullptr) {
this->m_MinCtx->Printf("Out of memory when creating Memory File.");
return CKERROR::CKERR_OUTOFMEMORY;
}
std::unique_ptr<VxMath::VxMemoryMappedFile> mappedFile(new VxMath::VxMemoryMappedFile(u8_filename));
if (!mappedFile->IsValid()) {
this->m_MinCtx->Printf("Fail to create Memory File for \"%s\".", u8_filename);
this->m_Ctx->OutputToConsoleEx("Fail to create Memory File for \"%s\".", u8_filename);
return CKERROR::CKERR_INVALIDFILE;
}
// create document
std::unique_ptr<CKFileDocument> doc(new(std::nothrow) CKFileDocument());
if (doc == nullptr) return CKERROR::CKERR_OUTOFMEMORY;
// create buffer and start loading
std::unique_ptr<CKBufferParser> parser(new(std::nothrow) CKBufferParser(mappedFile->GetBase(), mappedFile->GetFileSize(), false));
CKERROR err = this->ReadFileHeader(parser.get(), doc.get());
std::unique_ptr<CKBufferParser> parser(new CKBufferParser(mappedFile->GetBase(), mappedFile->GetFileSize(), false));
CKERROR err = this->ReadFileHeader(parser.get());
if (err != CKERROR::CKERR_OK) return err;
err = this->ReadFileData(parser.get(), doc.get());
err = this->ReadFileData(parser.get());
if (err != CKERROR::CKERR_OK) return err;
// unbind document and assign it
*out_doc = doc.release();
// other data will be free automatically
return CKERROR::CKERR_OK;
}
CKERROR CKFile::ReadFileHeader(CKBufferParser* ParserPtr, CKFileDocument* doc) {
std::unique_ptr<CKBufferParser> parser(new(std::nothrow) CKBufferParser(ParserPtr->GetBase(), ParserPtr->GetSize(), false));
if (parser == nullptr) return CKERROR::CKERR_OUTOFMEMORY;
CKERROR CKFileReader::ReadFileHeader(CKBufferParser* ParserPtr) {
std::unique_ptr<CKBufferParser> parser(new CKBufferParser(ParserPtr->GetBase(), ParserPtr->GetSize(), false));
parser->SetCursor(ParserPtr->GetCursor());
std::string name_conv;
std::string name_conv, name_dest;
// ========== read header ==========
// check header size
@ -74,23 +63,23 @@ namespace LibCmo::CK2 {
}
// ========== assign value ==========
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;
this->m_FileInfo.ProductVersion = rawHeader.ProductVersion;
this->m_FileInfo.ProductBuild = rawHeader.ProductBuild;
this->m_FileInfo.FileWriteMode = static_cast<CK_FILE_WRITEMODE>(rawHeader.FileWriteMode);
this->m_FileInfo.CKVersion = rawHeader.CKVersion;
this->m_FileInfo.FileVersion = rawHeader.FileVersion;
this->m_FileInfo.FileSize = static_cast<CKDWORD>(parser->GetSize());
this->m_FileInfo.ManagerCount = rawHeader.ManagerCount;
this->m_FileInfo.ObjectCount = rawHeader.ObjectCount;
this->m_FileInfo.MaxIDSaved = rawHeader.MaxIDSaved;
this->m_FileInfo.Hdr1PackSize = rawHeader.FileVersion >= 8 ? rawHeader.Hdr1PackSize : 0u;
this->m_FileInfo.Hdr1UnPackSize = rawHeader.FileVersion >= 8 ? rawHeader.Hdr1UnPackSize : 0u;
this->m_FileInfo.DataPackSize = rawHeader.DataPackSize;
this->m_FileInfo.DataUnPackSize = rawHeader.DataUnPackSize;
this->m_FileInfo.Crc = rawHeader.Crc;
// ========== crc and body unpacker ==========
if (doc->m_FileInfo.FileVersion >= 8) {
if (this->m_FileInfo.FileVersion >= 8) {
// crc checker for file ver >= 8
// reset crc field of header
rawHeader.Crc = 0u;
@ -98,12 +87,12 @@ namespace LibCmo::CK2 {
// compute crc
CKDWORD gotten_crc = CKComputeDataCRC(&rawHeader, sizeof(CKRawFileInfo), 0u);
parser->SetCursor(sizeof(CKRawFileInfo));
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);
gotten_crc = CKComputeDataCRC(parser->GetPtr(), this->m_FileInfo.Hdr1PackSize, gotten_crc);
parser->MoveCursor(this->m_FileInfo.Hdr1PackSize);
gotten_crc = CKComputeDataCRC(parser->GetPtr(), this->m_FileInfo.DataPackSize, gotten_crc);
if (gotten_crc != doc->m_FileInfo.Crc) {
this->m_MinCtx->Printf("Virtools file CRC error.");
if (gotten_crc != this->m_FileInfo.Crc) {
this->m_Ctx->OutputToConsole("Virtools file CRC error.");
return CKERROR::CKERR_FILECRCERROR;
}
@ -111,13 +100,9 @@ namespace LibCmo::CK2 {
parser->SetCursor(sizeof(CKRawFileInfo));
// compare size to decide wheher use compress feature
void* decomp_buffer = CKUnPackData(doc->m_FileInfo.Hdr1UnPackSize, parser->GetPtr(), doc->m_FileInfo.Hdr1PackSize);
void* decomp_buffer = CKUnPackData(this->m_FileInfo.Hdr1UnPackSize, parser->GetPtr(), this->m_FileInfo.Hdr1PackSize);
if (decomp_buffer != nullptr) {
parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.Hdr1UnPackSize, true));
if (parser == nullptr) {
delete[] reinterpret_cast<char*>(decomp_buffer);
return CKERROR::CKERR_OUTOFMEMORY;
}
parser = std::unique_ptr<CKBufferParser>(new CKBufferParser(decomp_buffer, this->m_FileInfo.Hdr1UnPackSize, true));
}
}
@ -125,12 +110,12 @@ namespace LibCmo::CK2 {
// file ver >= 7 have this features
{
// apply max id saved
doc->m_SaveIDMax = doc->m_FileInfo.MaxIDSaved;
this->m_SaveIDMax = this->m_FileInfo.MaxIDSaved;
// resize
doc->m_FileObjects.resize(doc->m_FileInfo.ObjectCount);
this->m_FileObjects.resize(this->m_FileInfo.ObjectCount);
// read data
for (auto& fileobj : doc->m_FileObjects) {
for (auto& fileobj : this->m_FileObjects) {
// read basic fields
parser->Read(&(fileobj.ObjectId), sizeof(CK_ID));
parser->Read(&(fileobj.ObjectCid), sizeof(CK_CLASSID));
@ -141,21 +126,22 @@ namespace LibCmo::CK2 {
if (namelen != 0) {
name_conv.resize(namelen);
parser->Read(name_conv.data(), namelen);
m_MinCtx->GetUtf8String(name_conv, fileobj.Name);
m_Ctx->GetUtf8String(name_conv, name_dest);
fileobj.Name = name_dest;
}
}
}
// ========== dep list read ==========
// file ver >= 8 have this feature
if (doc->m_FileInfo.FileVersion >= 8) {
if (this->m_FileInfo.FileVersion >= 8) {
// get size and resize
CKDWORD depSize;
parser->Read(&depSize, sizeof(CKDWORD));
doc->m_PluginDep.resize(depSize);
this->m_PluginsDep.resize(depSize);
CKDWORD guid_size;
for (auto& dep : doc->m_PluginDep) {
for (auto& dep : this->m_PluginsDep) {
// read category
parser->Read(&(dep.m_PluginCategory), sizeof(CK_PLUGIN_TYPE));
// get size and resize
@ -171,7 +157,7 @@ namespace LibCmo::CK2 {
// ========== included file list read ==========
// file ver >= 8 have this feature
if (doc->m_FileInfo.FileVersion >= 8) {
if (this->m_FileInfo.FileVersion >= 8) {
// MARK: i don't knwo what is this!
int32_t hasIncludedFile;
parser->Read(&hasIncludedFile, sizeof(int32_t));
@ -180,7 +166,7 @@ namespace LibCmo::CK2 {
// read included file size and resize
CKDWORD includedFileCount;
parser->Read(&includedFileCount, sizeof(CKDWORD));
doc->m_IncludedFiles.resize(includedFileCount);
this->m_IncludedFiles.resize(includedFileCount);
hasIncludedFile -= 4;
}
@ -191,10 +177,10 @@ namespace LibCmo::CK2 {
}
// ========== sync main parser ==========
if (doc->m_FileInfo.FileVersion >= 8) {
if (this->m_FileInfo.FileVersion >= 8) {
// file ver >= 8, use header offset
// because it have compress feature
ParserPtr->SetCursor(doc->m_FileInfo.Hdr1PackSize + sizeof(CKRawFileInfo));
ParserPtr->SetCursor(this->m_FileInfo.Hdr1PackSize + sizeof(CKRawFileInfo));
} else {
// otherwise, sync with current parser.
ParserPtr->SetCursor(parser->GetCursor());
@ -203,7 +189,7 @@ namespace LibCmo::CK2 {
return CKERROR::CKERR_OK;
}
CKERROR CKFile::ReadFileData(CKBufferParser* ParserPtr, CKFileDocument* doc) {
CKERROR CKFileReader::ReadFileData(CKBufferParser* ParserPtr) {
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());
@ -211,52 +197,47 @@ namespace LibCmo::CK2 {
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)) {
if (EnumsHelper::Has(this->m_FileInfo.FileWriteMode, CK_FILE_WRITEMODE::CKFILE_CHUNKCOMPRESSED_OLD) ||
EnumsHelper::Has(this->m_FileInfo.FileWriteMode, CK_FILE_WRITEMODE::CKFILE_WHOLECOMPRESSED)) {
void* decomp_buffer = CKUnPackData(doc->m_FileInfo.DataUnPackSize, parser->GetPtr(), doc->m_FileInfo.DataPackSize);
void* decomp_buffer = CKUnPackData(this->m_FileInfo.DataUnPackSize, parser->GetPtr(), this->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) {
delete[] reinterpret_cast<char*>(decomp_buffer);
return CKERROR::CKERR_OUTOFMEMORY;
}
parser = std::unique_ptr<CKBufferParser>(new CKBufferParser(decomp_buffer, this->m_FileInfo.DataUnPackSize, true));
}
}
// ========== old file crc and obj list read ==========
// only file ver < 8 run this
if (doc->m_FileInfo.FileVersion < 8) {
if (this->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.");
if (gotten_crc != this->m_FileInfo.Crc) {
this->m_Ctx->OutputToConsole("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));
parser->Read(&this->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);
parser->Read(&this->m_FileInfo.ObjectCount, sizeof(CKDWORD));
if (this->m_FileObjects.empty()) {
this->m_FileObjects.resize(this->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);
if (this->m_FileInfo.ManagerCount != 0) {
this->m_ManagersData.resize(this->m_FileInfo.ManagerCount);
CKDWORD stateChunkLen = 0u;
bool stateChkParseSuccess = false;
for (auto& mgr : doc->m_FileManagersData) {
for (auto& mgr : this->m_ManagersData) {
// read guid
parser->Read(&(mgr.Manager), sizeof(CKGUID));
@ -269,13 +250,11 @@ namespace LibCmo::CK2 {
}
// read statechunk
mgr.Data = new(std::nothrow) CKStateChunk(doc, this->m_MinCtx);
if (mgr.Data != nullptr) {
stateChkParseSuccess = mgr.Data->ConvertFromBuffer(parser->GetPtr());
if (!stateChkParseSuccess) {
delete mgr.Data;
mgr.Data = nullptr;
}
mgr.Data = new CKStateChunk(&this->m_Visitor, this->m_Ctx);
stateChkParseSuccess = mgr.Data->ConvertFromBuffer(parser->GetPtr());
if (!stateChkParseSuccess) {
delete mgr.Data;
mgr.Data = nullptr;
}
parser->MoveCursor(stateChunkLen);
}
@ -283,11 +262,11 @@ namespace LibCmo::CK2 {
// ========== object read ==========
// only works file version >= 4. < 4 section has been removed.
if (doc->m_FileInfo.ObjectCount != 0) {
if (this->m_FileInfo.ObjectCount != 0) {
// new file reader section
CKDWORD stateChunkLen = 0u;
bool stateChkParseSuccess = false;
for (auto& obj : doc->m_FileObjects) {
for (auto& obj : this->m_FileObjects) {
// get statechunk len
parser->Read(&stateChunkLen, sizeof(CKDWORD));
// check state chunk len
@ -297,13 +276,11 @@ namespace LibCmo::CK2 {
}
// read state chunk
obj.Data = new(std::nothrow) CKStateChunk(doc, this->m_MinCtx);
if (obj.Data != nullptr) {
stateChkParseSuccess = obj.Data->ConvertFromBuffer(parser->GetPtr());
if (!stateChkParseSuccess) {
delete obj.Data;
obj.Data = nullptr;
}
obj.Data = new CKStateChunk(&this->m_Visitor, this->m_Ctx);
stateChkParseSuccess = obj.Data->ConvertFromBuffer(parser->GetPtr());
if (!stateChkParseSuccess) {
delete obj.Data;
obj.Data = nullptr;
}
parser->MoveCursor(stateChunkLen);
@ -314,11 +291,11 @@ namespace LibCmo::CK2 {
// 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);
parser->MoveCursor(this->m_FileInfo.DataPackSize);
// then we can read it.
if (doc->m_IncludedFiles.size() != 0) {
for (auto& file : doc->m_IncludedFiles) {
if (this->m_IncludedFiles.size() != 0) {
for (auto& file : this->m_IncludedFiles) {
// get file name length and resize it
CKDWORD filenamelen = 0u;
parser->Read(&filenamelen, sizeof(CKDWORD));
@ -327,7 +304,7 @@ namespace LibCmo::CK2 {
// read filename
if (filenamelen != 0) {
parser->Read(name_conv.data(), filenamelen);
m_MinCtx->GetUtf8String(name_conv, file);
m_Ctx->GetUtf8String(name_conv, file);
}
// read file body length
@ -335,7 +312,7 @@ namespace LibCmo::CK2 {
parser->Read(&filebodylen, sizeof(CKDWORD));
// read file body
FILE* fp = m_MinCtx->OpenTempFile(file.c_str(), false);
FILE* fp = m_Ctx->OpenTempFile(file.c_str(), false);
if (fp != nullptr) {
StreamHelper::CopyStream(parser->GetPtr(), fp, filebodylen);
fclose(fp);
@ -349,32 +326,26 @@ namespace LibCmo::CK2 {
return CKERROR::CKERR_OK;
}
CKERROR CKFile::DeepLoad(CKSTRING u8_filename, CKFileDocument** out_doc) {
CKERROR CKFileReader::DeepLoad(CKSTRING u8_filename) {
// ========== prepare work ==========
// preset value
*out_doc = nullptr;
CKERROR err = CKERROR::CKERR_OK;
// get shallow document first
CKFileDocument* rawShallowDoc = nullptr;
err = this->ShallowLoad(u8_filename, &rawShallowDoc);
if (rawShallowDoc == nullptr) return err;
// use unique ptr wrap it as a deep doc
std::unique_ptr<CKFileDocument> deepDoc(rawShallowDoc);
err = this->ShallowLoad(u8_filename);
if (err != CKERROR::CKERR_OK) return err;
// ========== create object first ==========
for (auto& obj : deepDoc->m_FileObjects) {
for (auto& obj : this->m_FileObjects) {
// todo: skip CK_LEVEL
// todo: resolve references
if (obj.Data == nullptr) continue;
// create object and assign created obj ckid
obj.ObjPtr = m_MinCtx->CreateCKObject(obj.ObjectId, obj.ObjectCid, obj.Name.c_str());
obj.ObjPtr = m_Ctx->CreateCKObject(obj.ObjectCid, obj.Name.c_str());
if (obj.ObjPtr == nullptr) {
obj.CreatedObject = 0u;
obj.CreatedObjectId = 0u;
} else {
obj.CreatedObject = obj.ObjPtr->m_ID;
obj.CreatedObjectId = obj.ObjPtr->GetID();
}
}
@ -386,13 +357,13 @@ namespace LibCmo::CK2 {
// todo...
// ========== analyze objects CKStateChunk ==========
for (auto& obj : deepDoc->m_FileObjects) {
for (auto& obj : this->m_FileObjects) {
if (obj.Data == nullptr || obj.ObjPtr == nullptr) continue;
// todo: special treat for CK_LEVEL
// try parsing data
obj.Data->StartRead();
bool success = obj.ObjPtr->Load(obj.Data, deepDoc.get());
bool success = obj.ObjPtr->Load(obj.Data, &this->m_Visitor);
obj.Data->StopRead();
if (success) {
// if success, clear CKStateChunk*
@ -400,15 +371,13 @@ namespace LibCmo::CK2 {
obj.Data = nullptr;
} else {
// if failed, delete it
m_MinCtx->DestroyCKObject(obj.ObjectId);
m_Ctx->DestroyCKObject(obj.ObjectId);
obj.ObjPtr = nullptr;
obj.CreatedObject = 0u;
obj.CreatedObjectId = 0u;
}
}
// ========== finalize work ==========
// detach and return
*out_doc = deepDoc.release();
return CKERROR::CKERR_OK;
}