mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Move base plugins to their respective packages
Move BaseControllerPlugin to controller/src/BaseControllerPlugin.ts Move BaseInstancePlugin to host/src/BaseInstancePlugin.ts Move BaseControlPlugin to ctl/src/BaseCtlPlugin.ts Move BaseWebPlugin to web_ui/src/BaseWebPlugin.ts This solves the circular package dependency problem of the base plugins needing types that are available only in the dependencies to lib.
This commit is contained in:
1 parent
63dc370ea0
commit
808eda0513
51 files changed
+1073
-1060
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { BaseControlPlugin, CommandTree, Command } from "@clusterio/lib";
|
||||
import { CommandTree, Command } from "@clusterio/lib";
|
||||
import { BaseCtlPlugin } from "@clusterio/ctl";
|
||||
import { ChatEvent } from "./messages";
|
||||
|
||||
const globalChatCommands = new CommandTree({
|
||||
@@ -13,7 +14,7 @@ globalChatCommands.add(new Command({
|
||||
},
|
||||
}));
|
||||
|
||||
export class ControlPlugin extends BaseControlPlugin {
|
||||
export class CtlPlugin extends BaseCtlPlugin {
|
||||
async addCommands(rootCommand: CommandTree) {
|
||||
rootCommand.add(globalChatCommands);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ const info: lib.PluginDeclaration = {
|
||||
title: "Global Chat",
|
||||
description: "Forwards chat between instances.",
|
||||
instanceEntrypoint: "dist/plugin/instance",
|
||||
controlEntrypoint: "dist/plugin/control",
|
||||
ctlEntrypoint: "dist/plugin/control",
|
||||
|
||||
messages: [
|
||||
ChatEvent,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
import { ChatEvent } from "./messages";
|
||||
|
||||
/**
|
||||
@@ -11,7 +12,7 @@ function removeTags(content: string): string {
|
||||
return content.replace(/(\[gps=-?\d+,-?\d+\]|\[train=\d+\])/g, "");
|
||||
}
|
||||
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
messageQueue: string[] = [];
|
||||
|
||||
async init() {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type InstanceInfo from "@clusterio/controller/src/InstanceInfo";
|
||||
import type Controller from "@clusterio/controller/src/Controller";
|
||||
import { BaseControllerPlugin, type InstanceInfo } from "@clusterio/controller";
|
||||
import type { IpcPlayerData } from "./instance";
|
||||
|
||||
import fs from "fs-extra";
|
||||
@@ -36,7 +35,7 @@ async function saveDatabase(
|
||||
}
|
||||
}
|
||||
|
||||
export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
acquiredPlayers!: Map<string, { instanceId: number, expires?: number }>;
|
||||
playerDatastore!: Map<string, IpcPlayerData>;
|
||||
autosaveId!: ReturnType<typeof setInterval>;
|
||||
@@ -48,7 +47,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
saveDatabase(this.controller.config, this.playerDatastore, this.logger).catch(err => {
|
||||
this.logger.error(`Unexpected error autosaving player data:\n${err.stack}`);
|
||||
});
|
||||
}, this.controller.config.get("inventory_sync.autosave_interval") * 1000);
|
||||
}, this.controller.config.get("inventory_sync.autosave_interval") as number * 1000);
|
||||
|
||||
this.controller.handle(msg.AcquireRequest, this.handleAcquireRequest.bind(this));
|
||||
this.controller.handle(msg.ReleaseRequest, this.handleReleaseRequest.bind(this));
|
||||
@@ -68,7 +67,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
}
|
||||
|
||||
if (["unknown", "stopped"].includes(instance.status)) {
|
||||
let timeout = this.controller.config.get("inventory_sync.player_lock_timeout") * 1000;
|
||||
let timeout = this.controller.config.get("inventory_sync.player_lock_timeout") as number * 1000;
|
||||
for (let acquisitonRecord of this.acquiredPlayers.values()) {
|
||||
if (acquisitonRecord.instanceId === instanceId && !acquisitonRecord.expires) {
|
||||
acquisitonRecord.expires = Date.now() + timeout;
|
||||
@@ -90,7 +89,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
if (
|
||||
!acquisitionRecord
|
||||
|| acquisitionRecord.instanceId === instanceId
|
||||
|| !this.controller.instances.has(acquisitionRecord.instanceId)
|
||||
|| !this.controller.instances!.has(acquisitionRecord.instanceId)
|
||||
|| acquisitionRecord.expires && acquisitionRecord.expires < Date.now()
|
||||
) {
|
||||
this.acquiredPlayers.set(playerName, { instanceId });
|
||||
@@ -104,7 +103,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
let { instanceId, playerName } = request;
|
||||
if (!this.acquire(instanceId, playerName)) {
|
||||
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||
let instance = this.controller.instances.get(acquisitionRecord!.instanceId);
|
||||
let instance = this.controller.instances!.get(acquisitionRecord!.instanceId)!;
|
||||
return {
|
||||
status: "busy",
|
||||
message: instance.config.get("instance.name"),
|
||||
@@ -133,7 +132,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
|
||||
async handleUploadRequest(request: msg.UploadRequest) {
|
||||
let { instanceId, playerName, playerData } = request;
|
||||
let instanceName = this.controller.instances.get(instanceId).config.get("instance.name");
|
||||
let instanceName = this.controller.instances!.get(instanceId)!.config.get("instance.name");
|
||||
let store = true;
|
||||
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||
if (!acquisitionRecord) {
|
||||
@@ -167,7 +166,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
|
||||
async handleDownloadRequest(request: msg.DownloadRequest) {
|
||||
let { instanceId, playerName } = request;
|
||||
let instanceName = this.controller.instances.get(instanceId).config.get("instance.name");
|
||||
let instanceName = this.controller.instances!.get(instanceId)!.config.get("instance.name");
|
||||
|
||||
let acquisitionRecord = this.acquiredPlayers.get(playerName);
|
||||
if (!acquisitionRecord) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
import {
|
||||
AcquireRequest, AcquireResponse, ReleaseRequest, UploadRequest, DownloadRequest, DownloadResponse,
|
||||
} from "./messages";
|
||||
@@ -44,7 +45,7 @@ function chunkify(chunkSize: number, string: string): string[] {
|
||||
return string.match(new RegExp(`.{1,${chunkSize}}`, "g")) || [];
|
||||
}
|
||||
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
playersToRelease!: Set<string>;
|
||||
disconnecting!: boolean;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { PageLayout, ControlContext } from "@clusterio/web_ui";
|
||||
import { BaseWebPlugin, PageLayout, ControlContext } from "@clusterio/web_ui";
|
||||
import { DatabaseStatsRequest } from "../dist/plugin/messages";
|
||||
|
||||
import "./index.css";
|
||||
@@ -28,7 +27,7 @@ function InventoryPage() {
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
export class WebPlugin extends lib.BaseWebPlugin {
|
||||
export class WebPlugin extends BaseWebPlugin {
|
||||
async init() {
|
||||
this.pages = [
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ import util from "util";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
const { basicType } = lib;
|
||||
|
||||
import { FetchPlayerCodeRequest, SetVerifyCodeRequest } from "./messages";
|
||||
@@ -27,7 +28,7 @@ async function generateCode(length: number): Promise<string> {
|
||||
|
||||
type PlayerCode = { playerCode: string, verifyCode: string | null, expires: number };
|
||||
|
||||
export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
players!: Map<string, PlayerCode>;
|
||||
|
||||
async init() {
|
||||
@@ -46,7 +47,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
|
||||
this.controller.app.get("/api/player_auth/servers", (req: Request, res: Response) => {
|
||||
let servers = [];
|
||||
for (let instance of this.controller.instances.values()) {
|
||||
for (let instance of this.controller.instances!.values()) {
|
||||
if (instance.status === "running" && instance.config.get("player_auth.load_plugin")) {
|
||||
servers.push(instance.config.get("factorio.settings")["name"] || "unnamed server");
|
||||
}
|
||||
@@ -88,7 +89,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
|
||||
for (let entry of this.players.values()) {
|
||||
if (entry.playerCode === playerCode && entry.expires > Date.now()) {
|
||||
let verifyCode = await generateCode(this.controller.config.get("player_auth.code_length"));
|
||||
let verifyCode = await generateCode(this.controller.config.get("player_auth.code_length") as number);
|
||||
let secret = Buffer.from(this.controller.config.get("controller.auth_secret"), "base64");
|
||||
let verifyToken = jwt.sign(
|
||||
{
|
||||
@@ -172,8 +173,8 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
}
|
||||
|
||||
async handleFetchPlayerCodeRequest(request: FetchPlayerCodeRequest) {
|
||||
let playerCode = await generateCode(this.controller.config.get("player_auth.code_length"));
|
||||
let expires = Date.now() + this.controller.config.get("player_auth.code_timeout") * 1000;
|
||||
let playerCode = await generateCode(this.controller.config.get("player_auth.code_length") as number);
|
||||
let expires = Date.now() + (this.controller.config.get("player_auth.code_timeout") as number) * 1000;
|
||||
this.players.set(request.player, { playerCode, verifyCode: null, expires });
|
||||
return { player_code: playerCode, controller_url: this.controller.getControllerUrl() };
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
|
||||
import { FetchPlayerCodeRequest, SetVerifyCodeRequest } from "./messages";
|
||||
|
||||
@@ -11,7 +11,7 @@ type IpcPlayerAuth = {
|
||||
verify_code: string,
|
||||
}
|
||||
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
async init() {
|
||||
if (!this.instance.config.get("factorio.enable_save_patching")) {
|
||||
throw new Error("player_auth plugin requires save patching.");
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { notifyErrorHandler } from "@clusterio/web_ui";
|
||||
import { BaseWebPlugin, notifyErrorHandler } from "@clusterio/web_ui";
|
||||
import { Button, Form, Input, Spin, Typography } from "antd";
|
||||
|
||||
import "./index.css";
|
||||
@@ -146,7 +145,7 @@ function LoginForm(props) {
|
||||
}
|
||||
|
||||
|
||||
export class WebPlugin extends lib.BaseWebPlugin {
|
||||
export class WebPlugin extends BaseWebPlugin {
|
||||
async init() {
|
||||
this.logger.info("Player Auth init");
|
||||
this.loginForms = [{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
import { Static } from "@sinclair/typebox";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
@@ -48,7 +49,7 @@ async function saveTechnologies(
|
||||
await lib.safeOutputFile(filePath, JSON.stringify([...technologies.entries()], null, "\t"));
|
||||
}
|
||||
|
||||
export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
technologies!: Map<string, Technology>;
|
||||
progressRateLimiter!: lib.RateLimiter;
|
||||
progressBroadcastId!: ReturnType<typeof setInterval> | null;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
import {
|
||||
ContributionEvent,
|
||||
ProgressEvent,
|
||||
@@ -18,7 +19,7 @@ type IpcFinished = {
|
||||
level: number,
|
||||
};
|
||||
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
syncStarted!: boolean;
|
||||
|
||||
unexpectedError(err: Error) {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
|
||||
const { Gauge } = lib;
|
||||
|
||||
type IpcStats = {
|
||||
@@ -59,7 +61,7 @@ function setForceFlowStatistic(
|
||||
}
|
||||
|
||||
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
async init() {
|
||||
if (!this.instance.config.get("factorio.enable_save_patching")) {
|
||||
throw new Error("statistics_exporter plugin requires save patching.");
|
||||
@@ -107,7 +109,7 @@ export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
// take a long time for the command to go through if the command
|
||||
// stream is overloaded. Should the timeout be exceeded the
|
||||
// previous values for the metrics will end up being sent to controller.
|
||||
let timeout = this.instance.config.get("statistics_exporter.command_timeout") * 1000;
|
||||
let timeout = this.instance.config.get("statistics_exporter.command_timeout") as number * 1000;
|
||||
await lib.timeout(this.gatherMetrics(), timeout, undefined);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type ControlConnection from "@clusterio/controller/src/ControlConnection";
|
||||
import { BaseControllerPlugin, type ControlConnection } from "@clusterio/controller";
|
||||
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
@@ -70,7 +70,7 @@ async function saveDatabase(
|
||||
}
|
||||
}
|
||||
|
||||
export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
items!: lib.ItemDatabase;
|
||||
itemUpdateRateLimiter!: lib.RateLimiter;
|
||||
itemsLastUpdate!: Map<string, number>;
|
||||
@@ -96,7 +96,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
saveDatabase(this.controller.config, this.items, this.logger).catch(err => {
|
||||
this.logger.error(`Unexpected error autosaving items:\n${err.stack}`);
|
||||
});
|
||||
}, this.controller.config.get("subspace_storage.autosave_interval") * 1000);
|
||||
}, this.controller.config.get("subspace_storage.autosave_interval") as number * 1000);
|
||||
|
||||
this.neuralDole = new dole.NeuralDole({ items: this.items });
|
||||
this.doleMagicId = setInterval(() => {
|
||||
@@ -180,7 +180,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
}
|
||||
|
||||
} else {
|
||||
let instance = this.controller.instances.get(instanceId);
|
||||
let instance = this.controller.instances!.get(instanceId);
|
||||
let instanceName = instance ? instance.config.get("instance.name") : "unkonwn";
|
||||
|
||||
// use fancy neural net to calculate a "fair" dole division rate.
|
||||
@@ -200,7 +200,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
let count = dole.doleDivider({
|
||||
object: { name: item.name, count: item.count, instanceId, instanceName },
|
||||
items: this.items,
|
||||
logItemTransfers: this.controller.config.get("subspace_storage.log_item_transfers"),
|
||||
logItemTransfers: this.controller.config.get("subspace_storage.log_item_transfers") as boolean,
|
||||
logger: this.logger,
|
||||
});
|
||||
if (count > 0) {
|
||||
@@ -230,7 +230,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
}
|
||||
|
||||
async handleSetStorageSubscriptionRequest(request: SetStorageSubscriptionRequest, src: lib.Address) {
|
||||
let link = this.controller.wsServer.controlConnections.get(src.id);
|
||||
let link = this.controller.wsServer.controlConnections.get(src.id)!;
|
||||
if (request.storage) {
|
||||
this.subscribedControlLinks.add(link);
|
||||
} else {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
|
||||
import {
|
||||
PlaceEvent,
|
||||
@@ -10,7 +11,7 @@ import {
|
||||
|
||||
type IpcItems = [string, number][];
|
||||
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
pendingTasks!: Set<any>;
|
||||
pingId?: ReturnType<typeof setTimeout>;
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ import React, { useContext, useEffect, useState } from "react";
|
||||
import { Input, Table, Typography } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { notifyErrorHandler, useItemMetadata, useLocale, PageLayout, ControlContext } from "@clusterio/web_ui";
|
||||
import {
|
||||
BaseWebPlugin, PageLayout, ControlContext,
|
||||
notifyErrorHandler, useItemMetadata, useLocale,
|
||||
} from "@clusterio/web_ui";
|
||||
import { GetStorageRequest, SetStorageSubscriptionRequest } from "../dist/plugin/messages";
|
||||
|
||||
import "./index.css";
|
||||
@@ -114,7 +117,7 @@ function StoragePage() {
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
export class WebPlugin extends lib.BaseWebPlugin {
|
||||
export class WebPlugin extends BaseWebPlugin {
|
||||
async init() {
|
||||
this.pages = [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user