/* Triforce launcher: the page. It draws, plays the sounds and takes the mouse, keyboard and controller; everything real (reading the game, building, updating, saves, mods, settings) is the launcher's, behind messages: page -> launcher {t:'ready'} {t:'win', a:'min'|'max'|'close'} {t:'pick', kind:'file'|'folder'} {t:'drop', files:[file:// on Linux]} {t:'cmd', c:, ...args} (build, stop, retry, play, update, playold, showgame, open, lang, folder, setting, saves.import, saves.export, saves.open, mods.*, bench, benchstop, benchapply, report, sound, color, workback, ...) launcher -> page {t:'hello', chrome, os} {t:'state', ...parts} {t:'go', s} {t:'check', st, name, msg} {t:'work', ...} {t:'log', lines} {t:'toast', html} {t:'sfx', id} {t:'bench', ...} {t:'max', on} Without a launcher (a plain browser) demo.js answers instead, so the design can be looked at anywhere. */ 'use strict'; const $ = (s, r = document) => r.querySelector(s), $$ = (s, r = document) => [...r.querySelectorAll(s)]; const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); /* ---------------- the window this page lives in ---------------- */ const HOST = window.chrome && window.chrome.webview ? 'win' : (window.external && typeof window.external.sendMessage === 'function' ? 'linux' : null); const hostHandlers = []; function onHost(fn) { hostHandlers.push(fn); } function fromHost(s) { let m; try { m = typeof s === 'string' ? JSON.parse(s) : s; } catch { return; } hostHandlers.forEach(f => { try { f(m); } catch (e) { console.error(e); } }); } function send(o) { const s = JSON.stringify(o); if (HOST === 'win') window.chrome.webview.postMessage(s); else if (HOST === 'linux') window.external.sendMessage(s); else if (window.DEMO) window.DEMO.receive(o, fromHost); } function cmd(c, args) { send(Object.assign({ t: 'cmd', c }, args || {})); } if (HOST === 'win') window.chrome.webview.addEventListener('message', e => fromHost(e.data)); else if (HOST === 'linux') window.external.receiveMessage(fromHost); document.documentElement.classList.add(HOST ? 'hosted' : 'browser', 'noart'); // (no game art until the launcher says there is) if (HOST) document.documentElement.classList.add('host-' + HOST); window.addEventListener('error', e => send({ t: 'log', s: 'page error: ' + e.message + ' @' + e.lineno })); /* ---------------- fit the 1280x800 design into the window ---------------- */ function fit() { const k = Math.min(innerWidth / 1280, innerHeight / 800); $('#fit').style.transform = `translate(-50%,-50%) scale(${k})`; } addEventListener('resize', fit); fit(); /* ---------------- the launcher's state, as last sent ---------------- */ const S = { media: { art: false, music: false, sfx: false }, sound: { music: .4, sfx: .7, muted: false }, files: [], settings: [], mods: { tab: 'browse', items: [] } }; /* ---------------- sound: the game's own music and menu sounds (only once a game is known) ---------------- */ const TRACKS = { saria: "Saria's Song", fairy: 'Fairy Fountain', temple: 'Temple of Time', itemget: 'Item get' }; /* Music plays through Web Audio: a track is read and decoded once into an AudioBuffer and played by a buffer source that loops between the game's own loop points, sample for sample, so an intro plays once and the loop joins with no gap and no jump back to the start (the launcher writes the loop into the WAV as a smpl chunk: launcher/Core/GameFiles/Bwav.cs ToWav). An