Skip to content

Commit

Permalink
Apply texture prefix in replacer
Browse files Browse the repository at this point in the history
Also, fixed the last warning.
  • Loading branch information
Torphedo committed Apr 12, 2024
1 parent 31a1410 commit 3aca2c2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion alr/src/alr.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ bool alr_parse(char* alr_filename, flags options, alr_interface handlers) {
}
resource_entry* entries = (resource_entry*)(res_chunk_buf + sizeof(u32));

fseek(alr, sizeof(u32) * -1, SEEK_CUR);
fseek(alr, -1 * (signed long)sizeof(u32), SEEK_CUR);
long pos = ftell(alr);
fread(res_chunk_buf, (sizeof(*entries) * res_header.array_size) + sizeof(u32), 1, alr);

Expand Down
27 changes: 19 additions & 8 deletions alr/src/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ u32 u32_min(u32 x, u32 y) {
return y;
}

void replace_texture(u8* buf, u32 size, u32 idx) {
char path[256] = {0};
snprintf(path, sizeof(path), "textures/%d.dds", idx);
if (file_exists(path)) {
u32 tex_size = filesize(path);
void replace_texture(void* ctx, u8* buf, u32 size, u32 idx) {
char* path = (char*) ctx;
u32 dot_idx;
for (u32 i = strlen(path); i > 0; i--) {
if (path[i] == '.') {
dot_idx = i;
}
}
path[dot_idx] = 0x00;
char filename[256] = {0};
sprintf(filename, "textures/%s_%d.dds", path, idx);
path[dot_idx] = '.';

// Make the directory if it doesn't exist.
if (file_exists(filename)) {
u32 tex_size = filesize(filename);

// Load the mod texture data
u8* mod_dds = file_load(path);
u8* mod_dds = file_load(filename);
if (mod_dds == NULL) {
LOG_MSG(error, "Failed to load %d bytes from %s\n", tex_size, path);
LOG_MSG(error, "Failed to load %d bytes from %s\n", tex_size, filename);
}
else {
// Raw buffer without DDS header
Expand All @@ -41,7 +52,7 @@ void replace_texture(u8* buf, u32 size, u32 idx) {
}
}
else {
LOG_MSG(info, "%s not found, skipping\n", path);
LOG_MSG(info, "%s not found, skipping\n", filename);
}
}

4 changes: 2 additions & 2 deletions alr/src/replace.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// Stub to fill out the interface
// TODO: Allow NULL interface functions and replace them with a stub in the alr
// functions at runtime. Will make the declarations much simpler and clearer.
static void replace_chunk(chunk_generic chunk, u8* chunk_buf, u32 idx) {
static void replace_chunk(void* ctx, chunk_generic chunk, u8* chunk_buf, u32 idx) {
return;
}

void replace_texture(u8* buf, u32 size, u32 idx);
void replace_texture(void* ctx, u8* buf, u32 size, u32 idx);

// Interface to call parse_alr() with to trigger texture replacement
const alr_interface replace_interface = {
Expand Down
4 changes: 2 additions & 2 deletions alr/src/split.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "logging.h"
#include "alr.h"

void split_generic_chunk(chunk_generic chunk, u8* chunk_buf, u32 idx) {
void split_generic_chunk(void* ctx, chunk_generic chunk, u8* chunk_buf, u32 idx) {
if (!dir_exists("resources")) {
system("mkdir resources");
}
Expand All @@ -25,7 +25,7 @@ void split_generic_chunk(chunk_generic chunk, u8* chunk_buf, u32 idx) {
fclose(dump);
}

void split_resource(u8* buf, u32 size, u32 idx) {
void split_resource(void* ctx, u8* buf, u32 size, u32 idx) {
if (!dir_exists("resources")) {
system("mkdir resources");
}
Expand Down
4 changes: 2 additions & 2 deletions alr/src/split.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

// Write chunks to separate files based on their index in the header's offset
// array.
void split_generic_chunk(chunk_generic chunk, u8* chunk_buf, u32 idx);
void split_generic_chunk(void* ctx, chunk_generic chunk, u8* chunk_buf, u32 idx);

// Write each texture buffer to its own file. Useful for pasting into image
// preview tools, and hand-crafting your own image headers.
void split_resource(u8* buf, u32 size, u32 idx);
void split_resource(void* ctx, u8* buf, u32 size, u32 idx);

// Interface to call parse_alr() with to trigger splitting behaviour.
alr_interface split_interface = {
Expand Down

0 comments on commit 3aca2c2

Please sign in to comment.