| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "glk.h" |
| 7 |
#include "glulxe.h" |
| 8 |
|
| 9 |
int vm_exited_cleanly = TRUE; |
| 10 |
strid_t gamefile = NULL; |
| 11 |
glui32 gamefile_start = 0; |
| 12 |
|
| 13 |
glui32 gamefile_len = 0; |
| 14 |
char *init_err = NULL; |
| 15 |
char *init_err2 = NULL; |
| 16 |
|
| 17 |
glui32 init_rng_seed = 0; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static void (*library_start_hook)(void) = NULL; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static void (*library_autorestore_hook)(void) = NULL; |
| 29 |
|
| 30 |
static winid_t get_error_win(void); |
| 31 |
static void stream_hexnum(glsi32 val); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
void glk_main() |
| 38 |
{ |
| 39 |
vm_exited_cleanly = FALSE; |
| 40 |
|
| 41 |
if (library_start_hook) |
| 42 |
library_start_hook(); |
| 43 |
|
| 44 |
if (init_err) { |
| 45 |
fatal_error_2(init_err, init_err2); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if (!is_gamefile_valid()) { |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
glulx_setrandom(init_rng_seed); |
| 55 |
#ifdef FLOAT_SUPPORT |
| 56 |
if (!init_float()) { |
| 57 |
return; |
| 58 |
} |
| 59 |
#endif |
| 60 |
if (!init_dispatch()) { |
| 61 |
return; |
| 62 |
} |
| 63 |
if (!init_profile()) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
setup_vm(); |
| 68 |
if (library_autorestore_hook) |
| 69 |
library_autorestore_hook(); |
| 70 |
execute_loop(); |
| 71 |
finalize_vm(); |
| 72 |
|
| 73 |
gamefile = NULL; |
| 74 |
gamefile_start = 0; |
| 75 |
gamefile_len = 0; |
| 76 |
init_err = NULL; |
| 77 |
vm_exited_cleanly = TRUE; |
| 78 |
|
| 79 |
profile_quit(); |
| 80 |
glk_exit(); |
| 81 |
} |
| 82 |
|
| 83 |
void set_library_start_hook(void (*func)(void)) |
| 84 |
{ |
| 85 |
library_start_hook = func; |
| 86 |
} |
| 87 |
|
| 88 |
void set_library_autorestore_hook(void (*func)(void)) |
| 89 |
{ |
| 90 |
library_autorestore_hook = func; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static winid_t get_error_win() |
| 99 |
{ |
| 100 |
static winid_t errorwin = NULL; |
| 101 |
|
| 102 |
if (!errorwin) { |
| 103 |
winid_t rootwin = glk_window_get_root(); |
| 104 |
if (!rootwin) { |
| 105 |
errorwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1); |
| 106 |
} |
| 107 |
else { |
| 108 |
errorwin = glk_window_open(rootwin, winmethod_Below | winmethod_Fixed, |
| 109 |
3, wintype_TextBuffer, 0); |
| 110 |
} |
| 111 |
if (!errorwin) |
| 112 |
errorwin = rootwin; |
| 113 |
} |
| 114 |
|
| 115 |
return errorwin; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
void fatal_error_handler(char *str, char *arg, int useval, glsi32 val) |
| 122 |
{ |
| 123 |
winid_t win; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
debugger_handle_crash(str); |
| 129 |
|
| 130 |
win = get_error_win(); |
| 131 |
if (win) { |
| 132 |
glk_set_window(win); |
| 133 |
glk_put_string("Glulxe fatal error: "); |
| 134 |
glk_put_string(str); |
| 135 |
if (arg || useval) { |
| 136 |
glk_put_string(" ("); |
| 137 |
if (arg) |
| 138 |
glk_put_string(arg); |
| 139 |
if (arg && useval) |
| 140 |
glk_put_string(" "); |
| 141 |
if (useval) |
| 142 |
stream_hexnum(val); |
| 143 |
glk_put_string(")"); |
| 144 |
} |
| 145 |
glk_put_string("\n"); |
| 146 |
} |
| 147 |
glk_exit(); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void nonfatal_warning_handler(char *str, char *arg, int useval, glsi32 val) |
| 154 |
{ |
| 155 |
winid_t win = get_error_win(); |
| 156 |
if (win) { |
| 157 |
strid_t oldstr = glk_stream_get_current(); |
| 158 |
glk_set_window(win); |
| 159 |
glk_put_string("Glulxe warning: "); |
| 160 |
glk_put_string(str); |
| 161 |
if (arg || useval) { |
| 162 |
glk_put_string(" ("); |
| 163 |
if (arg) |
| 164 |
glk_put_string(arg); |
| 165 |
if (arg && useval) |
| 166 |
glk_put_string(" "); |
| 167 |
if (useval) |
| 168 |
stream_hexnum(val); |
| 169 |
glk_put_string(")"); |
| 170 |
} |
| 171 |
glk_put_string("\n"); |
| 172 |
glk_stream_set_current(oldstr); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static void stream_hexnum(glsi32 val) |
| 180 |
{ |
| 181 |
char buf[16]; |
| 182 |
glui32 ival; |
| 183 |
int ix; |
| 184 |
|
| 185 |
if (val == 0) { |
| 186 |
glk_put_char('0'); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if (val < 0) { |
| 191 |
glk_put_char('-'); |
| 192 |
ival = -val; |
| 193 |
} |
| 194 |
else { |
| 195 |
ival = val; |
| 196 |
} |
| 197 |
|
| 198 |
ix = 0; |
| 199 |
while (ival != 0) { |
| 200 |
buf[ix] = (ival % 16) + '0'; |
| 201 |
if (buf[ix] > '9') |
| 202 |
buf[ix] += ('A' - ('9' + 1)); |
| 203 |
ix++; |
| 204 |
ival /= 16; |
| 205 |
} |
| 206 |
|
| 207 |
while (ix) { |
| 208 |
ix--; |
| 209 |
glk_put_char(buf[ix]); |
| 210 |
} |
| 211 |
} |
| 212 |
|