mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-17 22:33:13 +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
@@ -1,11 +1,14 @@
|
||||
import { BaseControllerPlugin, type Controller } from "@clusterio/controller";
|
||||
import type { Controller, ControllerPluginContext } 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;
|
||||
groups!: lib.SubscribableDatastore<messages.GroupRecord>;
|
||||
roleMappings!: lib.SubscribableDatastore<messages.RoleMappingRecord>;
|
||||
manualAssignments!: lib.SubscribableDatastore<messages.AssignmentRecord>;
|
||||
@@ -16,6 +19,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");
|
||||
@@ -70,6 +79,8 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
this.controller.handle(messages.RoleMappingDeleteRequest, this.handleRoleMappingDeleteRequest.bind(this));
|
||||
this.controller.handle(messages.RoleMappingGetRequest, this.handleRoleMappingGetRequest.bind(this));
|
||||
this.controller.handle(messages.RoleMappingListRequest, this.handleRoleMappingListRequest.bind(this));
|
||||
|
||||
this.controller.hooks.shutdown.attach(this.name, this.onShutdown.bind(this));
|
||||
}
|
||||
|
||||
async onShutdown() {
|
||||
@@ -440,3 +451,7 @@ export class ControllerPlugin extends BaseControllerPlugin {
|
||||
return Promise.all(playerNames.map(name => this.computeResolvedAssignment(name)));
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
@@ -17,7 +17,10 @@ export type IpcPlayerAssignments = {
|
||||
assignments: Record<string, number>,
|
||||
};
|
||||
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
export class InstancePlugin {
|
||||
instance: Instance;
|
||||
logger: lib.Logger;
|
||||
name: string;
|
||||
// Once only, don't send permissions for these groups
|
||||
// This is used for groups created on this instance that only need the controller generated id
|
||||
skipSendingPermissions = new Set<string>();
|
||||
@@ -26,12 +29,23 @@ export class InstancePlugin extends BaseInstancePlugin {
|
||||
// Track known online players so that we only apply assignment updates for them
|
||||
onlinePlayers = new Set<string>();
|
||||
|
||||
constructor(context: InstancePluginContext) {
|
||||
this.instance = context.instance;
|
||||
this.logger = context.logger;
|
||||
this.name = context.plugin.name;
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.instance.handle(messages.GroupUpdatedEvent, this.handleGroupUpdatedEvent.bind(this));
|
||||
this.instance.handle(messages.ResolvedAssignmentUpdatedEvent, this.handleResolvedAssignmentUpdatedEvent.bind(this));
|
||||
this.instance.server.handle(`exp_group:group_updated`, this.handleGroupUpdatedIPC.bind(this))
|
||||
this.instance.server.handle(`exp_group:group_deleted`, this.handleGroupDeletedIPC.bind(this))
|
||||
this.instance.server.handle(`exp_group:player_assignments`, this.handlePlayerAssignmentsIPC.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));
|
||||
hooks.playerEvent.attach(this.name, this.onPlayerEvent.bind(this));
|
||||
}
|
||||
|
||||
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
||||
@@ -169,3 +183,7 @@ export class InstancePlugin extends BaseInstancePlugin {
|
||||
await this.instance.sendRcon(`/sc exp_groups.${receiver}(helpers.json_to_table[=[${JSON.stringify(json)}]=])`, true)
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (context: InstancePluginContext) {
|
||||
await new InstancePlugin(context).init();
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { Modal, Form, Select } from "antd";
|
||||
|
||||
import { ControlContext, useUsers } from "@clusterio/web_ui";
|
||||
import * as messages from "../../messages.js";
|
||||
import type { WebPlugin } from "..";
|
||||
import { useGroups } from "..";
|
||||
|
||||
export default function AssignmentForm({ open, setOpen, initial }: {
|
||||
open: boolean,
|
||||
@@ -11,9 +11,8 @@ export default function AssignmentForm({ open, setOpen, initial }: {
|
||||
initial?: messages.AssignmentRecord,
|
||||
}) {
|
||||
const control = useContext(ControlContext);
|
||||
const plugin = control.plugins.get("exp_groups") as WebPlugin;
|
||||
|
||||
const [groups] = plugin.useGroups();
|
||||
const [groups] = useGroups();
|
||||
const [users] = useUsers();
|
||||
|
||||
const [form] = Form.useForm();
|
||||
|
||||
@@ -4,7 +4,7 @@ import { EditOutlined, SearchOutlined } from "@ant-design/icons";
|
||||
|
||||
import { ControlContext, useAccount } from "@clusterio/web_ui";
|
||||
import { AssignmentDeleteRequest, AssignmentRecord } from "../../messages.js";
|
||||
import type { WebPlugin } from "..";
|
||||
import { useAssignments, useGroups } from "..";
|
||||
|
||||
import AssignmentForm from "./AssignmentForm";
|
||||
import DeletedConfirm from "./DeleteConfirm";
|
||||
@@ -13,13 +13,12 @@ const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base"
|
||||
|
||||
export default function AssignmentsTable() {
|
||||
const control = useContext(ControlContext);
|
||||
const plugin = control.plugins.get("exp_groups") as WebPlugin;
|
||||
const account = useAccount();
|
||||
|
||||
const searchInput = useRef<InputRef>(null);
|
||||
|
||||
const [assignments, assignmentsSynced] = plugin.useAssignments();
|
||||
const [groups, groupsSynced] = plugin.useGroups();
|
||||
const [assignments, assignmentsSynced] = useAssignments();
|
||||
const [groups, groupsSynced] = useGroups();
|
||||
|
||||
const [editing, setEditing] = useState<AssignmentRecord | undefined>();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ControlContext, useAccount, useDefaultModPack, PageLayout, PageHeader,
|
||||
import DeletedConfirm from "./DeleteConfirm";
|
||||
|
||||
import * as messages from "../../messages.js";
|
||||
import type { WebPlugin } from "..";
|
||||
import { useGroups } from "..";
|
||||
|
||||
const DOMAIN_MAPPING = {
|
||||
"Admin": ["admin", "cheat", "permission", "infinity", "editor", "spawn"],
|
||||
@@ -34,14 +34,13 @@ function getDomain(name: string) {
|
||||
|
||||
export default function GroupViewPage() {
|
||||
const control = useContext(ControlContext);
|
||||
const plugin = control.plugins.get("exp_groups") as WebPlugin;
|
||||
const navigate = useNavigate();
|
||||
const account = useAccount();
|
||||
|
||||
const { id } = useParams();
|
||||
const groupId = Number(id);
|
||||
|
||||
const [groups, synced] = plugin.useGroups();
|
||||
const [groups, synced] = useGroups();
|
||||
const group = groups.get(groupId);
|
||||
|
||||
const [name, setName] = useState("");
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import React, { useState } from "react";
|
||||
import { Table } from "antd";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import { ControlContext } from "@clusterio/web_ui";
|
||||
import { GroupRecord } from "../../messages.js";
|
||||
import type { WebPlugin } from "..";
|
||||
import { useGroups } from "..";
|
||||
|
||||
import GroupForm from "./GroupForm";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
export default function GroupsTable() {
|
||||
const control = useContext(ControlContext);
|
||||
const plugin = control.plugins.get("exp_groups") as WebPlugin;
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [groups, synced] = plugin.useGroups();
|
||||
const [groups, synced] = useGroups();
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Modal, Form, Select, InputNumber, Switch, Alert } from "antd";
|
||||
|
||||
import { ControlContext, useRoles } from "@clusterio/web_ui";
|
||||
import { RoleMappingRecord, RoleMappingCreateRequest, RoleMappingUpdateRequest } from "../../messages.js";
|
||||
import type { WebPlugin } from "..";
|
||||
import { useGroups } from "..";
|
||||
|
||||
export default function RoleMappingForm({ open, setOpen, initial }: {
|
||||
open: boolean,
|
||||
@@ -11,9 +11,8 @@ export default function RoleMappingForm({ open, setOpen, initial }: {
|
||||
initial?: RoleMappingRecord,
|
||||
}) {
|
||||
const control = useContext(ControlContext);
|
||||
const plugin = control.plugins.get("exp_groups") as WebPlugin;
|
||||
|
||||
const [groups] = plugin.useGroups();
|
||||
const [groups] = useGroups();
|
||||
const [roles] = useRoles();
|
||||
|
||||
const [form] = Form.useForm();
|
||||
|
||||
@@ -4,18 +4,17 @@ import { EditOutlined } from "@ant-design/icons";
|
||||
|
||||
import { ControlContext, useAccount, useRoles } from "@clusterio/web_ui";
|
||||
import { RoleMappingRecord, RoleMappingDeleteRequest } from "../../messages.js";
|
||||
import type { WebPlugin } from "..";
|
||||
import { useGroups, useRoleMappings } from "..";
|
||||
|
||||
import RoleMappingForm from "./RoleMappingForm";
|
||||
import DeletedConfirm from "./DeleteConfirm";
|
||||
|
||||
export default function RoleMappingsTable() {
|
||||
const control = useContext(ControlContext);
|
||||
const plugin = control.plugins.get("exp_groups") as WebPlugin;
|
||||
const account = useAccount();
|
||||
|
||||
const [roleMappings, roleMappingsSynced] = plugin.useRoleMappings();
|
||||
const [groups, groupsSynced] = plugin.useGroups();
|
||||
const [roleMappings, roleMappingsSynced] = useRoleMappings();
|
||||
const [groups, groupsSynced] = useGroups();
|
||||
const [roles, rolesSynced] = useRoles();
|
||||
|
||||
const [editing, setEditing] = useState<RoleMappingRecord | undefined>();
|
||||
|
||||
+39
-37
@@ -1,5 +1,5 @@
|
||||
import React, { useState, useCallback, useSyncExternalStore } from "react";
|
||||
import { BaseWebPlugin, PageLayout, PageHeader, useAccount, SectionHeader } from "@clusterio/web_ui";
|
||||
import { WebPluginContext, PageLayout, PageHeader, useAccount, SectionHeader } from "@clusterio/web_ui";
|
||||
import { Button } from "antd";
|
||||
|
||||
import * as messages from "../messages.js";
|
||||
@@ -65,44 +65,46 @@ function ExpGroupsPage() {
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
export class WebPlugin extends BaseWebPlugin {
|
||||
groups = new lib.MapSubscriber(messages.GroupUpdatedEvent, this.control);
|
||||
assignments = new lib.MapSubscriber(messages.ManualAssignmentUpdatedEvent, this.control);
|
||||
roleMappings = new lib.MapSubscriber(messages.RoleMappingUpdatedEvent, this.control);
|
||||
let groups: lib.MapSubscriber<messages.GroupUpdatedEvent> | undefined;
|
||||
let assignments: lib.MapSubscriber<messages.ManualAssignmentUpdatedEvent> | undefined;
|
||||
let roleMappings: lib.MapSubscriber<messages.RoleMappingUpdatedEvent> | undefined;
|
||||
|
||||
async init() {
|
||||
this.pages = [
|
||||
{
|
||||
path: "/permission_groups",
|
||||
sidebarName: "Permission Groups",
|
||||
permission: (account => account.hasAnyPermission(
|
||||
"exp_groups.group.list",
|
||||
"exp_groups.assignment.list",
|
||||
"exp_groups.role_mapping.list",
|
||||
)),
|
||||
content: <ExpGroupsPage />,
|
||||
},
|
||||
{
|
||||
path: "/permission_groups/:id/view",
|
||||
sidebarPath: "/permission_groups",
|
||||
permission: "exp_groups.group.get",
|
||||
content: <GroupViewPage />,
|
||||
},
|
||||
];
|
||||
}
|
||||
export function useGroups() {
|
||||
const subscribe = useCallback((cb: () => void) => groups!.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => groups!.getSnapshot());
|
||||
}
|
||||
|
||||
useGroups() {
|
||||
const subscribe = useCallback((cb: () => void) => this.groups.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => this.groups.getSnapshot());
|
||||
}
|
||||
export function useAssignments() {
|
||||
const subscribe = useCallback((cb: () => void) => assignments!.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => assignments!.getSnapshot());
|
||||
}
|
||||
|
||||
useAssignments() {
|
||||
const subscribe = useCallback((cb: () => void) => this.assignments.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => this.assignments.getSnapshot());
|
||||
}
|
||||
export function useRoleMappings() {
|
||||
const subscribe = useCallback((cb: () => void) => roleMappings!.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => roleMappings!.getSnapshot());
|
||||
}
|
||||
|
||||
useRoleMappings() {
|
||||
const subscribe = useCallback((cb: () => void) => this.roleMappings.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => this.roleMappings.getSnapshot());
|
||||
}
|
||||
export default function registerPlugin(context: WebPluginContext) {
|
||||
context.control.hooks.pages.attach("exp_groups", () => [
|
||||
{
|
||||
path: "/permission_groups",
|
||||
sidebarName: "Permission Groups",
|
||||
permission: (account => account.hasAnyPermission(
|
||||
"exp_groups.group.list",
|
||||
"exp_groups.assignment.list",
|
||||
"exp_groups.role_mapping.list",
|
||||
)),
|
||||
content: <ExpGroupsPage />,
|
||||
},
|
||||
{
|
||||
path: "/permission_groups/:id/view",
|
||||
sidebarPath: "/permission_groups",
|
||||
permission: "exp_groups.group.get",
|
||||
content: <GroupViewPage />,
|
||||
},
|
||||
]);
|
||||
|
||||
groups = new lib.MapSubscriber(messages.GroupUpdatedEvent, context.control);
|
||||
assignments = new lib.MapSubscriber(messages.ManualAssignmentUpdatedEvent, context.control);
|
||||
roleMappings = new lib.MapSubscriber(messages.RoleMappingUpdatedEvent, context.control);
|
||||
}
|
||||
Reference in new issue
Block a user