@@ -5,6 +5,7 @@ use bevy::ecs::{entity::Entity, system::Resource, world::World};
55use crate :: {
66 prelude:: { Runtime , ScriptError } ,
77 script:: { Script , ScriptId } ,
8+ IntoScriptPluginParams ,
89} ;
910
1011pub trait Context : ' static { }
@@ -13,11 +14,11 @@ impl<T: 'static> Context for T {}
1314pub type ContextId = u32 ;
1415
1516#[ 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 > ,
1819}
1920
20- impl < T : Context > Default for ScriptContexts < T > {
21+ impl < P : IntoScriptPluginParams > Default for ScriptContexts < P > {
2122 fn default ( ) -> Self {
2223 Self {
2324 contexts : Default :: default ( ) ,
@@ -26,15 +27,15 @@ impl<T: Context> Default for ScriptContexts<T> {
2627}
2728
2829static CONTEXT_ID_COUNTER : AtomicU32 = AtomicU32 :: new ( 0 ) ;
29- impl < T : Context > ScriptContexts < T > {
30+ impl < P : IntoScriptPluginParams > ScriptContexts < P > {
3031 pub fn new ( ) -> Self {
3132 Self {
3233 contexts : HashMap :: new ( ) ,
3334 }
3435 }
3536
3637 /// 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 {
3839 let id = CONTEXT_ID_COUNTER . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
3940 self . contexts . insert ( id, ctxt) ;
4041 id
@@ -45,26 +46,27 @@ impl<T: Context> ScriptContexts<T> {
4546 CONTEXT_ID_COUNTER . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed )
4647 }
4748
48- pub fn remove ( & mut self , id : ContextId ) -> Option < T > {
49+ pub fn remove ( & mut self , id : ContextId ) -> Option < P :: C > {
4950 self . contexts . remove ( & id)
5051 }
5152}
5253
5354/// 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 > ;
5557/// 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 > ;
5860
5961#[ 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 > > ,
6567}
6668
67- impl < C : Context , R : Runtime > Default for ContextLoadingSettings < C , R > {
69+ impl < P : IntoScriptPluginParams > Default for ContextLoadingSettings < P > {
6870 fn default ( ) -> Self {
6971 Self {
7072 loader : None ,
@@ -75,7 +77,7 @@ impl<C: Context, R: Runtime> Default for ContextLoadingSettings<C, R> {
7577 }
7678}
7779
78- impl < C : Context , R : Runtime > Clone for ContextLoadingSettings < C , R > {
80+ impl < P : IntoScriptPluginParams > Clone for ContextLoadingSettings < P > {
7981 fn clone ( & self ) -> Self {
8082 Self {
8183 loader : self . loader . clone ( ) ,
@@ -87,27 +89,27 @@ impl<C: Context, R: Runtime> Clone for ContextLoadingSettings<C, R> {
8789}
8890
8991/// A strategy for loading and reloading contexts
90- pub struct ContextBuilder < C : Context , R : Runtime > {
92+ pub struct ContextBuilder < P : IntoScriptPluginParams > {
9193 pub load : fn (
9294 script : & ScriptId ,
9395 content : & [ u8 ] ,
94- & [ ContextInitializer < C > ] ,
95- & [ ContextPreHandlingInitializer < C > ] ,
96+ & [ ContextInitializer < P > ] ,
97+ & [ ContextPreHandlingInitializer < P > ] ,
9698 & mut World ,
97- runtime : & mut R ,
98- ) -> Result < C , ScriptError > ,
99+ runtime : & mut P :: R ,
100+ ) -> Result < P :: C , ScriptError > ,
99101 pub reload : fn (
100102 script : & ScriptId ,
101103 new_content : & [ u8 ] ,
102- context : & mut C ,
103- & [ ContextInitializer < C > ] ,
104- & [ ContextPreHandlingInitializer < C > ] ,
104+ context : & mut P :: C ,
105+ & [ ContextInitializer < P > ] ,
106+ & [ ContextPreHandlingInitializer < P > ] ,
105107 & mut World ,
106- & mut R ,
108+ & mut P :: R ,
107109 ) -> Result < ( ) , ScriptError > ,
108110}
109111
110- impl < C : Context , R : Runtime > Clone for ContextBuilder < C , R > {
112+ impl < P : IntoScriptPluginParams > Clone for ContextBuilder < P > {
111113 fn clone ( & self ) -> Self {
112114 Self {
113115 load : self . load ,
@@ -117,23 +119,23 @@ impl<C: Context, R: Runtime> Clone for ContextBuilder<C, R> {
117119}
118120
119121/// 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 > {
121123 /// 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`.
122124 /// Returning None means the script should be assigned a new context
123125 pub assign : fn (
124126 old_script : Option < & Script > ,
125127 script_id : & ScriptId ,
126128 new_content : & [ u8 ] ,
127- contexts : & ScriptContexts < C > ,
129+ contexts : & ScriptContexts < P > ,
128130 ) -> Option < ContextId > ,
129131
130132 /// Handle the removal of the script, if any clean up in contexts is necessary perform it here.
131133 /// This will also be called, when a script is assigned a contextId on reload different from the previous one
132134 /// 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 > ) ,
134136}
135137
136- impl < C : Context > Default for ContextAssigner < C > {
138+ impl < P : IntoScriptPluginParams > Default for ContextAssigner < P > {
137139 fn default ( ) -> Self {
138140 Self {
139141 assign : |old, _, _, _| old. map ( |s| s. context_id ) ,
@@ -142,7 +144,7 @@ impl<C: Context> Default for ContextAssigner<C> {
142144 }
143145}
144146
145- impl < C : Context > Clone for ContextAssigner < C > {
147+ impl < P : IntoScriptPluginParams > Clone for ContextAssigner < P > {
146148 fn clone ( & self ) -> Self {
147149 Self {
148150 assign : self . assign ,
0 commit comments