@@ -7,11 +7,11 @@ import { withErrorMessage } from "common/utils/with-error-message";
7
7
import { UI_SYSTEM_CUSTOM_MESSAGE } from "./events" ;
8
8
import throttle from "lodash.throttle" ;
9
9
import { Janitor } from "three-janitor" ;
10
- import { mix } from "@utils/object-utils" ;
11
10
import { log } from "@ipc/log" ;
12
- import { PluginBase } from "./plugin-base" ;
11
+ import { PluginBase , PluginSessionContext } from "./plugin-base" ;
13
12
import { SceneController , VRSceneController } from "./scene-controller" ;
14
13
import lSet from "lodash.set" ;
14
+ import { mix } from "@utils/object-utils" ;
15
15
16
16
type PluginsConfigSnapshot = Record <
17
17
string ,
@@ -28,6 +28,7 @@ export class PluginSystemNative {
28
28
29
29
#sendCustomUIMessage! : ( pluginId : string , message : any ) => void ;
30
30
#compartments = new WeakMap < PluginBase , { globalThis : object } > ( ) ;
31
+ #sessionContext: PluginSessionContext ;
31
32
32
33
/**
33
34
* Error trapping is signficantly slower than not using it.
@@ -43,29 +44,36 @@ export class PluginSystemNative {
43
44
return this . #plugins. reduce . bind ( this . #plugins) ;
44
45
}
45
46
47
+ constructor ( sessionContext : PluginSessionContext ) {
48
+ this . #sessionContext = sessionContext ;
49
+ }
50
+
46
51
async activatePlugin (
47
52
pluginPackage : PluginMetaData ,
48
53
createCompartment : ( env : unknown ) => {
49
54
globalThis : {
50
55
Function : ( ...args : any [ ] ) => ( ) => object | PluginBase ;
51
56
} ;
52
- }
57
+ } ,
58
+ sessionContext : PluginSessionContext
53
59
) {
54
60
61
+ //todo: remove this
55
62
const compartment = createCompartment ( {
56
63
PluginBase,
57
64
SceneController,
58
65
VRSceneController,
59
66
} ) ;
60
67
61
68
try {
62
- const Plugin = await import ( /* @vite -ignore */ pluginPackage . urls . host ! ) ;
69
+ const Plugin = await import ( /* @vite -ignore */ pluginPackage . urls . host ! ) ;
63
70
64
71
if ( ! Plugin ) {
65
72
throw new Error ( "Plugin constructor must extend PluginBase" ) ;
66
73
}
67
74
68
- const plugin = new Plugin . default ( pluginPackage ) ;
75
+ const plugin = new Plugin . default ( pluginPackage , sessionContext ) ;
76
+ mix ( plugin , sessionContext . game ) ;
69
77
70
78
plugin . isSceneController = pluginPackage . isSceneController ;
71
79
@@ -95,12 +103,12 @@ export class PluginSystemNative {
95
103
async init (
96
104
pluginPackages : PluginMetaData [ ] ,
97
105
msg : ( id : string , message : any ) => void ,
98
- createCompartment : ( env : any ) => any
106
+ createCompartment : ( env : any ) => any ,
99
107
) {
100
108
101
109
log . debug ( `@plugin-system-native: init` ) ;
102
110
for ( const pkg of pluginPackages ) {
103
- const plugin = await this . activatePlugin ( pkg , createCompartment ) ;
111
+ const plugin = await this . activatePlugin ( pkg , createCompartment , this . #sessionContext ) ;
104
112
if ( plugin ) {
105
113
this . #plugins. push ( plugin ) ;
106
114
}
@@ -231,8 +239,9 @@ export class PluginSystemNative {
231
239
pluginPackages : PluginMetaData [ ] ,
232
240
createCompartment : ( env : any ) => any
233
241
) {
242
+
234
243
const additionalPlugins = pluginPackages
235
- . map ( ( p ) => this . activatePlugin ( p , createCompartment ) )
244
+ . map ( ( p ) => this . activatePlugin ( p , createCompartment , this . #sessionContext ) )
236
245
. filter ( Boolean ) ;
237
246
238
247
this . #plugins = [ ...this . #plugins, ...additionalPlugins ] as PluginBase [ ] ;
@@ -241,17 +250,16 @@ export class PluginSystemNative {
241
250
/**
242
251
* Temporarily inject an api into all active plugins.
243
252
*/
244
- injectApi ( object : object ) {
245
- mix ( PluginBase . prototype , object ) ;
246
- const keys = Object . keys ( object ) ;
247
-
248
- return ( ) => {
249
- keys . forEach ( ( key ) => {
250
- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
251
- delete PluginBase . prototype [ key as keyof typeof PluginBase . prototype ] ;
252
- } ) ;
253
- } ;
254
- }
253
+ // injectApi(object: object) {
254
+ // const keys = Object.keys(object);
255
+
256
+ // return () => {
257
+ // keys.forEach((key) => {
258
+ // // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
259
+ // delete PluginBase.prototype[key as keyof typeof PluginBase.prototype];
260
+ // });
261
+ // };
262
+ // }
255
263
256
264
getConfigSnapshot ( ) {
257
265
return this . #plugins. reduce ( ( acc , plugin ) => {
0 commit comments