From 92b999255236d3cd577af018222075b3a1c913c5 Mon Sep 17 00:00:00 2001 From: bnnm Date: Thu, 29 Mar 2018 22:34:21 +0200 Subject: [PATCH] Rename open_stream_ext/name to open_streamfile_by_ext/filename (cleanup) --- src/meta/atx.c | 2 +- src/meta/dec.c | 8 +++--- src/meta/ea_eaac.c | 2 +- src/meta/gsp_gsb.c | 2 +- src/meta/ps2_adm.c | 2 +- src/meta/ps2_rxws.c | 2 +- src/meta/sab.c | 2 +- src/meta/sgxd.c | 2 +- src/meta/sxd.c | 2 +- src/meta/ubi_sb.c | 6 ++--- src/meta/xwb.c | 2 +- src/streamfile.c | 62 ++++++++++++++++++++++----------------------- src/streamfile.h | 39 ++++++++++++++++------------ 13 files changed, 70 insertions(+), 63 deletions(-) diff --git a/src/meta/atx.c b/src/meta/atx.c index c969f361..bace1a29 100644 --- a/src/meta/atx.c +++ b/src/meta/atx.c @@ -59,7 +59,7 @@ static STREAMFILE* setup_atx_streamfile(STREAMFILE *streamFile) { size_t subfile_size; filename[filename_len - 5] = ('0'+i+1); /* ghetto digit conversion */ - new_streamFile = open_stream_name(streamFile, filename); + new_streamFile = open_streamfile_by_filename(streamFile, filename); if (!new_streamFile) goto fail; segment_streamFiles[i] = new_streamFile; diff --git a/src/meta/dec.c b/src/meta/dec.c index b8d959ae..43a3fe18 100644 --- a/src/meta/dec.c +++ b/src/meta/dec.c @@ -103,16 +103,16 @@ static int get_falcom_looping(STREAMFILE *streamFile, int *out_loop_start, int * /* try one of the many loop files */ - if ((streamText = open_stream_name(streamFile,"bgm.tbl")) != NULL) { + if ((streamText = open_streamfile_by_filename(streamFile,"bgm.tbl")) != NULL) { type = XANADU_NEXT; } - else if ((streamText = open_stream_name(streamFile,"bgm.scr")) != NULL) { + else if ((streamText = open_streamfile_by_filename(streamFile,"bgm.scr")) != NULL) { type = ZWEI; } - else if ((streamText = open_stream_name(streamFile,"loop.txt")) != NULL) { /* actual name in Shift JIS, 0x838B815B8376 */ + else if ((streamText = open_streamfile_by_filename(streamFile,"loop.txt")) != NULL) { /* actual name in Shift JIS, 0x838B815B8376 */ type = DINOSAUR_RESURRECTION; } - else if ((streamText = open_stream_name(streamFile,"map.itm")) != NULL) { + else if ((streamText = open_streamfile_by_filename(streamFile,"map.itm")) != NULL) { type = GURUMIN; } else { diff --git a/src/meta/ea_eaac.c b/src/meta/ea_eaac.c index d693e5b1..20fd9a0d 100644 --- a/src/meta/ea_eaac.c +++ b/src/meta/ea_eaac.c @@ -29,7 +29,7 @@ VGMSTREAM * init_vgmstream_ea_snr_sns(STREAMFILE * streamFile) { if (!vgmstream) goto fail; } else { - streamData = open_stream_ext(streamFile,"sns"); + streamData = open_streamfile_by_ext(streamFile,"sns"); if (!streamData) goto fail; vgmstream = init_vgmstream_eaaudiocore_header(streamFile, streamData, 0x00, 0x00, meta_EA_SNR_SNS); diff --git a/src/meta/gsp_gsb.c b/src/meta/gsp_gsb.c index b3cc95f7..d51ec240 100644 --- a/src/meta/gsp_gsb.c +++ b/src/meta/gsp_gsb.c @@ -16,7 +16,7 @@ VGMSTREAM * init_vgmstream_gsp_gsb(STREAMFILE *streamFile) { if (!check_extensions(streamFile,"gsb")) goto fail; - streamFileGSP = open_stream_ext(streamFile, "gsp"); + streamFileGSP = open_streamfile_by_ext(streamFile, "gsp"); if (!streamFileGSP) goto fail; /* check header */ diff --git a/src/meta/ps2_adm.c b/src/meta/ps2_adm.c index cd23625f..3e2cad5f 100644 --- a/src/meta/ps2_adm.c +++ b/src/meta/ps2_adm.c @@ -74,7 +74,7 @@ static int get_adm_loop_info(STREAMFILE *streamFile, off_t *loop_start_offset) { int i, name_index = -1, loop_flag; off_t offset; - streamExe = open_stream_name(streamFile, "SLPM_655.55"); + streamExe = open_streamfile_by_filename(streamFile, "SLPM_655.55"); if (!streamExe) goto fail; get_streamfile_filename(streamFile, file_name, PATH_LIMIT); diff --git a/src/meta/ps2_rxws.c b/src/meta/ps2_rxws.c index d32b69e0..2cffb994 100644 --- a/src/meta/ps2_rxws.c +++ b/src/meta/ps2_rxws.c @@ -24,7 +24,7 @@ VGMSTREAM * init_vgmstream_ps2_rxws(STREAMFILE *streamFile) { (read_32bitBE(0x00,streamFile) == 0x444E4257)) /* "DNBW" (BE) */ goto fail; - streamHeader = open_stream_ext(streamFile, "xwh"); + streamHeader = open_streamfile_by_ext(streamFile, "xwh"); if (!streamHeader) goto fail; } else { streamHeader = streamFile; diff --git a/src/meta/sab.c b/src/meta/sab.c index 6ce952b1..3fce2b60 100644 --- a/src/meta/sab.c +++ b/src/meta/sab.c @@ -109,7 +109,7 @@ static void get_stream_name(char * stream_name, STREAMFILE *streamFile, int targ size_t name_size = 0; off_t name_offset = 0x10; - streamInfo = open_stream_ext(streamFile, "sob"); + streamInfo = open_streamfile_by_ext(streamFile, "sob"); if (!streamInfo) goto end; if (read_32bitBE(0x00,streamInfo) != 0x43544632) /* "CTF2" */ goto end; diff --git a/src/meta/sgxd.c b/src/meta/sgxd.c index 33c04628..e3744463 100644 --- a/src/meta/sgxd.c +++ b/src/meta/sgxd.c @@ -24,7 +24,7 @@ VGMSTREAM * init_vgmstream_sgxd(STREAMFILE *streamFile) { /* SGB+SGH: use SGH as header; otherwise use the current file as header */ if (is_sgb) { - streamHeader = open_stream_ext(streamFile, "sgh"); + streamHeader = open_streamfile_by_ext(streamFile, "sgh"); if (!streamHeader) goto fail; } else { streamHeader = streamFile; diff --git a/src/meta/sxd.c b/src/meta/sxd.c index 224dc80a..8a8c85ac 100644 --- a/src/meta/sxd.c +++ b/src/meta/sxd.c @@ -25,7 +25,7 @@ VGMSTREAM * init_vgmstream_sxd(STREAMFILE *streamFile) { if (is_dual) { if (read_32bitBE(0x00,streamFile) != 0x53584453) /* "SXDS" */ goto fail; - streamHeader = open_stream_ext(streamFile, "sxd1"); + streamHeader = open_streamfile_by_ext(streamFile, "sxd1"); if (!streamHeader) goto fail; } else { streamHeader = streamFile; diff --git a/src/meta/ubi_sb.c b/src/meta/ubi_sb.c index ad0d9abb..5b9c3339 100644 --- a/src/meta/ubi_sb.c +++ b/src/meta/ubi_sb.c @@ -90,7 +90,7 @@ VGMSTREAM * init_vgmstream_ubi_sb(STREAMFILE *streamFile) { if (sb.autodetect_external) { /* works most of the time but could give false positives */ VGM_LOG("UBI SB: autodetecting external stream '%s'\n", sb.stream_name); - streamData = open_stream_name(streamFile,sb.stream_name); + streamData = open_streamfile_by_filename(streamFile,sb.stream_name); if (!streamData) { streamData = streamFile; /* assume internal */ if (sb.stream_size > get_streamfile_size(streamData)) { @@ -102,7 +102,7 @@ VGMSTREAM * init_vgmstream_ubi_sb(STREAMFILE *streamFile) { } } else if (sb.is_external) { - streamData = open_stream_name(streamFile,sb.stream_name); + streamData = open_streamfile_by_filename(streamFile,sb.stream_name); if (!streamData) { VGM_LOG("UBI SB: external stream '%s' not found\n", sb.stream_name); goto fail; @@ -674,7 +674,7 @@ static int config_sb_header_version(ubi_sb_header * sb, STREAMFILE *streamFile) /* two games with same id; use project file as identifier */ if (sb->version == 0x0012000C && is_sb4) { - STREAMFILE * streamTest = open_stream_name(streamFile, "BIAAUDIO.SP4"); + STREAMFILE * streamTest = open_streamfile_by_filename(streamFile, "BIAAUDIO.SP4"); if (streamTest) { is_biadd_psp = 1; close_streamfile(streamTest); diff --git a/src/meta/xwb.c b/src/meta/xwb.c index 550070c2..8c104f46 100644 --- a/src/meta/xwb.c +++ b/src/meta/xwb.c @@ -601,7 +601,7 @@ static int get_xsb_name(char * buf, size_t maxsize, int target_subsong, xwb_head xsb_header xsb = {0}; - streamFile = open_stream_ext(streamXwb, "xsb"); + streamFile = open_streamfile_by_ext(streamXwb, "xsb"); if (!streamFile) goto fail; /* check header */ diff --git a/src/streamfile.c b/src/streamfile.c index 0b6a87b6..a909a2e3 100644 --- a/src/streamfile.c +++ b/src/streamfile.c @@ -684,6 +684,37 @@ fail: /* **************************************************** */ +STREAMFILE * open_streamfile_by_ext(STREAMFILE *streamFile, const char * ext) { + char filename_ext[PATH_LIMIT]; + + streamFile->get_name(streamFile,filename_ext,sizeof(filename_ext)); + strcpy(filename_ext + strlen(filename_ext) - strlen(filename_extension(filename_ext)), ext); + + return streamFile->open(streamFile,filename_ext,STREAMFILE_DEFAULT_BUFFER_SIZE); +} + +STREAMFILE * open_streamfile_by_filename(STREAMFILE *streamFile, const char * name) { + char foldername[PATH_LIMIT]; + char filename[PATH_LIMIT]; + const char *path; + + streamFile->get_name(streamFile,foldername,sizeof(foldername)); + + path = strrchr(foldername,DIR_SEPARATOR); + if (path!=NULL) path = path+1; + + if (path) { + strcpy(filename, foldername); + filename[path-foldername] = '\0'; + strcat(filename, name); + } else { + strcpy(filename, name); + } + + return streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE); +} + + /* Read a line into dst. The source files are lines separated by CRLF (Windows) / LF (Unux) / CR (Mac). * The line will be null-terminated and CR/LF removed if found. * @@ -762,37 +793,6 @@ fail: return 0; } -/* Opens an stream using the base streamFile name plus a new extension (ex. for headers in a separate file) */ -STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext) { - char filename_ext[PATH_LIMIT]; - - streamFile->get_name(streamFile,filename_ext,sizeof(filename_ext)); - strcpy(filename_ext + strlen(filename_ext) - strlen(filename_extension(filename_ext)), ext); - - return streamFile->open(streamFile,filename_ext,STREAMFILE_DEFAULT_BUFFER_SIZE); -} - -/* Opens an stream using the passed name, in the same folder */ -STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * name) { - char foldername[PATH_LIMIT]; - char filename[PATH_LIMIT]; - const char *path; - - streamFile->get_name(streamFile,foldername,sizeof(foldername)); - - path = strrchr(foldername,DIR_SEPARATOR); - if (path!=NULL) path = path+1; - - if (path) { - strcpy(filename, foldername); - filename[path-foldername] = '\0'; - strcat(filename, name); - } else { - strcpy(filename, name); - } - - return streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE); -} /* Opens a file containing decryption keys and copies to buffer. * Tries combinations of keynames based on the original filename. diff --git a/src/streamfile.h b/src/streamfile.h index 53b2b8f6..03746a43 100644 --- a/src/streamfile.h +++ b/src/streamfile.h @@ -65,41 +65,51 @@ typedef struct _STREAMFILE { } STREAMFILE; -/* create a STREAMFILE from path */ -STREAMFILE * open_stdio_streamfile(const char * filename); +/* Opens a standard STREAMFILE, opening from path. + * Uses stdio (FILE) for operations, thus plugins may not want to use it. */ +STREAMFILE *open_stdio_streamfile(const char * filename); -/* create a STREAMFILE from pre-opened file path */ -STREAMFILE * open_stdio_streamfile_by_file(FILE * file, const char * filename); +/* Opens a standard STREAMFILE from a pre-opened FILE. */ +STREAMFILE *open_stdio_streamfile_by_file(FILE * file, const char * filename); -/* A STREAMFILE that doesn't close the underlying stream. +/* Opens a STREAMFILE that doesn't close the underlying streamfile. * Calls to open won't wrap the new SF (assumes it needs to be closed). * Can be used in metas to test custom IO without closing the external SF. */ STREAMFILE *open_wrap_streamfile(STREAMFILE *streamfile); -/* A STREAMFILE that clamps IO to a section of a larger stream. - * Can be used with subfiles inside a bigger file, so it looks standard to a meta. */ +/* Opens a STREAMFILE that clamps reads to a section of a larger streamfile. + * Can be used with subfiles inside a bigger file (to fool metas, or to simplify custom IO). */ STREAMFILE *open_clamp_streamfile(STREAMFILE *streamfile, off_t start, size_t size); -/* A STREAMFILE with custom IO, that clamps IO to a section of a larger stream. - * Can be used with subfiles inside a bigger file, so it looks standard to a meta. */ +/* Opens a STREAMFILE that uses custom IO for streamfile reads. + * Can be used to modify data on the fly (ex. decryption), or even transform it from a format to another. */ STREAMFILE *open_io_streamfile(STREAMFILE *streamfile, void* data, size_t data_size, void* read_callback);//void* size_callback, void* seek_callback); -/* A STREAMFILE that reports a fake name, but still re-opens itself properly. +/* Opens a STREAMFILE that reports a fake name, but still re-opens itself properly. * Can be used to trick a meta's extension check (to call from another, with a modified SF). * When fakename isn't supplied it's read from the streamfile, and the extension swapped with fakeext. * If the fakename is an existing file, open won't work on it as it'll reopen the fake-named streamfile. */ STREAMFILE *open_fakename_streamfile(STREAMFILE *streamfile, const char * fakename, const char * fakeext); -/* A streamfile formed from multiple streamfiles, their data joined during reads. +//todo probably could simply use custom IO +/* Opens streamfile formed from multiple streamfiles, their data joined during reads. * Can be used when data is segmented in multiple separate files. * The first streamfile is used to get names, stream index and so on. */ STREAMFILE *open_multifile_streamfile(STREAMFILE **streamfiles, size_t streamfiles_size); +/* Opens a STREAMFILE from a base pathname + new extension + * Can be used to get companion headers. */ +STREAMFILE * open_streamfile_by_ext(STREAMFILE *streamFile, const char * ext); + +/* Opens a STREAMFILE from a base path + new filename + * Can be used to get companion files. */ +STREAMFILE * open_streamfile_by_filename(STREAMFILE *streamFile, const char * filename); + /* close a file, destroy the STREAMFILE object */ static inline void close_streamfile(STREAMFILE * streamfile) { - if (streamfile==NULL) return; - streamfile->close(streamfile); + if (streamfile!=NULL) + streamfile->close(streamfile); } /* read from a file, returns number of bytes read */ @@ -164,9 +174,6 @@ static inline int8_t read_8bit(off_t offset, STREAMFILE * streamfile) { size_t get_streamfile_text_line(int dst_length, char * dst, off_t offset, STREAMFILE * streamfile, int *line_done_ptr); -STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext); -STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * ext); - size_t read_string(char * buf, size_t bufsize, off_t offset, STREAMFILE *streamFile); size_t read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile);