| 1 |
/* iosstart.m: iOS-specific interface code for Glulx. (Objective C) |
| 2 |
Designed by Andrew Plotkin <[email protected]> |
| 3 |
http://eblong.com/zarf/glulx/index.html |
| 4 |
*/ |
| 5 |
|
| 6 |
#import "TerpGlkViewController.h" |
| 7 |
#import "TerpGlkDelegate.h" |
| 8 |
#import "GlkLibrary.h" |
| 9 |
#import "GlkAppWrapper.h" |
| 10 |
#import "GlkWindow.h" |
| 11 |
#import "GlkStream.h" |
| 12 |
#import "GlkFileRef.h" |
| 13 |
#import "GlkUtilTypes.h" |
| 14 |
#import "Geometry.h" |
| 15 |
|
| 16 |
#include "glk.h" /* This comes with the IosGlk library. */ |
| 17 |
#include "glulxe.h" |
| 18 |
#include "iosstart.h" |
| 19 |
#include "iosglk_startup.h" /* This comes with the IosGlk library. */ |
| 20 |
|
| 21 |
static library_state_data library_state; /* used by the archive/unarchive hooks */ |
| 22 |
|
| 23 |
#if VM_DEBUGGER |
| 24 |
/* This header file may come with the Glk library. If it doesn't, comment |
| 25 |
out VM_DEBUGGER in glulxe.h -- you won't be able to use debugging. */ |
| 26 |
#include "gi_debug.h" |
| 27 |
#endif /* VM_DEBUGGER */ |
| 28 |
|
| 29 |
static void iosglk_game_start(void); |
| 30 |
static void iosglk_game_autorestore(void); |
| 31 |
static void iosglk_game_select(glui32 selector, glui32 arg0, glui32 arg1, glui32 arg2); |
| 32 |
static void stash_library_state(void); |
| 33 |
static void recover_library_state(void); |
| 34 |
static void free_library_state(void); |
| 35 |
static void iosglk_library_archive(NSCoder *encoder); |
| 36 |
static void iosglk_library_unarchive(NSCoder *decoder); |
| 37 |
|
| 38 |
/* This is only needed for autorestore. */ |
| 39 |
extern gidispatch_rock_t glulxe_classtable_register_existing(void *obj, glui32 objclass, glui32 dispid); |
| 40 |
|
| 41 |
|
| 42 |
static NSString *documents_dir() |
| 43 |
{ |
| 44 |
/* We use an old-fashioned way of locating the Documents directory. (The NSManager method for this is iOS 4.0 and later.) */ |
| 45 |
|
| 46 |
NSArray *dirlist = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 47 |
if (!dirlist || dirlist.count == 0) { |
| 48 |
NSLog(@"unable to locate Documents directory."); |
| 49 |
return nil; |
| 50 |
} |
| 51 |
|
| 52 |
return dirlist[0]; |
| 53 |
} |
| 54 |
|
| 55 |
/* Backtrack through the current opcode (at prevpc), and figure out whether its input arguments are on the stack or not. This will be important when setting up the saved VM state for restarting its opcode. |
| 56 |
|
| 57 |
The opmodes argument must be an array int[3]. Returns YES on success. |
| 58 |
*/ |
| 59 |
static int parse_partial_operand(int *opmodes) |
| 60 |
{ |
| 61 |
glui32 addr = prevpc; |
| 62 |
|
| 63 |
/* Fetch the opcode number. */ |
| 64 |
glui32 opcode = Mem1(addr); |
| 65 |
addr++; |
| 66 |
if (opcode & 0x80) { |
| 67 |
/* More than one-byte opcode. */ |
| 68 |
if (opcode & 0x40) { |
| 69 |
/* Four-byte opcode */ |
| 70 |
opcode &= 0x3F; |
| 71 |
opcode = (opcode << 8) | Mem1(addr); |
| 72 |
addr++; |
| 73 |
opcode = (opcode << 8) | Mem1(addr); |
| 74 |
addr++; |
| 75 |
opcode = (opcode << 8) | Mem1(addr); |
| 76 |
addr++; |
| 77 |
} |
| 78 |
else { |
| 79 |
/* Two-byte opcode */ |
| 80 |
opcode &= 0x7F; |
| 81 |
opcode = (opcode << 8) | Mem1(addr); |
| 82 |
addr++; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if (opcode != 0x130) { /* op_glk */ |
| 87 |
NSLog(@"iosglk_startup_code: parsed wrong opcode: %d", opcode); |
| 88 |
return NO; |
| 89 |
} |
| 90 |
|
| 91 |
/* @glk has operands LLS. */ |
| 92 |
opmodes[0] = Mem1(addr) & 0x0F; |
| 93 |
opmodes[1] = (Mem1(addr) >> 4) & 0x0F; |
| 94 |
opmodes[2] = Mem1(addr+1) & 0x0F; |
| 95 |
|
| 96 |
return YES; |
| 97 |
} |
| 98 |
|
| 99 |
/* We don't load in the game file here. Instead, we set a hook which glk_main() will call back to do that. Why? Because of the annoying restartability of the VM under iosglk; we may finish glk_main() and then have to call it again. |
| 100 |
*/ |
| 101 |
void iosglk_startup_code() |
| 102 |
{ |
| 103 |
set_library_start_hook(&iosglk_game_start); |
| 104 |
set_library_autorestore_hook(&iosglk_game_autorestore); |
| 105 |
set_library_select_hook(&iosglk_game_select); |
| 106 |
max_undo_level = 32; // allow 32 undo steps |
| 107 |
#ifdef IOSGLK_EXTEND_STARTUP_CODE |
| 108 |
IOSGLK_EXTEND_STARTUP_CODE |
| 109 |
#endif // IOSGLK_EXTEND_STARTUP_CODE |
| 110 |
} |
| 111 |
|
| 112 |
/* This is the library_start_hook, which will be called every time glk_main() begins. (VM thread) |
| 113 |
*/ |
| 114 |
static void iosglk_game_start() |
| 115 |
{ |
| 116 |
TerpGlkViewController *glkviewc = [TerpGlkViewController singleton]; |
| 117 |
NSString *pathname = glkviewc.terpDelegate.gamePath; |
| 118 |
NSLog(@"iosglk_startup_code: game path is %@", pathname); |
| 119 |
|
| 120 |
/* Retain this, because we're assigning it to a global. (It will look like a leak to XCode's leak-profiler.) */ |
| 121 |
gamefile = [[GlkStreamFile alloc] initWithMode:filemode_Read rock:1 unicode:NO textmode:NO dirname:@"." pathname:pathname]; |
| 122 |
|
| 123 |
/* Now we have to check to see if it's a Blorb file. */ |
| 124 |
int res; |
| 125 |
unsigned char buf[12]; |
| 126 |
|
| 127 |
glk_stream_set_position(gamefile, 0, seekmode_Start); |
| 128 |
res = glk_get_buffer_stream(gamefile, (char *)buf, 12); |
| 129 |
if (!res) { |
| 130 |
init_err = "The data in this stand-alone game is too short to read."; |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
if (buf[0] == 'G' && buf[1] == 'l' && buf[2] == 'u' && buf[3] == 'l') { |
| 135 |
locate_gamefile(FALSE); |
| 136 |
} |
| 137 |
else if (buf[0] == 'F' && buf[1] == 'O' && buf[2] == 'R' && buf[3] == 'M' |
| 138 |
&& buf[8] == 'I' && buf[9] == 'F' && buf[10] == 'R' && buf[11] == 'S') { |
| 139 |
locate_gamefile(TRUE); |
| 140 |
} |
| 141 |
else { |
| 142 |
init_err = "This is neither a Glulx game file nor a Blorb file which contains one."; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/* This is the library_autorestore_hook, which will be called from glk_main() between VM setup and the beginning of the execution loop. (VM thread) |
| 147 |
*/ |
| 148 |
static void iosglk_game_autorestore() |
| 149 |
{ |
| 150 |
GlkLibrary *library = [GlkLibrary singleton]; |
| 151 |
|
| 152 |
NSString *dirname = documents_dir(); |
| 153 |
if (!dirname) |
| 154 |
return; |
| 155 |
NSString *gamepath = [dirname stringByAppendingPathComponent:@"autosave.glksave"]; |
| 156 |
NSString *libpath = [dirname stringByAppendingPathComponent:@"autosave.plist"]; |
| 157 |
|
| 158 |
if (![library.filemanager fileExistsAtPath:gamepath]) |
| 159 |
return; |
| 160 |
if (![library.filemanager fileExistsAtPath:libpath]) |
| 161 |
return; |
| 162 |
|
| 163 |
|
| 164 |
library_state.active = NO; |
| 165 |
library_state.protectstart = 0; |
| 166 |
library_state.protectend = 0; |
| 167 |
library_state.iosys_mode = 0; |
| 168 |
library_state.iosys_rock = 0; |
| 169 |
library_state.stringtable = 0; |
| 170 |
library_state.gamefiletag = 0; |
| 171 |
library_state.accel_params = NULL; |
| 172 |
library_state.accel_funcs = NULL; |
| 173 |
library_state.id_map_list = NULL; |
| 174 |
GlkLibrary *newlib = nil; |
| 175 |
|
| 176 |
[GlkLibrary setExtraUnarchiveHook:iosglk_library_unarchive]; |
| 177 |
NSError *error = nil; |
| 178 |
@try { |
| 179 |
newlib = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[[GlkLibrary class], [NSString class], [NSMutableArray class], [NSMutableString class], [GlkWindow class], [GlkStream class], [GlkFileRef class], [NSValue class], [NSNumber class], [GlkStyledLine class], [GlkStyledString class], [GlkGridLine class], [Geometry class], [GlkObjIdEntry class]]] fromData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:libpath]] error:&error]; |
| 180 |
} |
| 181 |
@catch (NSException *ex) { |
| 182 |
// leave newlib as nil |
| 183 |
NSLog(@"Unable to restore autosave library: Exception %@ Error %@", ex, error); |
| 184 |
} |
| 185 |
[GlkLibrary setExtraUnarchiveHook:nil]; |
| 186 |
|
| 187 |
if (!newlib || !library_state.active) { |
| 188 |
/* Without a Glk state, there's no point in even trying the VM state. */ |
| 189 |
NSLog(@"library autorestore failed! Error: %@", error); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
int res; |
| 194 |
GlkStreamFile *savefile = [[GlkStreamFile alloc] initWithMode:filemode_Read rock:1 unicode:NO textmode:NO dirname:dirname pathname:gamepath]; |
| 195 |
res = perform_restore(savefile, TRUE); |
| 196 |
glk_stream_close(savefile, nil); |
| 197 |
savefile = nil; |
| 198 |
|
| 199 |
if (res) { |
| 200 |
NSLog(@"VM autorestore failed!"); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
pop_callstub(0); |
| 205 |
|
| 206 |
/* Annoyingly, the updateFromLibrary we're about to do will close the currently-open gamefile. We'll recover it immediately, in recover_library_state(). */ |
| 207 |
gamefile = nil; |
| 208 |
|
| 209 |
[library updateFromLibrary:newlib]; |
| 210 |
recover_library_state(); |
| 211 |
NSLog(@"autorestore succeeded."); |
| 212 |
|
| 213 |
free_library_state(); |
| 214 |
} |
| 215 |
|
| 216 |
/* This is the library_select_hook, which will be called every time glk_select() is invoked. (VM thread) |
| 217 |
*/ |
| 218 |
static void iosglk_game_select(glui32 selector, glui32 arg0, glui32 arg1, glui32 arg2) |
| 219 |
{ |
| 220 |
glui32 lasteventtype = [GlkAppWrapper singleton].lasteventtype; |
| 221 |
//NSLog(@"### game called select, last event was %d", lasteventtype); |
| 222 |
|
| 223 |
/* Do not autosave if we've just started up, or if the last event was a rearrange event. (We get rearranges in clusters, and they don't change anything interesting anyhow.) */ |
| 224 |
if (lasteventtype == -1 || lasteventtype == evtype_Arrange) |
| 225 |
return; |
| 226 |
|
| 227 |
iosglk_do_autosave(selector, arg0, arg1, arg2); |
| 228 |
} |
| 229 |
|
| 230 |
void iosglk_do_autosave(glui32 selector, glui32 arg0, glui32 arg1, glui32 arg2) |
| 231 |
{ |
| 232 |
GlkLibrary *library = [GlkLibrary singleton]; |
| 233 |
//NSLog(@"### attempting autosave (pc = %x, eventaddr = %x, stack = %d before stub)", prevpc, eventaddr, stackptr); |
| 234 |
|
| 235 |
/* When the save file is autorestored, the VM will restart the @glk opcode. That means that the Glk argument (the event structure address) must be waiting on the stack. Possibly also the @glk opcode's operands -- these might or might not have come off the stack. */ |
| 236 |
|
| 237 |
glui32 oldmode, oldrock; |
| 238 |
|
| 239 |
int res; |
| 240 |
int opmodes[3]; |
| 241 |
res = parse_partial_operand(opmodes); |
| 242 |
if (!res) |
| 243 |
return; |
| 244 |
|
| 245 |
NSString *dirname = documents_dir(); |
| 246 |
if (!dirname) |
| 247 |
return; |
| 248 |
NSString *tmpgamepath = [dirname stringByAppendingPathComponent:@"autosave-tmp.glksave"]; |
| 249 |
|
| 250 |
GlkStreamFile *savefile = [[GlkStreamFile alloc] initWithMode:filemode_Write rock:1 unicode:NO textmode:NO dirname:dirname pathname:tmpgamepath]; |
| 251 |
|
| 252 |
/* Push all the necessary arguments for the @glk opcode. */ |
| 253 |
glui32 origstackptr = stackptr; |
| 254 |
int stackvals = 0; |
| 255 |
if (selector == 0x00C0) { |
| 256 |
/* The event structure address: */ |
| 257 |
stackvals++; |
| 258 |
if (stackptr+4 > stacksize) |
| 259 |
fatal_error("Stack overflow in autosave callstub."); |
| 260 |
StkW4(stackptr, arg0); /* eventaddr */ |
| 261 |
stackptr += 4; |
| 262 |
} |
| 263 |
else if (selector == 0x0062) { |
| 264 |
/* The three arguments: */ |
| 265 |
stackvals++; |
| 266 |
if (stackptr+4 > stacksize) |
| 267 |
fatal_error("Stack overflow in autosave callstub."); |
| 268 |
StkW4(stackptr, arg2); /* rock */ |
| 269 |
stackptr += 4; |
| 270 |
stackvals++; |
| 271 |
if (stackptr+4 > stacksize) |
| 272 |
fatal_error("Stack overflow in autosave callstub."); |
| 273 |
StkW4(stackptr, arg1); /* fmode */ |
| 274 |
stackptr += 4; |
| 275 |
stackvals++; |
| 276 |
if (stackptr+4 > stacksize) |
| 277 |
fatal_error("Stack overflow in autosave callstub."); |
| 278 |
StkW4(stackptr, arg0); /* usage */ |
| 279 |
stackptr += 4; |
| 280 |
} |
| 281 |
else { |
| 282 |
fatal_error("Autosave with unrecognized glk selector."); |
| 283 |
} |
| 284 |
|
| 285 |
if (opmodes[1] == 8) { |
| 286 |
/* The number of Glk arguments (1): */ |
| 287 |
stackvals++; |
| 288 |
if (stackptr+4 > stacksize) |
| 289 |
fatal_error("Stack overflow in autosave callstub."); |
| 290 |
StkW4(stackptr, 1); |
| 291 |
stackptr += 4; |
| 292 |
} |
| 293 |
if (opmodes[0] == 8) { |
| 294 |
/* The Glk call selector (0x00C0): */ |
| 295 |
stackvals++; |
| 296 |
if (stackptr+4 > stacksize) |
| 297 |
fatal_error("Stack overflow in autosave callstub."); |
| 298 |
StkW4(stackptr, selector); |
| 299 |
stackptr += 4; |
| 300 |
} |
| 301 |
/* Push a temporary callstub which contains the *last* PC -- the address of the @glk(select) invocation. */ |
| 302 |
if (stackptr+16 > stacksize) |
| 303 |
fatal_error("Stack overflow in autosave callstub."); |
| 304 |
StkW4(stackptr+0, 0); |
| 305 |
StkW4(stackptr+4, 0); |
| 306 |
StkW4(stackptr+8, prevpc); |
| 307 |
StkW4(stackptr+12, frameptr); |
| 308 |
stackptr += 16; |
| 309 |
|
| 310 |
/* temporarily set I/O system to GLK, which perform_save() needs */ |
| 311 |
stream_get_iosys(&oldmode, &oldrock); |
| 312 |
stream_set_iosys(2, 0); |
| 313 |
|
| 314 |
res = perform_save(savefile); |
| 315 |
|
| 316 |
stream_set_iosys(oldmode, oldrock); |
| 317 |
|
| 318 |
stackptr -= 16; // discard the temporary callstub |
| 319 |
stackptr -= 4 * stackvals; // discard the temporary arguments |
| 320 |
if (origstackptr != stackptr) |
| 321 |
fatal_error("Stack pointer mismatch in autosave"); |
| 322 |
|
| 323 |
glk_stream_close(savefile, nil); |
| 324 |
savefile = nil; |
| 325 |
|
| 326 |
if (res) { |
| 327 |
NSLog(@"VM autosave failed!"); |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
library_state.active = NO; |
| 332 |
library_state.protectstart = 0; |
| 333 |
library_state.protectend = 0; |
| 334 |
library_state.iosys_mode = 0; |
| 335 |
library_state.iosys_rock = 0; |
| 336 |
library_state.stringtable = 0; |
| 337 |
library_state.gamefiletag = 0; |
| 338 |
library_state.accel_params = NULL; |
| 339 |
library_state.accel_funcs = NULL; |
| 340 |
library_state.id_map_list = NULL; |
| 341 |
|
| 342 |
stash_library_state(); |
| 343 |
/* The iosglk_library_archive hook will write out the contents of library_state. */ |
| 344 |
|
| 345 |
NSString *finallibpath = [dirname stringByAppendingPathComponent:@"autosave.plist"]; |
| 346 |
[GlkLibrary setExtraArchiveHook:iosglk_library_archive]; |
| 347 |
NSError *error = nil; |
| 348 |
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:library requiringSecureCoding:NO error:&error]; |
| 349 |
BOOL result = [data writeToURL:[NSURL fileURLWithPath:finallibpath] options:NSDataWritingAtomic error:&error]; |
| 350 |
res = (result == YES); |
| 351 |
[GlkLibrary setExtraArchiveHook:nil]; |
| 352 |
free_library_state(); |
| 353 |
|
| 354 |
if (!res) { |
| 355 |
NSLog(@"library serialize failed! Error: %@", error); |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
NSString *finalgamepath = [dirname stringByAppendingPathComponent:@"autosave.glksave"]; |
| 360 |
|
| 361 |
/* This is not really atomic, but we're already past the serious failure modes. */ |
| 362 |
[library.filemanager removeItemAtPath:finalgamepath error:nil]; |
| 363 |
error = nil; |
| 364 |
res = [library.filemanager moveItemAtPath:tmpgamepath toPath:finalgamepath error:&error]; |
| 365 |
if (!res) { |
| 366 |
NSLog(@"could not move game autosave to final position! Error: %@", error); |
| 367 |
return; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* Delete an autosaved game, if one exists. |
| 372 |
*/ |
| 373 |
void iosglk_clear_autosave() |
| 374 |
{ |
| 375 |
GlkLibrary *library = [GlkLibrary singleton]; |
| 376 |
|
| 377 |
NSString *dirname = documents_dir(); |
| 378 |
if (!dirname) |
| 379 |
return; |
| 380 |
|
| 381 |
NSString *finalgamepath = [dirname stringByAppendingPathComponent:@"autosave.glksave"]; |
| 382 |
NSString *finallibpath = [dirname stringByAppendingPathComponent:@"autosave.plist"]; |
| 383 |
|
| 384 |
[library.filemanager removeItemAtPath:finallibpath error:nil]; |
| 385 |
[library.filemanager removeItemAtPath:finalgamepath error:nil]; |
| 386 |
} |
| 387 |
|
| 388 |
/* Utility function used by stash_library_state. Assumes that library_state.accel_funcs is a valid NSMutableArray. */ |
| 389 |
static void stash_one_accel_func(glui32 index, glui32 addr) |
| 390 |
{ |
| 391 |
NSMutableArray *arr = (NSMutableArray *)library_state.accel_funcs; |
| 392 |
|
| 393 |
GlulxAccelEntry *ent = [[GlulxAccelEntry alloc] initWithIndex:index addr:addr]; |
| 394 |
[arr addObject:ent]; |
| 395 |
} |
| 396 |
|
| 397 |
/* Copy extra chunks of the VM state into the (static) library_state object. This is information needed by autosave, but not included in the regular save process. |
| 398 |
*/ |
| 399 |
static void stash_library_state() |
| 400 |
{ |
| 401 |
library_state.active = YES; |
| 402 |
|
| 403 |
library_state.protectstart = protectstart; |
| 404 |
library_state.protectend = protectend; |
| 405 |
stream_get_iosys(&library_state.iosys_mode, &library_state.iosys_rock); |
| 406 |
library_state.stringtable = stream_get_table(); |
| 407 |
|
| 408 |
glui32 count = accel_get_param_count(); |
| 409 |
NSMutableArray *accel_params = [NSMutableArray arrayWithCapacity:count]; |
| 410 |
library_state.accel_params = accel_params; |
| 411 |
for (int ix=0; ix<count; ix++) { |
| 412 |
glui32 param = accel_get_param(ix); |
| 413 |
[accel_params addObject:@(param)]; |
| 414 |
} |
| 415 |
|
| 416 |
NSMutableArray *accel_funcs = [NSMutableArray arrayWithCapacity:8]; |
| 417 |
library_state.accel_funcs = accel_funcs; |
| 418 |
accel_iterate_funcs(&stash_one_accel_func); |
| 419 |
|
| 420 |
if (gamefile) |
| 421 |
library_state.gamefiletag = gamefile.tag.intValue; |
| 422 |
|
| 423 |
GlkLibrary *library = [GlkLibrary singleton]; |
| 424 |
NSMutableArray *id_map_list = [NSMutableArray arrayWithCapacity:4]; |
| 425 |
library_state.id_map_list = id_map_list; |
| 426 |
|
| 427 |
for (GlkWindow *win in library.windows) { |
| 428 |
GlkObjIdEntry *ent = [[GlkObjIdEntry alloc] initWithClass:gidisp_Class_Window tag:win.tag id:find_id_for_window(win)]; |
| 429 |
[id_map_list addObject:ent]; |
| 430 |
} |
| 431 |
for (GlkStream *str in library.streams) { |
| 432 |
GlkObjIdEntry *ent = [[GlkObjIdEntry alloc] initWithClass:gidisp_Class_Stream tag:str.tag id:find_id_for_stream(str)]; |
| 433 |
[id_map_list addObject:ent]; |
| 434 |
} |
| 435 |
for (GlkFileRef *fref in library.filerefs) { |
| 436 |
GlkObjIdEntry *ent = [[GlkObjIdEntry alloc] initWithClass:gidisp_Class_Fileref tag:fref.tag id:find_id_for_fileref(fref)]; |
| 437 |
[id_map_list addObject:ent]; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/* Copy chunks of VM state out of the (static) library_state object. |
| 442 |
*/ |
| 443 |
static void recover_library_state() |
| 444 |
{ |
| 445 |
if (library_state.active) { |
| 446 |
protectstart = library_state.protectstart; |
| 447 |
protectend = library_state.protectend; |
| 448 |
stream_set_iosys(library_state.iosys_mode, library_state.iosys_rock); |
| 449 |
stream_set_table(library_state.stringtable); |
| 450 |
|
| 451 |
if (library_state.accel_params) { |
| 452 |
for (int ix=0; ix<library_state.accel_params.count; ix++) { |
| 453 |
NSNumber *num = library_state.accel_params[ix]; |
| 454 |
glui32 param = num.unsignedIntValue; |
| 455 |
accel_set_param(ix, param); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
if (library_state.accel_funcs) { |
| 460 |
for (GlulxAccelEntry *entry in library_state.accel_funcs) { |
| 461 |
accel_set_func(entry.index, entry.addr); |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
GlkLibrary *library = [GlkLibrary singleton]; |
| 467 |
if (library_state.id_map_list) { |
| 468 |
for (GlkObjIdEntry *ent in library_state.id_map_list) { |
| 469 |
switch (ent.objclass) { |
| 470 |
case gidisp_Class_Window: { |
| 471 |
GlkWindow *win = [library windowForIntTag:ent.tag]; |
| 472 |
if (!win) { |
| 473 |
NSLog(@"### Could not find window for tag %d", ent.tag); |
| 474 |
continue; |
| 475 |
} |
| 476 |
win.disprock = glulxe_classtable_register_existing((__bridge void *)(win), ent.objclass, ent.dispid); |
| 477 |
} |
| 478 |
break; |
| 479 |
case gidisp_Class_Stream: { |
| 480 |
GlkStream *str = [library streamForIntTag:ent.tag]; |
| 481 |
if (!str) { |
| 482 |
NSLog(@"### Could not find stream for tag %d", ent.tag); |
| 483 |
continue; |
| 484 |
} |
| 485 |
str.disprock = glulxe_classtable_register_existing((__bridge void *)(str), ent.objclass, ent.dispid); |
| 486 |
} |
| 487 |
break; |
| 488 |
case gidisp_Class_Fileref: { |
| 489 |
GlkFileRef *fref = [library filerefForIntTag:ent.tag]; |
| 490 |
if (!fref) { |
| 491 |
NSLog(@"### Could not find fileref for tag %d", ent.tag); |
| 492 |
continue; |
| 493 |
} |
| 494 |
fref.disprock = glulxe_classtable_register_existing((__bridge void *)(fref), ent.objclass, ent.dispid); |
| 495 |
} |
| 496 |
break; |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
if (library_state.gamefiletag) |
| 502 |
gamefile = [library streamForIntTag:library_state.gamefiletag]; |
| 503 |
} |
| 504 |
|
| 505 |
static void free_library_state() |
| 506 |
{ |
| 507 |
library_state.active = false; |
| 508 |
|
| 509 |
if (library_state.accel_params) { |
| 510 |
library_state.accel_params = nil; |
| 511 |
} |
| 512 |
if (library_state.accel_funcs) { |
| 513 |
library_state.accel_funcs = nil; |
| 514 |
} |
| 515 |
if (library_state.id_map_list) { |
| 516 |
library_state.id_map_list = nil; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
static void iosglk_library_archive(NSCoder *encoder) |
| 521 |
{ |
| 522 |
if (library_state.active) { |
| 523 |
[encoder encodeBool:YES forKey:@"glulx_library_state"]; |
| 524 |
[encoder encodeInt32:library_state.protectstart forKey:@"glulx_protectstart"]; |
| 525 |
[encoder encodeInt32:library_state.protectend forKey:@"glulx_protectend"]; |
| 526 |
[encoder encodeInt32:library_state.iosys_mode forKey:@"glulx_iosys_mode"]; |
| 527 |
[encoder encodeInt32:library_state.iosys_rock forKey:@"glulx_iosys_rock"]; |
| 528 |
[encoder encodeInt32:library_state.stringtable forKey:@"glulx_stringtable"]; |
| 529 |
if (library_state.accel_params) |
| 530 |
[encoder encodeObject:library_state.accel_params forKey:@"glulx_accel_params"]; |
| 531 |
if (library_state.accel_funcs) |
| 532 |
[encoder encodeObject:library_state.accel_funcs forKey:@"glulx_accel_funcs"]; |
| 533 |
[encoder encodeInt32:library_state.gamefiletag forKey:@"glulx_gamefiletag"]; |
| 534 |
if (library_state.id_map_list) |
| 535 |
[encoder encodeObject:library_state.id_map_list forKey:@"glulx_id_map_list"]; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
static void iosglk_library_unarchive(NSCoder *decoder) |
| 540 |
{ |
| 541 |
NSArray *arr; |
| 542 |
|
| 543 |
if ([decoder decodeBoolForKey:@"glulx_library_state"]) { |
| 544 |
library_state.active = true; |
| 545 |
library_state.protectstart = [decoder decodeInt32ForKey:@"glulx_protectstart"]; |
| 546 |
library_state.protectend = [decoder decodeInt32ForKey:@"glulx_protectend"]; |
| 547 |
library_state.iosys_mode = [decoder decodeInt32ForKey:@"glulx_iosys_mode"]; |
| 548 |
library_state.iosys_rock = [decoder decodeInt32ForKey:@"glulx_iosys_rock"]; |
| 549 |
library_state.stringtable = [decoder decodeInt32ForKey:@"glulx_stringtable"]; |
| 550 |
arr = [decoder decodeObjectForKey:@"glulx_accel_params"]; |
| 551 |
library_state.accel_params = arr; |
| 552 |
arr = [decoder decodeObjectForKey:@"glulx_accel_funcs"]; |
| 553 |
library_state.accel_funcs = arr; |
| 554 |
library_state.gamefiletag = [decoder decodeInt32ForKey:@"glulx_gamefiletag"]; |
| 555 |
arr = [decoder decodeObjectForKey:@"glulx_id_map_list"]; |
| 556 |
library_state.id_map_list = arr; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
int iosglk_can_restart_cleanly() |
| 561 |
{ |
| 562 |
return vm_exited_cleanly; |
| 563 |
} |
| 564 |
|
| 565 |
void iosglk_shut_down_process() |
| 566 |
{ |
| 567 |
/* Yes, we really do want to exit the app here. A fatal error has occurred at the interpreter level, so we can't restart it cleanly. The user has either hit a "goodbye" dialog button or the Home button; either way, it's time for suicide. */ |
| 568 |
NSLog(@"iosglk_shut_down_process: goodbye!"); |
| 569 |
exit(1); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
/* GlkObjIdEntry: A simple data class which stores the mapping of a GlkLibrary object (window, stream, etc) to its Glulx-VM ID number. */ |
| 574 |
|
| 575 |
@implementation GlkObjIdEntry |
| 576 |
|
| 577 |
+ (BOOL)supportsSecureCoding { |
| 578 |
return YES; |
| 579 |
} |
| 580 |
|
| 581 |
- (instancetype) initWithClass:(int)objclassval tag:(NSNumber *)tagref id:(glui32)dispidval |
| 582 |
{ |
| 583 |
self = [super init]; |
| 584 |
|
| 585 |
if (self) { |
| 586 |
objclass = objclassval; |
| 587 |
tag = tagref.intValue; |
| 588 |
dispid = dispidval; |
| 589 |
} |
| 590 |
|
| 591 |
return self; |
| 592 |
} |
| 593 |
|
| 594 |
- (instancetype) initWithCoder:(NSCoder *)decoder |
| 595 |
{ |
| 596 |
self = [super init]; |
| 597 |
if (self) { |
| 598 |
objclass = [decoder decodeInt32ForKey:@"objclass"]; |
| 599 |
tag = [decoder decodeInt32ForKey:@"tag"]; |
| 600 |
dispid = [decoder decodeInt32ForKey:@"dispid"]; |
| 601 |
} |
| 602 |
return self; |
| 603 |
} |
| 604 |
|
| 605 |
- (void) encodeWithCoder:(NSCoder *)encoder |
| 606 |
{ |
| 607 |
[encoder encodeInt32:objclass forKey:@"objclass"]; |
| 608 |
[encoder encodeInt32:tag forKey:@"tag"]; |
| 609 |
[encoder encodeInt32:dispid forKey:@"dispid"]; |
| 610 |
} |
| 611 |
|
| 612 |
- (glui32) objclass { return objclass; } |
| 613 |
- (glui32) tag { return tag; } |
| 614 |
- (glui32) dispid { return dispid; } |
| 615 |
|
| 616 |
@end |
| 617 |
|
| 618 |
|
| 619 |
/* GlulxAccelEntry: A simple data class which stores an accelerated-function table entry. */ |
| 620 |
|
| 621 |
@implementation GlulxAccelEntry |
| 622 |
|
| 623 |
+ (BOOL)supportsSecureCoding { |
| 624 |
return YES; |
| 625 |
} |
| 626 |
|
| 627 |
- (instancetype) initWithIndex:(glui32)indexval addr:(glui32)addrval |
| 628 |
{ |
| 629 |
self = [super init]; |
| 630 |
|
| 631 |
if (self) { |
| 632 |
index = indexval; |
| 633 |
addr = addrval; |
| 634 |
} |
| 635 |
|
| 636 |
return self; |
| 637 |
} |
| 638 |
|
| 639 |
- (instancetype) initWithCoder:(NSCoder *)decoder |
| 640 |
{ |
| 641 |
self = [super init]; |
| 642 |
if (self) { |
| 643 |
index = [decoder decodeInt32ForKey:@"index"]; |
| 644 |
addr = [decoder decodeInt32ForKey:@"addr"]; |
| 645 |
} |
| 646 |
return self; |
| 647 |
} |
| 648 |
|
| 649 |
- (void) encodeWithCoder:(NSCoder *)encoder |
| 650 |
{ |
| 651 |
[encoder encodeInt32:index forKey:@"index"]; |
| 652 |
[encoder encodeInt32:addr forKey:@"addr"]; |
| 653 |
} |
| 654 |
|
| 655 |
- (glui32) index { return index; } |
| 656 |
- (glui32) addr { return addr; } |
| 657 |
|
| 658 |
@end |
| 659 |
|
| 660 |
|