From e9ba8e2d7820b103df2ba355cf16eef0be1ec064 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 20 Mar 2010 00:15:18 +1300 Subject: [PATCH] Read/Write settings.ini; use this to select the game to start. --- packaging/osx/launcher/Controller.h | 6 +- packaging/osx/launcher/Controller.m | 26 +++++- .../osx/launcher/English.lproj/MainMenu.xib | 6 +- .../launcher/OpenRA.xcodeproj/project.pbxproj | 6 ++ packaging/osx/launcher/Settings.h | 33 +++++++ packaging/osx/launcher/Settings.m | 92 +++++++++++++++++++ 6 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 packaging/osx/launcher/Settings.h create mode 100644 packaging/osx/launcher/Settings.m diff --git a/packaging/osx/launcher/Controller.h b/packaging/osx/launcher/Controller.h index c865ad4894..9c9aa583ce 100644 --- a/packaging/osx/launcher/Controller.h +++ b/packaging/osx/launcher/Controller.h @@ -18,9 +18,13 @@ #import - +@class Settings; @interface Controller : NSObject { // Main Window + NSDictionary *modButtonMappings; + Settings *settings; + + // Package Downloader NSString *localDownloadPath; NSString *packageDirectory; NSURLDownload *currentDownload; diff --git a/packaging/osx/launcher/Controller.m b/packaging/osx/launcher/Controller.m index ac7279ef7f..86f5032448 100644 --- a/packaging/osx/launcher/Controller.m +++ b/packaging/osx/launcher/Controller.m @@ -17,12 +17,28 @@ */ #import "Controller.h" - +#import "Settings.h" @implementation Controller +- (void)awakeFromNib +{ + NSURL *settingsFile = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"launcher.ini"]]; + settings = [[Settings alloc] init]; + + [settings loadSettingsFile:settingsFile]; + + modButtonMappings = [[NSDictionary dictionaryWithObjectsAndKeys: + @"cnc",@"C&C", + @"ra",@"Red Alert", + nil] retain]; +} + -(IBAction)launchApp:(id)sender { + [settings setValue:[modButtonMappings objectForKey:[sender title]] forSetting:@"InitialMods"]; + [settings save]; + [[NSWorkspace sharedWorkspace] launchApplication:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"OpenRA.app"]]; [NSApp terminate: nil]; } @@ -140,4 +156,12 @@ } } +- (void) dealloc +{ + [modButtonMappings release]; + [settings release]; + [super dealloc]; +} + + @end diff --git a/packaging/osx/launcher/English.lproj/MainMenu.xib b/packaging/osx/launcher/English.lproj/MainMenu.xib index 28161654f2..2ebe447a29 100644 --- a/packaging/osx/launcher/English.lproj/MainMenu.xib +++ b/packaging/osx/launcher/English.lproj/MainMenu.xib @@ -12,8 +12,8 @@ YES - + @@ -925,6 +925,7 @@ + cnc 534 @@ -944,6 +945,7 @@ + ra 549 @@ -1215,7 +1217,7 @@ com.apple.InterfaceBuilder.CocoaPlugin {{525, 802}, {197, 73}} - {{226, 941}, {221, 20}} + {{226, 836}, {221, 20}} com.apple.InterfaceBuilder.CocoaPlugin {74, 862} diff --git a/packaging/osx/launcher/OpenRA.xcodeproj/project.pbxproj b/packaging/osx/launcher/OpenRA.xcodeproj/project.pbxproj index 32a3b247f7..0eae321e17 100644 --- a/packaging/osx/launcher/OpenRA.xcodeproj/project.pbxproj +++ b/packaging/osx/launcher/OpenRA.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; }; DA1241101151F194002EFE2B /* OpenRa.icns in Resources */ = {isa = PBXBuildFile; fileRef = DA12410F1151F194002EFE2B /* OpenRa.icns */; }; + DAAB5C7911536D6500DCCB80 /* Settings.m in Sources */ = {isa = PBXBuildFile; fileRef = DAAB5C7811536D6500DCCB80 /* Settings.m */; }; DAFD657B11520799001F4C97 /* launcher.ini in Resources */ = {isa = PBXBuildFile; fileRef = DAFD657A11520799001F4C97 /* launcher.ini */; }; /* End PBXBuildFile section */ @@ -30,6 +31,8 @@ 8D1107310486CEB800E47090 /* OpenRA-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "OpenRA-Info.plist"; sourceTree = ""; }; 8D1107320486CEB800E47090 /* OpenRA.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OpenRA.app; sourceTree = BUILT_PRODUCTS_DIR; }; DA12410F1151F194002EFE2B /* OpenRa.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = OpenRa.icns; sourceTree = ""; }; + DAAB5C7711536D6500DCCB80 /* Settings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Settings.h; sourceTree = ""; }; + DAAB5C7811536D6500DCCB80 /* Settings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Settings.m; sourceTree = ""; }; DAFD657A11520799001F4C97 /* launcher.ini */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = launcher.ini; sourceTree = ""; }; /* End PBXFileReference section */ @@ -50,6 +53,8 @@ children = ( 8990E7AD1151C0F20089C198 /* Controller.h */, 8990E7AE1151C0F20089C198 /* Controller.m */, + DAAB5C7711536D6500DCCB80 /* Settings.h */, + DAAB5C7811536D6500DCCB80 /* Settings.m */, ); name = Classes; sourceTree = ""; @@ -200,6 +205,7 @@ files = ( 8D11072D0486CEB800E47090 /* main.m in Sources */, 8990E7AF1151C0F20089C198 /* Controller.m in Sources */, + DAAB5C7911536D6500DCCB80 /* Settings.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/packaging/osx/launcher/Settings.h b/packaging/osx/launcher/Settings.h new file mode 100644 index 0000000000..1e9a023595 --- /dev/null +++ b/packaging/osx/launcher/Settings.h @@ -0,0 +1,33 @@ +/* + * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. + * This file is part of OpenRA. + * + * OpenRA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * OpenRA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OpenRA. If not, see . + */ + +#import + + +@interface Settings : NSObject { + NSMutableDictionary *settings; + NSURL *filePath; +} + + +- (NSString *)valueForSetting:(NSString *)key; +- (void)setValue:(NSString *)value forSetting:(NSString *)key; +- (void)loadSettingsFile:(NSURL *)filePath; +- (void)save; +- (void)saveToFile:(NSURL *)filePath; +@end diff --git a/packaging/osx/launcher/Settings.m b/packaging/osx/launcher/Settings.m new file mode 100644 index 0000000000..0194d0ebc0 --- /dev/null +++ b/packaging/osx/launcher/Settings.m @@ -0,0 +1,92 @@ +/* + * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. + * This file is part of OpenRA. + * + * OpenRA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * OpenRA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OpenRA. If not, see . + */ + +#import "Settings.h" + + +@implementation Settings + +- (id)init +{ + self = [super init]; + if (self != nil) + { + settings = [[NSMutableDictionary alloc] init]; + filePath = nil; + } + return self; +} + +- (NSString *)valueForSetting:(NSString *)key +{ + return [settings valueForKey:key]; +} + +- (void)setValue:(NSString *)value forSetting:(NSString *)key +{ + [settings setValue:value forKey:key]; +} + +- (void)loadSettingsFile:(NSURL *)file +{ + [filePath autorelease]; + filePath = file; + [filePath retain]; + + NSError *error; + NSString *data = [NSString stringWithContentsOfURL:filePath encoding:NSUTF8StringEncoding error:&error]; + NSArray *lines = [data componentsSeparatedByString:@"\n"]; + + for (id line in lines) + { + NSArray *cmp = [line componentsSeparatedByString:@"="]; + if ([cmp count] != 2) + continue; + + NSString *key = [[cmp objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + NSString *value = [[cmp objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + [settings setObject:value forKey:key]; + } + NSLog(@"Loaded settings: %@",settings); +} + +- (void)save +{ + [self saveToFile:filePath]; +} + +- (void)saveToFile:(NSURL *)file +{ + NSMutableString *data = [NSMutableString stringWithString:@"[Settings]\n"]; + for (id key in settings) + { + [data appendFormat:@"%@=%@\n",key, [settings valueForKey:key]]; + } + NSError *error; + [data writeToURL:file atomically:YES encoding:NSUTF8StringEncoding error:&error]; +} + +- (void) dealloc +{ + [settings release]; settings = nil; + [super dealloc]; +} + + +@end