1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/adrift/scare.h"
#include "glk/adrift/sxprotos.h"
#include "common/str.h"
namespace Glk {
namespace Adrift {
/*
* Module notes:
*
* o Better, or perhaps less strace-like, tracing might be more beneficial
* for game script debugging.
*/
/* Stubs trace flag. */
static sc_bool stub_trace = FALSE;
/*
* Input/output handler functions. If assigned, calls to os_* functions are
* routed here to allow the script runner to catch interpeter i/o.
*/
static sc_bool (*stub_read_line) (sc_char*, sc_int) = NULL;
static void (*stub_print_string) (const sc_char*) = NULL;
static void *(*stub_open_file) (sc_bool) = NULL;
static sc_int (*stub_read_file) (void*, sc_byte*, sc_int) = NULL;
static void (*stub_write_file) (void*, const sc_byte*, sc_int) = NULL;
static void (*stub_close_file) (void*) = NULL;
/* Flags for whether to report tags and resources via stub_print_string(). */
static sc_int stub_show_resources = 0;
static sc_int stub_show_tags = 0;
/*
* stub_attach_handlers()
* stub_detach_handlers()
*
* Attach input/output handler functions, and reset to NULLs.
*/
void
stub_attach_handlers (sc_bool (*read_line) (sc_char*, sc_int),
void (*print_string) (const sc_char*),
void *(*open_file) (sc_bool),
sc_int (*read_file) (void*, sc_byte*, sc_int),
void (*write_file) (void*, const sc_byte*, sc_int),
void (*close_file) (void*))
{
stub_read_line = read_line;
stub_print_string = print_string;
stub_open_file = open_file;
stub_read_file = read_file;
stub_write_file = write_file;
stub_close_file = close_file;
stub_show_resources = 0;
stub_show_tags = 0;
}
void
stub_detach_handlers (void)
{
stub_read_line = NULL;
stub_print_string = NULL;
stub_open_file = NULL;
stub_read_file = NULL;
stub_write_file = NULL;
stub_close_file = NULL;
stub_show_resources = 0;
stub_show_tags = 0;
}
/*
* stub_adjust_test_control()
* stub_catch_test_control()
*
* Trap testing control tags from the game, incrementing or decrementing
* stub_show_resources or stub_show_tags if a testing control tag arrives.
* Returns TRUE if the tag trapped was one of our testing ones.
*/
static void
stub_adjust_test_control (sc_int *control, sc_bool is_begin)
{
*control += is_begin ? 1 : (*control > 0 ? -1 : 0);
}
static sc_bool
stub_catch_test_control (sc_int tag, const sc_char *argument)
{
if (tag == SC_TAG_UNKNOWN && argument)
{
sc_bool is_begin;
const sc_char *name;
is_begin = !(argument[0] == '/');
name = is_begin ? argument : argument + 1;
if (sc_strcasecmp (name, "sxshowresources") == 0)
{
stub_adjust_test_control (&stub_show_resources, is_begin);
return TRUE;
}
else if (sc_strcasecmp (name, "sxshowtags") == 0)
{
stub_adjust_test_control (&stub_show_tags, is_begin);
return TRUE;
}
}
return FALSE;
}
/*
* stub_notnull()
*
* Returns the string address passed in, or "(nil)" if NULL, for printf
* safety. Most libc's handle this themselves, but it's not defined by ANSI.
*/
static const sc_char *
stub_notnull (const sc_char *string)
{
return string ? string : "(nil)";
}
/*
* os_*()
*
* Stub functions called by the interpreter core.
*/
void
os_print_tag (sc_int tag, const sc_char *argument)
{
if (stub_trace)
sx_trace ("os_print_tag (%ld, \"%s\")\n", tag, stub_notnull (argument));
if (!stub_catch_test_control (tag, argument))
{
if (stub_print_string)
{
if (stub_show_tags > 0) {
stub_print_string ("<<Tag: id=");
Common::String buffer = Common::String::format("%ld", tag);
stub_print_string (buffer.c_str());
stub_print_string (", argument=\"");
stub_print_string (stub_notnull (argument));
stub_print_string ("\">>");
}
else if (tag == SC_TAG_WAITKEY || tag == SC_TAG_CLS)
stub_print_string (" ");
}
}
}
void
os_print_string (const sc_char *string)
{
if (stub_trace)
sx_trace ("os_print_string (\"%s\")\n", stub_notnull (string));
if (stub_print_string)
stub_print_string (string);
}
void
os_print_string_debug (const sc_char *string)
{
if (stub_trace)
sx_trace ("os_print_string_debug (\"%s\")\n", stub_notnull (string));
if (stub_print_string)
stub_print_string (string);
}
void
os_play_sound (const sc_char *filepath,
sc_int offset, sc_int length, sc_bool is_looping)
{
if (stub_trace)
sx_trace ("os_play_sound (\"%s\", %ld, %ld, %s)\n",
stub_notnull (filepath), offset, length,
is_looping ? "true" : "false");
if (stub_print_string && stub_show_resources > 0)
{
sc_char buffer[32];
stub_print_string ("<<Sound: id=\"");
stub_print_string (stub_notnull (filepath));
stub_print_string ("\", offset=");
sprintf (buffer, "%ld", offset);
stub_print_string (buffer);
stub_print_string (", length=");
sprintf (buffer, "%ld", length);
stub_print_string (buffer);
stub_print_string (", looping=");
stub_print_string (is_looping ? "true" : "false");
stub_print_string (">>");
}
}
void
os_stop_sound (void)
{
if (stub_trace)
sx_trace ("os_stop_sound ()\n");
if (stub_print_string && stub_show_resources > 0)
stub_print_string ("<<Sound: stop>>");
}
void
os_show_graphic (const sc_char *filepath, sc_int offset, sc_int length)
{
if (stub_trace)
sx_trace ("os_show_graphic (\"%s\", %ld, %ld)\n",
stub_notnull (filepath), offset, length);
if (stub_print_string && stub_show_resources > 0)
{
sc_char buffer[32];
stub_print_string ("<<Graphic: id=\"");
stub_print_string (stub_notnull (filepath));
stub_print_string ("\", offset=");
sprintf (buffer, "%ld", offset);
stub_print_string (buffer);
stub_print_string (", length=");
sprintf (buffer, "%ld", length);
stub_print_string (buffer);
stub_print_string (">>");
}
}
sc_bool
os_read_line (sc_char *buffer, sc_int length)
{
sc_bool status;
if (stub_read_line)
status = stub_read_line (buffer, length);
else
{
assert (buffer && length > 4);
sprintf (buffer, "%s", "quit");
status = TRUE;
}
if (stub_trace)
{
if (status)
sx_trace ("os_read_line (\"%s\", %ld) -> true\n",
stub_notnull (buffer), length);
else
sx_trace ("os_read_line (\"...\", %ld) -> false\n", length);
}
return status;
}
sc_bool
os_read_line_debug (sc_char *buffer, sc_int length)
{
assert (buffer && length > 8);
sprintf (buffer, "%s", "continue");
if (stub_trace)
sx_trace ("os_read_line_debug (\"%s\", %ld) -> true\n", buffer, length);
return TRUE;
}
sc_bool
os_confirm (sc_int type)
{
if (stub_trace)
sx_trace ("os_confirm (%ld) -> true\n", type);
return TRUE;
}
void *
os_open_file (sc_bool is_save)
{
void *opaque;
if (stub_open_file)
opaque = stub_open_file (is_save);
else
opaque = NULL;
if (stub_trace)
{
if (opaque)
sx_trace ("os_open_file (%s) -> %p\n",
is_save ? "true" : "false", opaque);
else
sx_trace ("os_open_file (%s) -> null\n", is_save ? "true" : "false");
}
return opaque;
}
sc_int
os_read_file (void *opaque, sc_byte *buffer, sc_int length)
{
sc_int bytes;
if (stub_read_file)
bytes = stub_read_file (opaque, buffer, length);
else
bytes = 0;
if (stub_trace)
sx_trace ("os_read_file (%p, %p, %ld) -> %ld\n",
opaque, buffer, length, bytes);
return bytes;
}
void
os_write_file (void *opaque, const sc_byte *buffer, sc_int length)
{
if (stub_write_file)
stub_write_file (opaque, buffer, length);
if (stub_trace)
sx_trace ("os_write_file (%p, %p, %ld)\n", opaque, buffer, length);
}
void
os_close_file (void *opaque)
{
if (stub_close_file)
stub_close_file (opaque);
if (stub_trace)
sx_trace ("os_close_file (%p)\n", opaque);
}
void
os_display_hints (sc_game game)
{
if (stub_trace)
sx_trace ("os_display_hints (%p)\n", game);
if (stub_print_string)
{
sc_game_hint hint;
for (hint = sc_get_first_game_hint (game);
hint; hint = sc_get_next_game_hint (game, hint))
{
const sc_char *hint_text;
stub_print_string (sc_get_game_hint_question (game, hint));
stub_print_string ("\n");
hint_text = sc_get_game_subtle_hint (game, hint);
if (hint_text)
{
stub_print_string ("- ");
stub_print_string (hint_text);
stub_print_string ("\n");
}
hint_text = sc_get_game_unsubtle_hint (game, hint);
if (hint_text)
{
stub_print_string ("- ");
stub_print_string (hint_text);
stub_print_string ("\n");
}
}
}
}
/*
* stub_debug_trace()
*
* Set stubs tracing on/off.
*/
void
stub_debug_trace (sc_bool flag)
{
stub_trace = flag;
}
} // End of namespace Adrift
} // End of namespace Glk
|