From b40b8521dd4e8c7fa6bd4347dcd63a1b7c0ecbb7 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 11 Feb 2022 04:18:23 -0800 Subject: [PATCH] SID Input: Open the files for decoding regardless No more file handle caching, this was probably a source of crashes. Signed-off-by: Christopher Snowhill --- Plugins/sidplay/SidDecoder.h | 3 - Plugins/sidplay/SidDecoder.mm | 104 +++------------------------------- 2 files changed, 9 insertions(+), 98 deletions(-) diff --git a/Plugins/sidplay/SidDecoder.h b/Plugins/sidplay/SidDecoder.h index e3b69a691..de7f75bce 100644 --- a/Plugins/sidplay/SidDecoder.h +++ b/Plugins/sidplay/SidDecoder.h @@ -23,7 +23,6 @@ sidplayfp *engine; sidbuilder *builder; - id source; long length; NSString *currentUrl; @@ -36,7 +35,5 @@ long fadeRemain; } -- (void)setSource:(id)s; -- (id)source; - (void)cleanUp; @end diff --git a/Plugins/sidplay/SidDecoder.mm b/Plugins/sidplay/SidDecoder.mm index d5580c3f3..555545708 100644 --- a/Plugins/sidplay/SidDecoder.mm +++ b/Plugins/sidplay/SidDecoder.mm @@ -21,83 +21,18 @@ static const char *extListEmpty[] = { NULL }; static const char *extListStr[] = { ".str", NULL }; -static NSLock *_lock = [[NSLock alloc] init]; - -@interface sid_file_container : NSObject { - NSLock *lock; - NSMutableDictionary *list; -} -+ (sid_file_container *)instance; -- (void)add_hint:(NSString *)path source:(id)source; -- (void)remove_hint:(NSString *)path; -- (BOOL)try_hint:(NSString *)path source:(id *)source; -@end - -@implementation sid_file_container -+ (sid_file_container *)instance { - static sid_file_container *instance; - - @synchronized(self) { - if(!instance) { - instance = [[self alloc] init]; - } - } - - return instance; -} -- (sid_file_container *)init { - if((self = [super init])) { - lock = [[NSLock alloc] init]; - list = [[NSMutableDictionary alloc] initWithCapacity:0]; - } - return self; -} -- (void)add_hint:(NSString *)path source:(id)source { - [lock lock]; - [list setObject:source forKey:path]; - [lock unlock]; -} -- (void)remove_hint:(NSString *)path { - [lock lock]; - [list removeObjectForKey:path]; - [lock unlock]; -} -- (BOOL)try_hint:(NSString *)path source:(id *)source { - [lock lock]; - *source = [list objectForKey:path]; - [lock unlock]; - if(*source) { - [*source seek:0 whence:0]; - return YES; - } else { - return NO; - } -} -@end - static void sidTuneLoader(const char *fileName, std::vector &bufferRef) { - id source; - BOOL usedHint = YES; - [_lock lock]; - if(![[sid_file_container instance] try_hint:[NSString stringWithUTF8String:fileName] source:&source]) { - usedHint = NO; + NSString *urlString = [NSString stringWithUTF8String:fileName]; + NSURL *url = [NSURL URLWithDataRepresentation:[urlString dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil]; - NSString *urlString = [NSString stringWithUTF8String:fileName]; - NSURL *url = [NSURL URLWithDataRepresentation:[urlString dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil]; + id audioSourceClass = NSClassFromString(@"AudioSource"); + id source = [audioSourceClass audioSourceForURL:url]; - id audioSourceClass = NSClassFromString(@"AudioSource"); - source = [audioSourceClass audioSourceForURL:url]; + if(![source open:url]) + return; - if(![source open:url]) { - [_lock unlock]; - return; - } - - if(![source seekable]) { - [_lock unlock]; - return; - } - } + if(![source seekable]) + return; [source seek:0 whence:SEEK_END]; long size = [source tell]; @@ -107,10 +42,7 @@ static void sidTuneLoader(const char *fileName, std::vector &bufferRef) [source read:&bufferRef[0] amount:size]; - if(!usedHint) - [source close]; - - [_lock unlock]; + [source close]; } @implementation SidDecoder @@ -119,8 +51,6 @@ static void sidTuneLoader(const char *fileName, std::vector &bufferRef) if(![s seekable]) return NO; - [self setSource:s]; - NSString *path = [[s url] absoluteString]; NSRange fragmentRange = [path rangeOfString:@"#" options:NSBackwardsSearch]; if(fragmentRange.location != NSNotFound) { @@ -129,9 +59,6 @@ static void sidTuneLoader(const char *fileName, std::vector &bufferRef) currentUrl = [path stringByRemovingPercentEncoding]; - [[sid_file_container instance] add_hint:currentUrl source:s]; - hintAdded = YES; - NSString *extension = [[s url] pathExtension]; const char **extList = [extension isEqualToString:@"mus"] ? extListStr : extListEmpty; @@ -326,11 +253,6 @@ static void sidTuneLoader(const char *fileName, std::vector &bufferRef) tune = NULL; } - source = nil; - if(hintAdded) { - [[sid_file_container instance] remove_hint:currentUrl]; - hintAdded = NO; - } currentUrl = nil; } @@ -342,14 +264,6 @@ static void sidTuneLoader(const char *fileName, std::vector &bufferRef) [self close]; } -- (void)setSource:(id)s { - source = s; -} - -- (id)source { - return source; -} - + (NSArray *)fileTypes { return @[@"sid", @"mus"]; }