Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Add texture metadata test
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoticgd committed Sep 5, 2023
1 parent 681e594 commit 4e48398
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ add_executable(diffdat diffdat.c)
target_link_libraries(diffdat libra)
add_executable(modmanager modmanager.c)
target_link_libraries(modmanager libra gui versioninfo)
add_executable(testtexturemeta testtexturemeta.c)
target_link_libraries(testtexturemeta libra)

if(WIN32)
# Set the windows codepage to UTF-8.
Expand Down
73 changes: 2 additions & 71 deletions libra/dat_container.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ const char* RA_dat_lump_type_name(u32 type_crc) {

// Testing

static RA_Result diff_buffers(const u8* lhs, u32 lhs_size, const u8* rhs, u32 rhs_size, const char* context, b8 print_hex_dump_on_failure);

RA_Result RA_dat_test(const u8* original, u32 original_size, const u8* repacked, u32 repacked_size, b8 print_hex_dump_on_failure) {
RA_Result result;

Expand Down Expand Up @@ -358,7 +356,7 @@ RA_Result RA_dat_test(const u8* original, u32 original_size, const u8* repacked,
} else {
snprintf(context, 256, "lump %x", original_lump->type_crc);
}
if((result = diff_buffers(original_data, original_lump->size, repacked_data, repacked_lump->size, context, print_hex_dump_on_failure)) != RA_SUCCESS) {
if((result = RA_diff_buffers(original_data, original_lump->size, repacked_data, repacked_lump->size, context, print_hex_dump_on_failure)) != RA_SUCCESS) {
return result;
}
}
Expand All @@ -369,76 +367,9 @@ RA_Result RA_dat_test(const u8* original, u32 original_size, const u8* repacked,
}

const char* end_context = "sections match but file data doesn't; issue with strings, lump ordering, DAT header, or alignment";
if((result = diff_buffers(original, original_size, repacked, repacked_size, end_context, print_hex_dump_on_failure)) != RA_SUCCESS) {
if((result = RA_diff_buffers(original, original_size, repacked, repacked_size, end_context, print_hex_dump_on_failure)) != RA_SUCCESS) {
return result;
}

return RA_SUCCESS;
}

static RA_Result diff_buffers(const u8* lhs, u32 lhs_size, const u8* rhs, u32 rhs_size, const char* context, b8 print_hex_dump_on_failure) {
u32 min_size = MIN(lhs_size, rhs_size);
u32 max_size = MAX(lhs_size, rhs_size);

u32 diff_offset = UINT32_MAX;
for(u32 i = 0; i < min_size; i++) {
if(lhs[i] != rhs[i]) {
diff_offset = i;
break;
}
}

if(diff_offset == UINT32_MAX) {
if(lhs_size == rhs_size) {
return RA_SUCCESS;
} else {
diff_offset = min_size;
}
}

RA_Result error = RA_FAILURE("%s differs at offset %x", context, diff_offset);
if(!print_hex_dump_on_failure) {
return error;
}

printf("%s\n", error->message);

s64 row_start = (diff_offset / 0x10) * 0x10;
s64 hexdump_begin = MAX(0, row_start - 0x50);
s64 hexdump_end = max_size;
for(s64 i = hexdump_begin; i < hexdump_end; i += 0x10) {
printf("%08x: ", (s32) i);
const u8* current = lhs;
u32 current_size = lhs_size;
for(u32 j = 0; j < 2; j++) {
for(s64 j = 0; j < 0x10; j++) {
s64 pos = i + j;
const char* colour = NULL;
if(pos < lhs_size && pos < rhs_size) {
if(lhs[pos] == rhs[pos]) {
colour = "32";
} else {
colour = "31";
}
} else if(pos < current_size) {
colour = "33";
} else {
printf(" ");
}
if(colour != NULL) {
printf("\033[%sm%02x\033[0m ", colour, current[pos]);
}
if(j % 4 == 3 && j != 0xf) {
printf(" ");
}
}
printf("| ");
current = rhs;
current_size = rhs_size;
}
printf("\n");
}
printf("\n");

return error;
}
29 changes: 14 additions & 15 deletions libra/table_of_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,33 @@ typedef struct {

typedef struct {
/* 0x00 */ u32 asset_type_hash;
/* 0x04 */ u32 file_size;
/* 0x08 */ u32 unknown_8;
/* 0x0c */ u32 unknown_c;
/* 0x10 */ u32 unknown_10;
/* 0x04 */ u32 block_sizes[4];
/* 0x14 */ u32 unknown_14;
/* 0x18 */ u32 unknown_18;
/* 0x1c */ u32 unknown_1c;
/* 0x20 */ u32 unknown_20;
} RA_TocAssetHeader;

typedef struct {
/* 0x00 */ u32 unknown_0;
/* 0x04 */ u32 unknown_4;
/* 0x08 */ u32 unknown_8;
/* 0x0c */ u32 unknown_c;
/* 0x10 */ u32 unknown_10;
/* 0x00 */ u64 unknown_0;
/* 0x08 */ u64 unknown_8;
/* 0x10 */ u32 width_in_texture_file;
/* 0x14 */ u32 unknown_14;
/* 0x18 */ u32 unknown_18;
/* 0x18 */ u32 height_in_texture_file;
/* 0x1c */ u32 unknown_1c;
/* 0x20 */ u32 unknown_20;
/* 0x24 */ u32 unknown_24;
/* 0x20 */ u16 unknown_20;
/* 0x22 */ u16 unknown_22;
/* 0x24 */ u32 format;
/* 0x28 */ u32 unknown_28;
/* 0x2c */ u32 unknown_2c;
/* 0x30 */ u32 unknown_30;
/* 0x34 */ u32 unknown_34;
/* 0x38 */ u32 unknown_38;
/* 0x3c */ u32 unknown_3c;
/* 0x40 */ u32 unknown_40;
/* 0x38 */ u32 total_size;
/* 0x3c */ u32 streamed_size;
/* 0x40 */ u8 unknown_40;
/* 0x41 */ u8 unknown_41;
/* 0x42 */ u8 unknown_42;
/* 0x43 */ u8 unknown_43;
/* 0x44 */ u32 unknown_44;
} RA_TocTextureMeta;
RA_ASSERT_SIZE(RA_TocTextureMeta, 0x48);
Expand Down
27 changes: 19 additions & 8 deletions libra/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ extern "C" {
#endif

typedef struct {
u32 unknown_0;
u32 unknown_4;
s16 width;
s16 height;
u16 width_in_texture_file;
u16 height_in_texture_file;
u32 unknown_10;
u8 format;
/* 0x00 */ u32 texture_size;
/* 0x04 */ u32 streamed_size;
/* 0x08 */ s16 width;
/* 0x0a */ s16 height;
/* 0x0c */ u16 width_in_texture_file;
/* 0x0e */ u16 height_in_texture_file;
/* 0x10 */ u16 unknown_10;
/* 0x12 */ u16 unknown_12;
/* 0x14 */ u32 format;
/* 0x18 */ u32 unknown_18;
/* 0x1c */ u16 unknown_1c;
/* 0x1e */ u16 unknown_1e;
/* 0x20 */ u8 unknown_20;
/* 0x21 */ u8 unknown_21;
/* 0x22 */ u8 unknown_22;
/* 0x23 */ u8 unknown_23;
/* 0x24 */ u32 unknown_24;
/* 0x28 */ u32 unknown_28;
} RA_TextureHeader;
RA_ASSERT_SIZE(RA_TextureHeader, 0x2c);

typedef enum {
DXGI_FORMAT_UNKNOWN = 0,
Expand Down
67 changes: 67 additions & 0 deletions libra/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,70 @@ void RA_crc_string_parse(RA_CRCString* crc_string, u8* file_data, u32 file_size)
crc_string->string = NULL;
}
}

RA_Result RA_diff_buffers(const u8* lhs, u32 lhs_size, const u8* rhs, u32 rhs_size, const char* context, b8 print_hex_dump_on_failure) {
u32 min_size = MIN(lhs_size, rhs_size);
u32 max_size = MAX(lhs_size, rhs_size);

u32 diff_offset = UINT32_MAX;
for(u32 i = 0; i < min_size; i++) {
if(lhs[i] != rhs[i]) {
diff_offset = i;
break;
}
}

if(diff_offset == UINT32_MAX) {
if(lhs_size == rhs_size) {
return RA_SUCCESS;
} else {
diff_offset = min_size;
}
}

RA_Result error = RA_FAILURE("%s differs at offset %x", context, diff_offset);
if(!print_hex_dump_on_failure) {
return error;
}

printf("%s\n", error->message);

s64 row_start = (diff_offset / 0x10) * 0x10;
s64 hexdump_begin = MAX(0, row_start - 0x50);
s64 hexdump_end = max_size;
for(s64 i = hexdump_begin; i < hexdump_end; i += 0x10) {
printf("%08x: ", (s32) i);
const u8* current = lhs;
u32 current_size = lhs_size;
for(u32 j = 0; j < 2; j++) {
for(s64 j = 0; j < 0x10; j++) {
s64 pos = i + j;
const char* colour = NULL;
if(pos < lhs_size && pos < rhs_size) {
if(lhs[pos] == rhs[pos]) {
colour = "32";
} else {
colour = "31";
}
} else if(pos < current_size) {
colour = "33";
} else {
printf(" ");
}
if(colour != NULL) {
printf("\033[%sm%02x\033[0m ", colour, current[pos]);
}
if(j % 4 == 3 && j != 0xf) {
printf(" ");
}
}
printf("| ");
current = rhs;
current_size = rhs_size;
}
printf("\n");
}
printf("\n");

return error;
}
2 changes: 2 additions & 0 deletions libra/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ void RA_crc_string_parse(RA_CRCString* crc_string, u8* file_data, u32 file_size)
#endif
#define RA_ASSERT_SIZE(type, size) __maybe_unused static char assert_size_ ##type[(sizeof(type) == size) ? 1 : -1]

RA_Result RA_diff_buffers(const u8* lhs, u32 lhs_size, const u8* rhs, u32 rhs_size, const char* context, b8 print_hex_dump_on_failure);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 4e48398

Please sign in to comment.