/** Offline polish captures, with all writable app data isolated in OS temporary directories. */
import { spawnSync } from 'node:child_process'
import { 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/polish')
const temp = mkdtempSync(path.join(tmpdir(), 'fmm-polish-'))
const data = path.join(temp, 'Factorio')
const install = path.join(temp, 'install')
const settings = path.join(temp, 'settings')
const cache = path.join(temp, 'cache')
const metadata = path.join(temp, 'metadata')
for (const dir of [out, settings, metadata, path.join(cache, 'portal-thumbs'), path.join(data, 'mods'), path.join(install, 'data/base')]) mkdirSync(dir, { recursive: true })
writeFileSync(path.join(data, 'mods/mod-list.json'), '{"mods":[]}')
writeFileSync(path.join(install, 'data/base/info.json'), '{"version":"2.0.77"}')
const saveSettings = (verifyDownloads: boolean) => writeFileSync(path.join(settings, 'settings.json'), JSON.stringify({ dataDir: data, installDir: install, verifyDownloads }))
saveSettings(true)
const save = (name: string, value: unknown) => {
const file = path.join(temp, name + '.json')
writeFileSync(file, JSON.stringify(value))
return file
}
const key = (s: string) => {
let hash = 0xcbf29ce484222325n
for (const byte of Buffer.from(s)) hash = BigInt.asUintN(64, (hash ^ BigInt(byte)) * 0x100000001b3n)
return hash.toString(16).padStart(16, '0')
}
const icon = decodePng(readFileSync(path.join(root, 'assets/game/icons1x/space-age.png')))
const thumbnails = [icon, crop(icon, 0, 0, icon.width, Math.floor(icon.height / 2)), crop(icon, 0, 0, Math.floor(icon.width / 2), icon.height)]
const results = ['square', 'landscape', 'portrait', 'missing', 'square-two', 'landscape-two'].map(name => {
const thumbnail = `/assets/polish-${name}.png`
const shape = name.startsWith('landscape') ? 1 : name === 'portrait' ? 2 : 0
if (name !== 'missing') writeFileSync(path.join(cache, 'portal-thumbs', key('https://assets-mod.factorio.com' + thumbnail) + '.png'), encodePng(thumbnails[shape]!))
return {
name, title: name[0]!.toUpperCase() + name.slice(1) + ' thumbnail', owner: 'Fixture author',
thumbnail: name === 'missing' ? null : thumbnail, summary: 'Offline fixture for proportional, centred thumbnails above the download strip.',
category: 'utilities', downloads_count: 12000,
latest_release: { version: '1.0.0', info_json: { factorio_version: '2.0', dependencies: ['base'] } },
}
})
const catalogue = save('catalogue', { results })
const builtin = [{
name: 'base', title: 'Base mod', version: '2.0.77', source: 'builtin', dependencies: [],
author: 'Wube Software', description: '', factorioVersion: '2.0', path: path.join(install, 'data/base'), thumbnail: null,
}]
const local = { ...builtin[0], name: 'installed', title: 'Installed mod', version: '1.0.0', source: 'zip', path: path.join(data, 'mods/installed_1.0.0.zip') }
const fixture = save('fixture', {
snapshot: {
paths: { installDir: install, userDir: data, modsDir: path.join(data, 'mods'), modListPath: path.join(data, 'mods/mod-list.json'), gameVersion: '2.0.77' },
builtin, mods: [local], scanErrors: [], modList: { mods: [{ name: 'base', enabled: true }, { name: 'installed', enabled: true }] },
}, select: 'installed',
})
let log = ''
function capture(name: string, args: string[] = [], size = '1440x945') {
const result = spawnSync(path.join(root, 'target/release/fmm.exe'), [
'--screenshot', path.join(out, name + '.png'), '--fixture', fixture, '--size', size, '--catalogue', catalogue, ...args,
], {
cwd: root, windowsHide: true, encoding: 'utf8',
env: { ...process.env, FACTORIO_DIR: '', FMM_CACHE_DIR: cache, FMM_SETTINGS_DIR: settings, FMM_METADATA_DIR: metadata },
})
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)
}
capture('titlebar-rest')
capture('titlebar-hover-close', ['--pointer', '1404,25'])
capture('titlebar-pressed-close', ['--pointer', '1404,25', '--pointer-press', '1'])
capture('trash-hover', ['--pointer', '1406,64'])
capture('trash-pressed', ['--pointer', '1406,64', '--pointer-press', '1'])
capture('settings', ['--settings-open', '1'])
capture('settings-minimum', ['--settings-open', '1'], '1000x600')
saveSettings(false)
capture('settings-unchecked', ['--settings-open', '1'])
saveSettings(true)
capture('onboarding', ['--onboarding-open', '1'])
capture('explore-grid', ['--tab', 'explore', '--view', 'grid'])
capture('explore-grid-wide', ['--tab', 'explore', '--view', 'grid'], '1920x945')
capture('explore-grid-minimum', ['--tab', 'explore', '--view', 'grid'], '1000x600')
for (const name of ['titlebar-rest', 'titlebar-hover-close', 'titlebar-pressed-close']) {
const image = decodePng(readFileSync(path.join(out, name + '.png')))
writeFileSync(path.join(out, name + '-crop.png'), encodePng(crop(image, 1208, 4, 224, 42)))
}
for (const name of ['titlebar-rest', 'trash-hover', 'trash-pressed']) {
const image = decodePng(readFileSync(path.join(out, name + '.png')))
writeFileSync(path.join(out, name + '-trash-crop.png'), encodePng(crop(image, 1272, 44, 152, 42)))
}
writeFileSync(path.join(out, 'capture-data.txt'), temp)