Skip to content

Commit

Permalink
Cleanups & comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Torphedo committed Dec 24, 2023
1 parent 9aa2d74 commit d6f7d66
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
2 changes: 0 additions & 2 deletions alr/src/alr.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>

#include "logging.h"

#include "int_shorthands.h"
#include "filesystem.h"
#include "alr.h"
#include "images.h"

bool alr_parse(char* alr_filename, flags options, alr_interface handlers) {
FILE* alr = fopen(alr_filename, "rb");
Expand Down
1 change: 0 additions & 1 deletion alr/src/alr.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <stdint.h>
#include "arguments.h"

#include "int_shorthands.h"
Expand Down
9 changes: 4 additions & 5 deletions alr/src/dump.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>

#include "logging.h"

Expand Down Expand Up @@ -45,11 +42,13 @@ void chunk_texture(chunk_generic header, u8* chunk_buf, u32 idx) {

texture_meta[i].width = surfaces[i].width;
texture_meta[i].height = surfaces[i].height;
//

// Mip count here includes base texture.
texture_meta[i].mipmap_count = surfaces[i].mipmap_count - 1;

// TODO: Make this also go into the textures folder. This would require
// another allocation.
texture_meta[i].filename = (char*)&names[i].name;
// strncpy(texture_meta[i].filename, (char*)&names[i].name, 0x10);

u32 total_pixel_count = full_pixel_count(surfaces[i].width, surfaces[i].height, surfaces[i].mipmap_count);

Expand Down
17 changes: 14 additions & 3 deletions alr/src/dump.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
#pragma once
#include "alr.h"
#include "int_shorthands.h"

// Stub function to skip chunks we don't care about for texture dumping.
static void chunk_any(chunk_generic chunk, u8* chunk_buf, u32 idx) {
return;
}

// Sanity checks for 0xD chunks that ought to be empty.
void chunk_0xD(chunk_generic chunk, u8* chunk_buf, u32 idx);

// 0x10 (Texture metadata) chunk handling. Saves metadata to dump.c's internal
// state.
void chunk_texture(chunk_generic header, u8* chunk_buf, u32 idx);
void process_texture(u8* buf, u32 size, u32 idx);

// 0x15 (Resource/texture buffer layout) chunk handling. Saves information to
// dump.c's internal state.
void res_layout(chunk_generic chunk, u8* chunk_buf, u32 idx);
void stream_dump(chunk_generic chunk, u8* chunk_buf);

// This is run for each resource/texture buffer and dumps it to a DDS file as
// best it can. Metadata from 0x10 chunks will be used if available, and
// estimates from 0x15 chunks will be used as a fallback.
void process_texture(u8* buf, u32 size, u32 idx);

// Interface to call parse_alr() with to trigger texture dumping.
const alr_interface dump_interface = {
.chunk_0x1 = chunk_any,
.chunk_0x2 = chunk_any,
Expand Down
10 changes: 3 additions & 7 deletions alr/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

#include "logging.h"

#include "split.h"
#include "alr.h"
#include "dump.h"
#include "split.h"
#include "arguments.h"

// Cross-platform pause
static inline void pause() {
LOG_MSG(info, "Press Enter to exit...");
unsigned char dummy = getchar();
}

int main(int argc, char* argv[]) {
// Parse command-line arguments.
flags options = parse_arguments(argc, argv);
Expand All @@ -25,6 +19,8 @@ int main(int argc, char* argv[]) {
if (options.silent) {
disable_logging();
}
// Default to dump behaviour if nothing is specified. No arguments probably
// means someone drag-and-dropped, which means they probably want textures.
alr_interface interface = dump_interface;

if (options.split) {
Expand Down
6 changes: 1 addition & 5 deletions alr/src/split.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#include "int_shorthands.h"
#include "filesystem.h"
#include "logging.h"
#include "alr.h"
Expand All @@ -15,7 +11,7 @@ void split_generic_chunk(chunk_generic chunk, u8* chunk_buf, u32 idx) {
}

char filename[256] = {0};
if (snprintf(filename, 256, "resources/%u.bin", chunk.id) < 0) {
if (snprintf(filename, 256, "resources/%u.bin", idx) < 0) {
LOG_MSG(error, "Couldn't print output name for resource %d\n", idx);
return;
}
Expand Down
10 changes: 6 additions & 4 deletions alr/src/split.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once
#include <stdbool.h>
#include "alr.h"

// Writes each distinct section of an ALR to separate files on disk.
// bool split_alr(char* alr_filename);

// 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);

// 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);

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

0 comments on commit d6f7d66

Please sign in to comment.