mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
import { BaseInstancePlugin } from "@clusterio/host";
|
|
import { ChatEvent } from "./messages.js";
|
|
|
|
/**
|
|
* Removes server-specific tags from messages.
|
|
*
|
|
* @param content - string to strip tags from.
|
|
* @returns stripped string.
|
|
*/
|
|
function removeTags(content: string): string {
|
|
return content.replace(/\[(?:gps|special-item|train|train-stop)=\S*?\]/gm, "");
|
|
}
|
|
|
|
export class InstancePlugin extends BaseInstancePlugin {
|
|
messageQueue: string[] = [];
|
|
|
|
async init() {
|
|
this.instance.handle(ChatEvent, this.handleChatEvent.bind(this));
|
|
}
|
|
|
|
onControllerConnectionEvent(event: string) {
|
|
if (event === "connect") {
|
|
for (let message of this.messageQueue) {
|
|
this.sendChat(message);
|
|
}
|
|
this.messageQueue = [];
|
|
}
|
|
}
|
|
|
|
async handleChatEvent(event: ChatEvent) {
|
|
// TODO check if cross server chat is enabled
|
|
let content = `[${event.instanceName}] ${removeTags(event.content)}`;
|
|
await this.sendRcon(`/sc game.print('${lib.escapeString(content)}')`, true);
|
|
}
|
|
|
|
sendChat(message: string) {
|
|
this.instance.sendTo("allInstances", new ChatEvent(this.instance.name, message));
|
|
}
|
|
|
|
async onOutput(output: lib.ParsedFactorioOutput) {
|
|
if (output.type === "action" && output.action === "CHAT") {
|
|
if (this.host.connector.connected) {
|
|
this.sendChat(output.message);
|
|
} else {
|
|
this.messageQueue.push(output.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO implement info command in lua?
|
|
}
|
|
|
|
// For testing only
|
|
export const _removeTags = removeTags;
|