diff --git a/Ample/AppDelegate.m b/Ample/AppDelegate.m index 5c91a5a..2d57b7e 100644 --- a/Ample/AppDelegate.m +++ b/Ample/AppDelegate.m @@ -115,11 +115,11 @@ dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)); [task setTerminationHandler: ^(NSTask *task){ - + + int st = [task terminationStatus]; + // delay so the install window is visible, I think dispatch_after(when, dispatch_get_main_queue(), ^{ - int st = [task terminationStatus]; - if (st) { NSAlert *alert = [NSAlert new]; [alert setMessageText: @"An error occurred extracting MAME components"]; @@ -238,4 +238,70 @@ } +-(IBAction)installMameComponents:(id)sender { + + + /* install the mame data components. */ + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSBundle *bundle = [NSBundle mainBundle]; + NSURL *sd = SupportDirectory(); + + NSURL *ample_url = [sd URLByAppendingPathComponent: @"Ample.plist"]; + NSMutableDictionary *d = [NSMutableDictionary dictionaryWithContentsOfURL: ample_url]; + + //NSDate *oldDate = [d objectForKey: kMameComponentsDate]; + NSDate *newDate = [defaults objectForKey: kMameComponentsDate]; + if (![newDate isKindOfClass: [NSDate class]]) + newDate = nil; + + NSString *path = [bundle pathForResource: @"mame-data" ofType: @"tgz"]; + if (!path) return; // Ample Lite? + + + NSWindow *win = _installWindow; + [win makeKeyAndOrderFront: nil]; + NSTask *task = [NSTask new]; + NSArray *argv = @[ + @"xfz", + path + ]; + if (@available(macOS 10.13, *)) { + [task setExecutableURL: [NSURL fileURLWithPath: @"/usr/bin/tar"]]; + [task setCurrentDirectoryURL: sd]; + } else { + [task setLaunchPath: @"/usr/bin/tar"]; + [task setCurrentDirectoryPath: SupportDirectoryPath()]; + } + [task setArguments: argv]; + + + //dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)); + [task setTerminationHandler: ^(NSTask *task){ + + int st = [task terminationStatus]; + dispatch_async(dispatch_get_main_queue(), ^{ + + if (st) { + NSAlert *alert = [NSAlert new]; + [alert setMessageText: @"An error occurred extracting MAME components"]; + [alert runModal]; + [win close]; + return; + } + + if (d) { + [d setObject: newDate forKey: kMameComponentsDate]; + [d writeToURL: ample_url atomically: YES]; + } else { + [@{ kMameComponentsDate: newDate } writeToURL: ample_url atomically: YES]; + } + [win close]; + // need to reload the software list data. + }); + + }]; + [task launch]; + +} + @end diff --git a/Ample/Base.lproj/MainMenu.xib b/Ample/Base.lproj/MainMenu.xib index 1ef203a..e6f90f9 100644 --- a/Ample/Base.lproj/MainMenu.xib +++ b/Ample/Base.lproj/MainMenu.xib @@ -45,6 +45,9 @@ + diff --git a/Ample/SoftwareList.h b/Ample/SoftwareList.h index 427185a..131e326 100644 --- a/Ample/SoftwareList.h +++ b/Ample/SoftwareList.h @@ -39,6 +39,8 @@ @interface SoftwareSet : NSObject +(instancetype)softwareSetForMachine: (NSString *)machine; ++(void)invalidate; + -(BOOL)nameIsUnique: (NSString *)name; -(NSString *)nameForSoftware: (Software *)software; diff --git a/Ample/SoftwareList.m b/Ample/SoftwareList.m index 63cddaf..0ad4272 100644 --- a/Ample/SoftwareList.m +++ b/Ample/SoftwareList.m @@ -512,9 +512,15 @@ NSArray *SoftwareListForMachine(NSString *machine) { } + +static NSCache *cache; ++(void)invalidate { +// called after mame components are updated. clears the cache. + cache = nil; +} + +(instancetype)softwareSetForMachine:(NSString *)machine { - static NSCache *cache; if (!cache) cache = [NSCache new];