22
33use crate :: {
44 bindings:: { ThreadWorldContainer , WorldContainer , WorldGuard } ,
5- error:: { InteropError , ScriptError } ,
5+ error:: ScriptError ,
66 script:: ScriptAttachment ,
77 IntoScriptPluginParams ,
88} ;
@@ -29,8 +29,6 @@ pub struct ContextLoadingSettings<P: IntoScriptPluginParams> {
2929 /// Whether to emit responses from core script_callbacks like `on_script_loaded` or `on_script_unloaded`.
3030 /// By default, this is `false` and responses are not emitted.
3131 pub emit_responses : bool ,
32- /// Defines the strategy used to load and reload contexts
33- pub loader : ContextBuilder < P > ,
3432 /// Initializers run once after creating a context but before executing it for the first time
3533 pub context_initializers : Vec < ContextInitializer < P > > ,
3634 /// Initializers run every time before executing or loading a script
@@ -41,7 +39,6 @@ impl<P: IntoScriptPluginParams> Default for ContextLoadingSettings<P> {
4139 fn default ( ) -> Self {
4240 Self {
4341 emit_responses : false ,
44- loader : ContextBuilder :: default ( ) ,
4542 context_initializers : Default :: default ( ) ,
4643 context_pre_handling_initializers : Default :: default ( ) ,
4744 }
@@ -52,7 +49,6 @@ impl<T: IntoScriptPluginParams> Clone for ContextLoadingSettings<T> {
5249 fn clone ( & self ) -> Self {
5350 Self {
5451 emit_responses : self . emit_responses ,
55- loader : self . loader . clone ( ) ,
5652 context_initializers : self . context_initializers . clone ( ) ,
5753 context_pre_handling_initializers : self . context_pre_handling_initializers . clone ( ) ,
5854 }
@@ -77,29 +73,34 @@ pub type ContextReloadFn<P> = fn(
7773 runtime : & <P as IntoScriptPluginParams >:: R ,
7874) -> Result < ( ) , ScriptError > ;
7975
80- /// A strategy for loading and reloading contexts
81- pub struct ContextBuilder < P : IntoScriptPluginParams > {
82- /// The function to load a context
83- pub load : ContextLoadFn < P > ,
84- /// The function to reload a context
85- pub reload : ContextReloadFn < P > ,
86- }
76+ /// A utility trait for types implementing `IntoScriptPluginParams`.
77+ ///
78+ /// Provides methods for initializing and reloading script contexts using the plugin's context loader and reloader functions.
79+ pub trait ScriptingLoader < P : IntoScriptPluginParams > {
80+ /// Loads a script context using the provided loader function
81+ fn load (
82+ attachment : & ScriptAttachment ,
83+ content : & [ u8 ] ,
84+ context_initializers : & [ ContextInitializer < P > ] ,
85+ pre_handling_initializers : & [ ContextPreHandlingInitializer < P > ] ,
86+ world : WorldGuard ,
87+ runtime : & P :: R ,
88+ ) -> Result < P :: C , ScriptError > ;
8789
88- impl < P : IntoScriptPluginParams > Default for ContextBuilder < P > {
89- fn default ( ) -> Self {
90- Self {
91- load : |_, _, _, _, _| Err ( InteropError :: invariant ( "no context loader set" ) . into ( ) ) ,
92- reload : |_, _, _, _, _, _| {
93- Err ( InteropError :: invariant ( "no context reloader set" ) . into ( ) )
94- } ,
95- }
96- }
90+ /// Reloads a script context using the provided reloader function
91+ fn reload (
92+ attachment : & ScriptAttachment ,
93+ content : & [ u8 ] ,
94+ previous_context : & mut P :: C ,
95+ context_initializers : & [ ContextInitializer < P > ] ,
96+ pre_handling_initializers : & [ ContextPreHandlingInitializer < P > ] ,
97+ world : WorldGuard ,
98+ runtime : & P :: R ,
99+ ) -> Result < ( ) , ScriptError > ;
97100}
98101
99- impl < P : IntoScriptPluginParams > ContextBuilder < P > {
100- /// load a context
101- pub fn load (
102- loader : ContextLoadFn < P > ,
102+ impl < P : IntoScriptPluginParams > ScriptingLoader < P > for P {
103+ fn load (
103104 attachment : & ScriptAttachment ,
104105 content : & [ u8 ] ,
105106 context_initializers : & [ ContextInitializer < P > ] ,
@@ -109,7 +110,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
109110 ) -> Result < P :: C , ScriptError > {
110111 WorldGuard :: with_existing_static_guard ( world. clone ( ) , |world| {
111112 ThreadWorldContainer . set_world ( world) ?;
112- ( loader ) (
113+ Self :: context_loader ( ) (
113114 attachment,
114115 content,
115116 context_initializers,
@@ -119,9 +120,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
119120 } )
120121 }
121122
122- /// reload a context
123- pub fn reload (
124- reloader : ContextReloadFn < P > ,
123+ fn reload (
125124 attachment : & ScriptAttachment ,
126125 content : & [ u8 ] ,
127126 previous_context : & mut P :: C ,
@@ -132,7 +131,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
132131 ) -> Result < ( ) , ScriptError > {
133132 WorldGuard :: with_existing_static_guard ( world, |world| {
134133 ThreadWorldContainer . set_world ( world) ?;
135- ( reloader ) (
134+ Self :: context_reloader ( ) (
136135 attachment,
137136 content,
138137 previous_context,
@@ -143,12 +142,3 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
143142 } )
144143 }
145144}
146-
147- impl < P : IntoScriptPluginParams > Clone for ContextBuilder < P > {
148- fn clone ( & self ) -> Self {
149- Self {
150- load : self . load ,
151- reload : self . reload ,
152- }
153- }
154- }
0 commit comments