| 1 |
/* GemRB - Infinity Engine Emulator |
| 2 |
* Copyright (C) 2011 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 "GEM_ConfController.h" |
| 22 |
|
| 23 |
#import <SDL.h> |
| 24 |
|
| 25 |
#include <archive.h> |
| 26 |
#include <archive_entry.h> |
| 27 |
|
| 28 |
#include <sys/stat.h> |
| 29 |
#include <cstdio> |
| 30 |
#include <fcntl.h> |
| 31 |
|
| 32 |
enum ConfigTableSection { |
| 33 |
TABLE_SEC_CONFIG = 0, |
| 34 |
TABLE_SEC_INSTALL = 1, |
| 35 |
TABLE_SEC_LOG = 2 |
| 36 |
}; |
| 37 |
|
| 38 |
@implementation GEM_NavController |
| 39 |
|
| 40 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation |
| 41 |
{ |
| 42 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
| 43 |
// Don't ask me why, but these need to be reversed to revent the screen from being upsidedown. |
| 44 |
if (interfaceOrientation == UIDeviceOrientationLandscapeLeft) |
| 45 |
SDL_SetHintWithPriority(SDL_HINT_ORIENTATIONS, "LandscapeRight", SDL_HINT_OVERRIDE); |
| 46 |
else if (interfaceOrientation == UIDeviceOrientationLandscapeRight) |
| 47 |
SDL_SetHintWithPriority(SDL_HINT_ORIENTATIONS, "LandscapeLeft", SDL_HINT_OVERRIDE); |
| 48 |
return UIInterfaceOrientationIsLandscape(interfaceOrientation); |
| 49 |
}else{ |
| 50 |
return UIInterfaceOrientationIsPortrait(interfaceOrientation); |
| 51 |
} |
| 52 |
return NO; |
| 53 |
} |
| 54 |
|
| 55 |
@end |
| 56 |
|
| 57 |
@implementation GEM_ConfController |
| 58 |
@synthesize configIndexPath; |
| 59 |
@synthesize editor; |
| 60 |
@synthesize controlTable; |
| 61 |
@synthesize rootVC; |
| 62 |
@synthesize editorVC; |
| 63 |
@synthesize playButton; |
| 64 |
@synthesize delegate; |
| 65 |
|
| 66 |
- (id)init |
| 67 |
{ |
| 68 |
self = [super init]; |
| 69 |
if (self) { |
| 70 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 71 |
docDir = [[paths objectAtIndex:0] copy]; |
| 72 |
|
| 73 |
configFiles = nil; |
| 74 |
installFiles = nil; |
| 75 |
logFiles = nil; |
| 76 |
configIndexPath = nil; |
| 77 |
rootVC = nil; |
| 78 |
editorVC = nil; |
| 79 |
|
| 80 |
editorButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveConfig:)]; |
| 81 |
editorButton.style = UIBarButtonItemStyleDone; |
| 82 |
editorButton.enabled = NO; |
| 83 |
|
| 84 |
UIBarButtonItem* debugBtn = [[UIBarButtonItem alloc] initWithTitle:@"Debug" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleDebug:)]; |
| 85 |
|
| 86 |
UIBarButtonItem* savesBtn = [[UIBarButtonItem alloc] initWithTitle:@"Saves" style:UIBarButtonItemStyleBordered target:self action:@selector(showSaves:)]; |
| 87 |
|
| 88 |
UIBarButtonItem* space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; |
| 89 |
|
| 90 |
toolBarItems = [NSArray arrayWithObjects:debugBtn, savesBtn, space, editorButton, nil]; |
| 91 |
[debugBtn release]; |
| 92 |
[savesBtn release]; |
| 93 |
[space release]; |
| 94 |
[toolBarItems retain]; |
| 95 |
|
| 96 |
[self reloadTableData]; |
| 97 |
} |
| 98 |
return self; |
| 99 |
} |
| 100 |
|
| 101 |
- (void)toggleDebug:(id) __unused sender |
| 102 |
{ |
| 103 |
[delegate toggleDebug:sender]; |
| 104 |
} |
| 105 |
|
| 106 |
- (void)reloadTableData |
| 107 |
{ |
| 108 |
if (configFiles) [configFiles release]; |
| 109 |
if (installFiles) [installFiles release]; |
| 110 |
if (logFiles) [logFiles release]; |
| 111 |
|
| 112 |
NSFileManager *manager = [NSFileManager defaultManager]; |
| 113 |
NSArray* files = [manager contentsOfDirectoryAtPath:docDir error:nil]; |
| 114 |
|
| 115 |
NSLog(@"scanning documents: %@", [files description]); |
| 116 |
|
| 117 |
configFiles = [files filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.cfg'"]]; |
| 118 |
NSArray *installExtensions = [NSArray arrayWithObjects:@"bg1", @"bg2", @"iwd", @"how", @"iwd2", @"pst", nil]; |
| 119 |
installFiles = [files filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", installExtensions]]; |
| 120 |
logFiles = [files filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.log'"]]; |
| 121 |
|
| 122 |
[configFiles retain]; |
| 123 |
[installFiles retain]; |
| 124 |
[logFiles retain]; |
| 125 |
|
| 126 |
[controlTable reloadData]; |
| 127 |
} |
| 128 |
|
| 129 |
- (void)dealloc |
| 130 |
{ |
| 131 |
// cancel any pending requests to install game data |
| 132 |
[NSObject cancelPreviousPerformRequestsWithTarget:self]; |
| 133 |
|
| 134 |
[docDir release]; |
| 135 |
[editorButton release]; |
| 136 |
|
| 137 |
[configFiles release]; |
| 138 |
[installFiles release]; |
| 139 |
[logFiles release]; |
| 140 |
|
| 141 |
[toolBarItems release]; |
| 142 |
|
| 143 |
[super dealloc]; |
| 144 |
} |
| 145 |
|
| 146 |
- (void)navigationController:(UINavigationController *) __unused navigationController willShowViewController:(UIViewController *) __unused viewController animated:(BOOL) __unused animated |
| 147 |
{ |
| 148 |
rootVC.topViewController.toolbarItems = toolBarItems; |
| 149 |
} |
| 150 |
|
| 151 |
- (NSString*)selectedConfigPath |
| 152 |
{ |
| 153 |
if (!configIndexPath) return nil; |
| 154 |
return [NSString stringWithFormat:@"%@/%@", docDir, [configFiles objectAtIndex:configIndexPath.row]]; |
| 155 |
} |
| 156 |
|
| 157 |
- (BOOL)installGame:(NSIndexPath*)indexPath |
| 158 |
{ |
| 159 |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
| 160 |
NSString* archivePath = [installFiles objectAtIndex:indexPath.row]; |
| 161 |
NSString* srcPath = [NSString stringWithFormat:@"%@/%@", docDir, archivePath]; |
| 162 |
|
| 163 |
NSFileManager* fm = [NSFileManager defaultManager]; |
| 164 |
|
| 165 |
NSError* error = nil; |
| 166 |
unsigned long long totalBytes = [[fm attributesOfItemAtPath:srcPath error:&error] fileSize]; |
| 167 |
|
| 168 |
if (error) { |
| 169 |
NSLog(@"FM error for %@:\n%@", srcPath, [error localizedDescription]); |
| 170 |
[pool release]; |
| 171 |
return NO; |
| 172 |
} |
| 173 |
|
| 174 |
__block UIProgressView* pv = nil; |
| 175 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 176 |
UITableViewCell* selectedCell = [controlTable cellForRowAtIndexPath:indexPath]; |
| 177 |
|
| 178 |
pv = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; |
| 179 |
UIView* contentView = selectedCell.contentView; |
| 180 |
CGRect frame = CGRectMake(0.0, 0.0, contentView.frame.size.width, contentView.frame.size.height); |
| 181 |
pv.frame = frame; |
| 182 |
[contentView addSubview:pv]; |
| 183 |
}); |
| 184 |
|
| 185 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); |
| 186 |
NSString* libDir = [[paths objectAtIndex:0] copy]; |
| 187 |
|
| 188 |
NSString* dstPath = [NSString stringWithFormat:@"%@/%@/tmp/", libDir, [archivePath pathExtension]]; |
| 189 |
[fm createDirectoryAtPath:dstPath withIntermediateDirectories:YES attributes:nil error:nil]; |
| 190 |
NSLog(@"installing %@ to %@...", srcPath, dstPath); |
| 191 |
|
| 192 |
struct archive *a; |
| 193 |
const char* archiveAbsPath = [srcPath cStringUsingEncoding:NSASCIIStringEncoding]; |
| 194 |
a = archive_read_new(); |
| 195 |
archive_read_support_format_all(a); |
| 196 |
|
| 197 |
struct archive *ext = archive_write_disk_new(); |
| 198 |
archive_write_disk_set_options(ext, 0); |
| 199 |
archive_write_disk_set_standard_lookup(ext); |
| 200 |
struct archive_entry *entry; |
| 201 |
|
| 202 |
@try { |
| 203 |
int r; |
| 204 |
if ((r = archive_read_open_file(a, archiveAbsPath, 10240))) { |
| 205 |
@throw ([NSException exceptionWithName:@"ArchiveException" |
| 206 |
reason:[NSString stringWithFormat:@"Error opening archive (%i):%s", |
| 207 |
r, archive_error_string(a)] |
| 208 |
userInfo:nil]); |
| 209 |
} |
| 210 |
NSString* installName = nil; |
| 211 |
BOOL archiveHasRootDir = YES; |
| 212 |
unsigned long long progressSize = 0; |
| 213 |
for (;;) { |
| 214 |
r = archive_read_next_header(a, &entry); |
| 215 |
if (r == ARCHIVE_EOF) break; |
| 216 |
NSString* tmp = [NSString stringWithFormat:@"%s", archive_entry_pathname(entry)]; |
| 217 |
archive_entry_set_pathname(entry, [[dstPath stringByAppendingString:tmp] cStringUsingEncoding:NSASCIIStringEncoding]); |
| 218 |
// Mac OS X has an annoying thing where it embeds crap in the archive. skip it. |
| 219 |
if ([tmp rangeOfString:@"__MACOSX/"].location != NSNotFound) continue; |
| 220 |
if (!installName) { |
| 221 |
if ([tmp rangeOfString:@"/"].location != NSNotFound) { |
| 222 |
installName = tmp; |
| 223 |
} else { |
| 224 |
archiveHasRootDir = NO; |
| 225 |
} |
| 226 |
} else if ([tmp rangeOfString:installName].location != 0) { |
| 227 |
archiveHasRootDir = NO; |
| 228 |
} |
| 229 |
|
| 230 |
if (r != ARCHIVE_OK) { |
| 231 |
NSLog(@"error reading archive (%i):%s", r, archive_error_string(a)); |
| 232 |
break; |
| 233 |
} |
| 234 |
|
| 235 |
r = archive_write_header(ext, entry); |
| 236 |
if (r != ARCHIVE_OK) |
| 237 |
NSLog(@"%s", archive_error_string(a)); |
| 238 |
else { |
| 239 |
int status; |
| 240 |
const void *buff; |
| 241 |
size_t size; |
| 242 |
off_t offset; |
| 243 |
|
| 244 |
for (;;) { |
| 245 |
status = archive_read_data_block(a, &buff, &size, &offset); |
| 246 |
if (status == ARCHIVE_EOF) break; |
| 247 |
if (status != ARCHIVE_OK) { |
| 248 |
NSLog(@"%s", archive_error_string(a)); |
| 249 |
break; |
| 250 |
} |
| 251 |
progressSize += size; |
| 252 |
status = archive_write_data_block(ext, buff, size, offset); |
| 253 |
if (status != ARCHIVE_OK) { |
| 254 |
NSLog(@"%s", archive_error_string(a)); |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
dispatch_async(dispatch_get_main_queue(), ^{ |
| 260 |
pv.progress = (float)((double)progressSize / (double)totalBytes); |
| 261 |
}); |
| 262 |
} |
| 263 |
} |
| 264 |
// result from a break above |
| 265 |
if (r == ARCHIVE_FATAL) { |
| 266 |
@throw ([NSException exceptionWithName:@"Archive Error" |
| 267 |
reason:[NSString stringWithFormat:@"Fatal error reading archive:%s", archive_error_string(a)] |
| 268 |
userInfo:nil]); |
| 269 |
} |
| 270 |
|
| 271 |
installName = [installName lastPathComponent]; |
| 272 |
NSString* gamePath = [NSString stringWithFormat:@"Library/%@/%@/", [archivePath pathExtension], installName]; |
| 273 |
// delete anything at gamePath. our install overwrites existing data. |
| 274 |
[fm removeItemAtPath:gamePath error:nil]; |
| 275 |
if (archiveHasRootDir) { |
| 276 |
[fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@/", dstPath, installName] |
| 277 |
toPath:gamePath error:nil]; |
| 278 |
[fm removeItemAtPath:dstPath error:nil]; |
| 279 |
} else { |
| 280 |
// simply rename the tmp dir to archivePath |
| 281 |
installName = [archivePath stringByDeletingPathExtension]; |
| 282 |
[fm moveItemAtPath:dstPath toPath:gamePath error:nil]; |
| 283 |
} |
| 284 |
|
| 285 |
NSString* savePath = [NSString stringWithFormat:@"Library/saves/%@/", [archivePath pathExtension]]; |
| 286 |
NSString* oldSavePath = [NSString stringWithFormat:@"%@save/", gamePath]; |
| 287 |
[fm createDirectoryAtPath:[NSString stringWithFormat:@"%@save/", savePath] withIntermediateDirectories:YES attributes:nil error:nil]; |
| 288 |
[fm createDirectoryAtPath:[NSString stringWithFormat:@"%@mpsave/", savePath] withIntermediateDirectories:YES attributes:nil error:nil]; |
| 289 |
[fm createDirectoryAtPath:[NSString stringWithFormat:@"Library/Caches/%@/", [archivePath pathExtension]] withIntermediateDirectories:YES attributes:nil error:nil]; |
| 290 |
|
| 291 |
NSLog(@"moving %@ to %@", oldSavePath, savePath); |
| 292 |
NSArray* saves = [fm contentsOfDirectoryAtPath:oldSavePath error:nil]; |
| 293 |
for (NSString* saveName in saves) { |
| 294 |
NSString* fullSavePath = [NSString stringWithFormat:@"%@save/%@", savePath, saveName]; |
| 295 |
[fm removeItemAtPath:fullSavePath error:nil]; |
| 296 |
[fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@", oldSavePath, saveName] toPath:fullSavePath error:nil]; |
| 297 |
NSLog(@"Moving save '%@' to %@", saveName, fullSavePath); |
| 298 |
} |
| 299 |
|
| 300 |
oldSavePath = [NSString stringWithFormat:@"%@/mpsave/", gamePath]; |
| 301 |
saves = [fm contentsOfDirectoryAtPath:oldSavePath error:nil]; |
| 302 |
|
| 303 |
for (NSString* saveName in saves) { |
| 304 |
NSString* fullSavePath = [NSString stringWithFormat:@"%@mpsave/%@", savePath, saveName]; |
| 305 |
[fm removeItemAtPath:fullSavePath error:nil]; |
| 306 |
[fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@", oldSavePath, saveName] toPath:fullSavePath error:nil]; |
| 307 |
NSLog(@"Moving mpsave '%@' to %@", saveName, fullSavePath); |
| 308 |
} |
| 309 |
|
| 310 |
NSString* newCfgPath = [NSString stringWithFormat:@"%@/%@.%@.cfg", docDir, installName, [archivePath pathExtension]]; |
| 311 |
|
| 312 |
NSLog(@"Automatically creating config for %@ installed at %@ running on %i", [archivePath pathExtension], gamePath, [[UIDevice currentDevice] userInterfaceIdiom]); |
| 313 |
NSMutableString* newConfig = [NSMutableString stringWithContentsOfFile:@"GemRB.cfg.newinstall" encoding:NSUTF8StringEncoding error:nil]; |
| 314 |
if ([fm fileExistsAtPath:newCfgPath]) { |
| 315 |
if (configIndexPath) { |
| 316 |
// close the config file before overwriting it. |
| 317 |
dispatch_async(dispatch_get_main_queue(), ^{ |
| 318 |
[controlTable deselectRowAtIndexPath:configIndexPath animated:YES]; |
| 319 |
[self tableView:controlTable didDeselectRowAtIndexPath:configIndexPath]; |
| 320 |
}); |
| 321 |
} |
| 322 |
// new data overwrites old data therefore new config should do the same. |
| 323 |
[fm removeItemAtPath:newCfgPath error:nil]; |
| 324 |
} |
| 325 |
if (newConfig) { |
| 326 |
[newConfig appendFormat:@"\nGameType = %@", [archivePath pathExtension]]; |
| 327 |
[newConfig appendFormat:@"\nGamePath = %@", gamePath]; |
| 328 |
// No need for CD paths |
| 329 |
[newConfig appendFormat:@"\nCachePath = Library/Caches/%@/", [archivePath pathExtension]]; |
| 330 |
[newConfig appendFormat:@"\nSavePath = %@", savePath]; |
| 331 |
|
| 332 |
[newConfig appendFormat:@"\nCustomFontPath = %@", docDir]; |
| 333 |
|
| 334 |
CGRect screenRect = [[UIScreen mainScreen] bounds]; |
| 335 |
[newConfig appendFormat:@"\nWidth = %d", (int)screenRect.size.width]; |
| 336 |
[newConfig appendFormat:@"\nHeight = %d", (int)screenRect.size.height]; |
| 337 |
|
| 338 |
NSError* err = nil; |
| 339 |
if (![newConfig writeToFile:newCfgPath atomically:YES encoding:NSUTF8StringEncoding error:&err]){ |
| 340 |
NSLog(@"Unable to write config file:%@\nError:%@", newCfgPath, [err localizedDescription]); |
| 341 |
} |
| 342 |
}else{ |
| 343 |
NSLog(@"Unable to open GemRB.cfg.newinstall"); |
| 344 |
} |
| 345 |
[pv removeFromSuperview]; |
| 346 |
[pv release]; |
| 347 |
// dont delete the archive from simulator |
| 348 |
#if TARGET_IPHONE_SIMULATOR == 0 |
| 349 |
[fm removeItemAtPath:srcPath error:nil]; |
| 350 |
#endif |
| 351 |
} |
| 352 |
@catch (NSException *exception) { |
| 353 |
// TODO: present an alert box |
| 354 |
NSLog(@"%@", exception); |
| 355 |
return NO; |
| 356 |
} |
| 357 |
@finally { |
| 358 |
archive_read_close(a); |
| 359 |
archive_read_finish(a); |
| 360 |
[libDir release]; |
| 361 |
[self reloadTableData]; |
| 362 |
[pool release]; |
| 363 |
} |
| 364 |
|
| 365 |
return YES; |
| 366 |
} |
| 367 |
|
| 368 |
#pragma mark TableViewDatasource |
| 369 |
|
| 370 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 371 |
{ |
| 372 |
static NSString *CellIdentifier = @"Cell"; |
| 373 |
|
| 374 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 375 |
if (cell == nil) { |
| 376 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
| 377 |
} |
| 378 |
switch (indexPath.section) { |
| 379 |
case TABLE_SEC_CONFIG: |
| 380 |
cell.textLabel.text = [configFiles count] ? |
| 381 |
[configFiles objectAtIndex:indexPath.row] : |
| 382 |
@"No config files found."; |
| 383 |
break; |
| 384 |
case TABLE_SEC_INSTALL: |
| 385 |
cell.textLabel.text = [installFiles count] ? |
| 386 |
[installFiles objectAtIndex:indexPath.row] : |
| 387 |
@"No game files found"; |
| 388 |
break; |
| 389 |
case TABLE_SEC_LOG: |
| 390 |
cell.textLabel.text = [logFiles objectAtIndex:indexPath.row]; |
| 391 |
break; |
| 392 |
} |
| 393 |
return cell; |
| 394 |
} |
| 395 |
|
| 396 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *) __unused tableView |
| 397 |
{ |
| 398 |
if (logFiles) return 3; |
| 399 |
return 2; |
| 400 |
} |
| 401 |
|
| 402 |
- (NSInteger)tableView:(UITableView *) __unused tableView numberOfRowsInSection:(NSInteger)section |
| 403 |
{ |
| 404 |
switch(section) |
| 405 |
{ |
| 406 |
case TABLE_SEC_CONFIG: |
| 407 |
return [configFiles count] ?: 1; |
| 408 |
case TABLE_SEC_INSTALL: |
| 409 |
return [installFiles count] ?: 1; |
| 410 |
case TABLE_SEC_LOG: |
| 411 |
return [logFiles count]; |
| 412 |
} |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
- (NSString *)tableView:(UITableView *) __unused tableView titleForHeaderInSection:(NSInteger)section |
| 417 |
{ |
| 418 |
switch(section) |
| 419 |
{ |
| 420 |
case TABLE_SEC_CONFIG: |
| 421 |
return @"Select a configuration file."; |
| 422 |
case TABLE_SEC_INSTALL: |
| 423 |
return @"Install a game."; |
| 424 |
case TABLE_SEC_LOG: |
| 425 |
if ([logFiles count]) |
| 426 |
return @"View a debug run log."; |
| 427 |
} |
| 428 |
return nil; |
| 429 |
} |
| 430 |
|
| 431 |
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath |
| 432 |
{ |
| 433 |
switch (indexPath.section) { |
| 434 |
case TABLE_SEC_CONFIG: |
| 435 |
[editor resignFirstResponder]; |
| 436 |
[tableView cellForRowAtIndexPath:configIndexPath].accessoryType = UITableViewCellAccessoryNone; |
| 437 |
playButton.enabled = NO; |
| 438 |
editorButton.enabled = NO; |
| 439 |
editor.editable = NO; |
| 440 |
editor.text = @""; |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
| 446 |
{ |
| 447 |
UITableViewCell* selectedCell = [tableView cellForRowAtIndexPath:indexPath]; |
| 448 |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 449 |
[self tableView:tableView didDeselectRowAtIndexPath:indexPath]; |
| 450 |
switch (indexPath.section) { |
| 451 |
case TABLE_SEC_CONFIG: |
| 452 |
if ([configFiles count] == 0) return; |
| 453 |
|
| 454 |
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 455 |
selectedCell.highlighted = NO; |
| 456 |
|
| 457 |
self.configIndexPath = indexPath; |
| 458 |
self.editor.text = [NSString stringWithContentsOfFile:[self selectedConfigPath] encoding:NSUTF8StringEncoding error:nil]; |
| 459 |
if (![editor becomeFirstResponder]){ |
| 460 |
NSLog(@"cannot set first responder"); |
| 461 |
} |
| 462 |
|
| 463 |
editor.editable = YES; |
| 464 |
editorButton.enabled = YES; |
| 465 |
playButton.enabled = YES; |
| 466 |
if (editorVC != nil){ |
| 467 |
editorVC.navigationItem.rightBarButtonItem = editorButton; |
| 468 |
[editorVC setTitle: [configFiles objectAtIndex:configIndexPath.row]]; |
| 469 |
[rootVC pushViewController:editorVC animated:YES]; |
| 470 |
} |
| 471 |
|
| 472 |
break; |
| 473 |
case TABLE_SEC_INSTALL: |
| 474 |
if ([installFiles count] == 0) return; |
| 475 |
[self performSelectorInBackground:@selector(installGame:) withObject:indexPath]; |
| 476 |
break; |
| 477 |
case TABLE_SEC_LOG: |
| 478 |
self.editor.text = [NSString stringWithContentsOfFile:[docDir stringByAppendingFormat:@"/%@", [logFiles objectAtIndex:indexPath.row]] encoding:NSUTF8StringEncoding error:nil]; |
| 479 |
if (editorVC != nil){ |
| 480 |
editorVC.navigationItem.rightBarButtonItem = editorButton; |
| 481 |
[editorVC setTitle: [configFiles objectAtIndex:configIndexPath.row]]; |
| 482 |
[rootVC pushViewController:editorVC animated:YES]; |
| 483 |
} |
| 484 |
break; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath |
| 489 |
{ |
| 490 |
if (editingStyle == UITableViewCellEditingStyleInsert) return; |
| 491 |
|
| 492 |
NSFileManager* fm = [NSFileManager defaultManager]; |
| 493 |
switch (indexPath.section) { |
| 494 |
case TABLE_SEC_CONFIG: |
| 495 |
// check if the config file we are deleting is currently selected and deselect it |
| 496 |
if (tableView.indexPathForSelectedRow.row == indexPath.row) { |
| 497 |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 498 |
[self tableView:tableView didDeselectRowAtIndexPath:indexPath]; |
| 499 |
} |
| 500 |
if (configIndexPath.row == indexPath.row) { |
| 501 |
[controlTable deselectRowAtIndexPath:configIndexPath animated:YES]; |
| 502 |
[self tableView:controlTable didDeselectRowAtIndexPath:configIndexPath]; |
| 503 |
} |
| 504 |
[fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@", docDir, [configFiles objectAtIndex:indexPath.row]] error:nil]; |
| 505 |
break; |
| 506 |
case TABLE_SEC_INSTALL: |
| 507 |
[fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@", docDir, [installFiles objectAtIndex:indexPath.row]] error:nil]; |
| 508 |
break; |
| 509 |
case TABLE_SEC_LOG: |
| 510 |
[fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@", docDir, [logFiles objectAtIndex:indexPath.row]] error:nil]; |
| 511 |
break; |
| 512 |
} |
| 513 |
[self reloadTableData]; |
| 514 |
} |
| 515 |
|
| 516 |
- (BOOL)tableView:(UITableView *) __unused tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath |
| 517 |
{ |
| 518 |
switch (indexPath.section) { |
| 519 |
case TABLE_SEC_CONFIG: |
| 520 |
return (BOOL)[configFiles count]; |
| 521 |
case TABLE_SEC_INSTALL: |
| 522 |
return (BOOL)[installFiles count]; |
| 523 |
case TABLE_SEC_LOG: |
| 524 |
return (BOOL)[logFiles count]; |
| 525 |
} |
| 526 |
return NO; |
| 527 |
} |
| 528 |
|
| 529 |
- (IBAction)saveConfig:(id) __unused sender |
| 530 |
{ |
| 531 |
if (editorVC)[rootVC popViewControllerAnimated:YES]; |
| 532 |
if (editor.editable) |
| 533 |
[editor.text writeToFile:[self selectedConfigPath] atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
| 534 |
} |
| 535 |
|
| 536 |
- (IBAction)launchGEM:(id) __unused sender |
| 537 |
{ |
| 538 |
/* |
| 539 |
Ensure a few things about starting GemRB: |
| 540 |
1. ensure GemRB_main is called from the main thread. |
| 541 |
2. ensure we won't return here since this will be deallocated |
| 542 |
3. ensure we have dropped back to the main runloop so autoreleased objects are disposed of |
| 543 |
bonus: the call stack is significantly reduced this way |
| 544 |
*/ |
| 545 |
|
| 546 |
// SDL disables the event pump after SDL_main returns because normally SDL_main houses the application event loop |
| 547 |
// GemRB is diffrent and our wrapper interface makes that impossible. |
| 548 |
SDL_iPhoneSetEventPump(SDL_TRUE); |
| 549 |
|
| 550 |
SDL_SetHintWithPriority(SDL_HINT_ORIENTATIONS, "LandscapeRight LandscapeLeft", SDL_HINT_OVERRIDE); |
| 551 |
SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, "opengles", SDL_HINT_OVERRIDE); |
| 552 |
|
| 553 |
// Note: use NSRunLoop over NSObject performSelector! |
| 554 |
NSArray* modes = [NSArray arrayWithObject:NSDefaultRunLoopMode]; |
| 555 |
[[NSRunLoop mainRunLoop] performSelector:@selector(setupComplete:) |
| 556 |
target:[self delegate] |
| 557 |
argument:[self selectedConfigPath] |
| 558 |
order:0 modes:modes]; |
| 559 |
} |
| 560 |
|
| 561 |
- (void)textViewDidBeginEditing:(UITextView *)textView |
| 562 |
{ |
| 563 |
[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor]; |
| 564 |
[UIView animateWithDuration:0.5 animations:^{ |
| 565 |
CGRect frame = textView.frame; |
| 566 |
frame.size.height -= 300; |
| 567 |
textView.frame = frame; |
| 568 |
}]; |
| 569 |
} |
| 570 |
|
| 571 |
- (void)textViewDidEndEditing:(UITextView *)textView |
| 572 |
{ |
| 573 |
[UIView animateWithDuration:0.25 animations:^{ |
| 574 |
CGRect frame = textView.frame; |
| 575 |
frame.size.height += 300; |
| 576 |
textView.frame = frame; |
| 577 |
}]; |
| 578 |
[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor blackColor]; |
| 579 |
} |
| 580 |
|
| 581 |
@end |