pso_gc_tools/gen_qst_header.c

118 lines
3.6 KiB
C
Raw Normal View History

2021-03-20 11:32:30 -04:00
#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
#include <string.h>
#include <sylverant/prs.h>
2021-03-20 12:38:52 -04:00
#include "utils.h"
#include "quests.h"
2021-03-20 14:04:57 -04:00
#include "textconv.h"
2021-03-20 11:32:30 -04:00
int main(int argc, char *argv[]) {
2021-03-24 16:30:32 -04:00
int returncode, validation_result;
uint8_t *bin_data = NULL;
uint8_t *dat_data = NULL;
char *bin_hdr_file = NULL;
char *dat_hdr_file = NULL;
2021-03-20 11:32:30 -04:00
if (argc != 3) {
printf("Usage: gen_qst_header quest.bin quest.dat\n");
return 1;
}
const char *bin_file = argv[1];
const char *dat_file = argv[2];
const char *bin_base_filename = path_to_filename(bin_file);
2021-03-24 16:30:32 -04:00
if (strlen(bin_base_filename) > QUEST_FILENAME_MAX_LENGTH) {
2021-03-20 11:32:30 -04:00
printf("Bin filename is too long to fit in a QST header. Maximum length is 16 including file extension.\n");
2021-03-24 16:30:32 -04:00
goto error;
2021-03-20 11:32:30 -04:00
}
const char *dat_base_filename = path_to_filename(dat_file);
2021-03-24 16:30:32 -04:00
if (strlen(dat_base_filename) > QUEST_FILENAME_MAX_LENGTH) {
2021-03-20 11:32:30 -04:00
printf("Dat filename is too long to fit in a QST header. Maximum length is 16 including file extension.\n");
2021-03-24 16:30:32 -04:00
goto error;
2021-03-20 11:32:30 -04:00
}
size_t bin_compressed_size, dat_compressed_size;
2021-03-24 16:30:32 -04:00
returncode = get_filesize(bin_file, &bin_compressed_size);
if (returncode) {
printf("Error code %d (%s) getting size of bin file: %s\n", returncode, get_error_message(returncode), bin_file);
goto error;
2021-03-20 11:32:30 -04:00
}
2021-03-24 16:30:32 -04:00
returncode = get_filesize(dat_file, &dat_compressed_size);
if (returncode) {
printf("Error code %d (%s) getting size of dat file: %s\n", returncode, get_error_message(returncode), dat_file);
goto error;
2021-03-20 11:32:30 -04:00
}
2021-03-24 16:30:32 -04:00
2021-03-20 11:32:30 -04:00
size_t bin_decompressed_size = prs_decompress_file(bin_file, &bin_data);
if (bin_decompressed_size < 0) {
printf("Error opening and decompressing bin file: %s\n", bin_file);
2021-03-24 16:30:32 -04:00
goto error;
2021-03-20 11:32:30 -04:00
}
size_t dat_decompressed_size = prs_decompress_file(dat_file, &dat_data);
if (dat_decompressed_size < 0) {
printf("Error opening and decompressing dat file: %s\n", dat_file);
2021-03-24 16:30:32 -04:00
goto error;
2021-03-20 11:32:30 -04:00
}
QUEST_BIN_HEADER *bin_header = (QUEST_BIN_HEADER*)bin_data;
2021-03-24 16:30:32 -04:00
validation_result = validate_quest_bin(bin_header, bin_decompressed_size, true);
if (validation_result) {
printf("Aborting due to invalid quest .bin data.\n");
goto error;
}
2021-03-20 11:32:30 -04:00
2021-03-24 16:30:32 -04:00
//sjis_to_utf8(bin_header->name, sizeof(bin_header->name));
//sjis_to_utf8(bin_header->short_description, sizeof(bin_header->short_description));
//sjis_to_utf8(bin_header->long_description, sizeof(bin_header->long_description));
2021-03-20 11:32:30 -04:00
2021-03-24 16:30:32 -04:00
printf("Quest: id=%d (%d), episode=%d, download=%d, unknown=0x%02x, name=\"%s\", compressed_bin_size=%ld, compressed_dat_size=%ld\n",
bin_header->quest_number_byte,
bin_header->quest_number_word,
bin_header->episode+1,
bin_header->download,
bin_header->unknown,
bin_header->name,
bin_compressed_size,
dat_compressed_size);
2021-03-20 11:32:30 -04:00
QST_HEADER qst_bin_header, qst_dat_header;
generate_qst_header(bin_base_filename, bin_compressed_size, bin_header, &qst_bin_header);
generate_qst_header(dat_base_filename, dat_compressed_size, bin_header, &qst_dat_header);
2021-03-24 16:30:32 -04:00
bin_hdr_file = append_string(bin_file, ".hdr");
dat_hdr_file = append_string(dat_file, ".hdr");
2021-03-24 16:30:32 -04:00
returncode = write_file(bin_hdr_file, &qst_bin_header, sizeof(QST_HEADER));
if (returncode) {
printf("Error code %d (%s) writing out bin header file: %s\n", returncode, get_error_message(returncode), bin_hdr_file);
goto error;
2021-03-20 11:32:30 -04:00
}
2021-03-24 16:30:32 -04:00
returncode = write_file(dat_hdr_file, &qst_dat_header, sizeof(QST_HEADER));
if (returncode) {
printf("Error code %d (%s) writing out dat header file: %s\n", returncode, get_error_message(returncode), dat_hdr_file);
goto error;
2021-03-20 11:32:30 -04:00
}
2021-03-24 16:30:32 -04:00
returncode = 0;
goto quit;
error:
returncode = 1;
quit:
free(bin_hdr_file);
free(dat_hdr_file);
2021-03-20 11:32:30 -04:00
free(bin_data);
free(dat_data);
2021-03-24 16:30:32 -04:00
return returncode;
2021-03-20 11:32:30 -04:00
}