mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-07-11 21:21:57 -04:00
Use BE(T) for JKRArchive
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "JSystem/JKernel/JKRCompression.h"
|
||||
#include "JSystem/JKernel/JKRFileLoader.h"
|
||||
#include "global.h"
|
||||
#include "dusk/endian.h"
|
||||
|
||||
class JKRHeap;
|
||||
|
||||
@@ -12,14 +13,14 @@ class JKRHeap;
|
||||
*
|
||||
*/
|
||||
struct SArcHeader {
|
||||
/* 0x00 */ u32 signature;
|
||||
/* 0x04 */ u32 file_length;
|
||||
/* 0x08 */ u32 header_length;
|
||||
/* 0x0C */ u32 file_data_offset;
|
||||
/* 0x10 */ u32 file_data_length;
|
||||
/* 0x14 */ u32 field_0x14;
|
||||
/* 0x18 */ u32 field_0x18;
|
||||
/* 0x1C */ u32 field_0x1c;
|
||||
/* 0x00 */ BE(u32) signature;
|
||||
/* 0x04 */ BE(u32) file_length;
|
||||
/* 0x08 */ BE(u32) header_length;
|
||||
/* 0x0C */ BE(u32) file_data_offset;
|
||||
/* 0x10 */ BE(u32) file_data_length;
|
||||
/* 0x14 */ BE(u32) field_0x14;
|
||||
/* 0x18 */ BE(u32) field_0x18;
|
||||
/* 0x1C */ BE(u32) field_0x1c;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -27,13 +28,13 @@ struct SArcHeader {
|
||||
*
|
||||
*/
|
||||
struct SArcDataInfo {
|
||||
/* 0x00 */ u32 num_nodes;
|
||||
/* 0x04 */ u32 node_offset;
|
||||
/* 0x08 */ u32 num_file_entries;
|
||||
/* 0x0C */ u32 file_entry_offset;
|
||||
/* 0x10 */ u32 string_table_length;
|
||||
/* 0x14 */ u32 string_table_offset;
|
||||
/* 0x18 */ u16 next_free_file_id;
|
||||
/* 0x00 */ BE(u32) num_nodes;
|
||||
/* 0x04 */ BE(u32) node_offset;
|
||||
/* 0x08 */ BE(u32) num_file_entries;
|
||||
/* 0x0C */ BE(u32) file_entry_offset;
|
||||
/* 0x10 */ BE(u32) string_table_length;
|
||||
/* 0x14 */ BE(u32) string_table_offset;
|
||||
/* 0x18 */ BE(u16) next_free_file_id;
|
||||
/* 0x1A */ bool sync_file_ids_and_indices;
|
||||
/* 0x1B */ u8 field_1b[5];
|
||||
};
|
||||
@@ -64,19 +65,19 @@ public:
|
||||
};
|
||||
|
||||
struct SDIDirEntry {
|
||||
u32 type;
|
||||
u32 name_offset;
|
||||
u16 field_0x8;
|
||||
u16 num_entries;
|
||||
u32 first_file_index;
|
||||
BE(u32) type;
|
||||
BE(u32) name_offset;
|
||||
BE(u16) field_0x8;
|
||||
BE(u16) num_entries;
|
||||
BE(u32) first_file_index;
|
||||
};
|
||||
|
||||
struct SDIFileEntry {
|
||||
u16 file_id;
|
||||
u16 name_hash;
|
||||
u32 type_flags_and_name_offset;
|
||||
u32 data_offset;
|
||||
u32 data_size;
|
||||
BE(u16) file_id;
|
||||
BE(u16) name_hash;
|
||||
BE(u32) type_flags_and_name_offset;
|
||||
BE(u32) data_offset;
|
||||
BE(u32) data_size;
|
||||
void* data;
|
||||
|
||||
u32 getNameOffset() const { return type_flags_and_name_offset & 0xFFFFFF; }
|
||||
@@ -220,75 +221,6 @@ protected:
|
||||
static u32 sCurrentDirID;
|
||||
};
|
||||
|
||||
#ifdef TARGET_PC
|
||||
#include "dusk/endian.h"
|
||||
|
||||
// Byte-swap archive header from Big-Endian to host after loading from disk
|
||||
inline void JKRSwapArcHeader(SArcHeader* h) {
|
||||
h->signature = be32(h->signature);
|
||||
h->file_length = be32(h->file_length);
|
||||
h->header_length = be32(h->header_length);
|
||||
h->file_data_offset = be32(h->file_data_offset);
|
||||
h->file_data_length = be32(h->file_data_length);
|
||||
h->field_0x14 = be32(h->field_0x14);
|
||||
h->field_0x18 = be32(h->field_0x18);
|
||||
h->field_0x1c = be32(h->field_0x1c);
|
||||
}
|
||||
|
||||
// Byte-swap archive data info block from Big-Endian to host
|
||||
inline void JKRSwapArcDataInfo(SArcDataInfo* info) {
|
||||
info->num_nodes = be32(info->num_nodes);
|
||||
info->node_offset = be32(info->node_offset);
|
||||
info->num_file_entries = be32(info->num_file_entries);
|
||||
info->file_entry_offset = be32(info->file_entry_offset);
|
||||
info->string_table_length = be32(info->string_table_length);
|
||||
info->string_table_offset = be32(info->string_table_offset);
|
||||
info->next_free_file_id = be16(info->next_free_file_id);
|
||||
}
|
||||
|
||||
// Byte-swap all directory entries
|
||||
inline void JKRSwapDirEntries(JKRArchive::SDIDirEntry* nodes, u32 count) {
|
||||
for (u32 i = 0; i < count; i++) {
|
||||
nodes[i].type = be32(nodes[i].type);
|
||||
nodes[i].name_offset = be32(nodes[i].name_offset);
|
||||
nodes[i].field_0x8 = be16(nodes[i].field_0x8);
|
||||
nodes[i].num_entries = be16(nodes[i].num_entries);
|
||||
nodes[i].first_file_index = be32(nodes[i].first_file_index);
|
||||
}
|
||||
}
|
||||
|
||||
// Byte-swap all file entries
|
||||
inline void JKRSwapFileEntries(JKRArchive::SDIFileEntry* files, u32 count) {
|
||||
for (u32 i = 0; i < count; i++) {
|
||||
files[i].file_id = be16(files[i].file_id);
|
||||
files[i].name_hash = be16(files[i].name_hash);
|
||||
files[i].type_flags_and_name_offset = be32(files[i].type_flags_and_name_offset);
|
||||
files[i].data_offset = be32(files[i].data_offset);
|
||||
files[i].data_size = be32(files[i].data_size);
|
||||
// data pointer is runtime-only, no swap needed
|
||||
}
|
||||
}
|
||||
|
||||
// Swap all archive structures after loading from disk
|
||||
inline void JKRSwapArchiveMemory(SArcDataInfo* arcInfo) {
|
||||
// First swap the info block itself to read offsets
|
||||
JKRSwapArcDataInfo(arcInfo);
|
||||
|
||||
// Then swap directory and file entries using the now-native offsets
|
||||
JKRArchive::SDIDirEntry* nodes = (JKRArchive::SDIDirEntry*)((u8*)arcInfo + arcInfo->node_offset);
|
||||
JKRArchive::SDIFileEntry* files = (JKRArchive::SDIFileEntry*)((u8*)arcInfo + arcInfo->file_entry_offset);
|
||||
|
||||
JKRSwapDirEntries(nodes, arcInfo->num_nodes);
|
||||
JKRSwapFileEntries(files, arcInfo->num_file_entries);
|
||||
}
|
||||
#else
|
||||
inline void JKRSwapArcHeader(SArcHeader*) {}
|
||||
inline void JKRSwapArcDataInfo(SArcDataInfo*) {}
|
||||
inline void JKRSwapDirEntries(JKRArchive::SDIDirEntry*, u32) {}
|
||||
inline void JKRSwapFileEntries(JKRArchive::SDIFileEntry*, u32) {}
|
||||
inline void JKRSwapArchiveMemory(SArcDataInfo*) {}
|
||||
#endif
|
||||
|
||||
inline JKRCompression JKRConvertAttrToCompressionType(int attr) {
|
||||
return JKRArchive::convertAttrToCompressionType(attr);
|
||||
}
|
||||
|
||||
@@ -121,7 +121,6 @@ bool JKRAramArchive::open(s32 entryNum) {
|
||||
JKRDvdToMainRam(entryNum, (u8*)mem, EXPAND_SWITCH_UNKNOWN1, 32, NULL,
|
||||
JKRDvdRipper::ALLOC_DIRECTION_FORWARD, 0, &mCompression, NULL);
|
||||
DCInvalidateRange(mem, 32);
|
||||
JKRSwapArcHeader(mem);
|
||||
int alignment = mMountDirection == MOUNT_DIRECTION_HEAD ? 32 : -32;
|
||||
u32 alignedSize = ALIGN_NEXT(mem->file_data_offset, 32);
|
||||
mArcInfoBlock = (SArcDataInfo*)JKRAllocFromHeap(mHeap, alignedSize, alignment);
|
||||
@@ -131,7 +130,6 @@ bool JKRAramArchive::open(s32 entryNum) {
|
||||
JKRDvdToMainRam(entryNum, (u8*)mArcInfoBlock, EXPAND_SWITCH_UNKNOWN1, alignedSize, NULL,
|
||||
JKRDvdRipper::ALLOC_DIRECTION_FORWARD, 32, NULL, NULL);
|
||||
DCInvalidateRange(mArcInfoBlock, alignedSize);
|
||||
JKRSwapArchiveMemory(mArcInfoBlock);
|
||||
|
||||
mNodes = (SDIDirEntry*)((u8*)mArcInfoBlock + mArcInfoBlock->node_offset);
|
||||
mFiles = (SDIFileEntry*)((u8*)mArcInfoBlock + mArcInfoBlock->file_entry_offset);
|
||||
|
||||
@@ -88,7 +88,6 @@ bool JKRCompArchive::open(s32 entryNum) {
|
||||
|
||||
JKRDvdToMainRam(entryNum, (u8 *)arcHeader, EXPAND_SWITCH_UNKNOWN1, 32, NULL, JKRDvdRipper::ALLOC_DIRECTION_FORWARD, 0, &mCompression, NULL);
|
||||
DCInvalidateRange(arcHeader, 32);
|
||||
JKRSwapArcHeader(arcHeader);
|
||||
|
||||
mSizeOfMemPart = arcHeader->field_0x14;
|
||||
mSizeOfAramPart = arcHeader->field_0x18;
|
||||
@@ -109,7 +108,6 @@ bool JKRCompArchive::open(s32 entryNum) {
|
||||
JKRDvdToMainRam(entryNum, (u8 *)mArcInfoBlock, EXPAND_SWITCH_UNKNOWN1, (uintptr_t)arcHeader->file_data_offset + mSizeOfMemPart,
|
||||
NULL, JKRDvdRipper::ALLOC_DIRECTION_FORWARD, 0x20, NULL, NULL);
|
||||
DCInvalidateRange(mArcInfoBlock, (uintptr_t)arcHeader->file_data_offset + mSizeOfMemPart);
|
||||
JKRSwapArchiveMemory(mArcInfoBlock);
|
||||
field_0x64 = (uintptr_t)mArcInfoBlock + arcHeader->file_data_offset;
|
||||
|
||||
if (mSizeOfAramPart != 0) {
|
||||
@@ -158,7 +156,6 @@ bool JKRCompArchive::open(s32 entryNum) {
|
||||
else {
|
||||
// arcHeader + 1 should lead to 0x20, which is the data after the header
|
||||
JKRHeap::copyMemory((u8 *)mArcInfoBlock, arcHeader + 1, (arcHeader->file_data_offset + mSizeOfMemPart));
|
||||
JKRSwapArchiveMemory(mArcInfoBlock);
|
||||
field_0x64 = (uintptr_t)mArcInfoBlock + arcHeader->file_data_offset;
|
||||
if (mSizeOfAramPart != 0) {
|
||||
mAramPart = (JKRAramBlock*)JKRAllocFromAram(mSizeOfAramPart, JKRAramHeap::HEAD);
|
||||
|
||||
@@ -76,7 +76,6 @@ bool JKRDvdArchive::open(s32 entryNum) {
|
||||
JKRDvdToMainRam(entryNum, (u8*)arcHeader, EXPAND_SWITCH_UNKNOWN1, sizeof(SArcHeader), NULL,
|
||||
JKRDvdRipper::ALLOC_DIRECTION_FORWARD, 0, &mCompression, NULL);
|
||||
DCInvalidateRange(arcHeader, sizeof(SArcHeader));
|
||||
JKRSwapArcHeader(arcHeader);
|
||||
|
||||
int alignment;
|
||||
alignment = mMountDirection == MOUNT_DIRECTION_HEAD ? 0x20 : -0x20;
|
||||
@@ -91,7 +90,6 @@ bool JKRDvdArchive::open(s32 entryNum) {
|
||||
arcHeader->file_data_offset, NULL, JKRDvdRipper::ALLOC_DIRECTION_FORWARD,
|
||||
sizeof(SArcHeader), NULL, NULL);
|
||||
DCInvalidateRange(mArcInfoBlock, arcHeader->file_data_offset);
|
||||
JKRSwapArchiveMemory(mArcInfoBlock);
|
||||
|
||||
mNodes = (SDIDirEntry*)((intptr_t)&mArcInfoBlock->num_nodes + mArcInfoBlock->node_offset);
|
||||
mFiles = (SDIFileEntry*)((intptr_t)&mArcInfoBlock->num_nodes + mArcInfoBlock->file_entry_offset);
|
||||
|
||||
@@ -89,10 +89,8 @@ bool JKRMemArchive::open(s32 entryNum, JKRArchive::EMountDirection mountDirectio
|
||||
mMountMode = UNKNOWN_MOUNT_MODE;
|
||||
}
|
||||
else {
|
||||
JKRSwapArcHeader(mArcHeader);
|
||||
JUT_ASSERT(438, mArcHeader->signature == 'RARC');
|
||||
mArcInfoBlock = (SArcDataInfo *)((u8 *)mArcHeader + mArcHeader->header_length);
|
||||
JKRSwapArchiveMemory(mArcInfoBlock);
|
||||
mNodes = (SDIDirEntry *)((u8 *)&mArcInfoBlock->num_nodes + mArcInfoBlock->node_offset);
|
||||
mFiles = (SDIFileEntry *)((u8 *)&mArcInfoBlock->num_nodes + mArcInfoBlock->file_entry_offset);
|
||||
mStringTable = (char *)((u8 *)&mArcInfoBlock->num_nodes + mArcInfoBlock->string_table_offset);
|
||||
@@ -113,10 +111,8 @@ bool JKRMemArchive::open(s32 entryNum, JKRArchive::EMountDirection mountDirectio
|
||||
|
||||
bool JKRMemArchive::open(void* buffer, u32 bufferSize, JKRMemBreakFlag flag) {
|
||||
mArcHeader = (SArcHeader *)buffer;
|
||||
JKRSwapArcHeader(mArcHeader);
|
||||
JUT_ASSERT(491, mArcHeader->signature == 'RARC');
|
||||
mArcInfoBlock = (SArcDataInfo *)((u8 *)mArcHeader + mArcHeader->header_length);
|
||||
JKRSwapArchiveMemory(mArcInfoBlock);
|
||||
mNodes = (SDIDirEntry *)((u8 *)&mArcInfoBlock->num_nodes + mArcInfoBlock->node_offset);
|
||||
mFiles = (SDIFileEntry *)((u8 *)&mArcInfoBlock->num_nodes + mArcInfoBlock->file_entry_offset);
|
||||
mStringTable = (char *)((u8 *)&mArcInfoBlock->num_nodes + mArcInfoBlock->string_table_offset);
|
||||
|
||||
Reference in New Issue
Block a user