@@ -5,12 +5,12 @@ use crate::{
5
5
bindings:: WorldGuard ,
6
6
context:: ContextBuilder ,
7
7
event:: { IntoCallbackLabel , OnScriptLoaded , OnScriptUnloaded } ,
8
- extractors:: { HandlerContext , WithWorldGuard } ,
8
+ extractors:: { with_handler_system_state , HandlerContext } ,
9
9
handler:: { handle_script_errors, CallbackSettings } ,
10
10
script:: { Script , ScriptId , StaticScripts } ,
11
11
IntoScriptPluginParams ,
12
12
} ;
13
- use bevy:: { asset:: Handle , ecs :: system :: SystemState , log:: debug, prelude:: Command } ;
13
+ use bevy:: { asset:: Handle , log:: debug, prelude:: Command } ;
14
14
use std:: marker:: PhantomData ;
15
15
16
16
/// Deletes a script with the given ID
@@ -33,67 +33,62 @@ impl<P: IntoScriptPluginParams> DeleteScript<P> {
33
33
34
34
impl < P : IntoScriptPluginParams > Command for DeleteScript < P > {
35
35
fn apply ( self , world : & mut bevy:: prelude:: World ) {
36
- let mut system_state: SystemState < WithWorldGuard < HandlerContext < P > > > =
37
- SystemState :: new ( world) ;
38
-
39
- let mut with_guard = system_state. get_mut ( world) ;
40
-
41
- let ( guard, handler_ctxt) = with_guard. get_mut ( ) ;
42
-
43
- if let Some ( script) = handler_ctxt. scripts . scripts . remove ( & self . id ) {
44
- debug ! ( "Deleting script with id: {}" , self . id) ;
45
-
46
- match handler_ctxt. script_contexts . get_mut ( script. context_id ) {
47
- Some ( context) => {
48
- // first let the script uninstall itself
49
- match ( CallbackSettings :: < P > :: call) (
50
- handler_ctxt. callback_settings . callback_handler ,
51
- vec ! [ ] ,
52
- bevy:: ecs:: entity:: Entity :: from_raw ( 0 ) ,
53
- & self . id ,
54
- & OnScriptUnloaded :: into_callback_label ( ) ,
55
- context,
56
- & handler_ctxt
57
- . context_loading_settings
58
- . context_pre_handling_initializers ,
59
- & mut handler_ctxt. runtime_container . runtime ,
60
- guard. clone ( ) ,
61
- ) {
62
- Ok ( _) => { }
63
- Err ( e) => {
64
- handle_script_errors (
65
- guard,
66
- [ e. with_context ( format ! (
67
- "Running unload hook for script with id: {}. Language: {}" ,
68
- self . id,
69
- P :: LANGUAGE
70
- ) ) ]
71
- . into_iter ( ) ,
72
- ) ;
36
+ with_handler_system_state ( world, |guard, handler_ctxt : & mut HandlerContext < P > | {
37
+ if let Some ( script) = handler_ctxt. scripts . scripts . remove ( & self . id ) {
38
+ debug ! ( "Deleting script with id: {}" , self . id) ;
39
+
40
+ match handler_ctxt. script_contexts . get_mut ( script. context_id ) {
41
+ Some ( context) => {
42
+ // first let the script uninstall itself
43
+ match ( CallbackSettings :: < P > :: call) (
44
+ handler_ctxt. callback_settings . callback_handler ,
45
+ vec ! [ ] ,
46
+ bevy:: ecs:: entity:: Entity :: from_raw ( 0 ) ,
47
+ & self . id ,
48
+ & OnScriptUnloaded :: into_callback_label ( ) ,
49
+ context,
50
+ & handler_ctxt
51
+ . context_loading_settings
52
+ . context_pre_handling_initializers ,
53
+ & mut handler_ctxt. runtime_container . runtime ,
54
+ guard. clone ( ) ,
55
+ ) {
56
+ Ok ( _) => { }
57
+ Err ( e) => {
58
+ handle_script_errors (
59
+ guard,
60
+ [ e. with_context ( format ! (
61
+ "Running unload hook for script with id: {}. Language: {}" ,
62
+ self . id,
63
+ P :: LANGUAGE
64
+ ) ) ]
65
+ . into_iter ( ) ,
66
+ ) ;
67
+ }
73
68
}
74
- }
75
69
76
- debug ! ( "Removing script with id: {}" , self . id) ;
77
- ( handler_ctxt. context_loading_settings . assigner . remove ) (
78
- script. context_id ,
79
- & script,
80
- & mut handler_ctxt. script_contexts ,
81
- )
82
- }
83
- None => {
84
- bevy:: log:: error!(
85
- "Could not find context with id: {} corresponding to script with id: {}. Removing script without running callbacks." ,
70
+ debug ! ( "Removing script with id: {}" , self . id) ;
71
+ ( handler_ctxt. context_loading_settings . assigner . remove ) (
86
72
script. context_id ,
87
- self . id
88
- ) ;
89
- }
90
- } ;
91
- } else {
92
- bevy:: log:: error!(
93
- "Attempted to delete script with id: {} but it does not exist, doing nothing!" ,
94
- self . id
95
- ) ;
96
- }
73
+ & script,
74
+ & mut handler_ctxt. script_contexts ,
75
+ )
76
+ }
77
+ None => {
78
+ bevy:: log:: error!(
79
+ "Could not find context with id: {} corresponding to script with id: {}. Removing script without running callbacks." ,
80
+ script. context_id,
81
+ self . id
82
+ ) ;
83
+ }
84
+ } ;
85
+ } else {
86
+ bevy:: log:: error!(
87
+ "Attempted to delete script with id: {} but it does not exist, doing nothing!" ,
88
+ self . id
89
+ ) ;
90
+ }
91
+ } )
97
92
}
98
93
}
99
94
@@ -207,86 +202,84 @@ impl<P: IntoScriptPluginParams> CreateOrUpdateScript<P> {
207
202
208
203
impl < P : IntoScriptPluginParams > Command for CreateOrUpdateScript < P > {
209
204
fn apply ( self , world : & mut bevy:: prelude:: World ) {
210
- let mut system_state: SystemState < WithWorldGuard < HandlerContext < P > > > =
211
- SystemState :: new ( world) ;
212
-
213
- let mut with_guard = system_state. get_mut ( world) ;
214
-
215
- let ( guard, handler_ctxt) = with_guard. get_mut ( ) ;
205
+ with_handler_system_state ( world, |guard, handler_ctxt : & mut HandlerContext < P > | {
206
+ let script = handler_ctxt. scripts . scripts . get ( & self . id ) ;
207
+ let previous_context_id = script. as_ref ( ) . map ( |s| s. context_id ) ;
208
+ debug ! (
209
+ "{}: CreateOrUpdateScript command applying (script_id: {}, previous_context_id: {:?})" ,
210
+ P :: LANGUAGE ,
211
+ self . id,
212
+ previous_context_id
213
+ ) ;
216
214
217
- let script = handler_ctxt. scripts . scripts . get ( & self . id ) ;
218
- let previous_context_id = script. as_ref ( ) . map ( |s| s. context_id ) ;
219
- debug ! (
220
- "{}: CreateOrUpdateScript command applying (script_id: {}, previous_context_id: {:?})" ,
221
- P :: LANGUAGE ,
222
- self . id,
223
- previous_context_id
224
- ) ;
215
+ match previous_context_id {
216
+ Some ( previous_context_id) => {
217
+ bevy:: log:: debug!(
218
+ "{}: script with id already has a context: {}" ,
219
+ P :: LANGUAGE ,
220
+ self . id
221
+ ) ;
222
+ self . reload_context ( guard. clone ( ) , handler_ctxt, previous_context_id) ;
223
+ }
224
+ None => {
225
+ let log_context = format ! ( "{}: Loading script: {}" , P :: LANGUAGE , self . id) ;
225
226
226
- match previous_context_id {
227
- Some ( previous_context_id) => {
228
- bevy:: log:: debug!(
229
- "{}: script with id already has a context: {}" ,
230
- P :: LANGUAGE ,
231
- self . id
232
- ) ;
233
- self . reload_context ( guard. clone ( ) , handler_ctxt, previous_context_id) ;
234
- }
235
- None => {
236
- let log_context = format ! ( "{}: Loading script: {}" , P :: LANGUAGE , self . id) ;
237
-
238
- let new_context_id = ( handler_ctxt. context_loading_settings . assigner . assign ) (
239
- & self . id ,
240
- & self . content ,
241
- & handler_ctxt. script_contexts ,
242
- )
243
- . unwrap_or_else ( || handler_ctxt. script_contexts . allocate_id ( ) ) ;
244
- if handler_ctxt. script_contexts . contains ( new_context_id) {
245
- self . reload_context ( guard, handler_ctxt, new_context_id) ;
246
- } else {
247
- // load new context
248
- bevy:: log:: debug!( "{}" , log_context) ;
249
- let ctxt = ( ContextBuilder :: < P > :: load) (
250
- handler_ctxt. context_loading_settings . loader . load ,
227
+ let new_context_id = ( handler_ctxt. context_loading_settings . assigner . assign ) (
251
228
& self . id ,
252
229
& self . content ,
253
- & handler_ctxt. context_loading_settings . context_initializers ,
254
- & handler_ctxt
255
- . context_loading_settings
256
- . context_pre_handling_initializers ,
257
- guard. clone ( ) ,
258
- & mut handler_ctxt. runtime_container . runtime ,
259
- ) ;
230
+ & handler_ctxt. script_contexts ,
231
+ )
232
+ . unwrap_or_else ( || handler_ctxt. script_contexts . allocate_id ( ) ) ;
233
+ if handler_ctxt. script_contexts . contains ( new_context_id) {
234
+ self . reload_context ( guard, handler_ctxt, new_context_id) ;
235
+ } else {
236
+ // load new context
237
+ bevy:: log:: debug!( "{}" , log_context) ;
238
+ let ctxt = ( ContextBuilder :: < P > :: load) (
239
+ handler_ctxt. context_loading_settings . loader . load ,
240
+ & self . id ,
241
+ & self . content ,
242
+ & handler_ctxt. context_loading_settings . context_initializers ,
243
+ & handler_ctxt
244
+ . context_loading_settings
245
+ . context_pre_handling_initializers ,
246
+ guard. clone ( ) ,
247
+ & mut handler_ctxt. runtime_container . runtime ,
248
+ ) ;
260
249
261
- let mut ctxt = match ctxt {
262
- Ok ( ctxt) => ctxt,
263
- Err ( e) => {
264
- handle_script_errors ( guard, [ e. with_context ( log_context) ] . into_iter ( ) ) ;
265
- return ;
250
+ let mut ctxt = match ctxt {
251
+ Ok ( ctxt) => ctxt,
252
+ Err ( e) => {
253
+ handle_script_errors (
254
+ guard,
255
+ [ e. with_context ( log_context) ] . into_iter ( ) ,
256
+ ) ;
257
+ return ;
258
+ }
259
+ } ;
260
+
261
+ self . run_on_load_callback ( handler_ctxt, guard, & mut ctxt) ;
262
+
263
+ if handler_ctxt
264
+ . script_contexts
265
+ . insert_with_id ( new_context_id, ctxt)
266
+ . is_some ( )
267
+ {
268
+ bevy:: log:: warn!( "{}: Context with id {} was not expected to exist. Overwriting it with a new context. This might happen if a script is not completely removed." , P :: LANGUAGE , new_context_id) ;
266
269
}
267
- } ;
268
-
269
- self . run_on_load_callback ( handler_ctxt, guard, & mut ctxt) ;
270
-
271
- if handler_ctxt
272
- . script_contexts
273
- . insert_with_id ( new_context_id, ctxt)
274
- . is_some ( )
275
- {
276
- bevy:: log:: warn!( "{}: Context with id {} was not expected to exist. Overwriting it with a new context. This might happen if a script is not completely removed." , P :: LANGUAGE , new_context_id) ;
277
270
}
278
- }
279
271
280
- handler_ctxt. scripts . scripts . insert (
281
- self . id . clone ( ) ,
282
- Script {
283
- id : self . id ,
284
- asset : self . asset ,
285
- context_id : new_context_id,
286
- } ,
287
- ) ;
272
+ handler_ctxt. scripts . scripts . insert (
273
+ self . id . clone ( ) ,
274
+ Script {
275
+ id : self . id ,
276
+ asset : self . asset ,
277
+ context_id : new_context_id,
278
+ } ,
279
+ ) ;
280
+ }
288
281
}
289
- }
282
+ } )
290
283
}
291
284
}
292
285
0 commit comments