/* iosstart.m: iOS-specific interface code for Glulx. (Objective C) Designed by Andrew Plotkin http://eblong.com/zarf/glulx/index.html */ #import "TerpGlkViewController.h" #import "TerpGlkDelegate.h" #import "GlkLibrary.h" #import "GlkAppWrapper.h" #import "GlkWindow.h" #import "GlkStream.h" #import "GlkFileRef.h" #include "glk.h" /* This comes with the IosGlk library. */ #include "glulxe.h" #include "iosstart.h" #include "iosglk_startup.h" /* This comes with the IosGlk library. */ static library_state_data library_state; /* used by the archive/unarchive hooks */ static void iosglk_game_start(void); static void iosglk_game_autorestore(void); static void iosglk_game_select(glui32 eventaddr); static void stash_library_state(void); static void recover_library_state(void); static void free_library_state(void); static void iosglk_library_archive(NSCoder *encoder); static void iosglk_library_unarchive(NSCoder *decoder); /* This is only needed for autorestore. */ extern gidispatch_rock_t glulxe_classtable_register_existing(void *obj, glui32 objclass, glui32 dispid); static NSString *documents_dir() { /* We use an old-fashioned way of locating the Documents directory. (The NSManager method for this is iOS 4.0 and later.) */ NSArray *dirlist = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); if (!dirlist || [dirlist count] == 0) { NSLog(@"unable to locate Documents directory."); return nil; } return [dirlist objectAtIndex:0]; } /* 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. The opmodes argument must be an array int[3]. Returns YES on success. */ static int parse_partial_operand(int *opmodes) { glui32 addr = prevpc; /* Fetch the opcode number. */ glui32 opcode = Mem1(addr); addr++; if (opcode & 0x80) { /* More than one-byte opcode. */ if (opcode & 0x40) { /* Four-byte opcode */ opcode &= 0x3F; opcode = (opcode << 8) | Mem1(addr); addr++; opcode = (opcode << 8) | Mem1(addr); addr++; opcode = (opcode << 8) | Mem1(addr); addr++; } else { /* Two-byte opcode */ opcode &= 0x7F; opcode = (opcode << 8) | Mem1(addr); addr++; } } if (opcode != 0x130) { /* op_glk */ NSLog(@"iosglk_startup_code: parsed wrong opcode: %d", opcode); return NO; } /* @glk has operands LLS. */ opmodes[0] = Mem1(addr) & 0x0F; opmodes[1] = (Mem1(addr) >> 4) & 0x0F; opmodes[2] = Mem1(addr+1) & 0x0F; return YES; } /* 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. */ void iosglk_startup_code() { set_library_start_hook(&iosglk_game_start); set_library_autorestore_hook(&iosglk_game_autorestore); set_library_select_hook(&iosglk_game_select); max_undo_level = 32; // allow 32 undo steps #ifdef IOSGLK_EXTEND_STARTUP_CODE IOSGLK_EXTEND_STARTUP_CODE #endif // IOSGLK_EXTEND_STARTUP_CODE } /* This is the library_start_hook, which will be called every time glk_main() begins. (VM thread) */ static void iosglk_game_start() { TerpGlkViewController *glkviewc = [TerpGlkViewController singleton]; NSString *pathname = glkviewc.terpDelegate.gamePath; NSLog(@"iosglk_startup_code: game path is %@", pathname); /* Retain this, because we're assigning it to a global. (It will look like a leak to XCode's leak-profiler.) */ gamefile = [[GlkStreamFile alloc] initWithMode:filemode_Read rock:1 unicode:NO textmode:NO dirname:@"." pathname:pathname]; /* Now we have to check to see if it's a Blorb file. */ int res; unsigned char buf[12]; glk_stream_set_position(gamefile, 0, seekmode_Start); res = glk_get_buffer_stream(gamefile, (char *)buf, 12); if (!res) { init_err = "The data in this stand-alone game is too short to read."; return; } if (buf[0] == 'G' && buf[1] == 'l' && buf[2] == 'u' && buf[3] == 'l') { locate_gamefile(FALSE); } else if (buf[0] == 'F' && buf[1] == 'O' && buf[2] == 'R' && buf[3] == 'M' && buf[8] == 'I' && buf[9] == 'F' && buf[10] == 'R' && buf[11] == 'S') { locate_gamefile(TRUE); } else { init_err = "This is neither a Glulx game file nor a Blorb file which contains one."; } } /* 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) */ static void iosglk_game_autorestore() { GlkLibrary *library = [GlkLibrary singleton]; NSString *dirname = documents_dir(); if (!dirname) return; NSString *gamepath = [dirname stringByAppendingPathComponent:@"autosave.glksave"]; NSString *libpath = [dirname stringByAppendingPathComponent:@"autosave.plist"]; if (![library.filemanager fileExistsAtPath:gamepath]) return; if (![library.filemanager fileExistsAtPath:libpath]) return; bzero(&library_state, sizeof(library_state)); GlkLibrary *newlib = nil; [GlkLibrary setExtraUnarchiveHook:iosglk_library_unarchive]; @try { newlib = [NSKeyedUnarchiver unarchiveObjectWithFile:libpath]; } @catch (NSException *ex) { // leave newlib as nil NSLog(@"Unable to restore autosave library: %@", ex); } [GlkLibrary setExtraUnarchiveHook:nil]; if (!newlib || !library_state.active) { /* Without a Glk state, there's no point in even trying the VM state. */ NSLog(@"library autorestore failed!"); return; } int res; GlkStreamFile *savefile = [[[GlkStreamFile alloc] initWithMode:filemode_Read rock:1 unicode:NO textmode:NO dirname:dirname pathname:gamepath] autorelease]; res = perform_restore(savefile, TRUE); glk_stream_close(savefile, nil); savefile = nil; if (res) { NSLog(@"VM autorestore failed!"); return; } pop_callstub(0); /* Annoyingly, the updateFromLibrary we're about to do will close the currently-open gamefile. We'll recover it immediately, in recover_library_state(). */ gamefile = nil; [library updateFromLibrary:newlib]; recover_library_state(); NSLog(@"autorestore succeeded."); free_library_state(); } /* This is the library_select_hook, which will be called every time glk_select() is invoked. (VM thread) */ static void iosglk_game_select(glui32 eventaddr) { glui32 lasteventtype = [GlkAppWrapper singleton].lasteventtype; //NSLog(@"### game called select, last event was %d", lasteventtype); /* 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.) */ if (lasteventtype == -1 || lasteventtype == evtype_Arrange) return; iosglk_do_autosave(eventaddr); } void iosglk_do_autosave(glui32 eventaddr) { GlkLibrary *library = [GlkLibrary singleton]; //NSLog(@"### attempting autosave (pc = %x, eventaddr = %x, stack = %d before stub)", prevpc, eventaddr, stackptr); /* 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. */ int res; int opmodes[3]; res = parse_partial_operand(opmodes); if (!res) return; NSString *dirname = documents_dir(); if (!dirname) return; NSString *tmpgamepath = [dirname stringByAppendingPathComponent:@"autosave-tmp.glksave"]; GlkStreamFile *savefile = [[[GlkStreamFile alloc] initWithMode:filemode_Write rock:1 unicode:NO textmode:NO dirname:dirname pathname:tmpgamepath] autorelease]; /* Push all the necessary arguments for the @glk opcode. */ glui32 origstackptr = stackptr; int stackvals = 0; /* The event structure address: */ stackvals++; if (stackptr+4 > stacksize) fatal_error("Stack overflow in autosave callstub."); StkW4(stackptr, eventaddr); stackptr += 4; if (opmodes[1] == 8) { /* The number of Glk arguments (1): */ stackvals++; if (stackptr+4 > stacksize) fatal_error("Stack overflow in autosave callstub."); StkW4(stackptr, 1); stackptr += 4; } if (opmodes[0] == 8) { /* The Glk call selector (0x00C0): */ stackvals++; if (stackptr+4 > stacksize) fatal_error("Stack overflow in autosave callstub."); StkW4(stackptr, 0x00C0); /* glk_select */ stackptr += 4; } /* Push a temporary callstub which contains the *last* PC -- the address of the @glk(select) invocation. */ if (stackptr+16 > stacksize) fatal_error("Stack overflow in autosave callstub."); StkW4(stackptr+0, 0); StkW4(stackptr+4, 0); StkW4(stackptr+8, prevpc); StkW4(stackptr+12, frameptr); stackptr += 16; res = perform_save(savefile); stackptr -= 16; // discard the temporary callstub stackptr -= 4 * stackvals; // discard the temporary arguments if (origstackptr != stackptr) fatal_error("Stack pointer mismatch in autosave"); glk_stream_close(savefile, nil); savefile = nil; if (res) { NSLog(@"VM autosave failed!"); return; } bzero(&library_state, sizeof(library_state)); stash_library_state(); /* The iosglk_library_archive hook will write out the contents of library_state. */ NSString *tmplibpath = [dirname stringByAppendingPathComponent:@"autosave-tmp.plist"]; [GlkLibrary setExtraArchiveHook:iosglk_library_archive]; res = [NSKeyedArchiver archiveRootObject:library toFile:tmplibpath]; [GlkLibrary setExtraArchiveHook:nil]; free_library_state(); if (!res) { NSLog(@"library serialize failed!"); return; } NSString *finalgamepath = [dirname stringByAppendingPathComponent:@"autosave.glksave"]; NSString *finallibpath = [dirname stringByAppendingPathComponent:@"autosave.plist"]; /* This is not really atomic, but we're already past the serious failure modes. */ [library.filemanager removeItemAtPath:finallibpath error:nil]; [library.filemanager removeItemAtPath:finalgamepath error:nil]; res = [library.filemanager moveItemAtPath:tmpgamepath toPath:finalgamepath error:nil]; if (!res) { NSLog(@"could not move game autosave to final position!"); return; } res = [library.filemanager moveItemAtPath:tmplibpath toPath:finallibpath error:nil]; if (!res) { NSLog(@"could not move library autosave to final position"); return; } } /* Delete an autosaved game, if one exists. */ void iosglk_clear_autosave() { GlkLibrary *library = [GlkLibrary singleton]; NSString *dirname = documents_dir(); if (!dirname) return; NSString *finalgamepath = [dirname stringByAppendingPathComponent:@"autosave.glksave"]; NSString *finallibpath = [dirname stringByAppendingPathComponent:@"autosave.plist"]; [library.filemanager removeItemAtPath:finallibpath error:nil]; [library.filemanager removeItemAtPath:finalgamepath error:nil]; } /* Utility function used by stash_library_state. Assumes that library_state.accel_funcs is a valid NSMutableArray. */ static void stash_one_accel_func(glui32 index, glui32 addr) { NSMutableArray *arr = (NSMutableArray *)library_state.accel_funcs; GlulxAccelEntry *ent = [[[GlulxAccelEntry alloc] initWithIndex:index addr:addr] autorelease]; [arr addObject:ent]; } /* 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. */ static void stash_library_state() { library_state.active = YES; library_state.protectstart = protectstart; library_state.protectend = protectend; stream_get_iosys(&library_state.iosys_mode, &library_state.iosys_rock); library_state.stringtable = stream_get_table(); glui32 count = accel_get_param_count(); NSMutableArray *accel_params = [NSMutableArray arrayWithCapacity:count]; library_state.accel_params = [accel_params retain]; for (int ix=0; ix