GLK: AGT: Initialization fixes

This commit is contained in:
Paul Gilbert 2019-11-24 22:30:58 -08:00
parent 28c3584148
commit 88444ddc88
6 changed files with 30 additions and 108 deletions

View File

@ -31,15 +31,22 @@ namespace AGT {
AGT *g_vm;
extern void glk_main();
extern int glk_startup_code(int argc, char *argv[]);
extern int glk_startup_code();
extern void gagt_finalizer();
AGT::AGT(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(syst, gameDesc) {
AGT::AGT(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(syst, gameDesc),
gagt_gamefile(nullptr), gagt_game_message(nullptr) {
g_vm = this;
}
void AGT::runGame() {
glk_startup_code(0, nullptr);
_gameFile.close();
gagt_gamefile = getFilename().c_str();
glk_startup_code();
glk_main();
gagt_finalizer();
}
Common::Error AGT::readSaveData(Common::SeekableReadStream *rs) {

View File

@ -36,6 +36,9 @@ namespace AGT {
* AGT Adams game interpreter
*/
class AGT : public GlkAPI {
public:
const char *gagt_gamefile = NULL; /* Name of game file. */
const char *gagt_game_message = NULL; /* Error message. */
public:
/**
* Constructor

View File

@ -550,14 +550,10 @@ rbool filevalid(genfile f, filetype ft) {
void binseek(genfile f, long offset) {
assert(f != NULL);
assert(offset >= 0);
#ifdef UNIX_IO
if (lseek(fileno(f), offset, SEEK_SET) == -1)
#else
if (fseek(f, offset, SEEK_SET) != 0)
#endif
fatal("binseek");
Common::SeekableReadStream *rs = dynamic_cast<Common::SeekableReadStream *>(f);
assert(rs);
rs->seek(offset);
}

View File

@ -453,7 +453,7 @@ global volatile int screen_width, status_width;
global int screen_height;
global volatile int curr_x;
extern void init_interface(int argc, char *argv[]);
extern void init_interface();
extern void start_interface(fc_type fc);
extern void close_interface(void);
extern char *agt_input(int in_type); /* read line, return malloc'd string */

View File

@ -5067,7 +5067,7 @@ genfile agt_globalfile(int fid) {
* General initialization for the module; sets some variables, and creates
* the Glk windows to work in. Called from the AGiliTy main().
*/
void init_interface(int argc, char *argv[]) {
void init_interface() {
glui32 status_height;
/*
@ -5147,7 +5147,6 @@ void init_interface(int argc, char *argv[]) {
}
agt_clrscr();
gagt_debug("init_interface", "argc=%d, argv=%p", argc, argv);
}
@ -5524,15 +5523,6 @@ int __wrap_tolower(int ch) {
/* External declaration of interface.c's set default options function. */
extern void set_default_options();
/*
* The following values need to be passed between the startup_code and main
* functions.
*/
static int gagt_saved_argc = 0; /* Recorded argc. */
static char **gagt_saved_argv = NULL, /* Recorded argv. */
*gagt_gamefile = NULL; /* Name of game file. */
static const char *gagt_game_message = NULL; /* Error message. */
/*
* Flag to set if we want to test for a clean exit. Without this it's a
* touch tricky sometimes to corner AGiliTy into calling exit() for us; it
@ -5656,73 +5646,19 @@ static int gagt_parse_option(const char *option) {
* gagt_main()
*
* Together, these functions take the place of the original AGiliTy main().
* The first one is called from glkunix_startup_code(), to parse and
* generally handle options. The second is called from glk_main(), and
* does the real work of running the game.
* The first one is called from glkunix_startup_code(). The second is called
* from glk_main(), and does the real work of running the game.
*/
int gagt_startup_code(int argc, char *argv[]) {
int argv_index;
/*
* Before doing anything else, stash argc and argv away for use by
* gagt_main() below.
*/
gagt_saved_argc = argc;
gagt_saved_argv = argv;
bool gagt_startup_code() {
/* Make the mandatory call for initialization. */
set_default_options();
/* Handle command line arguments. */
for (argv_index = 1;
argv_index < argc && argv[argv_index][0] == '-'; argv_index++) {
/*
* Handle an option string coming after "-". If the options parse
* fails, return FALSE.
*/
if (!gagt_parse_option(argv[argv_index]))
return FALSE;
}
/*
* Get the name of the game file. Since we need this in our call from
* glk_main, we need to keep it in a module static variable. If the game
* file name is omitted, then here we'll set the pointer to NULL, and
* complain about it later in main. Passing the message string around
* like this is a nuisance...
*/
if (argv_index == argc - 1) {
gagt_gamefile = argv[argv_index];
gagt_game_message = NULL;
#ifdef GARGLK
char *s;
s = strrchr(gagt_gamefile, '\\');
if (s) g_vm->garglk_set_story_name(s + 1);
s = strrchr(gagt_gamefile, '/');
if (s) g_vm->garglk_set_story_name(s + 1);
#endif /* GARGLK */
} else {
gagt_gamefile = NULL;
if (argv_index < argc - 1)
gagt_game_message = "More than one game file was given"
" on the command line.";
else
gagt_game_message = "No game file was given on the command line.";
}
/* All startup options were handled successfully. */
return TRUE;
}
static void gagt_main() {
fc_type fc;
assert(gagt_saved_argc != 0 && gagt_saved_argv);
/* Ensure AGiliTy internal types have the right sizes. */
if (sizeof(integer) < 2 || sizeof(int32) < 4 || sizeof(uint32) < 4) {
gagt_fatal("GLK: Types sized incorrectly, recompilation is needed");
gagt_exit();
}
/*
* Initialize the interface. As it happens, init_interface() is in our
@ -5734,7 +5670,7 @@ static void gagt_main() {
* window. As it doesn't return status, we have to detect this by checking
* that gagt_main_window is not NULL.
*/
init_interface(gagt_saved_argc, gagt_saved_argv);
init_interface();
if (!gagt_main_window) {
gagt_fatal("GLK: Can't open main window");
gagt_exit();
@ -5743,29 +5679,18 @@ static void gagt_main() {
g_vm->glk_set_window(gagt_main_window);
g_vm->glk_set_style(style_Normal);
/* If there's a problem with the game file, complain now. */
if (!gagt_gamefile) {
assert(gagt_game_message);
if (gagt_status_window)
g_vm->glk_window_close(gagt_status_window, NULL);
gagt_header_string("Glk AGiliTy Error\n\n");
gagt_normal_string(gagt_game_message);
gagt_normal_char('\n');
gagt_exit();
}
/*
* Create a game file context, and try to ensure it will open successfully
* in run_game().
*/
fc = init_file_context(gagt_gamefile, fDA1);
fc = init_file_context(g_vm->gagt_gamefile, fDA1);
if (!(gagt_workround_fileexist(fc, fAGX)
|| gagt_workround_fileexist(fc, fDA1))) {
if (gagt_status_window)
g_vm->glk_window_close(gagt_status_window, NULL);
gagt_header_string("Glk AGiliTy Error\n\n");
gagt_normal_string("Can't find or open game '");
gagt_normal_string(gagt_gamefile);
gagt_normal_string(g_vm->gagt_gamefile);
gagt_normal_char('\'');
gagt_normal_char('\n');
gagt_exit();
@ -5860,7 +5785,7 @@ static int gagt_agility_running = FALSE;
* we do, and interpreter code is still running, it's a sign that we need
* to take actions we'd hoped not to have to take.
*/
static void gagt_finalizer() {
void gagt_finalizer() {
/*
* If interpreter code is still active, and we're not in a g_vm->glk_select(),
* the core interpreter code called exit(). Handle cleanup.
@ -5982,16 +5907,6 @@ void glk_main() {
assert(gagt_startup_called && !gagt_main_called);
gagt_main_called = TRUE;
/*
* Register gagt_finalizer() with atexit() to cleanup on exit. Note that
* this module doesn't expect the atexit() handler to be called on all
* forms of exit -- see comments in gagt_finalizer() for more.
*/
if (atexit(gagt_finalizer) != 0) {
gagt_fatal("GLK: Failed to register finalizer");
gagt_exit();
}
/*
* If we're testing for a clean exit, deliberately call exit() to see what
* happens. We're hoping for a clean process termination, but our exit
@ -6088,11 +6003,11 @@ glkunix_argumentlist_t glkunix_arguments[] = {
* function to parse arguments and generally set stuff up.
*/
int glk_startup_code(int argc, char *argv[]) {
int glk_startup_code() {
assert(!gagt_startup_called);
gagt_startup_called = TRUE;
return gagt_startup_code(argc, argv);
return gagt_startup_code();
}
} // End of namespace AGT

View File

@ -369,7 +369,8 @@ int fseek(genfile stream, long int offset, int whence) {
size_t fread(void *ptr, size_t size, size_t nmemb, genfile stream) {
Common::SeekableReadStream *rs = dynamic_cast<Common::SeekableReadStream *>(stream);
assert(rs);
return rs->read(ptr, size * nmemb);
size_t bytesRead = rs->read(ptr, size * nmemb);
return bytesRead / size;
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, genfile stream) {