mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 15:43:13 +09:00
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:
1 parent
308cca9b7d
commit
4a2c164188
9 files changed
+99
-42
No files matched your search
@@ -269,13 +269,13 @@ export default class InstanceManager {
|
||||
) as InheritableField[];
|
||||
}
|
||||
|
||||
/** Default value of a field, or undefined if it is generated */
|
||||
private static _defaultValue(name: InheritableField): unknown {
|
||||
/** Default value of a field for the given config */
|
||||
private static _defaultValue(name: InheritableField, config: lib.InstanceConfig): unknown {
|
||||
const def = lib.InstanceConfig.fieldDefinitions[name];
|
||||
if (name === "factorio.settings") {
|
||||
return InstanceManager.DefaultFactorioSettings;
|
||||
}
|
||||
return typeof def.initialValue === "function" ? undefined : def.initialValue ?? null;
|
||||
return typeof def.initialValue === "function" ? def.initialValue(config) : def.initialValue ?? null;
|
||||
}
|
||||
|
||||
/** Values the given inherited fields take from the template, with object overrides applied */
|
||||
@@ -345,7 +345,7 @@ export default class InstanceManager {
|
||||
for (const name of InstanceManager._inheritableFields()) {
|
||||
const value = instance.config.get(name) as any;
|
||||
const templateValue = template.config.get(name) as any;
|
||||
const defaultValue = InstanceManager._defaultValue(name) as any;
|
||||
const defaultValue = InstanceManager._defaultValue(name, instance.config) as any;
|
||||
if (lib.InstanceConfig.fieldDefinitions[name].type === "object") {
|
||||
inherited[name] = Object.keys(value ?? {}).filter(prop => !(
|
||||
isDeepStrictEqual(value[prop], templateValue?.[prop])
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user