Use BE(T) for JKRArchive

This commit is contained in:
PJB3005
2026-02-24 16:55:08 +01:00
parent 78be0f28fa
commit 99e0ca4a46
5 changed files with 26 additions and 105 deletions
+26 -94
View File
@@ -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);
}