From ab39297a96b529e85d9b3daea58b617bac0a9856 Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Mon, 16 Nov 2026 14:14:06 +0000 Subject: [PATCH 1/3] GME: Count GBS playlist tracks from zero Playlists for GBS rips, as NEZplug defined them and Zophar's packs ship them, number tracks from 0, the same as KSS. libgme took them as 1-based, so every entry played the song before the one it names, and the first entry pointed before the first song and played nothing. --- Frameworks/GME/gme/Gbs_Emu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Frameworks/GME/gme/Gbs_Emu.cpp b/Frameworks/GME/gme/Gbs_Emu.cpp index 3e2a7eab6..29d5931e3 100644 --- a/Frameworks/GME/gme/Gbs_Emu.cpp +++ b/Frameworks/GME/gme/Gbs_Emu.cpp @@ -100,7 +100,7 @@ struct Gbs_File : Gme_Info_ static Music_Emu* new_gbs_emu () { return BLARGG_NEW Gbs_Emu ; } static Music_Emu* new_gbs_file() { return BLARGG_NEW Gbs_File; } -static gme_type_t_ const gme_gbs_type_ = { "Game Boy", 0, &new_gbs_emu, &new_gbs_file, "GBS", 1 }; +static gme_type_t_ const gme_gbs_type_ = { "Game Boy", 0, &new_gbs_emu, &new_gbs_file, "GBS", 0x03 }; extern gme_type_t const gme_gbs_type = &gme_gbs_type_; // Setup From 1d57958b96f7f3318dce601ac48b6b3be7f0f697 Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Mon, 16 Nov 2026 14:14:06 +0000 Subject: [PATCH 2/3] Plugins: Let sources list the files beside a file A file can come with others it needs, such as playlists beside it. Add an optional filesBesideURL: to CogSource, which FileSource answers by listing the folder, with the sandbox access it already takes care of. --- Audio/Plugin.h | 3 +++ Plugins/FileSource/FileSource.m | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Audio/Plugin.h b/Audio/Plugin.h index 0be883b1b..55888f4d4 100644 --- a/Audio/Plugin.h +++ b/Audio/Plugin.h @@ -23,6 +23,9 @@ // Promptly unblock any in-flight read without waiting for full teardown. // The normal -close call still follows on the reader's own thread. - (void)interrupt; +// The files in the folder that holds `url`, or in that folder of its +// archive, by name, as URLs this source opens. Needs no open: first. +- (NSDictionary *)filesBesideURL:(NSURL *)url; @end @protocol CogVersionCheck diff --git a/Plugins/FileSource/FileSource.m b/Plugins/FileSource/FileSource.m index ebfddd6a5..295561fd3 100644 --- a/Plugins/FileSource/FileSource.m +++ b/Plugins/FileSource/FileSource.m @@ -149,6 +149,25 @@ - (void)close { } } +- (NSDictionary *)filesBesideURL:(NSURL *)url { + url = CogNormalizeURL(url); + NSString *folder = [[url path] stringByDeletingLastPathComponent]; + + id sandboxBrokerClass = NSClassFromString(@"SandboxBroker"); + id sandboxBroker = [sandboxBrokerClass sharedSandboxBroker]; + + const void *handle = [sandboxBroker beginFolderAccess:url]; + NSArray *names = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil]; + [sandboxBroker endFolderAccess:handle]; + + NSMutableDictionary *files = [NSMutableDictionary dictionaryWithCapacity:[names count]]; + for(NSString *name in names) { + if(![name hasPrefix:@"."]) + files[name] = [NSURL fileURLWithPath:[folder stringByAppendingPathComponent:name]]; + } + return files; +} + - (NSURL *)url { return _url; } From 54a96c6212eccd1954d8f9fd06f5aa77569ca04d Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Mon, 16 Nov 2026 14:14:06 +0000 Subject: [PATCH 3/3] GME: Read the per-track playlists of NEZplug rips GBS, NSF and similar rips made for NEZplug often come with one playlist per track ("01 Title.m3u" holding "Game.gbs::GBS,12,Title,0:04,,1") rather than one named after the file. Only that one was read, so such a rip listed its raw subsongs, all with the default length. When there is no playlist named after the file, gather the lines that name it from the playlists beside it, in Finder order, so the tracks get their order, titles, lengths and fades. The container, decoder and metadata reader share the loading, so all three see the same tracks. --- Plugins/GME/GameContainer.m | 19 +------ Plugins/GME/GameDecoder.h | 4 ++ Plugins/GME/GameDecoder.m | 91 ++++++++++++++++++++++++++------ Plugins/GME/GameMetadataReader.m | 17 +----- 4 files changed, 80 insertions(+), 51 deletions(-) diff --git a/Plugins/GME/GameContainer.m b/Plugins/GME/GameContainer.m index 0c8c5c9f5..3f87348ba 100644 --- a/Plugins/GME/GameContainer.m +++ b/Plugins/GME/GameContainer.m @@ -65,24 +65,7 @@ + (NSArray *)urlsForContainerURL:(NSURL *)url trackInfo:(NSDictionary **)trackIn return @[url]; } - NSURL *m3uurl = [url URLByDeletingPathExtension]; - m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"]; - id m3usrc = [audioSourceClass audioSourceForURL:m3uurl]; - if([m3usrc open:m3uurl]) { - if([m3usrc seekable]) { - [m3usrc seek:0 whence:SEEK_END]; - size = [m3usrc tell]; - [m3usrc seek:0 whence:SEEK_SET]; - - data = malloc(size); - [m3usrc read:data amount:size]; - - error = gme_load_m3u_data(emu, data, size); - free(data); - - ALog(@"M3U loaded: %s", error ? error : "no error"); - } - } + [GameDecoder loadPlaylistForURL:url intoEmu:emu]; int track_count = gme_track_count(emu); diff --git a/Plugins/GME/GameDecoder.h b/Plugins/GME/GameDecoder.h index 4280da4ca..ed50680ac 100644 --- a/Plugins/GME/GameDecoder.h +++ b/Plugins/GME/GameDecoder.h @@ -35,5 +35,9 @@ extern gme_err_t readCallback(void* data, void* out, int count); // The properties of a track that plays `length` milliseconds at // `sampleRate`. + (NSDictionary *)propertiesWithSampleRate:(double)sampleRate length:(long)length; +// Loads the m3u playlist named after the file at `url`, or, failing that, +// the lines naming the file in the playlists beside it, as rips that give +// each track its own playlist have them. ++ (void)loadPlaylistForURL:(NSURL *)url intoEmu:(Music_Emu *)emu; @end diff --git a/Plugins/GME/GameDecoder.m b/Plugins/GME/GameDecoder.m index 77b132500..83749e38d 100644 --- a/Plugins/GME/GameDecoder.m +++ b/Plugins/GME/GameDecoder.m @@ -91,6 +91,79 @@ + (NSDictionary *)propertiesWithSampleRate:(double)sampleRate length:(long)lengt @"encoding": @"synthesized" }; } +// All that `url` holds, or nil if it does not open. +static NSData *dataForURL(NSURL *url) { + id audioSourceClass = NSClassFromString(@"AudioSource"); + id source = [audioSourceClass audioSourceForURL:url]; + if(![source open:url] || ![source seekable]) + return nil; + + [source seek:0 whence:SEEK_END]; + long size = [source tell]; + [source seek:0 whence:SEEK_SET]; + + NSMutableData *data = [NSMutableData dataWithLength:size]; + [source read:[data mutableBytes] amount:size]; + [source close]; + return data; +} + ++ (void)loadPlaylistForURL:(NSURL *)url intoEmu:(Music_Emu *)emu { + NSURL *m3uurl = [url URLByDeletingPathExtension]; + m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"]; + NSData *data = dataForURL(m3uurl); + + if(!data) { + // Rips made for NEZplug often give each track a playlist of its own, + // "01 Title.m3u" holding "Game.gbs::GBS,12,Title,0:04,,1", in place + // of one named after the file: gather the lines naming this file from + // the playlists beside it, in order. + id audioSourceClass = NSClassFromString(@"AudioSource"); + id source = [audioSourceClass audioSourceForURL:url]; + NSDictionary *files = nil; + if([source respondsToSelector:@selector(filesBesideURL:)]) + files = [source filesBesideURL:url]; + + // This file's name, which an archive's URL does not end with. + NSString *name = nil; + for(NSString *fileName in files) { + if([[files[fileName] path] isEqualToString:[url path]]) { + name = fileName; + break; + } + } + NSMutableData *playlist = [NSMutableData data]; + if(name) { + NSData *prefix = [[name stringByAppendingString:@"::"] dataUsingEncoding:NSUTF8StringEncoding]; + for(NSString *fileName in [[files allKeys] sortedArrayUsingSelector:@selector(localizedStandardCompare:)]) { + if(![[[fileName pathExtension] lowercaseString] isEqualToString:@"m3u"]) + continue; + NSData *contents = dataForURL(files[fileName]); + const char *line = [contents bytes]; + const char *end = line + [contents length]; + while(line < end) { + const char *next = line; + while(next < end && *next != '\r' && *next != '\n') + ++next; + if((NSUInteger)(next - line) > [prefix length] && strncasecmp(line, [prefix bytes], [prefix length]) == 0) { + [playlist appendBytes:line length:next - line]; + [playlist appendBytes:"\n" length:1]; + } + line = next + 1; + } + } + } + data = playlist; + } + + if([data length]) { + gme_err_t error = gme_load_m3u_data(emu, [data bytes], [data length]); + if(error) { + ALog(@"GME: Error loading playlist for %@: %s", url, error); + } + } +} + - (id)init { self = [super init]; if(self) { @@ -137,23 +210,7 @@ - (BOOL)open:(id)s { return NO; } - NSURL *m3uurl = [[source url] URLByDeletingPathExtension]; - m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"]; - id audioSourceClass = NSClassFromString(@"AudioSource"); - id m3usrc = [audioSourceClass audioSourceForURL:m3uurl]; - if([m3usrc open:m3uurl]) { - if([m3usrc seekable]) { - [m3usrc seek:0 whence:SEEK_END]; - long size = [m3usrc tell]; - [m3usrc seek:0 whence:SEEK_SET]; - - void *data = malloc(size); - [m3usrc read:data amount:size]; - - gme_load_m3u_data(emu, data, size); - free(data); - } - } + [GameDecoder loadPlaylistForURL:[source url] intoEmu:emu]; int track_num = [[[source url] fragment] intValue]; // What if theres no fragment? Assuming we get 0. diff --git a/Plugins/GME/GameMetadataReader.m b/Plugins/GME/GameMetadataReader.m index 8304ab429..857a5ad50 100644 --- a/Plugins/GME/GameMetadataReader.m +++ b/Plugins/GME/GameMetadataReader.m @@ -64,22 +64,7 @@ + (NSDictionary *)metadataForURL:(NSURL *)url { return nil; } - NSURL *m3uurl = [url URLByDeletingPathExtension]; - m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"]; - id m3usrc = [audioSourceClass audioSourceForURL:m3uurl]; - if([m3usrc open:m3uurl]) { - if([m3usrc seekable]) { - [m3usrc seek:0 whence:SEEK_END]; - long size = [m3usrc tell]; - [m3usrc seek:0 whence:SEEK_SET]; - - void *data = malloc(size); - [m3usrc read:data amount:size]; - - gme_load_m3u_data(emu, data, size); - free(data); - } - } + [GameDecoder loadPlaylistForURL:url intoEmu:emu]; int track_num; if([[url fragment] length] == 0)