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.
This commit is contained in:
Hornwitser committed 2020-07-06 13:10:26 +02:00
1 parent 9458155a1b
commit 8c6130adba
4 files changed
+99 -11

No files matched your search

+17
View File
@@ -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({
+10
View File
@@ -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"],
+5
View File
@@ -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",
+67 -11
View File
@@ -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);