mirror of
https://github.com/asphyxia-core/core.git
synced 2026-12-04 22:49:12 -06:00
new plugin system
This commit is contained in:
@@ -33,5 +33,15 @@ block content
|
||||
| Information
|
||||
.card-content !{readme}
|
||||
.column
|
||||
.card
|
||||
header.card-header
|
||||
p.card-header-title
|
||||
span.icon
|
||||
i.mdi.mdi-gamepad-variant-outline
|
||||
| Game Support
|
||||
.card-content
|
||||
span.tags
|
||||
each code in gameCodes
|
||||
span.tag.is-primary #{code}
|
||||
if config.length
|
||||
+form("Plugin Settings", config)
|
||||
@@ -41,8 +41,8 @@ if (ARGS.dev) {
|
||||
Logger.info(` [Console Output Enabled]`);
|
||||
}
|
||||
const external = LoadExternalPlugins();
|
||||
process.title = `Asphyxia CORE ${VERSION} | Plugins: ${external.plugins.size}`;
|
||||
if (external.plugins.size <= 0) {
|
||||
process.title = `Asphyxia CORE ${VERSION} | Plugins: ${external.length}`;
|
||||
if (external.length <= 0) {
|
||||
Logger.warn(chalk.yellowBright('no plugins are installed.'));
|
||||
Logger.info('');
|
||||
}
|
||||
@@ -54,7 +54,7 @@ SaveConfig();
|
||||
// ========== EAMUSE ============
|
||||
EAMUSE.set('views', path.join(ASSETS_PATH, 'views'));
|
||||
EAMUSE.set('view engine', 'pug');
|
||||
EAMUSE.use('*', services(`http://${CONFIG.bind}:${CONFIG.port}`, external.router));
|
||||
EAMUSE.use('*', services(`http://${CONFIG.bind}:${CONFIG.port}`, external));
|
||||
EAMUSE.use('/static', express.static(path.join(ASSETS_PATH, 'static')));
|
||||
EAMUSE.use(webui);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { KonmaiEncrypt } from './utils/KonmaiEncrypt';
|
||||
import iconv from 'iconv-lite';
|
||||
import { writeFileSync, readFileSync, write } from 'fs';
|
||||
import { ReadConfig, SaveConfig } from './utils/ArgConfig';
|
||||
import { renderFile as pugFile } from 'pug';
|
||||
|
||||
// const data = readFileSync('response.bin');
|
||||
// const decode = kdecode(data);
|
||||
@@ -34,5 +35,8 @@ import { ReadConfig, SaveConfig } from './utils/ArgConfig';
|
||||
// console.log(data2);
|
||||
// console.log(JSON.stringify(xmlToData(data2), null, 2));
|
||||
|
||||
ReadConfig();
|
||||
SaveConfig();
|
||||
// ReadConfig();
|
||||
// SaveConfig();
|
||||
|
||||
const data = xmlToData(pugFile('build\\plugins\\sdvx\\template\\common4.pug'));
|
||||
console.log(data);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { EamusePluginContainer } from '../EamusePluginContainer';
|
||||
import { EamuseRouteContainer } from '../EamuseRouteContainer';
|
||||
import { padStart, get, toSafeInteger, toString } from 'lodash';
|
||||
import { Logger } from '../../utils/Logger';
|
||||
import { SAFEHEX } from '../../utils/Consts';
|
||||
|
||||
export const cardmng = new EamusePluginContainer();
|
||||
export const cardmng = new EamuseRouteContainer();
|
||||
|
||||
const ProfileCheck: {
|
||||
[key: string]: () => boolean;
|
||||
@@ -46,7 +46,7 @@ export function RemoveProfileCheck(gameCode: string) {
|
||||
delete ProfileCheck[gameCode];
|
||||
}
|
||||
|
||||
cardmng.add('*', 'cardmng.inquire', async (info, data, send) => {
|
||||
cardmng.add('cardmng.inquire', async (info, data, send) => {
|
||||
const model: string = info.model;
|
||||
const modelCode = model.split(':')[0] || 'NUL';
|
||||
|
||||
@@ -69,7 +69,7 @@ cardmng.add('*', 'cardmng.inquire', async (info, data, send) => {
|
||||
return;
|
||||
});
|
||||
|
||||
cardmng.add('*', 'cardmng.getrefid', async (info, data, send) => {
|
||||
cardmng.add('cardmng.getrefid', async (info, data, send) => {
|
||||
const refid = toRefID(PROFILE_QUEUE_INDEX++);
|
||||
if (PROFILE_QUEUE_INDEX >= 1024) {
|
||||
PROFILE_QUEUE_INDEX = 0;
|
||||
@@ -79,7 +79,7 @@ cardmng.add('*', 'cardmng.getrefid', async (info, data, send) => {
|
||||
return;
|
||||
});
|
||||
|
||||
cardmng.add('*', 'cardmng.authpass', async (info, data, send) => {
|
||||
cardmng.add('cardmng.authpass', async (info, data, send) => {
|
||||
const refid = toQueueIndex(get(data, '@attr.refid', null));
|
||||
const pass = toSafeInteger(get(data, '@attr.pass', '0000'));
|
||||
|
||||
@@ -91,7 +91,7 @@ cardmng.add('*', 'cardmng.authpass', async (info, data, send) => {
|
||||
await send.success();
|
||||
});
|
||||
|
||||
cardmng.add('*', 'cardmng.bindmodel', async (info, data, send) => {
|
||||
cardmng.add('cardmng.bindmodel', async (info, data, send) => {
|
||||
const refid = get(data, '@attr.refid', 'DEADC0DEFEEDBEEF');
|
||||
await send.object({ '@attr': { dataid: refid } });
|
||||
});
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { set } from 'lodash';
|
||||
|
||||
import { kitem } from '../../utils/KBinJSON';
|
||||
import { EamusePluginContainer } from '../EamusePluginContainer';
|
||||
import { EamuseRouteContainer } from '../EamuseRouteContainer';
|
||||
|
||||
export const eacoin = new EamusePluginContainer();
|
||||
export const eacoin = new EamuseRouteContainer();
|
||||
|
||||
eacoin.add('*', 'eacoin.checkout', async (info, data, send) => {
|
||||
eacoin.add('eacoin.checkout', async (info, data, send) => {
|
||||
await send.success();
|
||||
return;
|
||||
});
|
||||
|
||||
eacoin.add('*', 'eacoin.checkin', async (info, data, send) => {
|
||||
eacoin.add('eacoin.checkin', async (info, data, send) => {
|
||||
const result = {};
|
||||
|
||||
set(result, 'balance', kitem('s32', 88410));
|
||||
@@ -24,7 +24,7 @@ eacoin.add('*', 'eacoin.checkin', async (info, data, send) => {
|
||||
return;
|
||||
});
|
||||
|
||||
eacoin.add('*', 'eacoin.consume', async (info, data, send) => {
|
||||
eacoin.add('eacoin.consume', async (info, data, send) => {
|
||||
const result = {};
|
||||
|
||||
set(result, 'autocharge', kitem('u8', 0));
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { ip2int, kitem } from '../../utils/KBinJSON';
|
||||
import { VERSION } from '../../utils/Consts';
|
||||
import { EamusePluginContainer } from '../EamusePluginContainer';
|
||||
import { EamuseRouteContainer } from '../EamuseRouteContainer';
|
||||
import { CONFIG } from '../../utils/ArgConfig';
|
||||
|
||||
export const facility = new EamusePluginContainer();
|
||||
export const facility = new EamuseRouteContainer();
|
||||
|
||||
facility.add('*', 'facility.get', async (info, data, send) => {
|
||||
facility.add('facility.get', async (info, data, send) => {
|
||||
const port = CONFIG.matching_port;
|
||||
|
||||
const result = {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { EamusePluginContainer } from '../EamusePluginContainer';
|
||||
import { EamuseRouteContainer } from '../EamuseRouteContainer';
|
||||
|
||||
export const pcbtracker = new EamusePluginContainer();
|
||||
export const pcbtracker = new EamuseRouteContainer();
|
||||
|
||||
pcbtracker.add('*', 'pcbtracker.alive', async (info, data, send) => {
|
||||
pcbtracker.add('pcbtracker.alive', async (info, data, send) => {
|
||||
const result = {
|
||||
'@attr': {
|
||||
ecenable: 1,
|
||||
|
||||
@@ -3,38 +3,38 @@ import { eacoin } from './EamuseCoin';
|
||||
import { facility } from './Facility';
|
||||
import { pcbtracker } from './PCBTracker';
|
||||
import { kitem } from '../../utils/KBinJSON';
|
||||
import { EamusePluginContainer } from '../EamusePluginContainer';
|
||||
import { EamuseRouteContainer } from '../EamuseRouteContainer';
|
||||
import { Logger } from '../../utils/Logger';
|
||||
|
||||
export const core = new EamusePluginContainer();
|
||||
export const core = new EamuseRouteContainer();
|
||||
|
||||
core.add('*', 'message.get', async (info, data, send) => {
|
||||
core.add('message.get', async (info, data, send) => {
|
||||
await send.object({
|
||||
'@attr': { expire: 1200 },
|
||||
});
|
||||
});
|
||||
|
||||
core.add('*', 'package.list', async (info, data, send) => {
|
||||
core.add('package.list', async (info, data, send) => {
|
||||
await send.object({
|
||||
'@attr': { expire: 1200 },
|
||||
});
|
||||
});
|
||||
|
||||
core.add('*', 'pcbevent.put', async (info, data, send) => {
|
||||
core.add('pcbevent.put', async (info, data, send) => {
|
||||
await send.success();
|
||||
});
|
||||
|
||||
core.add('*', 'tax.get_phase', async (info, data, send) => {
|
||||
core.add('tax.get_phase', async (info, data, send) => {
|
||||
await send.object({
|
||||
phase: kitem('s32', 0),
|
||||
});
|
||||
});
|
||||
|
||||
core.add('*', 'dlstatus.progress', async (info, data, send) => {
|
||||
core.add('dlstatus.progress', async (info, data, send) => {
|
||||
await send.success();
|
||||
});
|
||||
|
||||
core.add('*', 'posevent.income.sales.sale', async (info, data, send) => {
|
||||
core.add('posevent.income.sales.sale', async (info, data, send) => {
|
||||
Logger.debug(JSON.stringify(data, null, 4));
|
||||
await send.object([
|
||||
{ '@attr': { status: 0 } },
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { EamuseRouteHandler } from './EamuseRouteContainer';
|
||||
import { EamuseInfo } from '../middlewares/EamuseMiddleware';
|
||||
import { EamuseSend } from './EamuseSend';
|
||||
import { isNil } from 'lodash';
|
||||
import { Logger } from '../utils/Logger';
|
||||
|
||||
export class EamusePlugin {
|
||||
private pluginName: string;
|
||||
private gameCodes: string[];
|
||||
private routes: {
|
||||
[method: string]: boolean | EamuseRouteHandler;
|
||||
};
|
||||
private unhandled: boolean | EamuseRouteHandler;
|
||||
private contributors: {
|
||||
name: string;
|
||||
link?: string;
|
||||
}[];
|
||||
|
||||
constructor(pluginName: string) {
|
||||
this.pluginName = pluginName;
|
||||
this.gameCodes = [];
|
||||
this.routes = {};
|
||||
this.unhandled = false;
|
||||
this.contributors = [];
|
||||
}
|
||||
|
||||
public RegisterContributor(name: string, link?: string) {
|
||||
this.contributors.push({ name, link });
|
||||
}
|
||||
|
||||
public RegisterGameCode(gameCode: string) {
|
||||
this.gameCodes.push(gameCode);
|
||||
}
|
||||
|
||||
public RegisterRoute(method: string, route?: boolean | EamuseRouteHandler) {
|
||||
this.routes[method] = route;
|
||||
}
|
||||
|
||||
public RegisterUnhandled(handler?: EamuseRouteHandler) {
|
||||
if (handler) {
|
||||
this.unhandled = handler;
|
||||
} else {
|
||||
this.unhandled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public run(
|
||||
moduleName: string,
|
||||
method: string,
|
||||
info: EamuseInfo,
|
||||
data: any,
|
||||
send: EamuseSend
|
||||
): boolean {
|
||||
let handler = this.routes[`${moduleName}.${method}`];
|
||||
|
||||
if (isNil(handler)) {
|
||||
if (this.unhandled) {
|
||||
if (typeof this.unhandled == 'function') {
|
||||
this.unhandled(info, data, send);
|
||||
} else {
|
||||
Logger.warn(`unhandled method ${info.module}.${info.method}`, {
|
||||
plugin: this.pluginName,
|
||||
});
|
||||
send.success();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof handler === 'boolean') {
|
||||
handler ? send.success() : send.deny();
|
||||
return true;
|
||||
}
|
||||
|
||||
handler(info, data, send);
|
||||
return true;
|
||||
}
|
||||
|
||||
public get GameCodes() {
|
||||
return this.gameCodes;
|
||||
}
|
||||
|
||||
public get Name() {
|
||||
return this.pluginName;
|
||||
}
|
||||
|
||||
public get Contributors() {
|
||||
return this.contributors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
import { isNil } from 'lodash';
|
||||
|
||||
import { EamuseInfo } from '../middlewares/EamuseMiddleware';
|
||||
import { GetCallerPlugin } from '../utils/EamuseIO';
|
||||
import { Logger } from '../utils/Logger';
|
||||
import { EamuseSend } from './EamuseSend';
|
||||
|
||||
export type EamusePluginRoute = (info: EamuseInfo, data: any, send: EamuseSend) => Promise<any>;
|
||||
|
||||
export class EamusePluginContainer {
|
||||
private plugins: {
|
||||
[key: string]: boolean | EamusePluginRoute;
|
||||
};
|
||||
|
||||
private fallback: {
|
||||
[key: string]: EamusePluginRoute;
|
||||
};
|
||||
|
||||
private children: EamusePluginContainer[];
|
||||
|
||||
constructor() {
|
||||
this.plugins = {};
|
||||
this.fallback = {};
|
||||
this.children = [];
|
||||
}
|
||||
|
||||
public add(gameCode: string, method: string): void;
|
||||
public add(gameCode: string, method: string, handler: EamusePluginRoute | boolean): void;
|
||||
public add(gameCode: EamusePluginContainer): void;
|
||||
public add(
|
||||
gameCode: string | EamusePluginContainer,
|
||||
method?: string,
|
||||
handler?: EamusePluginRoute | boolean
|
||||
): void {
|
||||
if (typeof gameCode === 'string' && method !== null && typeof method === 'string') {
|
||||
let key = `${gameCode}:${method}`;
|
||||
if (gameCode === '*') {
|
||||
key = `${method}`;
|
||||
}
|
||||
if (handler) {
|
||||
this.plugins[key] = handler;
|
||||
} else {
|
||||
this.plugins[key] = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (gameCode instanceof EamusePluginContainer) {
|
||||
this.children.push(gameCode);
|
||||
}
|
||||
}
|
||||
|
||||
public unhandled(gameCode: string, handler?: EamusePluginRoute) {
|
||||
const plugin = GetCallerPlugin();
|
||||
if (typeof handler === 'function') {
|
||||
this.fallback[gameCode] = handler;
|
||||
} else {
|
||||
this.fallback[gameCode] = async (info, data, send) => {
|
||||
Logger.warn(`unhandled method ${info.module}.${info.method}`, { plugin: plugin.name });
|
||||
send.deny();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public removeHandler(gameCode: string, method: string) {
|
||||
delete this.plugins[`${gameCode}:${method}`];
|
||||
}
|
||||
|
||||
public removeUnhandled(gameCode: string) {
|
||||
delete this.fallback[`${gameCode}`];
|
||||
}
|
||||
|
||||
public clear() {
|
||||
delete this.plugins;
|
||||
delete this.fallback;
|
||||
delete this.children;
|
||||
this.plugins = {};
|
||||
this.fallback = {};
|
||||
this.children = [];
|
||||
}
|
||||
|
||||
public run(
|
||||
gameCode: string,
|
||||
moduleName: string,
|
||||
method: string,
|
||||
info: EamuseInfo,
|
||||
data: any,
|
||||
send: EamuseSend,
|
||||
root: boolean = true
|
||||
): boolean {
|
||||
let handler = this.plugins[`${moduleName}.${method}`];
|
||||
if (isNil(handler)) handler = this.plugins[`${gameCode}:${moduleName}.${method}`];
|
||||
|
||||
if (isNil(handler)) {
|
||||
if (this.fallback[gameCode]) {
|
||||
this.fallback[gameCode](info, data, send);
|
||||
return true;
|
||||
} else {
|
||||
for (const child of this.children) {
|
||||
if (child.run(gameCode, moduleName, method, info, data, send, false)) return true;
|
||||
}
|
||||
|
||||
if (root) send.deny();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof handler === 'boolean') {
|
||||
if (handler) {
|
||||
send.success();
|
||||
return true;
|
||||
} else {
|
||||
send.deny();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
handler(info, data, send);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
77
src/eamuse/EamuseRootRouter.ts
Normal file
77
src/eamuse/EamuseRootRouter.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { EamuseRouteContainer, EamuseRouteHandler } from './EamuseRouteContainer';
|
||||
import { EamusePlugin } from './EamusePlugin';
|
||||
import { Logger } from '../utils/Logger';
|
||||
import { EamuseInfo } from '../middlewares/EamuseMiddleware';
|
||||
import { EamuseSend } from './EamuseSend';
|
||||
|
||||
export class EamuseRootRouter {
|
||||
private core: EamuseRouteContainer;
|
||||
private pluginMap: {
|
||||
[gameCode: string]: EamusePlugin;
|
||||
};
|
||||
private pluginMapName: {
|
||||
[name: string]: EamusePlugin;
|
||||
};
|
||||
private plugins: EamusePlugin[];
|
||||
|
||||
constructor() {
|
||||
this.core = new EamuseRouteContainer();
|
||||
this.pluginMap = {};
|
||||
this.pluginMapName = {};
|
||||
}
|
||||
|
||||
public add(method: string): void;
|
||||
public add(method: string, handler: EamuseRouteHandler | boolean): void;
|
||||
public add(method: EamuseRouteContainer): void;
|
||||
public add(method: string | EamuseRouteContainer, handler?: EamuseRouteHandler | boolean): void {
|
||||
this.core.add(method, handler);
|
||||
}
|
||||
|
||||
public plugin(plugins: EamusePlugin[]) {
|
||||
this.plugins = plugins;
|
||||
for (const plugin of plugins) {
|
||||
for (const code of plugin.GameCodes) {
|
||||
if (this.pluginMap[code]) {
|
||||
Logger.warn(
|
||||
`register failed - '${code}' is already registered by ${this.pluginMap[code].Name}`,
|
||||
{ plugin: plugin.Name }
|
||||
);
|
||||
} else {
|
||||
this.pluginMap[code] = plugin;
|
||||
}
|
||||
}
|
||||
this.pluginMapName[plugin.Name] = plugin;
|
||||
}
|
||||
}
|
||||
|
||||
public run(
|
||||
gameCode: string,
|
||||
moduleName: string,
|
||||
method: string,
|
||||
info: EamuseInfo,
|
||||
data: any,
|
||||
send: EamuseSend
|
||||
): boolean {
|
||||
if (this.core.run(moduleName, method, info, data, send)) return;
|
||||
if (
|
||||
this.pluginMap[gameCode] &&
|
||||
this.pluginMap[gameCode].run(moduleName, method, info, data, send)
|
||||
)
|
||||
return;
|
||||
|
||||
send.deny();
|
||||
return;
|
||||
}
|
||||
|
||||
public getPluginByCode(gameCode: string) {
|
||||
return this.pluginMap[gameCode];
|
||||
}
|
||||
|
||||
public getPluginByName(gameCode: string) {
|
||||
return this.pluginMapName[gameCode];
|
||||
}
|
||||
|
||||
public get Plugins() {
|
||||
return this.plugins;
|
||||
}
|
||||
}
|
||||
55
src/eamuse/EamuseRouteContainer.ts
Normal file
55
src/eamuse/EamuseRouteContainer.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { isNil } from 'lodash';
|
||||
|
||||
import { EamuseInfo } from '../middlewares/EamuseMiddleware';
|
||||
import { EamuseSend } from './EamuseSend';
|
||||
|
||||
export type EamuseRouteHandler = (info: EamuseInfo, data: any, send: EamuseSend) => Promise<any>;
|
||||
|
||||
export class EamuseRouteContainer {
|
||||
private routes: {
|
||||
[key: string]: boolean | EamuseRouteHandler;
|
||||
};
|
||||
|
||||
constructor() {
|
||||
this.routes = {};
|
||||
}
|
||||
|
||||
public add(method: string): void;
|
||||
public add(method: string, handler: EamuseRouteHandler | boolean): void;
|
||||
public add(method: EamuseRouteContainer): void;
|
||||
public add(method: string | EamuseRouteContainer, handler?: EamuseRouteHandler | boolean): void;
|
||||
public add(method: string | EamuseRouteContainer, handler?: EamuseRouteHandler | boolean): void {
|
||||
if (typeof method === 'string') {
|
||||
if (handler) {
|
||||
this.routes[method] = handler;
|
||||
} else {
|
||||
this.routes[method] = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (method instanceof EamuseRouteContainer) {
|
||||
this.routes = { ...method.routes, ...this.routes };
|
||||
}
|
||||
}
|
||||
|
||||
public run(
|
||||
moduleName: string,
|
||||
method: string,
|
||||
info: EamuseInfo,
|
||||
data: any,
|
||||
send: EamuseSend
|
||||
): boolean {
|
||||
let handler = this.routes[`${moduleName}.${method}`];
|
||||
if (isNil(handler)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof handler === 'boolean') {
|
||||
handler ? send.success() : send.deny();
|
||||
return true;
|
||||
}
|
||||
|
||||
handler(info, data, send);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EamusePluginContainer, EamusePluginRoute } from './EamusePluginContainer';
|
||||
import { kitem, karray, kattr, dataToXML } from '../utils/KBinJSON';
|
||||
import path from 'path';
|
||||
import {
|
||||
@@ -19,23 +18,11 @@ import { KDataReader } from '../utils/KDataReader';
|
||||
import { PLUGIN_PATH, WriteFile, GetCallerPlugin } from '../utils/EamuseIO';
|
||||
import { readdirSync, existsSync } from 'fs';
|
||||
import { ARGS, PluginRegisterConfig } from '../utils/ArgConfig';
|
||||
import { AddProfileCheck } from './Core/CardManager';
|
||||
|
||||
/* Plugin Store */
|
||||
export const LOADED_PLUGINS: Map<
|
||||
string,
|
||||
{
|
||||
instance: any;
|
||||
contributors: {
|
||||
name: string;
|
||||
link?: string;
|
||||
}[];
|
||||
}
|
||||
> = new Map();
|
||||
import { EamusePlugin } from './EamusePlugin';
|
||||
import { EamuseRouteHandler } from './EamuseRouteContainer';
|
||||
|
||||
/* Exposing API */
|
||||
const $: any = global;
|
||||
const PLUGIN_ROUTER = new EamusePluginContainer();
|
||||
|
||||
const tsconfig = path.join(PLUGIN_PATH, 'tsconfig.json');
|
||||
/* ncc/pkg hack */
|
||||
@@ -77,33 +64,36 @@ $.U = {
|
||||
WriteFile,
|
||||
};
|
||||
|
||||
function EnableRegisterNamespace() {
|
||||
$.R = {
|
||||
Route: (method: string, handler: EamusePluginRoute | boolean) => {
|
||||
if (gameCode === '*') return;
|
||||
PLUGIN_ROUTER.add(gameCode, method, handler);
|
||||
},
|
||||
Unhandled: (handler?: EamusePluginRoute) => {
|
||||
if (gameCode === '*') return;
|
||||
PLUGIN_ROUTER.unhandled(gameCode, handler);
|
||||
},
|
||||
ProfileCheck: (gameCode: string, handler: () => boolean) => {
|
||||
if (gameCode === '*') return;
|
||||
AddProfileCheck(gameCode, handler);
|
||||
},
|
||||
Contributor: (name: string, link?: string) => {
|
||||
const plugin = GetCallerPlugin();
|
||||
if (!plugin) return;
|
||||
LOADED_PLUGINS.get(plugin.name).contributors.push({ name, link });
|
||||
},
|
||||
Config: PluginRegisterConfig,
|
||||
$.R = {
|
||||
GameCode: () => {},
|
||||
Route: () => {},
|
||||
Unhandled: () => {},
|
||||
Contributor: () => {},
|
||||
Config: () => {},
|
||||
};
|
||||
|
||||
function EnableRegisterNamespace(plugin: EamusePlugin) {
|
||||
$.R.GameCode = (gameCode: string) => {
|
||||
plugin.RegisterGameCode(gameCode);
|
||||
};
|
||||
$.R.Route = (method: string, handler?: boolean | EamuseRouteHandler) => {
|
||||
plugin.RegisterRoute(method, handler);
|
||||
};
|
||||
$.R.Unhandled = (handler?: EamuseRouteHandler) => {
|
||||
plugin.RegisterUnhandled(handler);
|
||||
};
|
||||
$.R.Contributor = (name: string, link?: string) => {
|
||||
plugin.RegisterContributor(name, link);
|
||||
};
|
||||
$.R.Config = PluginRegisterConfig;
|
||||
}
|
||||
|
||||
function DisableRegisterNamespace() {
|
||||
for (const prop in $.R) {
|
||||
$.R[prop] = () => {};
|
||||
}
|
||||
$.R.GameCode = () => {};
|
||||
$.R.Route = () => {};
|
||||
$.R.Unhandled = () => {};
|
||||
$.R.Contributor = () => {};
|
||||
$.R.Config = () => {};
|
||||
}
|
||||
|
||||
if (!ARGS.dev) {
|
||||
@@ -142,8 +132,13 @@ $.console.error = (...msgs: any[]) => {
|
||||
};
|
||||
|
||||
export function LoadExternalPlugins() {
|
||||
const loaded: EamusePlugin[] = [];
|
||||
|
||||
try {
|
||||
const plugins = readdirSync(PLUGIN_PATH);
|
||||
|
||||
const instances: { instance: any; name: string }[] = [];
|
||||
|
||||
for (const mod of plugins) {
|
||||
const name = path.basename(mod);
|
||||
const pluginPath = path.resolve(PLUGIN_PATH, mod);
|
||||
@@ -165,22 +160,25 @@ export function LoadExternalPlugins() {
|
||||
instance = instance.default;
|
||||
}
|
||||
|
||||
LOADED_PLUGINS.set(name, { instance, contributors: [] });
|
||||
instances.push({ instance, name });
|
||||
} catch (err) {
|
||||
Logger.error(`failed to load`, { plugin: name });
|
||||
Logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
EnableRegisterNamespace();
|
||||
for (const [name, plugin] of LOADED_PLUGINS) {
|
||||
for (const { instance, name } of instances) {
|
||||
const plugin = new EamusePlugin(name);
|
||||
EnableRegisterNamespace(plugin);
|
||||
try {
|
||||
plugin.instance.register();
|
||||
instance.register();
|
||||
loaded.push(plugin);
|
||||
} catch (err) {
|
||||
Logger.error(`failed during register()`, { plugin: name });
|
||||
Logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/* Disable route registering after external module has been loaded */
|
||||
DisableRegisterNamespace();
|
||||
} catch (err) {
|
||||
@@ -188,8 +186,5 @@ export function LoadExternalPlugins() {
|
||||
Logger.warn(`make sure your plugins are installed under: ${PLUGIN_PATH}`);
|
||||
}
|
||||
|
||||
return {
|
||||
plugins: LOADED_PLUGINS,
|
||||
router: PLUGIN_ROUTER,
|
||||
};
|
||||
return loaded;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,19 @@ import { Router } from 'express';
|
||||
|
||||
import { EamuseMiddleware, EamuseRoute } from '../middlewares/EamuseMiddleware';
|
||||
import { core } from './Core';
|
||||
import { EamusePluginContainer } from './EamusePluginContainer';
|
||||
import { EamusePlugin } from './EamusePlugin';
|
||||
import { EamuseRootRouter } from './EamuseRootRouter';
|
||||
import { Logger } from '../utils/Logger';
|
||||
|
||||
export const services = (url: string, plugins: EamusePluginContainer) => {
|
||||
export const ROOT_CONTAINER = new EamuseRootRouter();
|
||||
let initialized = false;
|
||||
|
||||
export const services = (url: string, plugins: EamusePlugin[]) => {
|
||||
if (initialized) {
|
||||
Logger.warn(`Only one service can be handled one at a time.`);
|
||||
return;
|
||||
}
|
||||
const routeEamuse = Router();
|
||||
const rootEA = new EamusePluginContainer();
|
||||
|
||||
const coreModules = [
|
||||
'cardmng',
|
||||
@@ -32,10 +40,10 @@ export const services = (url: string, plugins: EamusePluginContainer) => {
|
||||
];
|
||||
|
||||
/* General Information */
|
||||
routeEamuse.use(EamuseMiddleware).all('*', EamuseRoute(rootEA));
|
||||
routeEamuse.use(EamuseMiddleware).all('*', EamuseRoute(ROOT_CONTAINER));
|
||||
|
||||
/* - Service */
|
||||
rootEA.add('*', 'services.get', async (info, data, send) => {
|
||||
ROOT_CONTAINER.add('services.get', async (info, data, send) => {
|
||||
const services = {
|
||||
'@attr': {
|
||||
expire: 10800,
|
||||
@@ -67,8 +75,8 @@ export const services = (url: string, plugins: EamusePluginContainer) => {
|
||||
});
|
||||
|
||||
/* Core */
|
||||
rootEA.add(core);
|
||||
rootEA.add(plugins);
|
||||
ROOT_CONTAINER.add(core);
|
||||
ROOT_CONTAINER.plugin(plugins);
|
||||
|
||||
return routeEamuse;
|
||||
};
|
||||
|
||||
@@ -12,9 +12,9 @@ import {
|
||||
import { KonmaiEncrypt } from '../utils/KonmaiEncrypt';
|
||||
import LzKN from '../utils/LzKN';
|
||||
import { Logger } from '../utils/Logger';
|
||||
import { EamusePluginContainer } from '../eamuse/EamusePluginContainer';
|
||||
import { EamuseSend } from '../eamuse/EamuseSend';
|
||||
import { dataToXML } from '../utils/KBinJSON';
|
||||
import { EamuseRootRouter } from '../eamuse/EamuseRootRouter';
|
||||
|
||||
// const ACCEPT_AGENTS = ['EAMUSE.XRPC/1.0', 'EAMUSE.Httpac/1.0'];
|
||||
|
||||
@@ -47,12 +47,6 @@ export const EamuseMiddleware: RequestHandler = async (req, res, next) => {
|
||||
return next();
|
||||
}
|
||||
|
||||
// if (ACCEPT_AGENTS.indexOf(agent) < 0) {
|
||||
// Logger.debug(`EAM: Unsupported agent: ${agent}`);
|
||||
// res.sendStatus(404);
|
||||
// return;
|
||||
// }
|
||||
|
||||
const eamuseInfo = req.headers['x-eamuse-info'];
|
||||
|
||||
let compress = req.headers['x-compress'];
|
||||
@@ -185,7 +179,7 @@ function removeCardID(data: any): any {
|
||||
return data;
|
||||
}
|
||||
|
||||
export const EamuseRoute = (container: EamusePluginContainer): RequestHandler => {
|
||||
export const EamuseRoute = (router: EamuseRootRouter): RequestHandler => {
|
||||
const route: RequestHandler = async (req, res, next) => {
|
||||
if ((req as any).skip) {
|
||||
next();
|
||||
@@ -199,7 +193,7 @@ export const EamuseRoute = (container: EamusePluginContainer): RequestHandler =>
|
||||
const send = new EamuseSend(body, res);
|
||||
const data = removeCardID(get(body.data, `call.${body.module}`));
|
||||
try {
|
||||
container.run(
|
||||
router.run(
|
||||
gameCode,
|
||||
body.module,
|
||||
body.method,
|
||||
|
||||
@@ -815,6 +815,10 @@ function stringed(data: any): any {
|
||||
}
|
||||
|
||||
function unstringed(data: any): any {
|
||||
if (data === '') return {};
|
||||
|
||||
if (typeof data == 'string') return kitem('str', data);
|
||||
|
||||
if (typeof data !== 'object') return undefined;
|
||||
|
||||
let result: any = {};
|
||||
|
||||
@@ -3,7 +3,6 @@ import { existsSync, readFileSync } from 'fs';
|
||||
import session from 'express-session';
|
||||
import flash from 'connect-flash';
|
||||
import { VERSION } from '../utils/Consts';
|
||||
import { LOADED_PLUGINS } from '../eamuse/ExternalPluginLoader';
|
||||
import { CONFIG_MAP, CONFIG_DATA, CONFIG, CONFIG_OPTIONS, SaveConfig } from '../utils/ArgConfig';
|
||||
import { get } from 'lodash';
|
||||
import { Converter } from 'showdown';
|
||||
@@ -12,6 +11,7 @@ import { urlencoded } from 'body-parser';
|
||||
import { Logger } from '../utils/Logger';
|
||||
import humanize from 'humanize-string';
|
||||
import path from 'path';
|
||||
import { ROOT_CONTAINER } from '../eamuse/index';
|
||||
|
||||
export const webui = Router();
|
||||
const md = new Converter({
|
||||
@@ -25,7 +25,7 @@ function data(title: string, attr?: any) {
|
||||
return {
|
||||
title,
|
||||
version: VERSION,
|
||||
plugins: Array.from(LOADED_PLUGINS.keys()),
|
||||
plugins: ROOT_CONTAINER.Plugins.map(p => p.Name),
|
||||
...attr,
|
||||
};
|
||||
}
|
||||
@@ -176,7 +176,6 @@ webui.post('*', urlencoded({ extended: true }), async (req, res) => {
|
||||
SaveConfig();
|
||||
}
|
||||
|
||||
Logger.debug(JSON.stringify(req.body));
|
||||
res.redirect(req.originalUrl);
|
||||
});
|
||||
|
||||
@@ -186,8 +185,8 @@ webui.get('/profiles', async (req, res) => {
|
||||
|
||||
webui.get('/about', async (req, res) => {
|
||||
const contributors = new Map<string, { name: string; link?: string }>();
|
||||
for (const [, plugin] of LOADED_PLUGINS) {
|
||||
for (const c of plugin.contributors) {
|
||||
for (const plugin of ROOT_CONTAINER.Plugins) {
|
||||
for (const c of plugin.Contributors) {
|
||||
contributors.set(c.name, c);
|
||||
}
|
||||
}
|
||||
@@ -208,7 +207,9 @@ webui.get('/plugin/:plugin', async (req, res) => {
|
||||
}
|
||||
|
||||
const config = configData(pluginName);
|
||||
const contributors = get(LOADED_PLUGINS.get(pluginName), 'contributors', []);
|
||||
const plugin = ROOT_CONTAINER.getPluginByName(pluginName);
|
||||
const contributors = plugin ? plugin.Contributors : [];
|
||||
const gameCodes = plugin ? plugin.GameCodes : [];
|
||||
|
||||
res.render('plugin', data(pluginName.toUpperCase(), { readme, config, contributors }));
|
||||
res.render('plugin', data(pluginName.toUpperCase(), { readme, config, contributors, gameCodes }));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user