2022-10-26 09:02:47 +08:00
|
|
|
import { Logger } from "log4js";
|
|
|
|
|
import { logger } from "../utils/util.log";
|
|
|
|
|
|
|
|
|
|
export type Registrable = {
|
|
|
|
|
name: string;
|
|
|
|
|
title: string;
|
|
|
|
|
desc?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-26 23:29:10 +08:00
|
|
|
export abstract class AbstractRegistrable<T extends Registrable> {
|
2022-10-26 09:02:47 +08:00
|
|
|
logger: Logger = logger;
|
2022-10-26 23:29:10 +08:00
|
|
|
// @ts-ignore
|
|
|
|
|
define: T;
|
|
|
|
|
|
|
|
|
|
getDefine(): T {
|
|
|
|
|
return this.define;
|
|
|
|
|
}
|
2022-10-26 09:02:47 +08:00
|
|
|
}
|
|
|
|
|
export class Registry<T extends typeof AbstractRegistrable> {
|
|
|
|
|
storage: {
|
|
|
|
|
[key: string]: T;
|
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
|
|
install(target: T) {
|
|
|
|
|
if (target == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-26 23:29:10 +08:00
|
|
|
// @ts-ignore
|
|
|
|
|
const define = new target().define;
|
|
|
|
|
let defineName = define.name;
|
2022-10-26 09:02:47 +08:00
|
|
|
if (defineName == null) {
|
|
|
|
|
defineName = target.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.register(defineName, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
register(key: string, value: T) {
|
|
|
|
|
if (!key || value == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.storage[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get(name: string) {
|
|
|
|
|
if (!name) {
|
|
|
|
|
throw new Error("插件名称不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const plugin = this.storage[name];
|
|
|
|
|
if (!plugin) {
|
|
|
|
|
throw new Error(`插件${name}还未注册`);
|
|
|
|
|
}
|
|
|
|
|
return plugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStorage() {
|
|
|
|
|
return this.storage;
|
|
|
|
|
}
|
|
|
|
|
}
|