Skip to content

Commit

Permalink
Better naming, find data addresses like the game does
Browse files Browse the repository at this point in the history
  • Loading branch information
mike309game committed Jan 7, 2022
1 parent aefbd4a commit 3578111
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ int main(int argc, char const *argv[])
/* Random size */
char path[0xFF];

for (int j = 0; j < pack_file.file_count; ++j) {
for (int j = 0; j < pack_file.item_count; ++j) {
/* Make file name */
sprintf(path, "%s/%s", argv_no_ext, pack_file.files[j].name);
sprintf(path, "%s/%s", argv_no_ext, pack_file.items[j].name);

FILE* fp = fopen(path, "wb");
if(!fp) {
perror("Error creating extracted file");
perror("Error creating extracted item");
exit(EXIT_FAILURE);
}
fwrite(pack_file.files[j].data, pack_file.files[j].data_length, 1, fp);
fwrite(pack_file.items[j].data, pack_file.items[j].data_length, 1, fp);
fclose(fp);
}
free_pack_file(&pack_file);
Expand Down
4 changes: 2 additions & 2 deletions main.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef _MAIN_H
#define _MAIN_H
static const char VERSION[] = "v0.1";
#define _MAIN_H 1
static const char VERSION[] = "v0.2";
#endif
48 changes: 24 additions & 24 deletions pack_file.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "pack_file.h"

pack_file_t load_pack_file(const char *filename) {
Expand All @@ -17,28 +16,29 @@ pack_file_t load_pack_file(const char *filename) {
PF_DBG("Processing %s\n", filename);

/* Read pack file's header */
fread(&pack_file, 0x14, 1, fp);
fread(&pack_file, PACK_FILE_HEADER_SIZE, 1, fp);

PF_DBG("Files: %d\n", pack_file.file_count);
PF_DBG("Items: %d\n", pack_file.item_count);

/* Allocate pf struct of files */
pack_file.files = (pf_file_t*) malloc(sizeof(pf_file_t) * pack_file.file_count);
/* Allocate memory for array of items */
pack_file.items = (pf_item_t*) malloc(sizeof(pf_item_t) * pack_file.item_count);

size_t data_address_current = pack_file.first_data_address;
/* Location of first item's data */
long data_address_current = pack_file.first_data_address_minus_0x14 + 0x14 & (~3);

/* For backtracking when loading file data */
size_t old_pos;
/* For backtracking when loading item data */
long old_pos;

for (int j = 0; j < pack_file.file_count; ++j) {
/* Read file's header */
fread(&pack_file.files[j], 0x18, 1, fp);
PF_DBG("\nFilename: %s\nData length: %li\nCombined size since last file: %li\n",
pack_file.files[j].name,
pack_file.files[j].data_length,
pack_file.files[j].combined_size_since_last_file
for (int j = 0; j < pack_file.item_count; ++j) {
/* Read item's header */
fread(&pack_file.items[j], PF_ITEM_HEADER_SIZE, 1, fp);
PF_DBG("\nFilename: %s\nData length: %li\nCombined size since last item: %li\n",
pack_file.items[j].name,
pack_file.items[j].data_length,
pack_file.items[j].combined_size_since_last_file
);

/* Load file's data */
/* Load item's data */

/* Set old position */
old_pos = ftell(fp);
Expand All @@ -47,13 +47,13 @@ pack_file_t load_pack_file(const char *filename) {
fseek(fp, data_address_current, SEEK_SET);

/* Allocate data buffer */
pack_file.files[j].data = (char*) malloc(pack_file.files[j].data_length);
pack_file.items[j].data = (char*) malloc(pack_file.items[j].data_length);

/* Read content into buffer */
fread(pack_file.files[j].data, pack_file.files[j].data_length, 1, fp);
fread(pack_file.items[j].data, pack_file.items[j].data_length, 1, fp);

/* Advance data address */
data_address_current += pack_file.files[j].data_length;
/* Advance data address and pad on 4 because the game does that */
data_address_current = data_address_current + (pack_file.items[j].data_length & (~3));

/* Go back to old position */
fseek(fp, old_pos, SEEK_SET);
Expand All @@ -63,13 +63,13 @@ pack_file_t load_pack_file(const char *filename) {

PF_DBG("Loaded pack file %s\n", filename);

/* Return the pack file struct pointer */
/* Return the pack file struct */
return pack_file;
}

void free_pack_file(pack_file_t* pack_file) {
for (int i = 0; i < pack_file->file_count; ++i) {
free(pack_file->files[i].data);
for (int i = 0; i < pack_file->item_count; ++i) {
free(pack_file->items[i].data);
}
free(pack_file->files);
free(pack_file->items);
}
11 changes: 7 additions & 4 deletions pack_file.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _PACK_FILE_H
#define _PACK_FILE_H
#define _PACK_FILE_H 1
#include <errno.h>
#include <string.h>

Expand All @@ -11,6 +11,9 @@
#endif
#define PF_ERR(string, ...) fprintf(stderr, string, __VA_ARGS__, strerror( errno ));

static const char PACK_FILE_HEADER_SIZE = 0x14;
static const char PF_ITEM_HEADER_SIZE = 0x18;

enum pack_file_kind {
PF_TEX = 0x31,
PF_PMD = 0x32,
Expand All @@ -21,12 +24,12 @@ typedef struct pf_file_t {
long data_length;
long combined_size_since_last_file;
char* data;
} pf_file_t;
} pf_item_t;

typedef struct pointer_file_t {
/* Look at file kind enum */
short type;
short file_count;
short item_count;
/* Despite the file having the address to the first data block,
the game chose to use this and adds 0x14 to it and ANDs it by (~3)*/
long first_data_address_minus_0x14;
Expand All @@ -36,7 +39,7 @@ typedef struct pointer_file_t {
long filesize_without_header;
/* Not used by game, for whatever reason */
long first_data_address;
pf_file_t* files;
pf_item_t* items;
} pack_file_t;

pack_file_t load_pack_file(const char filename[]);
Expand Down

0 comments on commit 3578111

Please sign in to comment.