Merge pull request #1183 from bbassie/fix/save-patching-plugin-default

Default load_plugin to off when the instance lacks required features
This commit is contained in:
Bastiaan authored and GitHub committed 2026-11-04 01:19:10 +00:00
1 parent 308cca9b7d
commit 4a2c164188
9 files changed
+99 -42

No files matched your search

+14 -12
View File
@@ -88,10 +88,11 @@ export interface FieldDefinition<
optional?: Type extends null ? true : false;
/**
* Value this config field should take in a newly initialized config.
* This can also be a synchronous function returning the value to use.
* This can also be a synchronous function returning the value to use,
* it is called after the stored fields are loaded so that it can read them.
* Promise results are not awaited.
*/
initialValue?: unknown | (() => unknown);
initialValue?: unknown | ((config: Config<Fields>) => unknown);
/**
* Array of all values that are acceptible. Useful for making
* enumerated values.
@@ -140,9 +141,6 @@ function defaultValue(def: FieldDefinition) {
if (def.initialValue === undefined) {
return null;
}
if (typeof def.initialValue === "function") {
return def.initialValue();
}
return def.initialValue;
}
@@ -246,14 +244,12 @@ export class Config<
}
super();
const defs = Object.entries(this.constructor.fieldDefinitions).filter(
([name, def]) => this._checkAccess(name, def, location, ConfigAccess.read, false)
);
// Function initial values are left undefined until the stored fields are loaded
this.fields = Object.fromEntries(
Object.entries(
this.constructor.fieldDefinitions
).filter(
([name, def]) => this._checkAccess(name, def, location, ConfigAccess.read, false)
).map(
([name, def]) => [name, defaultValue(def)]
)
defs.map(([name, def]) => [name, typeof def.initialValue === "function" ? undefined : defaultValue(def)])
) as Fields;
if (fields) {
@@ -264,6 +260,12 @@ export class Config<
this.update(migrated, false, location);
}
for (const [name, def] of defs) {
if (this.fields[name as keyof Fields] === undefined) {
this.fields[name as keyof Fields] = (def.initialValue as (config: Config<Fields>) => any)(this);
}
}
for (const [pluginName, version] of this.constructor.pluginVersions) {
const name = `${pluginName}.version` as keyof Fields & string;
if (name in this.fields && this.fields[name] !== version) {
+19 -24
View File
@@ -878,36 +878,31 @@ export function addPluginFieldDefinitions(
// The validator is dynamic based on the feature requirements of the plugin
if (kind === "instanceConfigFields" && pluginInfo.features) {
fieldDef.dependsOn = [];
const savePatching = pluginInfo.features.includes("SavePatching");
if (savePatching) {
fieldDef.dependsOn.push("factorio.enable_save_patching");
const requirements: [string, string][] = [];
if (pluginInfo.features.includes("SavePatching")) {
requirements.push(["factorio.enable_save_patching", "save patching"]);
}
if (pluginInfo.features.includes("ScriptCommands")) {
requirements.push(["factorio.enable_script_commands", "script commands"]);
}
if (pluginInfo.features.includes("LuaUdp")) {
requirements.push(["factorio.enable_lua_udp", "Lua UDP"]);
}
const scriptCommands = pluginInfo.features.includes("ScriptCommands");
if (scriptCommands) {
fieldDef.dependsOn.push("factorio.enable_script_commands");
}
const luaUdp = pluginInfo.features.includes("LuaUdp");
if (luaUdp) {
fieldDef.dependsOn.push("factorio.enable_lua_udp");
}
if (scriptCommands || savePatching || luaUdp) {
if (requirements.length) {
fieldDef.dependsOn = requirements.map(([field]) => field);
// Default to not loaded when the instance does not meet the requirements
fieldDef.initialValue = (config: classes.Config<any>) => requirements.every(
([field]) => config.get(field)
);
fieldDef.validator = (enabled, config) => {
if (!enabled) {
return;
}
if (savePatching && !config.get("factorio.enable_save_patching")) {
throw new Error(`Plugin ${pluginInfo.name} requires save patching`);
}
if (scriptCommands && !config.get("factorio.enable_script_commands")) {
throw new Error(`Plugin ${pluginInfo.name} requires script commands`);
}
if (luaUdp && !config.get("factorio.enable_lua_udp")) {
throw new Error(`Plugin ${pluginInfo.name} requires Lua UDP`);
for (const [field, description] of requirements) {
if (!config.get(field)) {
throw new Error(`Plugin ${pluginInfo.name} requires ${description}`);
}
}
};
}