Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Audio/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NSObject>
Expand Down
2 changes: 1 addition & 1 deletion Frameworks/GME/gme/Gbs_Emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions Plugins/FileSource/FileSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 1 addition & 18 deletions Plugins/GME/GameContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,7 @@ + (NSArray *)urlsForContainerURL:(NSURL *)url trackInfo:(NSDictionary **)trackIn
return @[url];
}

NSURL *m3uurl = [url URLByDeletingPathExtension];
m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"];
id<CogSource> 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);

Expand Down
4 changes: 4 additions & 0 deletions Plugins/GME/GameDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
91 changes: 74 additions & 17 deletions Plugins/GME/GameDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<CogSource> 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<CogSource> 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) {
Expand Down Expand Up @@ -137,23 +210,7 @@ - (BOOL)open:(id<CogSource>)s {
return NO;
}

NSURL *m3uurl = [[source url] URLByDeletingPathExtension];
m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"];
id audioSourceClass = NSClassFromString(@"AudioSource");
id<CogSource> 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.

Expand Down
17 changes: 1 addition & 16 deletions Plugins/GME/GameMetadataReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,7 @@ + (NSDictionary *)metadataForURL:(NSURL *)url {
return nil;
}

NSURL *m3uurl = [url URLByDeletingPathExtension];
m3uurl = [m3uurl URLByAppendingPathExtension:@"m3u"];
id<CogSource> 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)
Expand Down