libcmo21/Unvirt/StructFormatter.cpp

178 lines
6.0 KiB
C++
Raw Normal View History

2023-08-26 20:34:51 +08:00
#include <VTAll.hpp>
2023-08-27 22:14:02 +08:00
#include <CK2/CKStateChunk.hpp>
2023-02-14 16:28:37 +08:00
#include "StructFormatter.hpp"
#include "AccessibleValue.hpp"
#include "TerminalHelper.hpp"
2023-03-04 00:13:03 +08:00
namespace Unvirt::StructFormatter {
2023-08-27 22:14:02 +08:00
#pragma region Assist Functions
static void PrintCKSTRING(const LibCmo::TypeHelper::MKString& name) {
LibCmo::CK2::CKSTRING ckname = name.c_str();
if (ckname == nullptr) {
fputs(UNVIRT_TERMCOL_LIGHT_MAGENTA(("<anonymous>")), stdout);
} else {
fputs(ckname, stdout);
}
}
static void PrintPointer(const void* ptr) {
if (ptr == nullptr) {
fputs(UNVIRT_TERMCOL_LIGHT_CYAN(("<null>")), stdout);
} else {
#if UINTPTR_MAX == UINT32_MAX
// 32 bit. padding 8 zero
fprintf(stdout, "<0x%08" PRIXPTR ">", reinterpret_cast<uintptr_t>(ptr));
#else
// 64 bit. padding 16 zero
fprintf(stdout, "<0x%016" PRIXPTR ">", reinterpret_cast<uintptr_t>(ptr));
#endif
}
}
static void PrintCKGUID(const LibCmo::CK2::CKGUID& guid) {
fprintf(stdout, "<0x%08" PRIx32 ", 0x%08" PRIx32 ">", guid.d1, guid.d2);
}
static void PrintIdentifier(const LibCmo::CK2::CKDWORD val) {
fprintf(stdout, "0x%08" PRIx32, val);
}
template<class _Ty>
static void GeneralPrintList(
const std::vector<_Ty>& data, size_t page, size_t pageitems,
std::function<void()> printHdrFct, std::function<void(size_t,const _Ty&)> printEntryFct) {
size_t fulllen = data.size(),
startpos = page * pageitems,
fullpage = fulllen / pageitems;
// print header
printHdrFct();
// print body
for (size_t counter = startpos; counter < fulllen && (counter - startpos) < pageitems; ++counter) {
printEntryFct(counter, data[counter]);
}
fprintf(stdout, "Page %zu of %zu\n", page + 1, fullpage + 1);
}
#pragma endregion
2023-03-04 00:13:03 +08:00
void PrintCKFileInfo(const LibCmo::CK2::CKFileInfo& fileinfo) {
2023-08-27 22:14:02 +08:00
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("CKFileInfo\n")), stdout);
fprintf(stdout, "FileVersion: %" PRIu32 "\n", fileinfo.FileVersion);
fprintf(stdout, "CKVersion: %02" PRIX32 "/%02" PRIX32 "/%04" PRIX32 "\n",
2023-03-04 00:13:03 +08:00
(fileinfo.CKVersion >> 24) & 0xFF,
(fileinfo.CKVersion >> 16) & 0xFF,
(fileinfo.CKVersion >> 0) & 0xFFFF
);
2023-08-27 16:45:07 +08:00
LibCmo::CK2::CKDWORD product_series[4] {
2023-03-04 00:13:03 +08:00
(fileinfo.ProductBuild >> 24) & 0xFF,
(fileinfo.ProductBuild >> 16) & 0xFF,
(fileinfo.ProductBuild >> 8) & 0xFF,
(fileinfo.ProductBuild >> 0) & 0xFF,
};
2023-08-27 22:14:02 +08:00
fprintf(stdout, "Product (Version / Build): %" PRIu32 " / %" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
2023-03-04 00:13:03 +08:00
fileinfo.ProductVersion, product_series[0], product_series[1], product_series[2], product_series[3]
);
2023-08-27 22:14:02 +08:00
fprintf(stdout, "Save Flags: %s\n", Unvirt::AccessibleValue::GetFlagEnumName(
2023-08-26 20:34:51 +08:00
fileinfo.FileWriteMode, Unvirt::AccessibleValue::EnumDesc::CK_FILE_WRITEMODE
2023-03-04 00:13:03 +08:00
).c_str());
2023-08-27 22:14:02 +08:00
fprintf(stdout, "File Size: %s\n", Unvirt::AccessibleValue::GetReadableFileSize(fileinfo.FileSize).c_str());
2023-03-04 00:13:03 +08:00
2023-08-27 22:14:02 +08:00
fprintf(stdout, "Crc: 0x%" PRIX32 "\n", fileinfo.Crc);
fputc('\n', stdout);
2023-03-04 00:13:03 +08:00
2023-08-27 22:14:02 +08:00
fputs("Hdr1 (Pack / UnPack): ", stdout);
fprintf(stdout, "%s / ", Unvirt::AccessibleValue::GetReadableFileSize(fileinfo.Hdr1PackSize).c_str());
fprintf(stdout, "%s\n", Unvirt::AccessibleValue::GetReadableFileSize(fileinfo.Hdr1UnPackSize).c_str());
2023-03-04 00:13:03 +08:00
2023-08-27 22:14:02 +08:00
fputs("Data (Pack / UnPack): ", stdout);
fprintf(stdout, "%s / ", Unvirt::AccessibleValue::GetReadableFileSize(fileinfo.DataPackSize).c_str());
fprintf(stdout, "%s\n", Unvirt::AccessibleValue::GetReadableFileSize(fileinfo.DataUnPackSize).c_str());
fputc('\n', stdout);
2023-03-04 00:13:03 +08:00
2023-08-27 22:14:02 +08:00
fprintf(stdout, "Manager Count: %" PRIu32 "\nObject Count: %" PRIu32 "\nMax ID Saved: %" PRIu32 "\n",
2023-03-04 00:13:03 +08:00
fileinfo.ManagerCount, fileinfo.ObjectCount, fileinfo.MaxIDSaved
);
}
2023-02-14 16:28:37 +08:00
2023-08-26 20:34:51 +08:00
void PrintObjectList(const LibCmo::XContainer::XArray<LibCmo::CK2::CKFileObject>& ls, size_t page, size_t pageitems) {
2023-03-04 00:13:03 +08:00
2023-08-27 22:14:02 +08:00
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("CKFileObject\n")), stdout);
GeneralPrintList<LibCmo::CK2::CKFileObject>(ls, page, pageitems,
[]() -> void {
fputs("Index\tThis CK ID\tFile CK ID\tType\tCKObject\tCKStateChunk\tName\n", stdout);
},
[](size_t index, const LibCmo::CK2::CKFileObject& obj) -> void {
fprintf(stdout, "#%zu\t", index);
fprintf(stdout, "%" PRIu32 "\t%" PRIu32 "\t", obj.CreatedObjectId, obj.ObjectId);
fprintf(stdout, "%s\t", Unvirt::AccessibleValue::GetClassIdName(obj.ObjectCid).c_str());
PrintPointer(obj.ObjPtr);
fputc('\t', stdout);
PrintPointer(obj.Data);
fputc('\t', stdout);
PrintCKSTRING(obj.Name);
fputc('\n', stdout);
}
);
2023-03-04 00:13:03 +08:00
}
2023-08-26 20:34:51 +08:00
void PrintManagerList(const LibCmo::XContainer::XArray<LibCmo::CK2::CKFileManagerData>& ls, size_t page, size_t pageitems) {
2023-02-14 16:28:37 +08:00
2023-08-27 22:14:02 +08:00
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("CKFileManager\n")), stdout);
GeneralPrintList<LibCmo::CK2::CKFileManagerData>(ls, page, pageitems,
[]() -> void {
// print header
fputs("Index\tCKGUID\tCKStateChunk\n", stdout);
},
[](size_t index, const LibCmo::CK2::CKFileManagerData& mgr) -> void {
// print body
fprintf(stdout, "#%zu\t", index);
PrintCKGUID(mgr.Manager);
fputc('\t', stdout);
PrintPointer(mgr.Data);
fputc('\n', stdout);
}
);
2023-02-14 16:28:37 +08:00
}
2023-03-04 00:13:03 +08:00
2023-08-27 22:14:02 +08:00
void PrintCKObject(const LibCmo::CK2::ObjImpls::CKObject* obj) {
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("CKObject\n")), stdout);
fputs(UNVIRT_TERMCOL_LIGHT_RED(("Not Implemented.\n")), stdout);
2023-08-27 16:45:07 +08:00
}
2023-08-27 22:14:02 +08:00
void PrintCKBaseManager(const LibCmo::CK2::MgrImpls::CKBaseManager* mgr) {
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("CKBaseManager\n")), stdout);
fputs(UNVIRT_TERMCOL_LIGHT_RED(("Not Implemented.\n")), stdout);
2023-08-27 16:45:07 +08:00
}
2023-08-27 22:14:02 +08:00
void PrintCKStateChunk(const LibCmo::CK2::CKStateChunk* chunk) {
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("CKStateChunk\n")), stdout);
// print header
fputs("Identifier\tData Pointer\tData Size\n", stdout);
// print body
LibCmo::CK2::CKStateChunk* operchunk = const_cast<LibCmo::CK2::CKStateChunk*>(chunk);
operchunk->StartRead();
auto collection = operchunk->GetIdentifierProfile();
operchunk->StopRead();
for (const auto& profile : collection) {
PrintIdentifier(profile.m_Identifier);
fputc('\t', stdout);
PrintPointer(profile.m_DataPtr);
fputc('\t', stdout);
fprintf(stdout, "%" PRIckDWORD "\n", profile.m_AreaSize);
}
2023-08-27 16:45:07 +08:00
}
2023-02-14 16:28:37 +08:00
}