mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-20 15:33:12 +09:00
Export init functions instead of plugin classes
clusterio logs "using deprecated class export" for every node entrypoint that exports a Base*Plugin subclass, and the web UI logs "using deprecated class hooks" for WebPlugin classes. Every entrypoint now has a default export function, like exp_auto_restart and the exp_roles web plugin. The classes stay where they hold state, but no longer extend the base classes. The constructor takes the load context and init() attaches the hooks the base class used to attach from the on* methods, so the tests can still call those methods directly. exp_server_ups sends its RCON command through instance.sendRcon with the plugin name, which is what the removed BaseInstancePlugin.sendRcon did. The instance entrypoints of exp_commands, exp_gui, exp_legacy, exp_util and exp_scenario were empty classes, kept only so the host loads the plugin and patches its module into the save. An empty function does the same. The exp_groups web components read the subscriptions through exported useGroups, useAssignments and useRoleMappings hooks instead of looking the plugin up in control.plugins, which only holds class plugins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ddf8497fa3
commit
0ff07c012c
26 files changed
+268
-131
No files matched your search
+20
-2
@@ -1,11 +1,14 @@
|
||||
import { BaseControllerPlugin, InstanceRecord, type Controller } from "@clusterio/controller";
|
||||
import type { Controller, ControllerPluginContext, InstanceRecord } from "@clusterio/controller";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "./messages.js";
|
||||
import * as path from "node:path";
|
||||
|
||||
const loaded = new WeakMap<Controller, ControllerPlugin>();
|
||||
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
export class ControllerPlugin {
|
||||
controller: Controller;
|
||||
logger: lib.Logger;
|
||||
name: string;
|
||||
roleMeta!: lib.SubscribableDatastore<messages.RoleMetaRecord>;
|
||||
|
||||
/** The plugin loaded on a controller, for the exp_scenario seed. */
|
||||
@@ -13,6 +16,12 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
return loaded.get(controller);
|
||||
}
|
||||
|
||||
constructor(context: ControllerPluginContext) {
|
||||
this.controller = context.controller;
|
||||
this.logger = context.logger;
|
||||
this.name = context.plugin.name;
|
||||
}
|
||||
|
||||
async init() {
|
||||
loaded.set(this.controller, this);
|
||||
const databaseDirectory = this.controller.config.get("controller.database_directory");
|
||||
@@ -45,6 +54,11 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
|
||||
this.controller.handle(messages.AssignmentListRequest, this.handleAssignmentListRequest.bind(this));
|
||||
this.controller.handle(messages.AssignmentUpdateRequest, this.handleAssignmentUpdateRequest.bind(this));
|
||||
|
||||
const hooks = this.controller.hooks;
|
||||
hooks.shutdown.attach(this.name, this.onShutdown.bind(this));
|
||||
hooks.controllerConfigFieldChanged.attach(this.name, this.onControllerConfigFieldChanged.bind(this));
|
||||
hooks.playerEvent.attach(this.name, this.onPlayerEvent.bind(this));
|
||||
}
|
||||
|
||||
async onShutdown() {
|
||||
@@ -319,3 +333,7 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (context: ControllerPluginContext) {
|
||||
await new ControllerPlugin(context).init();
|
||||
}
|
||||
+20
-2
@@ -1,4 +1,4 @@
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
import type { Instance, InstancePluginContext } from "@clusterio/host";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "./messages.js";
|
||||
|
||||
@@ -9,11 +9,25 @@ export type IpcAssignmentUpdate = {
|
||||
unassign: number[] | undefined,
|
||||
};
|
||||
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
export class InstancePlugin {
|
||||
instance: Instance;
|
||||
logger: lib.Logger;
|
||||
name: string;
|
||||
|
||||
constructor(context: InstancePluginContext) {
|
||||
this.instance = context.instance;
|
||||
this.logger = context.logger;
|
||||
this.name = context.plugin.name;
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.instance.handle(messages.RoleUpdatedEvent, this.handleRoleUpdatedEvent.bind(this));
|
||||
this.instance.handle(messages.AssignmentUpdatedEvent, this.handleAssignmentUpdatedEvent.bind(this));
|
||||
this.instance.server.handle("exp_roles:assignment_update", this.handleAssignmentUpdateIPC.bind(this));
|
||||
|
||||
const hooks = this.instance.hooks;
|
||||
hooks.instanceConfigFieldChanged.attach(this.name, this.onInstanceConfigFieldChanged.bind(this));
|
||||
hooks.start.attach(this.name, this.onStart.bind(this));
|
||||
}
|
||||
|
||||
get syncMode() {
|
||||
@@ -106,3 +120,7 @@ export class InstancePlugin extends BaseInstancePlugin {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (context: InstancePluginContext) {
|
||||
await new InstancePlugin(context).init();
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import t from "tap";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Controller } from "@clusterio/controller";
|
||||
import { ControllerPlugin } from "../dist/node/controller.js";
|
||||
import entrypoint, { ControllerPlugin } from "../dist/node/controller.js";
|
||||
import * as messages from "../dist/node/messages.js";
|
||||
|
||||
// The controller validates message classes against the link registry
|
||||
@@ -44,13 +44,23 @@ async function startPlugin(t2, { roles = [] } = {}) {
|
||||
permissionsUpdated(user);
|
||||
};
|
||||
|
||||
const plugin = new ControllerPlugin({ name: "exp_roles" }, controller, undefined, logger);
|
||||
const plugin = new ControllerPlugin({ plugin: { name: "exp_roles" }, controller, logger });
|
||||
await plugin.init();
|
||||
return { plugin, controller, state };
|
||||
}
|
||||
|
||||
const role = (id, name, permissions = []) => new lib.Role(id, name, "", new Set(permissions));
|
||||
|
||||
t.test("entrypoint", async t2 => {
|
||||
const controllerConfig = new lib.ControllerConfig("controller", { "controller.database_directory": t2.testdir() });
|
||||
const controller = new Controller(logger, [], controllerConfig);
|
||||
await entrypoint({ controller, logger, plugin: { name: "exp_roles" }, metrics: undefined });
|
||||
for (const hook of ["shutdown", "controllerConfigFieldChanged", "playerEvent"]) {
|
||||
t2.ok([...controller.hooks[hook].attached].includes("exp_roles"), `attached to ${hook}`);
|
||||
}
|
||||
await controller.hooks.shutdown.invoke();
|
||||
});
|
||||
|
||||
t.test("class ControllerPlugin", t2 => {
|
||||
t2.test(".init() and .sweepRoleMeta() manage the role properties", async t3 => {
|
||||
const { plugin } = await startPlugin(t3, { roles: [role(0, "Cluster Admin"), role(5, "Moderator")] });
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import t from "tap";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Instance } from "@clusterio/host";
|
||||
import { InstancePlugin } from "../dist/node/instance.js";
|
||||
import entrypoint, { InstancePlugin } from "../dist/node/instance.js";
|
||||
import { plugin as pluginDeclaration } from "../dist/node/index.js";
|
||||
import * as messages from "../dist/node/messages.js";
|
||||
|
||||
@@ -69,7 +69,7 @@ async function startPlugin(t2, { syncMode = "bidirectional", roles = sampleRoles
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const plugin = new InstancePlugin({ name: "exp_roles" }, instance, {});
|
||||
const plugin = new InstancePlugin({ plugin: { name: "exp_roles" }, instance, host: {}, logger });
|
||||
plugin.logger = { ...logger, warn: message => state.warnings.push(message) };
|
||||
await plugin.init();
|
||||
return { plugin, state };
|
||||
@@ -81,6 +81,20 @@ function decodeRcon(command) {
|
||||
return { receiver: match[1], payload: JSON.parse(match[2]) };
|
||||
}
|
||||
|
||||
t.test("entrypoint", async t2 => {
|
||||
const instanceConfig = new lib.InstanceConfig("host");
|
||||
instanceConfig.set("instance.id", 1);
|
||||
instanceConfig.set("instance.name", "test");
|
||||
const instance = new Instance(
|
||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
||||
);
|
||||
instance.server = { handle: () => {} };
|
||||
await entrypoint({ instance, host: {}, logger, plugin: { name: "exp_roles" } });
|
||||
for (const hook of ["instanceConfigFieldChanged", "start"]) {
|
||||
t2.ok([...instance.hooks[hook].attached].includes("exp_roles"), `attached to ${hook}`);
|
||||
}
|
||||
});
|
||||
|
||||
t.test("class InstancePlugin", t2 => {
|
||||
t2.test(".onStart() subscribes and initialises the lua module", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3, { syncMode: "enabled" });
|
||||
|
||||
Reference in new issue
Block a user