mirror of
https://github.com/motajs/template.git
synced 2026-11-24 23:14:32 +08:00
refactor: 插件系统结构
This commit is contained in:
1 parent
3cc97e5ed3
commit
e6f4346924
30 files changed
+260
-169
No files matched your search
@@ -13,7 +13,7 @@ function createPlugin(
|
||||
): IDataPluginDefine {
|
||||
return {
|
||||
id,
|
||||
dependencies,
|
||||
dataDeps: dependencies,
|
||||
awake: () => {
|
||||
calls.push(`${id}.awake`);
|
||||
},
|
||||
|
||||
@@ -51,6 +51,8 @@ import {
|
||||
ICoreStateConfig,
|
||||
IDataPlugin,
|
||||
IDataPluginDefine,
|
||||
IPluginDefine,
|
||||
IPluginBase,
|
||||
ISaveableExecutor,
|
||||
PluginStatus
|
||||
} from './types';
|
||||
@@ -96,12 +98,10 @@ export class CoreState
|
||||
ISaveableExecutor<any>
|
||||
> = new Map();
|
||||
|
||||
/** 已注册的插件 */
|
||||
private readonly plugins: Map<string, IDataPlugin<IDataPluginDefine>> =
|
||||
new Map();
|
||||
private readonly orderedDataPlugins: IPluginOrderResult<
|
||||
IDataPlugin<IDataPluginDefine>
|
||||
>;
|
||||
/** 已注册的数据端插件 */
|
||||
private readonly dataPlugins: Map<string, IDataPlugin> = new Map();
|
||||
/** 排序后的数据端插件 */
|
||||
private readonly orderedDataPlugins: IPluginOrderResult;
|
||||
|
||||
constructor(config: Readonly<ICoreStateConfig>) {
|
||||
super();
|
||||
@@ -134,16 +134,20 @@ export class CoreState
|
||||
|
||||
// 数据端插件注册表
|
||||
for (const define of config.dataPlugins) {
|
||||
if (this.plugins.has(define.id)) {
|
||||
if (this.dataPlugins.has(define.id)) {
|
||||
logger.error(78, define.id);
|
||||
continue;
|
||||
}
|
||||
this.plugins.set(define.id, {
|
||||
this.dataPlugins.set(define.id, {
|
||||
status: PluginStatus.NotAwakened,
|
||||
plugin: define
|
||||
});
|
||||
}
|
||||
this.orderedDataPlugins = orderPlugins(this.plugins);
|
||||
const deps = new Map<string, readonly string[]>();
|
||||
for (const [id, plugin] of this.dataPlugins) {
|
||||
deps.set(id, plugin.plugin.dataDeps);
|
||||
}
|
||||
this.orderedDataPlugins = orderPlugins(deps);
|
||||
this.awake();
|
||||
}
|
||||
|
||||
@@ -159,6 +163,41 @@ export class CoreState
|
||||
|
||||
//#region 插件方法
|
||||
|
||||
/**
|
||||
* 检查插件加载状态
|
||||
* @param plugin 插件状态对象
|
||||
* @param expected 预期的插件加载状态
|
||||
*/
|
||||
protected checkPluginStatus(
|
||||
plugin: IPluginDefine<IPluginBase>,
|
||||
expected: PluginStatus
|
||||
) {
|
||||
if (plugin.status === expected) {
|
||||
return true;
|
||||
} else {
|
||||
logger.error(
|
||||
80,
|
||||
plugin.plugin.id,
|
||||
expected.toString(),
|
||||
plugin.status.toString()
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定插件的所有数据端插件依赖对象
|
||||
* @param plugin 目标插件定义对象
|
||||
*/
|
||||
protected getDataDependencies(plugin: IPluginBase): IDataPlugin[] {
|
||||
const dependencies: IDataPlugin[] = [];
|
||||
for (const dependency of plugin.dataDeps) {
|
||||
const plugin = this.dataPlugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
}
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件同步初始化
|
||||
*/
|
||||
@@ -172,14 +211,14 @@ export class CoreState
|
||||
logger.error(77, pluginId, dependency);
|
||||
}
|
||||
}
|
||||
for (const holder of result.order) {
|
||||
if (holder.status !== PluginStatus.NotAwakened) continue;
|
||||
const dependencies: IDataPlugin<IDataPluginDefine>[] = [];
|
||||
for (const dependency of holder.plugin.dependencies) {
|
||||
const plugin = this.plugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
for (const id of result.order) {
|
||||
const holder = this.dataPlugins.get(id)!;
|
||||
const plugin = holder.plugin;
|
||||
if (!this.checkPluginStatus(holder, PluginStatus.NotAwakened)) {
|
||||
continue;
|
||||
}
|
||||
holder.plugin.awake(this, dependencies);
|
||||
const dependencies = this.getDataDependencies(plugin);
|
||||
plugin.awake(this, dependencies);
|
||||
holder.status = PluginStatus.Awakened;
|
||||
}
|
||||
}
|
||||
@@ -189,14 +228,14 @@ export class CoreState
|
||||
*/
|
||||
protected async initialize(): Promise<void> {
|
||||
const result = this.orderedDataPlugins;
|
||||
for (const holder of result.order) {
|
||||
if (holder.status !== PluginStatus.Awakened) continue;
|
||||
const dependencies: IDataPlugin<IDataPluginDefine>[] = [];
|
||||
for (const dependency of holder.plugin.dependencies) {
|
||||
const plugin = this.plugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
for (const id of result.order) {
|
||||
const holder = this.dataPlugins.get(id)!;
|
||||
const plugin = holder.plugin;
|
||||
if (!this.checkPluginStatus(holder, PluginStatus.Awakened)) {
|
||||
continue;
|
||||
}
|
||||
await holder.plugin.initialize(this, dependencies);
|
||||
const dependencies = this.getDataDependencies(plugin);
|
||||
await plugin.initialize(this, dependencies);
|
||||
holder.status = PluginStatus.Initialized;
|
||||
}
|
||||
}
|
||||
@@ -206,14 +245,14 @@ export class CoreState
|
||||
*/
|
||||
protected async afterLoad(): Promise<void> {
|
||||
const result = this.orderedDataPlugins;
|
||||
for (const holder of result.order) {
|
||||
if (holder.status !== PluginStatus.Initialized) continue;
|
||||
const dependencies: IDataPlugin<IDataPluginDefine>[] = [];
|
||||
for (const dependency of holder.plugin.dependencies) {
|
||||
const plugin = this.plugins.get(dependency);
|
||||
if (plugin) dependencies.push(plugin);
|
||||
for (const id of result.order) {
|
||||
const holder = this.dataPlugins.get(id)!;
|
||||
const plugin = holder.plugin;
|
||||
if (!this.checkPluginStatus(holder, PluginStatus.Initialized)) {
|
||||
continue;
|
||||
}
|
||||
await holder.plugin.afterLoad(this, dependencies);
|
||||
const dependencies = this.getDataDependencies(plugin);
|
||||
await plugin.afterLoad(this, dependencies);
|
||||
holder.status = PluginStatus.Loaded;
|
||||
}
|
||||
}
|
||||
@@ -221,7 +260,7 @@ export class CoreState
|
||||
getDataPlugin<T extends IDataPluginDefine>(
|
||||
id: string
|
||||
): Readonly<IDataPlugin<T>> | null {
|
||||
return (this.plugins.get(id) as IDataPlugin<T>) ?? null;
|
||||
return (this.dataPlugins.get(id) as IDataPlugin<T>) ?? null;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -232,6 +271,11 @@ export class CoreState
|
||||
await this.initialize();
|
||||
yield* this.loader.start();
|
||||
await this.afterLoad();
|
||||
for (const plugin of this.dataPlugins.values()) {
|
||||
if (plugin.status !== PluginStatus.Loaded) {
|
||||
logger.error(81, plugin.plugin.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -1,20 +1,8 @@
|
||||
//#region 插件排序接口
|
||||
|
||||
export interface IPluginDepends {
|
||||
/** 插件标识符 */
|
||||
readonly id: string;
|
||||
/** 插件所依赖的其他插件 id */
|
||||
readonly dependencies: readonly string[];
|
||||
}
|
||||
|
||||
export interface IPluginHolder<T extends IPluginDepends> {
|
||||
/** 插件内容 */
|
||||
readonly plugin: T;
|
||||
}
|
||||
|
||||
export interface IPluginOrderResult<T> {
|
||||
export interface IPluginOrderResult {
|
||||
/** 可初始化的插件拓扑序 */
|
||||
readonly order: T[];
|
||||
readonly order: string[];
|
||||
/** 处于依赖环中的插件 id */
|
||||
readonly cyclic: Set<string>;
|
||||
/** 直接引用了不存在依赖的插件 id,与其缺失的依赖 id 列表 */
|
||||
@@ -29,33 +17,31 @@ export interface IPluginOrderResult<T> {
|
||||
* 依据插件的依赖关系计算可初始化的拓扑序,并检测依赖环与不存在的依赖
|
||||
* @param plugins 以插件 id 为键的插件映射表
|
||||
*/
|
||||
export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
plugins: ReadonlyMap<string, H>
|
||||
): IPluginOrderResult<H> {
|
||||
export function orderPlugins(
|
||||
plugins: ReadonlyMap<string, readonly string[]>
|
||||
): IPluginOrderResult {
|
||||
const cyclic = new Set<string>();
|
||||
const missing = new Map<string, string[]>();
|
||||
|
||||
// 依赖不存在检测:记录直接引用了不存在依赖的插件及其缺失的依赖
|
||||
for (const holder of plugins.values()) {
|
||||
const define = holder.plugin;
|
||||
for (const [id, deps] of plugins) {
|
||||
const lacked: string[] = [];
|
||||
for (const dependency of define.dependencies) {
|
||||
for (const dependency of deps) {
|
||||
if (!plugins.has(dependency)) {
|
||||
lacked.push(dependency);
|
||||
}
|
||||
}
|
||||
if (lacked.length > 0) {
|
||||
missing.set(define.id, lacked);
|
||||
missing.set(id, lacked);
|
||||
}
|
||||
}
|
||||
|
||||
// 环检测:从每个插件出发沿依赖边搜索,若能回到自身则其处于依赖环中
|
||||
for (const id of plugins.keys()) {
|
||||
const start = plugins.get(id)!;
|
||||
for (const [id, deps] of plugins) {
|
||||
const visited = new Set<string>();
|
||||
const pending: string[] = [];
|
||||
for (const dependency of start.plugin.dependencies) {
|
||||
pending.push(dependency);
|
||||
for (const dep of deps) {
|
||||
pending.push(dep);
|
||||
}
|
||||
while (pending.length > 0) {
|
||||
const current = pending.pop()!;
|
||||
@@ -67,7 +53,7 @@ export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
visited.add(current);
|
||||
const node = plugins.get(current);
|
||||
if (!node) continue;
|
||||
for (const dependency of node.plugin.dependencies) {
|
||||
for (const dependency of node) {
|
||||
pending.push(dependency);
|
||||
}
|
||||
}
|
||||
@@ -81,12 +67,11 @@ export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
let expanded = true;
|
||||
while (expanded) {
|
||||
expanded = false;
|
||||
for (const holder of plugins.values()) {
|
||||
const define = holder.plugin;
|
||||
if (blocked.has(define.id)) continue;
|
||||
for (const dependency of define.dependencies) {
|
||||
if (blocked.has(dependency)) {
|
||||
blocked.add(define.id);
|
||||
for (const [id, deps] of plugins) {
|
||||
if (blocked.has(id)) continue;
|
||||
for (const dep of deps) {
|
||||
if (blocked.has(dep)) {
|
||||
blocked.add(id);
|
||||
expanded = true;
|
||||
break;
|
||||
}
|
||||
@@ -95,24 +80,23 @@ export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
}
|
||||
|
||||
// 拓扑排序:反复扫描,逐轮输出依赖均已就绪的插件,保证依赖先于被依赖者
|
||||
const order: H[] = [];
|
||||
const order: string[] = [];
|
||||
const emitted = new Set<string>();
|
||||
let progressed = true;
|
||||
while (progressed) {
|
||||
progressed = false;
|
||||
for (const holder of plugins.values()) {
|
||||
const define = holder.plugin;
|
||||
if (blocked.has(define.id) || emitted.has(define.id)) continue;
|
||||
for (const [id, deps] of plugins) {
|
||||
if (blocked.has(id) || emitted.has(id)) continue;
|
||||
let ready = true;
|
||||
for (const dependency of define.dependencies) {
|
||||
if (!emitted.has(dependency)) {
|
||||
for (const dep of deps) {
|
||||
if (!emitted.has(dep)) {
|
||||
ready = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ready) continue;
|
||||
order.push(holder);
|
||||
emitted.add(define.id);
|
||||
order.push(id);
|
||||
emitted.add(id);
|
||||
progressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,49 +18,52 @@ export const enum PluginStatus {
|
||||
Loaded
|
||||
}
|
||||
|
||||
export interface IDataPlugin<T extends IDataPluginDefine> {
|
||||
export interface IPluginDefine<T> {
|
||||
/** 插件的当前状态 */
|
||||
status: PluginStatus;
|
||||
/** 插件内容 */
|
||||
plugin: T;
|
||||
}
|
||||
|
||||
export interface IDataPluginDefine {
|
||||
export interface IDataPlugin<
|
||||
T extends IDataPluginDefine = IDataPluginDefine
|
||||
> extends IPluginDefine<T> {}
|
||||
|
||||
export interface IPluginBase {
|
||||
/** 插件标识符 */
|
||||
readonly id: string;
|
||||
/** 插件所依赖的其他插件 id。依赖插件不允许循环依赖,否则会抛出错误,且所有在环上的插件都不会初始化 */
|
||||
readonly dependencies: string[];
|
||||
/** 插件所依赖的其他数据端插件 id。依赖插件不允许循环依赖,否则会抛出错误,且所有在环上的插件都不会初始化 */
|
||||
readonly dataDeps: readonly string[];
|
||||
}
|
||||
|
||||
export interface IDataPluginDefine extends IPluginBase {
|
||||
/**
|
||||
* 插件同步初始化,在数据端主对象创建完毕,初始化之前时调用
|
||||
* @param state 数据端主对象
|
||||
* @param dependencies 此插件的依赖插件对象
|
||||
* @param dependencies 此插件的数据端依赖插件对象
|
||||
*/
|
||||
awake(
|
||||
state: ICoreState,
|
||||
dependencies: IDataPlugin<IDataPluginDefine>[]
|
||||
): void;
|
||||
awake(state: ICoreState, dependencies: Readonly<IDataPlugin>[]): void;
|
||||
|
||||
/**
|
||||
* 插件异步初始化,在主对象创建完毕,初始化时,加载开始前调用。
|
||||
* 此方法具有阻塞效果,如果返回值迟迟不兑现,那么会导致依赖其的插件不会开始异步初始化,游戏加载也不会开始
|
||||
* @param state 数据端主对象
|
||||
* @param dependencies 此插件的依赖插件对象
|
||||
* @param dependencies 此插件的数据端依赖插件对象
|
||||
*/
|
||||
initialize(
|
||||
state: ICoreState,
|
||||
dependencies: IDataPlugin<IDataPluginDefine>[]
|
||||
dependencies: Readonly<IDataPlugin>[]
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* 当主对象加载完毕后调用,可以用于依赖加载内容的初始化。
|
||||
* 此方法具有阻塞效果,如果返回值迟迟不兑现,那么会导致依赖其的插件不会开始加载初始化,游戏也不会开始
|
||||
* @param state 数据端主对象
|
||||
* @param dependencies 此插件的依赖插件对象
|
||||
* @param dependencies 此插件的数据端依赖插件对象
|
||||
*/
|
||||
afterLoad(
|
||||
state: ICoreState,
|
||||
dependencies: IDataPlugin<IDataPluginDefine>[]
|
||||
dependencies: Readonly<IDataPlugin>[]
|
||||
): Promise<void>;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user