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/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 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; } 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)