From 58231cc02cb7d36724c49cac4853c4531ed477b4 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:40:22 +0000 Subject: [PATCH] Add IPC to templates and handle helper function server --- packages/create/templates/plugin-js/index.js | 14 +++++++++++--- .../create/templates/plugin-js/instance.js | 12 +++++++++--- .../templates/plugin-js/module/control.lua | 5 ++++- .../templates/plugin-js/module/module.json | 2 +- .../plugin-js/module/module_exports.lua | 7 ++++++- packages/create/templates/plugin-ts/index.ts | 17 ++++++++++++++--- .../create/templates/plugin-ts/instance.ts | 19 +++++++++++++++---- .../templates/plugin-ts/module/control.lua | 5 ++++- .../templates/plugin-ts/module/module.json | 2 +- .../plugin-ts/module/module_exports.lua | 7 ++++++- packages/host/src/server.ts | 7 +++++++ 11 files changed, 78 insertions(+), 19 deletions(-) diff --git a/packages/create/templates/plugin-js/index.js b/packages/create/templates/plugin-js/index.js index 32507df4..abb30dcd 100644 --- a/packages/create/templates/plugin-js/index.js +++ b/packages/create/templates/plugin-js/index.js @@ -24,10 +24,10 @@ lib.definePermission({ const plugin = { name: "// plugin_name //", title: "// plugin_name //", - description: "I didn't update my description", + description: "I didn't update my description",// [entry_points] // // entry_points // - // [controller] // - controlConfigFields: { + // [] //// [controller] // + controllerConfigFields: { "// plugin_name //.myControllerField": { title: "My Controller Field", description: "This should be removed", @@ -50,6 +50,14 @@ const plugin = { type: "string", initialValue: "Remove Me", }, + },// [] //// [ctl] // + controlConfigFields: { + "// plugin_name //.myControlField": { + title: "My Control Field", + description: "This should be removed", + type: "string", + initialValue: "Remove Me", + }, },// [] // messages: [ diff --git a/packages/create/templates/plugin-js/instance.js b/packages/create/templates/plugin-js/instance.js index 2a7020ef..99a6f19a 100644 --- a/packages/create/templates/plugin-js/instance.js +++ b/packages/create/templates/plugin-js/instance.js @@ -6,7 +6,8 @@ const { PluginExampleEvent, PluginExampleRequest } = require("./messages"); class InstancePlugin extends BaseInstancePlugin { async init() { this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this)); - this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this)); + this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));// [module] // + this.instance.server.handle("ipc-// plugin_name //-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));// [] // } async onInstanceConfigFieldChanged(field, curr, prev) { @@ -26,7 +27,8 @@ class InstancePlugin extends BaseInstancePlugin { } async handlePluginExampleEvent(event) { - this.logger.info(JSON.stringify(event)); + this.logger.info(JSON.stringify(event));// [module] // + this.sendRcon("/sc ipc_// plugin_name //.foo()");// [] // } async handlePluginExampleRequest(request) { @@ -35,7 +37,11 @@ class InstancePlugin extends BaseInstancePlugin { myResponseString: request.myString, myResponseNumbers: request.myNumberArray, }; - } + }// [module] // + + async handlePluginExampleIPC(event) { + this.logger.info(JSON.stringify(event)); + }// [] // } module.exports = { diff --git a/packages/create/templates/plugin-js/module/control.lua b/packages/create/templates/plugin-js/module/control.lua index ab32acd6..5e6a2e2a 100644 --- a/packages/create/templates/plugin-js/module/control.lua +++ b/packages/create/templates/plugin-js/module/control.lua @@ -14,7 +14,10 @@ MyModule.events[clusterio_api.events.on_server_startup] = function(event) end MyModule.events[defines.events.on_player_crafted_item] = function(event) - game.print(game.table_to_json(event)) + game.print(game.table_to_json(event))// [instance] // + clusterio_api.send_json("// plugin_name //-plugin_example_ipc", { + tick = game.tick, player_name = game.get_player(event.player_index).name + })// [] // end MyModule.on_nth_tick[300] = function() diff --git a/packages/create/templates/plugin-js/module/module.json b/packages/create/templates/plugin-js/module/module.json index 1edcac37..33915ac9 100644 --- a/packages/create/templates/plugin-js/module/module.json +++ b/packages/create/templates/plugin-js/module/module.json @@ -4,7 +4,7 @@ "control.lua" ], "require": [ - + // [instance] //"module_exports.lua"// [] // ], "dependencies": [ diff --git a/packages/create/templates/plugin-js/module/module_exports.lua b/packages/create/templates/plugin-js/module/module_exports.lua index cc0c5632..4399db98 100644 --- a/packages/create/templates/plugin-js/module/module_exports.lua +++ b/packages/create/templates/plugin-js/module/module_exports.lua @@ -1,2 +1,7 @@ +local MyModule = require("./control")// [instance] // -return require("./control") +-- Can contain anything you plan to use with send_rcon from functions, modules, or data +ipc_// plugin_name // = MyModule// [] // + +-- Can contain anything you want to allow other plugins to have access to, this example exposes the whole module +return MyModule diff --git a/packages/create/templates/plugin-ts/index.ts b/packages/create/templates/plugin-ts/index.ts index 48371429..92a7548f 100644 --- a/packages/create/templates/plugin-ts/index.ts +++ b/packages/create/templates/plugin-ts/index.ts @@ -29,16 +29,19 @@ declare module "@clusterio/lib" {// [controller] // }// [] //// [instance] // export interface InstanceConfigFields { "// plugin_name //.myInstanceField": string; + }// [] //// [ctl] // + export interface ControlConfigFields { + "// plugin_name //.myControlField": string; }// [] // } export const plugin: lib.PluginDeclaration = { name: "// plugin_name //", title: "// plugin_name //", - description: "I didn't update my description", + description: "I didn't update my description",// [entry_points] // // entry_points // - // [controller] // - controlConfigFields: { + // [] //// [controller] // + controllerConfigFields: { "// plugin_name //.myControllerField": { title: "My Controller Field", description: "This should be removed", @@ -61,6 +64,14 @@ export const plugin: lib.PluginDeclaration = { type: "string", initialValue: "Remove Me", }, + },// [] //// [ctl] // + controlConfigFields: { + "// plugin_name //.myControlField": { + title: "My Control Field", + description: "This should be removed", + type: "string", + initialValue: "Remove Me", + }, },// [] // messages: [ diff --git a/packages/create/templates/plugin-ts/instance.ts b/packages/create/templates/plugin-ts/instance.ts index f89429dd..02fce382 100644 --- a/packages/create/templates/plugin-ts/instance.ts +++ b/packages/create/templates/plugin-ts/instance.ts @@ -1,11 +1,17 @@ import * as lib from "@clusterio/lib"; import { BaseInstancePlugin } from "@clusterio/host"; -import { PluginExampleEvent, PluginExampleRequest } from "./messages"; +import { PluginExampleEvent, PluginExampleRequest } from "./messages";// [module] // + +type PuginExampleIPC = { + tick: number, + player_name: string, +};// [] // export class InstancePlugin extends BaseInstancePlugin { async init() { this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this)); - this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this)); + this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));// [module] // + this.instance.server.handle("ipc-// plugin_name //-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));// [] // } async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) { @@ -21,7 +27,8 @@ export class InstancePlugin extends BaseInstancePlugin { } async onPlayerEvent(event: lib.PlayerEvent) { - this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`); + this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);// [module] // + this.sendRcon("/sc ipc_// plugin_name //.foo()");// [] // } async handlePluginExampleEvent(event: PluginExampleEvent) { @@ -34,5 +41,9 @@ export class InstancePlugin extends BaseInstancePlugin { myResponseString: request.myString, myResponseNumbers: request.myNumberArray, }; - } + }// [module] // + + async handlePluginExampleIPC(event: PuginExampleIPC) { + this.logger.info(JSON.stringify(event)); + }// [] // } diff --git a/packages/create/templates/plugin-ts/module/control.lua b/packages/create/templates/plugin-ts/module/control.lua index ab32acd6..5e6a2e2a 100644 --- a/packages/create/templates/plugin-ts/module/control.lua +++ b/packages/create/templates/plugin-ts/module/control.lua @@ -14,7 +14,10 @@ MyModule.events[clusterio_api.events.on_server_startup] = function(event) end MyModule.events[defines.events.on_player_crafted_item] = function(event) - game.print(game.table_to_json(event)) + game.print(game.table_to_json(event))// [instance] // + clusterio_api.send_json("// plugin_name //-plugin_example_ipc", { + tick = game.tick, player_name = game.get_player(event.player_index).name + })// [] // end MyModule.on_nth_tick[300] = function() diff --git a/packages/create/templates/plugin-ts/module/module.json b/packages/create/templates/plugin-ts/module/module.json index 1edcac37..33915ac9 100644 --- a/packages/create/templates/plugin-ts/module/module.json +++ b/packages/create/templates/plugin-ts/module/module.json @@ -4,7 +4,7 @@ "control.lua" ], "require": [ - + // [instance] //"module_exports.lua"// [] // ], "dependencies": [ diff --git a/packages/create/templates/plugin-ts/module/module_exports.lua b/packages/create/templates/plugin-ts/module/module_exports.lua index cc0c5632..4399db98 100644 --- a/packages/create/templates/plugin-ts/module/module_exports.lua +++ b/packages/create/templates/plugin-ts/module/module_exports.lua @@ -1,2 +1,7 @@ +local MyModule = require("./control")// [instance] // -return require("./control") +-- Can contain anything you plan to use with send_rcon from functions, modules, or data +ipc_// plugin_name // = MyModule// [] // + +-- Can contain anything you want to allow other plugins to have access to, this example exposes the whole module +return MyModule diff --git a/packages/host/src/server.ts b/packages/host/src/server.ts index b34af3b9..2510903e 100644 --- a/packages/host/src/server.ts +++ b/packages/host/src/server.ts @@ -562,6 +562,13 @@ export class FactorioServer extends events.EventEmitter { } } + // TODO at the moment this is just a helper function, it has the potential for schema checking + handle(eventName: string, handler: (event: Event) => Promise) { + this.on(eventName, (event) => handler(event).catch(err => { + this._logger.error(`Error handling ipc event:\n${err.stack}`); + })); + } + async _handleIpc(line: Buffer) { let channelEnd = line.indexOf("?"); if (channelEnd === -1) {