/** Offline R9 captures: software renderer only, with isolated temporary app data. */
import { spawnSync } from 'node:child_process'
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { crop, decodePng, encodePng } from './bench/png'
const root = path.resolve(import.meta.dir, '..')
const out = path.join(root, 'out/r9')
const temp = mkdtempSync(path.join(tmpdir(), 'fmm-r9-'))
const data = path.join(temp, 'Factorio')
const settings = path.join(temp, 'settings')
const cache = path.join(temp, 'cache')
for (const dir of [out, settings, path.join(data, 'mods'), path.join(cache, 'portal-thumbs')]) mkdirSync(dir, { recursive: true })
writeFileSync(path.join(data, 'mods/mod-list.json'), '{"mods":[]}')
writeFileSync(path.join(settings, 'settings.json'), JSON.stringify({ dataDir: data }))
const save = (name: string, value: unknown) => {
const file = path.join(temp, name + '.json')
writeFileSync(file, JSON.stringify(value))
return file
}
const row = (name: string, title: string) => ({ name, title, version: '1.0.0', checked: true })
const normal = {
rows: [{ ...row('base', 'Base mod'), version: '2.0.77' }, { ...row('installed', 'Installed mod'), selected: true }],
'detail-title': 'Installed mod', description: 'Offline R9 fixture.',
info: [{ key: 'Mod portal', value: 'https://mods.factorio.com/mod/installed', link: true }, { key: 'Dependencies', dependency: true }],
dependencies: [{ name: 'base', text: 'base >= 2.0', installed: true }, { name: 'absent', text: 'absent (optional)', optional: true }],
}
const fixture = save('fixture', normal)
const downloads = save('downloads', {
...normal,
rows: [...normal.rows, ...[
{ name: 'queued', title: 'Queued dependency', download: true, progress_label: 'Queued' },
{ name: 'running', title: 'Running download', download: true, fraction: 0.46, progress_label: '46%' },
{ name: 'failed', title: 'Failed download', failed: true, progress_label: 'Mirror unavailable (HTTP 404)' },
].map(m => ({ ...m, version: '2.0.0', placeholder: true, locked: true, dimmed: true }))],
})
const full = save('full', { name: 'installed', title: 'Installed mod', owner: 'Fixture author',
homepage: 'https://mods.factorio.com/mod/installed', description: 'Offline link fixture.',
latest_release: { version: '1.0.0', info_json: { factorio_version: '2.0' } },
releases: [{ version: '1.0.0', info_json: { factorio_version: '2.0', dependencies: ['base >= 2.0', '? absent'] } }],
})
const key = (s: string) => { let h = 0xcbf29ce484222325n; for (const b of Buffer.from(s)) h = BigInt.asUintN(64, (h ^ BigInt(b)) * 0x100000001b3n); return h.toString(16).padStart(16, '0') }
const imageUrl = 'https://assets-mod.factorio.com/assets/r9-gallery.png'
copyFileSync(path.join(root, 'assets/game/icons1x/space-age.png'), path.join(cache, 'portal-thumbs', key(imageUrl) + '.png'))
const gallery = save('gallery', { ...JSON.parse(readFileSync(full, 'utf8')), images: [{ thumbnail: imageUrl, url: imageUrl }] })
const catalogue = save('catalogue', { results: [JSON.parse(readFileSync(full, 'utf8'))] })
let log = ''
function capture(name: string, args: string[] = [], file = fixture, size = '1440x945') {
const result = spawnSync(path.join(root, process.platform === 'win32' ? 'target/release/fmm.exe' : 'target/release/fmm'), [
'--screenshot', path.join(out, name + '.png'), '--fixture', file, '--size', size, '--catalogue', catalogue, ...args,
], { cwd: root, env: { ...process.env, FACTORIO_DIR: '', FMM_CACHE_DIR: path.join(temp, 'cache'), FMM_SETTINGS_DIR: settings, FMM_METADATA_DIR: path.join(temp, 'metadata') }, windowsHide: true, encoding: 'utf8' })
log += `${name}: ${result.status}\n${result.stdout}${result.stderr}\n`
writeFileSync(path.join(out, 'captures.log'), log)
if (result.error) throw result.error
if (result.status !== 0) throw new Error(log)
console.log(name)
}
for (const view of ['table', 'rows', 'grid']) capture('manage-downloads-' + view, ['--view', view], downloads)
// Project pending state through the controller, rather than painting fixture flags.
const pendingMods = [
['normal', 'Normal saved mod'], ['toggled', 'Pending toggle enabled'],
['unlisted', 'Pending unlisted install'], ['selected', 'Selected pending toggle'],
['disabled', 'Pending toggle disabled'],
].map(([name, title]) => ({ name: name!, title: title!, version: '1.0.0', author: 'Offline fixture',
description: 'Pending changes use the game blue. Selection keeps the orange surface.',
factorioVersion: '2.0', dependencies: [], source: 'folder', path: path.join(data, 'mods', name!), thumbnail: null }))
for (const mod of pendingMods) {
mkdirSync(mod.path, { recursive: true })
writeFileSync(path.join(mod.path, 'info.json'), JSON.stringify({ name: mod.name, version: mod.version }))
}
const pendingList = { mods: [
{ name: 'base', enabled: true }, ...pendingMods.filter(m => m.name !== 'unlisted').map(m => ({ name: m.name, enabled: m.name !== 'toggled' })),
] }
const pending = save('pending', { snapshot: {
paths: { installDir: path.join(temp, 'install'), userDir: data, modsDir: path.join(data, 'mods'), modListPath: path.join(data, 'mods/mod-list.json'), gameVersion: '2.0.77' },
builtin: [], mods: pendingMods, modList: pendingList, scanErrors: [],
}, select: 'selected', toggles: ['toggled', 'selected', 'disabled'] })
for (const view of ['table', 'rows', 'grid']) capture('manage-pending-' + view, ['--view', view], pending)
for (const view of ['table', 'rows', 'grid']) capture('manage-pending-' + view + '-minimum', ['--view', view], pending, '1000x600')
capture('window-rest')
for (const [name, x] of [['minimize', 1332], ['maximize', 1368], ['close', 1404]] as const) {
capture('window-hover-' + name, ['--pointer', `${x},25`])
capture('window-pressed-' + name, ['--pointer', `${x},25`, '--pointer-press', '1'])
}
capture('title-tooltip', ['--pointer', '40,25'])
for (const tab of ['manage', 'explore', 'updates']) capture('links-' + tab, ['--tab', tab, '--portal-full', full, '--explore-select', 'installed', '--updates-select', 'installed', '--portal-updates', catalogue])
for (const tab of ['manage', 'updates']) capture(tab + '-minimum', ['--tab', tab, '--portal-updates', catalogue], fixture, '1000x600')
const changed = save('changed', { ...normal, 'pending-count': 1 })
capture('save-visible', [], changed)
capture('save-visible-minimum', [], changed, '1000x600')
capture('save-hidden')
capture('dependency-tools', [], fixture, '1000x600')
capture('explore-manage-button', ['--tab', 'explore', '--portal-full', full, '--explore-select', 'installed'], fixture, '1000x600')
capture('lightbox-hover-close', ['--tab', 'explore', '--portal-full', gallery, '--explore-select', 'installed', '--lightbox', '0', '--lightbox-zoom', '8', '--pointer', '968,70'], fixture, '1000x600')
const rest = decodePng(readFileSync(path.join(out, 'window-rest.png')))
const links = decodePng(readFileSync(path.join(out, 'links-explore.png')))
writeFileSync(path.join(out, 'url-prefix.png'), encodePng(crop(links, 958, 170, 120, 28)))
writeFileSync(path.join(out, 'window-controls.png'), encodePng(crop(rest, 1208, 4, 224, 42)))
writeFileSync(path.join(out, 'window-edge.png'), encodePng(crop(rest, 0, 0, 120, 60)))
for (const [x, y] of [[0, 0], [2, 20], [1439, 20], [20, 944]]) {
const pixel = rest.data.subarray((y! * rest.width + x!) * 4, (y! * rest.width + x!) * 4 + 4)
if (pixel[0] === 0 && pixel[1] === 0 && pixel[2] === 0) throw new Error(`Black window margin at ${x},${y}`)
}
writeFileSync(path.join(out, 'capture-data.txt'), temp)
capture('settings-sounds', ['--settings-open', '1'])
capture('settings-sounds-minimum', ['--settings-open', '1'], fixture, '1000x600')
writeFileSync(path.join(settings, 'settings.json'), JSON.stringify({ dataDir: data, uiSoundsEnabled: false, uiSoundVolume: 50 }))
capture('settings-sounds-disabled', ['--settings-open', '1'], fixture, '1000x600')
writeFileSync(path.join(settings, 'settings.json'), JSON.stringify({ dataDir: data, uiSoundsEnabled: true, uiSoundVolume: 0 }))
capture('settings-sounds-zero', ['--settings-open', '1'], fixture, '1000x600')