Update the player to work with new versions of the Emglken and the cheap-glkote

This commit is contained in:
He4eT 2023-05-22 01:39:26 +03:00 committed by Alexey
commit b08d9725d4
4 changed files with 33 additions and 13 deletions

View file

@ -38,6 +38,6 @@
}, },
"staticFiles": { "staticFiles": {
"staticPath": "node_modules/emglken/build", "staticPath": "node_modules/emglken/build",
"staticOutPath": "node_modules/emglken/build" "staticOutPath": "emglken"
} }
} }

View file

@ -18,18 +18,23 @@ const INITIAL_STATUS = {
details: ['Preparing'], details: ['Preparing'],
} }
const runMachine = ({ engine: Engine, file, handlers }) => { const runMachine = ({ engine: Engine, wasmBinary, storyfile, handlers }) => {
const { Dialog, GlkOte, send } = CheapGlkOte(handlers)
const vm = new Engine() const vm = new Engine()
const { glkInterface, sendFn } = CheapGlkOte(handlers)
vm.prepare(file, glkInterface) vm.init(storyfile, {
Dialog,
GlkOte,
Glk: {},
wasmBinary,
})
vm.start() vm.start()
return { sendFn, instance: vm } return { send, instance: vm }
} }
export default function Player ({ export default function Player ({
vmParts: { file, engine }, singleWindow, vmParts: { storyfile, engine, wasmBinary }, singleWindow,
}) { }) {
const [status, setStatus] = useState(INITIAL_STATUS) const [status, setStatus] = useState(INITIAL_STATUS)
@ -44,7 +49,8 @@ export default function Player ({
useEffect(() => { useEffect(() => {
const vm = runMachine({ const vm = runMachine({
engine, engine,
file, wasmBinary,
storyfile,
handlers: Handlers({ handlers: Handlers({
setStatus, setStatus,
setWindows, setWindows,
@ -55,11 +61,11 @@ export default function Player ({
}) })
setVm(vm) setVm(vm)
}, [file, engine]) }, [storyfile, engine, wasmBinary])
useEffect(() => { useEffect(() => {
setSendMessage(() => vm setSendMessage(() => vm
? vm.sendFn ? vm.send
: null) : null)
}, [vm]) }, [vm])

View file

@ -22,6 +22,10 @@ const prepareVM = ({ url, setStatus, setParts }) => {
? url.replace(/#(.*)$/g, '') ? url.replace(/#(.*)$/g, '')
: url : url
const fetchWasm = (wasmBinaryName) =>
fetch(wasmBinaryName)
.then((response) => response.arrayBuffer())
return Promise.resolve(url) return Promise.resolve(url)
.then(st('loading', 'Downloading file')) .then(st('loading', 'Downloading file'))
.then(cleanUrl) .then(cleanUrl)
@ -30,9 +34,15 @@ const prepareVM = ({ url, setStatus, setParts }) => {
.then((response) => response.arrayBuffer()) .then((response) => response.arrayBuffer())
.then((arrayBuffer) => new Uint8Array(arrayBuffer)) .then((arrayBuffer) => new Uint8Array(arrayBuffer))
.then(st('loading', 'Downloading engine')) .then(st('loading', 'Downloading engine'))
.then((file) => setParts({ .then((storyfile) => {
file, let parts = engineByFilename(url)
engine: engineByFilename(url), return [storyfile, parts.engine, parts.wasmBinaryName]
})
.then(([storyfile, engine, wasmBinaryName]) => Promise.all([
storyfile, engine, fetchWasm(wasmBinaryName),
]))
.then(([storyfile, engine, wasmBinary]) => setParts({
storyfile, engine, wasmBinary,
})) }))
.then(st('loading', 'Running')) .then(st('loading', 'Running'))
.catch((e) => { .catch((e) => {

View file

@ -31,7 +31,11 @@ export const engineByFilename = (filename) => {
x.extensions.test(filename)) x.extensions.test(filename))
if (format) { if (format) {
return format.engine return {
...format,
/* @see staticFiles in package.json */
wasmBinaryName: `emglken/${format.id}-core.wasm`,
}
} }
throw new Error('Unsupported file type') throw new Error('Unsupported file type')
} }