| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
#include "glk.h" |
| 9 |
#include "gi_blorb.h" |
| 10 |
#include "glulxe.h" |
| 11 |
#include "unixstrt.h" |
| 12 |
#include "glkstart.h" |
| 13 |
|
| 14 |
#if VM_DEBUGGER |
| 15 |
|
| 16 |
|
| 17 |
#include "gi_debug.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
static void glkunix_game_select(glui32 selector, glui32 arg0, glui32 arg1, glui32 arg2); |
| 21 |
static void glkunix_game_start(void); |
| 22 |
static void glkunix_game_autorestore(void); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
glkunix_argumentlist_t glkunix_arguments[] = { |
| 35 |
|
| 36 |
{ "--undo", glkunix_arg_ValueFollows, "Number of undo states to store." }, |
| 37 |
{ "--rngseed", glkunix_arg_ValueFollows, "Fix initial RNG if nonzero." }, |
| 38 |
|
| 39 |
#if GLKUNIX_AUTOSAVE_FEATURES |
| 40 |
{ "--autosave", glkunix_arg_NoValue, "Autosave every turn." }, |
| 41 |
{ "--autorestore", glkunix_arg_NoValue, "Autorestore at launch." }, |
| 42 |
{ "--autodir", glkunix_arg_ValueFollows, "Directory for autosave/restore files (default: .)." }, |
| 43 |
{ "--autoname", glkunix_arg_ValueFollows, "Base filename for autosave/restore (default: autosave)." }, |
| 44 |
{ "--autoskiparrange", glkunix_arg_NoValue, "Don't autosave on arrange events." }, |
| 45 |
#endif |
| 46 |
|
| 47 |
#if VM_PROFILING |
| 48 |
{ "--profile", glkunix_arg_ValueFollows, "Generate profiling information to a file." }, |
| 49 |
{ "--profcalls", glkunix_arg_NoValue, "Include what-called-what details in profiling. (Slow!)" }, |
| 50 |
#endif |
| 51 |
|
| 52 |
#if VM_DEBUGGER |
| 53 |
{ "--gameinfo", glkunix_arg_ValueFollows, "Read debug information from a file." }, |
| 54 |
{ "--cpu", glkunix_arg_NoValue, "Display CPU usage of each command (debug)." }, |
| 55 |
{ "--starttrap", glkunix_arg_NoValue, "Enter debug mode at startup time (debug)." }, |
| 56 |
{ "--quittrap", glkunix_arg_NoValue, "Enter debug mode at quit time (debug)." }, |
| 57 |
{ "--crashtrap", glkunix_arg_NoValue, "Enter debug mode on any fatal error (debug)." }, |
| 58 |
#endif |
| 59 |
|
| 60 |
{ "", glkunix_arg_ValueFollows, "filename: The game file to load." }, |
| 61 |
|
| 62 |
{ NULL, glkunix_arg_End, NULL } |
| 63 |
}; |
| 64 |
|
| 65 |
int glkunix_startup_code(glkunix_startup_t *data) |
| 66 |
{ |
| 67 |
|
| 68 |
|
| 69 |
int ix; |
| 70 |
char *filename = NULL; |
| 71 |
char *gameinfofilename = NULL; |
| 72 |
int gameinfoloaded = FALSE; |
| 73 |
int pref_autosave = FALSE; |
| 74 |
int pref_autorestore = FALSE; |
| 75 |
unsigned char buf[12]; |
| 76 |
int res; |
| 77 |
|
| 78 |
#ifdef GARGLK |
| 79 |
char *cx; |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifdef GARGLK |
| 83 |
garglk_set_program_name("Glulxe 0.6.0"); |
| 84 |
garglk_set_program_info("Glulxe 0.6.0 by Andrew Plotkin"); |
| 85 |
#endif |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
for (ix=1; ix<data->argc; ix++) { |
| 91 |
|
| 92 |
if (!strcmp(data->argv[ix], "--undo")) { |
| 93 |
ix++; |
| 94 |
if (ix<data->argc) { |
| 95 |
char *endptr = NULL; |
| 96 |
int val = strtol(data->argv[ix], &endptr, 10); |
| 97 |
if (*endptr) { |
| 98 |
init_err = "--undo must be a number."; |
| 99 |
return TRUE; |
| 100 |
} |
| 101 |
max_undo_level = val; |
| 102 |
} |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
if (!strcmp(data->argv[ix], "--rngseed")) { |
| 107 |
ix++; |
| 108 |
if (ix<data->argc) { |
| 109 |
char *endptr = NULL; |
| 110 |
int val = strtol(data->argv[ix], &endptr, 10); |
| 111 |
if (*endptr) { |
| 112 |
init_err = "--rngseed must be a number."; |
| 113 |
return TRUE; |
| 114 |
} |
| 115 |
init_rng_seed = val; |
| 116 |
} |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
#if GLKUNIX_AUTOSAVE_FEATURES |
| 121 |
if (!strcmp(data->argv[ix], "--autosave")) { |
| 122 |
pref_autosave = TRUE; |
| 123 |
continue; |
| 124 |
} |
| 125 |
if (!strcmp(data->argv[ix], "--autorestore")) { |
| 126 |
pref_autorestore = TRUE; |
| 127 |
continue; |
| 128 |
} |
| 129 |
if (!strcmp(data->argv[ix], "--autodir")) { |
| 130 |
ix++; |
| 131 |
if (ix<data->argc) { |
| 132 |
pref_autosavedir = data->argv[ix]; |
| 133 |
} |
| 134 |
continue; |
| 135 |
} |
| 136 |
if (!strcmp(data->argv[ix], "--autoname")) { |
| 137 |
ix++; |
| 138 |
if (ix<data->argc) { |
| 139 |
pref_autosavename = data->argv[ix]; |
| 140 |
} |
| 141 |
continue; |
| 142 |
} |
| 143 |
if (!strcmp(data->argv[ix], "--autoskiparrange")) { |
| 144 |
pref_autosave_skiparrange = TRUE; |
| 145 |
continue; |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
#if VM_PROFILING |
| 150 |
if (!strcmp(data->argv[ix], "--profile")) { |
| 151 |
ix++; |
| 152 |
if (ix<data->argc) { |
| 153 |
strid_t profstr = glkunix_stream_open_pathname_gen(data->argv[ix], TRUE, FALSE, 1); |
| 154 |
if (!profstr) { |
| 155 |
init_err = "Unable to open profile output file."; |
| 156 |
init_err2 = data->argv[ix]; |
| 157 |
return TRUE; |
| 158 |
} |
| 159 |
setup_profile(profstr, NULL); |
| 160 |
} |
| 161 |
continue; |
| 162 |
} |
| 163 |
if (!strcmp(data->argv[ix], "--profcalls")) { |
| 164 |
profile_set_call_counts(TRUE); |
| 165 |
continue; |
| 166 |
} |
| 167 |
#endif |
| 168 |
|
| 169 |
#if VM_DEBUGGER |
| 170 |
if (!strcmp(data->argv[ix], "--gameinfo")) { |
| 171 |
ix++; |
| 172 |
if (ix<data->argc) { |
| 173 |
gameinfofilename = data->argv[ix]; |
| 174 |
} |
| 175 |
continue; |
| 176 |
} |
| 177 |
if (!strcmp(data->argv[ix], "--cpu")) { |
| 178 |
debugger_track_cpu(TRUE); |
| 179 |
continue; |
| 180 |
} |
| 181 |
if (!strcmp(data->argv[ix], "--starttrap")) { |
| 182 |
debugger_set_start_trap(TRUE); |
| 183 |
continue; |
| 184 |
} |
| 185 |
if (!strcmp(data->argv[ix], "--quittrap")) { |
| 186 |
debugger_set_quit_trap(TRUE); |
| 187 |
continue; |
| 188 |
} |
| 189 |
if (!strcmp(data->argv[ix], "--crashtrap")) { |
| 190 |
debugger_set_crash_trap(TRUE); |
| 191 |
continue; |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
if (filename) { |
| 196 |
init_err = "You must supply exactly one game file."; |
| 197 |
return TRUE; |
| 198 |
} |
| 199 |
filename = data->argv[ix]; |
| 200 |
} |
| 201 |
|
| 202 |
if (!filename) { |
| 203 |
init_err = "You must supply the name of a game file."; |
| 204 |
return TRUE; |
| 205 |
} |
| 206 |
|
| 207 |
gamefile = glkunix_stream_open_pathname(filename, FALSE, 1); |
| 208 |
if (!gamefile) { |
| 209 |
init_err = "The game file could not be opened."; |
| 210 |
init_err2 = filename; |
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
#ifdef GARGLK |
| 215 |
cx = strrchr(data->argv[1], '/'); |
| 216 |
if (!cx) cx = strrchr(data->argv[1], '\\'); |
| 217 |
garglk_set_story_name(cx ? cx + 1 : data->argv[1]); |
| 218 |
#endif |
| 219 |
|
| 220 |
#if GLKUNIX_AUTOSAVE_FEATURES |
| 221 |
if (pref_autosave || pref_autorestore) { |
| 222 |
set_library_start_hook(glkunix_game_start); |
| 223 |
if (pref_autorestore) |
| 224 |
set_library_autorestore_hook(glkunix_game_autorestore); |
| 225 |
if (pref_autosave) |
| 226 |
set_library_select_hook(glkunix_game_select); |
| 227 |
} |
| 228 |
#endif |
| 229 |
|
| 230 |
#if VM_DEBUGGER |
| 231 |
if (gameinfofilename) { |
| 232 |
strid_t debugstr = glkunix_stream_open_pathname_gen(gameinfofilename, FALSE, FALSE, 1); |
| 233 |
if (!debugstr) { |
| 234 |
nonfatal_warning("Unable to open gameinfo file for debug data."); |
| 235 |
} |
| 236 |
else { |
| 237 |
int bres = debugger_load_info_stream(debugstr); |
| 238 |
glk_stream_close(debugstr, NULL); |
| 239 |
if (!bres) |
| 240 |
nonfatal_warning("Unable to parse game info."); |
| 241 |
else |
| 242 |
gameinfoloaded = TRUE; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
gidebug_debugging_available(debugger_cmd_handler, debugger_cycle_handler); |
| 248 |
#endif |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
glk_stream_set_position(gamefile, 0, seekmode_Start); |
| 253 |
res = glk_get_buffer_stream(gamefile, (char *)buf, 12); |
| 254 |
if (!res) { |
| 255 |
init_err = "The data in this stand-alone game is too short to read."; |
| 256 |
return TRUE; |
| 257 |
} |
| 258 |
|
| 259 |
if (buf[0] == 'G' && buf[1] == 'l' && buf[2] == 'u' && buf[3] == 'l') { |
| 260 |
|
| 261 |
locate_gamefile(FALSE); |
| 262 |
|
| 263 |
return TRUE; |
| 264 |
} |
| 265 |
else if (buf[0] == 'F' && buf[1] == 'O' && buf[2] == 'R' && buf[3] == 'M' |
| 266 |
&& buf[8] == 'I' && buf[9] == 'F' && buf[10] == 'R' && buf[11] == 'S') { |
| 267 |
|
| 268 |
locate_gamefile(TRUE); |
| 269 |
|
| 270 |
#if VM_DEBUGGER |
| 271 |
|
| 272 |
if (!gameinfoloaded) { |
| 273 |
glui32 giblorb_ID_Dbug = giblorb_make_id('D', 'b', 'u', 'g'); |
| 274 |
giblorb_err_t err; |
| 275 |
giblorb_result_t blorbres; |
| 276 |
err = giblorb_load_chunk_by_type(giblorb_get_resource_map(), |
| 277 |
giblorb_method_FilePos, |
| 278 |
&blorbres, giblorb_ID_Dbug, 0); |
| 279 |
if (!err) { |
| 280 |
int bres = debugger_load_info_chunk(gamefile, blorbres.data.startpos, blorbres.length); |
| 281 |
if (!bres) |
| 282 |
nonfatal_warning("Unable to parse game info."); |
| 283 |
else |
| 284 |
gameinfoloaded = TRUE; |
| 285 |
} |
| 286 |
} |
| 287 |
#endif |
| 288 |
return TRUE; |
| 289 |
} |
| 290 |
else { |
| 291 |
init_err = "This is neither a Glulx game file nor a Blorb file " |
| 292 |
"which contains one."; |
| 293 |
return TRUE; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
#ifdef GLKUNIX_AUTOSAVE_FEATURES |
| 300 |
|
| 301 |
static void glkunix_game_start() |
| 302 |
{ |
| 303 |
unsigned char buf[64]; |
| 304 |
glk_stream_set_position(gamefile, gamefile_start, seekmode_Start); |
| 305 |
glui32 res = glk_get_buffer_stream(gamefile, (char *)buf, 64); |
| 306 |
if (res == 0) { |
| 307 |
fatal_error("Unable to read game file."); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
glkunix_set_autosave_signature(buf, res); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
static void glkunix_game_select(glui32 selector, glui32 arg0, glui32 arg1, glui32 arg2) |
| 317 |
{ |
| 318 |
glui32 lasteventtype = glkunix_get_last_event_type(); |
| 319 |
|
| 320 |
|
| 321 |
if (lasteventtype == 0xFFFFFFFF |
| 322 |
|| lasteventtype == 0xFFFFFFFE) |
| 323 |
return; |
| 324 |
|
| 325 |
if (pref_autosave_skiparrange && lasteventtype == evtype_Arrange) |
| 326 |
return; |
| 327 |
|
| 328 |
glkunix_do_autosave(selector, arg0, arg1, arg2); |
| 329 |
} |
| 330 |
|
| 331 |
static void glkunix_game_autorestore() |
| 332 |
{ |
| 333 |
int res = glkunix_do_autorestore(); |
| 334 |
if (!res) |
| 335 |
fatal_error("Autorestore failed."); |
| 336 |
} |
| 337 |
|
| 338 |
#endif |