Coerce mod settings types during data export

Use the settings prototypes to coerce any incorrectly typed mod settings
to the correct type either by casting or by replacing the value with the
default value from the prototype.

This will fix cases where an update to a mod changes the type of ono of
its defined settings.
This commit is contained in:
Hornwitser committed 2024-04-28 16:52:33 +02:00
1 parent c37fe0f438
commit 3cf9f96233
3 files changed
+99 -9

No files matched your search

+20 -9
View File
@@ -334,10 +334,11 @@ export default class ModPack {
}
/**
* Fill in missing settings with their default values
* Fill in missing and incorrect settings with their default values
*
* Uses the provided setting prototypes to add any missing mod settings
* in the mod pack with the default value from the prototype.
* Uses the provided setting prototypes to correct any missing and or
* incorrect mod settings in the mod pack with the default value from
* the prototype.
*
* @param settingPrototypes -
* Setting prototypes exported from the game.
@@ -360,13 +361,23 @@ export default class ModPack {
logger.warn(`Ignoring ${prototype.name} with missing default_value`);
continue;
}
if (this.settings[settingType].get(prototype.name)) {
continue;
let value = this.settings[settingType].get(prototype.name)?.value ?? prototype.default_value;
const type = prototype.type;
if (
type === "bool-setting" && typeof value !== "boolean"
|| type === "color-setting" && typeof value !== "object"
|| (type === "int-setting" || type === "double-setting") && typeof value !== "number"
) {
value = prototype.default_value;
}
let value = prototype.default_value;
if (prototype.type === "color-setting") {
value = libLuaTools.normalizeColor(value);
if (type === "string-setting" && typeof value !== "string") {
value = typeof value === "object" ? prototype.default_value : String(value ?? "");
}
if (type === "color-setting") {
if (typeof value !== "object") {
value = prototype.default_value;
}
value = libLuaTools.normalizeColor(value as object);
}
this.settings[settingType].set(prototype.name, { value });
}