docs(mods): add bilingual mod authoring guide and runnable example mod templates
This commit is contained in:
1 parent
dbc21182a2
commit
3aac60c8a3
6 files changed
+247
No files matched your search
@@ -0,0 +1,19 @@
|
||||
// Example experimental server-side TypeScript mod.
|
||||
// Demonstrates dataset/catalogue hooks, per-account isolated storage, and a custom HTTP endpoint.
|
||||
{
|
||||
"format": 1,
|
||||
"id": "example.server-script",
|
||||
"name": {
|
||||
"zh-CN": "示例:服务端 TS 脚本钩子",
|
||||
"en": "Example: Server TS Script Hooks"
|
||||
},
|
||||
"version": "0.1.0",
|
||||
"author": "WTOffline",
|
||||
"description": {
|
||||
"zh-CN": "演示实验期服务端 TypeScript 钩子、独立存档区与自定义 /mods/<id>/status 网页接口。",
|
||||
"en": "Demonstrates experimental server TypeScript hooks, isolated mod storage, and a custom /mods/<id>/status HTTP route."
|
||||
},
|
||||
"api_version": "0.1-experimental",
|
||||
"game_versions": ["2.61.0.*"],
|
||||
"load_after": ["example.vehicle-tweak"]
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Example server-side TypeScript mod (erasable TypeScript syntax, runs directly on Electron's Node 24).
|
||||
*
|
||||
* EXPERIMENTAL: Internal server contracts and hook shapes may change across WTOffline releases.
|
||||
*/
|
||||
interface ModState {
|
||||
refreshes: number;
|
||||
lastUpdated: string;
|
||||
}
|
||||
|
||||
export default {
|
||||
setup(ctx: {
|
||||
readonly apiVersion: string;
|
||||
readonly manifest: { readonly id: string; readonly version: string };
|
||||
readonly logger: { info(msg: string): void };
|
||||
readonly storage: {
|
||||
forPlayer(userId: string): { read<T>(): T | undefined; write(value: unknown): void };
|
||||
};
|
||||
readonly hooks: {
|
||||
onDatasetsLoaded(fn: (datasets: { readonly vehicles: unknown[] }) => void): void;
|
||||
onServerListening(fn: (info: { readonly port: number }) => void): void;
|
||||
readonly profile: {
|
||||
onRefresh(fn: (profile: unknown, userId: string) => void): void;
|
||||
};
|
||||
};
|
||||
readonly routes: {
|
||||
readonly http: {
|
||||
register(subPath: string, fn: () => { status: number; contentType: string; body: string }): void;
|
||||
};
|
||||
};
|
||||
}) {
|
||||
let vehicleCount = 0;
|
||||
|
||||
ctx.hooks.onDatasetsLoaded((datasets) => {
|
||||
vehicleCount = datasets.vehicles.length;
|
||||
ctx.logger.info(`observed ${vehicleCount} vehicles in merged dataset`);
|
||||
});
|
||||
|
||||
ctx.hooks.profile.onRefresh((_profile, userId) => {
|
||||
const store = ctx.storage.forPlayer(userId);
|
||||
const prev = store.read<ModState>() ?? { refreshes: 0, lastUpdated: '' };
|
||||
store.write({ refreshes: prev.refreshes + 1, lastUpdated: new Date().toISOString() });
|
||||
});
|
||||
|
||||
ctx.hooks.onServerListening(({ port }) => {
|
||||
ctx.logger.info(`custom status endpoint ready at http://127.0.0.1:${port}/mods/${ctx.manifest.id}/status`);
|
||||
});
|
||||
|
||||
ctx.routes.http.register('/status', () => ({
|
||||
status: 200,
|
||||
contentType: 'application/json; charset=utf-8',
|
||||
body: JSON.stringify({
|
||||
modId: ctx.manifest.id,
|
||||
version: ctx.manifest.version,
|
||||
apiVersion: ctx.apiVersion,
|
||||
vehicleCount,
|
||||
}),
|
||||
}));
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user