mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 16:13:12 +09:00
Merge upstream/master and address review
Resolves the ESM conflicts in patch.ts, Instance.ts and host.ts. The module gathering in Instance now goes through patch.loadModules with the instance's loadedPlugins, and imports use explicit extensions. Review changes: the existing-output check only treats ENOENT as "does not exist" and rethrows anything else, a --zip option keeps the output as a zip file for transfer (extracting stays the default), the --scenario option describes that it takes a path and what the default is, and the docs note that IPC is not available outside Clusterio. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
commit
187138bec5
297 files changed
+4980
-3274
No files matched your search
@@ -9,11 +9,11 @@ const execAsync = util.promisify(exec);
|
||||
// internal libraries
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { FactorioServer } from "./server";
|
||||
import { loadModules, patch } from "./patch";
|
||||
import { exportData } from "./export";
|
||||
import type Host from "./Host";
|
||||
import BaseInstancePlugin from "./BaseInstancePlugin";
|
||||
import { FactorioServer } from "./server.js";
|
||||
import { loadModules, patch } from "./patch.js";
|
||||
import { exportData } from "./export.js";
|
||||
import type Host from "./Host.js";
|
||||
import { BaseInstancePlugin, InstanceHooks } from "./BaseInstancePlugin.js";
|
||||
|
||||
const scriptCommands = [
|
||||
"/cheat", "/editor",
|
||||
@@ -140,7 +140,6 @@ export default class Instance extends lib.Link {
|
||||
* ID of this instance, equivalenet to `instance.config.get("instance.id")`.
|
||||
*/
|
||||
readonly id: number;
|
||||
plugins: Map<string, BaseInstancePlugin>;
|
||||
config: lib.InstanceConfig;
|
||||
logger: lib.Logger;
|
||||
server: FactorioServer;
|
||||
@@ -156,6 +155,10 @@ export default class Instance extends lib.Link {
|
||||
* Players currently online on the instance.
|
||||
*/
|
||||
playersOnline: Set<string> = new Set();
|
||||
/** Hooks which plugins can attach to */
|
||||
hooks: InstanceHooks;
|
||||
/** Plugins which are currently loaded */
|
||||
loadedPlugins: Set<lib.PluginNodeEnvInfo> = new Set();
|
||||
|
||||
|
||||
_host: Host;
|
||||
@@ -179,7 +182,6 @@ export default class Instance extends lib.Link {
|
||||
this._host = host;
|
||||
this._dir = dir;
|
||||
|
||||
this.plugins = new Map();
|
||||
this.config = instanceConfig;
|
||||
|
||||
this.id = this.config.get("instance.id");
|
||||
@@ -189,8 +191,10 @@ export default class Instance extends lib.Link {
|
||||
instance_name: this.name,
|
||||
});
|
||||
|
||||
this.hooks = new InstanceHooks(this.logger);
|
||||
|
||||
this._configFieldChanged = (field: string, curr: unknown, prev: unknown) => {
|
||||
let hook = () => lib.invokeHook(this.plugins, "onInstanceConfigFieldChanged", field, curr, prev);
|
||||
let hook = () => this.hooks.instanceConfigFieldChanged.invoke(field, curr, prev);
|
||||
|
||||
if (field === "factorio.shutdown_timeout") {
|
||||
this.server.shutdownTimeoutMs = curr as number * 1000;
|
||||
@@ -231,7 +235,7 @@ export default class Instance extends lib.Link {
|
||||
this.server.on("output", (parsed, line) => {
|
||||
this.logger.log("server", { message: line, instance_id: this.id, parsed });
|
||||
|
||||
lib.invokeHook(this.plugins, "onOutput", parsed, line);
|
||||
this.hooks.output.invoke(parsed, line);
|
||||
});
|
||||
|
||||
this.server.on("error", err => {
|
||||
@@ -398,7 +402,7 @@ export default class Instance extends lib.Link {
|
||||
stats,
|
||||
};
|
||||
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("join", name, stats));
|
||||
lib.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||
this.hooks.playerEvent.invoke(event);
|
||||
}
|
||||
|
||||
_recordPlayerLeave(name: string, reason: string) {
|
||||
@@ -419,7 +423,7 @@ export default class Instance extends lib.Link {
|
||||
stats,
|
||||
};
|
||||
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("leave", name, stats, reason));
|
||||
lib.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||
this.hooks.playerEvent.invoke(event);
|
||||
}
|
||||
|
||||
async handleInstanceExtractPlayersRequest() {
|
||||
@@ -450,7 +454,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
stats,
|
||||
};
|
||||
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("import", name, stats));
|
||||
lib.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||
this.hooks.playerEvent.invoke(event);
|
||||
count += 1;
|
||||
}
|
||||
this.logger.info(`Extracted data for ${count} player(s)`);
|
||||
@@ -588,9 +592,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
// Notify plugins of exit
|
||||
for (let pluginInstance of this.plugins.values()) {
|
||||
pluginInstance.onExit();
|
||||
}
|
||||
this.hooks.exit.invoke();
|
||||
|
||||
for (let player of this.playersOnline) {
|
||||
this._recordPlayerLeave(player, "server_quit");
|
||||
@@ -598,21 +600,6 @@ end`.replace(/\r?\n/g, " ");
|
||||
this._saveStats().catch(err => this.logger.error(`Error saving stats:\n${err.stack}`));
|
||||
}
|
||||
|
||||
async _loadPlugin(pluginInfo: lib.PluginNodeEnvInfo, host: Host) {
|
||||
let pluginLoadStartedMs = Date.now();
|
||||
let InstancePluginClass = await lib.loadPluginClass(
|
||||
pluginInfo.name,
|
||||
path.posix.join(pluginInfo.requirePath, pluginInfo.instanceEntrypoint!),
|
||||
"InstancePlugin",
|
||||
BaseInstancePlugin,
|
||||
);
|
||||
let instancePlugin = new InstancePluginClass(pluginInfo, this, host);
|
||||
this.plugins.set(pluginInfo.name, instancePlugin);
|
||||
await instancePlugin.init();
|
||||
|
||||
this.logger.info(`Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStartedMs}ms`);
|
||||
}
|
||||
|
||||
async _loadStats() {
|
||||
let instanceStats;
|
||||
try {
|
||||
@@ -667,6 +654,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
// load plugins
|
||||
const plugins: Record<string, string> = {};
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
if (
|
||||
!pluginInfo.instanceEntrypoint
|
||||
@@ -682,7 +670,18 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
try {
|
||||
await this._loadPlugin(pluginInfo, this._host);
|
||||
let pluginLoadStartedMs = Date.now();
|
||||
await lib.loadPlugin(
|
||||
pluginInfo,
|
||||
"instance",
|
||||
{ logger: this.logger, host: this._host, instance: this },
|
||||
"InstancePlugin",
|
||||
BaseInstancePlugin,
|
||||
);
|
||||
|
||||
this.loadedPlugins.add(pluginInfo);
|
||||
plugins[pluginInfo.name] = pluginInfo.version;
|
||||
this.logger.info(`Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStartedMs}ms`);
|
||||
} catch (err) {
|
||||
this.notifyExit();
|
||||
await this.sendSaveListUpdate();
|
||||
@@ -690,10 +689,6 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
}
|
||||
|
||||
let plugins: Record<string, string> = {};
|
||||
for (let [name, plugin] of this.plugins) {
|
||||
plugins[name] = plugin.info.version;
|
||||
}
|
||||
this.send(new lib.InstanceInitialisedEvent(plugins));
|
||||
}
|
||||
|
||||
@@ -796,6 +791,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
} else {
|
||||
modPack = await this.sendTo("controller", new lib.ModPackGetRequest(modPackId));
|
||||
}
|
||||
this.checkModPackVersion(modPack);
|
||||
this.activeModPack = modPack;
|
||||
|
||||
const mods = await this._host.fetchMods(modPack.mods.values(), modPack.factorioVersion);
|
||||
@@ -847,6 +843,25 @@ end`.replace(/\r?\n/g, " ");
|
||||
await fs.writeFile(this.path("mods", "mod-settings.dat"), this.activeModPack.toModSettingsDat());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the mod pack does not target a newer Factorio than the server
|
||||
*
|
||||
* The mod pack's Factorio version is written into mod-settings.dat and
|
||||
* Factorio refuses to read it if it is newer than the game. It then
|
||||
* loads default settings instead, which can corrupt the save.
|
||||
*
|
||||
* @throws {lib.RequestError} if the mod pack version is higher than the server version.
|
||||
*/
|
||||
checkModPackVersion(modPack: lib.ModPack) {
|
||||
const serverVersion = this.server.version;
|
||||
if (serverVersion && modPack.integerFactorioVersion > lib.integerFullVersion(serverVersion)) {
|
||||
throw new lib.RequestError(
|
||||
`Mod pack ${modPack.name} is for Factorio ${modPack.factorioVersion} ` +
|
||||
`which is newer than the Factorio ${serverVersion} this instance runs`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare instance for starting
|
||||
*
|
||||
@@ -951,7 +966,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
// Patch save with lua modules from plugins
|
||||
this.logger.verbose("Patching save");
|
||||
|
||||
let modules = await loadModules([...this.plugins.values()].map(plugin => plugin.info));
|
||||
let modules = await loadModules(this.loadedPlugins);
|
||||
await patch(this.path("saves", saveName), [...modules.values()]);
|
||||
return saveName;
|
||||
}
|
||||
@@ -986,7 +1001,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
await this.sendSaveListUpdate();
|
||||
await lib.invokeHook(this.plugins, "onStart");
|
||||
await this.hooks.start.invoke();
|
||||
|
||||
this.notifyStatus("running");
|
||||
}
|
||||
@@ -1011,7 +1026,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
await this.server.startScenario(scenario, seed, mapGenSettings, mapSettings);
|
||||
this._watchServerLogActions();
|
||||
|
||||
await lib.invokeHook(this.plugins, "onStart");
|
||||
await this.hooks.start.invoke();
|
||||
|
||||
this.notifyStatus("running");
|
||||
}
|
||||
@@ -1147,7 +1162,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
|
||||
// XXX this needs more thought to it
|
||||
if (this.server._state === "running") {
|
||||
await lib.invokeHook(this.plugins, "onStop");
|
||||
await this.hooks.stop.invoke();
|
||||
await this.server.stop();
|
||||
await this.sendSaveListUpdate();
|
||||
} else if (
|
||||
@@ -1166,17 +1181,17 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
async handleControllerConnectionEvent(event: lib.ControllerConnectionEvent) {
|
||||
await lib.invokeHook(this.plugins, "onControllerConnectionEvent", event.event);
|
||||
await this.hooks.controllerConnectionEvent.invoke(event.event);
|
||||
}
|
||||
|
||||
async handlePrepareControllerDisconnectRequest() {
|
||||
await lib.invokeHook(this.plugins, "onPrepareControllerDisconnect", this);
|
||||
await this.hooks.prepareControllerDisconnect.invoke(this);
|
||||
}
|
||||
|
||||
async handleInstanceMetricsRequest() {
|
||||
let results: ReturnType<typeof lib.serializeResult>[] = [];
|
||||
if (!["stopped", "stopping"].includes(this._status)) {
|
||||
let pluginResults = await lib.invokeHook(this.plugins, "onMetrics");
|
||||
let pluginResults = await this.hooks.metrics.collect();
|
||||
for (let metricIterator of pluginResults) {
|
||||
for await (let metric of metricIterator) {
|
||||
results.push(lib.serializeResult(metric));
|
||||
|
||||
Reference in new issue
Block a user