/** thread's run() method that does all the work in the right order */ public void run() { try { if (log != null) log.println( "-------------------\ncartridge " + gwcfile.name + " started (openWIG r" + VERSION + ")\n-------------------"); prepareState(); if (doRestore) restoreGame(); else newGame(); loglevel = LOG_PROP; ui.debugMsg("Starting game...\n"); ui.start(); player.refreshLocation(); cartridge.callEvent(doRestore ? "OnRestore" : "OnStart", null); ui.refresh(); eventRunner.unpause(); mainloop(); } catch (IOException e) { ui.showError("Could not load cartridge: " + e.getMessage()); } catch (Throwable t) { stacktrace(t); } finally { ui.end(); } }
/** creates a new global Engine instance */ public static Engine newInstance( CartridgeFile cf, OutputStream log, UI ui, LocationService service) throws IOException { ui.debugMsg("Creating engine...\n"); Engine.ui = ui; Engine.gps = service; instance = new Engine(cf, log); return instance; }
/** invokes creation of clean new game environment */ private void newGame() throws IOException { // starting game normally ui.debugMsg("Loading gwc..."); if (gwcfile == null) throw new IOException("invalid cartridge file"); ui.debugMsg("pre-setting properties..."); player.rawset("CompletionCode", gwcfile.code); player.rawset("Name", gwcfile.member); ui.debugMsg("loading code..."); byte[] lbc = gwcfile.getBytecode(); ui.debugMsg("parsing..."); LuaClosure closure = LuaPrototype.loadByteCode(new ByteArrayInputStream(lbc), state.getEnvironment()); ui.debugMsg("calling...\n"); state.call(closure, null, null, null); lbc = null; closure = null; }
/** prepares Lua state and some bookkeeping */ protected void prepareState() throws IOException { ui.debugMsg("Creating state...\n"); state = new LuaState(System.out); /*write("Registering base libs...\n"); BaseLib.register(state); MathLib.register(state); StringLib.register(state); CoroutineLib.register(state); OsLib.register(state);*/ ui.debugMsg("Building javafunc map...\n"); savegame.buildJavafuncMap(state.getEnvironment()); ui.debugMsg("Loading stdlib..."); InputStream stdlib = getClass().getResourceAsStream("/cz/matejcik/openwig/stdlib.lbc"); LuaClosure closure = LuaPrototype.loadByteCode(stdlib, state.getEnvironment()); ui.debugMsg("calling...\n"); state.call(closure, null, null, null); stdlib.close(); stdlib = null; ui.debugMsg("Registering WIG libs...\n"); WherigoLib.register(state); ui.debugMsg("Building event queue...\n"); eventRunner = new BackgroundRunner(true); eventRunner.setQueueListener( new Runnable() { public void run() { ui.refresh(); } }); }
/** invokes game restore */ private void restoreGame() throws IOException { ui.debugMsg("Restoring saved state..."); cartridge = new Cartridge(); savegame.restore(state.getEnvironment()); }