From 26e83a4c356bfb9dc6b8e136882ca9abebe51e32 Mon Sep 17 00:00:00 2001 From: OV2 Date: Sat, 3 Dec 2016 17:24:22 +0100 Subject: [PATCH] Look for msu1 files in .msu.zip --- memmap.cpp | 5 +++- msu1.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- msu1.h | 1 + 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/memmap.cpp b/memmap.cpp index 3d00bcbf..05386567 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -1295,7 +1295,10 @@ static bool8 is_GNEXT_Add_On (const uint8 *data, uint32 size) static bool8 MsuRomExists (void) { struct stat buf; - return (stat(S9xGetFilename(".msu", ROMFILENAME_DIR), &buf) == 0); + STREAM s = S9xMSU1OpenFile(".msu"); + bool8 exists = (s != NULL); + CLOSE_STREAM(s); + return exists; } int CMemory::ScoreHiROM (bool8 skip_header, int32 romoff) diff --git a/msu1.cpp b/msu1.cpp index 0c0d01c9..8923a648 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -207,6 +207,76 @@ uint32 partial_samples; // Sample buffer int16 *bufPos, *bufBegin, *bufEnd; +#ifdef UNZIP_SUPPORT +static int unzFindExtension(unzFile &file, const char *ext, bool restart = TRUE, bool print = TRUE) +{ + unz_file_info info; + int port, l = strlen(ext); + + if (restart) + port = unzGoToFirstFile(file); + else + port = unzGoToNextFile(file); + + while (port == UNZ_OK) + { + int len; + char name[132]; + + unzGetCurrentFileInfo(file, &info, name, 128, NULL, 0, NULL, 0); + len = strlen(name); + + if (len >= l + 1 && strcasecmp(name + len - l, ext) == 0 && unzOpenCurrentFile(file) == UNZ_OK) + { + if (print) + printf("Using msu file %s", name); + + return (port); + } + + port = unzGoToNextFile(file); + } + + return (port); +} +#endif + +STREAM S9xMSU1OpenFile(char *msu_ext) +{ + const char *filename = S9xGetFilename(msu_ext, ROMFILENAME_DIR); + STREAM file = OPEN_STREAM(filename, "rb"); + if (file) + printf("Using msu file %s.\n", filename); + +#ifdef UNZIP_SUPPORT + // look for msu file in .msu.zip if not found in rom dir + if (!file) + { + const char *zip_filename = S9xGetFilename(".msu.zip", ROMFILENAME_DIR); + if (zip_filename) + { + unzFile unzFile = unzOpen(zip_filename); + if (unzFile) + { + int port = unzFindExtension(unzFile, msu_ext); + if (port == UNZ_OK) + { + printf(" in %s.\n", zip_filename); + file = new unzStream(unzFile); + } + else + unzCloseCurrentFile(unzFile); + } + } + } +#endif + + if(!file) + printf("Unable to find msu file %s.\n", filename); + + return file; +} + bool AudioOpen() { MSU1.MSU1_STATUS |= AudioError; @@ -220,7 +290,7 @@ bool AudioOpen() char ext[_MAX_EXT]; snprintf(ext, _MAX_EXT, "-%d.pcm", MSU1.MSU1_CURRENT_TRACK); - audioStream = OPEN_STREAM(S9xGetFilename(ext, ROMFILENAME_DIR), "rb"); + audioStream = S9xMSU1OpenFile(ext); if (audioStream) { if (GETC_STREAM(audioStream) != 'M') @@ -252,7 +322,7 @@ bool DataOpen() dataStream = NULL; } - dataStream = OPEN_STREAM(S9xGetFilename(".msu", ROMFILENAME_DIR), "rb"); + dataStream = S9xMSU1OpenFile(".msu"); return dataStream != NULL; } diff --git a/msu1.h b/msu1.h index 6192a783..bd8c684a 100644 --- a/msu1.h +++ b/msu1.h @@ -226,6 +226,7 @@ enum SMSU1_CMD { extern struct SMSU1 MSU1; +STREAM S9xMSU1OpenFile(char *msu_ext); void S9xMSU1Init(void); void S9xMSU1Generate(int sample_count); uint8 S9xMSU1ReadPort(int port);