Made necessary changes to facilitate metadata reading from arbitrary file sources, like archives

CQTexperiment
Chris Moeller 2013-10-04 08:14:47 -07:00
parent 1c37cefae6
commit 810d58b4fe
6 changed files with 67 additions and 39 deletions

View File

@ -16,6 +16,8 @@
#import "Plugin.h" #import "Plugin.h"
extern DUMBFILE_SYSTEM dfs;
@interface DumbDecoder : NSObject <CogDecoder> { @interface DumbDecoder : NSObject <CogDecoder> {
DUH *duh; DUH *duh;
DUH_SIGRENDERER *dsr; DUH_SIGRENDERER *dsr;

View File

@ -12,9 +12,9 @@
int skipCallback(void *f, long n) int skipCallback(void *f, long n)
{ {
DumbDecoder *decoder = (DumbDecoder *)f; id source = (id)f;
if (![[decoder source] seek:n whence: SEEK_CUR]) if (![source seek:n whence: SEEK_CUR])
{ {
return 1; //Non-zero is error return 1; //Non-zero is error
} }
@ -24,11 +24,11 @@ int skipCallback(void *f, long n)
int getCharCallback(void *f) int getCharCallback(void *f)
{ {
DumbDecoder *decoder = (DumbDecoder *)f; id source = (id)f;
unsigned char c; unsigned char c;
if ([[decoder source] read:&c amount:1] < 1) if ([source read:&c amount:1] < 1)
{ {
return -1; return -1;
} }
@ -38,34 +38,34 @@ int getCharCallback(void *f)
long readCallback(char *ptr, long n, void *f) long readCallback(char *ptr, long n, void *f)
{ {
DumbDecoder *decoder = (DumbDecoder *)f; id source = (id)f;
return [[decoder source] read:ptr amount:n]; return [source read:ptr amount:n];
} }
int seekCallback(void *f, long n) int seekCallback(void *f, long n)
{ {
DumbDecoder *decoder = (DumbDecoder *)f; id source = (id)f;
if (![[decoder source] seekable]) return -1; if (![source seekable]) return -1;
if ([[decoder source] seek:n whence:SEEK_SET]) return 0; if ([source seek:n whence:SEEK_SET]) return 0;
else return -1; else return -1;
} }
long getsizeCallback(void *f) long getsizeCallback(void *f)
{ {
DumbDecoder *decoder = (DumbDecoder *)f; id source = (id)f;
if (![[decoder source] seekable]) return 0; if (![source seekable]) return 0;
long current_offset = [[decoder source] tell]; long current_offset = [source tell];
[[decoder source] seek:0 whence:SEEK_END]; [source seek:0 whence:SEEK_END];
long size = [[decoder source] tell]; long size = [source tell];
[[decoder source] seek:current_offset whence:SEEK_SET]; [source seek:current_offset whence:SEEK_SET];
return size; return size;
} }
@ -79,24 +79,25 @@ void oneTimeInit()
} }
} }
DUMBFILE_SYSTEM dfs = {
.open = NULL,
.skip = skipCallback,
.getc = getCharCallback,
.getnc = readCallback,
.close = NULL,
.seek = seekCallback,
.get_size = getsizeCallback
};
- (BOOL)open:(id<CogSource>)s - (BOOL)open:(id<CogSource>)s
{ {
[self setSource:s]; [self setSource:s];
DUMBFILE *df; DUMBFILE *df;
DUMBFILE_SYSTEM dfs;
dfs.open = NULL;
dfs.skip = skipCallback;
dfs.getc = getCharCallback;
dfs.getnc = readCallback;
dfs.close = NULL;
dfs.seek = seekCallback;
dfs.get_size = getsizeCallback;
// dumb_register_stdfiles(); // dumb_register_stdfiles();
df = dumbfile_open_ex(self, &dfs); df = dumbfile_open_ex(s, &dfs);
if (!df) if (!df)
{ {
NSLog(@"EX Failed"); NSLog(@"EX Failed");

View File

@ -25,14 +25,27 @@
+ (NSDictionary *)metadataForURL:(NSURL *)url + (NSDictionary *)metadataForURL:(NSURL *)url
{ {
if (![url isFileURL]) id audioSourceClass = NSClassFromString(@"AudioSource");
return nil; id<CogSource> source = [audioSourceClass audioSourceForURL:url];
dumb_register_stdfiles(); if (![source open:url])
return 0;
if (![source seekable])
return 0;
DUMBFILE * df = dumbfile_open_ex(source, &dfs);
if (!df)
{
NSLog(@"EX Failed");
return NO;
}
DUH *duh; DUH *duh;
NSString *ext = [[[url path] pathExtension] lowercaseString]; NSString *ext = [[[url path] pathExtension] lowercaseString];
duh = dumb_load_any_quick([[url path] UTF8String], [ext isEqualToString:@"mod"] ? 0 : 1, 0); duh = dumb_read_any_quick(df, [ext isEqualToString:@"mod"] ? 0 : 1, 0);
dumbfile_close(df);
if (!duh) if (!duh)
{ {

View File

@ -12,6 +12,8 @@
#import "Plugin.h" #import "Plugin.h"
extern gme_err_t readCallback( void* data, void* out, long count );
@interface GameDecoder : NSObject <CogDecoder> { @interface GameDecoder : NSObject <CogDecoder> {
Music_Emu* emu; Music_Emu* emu;
id<CogSource> source; id<CogSource> source;

View File

@ -12,9 +12,9 @@
gme_err_t readCallback( void* data, void* out, long count ) gme_err_t readCallback( void* data, void* out, long count )
{ {
GameDecoder *decoder = (GameDecoder *)data; id source = (id)data;
NSLog(@"Amount: %li", count); NSLog(@"Amount: %li", count);
int n = [[decoder source] read:out amount:count]; int n = [source read:out amount:count];
NSLog(@"Read: %i", n); NSLog(@"Read: %i", n);
if (n <= 0) { if (n <= 0) {
@ -58,7 +58,7 @@ gme_err_t readCallback( void* data, void* out, long count )
NSLog(@"Size: %li", size); NSLog(@"Size: %li", size);
error = gme_load_custom(emu, readCallback, size, self); error = gme_load_custom(emu, readCallback, size, s);
if (error) if (error)
{ {
NSLog(@"ERROR Loding custom!"); NSLog(@"ERROR Loding custom!");

View File

@ -26,8 +26,14 @@
+ (NSDictionary *)metadataForURL:(NSURL *)url + (NSDictionary *)metadataForURL:(NSURL *)url
{ {
if (![url isFileURL]) id audioSourceClass = NSClassFromString(@"AudioSource");
return nil; id<CogSource> source = [audioSourceClass audioSourceForURL:url];
if (![source open:url])
return 0;
if (![source seekable])
return 0;
NSString *ext = [[[url path] pathExtension] lowercaseString]; NSString *ext = [[[url path] pathExtension] lowercaseString];
@ -46,8 +52,12 @@
return NO; return NO;
} }
[source seek:0 whence:SEEK_END];
long size = [source tell];
[source seek:0 whence:SEEK_SET];
gme_err_t error; gme_err_t error;
error = gme_load_file(emu, [[url path] UTF8String]); error = gme_load_custom(emu, readCallback, size, source);
if (error) if (error)
{ {
NSLog(@"ERROR Loding file!"); NSLog(@"ERROR Loding file!");