Address review on findVersion and the export graphics lookup

findVersion takes headless as a plain optional argument, the helper is
checkInstallType, the warning is checkMissingBuiltinIcons and the full
install lookup is inlined into exportData.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-01 22:44:43 +00:00
1 parent 37d74ca2a6
commit f8aafe9265
4 files changed
+36 -71

No files matched your search

+12 -23
View File
@@ -97,6 +97,7 @@ async function loadZip(server: FactorioServer, modVersions: Map<string, string>,
/** Where icon files are read from during an export. */
interface ExportFiles {
/** Mod zips opened during the export, closed when it ends. */
zipCache: ZipCache;
/** Data dir of a full install to fall back to for builtin mod files. */
graphicsDataDir?: string;
@@ -525,23 +526,6 @@ async function exportLocale(
return mergedLocales;
}
/**
* Find a full install to read builtin mod graphics from
*
* @param server - The server the export runs on.
* @param files - Updated with the data dir found, or why there is none.
* @internal
*/
async function findGraphicsDataDir(server: FactorioServer, files: ExportFiles) {
const majorMinor = server.version!.replace(/\.\d+$/, "") as lib.MajorMinorVersion;
try {
[files.graphicsDataDir] = await findVersion(server._factorioDir, majorMinor, { headless: false });
} catch (err: any) {
// Without it the export still has everything but the base game icons
files.graphicsError = err.message;
}
}
/**
* Export locale, icons and metadata for the given factorio server
*
@@ -555,7 +539,13 @@ async function findGraphicsDataDir(server: FactorioServer, files: ExportFiles) {
export async function exportData(server: FactorioServer) {
const files: ExportFiles = { zipCache: new Map() };
if (await isHeadless(server.dataPath())) {
await findGraphicsDataDir(server, files);
const majorMinor = server.version!.replace(/\.\d+$/, "") as lib.MajorMinorVersion;
try {
[files.graphicsDataDir] = await findVersion(server._factorioDir, majorMinor, false);
} catch (err: any) {
// Without it the export still has everything but the base game icons
files.graphicsError = err.message;
}
}
try {
return await buildExport(server, files);
@@ -566,14 +556,14 @@ export async function exportData(server: FactorioServer) {
}
/**
* Warn about builtin mod icon files that could not be found
* Warn if any builtin mod icon files could not be found
*
* @param server - The server the export ran on.
* @param files - Where icon files were read from.
* @param iconCache - Icons loaded by the export, null for missing files.
* @internal
*/
function warnMissingBuiltinIcons(server: FactorioServer, files: ExportFiles, iconCache: IconCache) {
function checkMissingBuiltinIcons(server: FactorioServer, files: ExportFiles, iconCache: IconCache) {
const builtinModNames = ["core", ...lib.ModPack.getBuiltinModNames(server.version!)];
let missing = 0;
for (const [iconPath, icon] of iconCache) {
@@ -703,7 +693,7 @@ async function buildExport(server: FactorioServer, files: ExportFiles) {
const count = await exportIcons(server, modVersions, files, prototypesWithIcons, baseName, state);
server._logger.info(`Exported ${count} ${baseName} icons`);
}
warnMissingBuiltinIcons(server, files, state.iconCache);
checkMissingBuiltinIcons(server, files, state.iconCache);
// Crop sheet to actual used height and write single spritesheet + metadata.
const usedRows = Math.ceil(state.pos / (SHEET_WIDTH / SHEET_ICON_SIZE));
@@ -722,7 +712,6 @@ async function buildExport(server: FactorioServer, files: ExportFiles) {
export const _createSheetState = createSheetState;
export const _exportIcons = exportIcons;
export const _exportLocale = exportLocale;
export const _findGraphicsDataDir = findGraphicsDataDir;
export const _loadSimpleIcon = loadSimpleIcon;
export const _loadLayeredIcon = loadLayeredIcon;
export const _warnMissingBuiltinIcons = warnMissingBuiltinIcons;
export const _checkMissingBuiltinIcons = checkMissingBuiltinIcons;
+9 -10
View File
@@ -262,8 +262,7 @@ export async function checkForUpdates(
* @param targetVersion -
* Version to look for, supports the special value "latest" for the
* latest version available.
* @param options - Optional parameters.
* @param options.headless -
* @param headless -
* Only consider headless installs if true, or only full installs if
* false.
* @returns Array with path to data dir and version found.
@@ -271,23 +270,23 @@ export async function checkForUpdates(
export async function findVersion(
factorioDir: string,
targetVersion: lib.TargetVersion,
options: { headless?: boolean } = {},
headless?: boolean,
): Promise<[string, lib.FullVersion]> {
let kind = "";
if (options.headless !== undefined) {
kind = options.headless ? "headless " : "full ";
if (headless !== undefined) {
kind = headless ? "headless " : "full ";
}
const wrongKind = async (dir: string) => (
options.headless !== undefined && await isHeadless(path.join(dir, "data")) !== options.headless
const checkInstallType = async (dir: string) => (
headless === undefined || await isHeadless(path.join(dir, "data")) === headless
);
// Check if this is a direct installation dir
const directVersion = await getFactorioVersion(factorioDir);
if (directVersion !== null) {
if (await wrongKind(factorioDir)) {
if (!await checkInstallType(factorioDir)) {
throw new Error(
`Unable to find a ${kind}install of Factorio ${targetVersion}: ${factorioDir} is a direct ` +
`(single-version) install of the ${options.headless ? "full" : "headless"} build.`
`(single-version) install of the ${headless ? "full" : "headless"} build.`
);
}
if (targetVersion === "latest" || directVersion === targetVersion || directVersion.startsWith(targetVersion)) {
@@ -306,7 +305,7 @@ export async function findVersion(
// Avoid any fancy directory detection in order to follow symlinks
const fullPath = path.join(factorioDir, name);
const version = await getFactorioVersion(fullPath);
if (version === null || await wrongKind(fullPath)) {
if (version === null || !await checkInstallType(fullPath)) {
continue;
}