Write plugins without state shared elsewhere as functions (#492)

The instance plugins of exp_groups, exp_reports, exp_roles, exp_scenario
and exp_server_ups, and the controller plugins of exp_reports and
exp_scenario, were classes only to hold their handlers. They are now
init functions with closures, like clusterio's own plugins.

The exp_roles and exp_groups controller plugins stay classes because
exp_scenario reaches their datastores through get(controller).
AutoRestart stays a class because its tests step check() with a fake
clock.

Tests spy on handle() and the hooks to call the handlers. The reports
webhook tests stub fetch instead of replacing postWebhook, and the
feature reconcile test writes the stored records to disk before start.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BastiaanandClaude Opus 5.5 authored and GitHub committed 2026-10-13 21:50:15 +01:00
1 parent 26a801da5e
commit 68a964cf57
12 files changed
+709 -820

No files matched your search

+31 -51
View File
@@ -1,63 +1,25 @@
import type { Instance, InstancePluginContext } from "@clusterio/host";
import type { InstancePluginContext } from "@clusterio/host";
import * as lib from "@clusterio/lib";
import * as messages from "./messages.js";
import { features, FeatureValue } from "./features.js";
/** Sends feature config to the lua side, see module/features.lua. */
export class InstancePlugin {
export default async function (context: InstancePluginContext) {
const { instance, plugin } = context;
/** Rcon is up and the lua side can take updates */
started = false;
private constructor(
public instance: Instance,
public logger: lib.Logger,
public name: string,
) {}
static async fromContext(context: InstancePluginContext) {
const instance = context.instance;
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
instance.handle(messages.FeatureUpdatedEvent, plugin.handleFeatureUpdatedEvent.bind(plugin));
instance.hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
instance.hooks.exit.attach(plugin.name, plugin.onExit.bind(plugin));
instance.hooks.controllerConnectionEvent.attach(plugin.name, plugin.onControllerConnectionEvent.bind(plugin));
return plugin;
}
async onStart() {
this.started = true;
await this.syncFeatures();
}
async onExit() {
this.started = false;
}
/** A new session means the controller restarted and forgot the subscription. */
async onControllerConnectionEvent(event: "connect" | "drop" | "resume" | "close") {
if (event === "connect" && this.started) {
await this.syncFeatures();
}
}
let started = false;
/** Subscribe, then send every feature */
async syncFeatures() {
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
async function syncFeatures() {
await instance.sendTo("controller", new lib.SubscriptionRequest(
`exp_scenario:${messages.FeatureUpdatedEvent.name}`, "subscribe", Date.now(),
));
const features = await this.instance.sendTo("controller", new messages.FeatureListRequest());
await this.luaSendFeatures(features);
}
async handleFeatureUpdatedEvent(event: messages.FeatureUpdatedEvent) {
if (this.started) {
await this.luaSendFeatures(event.updates.filter(feature => !feature.isDeleted));
}
const records = await instance.sendTo("controller", new messages.FeatureListRequest());
await luaSendFeatures(records);
}
/** Every value of each feature is sent, so the lua side sets them all */
async luaSendFeatures(records: messages.FeatureRecord[]) {
async function luaSendFeatures(records: messages.FeatureRecord[]) {
if (!records.length) {
return;
}
@@ -75,12 +37,30 @@ export class InstancePlugin {
while (json.includes(`]${level}]`)) {
level += "=";
}
await this.instance.sendRcon(
await instance.sendRcon(
`/sc exp_scenario.features.receive_update(helpers.json_to_table[${level}[${json}]${level}])`, true,
);
}
}
export default async function (context: InstancePluginContext) {
await InstancePlugin.fromContext(context);
instance.handle(messages.FeatureUpdatedEvent, async event => {
if (started) {
await luaSendFeatures(event.updates.filter(feature => !feature.isDeleted));
}
});
instance.hooks.start.attach(plugin.name, async () => {
started = true;
await syncFeatures();
});
instance.hooks.exit.attach(plugin.name, async () => {
started = false;
});
// A new session means the controller restarted and forgot the subscription
instance.hooks.controllerConnectionEvent.attach(plugin.name, async event => {
if (event === "connect" && started) {
await syncFeatures();
}
});
}