@@ -5,6 +5,7 @@ use bevy::ecs::{entity::Entity, system::Resource, world::World};
5
5
use crate :: {
6
6
prelude:: { Runtime , ScriptError } ,
7
7
script:: { Script , ScriptId } ,
8
+ IntoScriptPluginParams ,
8
9
} ;
9
10
10
11
pub trait Context : ' static { }
@@ -13,11 +14,11 @@ impl<T: 'static> Context for T {}
13
14
pub type ContextId = u32 ;
14
15
15
16
#[ derive( Resource ) ]
16
- pub struct ScriptContexts < T : Context > {
17
- pub ( crate ) contexts : HashMap < ContextId , T > ,
17
+ pub struct ScriptContexts < P : IntoScriptPluginParams > {
18
+ pub ( crate ) contexts : HashMap < ContextId , P :: C > ,
18
19
}
19
20
20
- impl < T : Context > Default for ScriptContexts < T > {
21
+ impl < P : IntoScriptPluginParams > Default for ScriptContexts < P > {
21
22
fn default ( ) -> Self {
22
23
Self {
23
24
contexts : Default :: default ( ) ,
@@ -26,15 +27,15 @@ impl<T: Context> Default for ScriptContexts<T> {
26
27
}
27
28
28
29
static CONTEXT_ID_COUNTER : AtomicU32 = AtomicU32 :: new ( 0 ) ;
29
- impl < T : Context > ScriptContexts < T > {
30
+ impl < P : IntoScriptPluginParams > ScriptContexts < P > {
30
31
pub fn new ( ) -> Self {
31
32
Self {
32
33
contexts : HashMap :: new ( ) ,
33
34
}
34
35
}
35
36
36
37
/// Allocates a new ContextId and inserts the context into the map
37
- pub fn insert ( & mut self , ctxt : T ) -> ContextId {
38
+ pub fn insert ( & mut self , ctxt : P :: C ) -> ContextId {
38
39
let id = CONTEXT_ID_COUNTER . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
39
40
self . contexts . insert ( id, ctxt) ;
40
41
id
@@ -45,26 +46,27 @@ impl<T: Context> ScriptContexts<T> {
45
46
CONTEXT_ID_COUNTER . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed )
46
47
}
47
48
48
- pub fn remove ( & mut self , id : ContextId ) -> Option < T > {
49
+ pub fn remove ( & mut self , id : ContextId ) -> Option < P :: C > {
49
50
self . contexts . remove ( & id)
50
51
}
51
52
}
52
53
53
54
/// Initializer run once after creating a context but before executing it for the first time
54
- pub type ContextInitializer < C > = fn ( & ScriptId , & mut C ) -> Result < ( ) , ScriptError > ;
55
+ pub type ContextInitializer < P : IntoScriptPluginParams > =
56
+ fn ( & ScriptId , & mut P :: C ) -> Result < ( ) , ScriptError > ;
55
57
/// Initializer run every time before executing or loading a script
56
- pub type ContextPreHandlingInitializer < C > =
57
- fn ( & ScriptId , Entity , & mut C ) -> Result < ( ) , ScriptError > ;
58
+ pub type ContextPreHandlingInitializer < P : IntoScriptPluginParams > =
59
+ fn ( & ScriptId , Entity , & mut P :: C ) -> Result < ( ) , ScriptError > ;
58
60
59
61
#[ derive( Resource ) ]
60
- pub struct ContextLoadingSettings < C : Context , R : Runtime > {
61
- pub loader : Option < ContextBuilder < C , R > > ,
62
- pub assigner : Option < ContextAssigner < C > > ,
63
- pub context_initializers : Vec < ContextInitializer < C > > ,
64
- pub context_pre_handling_initializers : Vec < ContextPreHandlingInitializer < C > > ,
62
+ pub struct ContextLoadingSettings < P : IntoScriptPluginParams > {
63
+ pub loader : Option < ContextBuilder < P > > ,
64
+ pub assigner : Option < ContextAssigner < P > > ,
65
+ pub context_initializers : Vec < ContextInitializer < P > > ,
66
+ pub context_pre_handling_initializers : Vec < ContextPreHandlingInitializer < P > > ,
65
67
}
66
68
67
- impl < C : Context , R : Runtime > Default for ContextLoadingSettings < C , R > {
69
+ impl < P : IntoScriptPluginParams > Default for ContextLoadingSettings < P > {
68
70
fn default ( ) -> Self {
69
71
Self {
70
72
loader : None ,
@@ -75,7 +77,7 @@ impl<C: Context, R: Runtime> Default for ContextLoadingSettings<C, R> {
75
77
}
76
78
}
77
79
78
- impl < C : Context , R : Runtime > Clone for ContextLoadingSettings < C , R > {
80
+ impl < P : IntoScriptPluginParams > Clone for ContextLoadingSettings < P > {
79
81
fn clone ( & self ) -> Self {
80
82
Self {
81
83
loader : self . loader . clone ( ) ,
@@ -87,27 +89,27 @@ impl<C: Context, R: Runtime> Clone for ContextLoadingSettings<C, R> {
87
89
}
88
90
89
91
/// A strategy for loading and reloading contexts
90
- pub struct ContextBuilder < C : Context , R : Runtime > {
92
+ pub struct ContextBuilder < P : IntoScriptPluginParams > {
91
93
pub load : fn (
92
94
script : & ScriptId ,
93
95
content : & [ u8 ] ,
94
- & [ ContextInitializer < C > ] ,
95
- & [ ContextPreHandlingInitializer < C > ] ,
96
+ & [ ContextInitializer < P > ] ,
97
+ & [ ContextPreHandlingInitializer < P > ] ,
96
98
& mut World ,
97
- runtime : & mut R ,
98
- ) -> Result < C , ScriptError > ,
99
+ runtime : & mut P :: R ,
100
+ ) -> Result < P :: C , ScriptError > ,
99
101
pub reload : fn (
100
102
script : & ScriptId ,
101
103
new_content : & [ u8 ] ,
102
- context : & mut C ,
103
- & [ ContextInitializer < C > ] ,
104
- & [ ContextPreHandlingInitializer < C > ] ,
104
+ context : & mut P :: C ,
105
+ & [ ContextInitializer < P > ] ,
106
+ & [ ContextPreHandlingInitializer < P > ] ,
105
107
& mut World ,
106
- & mut R ,
108
+ & mut P :: R ,
107
109
) -> Result < ( ) , ScriptError > ,
108
110
}
109
111
110
- impl < C : Context , R : Runtime > Clone for ContextBuilder < C , R > {
112
+ impl < P : IntoScriptPluginParams > Clone for ContextBuilder < P > {
111
113
fn clone ( & self ) -> Self {
112
114
Self {
113
115
load : self . load ,
@@ -117,23 +119,23 @@ impl<C: Context, R: Runtime> Clone for ContextBuilder<C, R> {
117
119
}
118
120
119
121
/// A strategy for assigning contexts to new and existing but re-loaded scripts as well as for managing old contexts
120
- pub struct ContextAssigner < C : Context > {
122
+ pub struct ContextAssigner < P : IntoScriptPluginParams > {
121
123
/// Assign a context to the script, if script is `None`, this is a new script, otherwise it is an existing script with a context inside `contexts`.
122
124
/// Returning None means the script should be assigned a new context
123
125
pub assign : fn (
124
126
old_script : Option < & Script > ,
125
127
script_id : & ScriptId ,
126
128
new_content : & [ u8 ] ,
127
- contexts : & ScriptContexts < C > ,
129
+ contexts : & ScriptContexts < P > ,
128
130
) -> Option < ContextId > ,
129
131
130
132
/// Handle the removal of the script, if any clean up in contexts is necessary perform it here.
131
133
/// This will also be called, when a script is assigned a contextId on reload different from the previous one
132
134
/// the context_id in that case will be the old context_id and the one stored in the script will be the old one
133
- pub remove : fn ( context_id : ContextId , script : & Script , contexts : & mut ScriptContexts < C > ) ,
135
+ pub remove : fn ( context_id : ContextId , script : & Script , contexts : & mut ScriptContexts < P > ) ,
134
136
}
135
137
136
- impl < C : Context > Default for ContextAssigner < C > {
138
+ impl < P : IntoScriptPluginParams > Default for ContextAssigner < P > {
137
139
fn default ( ) -> Self {
138
140
Self {
139
141
assign : |old, _, _, _| old. map ( |s| s. context_id ) ,
@@ -142,7 +144,7 @@ impl<C: Context> Default for ContextAssigner<C> {
142
144
}
143
145
}
144
146
145
- impl < C : Context > Clone for ContextAssigner < C > {
147
+ impl < P : IntoScriptPluginParams > Clone for ContextAssigner < P > {
146
148
fn clone ( & self ) -> Self {
147
149
Self {
148
150
assign : self . assign ,
0 commit comments