From f863aee3c28bc79b78215ab1aacf74e6cd61e6bc Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 9 Oct 2022 16:22:34 +0200 Subject: [PATCH] Add enabled field for mods in mod packs Make it possible to disable built in mods like base by providing an enabled field that can be set to false in mod packs. --- packages/ctl/src/commands.js | 20 ++++++++++++-- packages/lib/data/ModPack.js | 5 ++-- .../web_ui/src/components/ModPackViewPage.jsx | 26 ++++++++++++++++++- packages/web_ui/src/index.css | 4 +-- test/integration/clusterio.js | 2 +- test/lib/data/ModPack.js | 8 +++--- 6 files changed, 53 insertions(+), 12 deletions(-) diff --git a/packages/ctl/src/commands.js b/packages/ctl/src/commands.js index f4a31510..6173e808 100644 --- a/packages/ctl/src/commands.js +++ b/packages/ctl/src/commands.js @@ -875,7 +875,8 @@ modPackCommands.add(new libCommand.Command({ if (field === "mods") { print(`${field}:`); for (let entry of value) { - print(` ${entry.name} ${entry.version}${entry.sha1 ? ` (${entry.sha1})` : ""}`); + const enabled = entry.enabled ? "" : "(disabled) "; + print(` ${enabled}${entry.name} ${entry.version}${entry.sha1 ? ` (${entry.sha1})` : ""}`); } } else if (field === "settings") { print(`${field}:`); @@ -958,7 +959,16 @@ function setModPackMods(modPack, mods) { if (sha1 && !/^[0-9a-f]{40}$/.test(sha1)) { throw new libErrors.CommandError("sha1 must be a 40 digit lower case hex string"); } - modPack.mods.set(name, { name, version, sha1 }); + modPack.mods.set(name, { name, enabled: true, version, sha1 }); + } +} + +function setModPackModsEnabled(modPack, mods, enabled) { + for (let mod of mods || []) { + if (!modPack.mods.has(mod)) { + throw new libErrors.CommandError(`Mod named ${mod} does not exist in the mod pack`); + } + modPack.mods.get(mod).enabled = enabled; } } @@ -969,6 +979,7 @@ modPackCommands.add(new libCommand.Command({ yargs.options({ "description": { describe: "Description for mod pack", type: "string" }, "mods": { describe: "Mods in the form of name:version[:sha1]", array: true, type: "string" }, + "disabled-mods": { describe: "Mods that are in the pack but not enabled", array: true, type: "string" }, "bool-setting": { describe: "Set boolean setting", array: true, nargs: 3, type: "string" }, "int-setting": { describe: "Set int setting", array: true, nargs: 3, type: "string" }, "double-setting": { describe: "Set double setting", array: true, nargs: 3, type: "string" }, @@ -986,6 +997,7 @@ modPackCommands.add(new libCommand.Command({ modPack.factorioVersion = args.factorioVersion; } setModPackMods(modPack, args.mods); + setModPackModsEnabled(modPack, args.disabledMods, false); setModPackSettings(modPack, args); await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() }); print(`Created mod pack ${modPack.name} (${modPack.id})`); @@ -1000,6 +1012,8 @@ modPackCommands.add(new libCommand.Command({ "description": { describe: "New description for mod pack", type: "string" }, "factorio-version": { describe: "Set version of factorio the mod pack is for", type: "string" }, "add-mods": { describe: "Mods in the form of name:version[:sha1] to add", array: true, type: "string" }, + "enable-mods": { describe: "Mods to set as enabled", array: true, type: "string" }, + "disable-mods": { describe: "Mods to set as disabled", array: true, type: "string" }, "remove-mods": { describe: "Name of mods to remove", array: true, type: "string" }, "bool-setting": { describe: "Set boolean setting", array: true, nargs: 3, type: "string" }, "int-setting": { describe: "Set int setting", array: true, nargs: 3, type: "string" }, @@ -1031,6 +1045,8 @@ modPackCommands.add(new libCommand.Command({ logger.warn(`Mod ${name} did not exist on ${modPack.name}`); } } + setModPackModsEnabled(modPack, args.disableMods, false); + setModPackModsEnabled(modPack, args.enableMods, true); setModPackSettings(modPack, args); if (args.removeSetting) { diff --git a/packages/lib/data/ModPack.js b/packages/lib/data/ModPack.js index 50f21db1..60d3c53c 100644 --- a/packages/lib/data/ModPack.js +++ b/packages/lib/data/ModPack.js @@ -29,15 +29,16 @@ const ModSettingsJsonSchema = { * A mod in a mod pack * @typedef {object} module:lib/data.ModPack~ModRecord * @property {string} name - name of the mod. + * @property {boolean} enabled - if mod is to be loaded. * @property {string} version - version of the mod. * @property {string=} sha1 - SHA1 hash of the zip file. */ const ModRecordJsonSchema = { type: "object", - additionalProperties: false, - required: ["name", "version"], + required: ["name", "enabled", "version"], properties: { "name": { type: "string" }, + "enabled": { type: "boolean" }, "version": { type: "string" }, "sha1": { type: "string" }, }, diff --git a/packages/web_ui/src/components/ModPackViewPage.jsx b/packages/web_ui/src/components/ModPackViewPage.jsx index 17042616..39264fe1 100644 --- a/packages/web_ui/src/components/ModPackViewPage.jsx +++ b/packages/web_ui/src/components/ModPackViewPage.jsx @@ -95,7 +95,7 @@ function SearchModsTable(props) { props.onChange({ type: "mods.set", name: mod.name, - value: { name: mod.name, version: mod.version, sha1: mod.sha1 }, + value: { name: mod.name, enabled: true, version: mod.version, sha1: mod.sha1 }, }); }} >{props.modPack.mods.has(mod.name) ? "change" : "add"} @@ -255,6 +255,30 @@ function ModsTable(props) { } mod.enabled).map(mod => mod.name), + getCheckboxProps(mod) { + return { disabled: deletedMods.has(mod.name) }; + }, + onChange(selectedRowKeys, selectedRows) { + function setEnabled(modNames, enabled) { + for (let name of modNames) { + props.onChange({ + type: "mods.set", + name, + value: { ...props.modPack.mods.get(name), enabled }, + }); + } + } + const added = selectedRowKeys.filter(key => !props.modPack.mods.get(key).enabled); + const removed = [...props.modPack.mods.values()].filter( + mod => mod.enabled && selectedRowKeys.indexOf(mod.name) === -1 + ).map(mod => mod.name); + setEnabled(added, true); + setEnabled(removed, false); + }, + }} columns={[ { title: "Name", diff --git a/packages/web_ui/src/index.css b/packages/web_ui/src/index.css index f4a3d45d..267f11bf 100644 --- a/packages/web_ui/src/index.css +++ b/packages/web_ui/src/index.css @@ -333,13 +333,13 @@ tr.no-expanded-padding > td, .ant-form-item.changed, input.ant-input.changed, textarea.ant-input.changed, -tr.ant-table-row.changed { +tr.ant-table-row.changed > td { background: #2a1912; } .ant-table-tbody > tr.ant-table-row.changed:hover > td, .ant-table-tbody > tr.changed > td.ant-table-cell-row-hover -tr.ant-table-row.deleted { +tr.ant-table-row.deleted > td { background: #3a2922; } diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index 2751ab87..843248c3 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -845,7 +845,7 @@ describe("Integration of Clusterio", function() { reference.name = "full-pack"; reference.description = "Description"; reference.factorioVersion = "0.17"; - reference.mods.set("empty_mod", { name: "empty_mod", version: "1.0.0" }); + reference.mods.set("empty_mod", { name: "empty_mod", enabled: true, version: "1.0.0" }); reference.settings["startup"].set("MyBool", { value: true }); reference.settings["runtime-global"].set("MyInt", { value: 1235 }); reference.settings["runtime-global"].set("MyDouble", { value: 12.25 }); diff --git a/test/lib/data/ModPack.js b/test/lib/data/ModPack.js index fede2912..c26540dd 100644 --- a/test/lib/data/ModPack.js +++ b/test/lib/data/ModPack.js @@ -26,8 +26,8 @@ describe("lib/data/ModPack", function() { check(new ModPack({ description: "My Description" })); check(new ModPack({ factorio_version: "2.0" })); check(new ModPack({ mods: [ - { name: "subspace_storage", version: "1.99.8" }, - { name: "clusterio_lib", version: "0.1.2", sha1: "012345abcd" }, + { name: "subspace_storage", enabled: true, version: "1.99.8" }, + { name: "clusterio_lib", enabled: true, version: "0.1.2", sha1: "012345abcd" }, ]})); check(new ModPack({ settings: { "startup": { @@ -46,8 +46,8 @@ describe("lib/data/ModPack", function() { description: "Every option at once.", factorio_version: "2.0", mods: [ - { name: "subspace_storage", version: "1.99.8" }, - { name: "clusterio_lib", version: "0.1.2", sha1: "012345abcd" }, + { name: "subspace_storage", enabled: true, version: "1.99.8" }, + { name: "clusterio_lib", enabled: true, version: "0.1.2", sha1: "012345abcd" }, ], settings: { "startup": {