mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
The mod pack page only looked up mod-setting-name.<name>, so settings with a localised_name showed the internal name, and rich text tags in the locale showed up as raw text. Resolve localised_name and localised_description from the exported settings prototypes against the exported locale, falling back to the mod-setting-name/mod-setting-description keys and then the internal name. The resolver handles parameters, the "" and "?" keys and __ITEM__/__ENTITY__/__TILE__/__FLUID__ references. Rich text formatting tags are removed and prototype icons are replaced with their name. Fixes #768 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import type * as lib from "@clusterio/lib";
|
|
|
|
export type ModSettingScope = "startup" | "runtime-global" | "runtime-per-user";
|
|
|
|
export type ModSettingField = {
|
|
name: string;
|
|
type: string;
|
|
mod?: string;
|
|
order?: string;
|
|
allowed_values?: unknown;
|
|
localised_name?: lib.FactorioLocalisedString;
|
|
localised_description?: lib.FactorioLocalisedString;
|
|
};
|
|
|
|
const settingTypes = ["bool-setting", "int-setting", "double-setting", "string-setting", "color-setting"];
|
|
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
|
|
|
function typeFromValue(value: lib.ModSetting["value"]) {
|
|
if (typeof value === "boolean") {
|
|
return "bool-setting";
|
|
} else if (typeof value === "number") {
|
|
return "double-setting";
|
|
} else if (typeof value === "string") {
|
|
return "string-setting";
|
|
} else if (typeof value === "object" && "r" in value) {
|
|
return "color-setting";
|
|
}
|
|
throw Error(`Unhandled setting type ${typeof value}`);
|
|
}
|
|
|
|
/**
|
|
* Split the settings of a scope into ones defined by the exported prototypes and stored ones
|
|
* no prototype defines. Without export data there is nothing to compare to, so none are unknown.
|
|
*/
|
|
export function modSettingFields(
|
|
prototypes: lib.ExportSettings | undefined,
|
|
scope: ModSettingScope,
|
|
stored: Map<string, lib.ModSetting>,
|
|
) {
|
|
const fromStored = (name: string): ModSettingField => ({ name, type: typeFromValue(stored.get(name)!.value) });
|
|
if (!prototypes) {
|
|
return { known: [...stored.keys()].map(fromStored), unknown: [] };
|
|
}
|
|
|
|
const known: ModSettingField[] = Object.entries(prototypes)
|
|
.filter(([type]) => settingTypes.includes(type))
|
|
.flatMap(([_, settings]) => Object.values(settings))
|
|
.filter(p => p.setting_type === scope)
|
|
.map(p => ({
|
|
name: p.name,
|
|
type: p.type,
|
|
mod: p.mod as string,
|
|
order: p.order,
|
|
allowed_values: p.allowed_values,
|
|
localised_name: p.localised_name,
|
|
localised_description: p.localised_description,
|
|
}))
|
|
.sort((a, b) => strcmp(a.mod, b.mod) || strcmp(a.order || "", b.order || "") || strcmp(a.name, b.name))
|
|
;
|
|
const knownNames = new Set(known.map(field => field.name));
|
|
const unknown = [...stored.keys()]
|
|
.filter(name => !knownNames.has(name))
|
|
.sort(strcmp)
|
|
.map(fromStored)
|
|
;
|
|
return { known, unknown };
|
|
}
|