int he_main(int argc, char *argv[]) #endif { time_t seed; my_argv = argv; if (!strcmp(program_path, "") && argv) MakeProgramPath(argv[0]); /* Seed the random number generator */ #if !defined (RANDOM) srand((unsigned int)time((time_t *)&seed)); #else SRANDOM((unsigned int)time((time_t *)&seed)); #endif #if !defined (GLK) /* no command line under Glk */ ParseCommandLine(argc, argv); #endif hugo_init_screen(); #if defined (DEBUGGER) debug_getinvocationpath(argv[0]); SwitchtoGame(); #endif SetupDisplay(); strcpy(pbuffer, ""); gameseg = 0; LoadGame(); #if defined (DEBUGGER) LoadDebuggableFile(); StartDebugger(); #endif RunGame(); hugo_cleanup_screen(); hugo_blockfree(mem); mem = NULL; hugo_closefiles(); return 0; }
long FindResource(char *filename, char *resname) { char resource_in_file[MAX_RES_PATH]; int i, len; int resfileversion, rescount; unsigned int startofdata; long resposition, reslength; #if defined (GLK) frefid_t fref; #endif /* Previously, resource positions were written as 24 bits, which meant that a given resource couldn't start after 16,777,216 bytes or be more than that length. The new resource file format (designated by 'r') corrects this. */ int res_32bits = true; resource_file = NULL; strcpy(loaded_filename, filename); strcpy(loaded_resname, resname); if (!strcmp(filename, "")) strcpy(loaded_filename, resname); /* See if the file is supposed to be in a resourcefile to begin with */ if (!strcmp(filename, "")) goto NotinResourceFile; /* Open the resourcefile */ strupr(filename); #if !defined (GLK) /* stdio implementation */ if (!(resource_file = TrytoOpen(filename, "rb", "games"))) if (!(resource_file = TrytoOpen(filename, "rb", "object"))) { var[system_status] = STAT_NOFILE; return 0; } #else /* Glk implementation */ fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, filename, 0); if (glk_fileref_does_file_exist(fref)) resource_file = glk_stream_open_file(fref, filemode_Read, 0); else resource_file = NULL; glk_fileref_destroy(fref); if (!resource_file) { var[system_status] = STAT_NOFILE; return 0; } #endif /* Read the resourcefile header */ /* if (fgetc(resource_file)!='R') goto ResfileError; */ i = fgetc(resource_file); if (i=='r') res_32bits = true; else if (i=='R') res_32bits = false; else goto ResfileError; resfileversion = fgetc(resource_file); rescount = fgetc(resource_file); rescount += fgetc(resource_file)*256; startofdata = fgetc(resource_file); startofdata += (unsigned int)fgetc(resource_file)*256; if (ferror(resource_file)) goto ResfileError; /* Now skim through the list of resources in the resourcefile to see if we have a match */ for (i=1; i<=rescount; i++) { len = fgetc(resource_file); if (ferror(resource_file)) goto ResfileError; if (!(fgets(resource_in_file, len+1, resource_file))) goto ResfileError; resposition = (long)fgetc(resource_file); resposition += (long)fgetc(resource_file)*256L; resposition += (long)fgetc(resource_file)*65536L; if (res_32bits) { resposition += (long)fgetc(resource_file)*16777216L; } reslength = (long)fgetc(resource_file); reslength += (long)fgetc(resource_file)*256L; reslength += (long)fgetc(resource_file)*65536L; if (res_32bits) { reslength += (long)fgetc(resource_file)*16777216L; } if (ferror(resource_file)) goto ResfileError; if (!strcmp(resname, resource_in_file)) { if (fseek(resource_file, (long)startofdata+resposition, SEEK_SET)) goto ResfileError; return reslength; } } ResfileError: var[system_status] = STAT_NORESOURCE; #if defined (DEBUGGER) SwitchtoDebugger(); sprintf(debug_line, "Unable to find \"%s\" in \"%s\"", resname, filename); DebugMessageBox("Resource Error", debug_line); SwitchtoGame(); #endif fclose(resource_file); resource_file = NULL; /* If we get here, we've either been unable to find the named resource in the given resourcefile, or no resourcefile was given */ NotinResourceFile: #if !defined (GLK) /* stdio implementation */ if (!(resource_file = TrytoOpen(resname, "rb", "resource"))) if (!(resource_file = TrytoOpen(resname, "rb", "source"))) { if (!strcmp(filename, "")) var[system_status] = STAT_NOFILE; else var[system_status] = STAT_NORESOURCE; return 0; } #else /* Glk implementation */ fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, resname, 0); if (glk_fileref_does_file_exist(fref)) resource_file = glk_stream_open_file(fref, filemode_Read, 0); else resource_file = NULL; glk_fileref_destroy(fref); if (!resource_file) { if (!strcmp(filename, "")) var[system_status] = STAT_NOFILE; else var[system_status] = STAT_NORESOURCE; return 0; } #endif /* resource_file here refers to a resource in an individual on-disk file, not a consolidated resource file */ fseek(resource_file, 0, SEEK_END); reslength = ftell(resource_file); fseek(resource_file, 0, SEEK_SET); if (ferror(resource_file)) { fclose(resource_file); resource_file = NULL; return false; } return reslength; }