Refactor JSBridge to simplify adding new methods. Add log(message) and a stub for fileExistsInMod(file,mod).

This commit is contained in:
Paul Chote
2010-11-17 12:31:52 +13:00
parent 40235db52e
commit f42f39f9c9
5 changed files with 56 additions and 11 deletions

View File

@@ -9,29 +9,71 @@
#import "JSBridge.h"
#import "Controller.h"
@implementation JSBridge
static JSBridge *SharedInstance;
-(id)initWithController:(Controller *)aController
@implementation JSBridge
@synthesize methods;
+ (JSBridge *)sharedInstance
{
if (SharedInstance == nil)
SharedInstance = [[JSBridge alloc] init];
return SharedInstance;
}
+ (NSString *)webScriptNameForSelector:(SEL)sel
{
return [[[JSBridge sharedInstance] methods] objectForKey:NSStringFromSelector(sel)];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
return [[[JSBridge sharedInstance] methods] objectForKey:NSStringFromSelector(sel)] == nil;
}
-(id)init
{
self = [super init];
if (self != nil)
{
controller = [aController retain];
methods = [[NSDictionary dictionaryWithObjectsAndKeys:
@"launchCurrentMod", NSStringFromSelector(@selector(launchCurrentMod)),
@"log", NSStringFromSelector(@selector(log:)),
@"fileExistsInMod", NSStringFromSelector(@selector(fileExists:inMod:)),
nil] retain];
}
return self;
}
- (void) dealloc
- (void)setController:(Controller *)aController
{
controller = [aController retain];
}
- (void)dealloc
{
[controller release]; controller = nil;
[super dealloc];
}
#pragma mark JS methods
- (void)launchCurrentMod
{
NSLog(@"launchcurrent");
[controller launchGame];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
- (void)log:(NSString *)message
{
NSLog(@"js: %@",message);
}
- (BOOL)fileExists:(NSString *)aFile inMod:(NSString *)aMod
{
NSLog(@"File %@ exists in mod %@",aFile, aMod);
return NO;
}
@end