[3ab618]: apple / CocoaWrapper / mac / CocoaWrapper.mm Maximize Restore History

Download this file

CocoaWrapper.mm    193 lines (160 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2011 The GemRB Project
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#import "CocoaWrapper.h"
#import "AppleLogger.h"
#import "Interface.h"
#import "System/FileStream.h"
#import "System/Logger/File.h"
using namespace GemRB;
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
but the method still is there and works. To avoid warnings, we declare
it ourselves here. */
@interface NSApplication(Missing_Methods)
- (void)setAppleMenu:(NSMenu *)menu;
@end
/* The main class of the application, the application's delegate */
@implementation CocoaWrapper
@synthesize prefrences=_prefrences;
- (BOOL)application:(NSApplication *) __unused theApplication openFile:(NSString *) filename
{
NSFileManager* fm = [NSFileManager defaultManager];
// only open if passed a directory
BOOL isDir = NO;
if ([fm fileExistsAtPath:filename isDirectory:&isDir] && isDir) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:filename forKey:@"GamePath"];
return YES;
}
return NO;
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *) __unused sender
{
//override this method using plugin categories.
NSLog(@"Application preparing for termination...");
return NSTerminateNow;
}
- (void)applicationWillTerminate:(NSNotification *) __unused aNotification
{
//override this method using plugin categories.
NSLog(@"Application terminate");
}
/* Called when the internal event loop has just started running */
- (void) applicationDidFinishLaunching: (NSNotification *) __unused note
{
AddLogger(createAppleLogger());
// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"defaults" ofType:@"plist"]]];
}
- (IBAction)openGame:(id) __unused sender
{
// be careful to use only methods available in 10.5!
NSOpenPanel* op = [NSOpenPanel openPanel];
[op setCanChooseDirectories:YES];
[op setCanChooseFiles:NO];
[op setAllowsMultipleSelection:NO];
[op setMessage:@"Select a folder containing an IE game."];
[op setPrompt:@"Select Game"];
if ([op runModal] == NSFileHandlingPanelOKButton) { //blocks till user selection
[self application:NSApp openFile:[op filename]];
}
}
- (IBAction)launchGame:(id) __unused sender
{
core = new Interface();
InterfaceConfig* config = new InterfaceConfig(0, NULL);
// load NSUserDefaults into config
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSDictionary* userValues = [defaults persistentDomainForName:@"net.sourceforge.gemrb"];
NSDictionary* defaultValues = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"defaults" ofType:@"plist"]];
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:defaultValues];
[dict addEntriesFromDictionary:userValues];
for ( NSString* key in dict ) {
NSString* value = nil;
id obj = [dict objectForKey:key];
if ([obj isKindOfClass:[NSNumber class]]) {
value = [(NSNumber*)obj stringValue];
} else if ([obj isKindOfClass:[NSString class]]) {
value = (NSString*)obj;
}
config->SetKeyValuePair([key cStringUsingEncoding:NSASCIIStringEncoding],
[value cStringUsingEncoding:NSASCIIStringEncoding]);
}
int status;
if ((status = core->Init(config)) == GEM_ERROR) {
delete config;
delete( core );
Log(MESSAGE, "Cocoa Wrapper", "Unable to initialize core. Terminating.");
} else {
[_prefrences close];
// pass control to GemRB
delete config;
core->Main();
delete( core );
ShutdownLogging();
// We must exit since the application runloop never returns.
// TODO: we want to be able to use NSApplication terminate method
// need fancier logic in shouldTerminate implementation first (in SDL plugin)
exit(status);
}
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([NSApp respondsToSelector:aSelector]) {
return YES;
}
return [super respondsToSelector:aSelector];
}
- (NSMethodSignature*) methodSignatureForSelector:(SEL)selector
{
// Check if car can handle the message
NSMethodSignature* signature = [super
methodSignatureForSelector:selector];
// If not, can the car info string handle the message?
if (!signature)
signature = [NSApp methodSignatureForSelector:selector];
return signature;
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
SEL selector = [invocation selector];
if ([NSApp respondsToSelector:selector])
{
[invocation invokeWithTarget:NSApp];
}
}
@end
/* Main entry point to executable - should *not* be GemRB_main! */
int main (int __unused argc, char ** __unused argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
/* Ensure the application object is initialised */
NSApplication* app = [NSApplication sharedApplication];
/* Set up the menubar */
[NSApp setMainMenu:[[NSMenu alloc] init]];
CocoaWrapper* wrapper = [[CocoaWrapper alloc] init];
[app setDelegate:wrapper];
[NSBundle loadNibNamed:@"GemRB" owner:wrapper];
/* Start the main event loop */
[pool drain];
[NSApp run];
[wrapper release];
[pool release];
return 0;
}