/ game-server / source / plugins / PluginLoader.ts
PluginLoader.ts
 1  import { L2DataApi } from '../data/interface/l2DataApi'
 2  import aigle from 'aigle'
 3  import { L2ExecutablePlugin } from './L2ExecutablePlugin'
 4  import _ from 'lodash'
 5  import { BoatRegistry } from './boats/BoatRegistry'
 6  
 7  export type PluginHook = () => Promise<Array<string>>
 8  
 9  const allPlugins : Array<L2ExecutablePlugin> = _.flatten( [
10      BoatRegistry
11  ] )
12  class Manager implements L2DataApi {
13      hooks: Array<PluginHook> = []
14  
15      async load(): Promise<Array<string>> {
16  
17          let lines : Array<Array<string>> = await aigle.resolve( this.hooks ).map( ( method: PluginHook ) => method() )
18          await aigle.resolve( allPlugins ).each( ( plugin: L2ExecutablePlugin ) => plugin.onStart() )
19  
20          return [
21              `PluginLoader : executed ${ this.hooks.length } hooks.`,
22              `PluginLoader : started ${ allPlugins.length } plugins.`,
23               ..._.flatten( lines )
24          ]
25      }
26  
27      registerHook( method: PluginHook ): void {
28          this.hooks.push( method )
29      }
30  }
31  
32  export const PluginLoader = new Manager()