QSP wasm engine
QSP code wasm bindings that allow to run QSP engine in browser or on Node.js server.
Installation
npm install --save @qsp/wasm-engine
Usage
This library has separate builds for running in browser or on server.
Use
import { initQspEngine } from '@qsp/wasm-engine';
import qspWasmUrl from '@qsp/wasm-engine/qsp-engine.wasm';
const wasm = await fetch(wasmPath).then((r) => r.arrayBuffer());
const api = await init(qspWasmUrl);
or
const { initQspEngine } = require('@qsp/wasm-engine');
const fsp = require('fs/promises');
const wasm = await fsp.readFile(require.resolve('@qsp/wasm-engine/qsp-engine.wasm'));
const api = await initQspEngine(wasm.buffer);
depending on your environment.
As first argument you need to pass URL of wasm module in browser (you might need to configure your bundler to handle wasm module as URL) or path to wasm module in node.
initQspEngine
returns a promise that resolves with API wrapper abstracting away all low level interactions with wasm.
API
api.on(event: string, callback: Function): void;
api.off(event: string, callback: Function): void;
api.version(): string;
api.openGame(data: ArrayBuffer, isNewGame: boolean): void;
api.saveGame(): ArrayBuffer | null;
api.loadSave(data: ArrayBuffer): void;
api.restartGame(): void;
api.selectAction(index: number): void;
api.execSelectedAction(): void;
api.selectObject(index: number): void;
api.updateUserInput(code: string): void;
api.execCode(code: string): void;
api.execCounter(): void;
api.execLoc(name: string): void;
api.readVariable(name: string): string | number | QspTuple;
api.readVariableByIndex(name: string, index: number): string | number | QspTuple;
api.readVariableByKey(name: string, key: string): string | number | QspTuple;
api.readVariableSize(name: string): number;
api.watchVariable(
name: string,
callback: (value: string | number | QspTuple) => void
): () => void;
api.watchVariable(
name: string,
index: number,
callback: (value: string | number | QspTuple) => void
): () => void;
api.watchVariableByKey(
name: string,
key: string,
callback: (value: string | number | QspTuple) => void
): () => void;
api.enableDebugMode(): void;
api.disableDebugMode(): void;
api.getLocationsList(): string[];
api.getLocationCode(name: string): string[];
api.getActionCode(location: string, index: number): string[];
Engine events
API triggers several events when engine provides updates on current state of game or needs to receive some input from user.
Here is the list of currently supported events.
main_changed
- event is triggered whenever text in main panel changes.
Arguments:
text
- current content of main panel
stats_changed
- event is triggered whenever text in stats panel changes.
Arguments:
text
- current content of stats panel
actions_changed
- event is triggered whenever list of actions changes.
Arguments:
actions
- array of actions (every action has two fields - name
and image
)
objects_changed
- event is triggered whenever list of objects changes.
Arguments:
objects
- array of objects (every object has two fields - name
and image
)
panel_visibility
- event is triggered whenever one of panels is shown/hidden.
Arguments:
type
- code of panel that changed
isShown
- if panel is shown now
user_input
- event is triggered whenever user input is changed (either from game of using api.updateUserInput
)
Arguments:
text
- current user input
menu
- event is triggered when MENU
operator is called in game
Arguments:
items
- array of menu items (every object has two fields - name
and image
)
callback
- function to be called after user selected some menu item (passing index). -1
should be passed ff no item has been selected.
msg
- event is triggered when MSG
operator is called in game
Arguments:
text
- content of message
callback
- function to be called when game should resume (pressing ok in MSG dialog)
input
- event is triggered when INPUT
function is called in game
Arguments:
text
- content of message
callback
- function to be called when game should resume (pressing ok in MSG dialog). Text entered by user should be passed as first parameter.
wait
- event is triggered when WAIT
operator is called in game
Arguments:
ms
- delay of milliseconds
callback
- function to be called after delay signaling that game can resume
timer
- event is triggered when SETTIMER
operator is called in game
Arguments:
ms
- delay of milliseconds between counter calls
view
- event is triggered whenever VIEW
operator is called in game
Arguments:
path
- path to image to be shown (may be empty string)
version
- event is triggered when $QSPVER
function is called in game
Arguments:
type
- type of version to be returned (currently player
or platform
)
callback
- function to be called passing needed version data
open_game
- event is triggered when OPENQST
or INCLIB
operator is called in game
Arguments:
path
- path to game file
isNewGame
- true for OPENQST
and false for INCLIB
callback
- function to be called after game data was loaded into engine using api.openGame
save_game
- event is triggered when SAVEGAME
operator is called in game
Arguments:
path
- path where game should be saved (may be empty - it is expected that player asks user to provide this path in such case)
callback
- function to be called after game state was received using api.saveGame
and stored at needed path
load_save
- event is triggered when OPENGAME
operator is called in game
Arguments:
path
- path to save (may be empty - it is expected that player asks user to provide this path in such case)
callback
- function to be called after game state has been loaded using api.loadSave
is_play
- event is triggered when ISPLAY
function is called in game
Arguments:
path
- path to audio file
callback
- function to be called with boolean argument indicating whether corresponding audio file is currently playing
play_file
- event is triggered when PLAY
operator is called in game
Arguments:
path
- path to audio file
volume
- current volume (if file is already playing should just change its volume)
callback
- function to be called when player is ready with changes
close_file
- event is triggered when CLOSE
operator is called in game
Arguments:
path
- path to audio file (empty string if CLOSE ALL
was called)
callback
- function to be called when player is ready with changes
system_cmd
- event is triggered when EXEC
operator is called in game
Arguments:
cmd
- command string (depends on player)
error
- event is triggered whenever error has happened while executing QSP code:
Arguments:
errorData
- object containing information about error
debug
- when debug mode is on this event will be fired fr every executed string
Arguments:
debugRecord
- object contaning current line, location name and action index
resume
- funtion to be executed to resume execution