From 8c6130adbabf540f19b3cd978137a6347c4af58e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 6 Jul 2020 13:10:26 +0200 Subject: [PATCH] Add load scenario command Add alternative method of starting an instance by loading a scenario. Since there's no save when starting a scenario it can only be used if the save patching is disabled. --- clusterctl.js | 17 ++++++++++ lib/link/messages.js | 10 ++++++ lib/users.js | 5 +++ slave.js | 78 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 99 insertions(+), 11 deletions(-) diff --git a/clusterctl.js b/clusterctl.js index b1ae99bd..5c439438 100644 --- a/clusterctl.js +++ b/clusterctl.js @@ -395,6 +395,23 @@ commands.push(new Command({ }, })); +commands.push(new Command({ + definition: ["load-scenario", "Start instance by loading a scenario", (yargs) => { + yargs.options({ + "instance": { describe: "Instance to start", nargs: 1, type: "string", demandOption: true }, + "scenario": { describe: "Scenario to load", nargs: 1, type: "string" }, + }); + }], + handler: async function(args, control) { + let instanceId = await resolveInstance(control, args.instance); + await link.messages.setInstanceOutputSubscriptions.send(control, { instance_ids: [instanceId] }); + let response = await link.messages.loadScenario.send(control, { + instance_id: instanceId, + scenario: args.scenario || null, + }); + }, +})); + commands.push(new Command({ definition: ["stop-instance", "Stop instance", (yargs) => { yargs.options({ diff --git a/lib/link/messages.js b/lib/link/messages.js index fd0d0876..9a795317 100644 --- a/lib/link/messages.js +++ b/lib/link/messages.js @@ -418,6 +418,16 @@ messages.createSave = new Request({ forwardTo: "instance", }); +messages.loadScenario = new Request({ + type: "load_scenario", + links: ["control-master", "master-slave", "slave-instance"], + permission: "core.instance.load_scenario", + forwardTo: "instance", + requestProperties: { + "scenario": { type: "string" }, + }, +}); + messages.exportData = new Request({ type: "export_data", links: ["control-master", "master-slave", "slave-instance"], diff --git a/lib/users.js b/lib/users.js index 11ef3fde..1ad2a8bf 100644 --- a/lib/users.js +++ b/lib/users.js @@ -318,6 +318,11 @@ definePermission({ title: "Start instance", description: "Start instances.", }); +definePermission({ + name: "core.instance.load_scenario", + title: "Load scenario", + description: "Start instances by loading a scenario.", +}); definePermission({ name: "core.instance.stop", title: "Stop instance", diff --git a/slave.js b/slave.js index c4274fc0..0ce609ec 100644 --- a/slave.js +++ b/slave.js @@ -288,17 +288,11 @@ class Instance extends link.Link{ } /** - * Prepare a save for starting + * Prepare instance for starting * - * Writes server settings, links mods, creates a new save if no save is - * passed, and patches it with modules. - * - * @param {String|null} saveName - - * Save to prepare from the instance saves directory. Creates a new - * save if null. - * @returns {String} Name of the save prepared. + * Writes server settings and links mods. */ - async prepare(saveName) { + async prepare() { console.log("Clusterio | Writing server-settings.json"); await this.writeServerSettings(); @@ -315,7 +309,19 @@ class Instance extends link.Link{ }catch(e){} await symlinkMods(this, "sharedMods", console); + } + /** + * Prepare a save for starting + * + * Creates a new save if no save is passed and patches it with modules. + * + * @param {String|null} saveName - + * Save to prepare from the instance saves directory. Creates a new + * save if null. + * @returns {String} Name of the save prepared. + */ + async prepareSave(saveName) { // Use latest save if no save was specified if (saveName === null) { saveName = await fileOps.getNewestFile( @@ -425,6 +431,28 @@ class Instance extends link.Link{ link.messages.instanceStarted.send(this, { instance_id: this.config.get("instance.id") }); } + /** + * Start Factorio server by loading a scenario + * + * Launches the Factorio server for this instance with the given + * scenario. + * + * @param {String} scenario - Name of scenario to load. + */ + async startScenario(scenario) { + this.server.on("rcon-ready", () => { + console.log("Clusterio | RCON connection established"); + }); + + this.server.on("exit", () => this.notifyExit()); + await this.server.startScenario(scenario); + + await plugin.invokeHook(this.plugins, "onStart"); + + this._running = true; + link.messages.instanceStarted.send(this, { instance_id: this.config.get("instance.id") }); + } + /** * Update instance information on the Factorio side */ @@ -490,14 +518,36 @@ class Instance extends link.Link{ async startInstanceRequestHandler(message) { let saveName = message.data.save; try { - saveName = await this.prepare(saveName); + await this.prepare(); + saveName = await this.prepareSave(saveName); } catch (err) { this.notifyExit(); throw err; } try { - await this.start(saveName, this.config, this.socket); + await this.start(saveName); + } catch (err) { + await this.stop(); + throw err; + } + } + + async loadScenarioRequestHandler(message) { + if (this.config.get("factorio.enable_save_patching")) { + this.notifyExit(); + throw new errors.RequestError("Load scenario cannot be used with save patching enabled"); + } + + try { + await this.prepare(); + } catch (err) { + this.notifyExit(); + throw err; + } + + try { + await this.startScenario(message.data.scenario); } catch (err) { await this.stop(); throw err; @@ -952,6 +1002,12 @@ class Slave extends link.Link { return await request.send(instanceConnection, message.data); } + async loadScenarioRequestHandler(message, request) { + let instanceId = message.data.instance_id; + let instanceConnection = await this._connectInstance(instanceId); + return await request.send(instanceConnection, message.data); + } + async createSaveRequestHandler(message, request) { let instanceId = message.data.instance_id; let instanceConnection = await this._connectInstance(instanceId);