Change to new preprocessor style

This commit is contained in:
Cooldude2606 committed 2024-02-27 23:07:38 +00:00
1 parent ec19d5456e
commit 62abfa903b
37 files changed
+692 -2193

No files matched your search

@@ -1,3 +0,0 @@
dist/
node_modules/
package-lock.json
File diff suppressed because it is too large. Load diff
@@ -1,2 +0,0 @@
webpack.config.js
dist/web/build
@@ -1,25 +1,37 @@
import * as lib from "@clusterio/lib";
import { BaseControllerPlugin, InstanceInfo } from "@clusterio/controller";
import { PluginExampleEvent, PluginExampleRequest,/*/ [subscribable] /*/ ExampleSubscribableUpdate, ExampleSubscribableValue/*/ [] /*/ } from "./messages";
export class ControllerPlugin extends BaseControllerPlugin {// [subscribable] //
import {
PluginExampleEvent, PluginExampleRequest,
//%if controller & web // Subscribing requires web content and the controller
ExampleSubscribableUpdate, ExampleSubscribableValue,
//%endif
} from "./messages";
export class ControllerPlugin extends BaseControllerPlugin {
//%if controller & web // Subscribing requires web content and the controller
exampleDatabase!: Map<string, ExampleSubscribableValue>;
storageDirty = false;
// [] //
//%endif
async init() {
this.controller.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.controller.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));// [subscribable] //
this.controller.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%if controller & web // Subscribing requires web content and the controller
this.controller.subscriptions.handle(ExampleSubscribableUpdate, this.handleExampleSubscription.bind(this));
this.exampleDatabase = new Map();// [] //
this.exampleDatabase = new Map(); // If needed, replace with loading from database file
//%endif
}
async onControllerConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
this.logger.info(`controller::onControllerConfigFieldChanged ${field}`);
}// [instance] //
}
//%if instance
async onInstanceConfigFieldChanged(instance: InstanceInfo, field: string, curr: unknown, prev: unknown) {
this.logger.info(`controller::onInstanceConfigFieldChanged ${instance.id} ${field}`);
}// [] //
}
//%endif
async onSaveData() {
this.logger.info("controller::onSaveData");
@@ -43,12 +55,14 @@ export class ControllerPlugin extends BaseControllerPlugin {// [subscribable] //
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}// [subscribable] //
}
//%if controller & web // Subscribing requires web content and the controller
async handleExampleSubscription(request: lib.SubscriptionRequest) {
const values = [...this.exampleDatabase.values()].filter(
value => value.updatedAtMs > request.lastRequestTimeMs,
);
return values.length ? new ExampleSubscribableUpdate(values) : null;
}// [] //
}
//%endif
}
+1 -1
View File
@@ -5,7 +5,7 @@ import { PluginExampleEvent, PluginExampleRequest } from "./messages";
/* eslint-disable no-console */
const pluginCommands = new CommandTree({
name: "// plugin_name //", description: "The default description I forgot to change",
name: "__plugin_name__", description: "The default description I forgot to change",
});
pluginCommands.add(new Command({
+64 -31
View File
@@ -2,86 +2,119 @@ import * as lib from "@clusterio/lib";
import * as Messages from "./messages";
lib.definePermission({
name: "// plugin_name //.example.permission.event",
name: "__plugin_name__.example.permission.event",
title: "Example permission event",
description: "My plugin's example permission that I forgot to remove",
});
lib.definePermission({
name: "// plugin_name //.example.permission.request",
name: "__plugin_name__.example.permission.request",
title: "Example permission request",
description: "My plugin's example permission that I forgot to remove",
});// [subscribable] //
});
//%if controller & web // Subscribing requires web content and the controller
lib.definePermission({
name: "// plugin_name //.example.permission.subscribe",
name: "__plugin_name__.example.permission.subscribe",
title: "Example permission subscribe",
description: "My plugin's example permission that I forgot to remove",
});// [] //// [web] //
});
//%endif
//%if web
lib.definePermission({
name: "// plugin_name //.page.view",
name: "__plugin_name__.page.view",
title: "Example page view permission",
description: "My plugin's example page permission that I forgot to remove",
});// [] //
});
//%endif
declare module "@clusterio/lib" {// [controller] //
declare module "@clusterio/lib" {
//%if controller
export interface ControllerConfigFields {
"// plugin_name //.myControllerField": string;
}// [] //// [host] //
"__plugin_name__.myControllerField": string;
}
//%endif
//%if host
export interface HostConfigFields {
"// plugin_name //.myHostField": string;
}// [] //// [instance] //
"__plugin_name__.myHostField": string;
}
//%endif
//%if instance
export interface InstanceConfigFields {
"// plugin_name //.myInstanceField": string;
}// [] //// [ctl] //
"__plugin_name__.myInstanceField": string;
}
//%endif
//%if ctl
export interface ControlConfigFields {
"// plugin_name //.myControlField": string;
}// [] //
"__plugin_name__.myControlField": string;
}
//%endif
}
export const plugin: lib.PluginDeclaration = {
name: "// plugin_name //",
title: "// plugin_name //",
description: "I didn't update my description",// [entry_points] //
// entry_points //
// [] //// [controller] //
name: "__plugin_name__",
title: "__plugin_name__",
description: "I didn't update my description",
//%if controller
controllerEntrypoint: "./dist/node/controller",
controllerConfigFields: {
"// plugin_name //.myControllerField": {
"__plugin_name__.myControllerField": {
title: "My Controller Field",
description: "This should be removed",
type: "string",
initialValue: "Remove Me",
},
},// [] //// [host] //
},
//%endif
//%if host
hostEntrypoint: "./dist/node/host",
hostConfigFields: {
"// plugin_name //.myHostField": {
"__plugin_name__.myHostField": {
title: "My Host Field",
description: "This should be removed",
type: "string",
initialValue: "Remove Me",
},
},// [] //// [instance] //
},
//%endif
//%if instance
instanceEntrypoint: "./dist/node/instance",
instanceConfigFields: {
"// plugin_name //.myInstanceField": {
"__plugin_name__.myInstanceField": {
title: "My Instance Field",
description: "This should be removed",
type: "string",
initialValue: "Remove Me",
},
},// [] //// [ctl] //
},
//%endif
//%if ctl
ctlEntrypoint: "./dist/node/ctl",
controlConfigFields: {
"// plugin_name //.myControlField": {
"__plugin_name__.myControlField": {
title: "My Control Field",
description: "This should be removed",
type: "string",
initialValue: "Remove Me",
},
},// [] //
},
//%endif
messages: [
Messages.PluginExampleEvent,
Messages.PluginExampleRequest,// [subscribable] //
Messages.ExampleSubscribableUpdate,// [] //
Messages.PluginExampleRequest,
//%if controller & web // Subscribing requires web content and the controller
Messages.ExampleSubscribableUpdate,
//%endif
],
//%if controller | web // The controller always includes web entry even if there is no content
webEntrypoint: "./web",
routes: [],
//%endif
};
@@ -1,17 +1,21 @@
import * as lib from "@clusterio/lib";
import { BaseInstancePlugin } from "@clusterio/host";
import { PluginExampleEvent, PluginExampleRequest } from "./messages";// [module] //
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
//%if module
type PuginExampleIPC = {
tick: number,
player_name: string,
};// [] //
};
//%endif
export class InstancePlugin extends BaseInstancePlugin {
async init() {
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.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));// [] //
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%if module
this.instance.server.handle("ipc-__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));
//%endif
}
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
@@ -27,8 +31,10 @@ export class InstancePlugin extends BaseInstancePlugin {
}
async onPlayerEvent(event: lib.PlayerEvent) {
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);// [module] //
this.sendRcon("/sc ipc_// plugin_name //.foo()");// [] //
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);
//%if module
this.sendRcon("/sc ipc___plugin_name__.foo()");
//%endif
}
async handlePluginExampleEvent(event: PluginExampleEvent) {
@@ -41,9 +47,11 @@ export class InstancePlugin extends BaseInstancePlugin {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}// [module] //
}
//%if module
async handlePluginExampleIPC(event: PuginExampleIPC) {
this.logger.info(JSON.stringify(event));
}// [] //
}
//%endif
}
@@ -6,8 +6,8 @@ export class PluginExampleEvent {
static type = "event" as const;
static src = ["host", "control"] as const;
static dst = ["controller", "host", "instance"] as const;
static plugin = "// plugin_name //" as const;
static permission = "// plugin_name //.example.permission.event";
static plugin = "__plugin_name__" as const;
static permission = "__plugin_name__.example.permission.event";
constructor(
public myString: string,
@@ -30,8 +30,8 @@ export class PluginExampleRequest {
static type = "request" as const;
static src = ["host", "control"] as const;
static dst = ["controller", "host", "instance"] as const;
static plugin = "// plugin_name //" as const;
static permission = "// plugin_name //.example.permission.request";
static plugin = "__plugin_name__" as const;
static permission = "__plugin_name__.example.permission.request";
constructor(
public myString: string,
@@ -52,7 +52,8 @@ export class PluginExampleRequest {
"myResponseString": Type.String(),
"myResponseNumbers": Type.Array(Type.Number()),
}));
}// [subscribable] //
}
//%if controller & web // Subscribing requires web content and the controller
export class ExampleSubscribableValue {
constructor(
@@ -78,8 +79,8 @@ export class ExampleSubscribableUpdate {
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static plugin = "// plugin_name //" as const;
static permission = "// plugin_name //.example.permission.subscribe";
static plugin = "__plugin_name__" as const;
static permission = "__plugin_name__.example.permission.subscribe";
constructor(
public updates: ExampleSubscribableValue[],
@@ -92,4 +93,5 @@ export class ExampleSubscribableUpdate {
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates.map(update => ExampleSubscribableValue.fromJSON(update)));
}
}// [] //// [] //
}
//%endif
@@ -1,52 +0,0 @@
local clusterio_api = require("modules/clusterio/api")
--- Top level module table, contains event handlers and public methods
local MyModule = {
events = {},
on_nth_tick = {},
}
--- global is 'synced' between players, you should use your plugin name to avoid conflicts
-- setupGlobalData should either be removed or called during clusterio_api.events.on_server_startup
local globalData = {}
local function setupGlobalData()
if global["// plugin_name //"] == nil then
global["// plugin_name //"] = {
-- starting values go here
}
end
globalData = global["// plugin_name //"]
end
--- Public methods should be available though your top level module table
function MyModule.foo()
game.print("foo")
end
--- Private methods should be local to the file, this will prevent others from calling it
local function bar()
game.print("bar")
end
--- Clusterio provides a few custom events, on_server_startup is the most useful and should be used in place of on_load
MyModule.events[clusterio_api.events.on_server_startup] = function(event)
setupGlobalData()
game.print(game.table_to_json(event))
end
--- Factorio events are accessible through defines.events, you can have one handler per event per module
MyModule.events[defines.events.on_player_crafted_item] = function(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
--- Nth tick is a special case that requires its own table, the index represents the time period between calls in ticks
MyModule.on_nth_tick[300] = function()
game.print(game.tick)
bar()
end
--- Always return the top level module table from control, this is how clusterio will access your event handlers
return MyModule
@@ -1,12 +0,0 @@
{
"name": "// plugin_name //",
"load": [
"control.lua"
],
"require": [
// [instance] //"module_exports.lua"// [] //
],
"dependencies": [
]
}
@@ -1,8 +0,0 @@
local MyModule = require("./control")// [instance] //
--- Can contain anything you plan to use with send_rcon including functions, modules, or data
-- luacheck: globals ipc_// plugin_name //
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
@@ -1,29 +0,0 @@
{
"name": "// plugin_name //",
"version": "0.1.0",
"description": "",
"main": "dist/plugin/index.js",
"scripts": {
"prepare": "// prepare //"
},
"engines": {
"node": ">=18"
},
"peerDependencies": {
"@clusterio/lib": "^2.0.0-alpha.14"
},
"devDependencies": {
// dev_dependencies //
"@clusterio/lib": "^2.0.0-alpha.14"
},
"dependencies": {
"@sinclair/typebox": "^0.30.4"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"clusterio",
"factorio"
]
}
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.browser.json",
"references": [
{ "path": "../../packages/lib/tsconfig.browser.json" },
{ "path": "../../packages/web_ui/tsconfig.browser.json" },
],
"include": [ "web/**/*.tsx", "web/**/*.ts", "messages.ts", "package.json" ],
"compilerOptions": {
"outDir": "dist/browser",
}
}
@@ -1,11 +1,9 @@
{
"extends": "../../tsconfig.node.json",
"files": [],
"references": [
{ "path": "../../packages/lib" },
],
"include": ["./**/*.ts"],
"exclude": ["test/*", "./dist/*"],
"compilerOptions": {
"outDir": "dist/plugin",
},
//%if web
{ "path": "./tsconfig.browser.json" },
//%endif
{ "path": "./tsconfig.node.json" }
]
}
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.node.json",
"references": [
{ "path": "../../packages/lib/tsconfig.node.json" },
],
"include": ["./**/*.ts"],
"exclude": ["test/*", "./dist/*"],
"compilerOptions": {
"outDir": "dist/node",
},
}
@@ -1,11 +0,0 @@
{
"extends": "../../tsconfig.web.json",
"references": [
{ "path": "../../packages/lib/tsconfig.web.json" },
{ "path": "../../packages/web_ui/tsconfig.web.json" },
],
"include": [ "web/**/*.tsx", "web/**/*.ts", "messages.ts", "package.json" ],
"compilerOptions": {
"outDir": "dist/web/build",
}
}
@@ -1,46 +1,68 @@
import React, { useContext, useEffect, useState/*/ [subscribable] /*/, useCallback, useSyncExternalStore/*/ [] /*/ } from "react";
// import { } from "antd";
import React, {
useContext, useEffect, useState,
//%if controller & web // Subscribing requires web content and the controller
useCallback, useSyncExternalStore,
//%endif
} from "react";
// import {
//
// } from "antd";
import {
BaseWebPlugin, PageLayout, Control, ControlContext, notifyErrorHandler,
} from "@clusterio/web_ui";
import {
PluginExampleEvent, PluginExampleRequest,
//%if controller & web // Subscribing requires web content and the controller
ExampleSubscribableUpdate,
//%endif
} from "../messages";
import * as lib from "@clusterio/lib";
import { PluginExampleEvent, PluginExampleRequest/*/ [subscribable] /*/, ExampleSubscribableUpdate/*/ [] /*/ } from "../messages";
function MyTemplatePage() {
const control = useContext(ControlContext);// [subscribable] //
const plugin = control.plugins.get("// plugin_name //") as WebPlugin;
const [subscribableData, synced] = plugin.useSubscribableData();// [] //
const control = useContext(ControlContext);
//%if controller & web // Subscribing requires web content and the controller
const plugin = control.plugins.get("__plugin_name__") as WebPlugin;
const [subscribableData, synced] = plugin.useSubscribableData();
//%endif
return <PageLayout nav={[{ name: "// plugin_name //" }]}>
<h2>// plugin_name //</h2>// [subscribable] //
Synced: {String(synced)} Data: {JSON.stringify([...subscribableData.values()])}// [] //
return <PageLayout nav={[{ name: "__plugin_name__" }]}>
<h2>__plugin_name__</h2>
//%if controller & web // Subscribing requires web content and the controller
Synced: {String(synced)} Data: {JSON.stringify([...subscribableData.values()])}
//%endif
</PageLayout>;
}
export class WebPlugin extends BaseWebPlugin {// [subscribable] //
export class WebPlugin extends BaseWebPlugin {
//%if controller & web // Subscribing requires web content and the controller
subscribableData = new lib.EventSubscriber(ExampleSubscribableUpdate, this.control);
// [] //
//%endif
async init() {
this.pages = [
{
path: "/// plugin_name //",
sidebarName: "// plugin_name //",
permission: "// plugin_name //.page.view",
path: "/__plugin_name__",
sidebarName: "__plugin_name__",
permission: "__plugin_name__.page.view",
content: <MyTemplatePage/>,
},
];
this.control.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.control.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
}// [subscribable] //
}
//%if controller & web // Subscribing requires web content and the controller
useSubscribableData() {
const control = useContext(ControlContext);
const subscribe = useCallback((callback: () => void) => this.subscribableData.subscribe(callback), [control]);
return useSyncExternalStore(subscribe, () => this.subscribableData.getSnapshot());
}// [] //
}
//%endif
async handlePluginExampleEvent(event: PluginExampleEvent) {
this.logger.info(JSON.stringify(event));
@@ -1,28 +0,0 @@
"use strict";
const path = require("path");
const webpack = require("webpack");
const { merge } = require("webpack-merge");
const common = require("@clusterio/web_ui/webpack.common");
module.exports = (env = {}) => merge(common(env), {
context: __dirname,
entry: "./web/index.tsx",
output: {
path: path.resolve(__dirname, "dist", "web"),
},
plugins: [
new webpack.container.ModuleFederationPlugin({
name: "// plugin_name //",
library: { type: "var", name: "plugin_// plugin_name //" },
exposes: {
"./": "./index.ts",
"./package.json": "./package.json",
},
shared: {
"@clusterio/lib": { import: false },
"@clusterio/web_ui": { import: false },
},
}),
],
});