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

@@ -16,7 +16,6 @@
{
SidebarEntry *sidebarItems;
GameInstall *game;
JSBridge *jsbridge;
IBOutlet NSOutlineView *outlineView;
IBOutlet WebView *webView;
}

View File

@@ -18,7 +18,7 @@
- (void) awakeFromNib
{
game = [[GameInstall alloc] initWithURL:[NSURL URLWithString:@"/Users/paul/src/OpenRA"]];
jsbridge = [[JSBridge alloc] initWithController:self];
[[JSBridge sharedInstance] setController:self];
NSTableColumn *col = [outlineView tableColumnWithIdentifier:@"mods"];
ImageAndTextCell *imageAndTextCell = [[[ImageAndTextCell alloc] init] autorelease];
@@ -153,7 +153,7 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
#pragma mark WebView delegates
- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame
{
[windowObject setValue:jsbridge forKey:@"external"];
[windowObject setValue:[JSBridge sharedInstance] forKey:@"external"];
}
@end

View File

@@ -12,7 +12,11 @@
@class Controller;
@interface JSBridge : NSObject {
Controller *controller;
NSDictionary *methods;
}
@property(readonly) NSDictionary *methods;
+ (JSBridge *)sharedInstance;
- (void)setController:(Controller *)aController;
-(id)initWithController:(Controller *)aController;
@end

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

View File

@@ -95,7 +95,7 @@
</div>
<div id="buttons">
<input type="button" class="button" onclick="window.external.launchCurrentMod();" value="Play" />
<input type="button" class="button" value="Install Music" />
<input type="button" class="button" onclick="window.external.fileExistsInMod('foo.mix','ra');"value="Debug" />
</div>
</div>
</body>