Use emglken 0.5.2

This commit is contained in:
He4eT 2023-05-20 23:39:01 +03:00
commit cb7d6e51f9
6 changed files with 49 additions and 41 deletions

View file

@ -4,11 +4,12 @@
* @see: https://github.com/curiousdannii/emglken/blob/master/bin/emglken.js * @see: https://github.com/curiousdannii/emglken/blob/master/bin/emglken.js
*/ */
const fs = require('fs') import { readFileSync } from 'fs'
const minimist = require('minimist') import minimist from 'minimist'
const CheapGlkOte = require('../src/') import CheapGlkOte from '../src/index.js'
const { handlers } = require('./stdio')
import { handlers } from './stdio.js'
const formats = [ const formats = [
{ {
@ -50,13 +51,19 @@ if (!format) {
process.exit(0) process.exit(0)
} }
const { glkInterface, sendFn } = CheapGlkOte(handlers) import(`emglken/src/${format.id}.js`)
handlers.setSend(sendFn) .then(({default: engine}) => engine)
.then((engine) => new engine())
.then((vm) => {
const cheapGlkOte = CheapGlkOte(handlers)
const engine = require('emglken/src/' + format.engine) handlers.setSend(cheapGlkOte.send)
const vm = new engine()
vm.prepare( vm.init(readFileSync(storyfile), {
fs.readFileSync(storyfile), Dialog: cheapGlkOte.Dialog,
glkInterface) GlkOte: cheapGlkOte.GlkOte,
vm.start() Glk: {},
wasmBinary: readFileSync(new URL(`../node_modules/emglken/build/${format.id}-core.wasm`, import.meta.url))
})
vm.start()
})

View file

@ -2,14 +2,14 @@
* @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-dumb.js * @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-dumb.js
*/ */
const readline = require('readline') import { createInterface, emitKeypressEvents } from 'readline'
const MuteStream = require('mute-stream') import MuteStream from 'mute-stream'
const ansiEscapes = require('ansi-escapes') import ansiEsc from 'ansi-escapes'
const stdin = process.stdin const stdin = process.stdin
const stdout = new MuteStream() const stdout = new MuteStream()
stdout.pipe(process.stdout) stdout.pipe(process.stdout)
const rl = readline.createInterface({ const rl = createInterface({
input: stdin, input: stdin,
output: stdout, output: stdout,
prompt: '' prompt: ''
@ -29,7 +29,7 @@ const onInit = () => {
if (stdin.isTTY) { if (stdin.isTTY) {
stdin.setRawMode(true) stdin.setRawMode(true)
} }
readline.emitKeypressEvents(stdin) emitKeypressEvents(stdin)
rl.resume() rl.resume()
clearScreen() clearScreen()
} }
@ -71,7 +71,7 @@ const drawBuffer = messages => {
if (x.style === 'input') { if (x.style === 'input') {
if (stdout.isTTY) { if (stdout.isTTY) {
stdout.write(ansiEscapes.eraseLines(2)) stdout.write(ansiEsc.eraseLines(2))
stdout.write('> ') stdout.write('> ')
} }
} }
@ -173,13 +173,13 @@ const detach_handlers = () => {
const handle_line_input = line => { const handle_line_input = line => {
if (stdout.isTTY) { if (stdout.isTTY) {
stdout.write(ansiEscapes.eraseLines(1)) stdout.write(ansiEsc.eraseLines(1))
} }
send(line, currentInputType, currentWindow) send(line, currentInputType, currentWindow)
detach_handlers() detach_handlers()
} }
module.exports.handlers = { export const handlers = {
onInit, onInit,
onUpdateWindows, onUpdateWindows,
onUpdateInputs, onUpdateInputs,

View file

@ -2,13 +2,14 @@
* @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-dumb.js * @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-dumb.js
*/ */
const GlkOte = require('./glkOte/glkote-term') import GlkOte from './glkOte/glkote-term.js'
class CheapGlkOte extends GlkOte { class CheapGlkOte extends GlkOte {
constructor(handlers, loggers, size) { constructor(handlers, loggers, size) {
super(size) super(size)
this.handlers = handlers this.handlers = handlers
this.loggers = loggers
} }
sendFn (message, type, window) { sendFn (message, type, window) {
@ -79,4 +80,4 @@ class CheapGlkOte extends GlkOte {
} }
} }
module.exports = CheapGlkOte export default CheapGlkOte

View file

@ -7,6 +7,7 @@ class FakeDialog {
constructor(handlers, loggers) { constructor(handlers, loggers) {
this.streaming = false this.streaming = false
this.handlers = handlers this.handlers = handlers
this.loggers = loggers
} }
file_ref_exists({ usage }) { file_ref_exists({ usage }) {
@ -15,11 +16,11 @@ class FakeDialog {
: false : false
} }
file_remove_ref (ref) { file_remove_ref () {
return true return true
} }
file_construct_ref(filename, usage, gameid) { file_construct_ref(filename, usage) {
return { return {
filename, filename,
usage: usage || '' usage: usage || ''
@ -52,4 +53,4 @@ class FakeDialog {
} }
} }
module.exports = FakeDialog export default FakeDialog

View file

@ -2,6 +2,8 @@
* @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-term.js * @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-term.js
*/ */
import packageJSON from '../../package.json' assert { type: 'json' }
class GlkOte { class GlkOte {
constructor({width, height}) { constructor({width, height}) {
this.width = width this.width = width
@ -11,7 +13,7 @@ class GlkOte {
this.disabled = false this.disabled = false
this.generation = 0 this.generation = 0
this.interface = null this.interface = null
this.version = require('../../package.json').version this.version = packageJSON.version
} }
measure_window() { measure_window() {
@ -149,4 +151,4 @@ class GlkOte {
} }
} }
module.exports = GlkOte export default GlkOte

View file

@ -1,5 +1,5 @@
const FakeDialog = require('./fakeDialog') import FakeDialog from './fakeDialog.js'
const CheapGlkOte = require('./cheapGlkOte') import CheapGlkOte from './cheapGlkOte.js'
const noop = () => void null const noop = () => void null
@ -12,39 +12,36 @@ const defaultHandlers = [
'onFileNameRequest', 'onFileNameRequest',
'onFileRead', 'onFileRead',
'onFileWrite', 'onFileWrite',
'onExit' 'onExit',
].reduce((acc, x) => ((acc[x] = noop), acc), {}) ].reduce((acc, x) => ((acc[x] = noop), acc), {})
const defaultLoggers = { const defaultLoggers = {
log: console.log, log: console.log,
warning: console.warn, warning: console.warn,
error: console.error error: console.error,
} }
const defaultSize = { const defaultSize = {
width: 80, width: 80,
height: 25 height: 25,
} }
module.exports = (handlers_, {loggers: loggers_, size: size_ } = {}) => { export default (handlers_, {loggers: loggers_, size: size_ } = {}) => {
const handlers = const handlers =
Object.assign({}, defaultHandlers, handlers_) Object.assign({}, defaultHandlers, handlers_)
const loggers = const loggers =
Object.assign({}, defaultLoggers, size_) Object.assign({}, defaultLoggers, loggers_)
const size = const size =
Object.assign({}, defaultSize, size_) Object.assign({}, defaultSize, size_)
const Dialog = new FakeDialog(handlers, loggers) const Dialog = new FakeDialog(handlers, loggers)
const GlkOte = new CheapGlkOte(handlers, loggers, size) const GlkOte = new CheapGlkOte(handlers, loggers, size)
const sendFn = GlkOte.sendFn.bind(GlkOte) const send = GlkOte.sendFn.bind(GlkOte)
return { return {
sendFn, Dialog,
glkInterface: { GlkOte,
Dialog, send,
GlkOte,
Glk: {}
}
} }
} }