From 6116a49d6b373b86f4ed42e1d18569f3ec7598df Mon Sep 17 00:00:00 2001 From: sorgelig Date: Sun, 8 Sep 2019 06:06:14 +0800 Subject: [PATCH] neogeo: zip support. --- file_io.cpp | 51 +++++++++++++++-------------- support/neogeo/loader.cpp | 68 +++++++++++++++++++++++++++++---------- support/neogeo/loader.h | 2 +- 3 files changed, 78 insertions(+), 43 deletions(-) diff --git a/file_io.cpp b/file_io.cpp index 06c27e5..090c79d 100644 --- a/file_io.cpp +++ b/file_io.cpp @@ -142,8 +142,8 @@ static bool isPathDirectory(char *path) return false; } -/* -static bool isPathRegularFile(char *path) + +static bool isPathRegularFile(const char *path) { make_fullpath(path); @@ -153,8 +153,8 @@ static bool isPathRegularFile(char *path) mz_zip_archive z{}; if (!mz_zip_reader_init_file(&z, zip_path, 0)) { - printf("isPathRegularFile(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path, - mz_zip_get_error_string(mz_zip_get_last_error(&z))); + //printf("isPathRegularFile(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path, + // mz_zip_get_error_string(mz_zip_get_last_error(&z))); return false; } @@ -167,9 +167,9 @@ static bool isPathRegularFile(char *path) const int file_index = mz_zip_reader_locate_file(&z, file_path, NULL, 0); if (file_index < 0) { - printf("isPathRegularFile(mz_zip_reader_locate_file) Zip:%s, file:%s, error: %s\n", - zip_path, file_path, - mz_zip_get_error_string(mz_zip_get_last_error(&z))); + //printf("isPathRegularFile(mz_zip_reader_locate_file) Zip:%s, file:%s, error: %s\n", + // zip_path, file_path, + // mz_zip_get_error_string(mz_zip_get_last_error(&z))); mz_zip_reader_end(&z); return false; } @@ -183,19 +183,12 @@ static bool isPathRegularFile(char *path) } else { - int stmode = get_stmode(full_path); - if (!stmode) - { - printf("isPathRegularFile(stat) path:%s, error:%s.\n", full_path, strerror(errno)); - return false; - } - - if (stmode & S_IFREG) return true; + if (get_stmode(full_path) & S_IFREG) return true; } return false; } -*/ + void FileClose(fileTYPE *file) { if (file->zip) @@ -471,7 +464,7 @@ int FileReadAdv(fileTYPE *file, void *pBuffer, int length) else { printf("FileReadAdv error(unknown file type).\n"); - return -1; + return 0; } file->offset += ret; @@ -598,7 +591,7 @@ int FileLoadConfig(const char *name, void *pBuffer, int size) int FileExists(const char *name) { - return !access(make_fullpath(name), F_OK); + return isPathRegularFile(name); } int FileCanWrite(const char *name) @@ -927,6 +920,7 @@ static bool IsInSameFolder(const char *folder, const char *path) int ScanDirectory(char* path, int mode, const char *extension, int options, const char *prefix) { static char file_name[1024]; + static char full_path[1024]; int has_trd = 0; const char *ext = extension; @@ -936,12 +930,6 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons ext += 3; } - const char* is_zipped = strcasestr(path, ".zip"); - if (is_zipped && strcasestr(is_zipped + 4, ".zip")) - { - printf("Nested zip-files are not supported: %s\n", path); - return 0; - } int extlen = strlen(extension); //printf("scan dir\n"); @@ -975,6 +963,13 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons sprintf(full_path, "%s/%s", getRootDir(), path); int path_len = strlen(full_path); + const char* is_zipped = strcasestr(full_path, ".zip"); + if (is_zipped && strcasestr(is_zipped + 4, ".zip")) + { + printf("Nested zip-files are not supported: %s\n", full_path); + return 0; + } + printf("Start to scan %sdir: %s\n", is_zipped ? "zipped " : "", full_path); printf("Position on item: %s\n", file_name); @@ -1062,6 +1057,11 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons if (options & SCANO_NEOGEO) { + if (de->d_type == DT_REG && !strcasecmp(de->d_name + strlen(de->d_name) - 4, ".zip")) + { + de->d_type = DT_DIR; + } + if (de->d_type != DT_DIR) continue; if (!strcmp(de->d_name, "..")) { @@ -1075,9 +1075,10 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons direntext_t dext = { *de, 0, "" }; memcpy(dext.altname, de->d_name, sizeof(dext.altname)); + if (!strcasecmp(dext.altname + strlen(dext.altname) - 4, ".zip")) dext.altname[strlen(dext.altname) - 4] = 0; full_path[path_len] = 0; - char *altname = neogeo_get_altname(full_path, de->d_name); + char *altname = neogeo_get_altname(full_path, &dext); if (altname) { dext.de.d_type = DT_REG; diff --git a/support/neogeo/loader.cpp b/support/neogeo/loader.cpp index 03bf91a..9525fec 100644 --- a/support/neogeo/loader.cpp +++ b/support/neogeo/loader.cpp @@ -506,6 +506,26 @@ static int xml_get_altname(XMLEvent evt, const XMLNode* node, SXML_CHAR* text, c return true; } +static void parse_xml(const char* filename, const SAX_Callbacks* sax, void* user) +{ + fileTYPE f = {}; + if(FileOpen(&f, filename) && f.size) + { + void *buf = malloc(f.size+1); // plus null; + if (buf) + { + int size = FileReadAdv(&f, buf, f.size); + if (size) + { + char *bufc = (char*)buf; + bufc[size] = 0; + XMLDoc_parse_buffer_SAX(bufc, filename, sax, user); + } + free(buf); + } + } +} + int neogeo_scan_xml() { static char full_path[1024]; @@ -516,19 +536,19 @@ int neogeo_scan_xml() memset(roms, 0, sizeof(roms)); rom_cnt = 0; sax.all_event = xml_scan; - XMLDoc_parse_file_SAX(full_path, &sax, 0); + parse_xml(full_path, &sax, 0); return rom_cnt; } -char *neogeo_get_altname(char *path, char *name) +char *neogeo_get_altname(char *path, direntext_t *de) { static char full_path[1024]; strcpy(full_path, path); strcat(full_path, "/"); - strcat(full_path, name); + strcat(full_path, de->de.d_name); strcat(full_path, "/romset.xml"); - if (!access(full_path, F_OK)) + if (FileExists(full_path)) { static char altname[256]; altname[0] = 0; @@ -537,11 +557,11 @@ char *neogeo_get_altname(char *path, char *name) SAX_Callbacks_init(&sax); sax.all_event = xml_get_altname; - XMLDoc_parse_file_SAX(full_path, &sax, &altname); + parse_xml(full_path, &sax, &altname); if (*altname) return altname; } - sprintf(full_path, ",%s,", name); + sprintf(full_path, ",%s,", de->altname); for (uint32_t i = 0; i < rom_cnt; i++) { if (roms[i].name[0] == ',') @@ -551,11 +571,11 @@ char *neogeo_get_altname(char *path, char *name) { if(p == roms[i].name) return roms[i].altname; - sprintf(full_path, "%s (%s)", roms[i].altname, name); + sprintf(full_path, "%s (%s)", roms[i].altname, de->altname); return full_path; } } - else if (!strcasecmp(name, roms[i].name)) + else if (!strcasecmp(de->altname, roms[i].name)) { return roms[i].altname; } @@ -577,6 +597,24 @@ static int has_name(const char *nameset, const char *name) return !strcasecmp(nameset, name); } +static const char* get_romset(const char *path) +{ + static char romset[32]; + if (!path) return 0; + + const char* p = strrchr(path, '/'); + if (!p) return 0; + + p++; + int len = strlen(p); + if (len > 4 && !strcasecmp(p + len - 4, ".zip")) len -= 4; + if (len > 31) len = 31; + memcpy(romset, p, len); + + romset[len] = 0; + return romset; +} + static int checked_ok; static int romsets = 0; static int xml_check_files(XMLEvent evt, const XMLNode* node, SXML_CHAR* text, const int n, SAX_Data* sd) @@ -585,10 +623,8 @@ static int xml_check_files(XMLEvent evt, const XMLNode* node, SXML_CHAR* text, c static char full_path[1024]; const char* path = (const char*)sd->user; - if (!path) return 0; - const char* romset = strrchr(path, '/'); + const char* romset = get_romset(path); if (!romset) return 0; - romset++; switch (evt) { @@ -712,10 +748,8 @@ static int xml_load_files(XMLEvent evt, const XMLNode* node, SXML_CHAR* text, co static int vrom_mirror = 1; const char* path = (const char*)sd->user; - if (!path) return 0; - const char* romset = strrchr(path, '/'); + const char* romset = get_romset(path); if (!romset) return 0; - romset++; switch (evt) { @@ -922,7 +956,7 @@ int neogeo_romset_tx(char* name) if (!(system_type & 2)) { sprintf(full_path, "%s/%s/romset.xml", getRootDir(), name); - if (access(full_path, F_OK)) sprintf(full_path, "%s/%s/romsets.xml", getRootDir(), HomeDir); + if (!FileExists(full_path)) sprintf(full_path, "%s/%s/romsets.xml", getRootDir(), HomeDir); printf("xml for %s: %s\n", name, full_path); @@ -932,11 +966,11 @@ int neogeo_romset_tx(char* name) checked_ok = false; romsets = 0; sax.all_event = xml_check_files; - XMLDoc_parse_file_SAX(full_path, &sax, name); + parse_xml(full_path, &sax, name); if (!checked_ok) return 0; sax.all_event = xml_load_files; - XMLDoc_parse_file_SAX(full_path, &sax, name); + parse_xml(full_path, &sax, name); } // Load system ROMs diff --git a/support/neogeo/loader.h b/support/neogeo/loader.h index 360967d..2bc794c 100644 --- a/support/neogeo/loader.h +++ b/support/neogeo/loader.h @@ -7,4 +7,4 @@ int neogeo_romset_tx(char* name); int neogeo_scan_xml(); -char *neogeo_get_altname(char *path, char *name); +char *neogeo_get_altname(char *path, direntext_t *de);