From 9458155a1baa779a332349c46829370626e8eecc Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sat, 4 Jul 2020 16:45:42 +0200 Subject: [PATCH] Add option to disable save patching Add factorio.enable_save_patching config entry to allow disabling the save patching done on startup. This is for running vannila games or scenarios that are not compatible with Clusterio. --- docs/how-it-works.md | 4 ++++ docs/writing-plugins.md | 14 ++++++++++++++ lib/config/definitions.js | 6 ++++++ plugins/research_sync/instance.js | 4 ++++ plugins/statistics_exporter/instance.js | 6 ++++++ slave.js | 11 +++++++++-- 6 files changed, 43 insertions(+), 2 deletions(-) diff --git a/docs/how-it-works.md b/docs/how-it-works.md index 3e835624..ad4084b0 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -9,6 +9,10 @@ based on the built in library to Factorio and requires scenarios to be coded specifically for it. Mods on the other hand work without modifications. +This can be disabled with the `factorio.enable_save_patching` config on +a per instance basis, however disabling it means that modules are not +loaded into the game and most plugins will not function. + 1: Before version 2.0 of Clusterio this was done at runtime with a patcher called Hotpatch, but due to the difficulty in supporting run time patching and the lack of documentation it was replace it with diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index 94b32f17..83b7fdf4 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -195,6 +195,20 @@ where appropriate. See [Configuration System](configuration-system.md) for more details on how this system works. +### Handling Invalid Configuration + +If the plugin requires a certain feature to be enabled to function it +should throw an error during init if this is not the case. The most +common such feature is the save patching, which can be disabled to run +vanilla or scenarios not compatible with Clusterio. For example: + + async init() { + if (!this.instance.config.get("factorio.enable_save_patching")) { + throw new Error("foo_frobber plugin requires save patching."); + } + } + + Communicating with Factorio --------------------------- diff --git a/lib/config/definitions.js b/lib/config/definitions.js index 02f2ee6d..e098c66b 100644 --- a/lib/config/definitions.js +++ b/lib/config/definitions.js @@ -239,6 +239,12 @@ FactorioGroup.define({ type: "string", optional: true, }); +FactorioGroup.define({ + name: "enable_save_patching", + description: "Patch saves with Lua code. Required for Clusterio integrations, lua modules, and most plugins.", + type: "boolean", + initial_value: true, +}); FactorioGroup.define({ name: "settings", description: "Settings overridden in server-settings.json", diff --git a/plugins/research_sync/instance.js b/plugins/research_sync/instance.js index d87b0b29..4a9f56c0 100644 --- a/plugins/research_sync/instance.js +++ b/plugins/research_sync/instance.js @@ -13,6 +13,10 @@ function unexpectedError(err) { class InstancePlugin extends plugin.BaseInstancePlugin { async init() { + if (!this.instance.config.get("factorio.enable_save_patching")) { + throw new Error("research_sync plugin requires save patching."); + } + this.instance.server.on("ipc-research_sync:contribution", (tech) => { this.researchContribution(tech).catch(unexpectedError); }); diff --git a/plugins/statistics_exporter/instance.js b/plugins/statistics_exporter/instance.js index b76ae99b..2e179d1f 100644 --- a/plugins/statistics_exporter/instance.js +++ b/plugins/statistics_exporter/instance.js @@ -22,6 +22,12 @@ const instanceGameFlowStatistics = new Gauge( class InstancePlugin extends plugin.BaseInstancePlugin { + async init() { + if (!this.instance.config.get("factorio.enable_save_patching")) { + throw new Error("statistics_exporter plugin requires save patching."); + } + } + async onMetrics() { let string = await this.instance.server.sendRcon("/sc statistics_exporter.export()"); let stats; diff --git a/slave.js b/slave.js index 41466a2f..c4274fc0 100644 --- a/slave.js +++ b/slave.js @@ -330,6 +330,10 @@ class Instance extends link.Link{ saveName = "world.zip"; } + if (!this.config.get("factorio.enable_save_patching")) { + return saveName; + } + // Patch save with lua modules from plugins console.log("Clusterio | Patching save"); @@ -409,8 +413,11 @@ class Instance extends link.Link{ this.server.on("exit", () => this.notifyExit()); await this.server.start(saveName); - await this.server.disableAchievements(); - await this.updateInstanceData(); + + if (this.config.get("factorio.enable_save_patching")) { + await this.server.disableAchievements(); + await this.updateInstanceData(); + } await plugin.invokeHook(this.plugins, "onStart");