Debian Codegraph @

platforms/apple/CocoaWrapper/ios/GEM_AppDelegate.mm

raw
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2012 The GemRB Project
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 *
19 */
20
21 #import "AppleLogger.h"
22 #import "GEM_AppDelegate.h"
23 #import "Streams/FileStream.h"
24 #import "Interface.h"
25
26 using namespace GemRB;
27
28 @implementation SDLUIKitDelegate (GemDelegate)
29 + (NSString *)getAppDelegateClassName
30 {
31 //override this SDL method so an instance of our subclass is used
32 return @"GEM_AppDelegate";
33 }
34 @end
35
36 @implementation GEM_AppDelegate
37 @synthesize confControl;
38
39 // we MUST setup out wrapper interface here or iOS 5 will bitch about root view controller not being set
40 // and our interface will be the wrong orientation and distorted
41 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
42 {
43 [[NSFileManager defaultManager] changeCurrentDirectoryPath:NSHomeDirectory()];
44 // Normally you would call super implemetation first, but don't!
45 AddLogWriter(Logger::WriterPtr(new AppleLogger()));
46 ToggleLogging(true);
47 [self setupWrapper];
48
49 return [super application:application didFinishLaunchingWithOptions:launchOptions];
50 }
51
52 - (void)setupWrapper
53 {
54 configWin = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
55 configWin.backgroundColor = [UIColor blackColor];
56 confControl = [[GEM_ConfController alloc] init];
57 confControl.delegate = self;
58 nibObjects = nil;
59 // now load the config selector nib and display the list modally
60 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
61 nibObjects = [[NSBundle mainBundle] loadNibNamed:@"GEM_ConfController-ipad" owner:confControl options:nil];
62 } else {
63 nibObjects = [[NSBundle mainBundle] loadNibNamed:@"GEM_ConfController-iphone" owner:confControl options:nil];
64 }
65
66 confControl.rootVC.delegate = confControl;
67
68 [nibObjects retain];
69 configWin.rootViewController = confControl.rootVC;
70 configWin.screen = [UIScreen mainScreen];
71 [configWin makeKeyAndVisible]; // Note that this causes the window to be retained!
72 }
73
74 - (void)setupComplete:(NSString*)configPath
75 {
76 [self runGemRB:configPath];
77 }
78
79 - (void)runGemRB:(NSString*)configPath
80 {
81 int ret = GEM_OK;
82 if (configPath) {
83 NSLog(@"Using config file:%@", configPath);
84 // get argv
85 // manipulate argc & argv to have gemrb passed the argument for the config file to use.
86 NSMutableArray* procArguments = [[[NSProcessInfo processInfo] arguments] mutableCopy];
87 [procArguments addObject:@"-c"];
88 [procArguments addObject:configPath];
89
90 int argc = [procArguments count];
91 char** argv = (char**)malloc(argc * sizeof(char*));
92
93 int i = 0;
94 for (NSString* arg in procArguments) {
95 argv[i++] = (char*)[arg cStringUsingEncoding:NSASCIIStringEncoding];
96 }
97
98 [configWin resignKeyWindow];
99 configWin.rootViewController = nil;
100
101 [configWin release];
102 [nibObjects release];
103 [confControl release];
104 [procArguments release];
105
106 [[NSFileManager defaultManager] changeCurrentDirectoryPath:NSHomeDirectory()];
107 setenv("PYTHONHOME", "Documents/python", 1);
108 setenv("PYTHONPATH", "Documents/python/lib/python27", 1);
109
110 try {
111 Interface gemrb(LoadFromArgs(argc, argv));
112 free(argv);
113 gemrb.Main(); // pass control to GemRB
114 } catch (CoreInitializationException& cie) {
115 Log(FATAL, "Main", "Aborting due to fatal error... {}", cie);
116 // reload the wrapper interface so we can try again instead of dying
117 [self setupWrapper];
118 }
119
120 // We must exit since the application runloop never returns.
121 exit(GEM_OK);
122 }
123 }
124
125 - (void)toggleDebug:(id) __unused sender
126 {
127 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
128 NSString* logFile = [NSString stringWithFormat:@"%@/GemRB.log", [paths objectAtIndex:0]];
129 //OpenFile();
130 const char* cLogFile = [logFile cStringUsingEncoding:NSASCIIStringEncoding];
131 FileStream *fs = new FileStream();
132 if (fs->Create(cLogFile)) {
133 AddLogWriter(createStreamLogWriter(fs));
134 Log(MESSAGE, "Cocoa Wrapper", "Started a log file at {}", cLogFile);
135 } else {
136 delete fs;
137 Log(ERROR, "Cocoa Wrapper", "Unable to start log file at {}", cLogFile);
138 }
139 }
140
141 - (void)dealloc
142 {
143 // This is really just formallity.
144 // these objects would have been deallocated in runGemRB under normal circumstances
145
146 [configWin release];
147 [nibObjects release];
148 [confControl release];
149
150 [super dealloc];
151 }
152
153 @end