Load mod.html into the right pane when clicked. Refactoring.

This commit is contained in:
Paul Chote
2010-11-16 18:45:46 +13:00
parent de7668e8ff
commit ed8203c896
10 changed files with 358 additions and 110 deletions

View File

@@ -11,47 +11,32 @@
@implementation GameInstall
-(id)initWithPath:(NSString *)path
-(id)initWithURL:(NSURL *)url
{
self = [super init];
if (self != nil)
{
gamePath = path;
gameURL = url;
}
return self;
}
- (void)dealloc
{
[utilityBuffer release];
[super dealloc];
}
-(void)clearBuffer
{
[utilityBuffer release];
utilityBuffer = [[NSMutableString stringWithString:@""] retain];
}
- (void)bufferData:(NSString *)string
{
if (string == nil) return;
[utilityBuffer appendString:string];
}
- (NSArray *)installedMods
{
[self clearBuffer];
[self runUtilityApp:@"-l" handleOutput:self withMethod:@selector(bufferData:)];
id mods = [[NSString stringWithString:utilityBuffer] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
id raw = [self runUtilityQuery:@"-l"];
id mods = [raw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return [mods componentsSeparatedByString:@"\n"];
}
- (NSArray *)infoForMods:(NSArray *)mods
{
[self clearBuffer];
[self runUtilityApp:[NSString stringWithFormat:@"-i=%@",[mods componentsJoinedByString:@","]] handleOutput:self withMethod:@selector(bufferData:)];
NSArray *lines = [utilityBuffer componentsSeparatedByString:@"\n"];
id query = [NSString stringWithFormat:@"-i=%@",[mods componentsJoinedByString:@","]];
NSArray *lines = [[self runUtilityQuery:query] componentsSeparatedByString:@"\n"];
NSMutableArray *ret = [NSMutableArray array];
NSMutableDictionary *fields = nil;
@@ -63,6 +48,9 @@
continue;
id kv = [line componentsSeparatedByString:@":"];
if ([kv count] < 2)
continue;
id key = [kv objectAtIndex:0];
id value = [kv objectAtIndex:1];
@@ -76,7 +64,10 @@
{
// Commit prev mod
if (current != nil)
[ret addObject:[Mod modWithId:current fields:fields]];
{
id url = [gameURL URLByAppendingPathComponent:[NSString stringWithFormat:@"mods/%@",current]];
[ret addObject:[Mod modWithId:current fields:fields baseURL:url]];
}
NSLog(@"Parsing mod %@",value);
current = value;
fields = [NSMutableDictionary dictionary];
@@ -93,7 +84,7 @@
{
// Use LaunchServices because neither NSTask or NSWorkspace support Info.plist _and_ arguments pre-10.6
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"OpenRA.app/Contents/MacOS/OpenRA"];
NSArray *args = [NSArray arrayWithObjects:gamePath, @"mono", @"--debug", @"OpenRA.Game.exe", @"Game.Mods=ra",nil];
NSArray *args = [NSArray arrayWithObjects:[gameURL absoluteString], @"mono", @"--debug", @"OpenRA.Game.exe", @"Game.Mods=ra",nil];
FSRef appRef;
CFURLGetFSRef((CFURLRef)[NSURL URLWithString:path], &appRef);
@@ -116,7 +107,7 @@
SetFrontProcess(&psn);
}
- (void)runUtilityApp:(NSString *)arg handleOutput:(id)obj withMethod:(SEL)sel
- (NSString *)runUtilityQuery:(NSString *)arg
{
NSTask *aTask = [[NSTask alloc] init];
NSPipe *aPipe = [NSPipe pipe];
@@ -125,7 +116,29 @@
NSMutableArray *taskArgs = [NSMutableArray arrayWithObject:@"OpenRA.Utility.exe"];
[taskArgs addObject:arg];
[aTask setCurrentDirectoryPath:gamePath];
[aTask setCurrentDirectoryPath:[gameURL absoluteString]];
[aTask setLaunchPath:@"/Library/Frameworks/Mono.framework/Commands/mono"];
[aTask setArguments:taskArgs];
[aTask setStandardOutput:aPipe];
[aTask setStandardError:[aTask standardOutput]];
[aTask launch];
NSData *data = [readHandle readDataToEndOfFile];
[aTask release];
return [NSString stringWithUTF8String:[data bytes]];
}
- (void)runUtilityQuery:(NSString *)arg handleOutput:(id)obj withMethod:(SEL)sel
{
NSTask *aTask = [[NSTask alloc] init];
NSPipe *aPipe = [NSPipe pipe];
NSFileHandle *readHandle = [aPipe fileHandleForReading];
NSMutableArray *taskArgs = [NSMutableArray arrayWithObject:@"OpenRA.Utility.exe"];
[taskArgs addObject:arg];
[aTask setCurrentDirectoryPath:[gameURL absoluteString]];
[aTask setLaunchPath:@"/Library/Frameworks/Mono.framework/Commands/mono"];
[aTask setArguments:taskArgs];
[aTask setStandardOutput:aPipe];
@@ -134,7 +147,7 @@
NSData *inData = nil;
while ((inData = [readHandle availableData]) && [inData length])
[obj performSelector:sel withObject:[NSString stringWithUTF8String:[inData bytes]]];
[aTask waitUntilExit];
[aTask release];
}