/**
* Copy the game's GUI assets the fidelity work needs into assets/game/ (the used build/runtime subset is tracked).
* Read-only on the install; safe to run any number of times (every output is rewritten).
*
* bun tools/extract-assets.ts FACTORIO_DIR overrides the install path
*
* - assets/game/sprites/<style>[-<part>].png: regions of data/core/graphics/gui-new.png and
* icon files, named after the style.lua style that uses them (list: SPRITES below; sources and
* sources in docs/game-assets.md).
* - assets/game/sprites/index.json: per sprite its source file, rect, corner_size/draw_type and the
* sprite scale (gui-new.png is drawn at 0.5 × UI scale, so 8 atlas px = 4 px at 100%, 5 px at 125%).
* - assets/game/missing-thumbnail.png, assets/game/icons/mod-manager/*.png, assets/game/fonts/TitilliumWeb-*.ttf.
*/
import { createHash } from 'node:crypto'
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { findFactorio } from '../src/core/paths'
import { crop, decodePng, encodePng, type Image } from './bench/png'
export type Rect = [number, number, number, number]
export interface SpriteSpec {
/** Output name: `<style-name>` or `<style-name>-<part>`. */
name: string
/** Path relative to data/core/graphics. */
file: string
rect: Rect
/** 9-slice corner size in source px, when style.lua draws the region as a 9-slice. */
corner?: number
draw?: 'inner' | 'outer'
/** Source px → logical px at UI scale 1. gui-new.png regions use default_sprite_scale = 0.5. */
scale: number
/** style.lua line of the style or helper that defines the region. */
line: number
}
const ATLAS = 'gui-new.png'
/** A corner_size-8 9-slice at (x, y): 17×17 source px (8 + 1 centre + 8). */
function nine(name: string, x: number, y: number, line: number, draw?: 'outer'): SpriteSpec {
return { name, file: ATLAS, rect: [x, y, 17, 17], corner: 8, draw, scale: 0.5, line }
}
function atlas(name: string, rect: Rect, line: number): SpriteSpec {
return { name, file: ATLAS, rect, scale: 0.5, line }
}
/** An icon PNG (mip icons keep mip 0 only; the smaller mips sit to its right). */
function icon(name: string, file: string, size: number, scale: number, line: number): SpriteSpec {
return { name, file, rect: [0, 0, size, size], scale, line }
}
// Arrow tiles of back_button / confirm_button (style.lua l.36-45, 164-220): index 0 idle, 1 disabled,
// 2 hovered, 3 clicked; each index moves the arrow 96 px right and the composition edge 17 px right.
const ARROW_STATES = ['', '-disabled', '-hovered', '-clicked'] as const
function arrows(): SpriteSpec[] {
const out: SpriteSpec[] = []
ARROW_STATES.forEach((state, i) => {
const a = i * 96, c = i * 17
out.push(atlas(`back_button${state}-left`, [0 + a, 232, 24, 64], 164))
out.push(atlas(`back_button${state}-center`, [24 + a, 232, 1, 64], 164))
out.push(atlas(`back_button${state}-right`, [9 + c, 17, 8, 17], 164))
out.push(atlas(`confirm_button${state}-right`, [24 + a, 296, 24, 64], 193))
out.push(atlas(`confirm_button${state}-center`, [24 + a, 296, 1, 64], 193))
out.push(atlas(`confirm_button${state}-left`, [68 + c, 17, 8, 17], 193))
})
return out
}
export const SPRITES: SpriteSpec[] = [
// c01 window frame, title and footer filler
nine('frame', 0, 0, 5280),
atlas('draggable_space-top', [192, 8, 8, 7], 8807),
atlas('draggable_space-center', [200, 8, 8, 8], 8807),
atlas('draggable_space-bottom', [208, 8, 8, 8], 8807),
// c02/c03 panels
nine('inside_deep_frame', 17, 0, 5922, 'outer'),
atlas('inside_deep_frame-center', [42, 8, 1, 1], 5922),
nine('inside_shallow_frame', 17, 0, 6009, 'outer'),
atlas('inside_shallow_frame-center', [76, 8, 1, 1], 6009),
nine('deep_frame_in_shallow_frame', 85, 0, 6069, 'outer'),
atlas('tabbed_pane_frame', [76, 0, 1, 17], 8433),
atlas('subheader_frame', [256, 25, 1, 9], 5679),
atlas('subfooter_frame', [256, 17, 1, 9], 5788),
atlas('top_glow', [208, 128, 1, 9], 320),
atlas('bottom_glow', [208, 136, 1, 9], 331),
nine('default_inner_shadow', 183, 128, 244),
nine('default_glow', 200, 128, 255, 'outer'),
// c04 tabs
nine('tab', 102, 0, 7637),
nine('tab-disabled', 119, 0, 7637),
nine('tab-selected', 136, 0, 7637),
nine('tab-hovered', 153, 0, 7637),
nine('tab-clicked', 170, 0, 7637),
nine('tab-badge', 176, 72, 7637),
nine('tab-badge-selected', 296, 71, 7637),
atlas('tab_glow-left_top', [216, 0, 16, 16], 292),
atlas('tab_glow-right_top', [232, 0, 16, 16], 292),
// c05 buttons (grey / green / red), also dropdown, list_box_item and table row states
nine('button', 0, 17, 1015),
nine('button-disabled', 17, 17, 1015),
nine('button-hovered', 34, 17, 1015),
nine('button-clicked', 51, 17, 1015),
nine('button-selected', 225, 17, 1015),
nine('button-selected_clicked', 352, 17, 1015),
nine('button-selected_hovered', 369, 17, 1015),
nine('green_button', 68, 17, 1078),
nine('green_button-disabled', 85, 17, 1078),
nine('green_button-hovered', 102, 17, 1078),
nine('green_button-clicked', 119, 17, 1078),
nine('red_button', 136, 17, 1270),
nine('red_button-disabled', 153, 17, 1270),
nine('red_button-hovered', 170, 17, 1270),
nine('red_button-clicked', 187, 17, 1270),
// c06 dialog buttons and their glow (back_button_glow / forward_button_glow, l.82/96)
...arrows(),
atlas('back_button-glow-left', [304, 424, 32, 80], 82),
atlas('forward_button-glow-right', [336, 424, 32, 80], 96),
atlas('dialog_button-glow-center', [336, 424, 1, 80], 82),
// c08 checkbox
atlas('checkbox', [0, 132, 28, 28], 7431),
atlas('checkbox-disabled', [28, 132, 28, 28], 7431),
atlas('checkbox-selected', [56, 132, 28, 28], 7431),
atlas('checkbox-clicked', [84, 132, 28, 28], 7431),
atlas('checkbox-checkmark', [112, 132, 28, 28], 7431),
atlas('checkbox-disabled_checkmark', [456, 188, 28, 28], 7431),
atlas('checkbox-intermediate_mark', [456, 160, 28, 28], 7431),
// c09 mod table
nine('table_with_selection-row', 208, 17, 4654),
nine('mods_scroll_pane-background', 282, 17, 586),
nine('mods_explore_results_table-row', 68, 0, 4679),
icon('table-sort-arrow-down-active', 'arrows/table-header-sort-arrow-down-active.png', 16, 0.5, 4529),
icon('table-sort-arrow-down-hover', 'arrows/table-header-sort-arrow-down-hover.png', 16, 0.5, 4529),
icon('table-sort-arrow-down-white', 'arrows/table-header-sort-arrow-down-white.png', 16, 0.5, 4529),
icon('table-sort-arrow-up-active', 'arrows/table-header-sort-arrow-up-active.png', 16, 0.5, 4529),
icon('table-sort-arrow-up-hover', 'arrows/table-header-sort-arrow-up-hover.png', 16, 0.5, 4529),
icon('table-sort-arrow-up-white', 'arrows/table-header-sort-arrow-up-white.png', 16, 0.5, 4529),
// c10 details table grid (border_image_set, l.649)
atlas('bordered_table-border', [0, 40, 120, 8], 649),
atlas('bordered_table-vertical_line', [0, 40, 8, 1], 649),
atlas('bordered_table-horizontal_line', [8, 40, 1, 8], 649),
// c11 thumbnail frame (mod_thumbnail_image = deep_frame_in_shallow_frame sprite + inner shadow)
nine('mod_thumbnail_image', 85, 0, 9132, 'outer'),
// c07 / c12 tool-button icons (utility-sprites.lua)
icon('search_icon', 'icons/mip/search.png', 32, 0.5, 2940),
icon('refresh', 'icons/mip/refresh.png', 32, 0.5, 2894),
icon('trash', 'icons/mip/trash.png', 32, 0.5, 2968),
icon('mod_dependency_arrow', 'icons/mip/go-to-minibutton-arrow.png', 16, 1, 2855),
// c13 textbox (search field style still UNKNOWN; these are the textbox base states)
nine('textbox', 248, 0, 5058),
nine('textbox-active', 265, 0, 5058),
nine('textbox-disabled', 282, 0, 5058),
// c14 dropdown arrow
icon('dropdown', 'icons/mip/dropdown.png', 32, 0.5, 3178),
// c15 filter column
icon('mods_filter_exclude_button', 'icons/mod-manager/ban.png', 32, 0.5, 4963),
icon('mods_filter_exclude_button-hovered', 'icons/mod-manager/ban-hovered.png', 32, 0.5, 4963),
icon('mods_filter_exclude_button-clicked', 'icons/mod-manager/ban-clicked.png', 32, 0.5, 4963),
icon('mods_filter_exclude_button-selected', 'icons/mod-manager/ban-selected.png', 32, 0.5, 4963),
// rich-text [img] in the Space Age filter label, 20 logical px (25 px measured at 125%); not a style.lua sprite
icon('space-age', 'icons/space-age.png', 64, 0.3125, 0),
// c16 scrollbar
nine('vertical_scrollbar-background', 0, 72, 7591),
atlas('vertical_scrollbar-thumb', [0, 48, 20, 22], 7591),
atlas('vertical_scrollbar-thumb-hovered', [20, 48, 20, 22], 7591),
atlas('vertical_scrollbar-thumb-clicked', [40, 48, 20, 22], 7591),
// c17 message banners
nine('negative_message_frame', 403, 17, 5038),
nine('neutral_message_frame', 386, 17, 5028),
nine('positive_message_frame', 420, 17, 5048),
icon('warning_white', 'icons/mip/warning-white.png', 32, 0.5, 3016),
]
/** Problems with the sprite list itself (pure; tested). */
export function validateSprites(list: SpriteSpec[]): string[] {
const errors: string[] = []
const seen = new Set<string>()
for (const s of list) {
if (seen.has(s.name)) errors.push(`duplicate name ${s.name}`)
seen.add(s.name)
if (!/^[a-z0-9_]+(-[a-z0-9_]+)*$/.test(s.name)) errors.push(`bad name ${s.name}`)
const [x, y, w, h] = s.rect
if (x < 0 || y < 0 || w <= 0 || h <= 0) errors.push(`bad rect ${s.name}`)
if (s.corner !== undefined && (w !== 2 * s.corner + 1 || h !== 2 * s.corner + 1)) errors.push(`9-slice ${s.name} is not ${2 * s.corner + 1} px`)
}
return errors
}
// Only clips currently used by the app, not future mini/switch/red-arrow styles.
export const UI_SOUNDS = [
['gui-click', 1], ['gui-green-confirm', 1], ['gui-red-button', 0.5],
['gui-tool-button', 1], ['gui-tab', 1], ['gui-checkbox-click', 1],
['list-box-click', 1], ['gui-dropdown-open', 1], ['gui-slider', 1], ['cannot-build', 1],
] as const
/** Optional refresh: validate every source before replacing any tracked clip. */
export function extractSounds(core: string, out: string, gameVersion: string): boolean {
const clips = UI_SOUNDS.map(([name, gain]) => {
const source = `data/core/sound/${name}.ogg`
try {
const bytes = readFileSync(path.join(core, 'sound', `${name}.ogg`))
const evidence = name === 'gui-tab' ? 'VERIFIED: style.lua tab.left_click_sound (7710)'
: name === 'gui-tool-button' ? 'VERIFIED: style.lua tool_button (1321), tool_button_red (1304), frame_action_button (2789); dependency/search/refresh/remove/lightbox tools use this cue'
: name === 'gui-red-button' ? 'VERIFIED: style.lua red_button (1295), gain 0.5; actual red tools override it. red_back_button inherits gui-click; red_confirm_button uses gui-red-confirm at 0.7 (1262)'
: undefined
return { name, gain, source, evidence, bytes, sha256: createHash('sha256').update(bytes).digest('hex') }
} catch { return undefined }
})
if (clips.some(clip => !clip)) return false
mkdirSync(path.join(out, 'sounds'), { recursive: true })
const inventory = clips.map(clip => {
if (!clip) throw new Error('Unreachable missing clip')
writeFileSync(path.join(out, 'sounds', `${clip.name}.ogg`), clip.bytes)
const { bytes: _, ...entry } = clip
return entry
})
writeFileSync(path.join(out, 'sounds/index.json'), JSON.stringify({ gameVersion, credit: 'Factorio / Wube Software', clips: inventory }, null, 2) + '\n')
return true
}
function main() {
const install = findFactorio(process.env.FACTORIO_DIR)
if (!install) {
console.log('[extract-assets] No install; keeping tracked assets')
return
}
const core = path.join(install.installDir, 'data/core')
const graphics = path.join(core, 'graphics')
const out = path.resolve('assets/game')
extractSounds(core, out, install.gameVersion)
if (process.argv.includes('--sounds-only')) return
const spritesDir = path.join(out, 'sprites')
mkdirSync(spritesDir, { recursive: true })
const errors = validateSprites(SPRITES)
if (errors.length) throw new Error(errors.join('\n'))
const cache = new Map<string, Image>()
const load = (file: string) => {
let img = cache.get(file)
if (!img) cache.set(file, (img = decodePng(readFileSync(path.join(graphics, file)))))
return img
}
const index: Record<string, Omit<SpriteSpec, 'name'>> = {}
for (const s of SPRITES) {
writeFileSync(path.join(spritesDir, `${s.name}.png`), encodePng(crop(load(s.file), ...s.rect)))
const { name, ...rest } = s
index[name] = rest
}
writeFileSync(path.join(spritesDir, 'index.json'), JSON.stringify({ source: 'data/core/graphics', sprites: index }, null, 2) + '\n')
const copies: [string, string][] = [
['graphics/missing-thumbnail.png', 'missing-thumbnail.png'],
['graphics/factorio.ico', 'factorio.ico'],
['graphics/factorio-icon.png', 'factorio-icon.png'],
]
for (const f of readdirSync(path.join(graphics, 'icons/mod-manager'))) {
if (f.endsWith('.png')) copies.push([`graphics/icons/mod-manager/${f}`, `icons/mod-manager/${f}`])
}
for (const w of ['Regular', 'SemiBold', 'Bold']) copies.push([`fonts/TitilliumWeb-${w}.ttf`, `fonts/TitilliumWeb-${w}.ttf`])
for (const [from, to] of copies) {
mkdirSync(path.dirname(path.join(out, to)), { recursive: true })
copyFileSync(path.join(core, from), path.join(out, to))
}
// Only macOS installs ship the bundle icon (factorio.app/Contents/Resources).
const icns = path.join(install.installDir, 'Resources/factorio.icns')
if (existsSync(icns)) copyFileSync(icns, path.join(out, 'factorio.icns'))
console.log(`[extract-assets] ${SPRITES.length} sprites -> ${path.relative(process.cwd(), spritesDir)}, ${copies.length} files copied (game ${install.gameVersion})`)
}
if (import.meta.main) main()