@@ -7,15 +7,14 @@ use super::{
7
7
access_map:: ReflectAccessId ,
8
8
function:: { from:: Val , into:: IntoScript , script_function:: AppScriptFunctionRegistry } ,
9
9
schedule:: AppScheduleRegistry ,
10
- script_value:: ScriptValue ,
11
10
} ;
11
+ use crate :: extractors:: CallContext ;
12
12
use crate :: {
13
13
IntoScriptPluginParams ,
14
14
bindings:: pretty_print:: DisplayWithWorld ,
15
- error:: { InteropError , ScriptError } ,
15
+ error:: InteropError ,
16
16
event:: CallbackLabel ,
17
17
extractors:: get_all_access_ids,
18
- handler:: ScriptingHandler ,
19
18
script:: { ScriptAttachment , ScriptContext } ,
20
19
} ;
21
20
use :: {
@@ -39,10 +38,8 @@ use bevy_ecs::{
39
38
} ;
40
39
use bevy_log:: { error, info, warn_once} ;
41
40
use bevy_system_reflection:: { ReflectSchedule , ReflectSystem } ;
42
- use parking_lot:: Mutex ;
43
41
use std:: {
44
42
any:: TypeId , borrow:: Cow , collections:: HashSet , hash:: Hash , marker:: PhantomData , ops:: Deref ,
45
- sync:: Arc ,
46
43
} ;
47
44
#[ derive( Clone , Hash , PartialEq , Eq ) ]
48
45
/// a system set for script systems.
@@ -198,64 +195,8 @@ impl ScriptSystemBuilder {
198
195
}
199
196
}
200
197
201
- struct DynamicHandlerContext < ' w , P : IntoScriptPluginParams > {
202
- script_context : & ' w ScriptContext < P > ,
203
- }
204
-
205
- #[ profiling:: all_functions]
206
- impl < ' w , P : IntoScriptPluginParams > DynamicHandlerContext < ' w , P > {
207
- #[ allow(
208
- clippy:: expect_used,
209
- reason = "cannot avoid panicking inside init_param due to Bevy API structure"
210
- ) ]
211
- pub fn init_param ( world : & mut World , system : & mut FilteredAccessSet < ComponentId > ) {
212
- let mut access = FilteredAccess :: < ComponentId > :: matches_nothing ( ) ;
213
-
214
- let script_context_res_id = world
215
- . resource_id :: < ScriptContext < P > > ( )
216
- . expect ( "Scripts resource not found" ) ;
217
-
218
- access. add_resource_read ( script_context_res_id) ;
219
-
220
- system. add ( access) ;
221
- }
222
-
223
- #[ allow(
224
- clippy:: expect_used,
225
- reason = "cannot avoid panicking inside get_param due to Bevy API structure"
226
- ) ]
227
- pub fn get_param ( system : & UnsafeWorldCell < ' w > ) -> Self {
228
- unsafe {
229
- Self {
230
- script_context : system. get_resource ( ) . expect ( "Scripts resource not found" ) ,
231
- }
232
- }
233
- }
234
-
235
- /// Call a dynamic label on a script
236
- pub fn call_dynamic_label (
237
- & self ,
238
- label : & CallbackLabel ,
239
- context_key : & ScriptAttachment ,
240
- context : Option < Arc < Mutex < P :: C > > > ,
241
- payload : Vec < ScriptValue > ,
242
- guard : WorldGuard < ' _ > ,
243
- ) -> Result < ScriptValue , ScriptError > {
244
- // find script
245
- let Some ( context) = context. or_else ( || self . script_context . get ( context_key) ) else {
246
- return Err ( InteropError :: missing_context ( context_key. clone ( ) ) . into ( ) ) ;
247
- } ;
248
-
249
- // call the script
250
-
251
- let mut context = context. lock ( ) ;
252
-
253
- P :: handle ( payload, context_key, label, & mut context, guard)
254
- }
255
- }
256
-
257
198
/// TODO: inline world guard into the system state, we should be able to re-use it
258
- struct ScriptSystemState {
199
+ struct ScriptSystemState < P : IntoScriptPluginParams > {
259
200
type_registry : AppTypeRegistry ,
260
201
function_registry : AppScriptFunctionRegistry ,
261
202
schedule_registry : AppScheduleRegistry ,
@@ -264,6 +205,7 @@ struct ScriptSystemState {
264
205
subset : HashSet < ReflectAccessId > ,
265
206
callback_label : CallbackLabel ,
266
207
system_params : Vec < ScriptSystemParam > ,
208
+ script_contexts : ScriptContext < P > ,
267
209
}
268
210
269
211
/// Equivalent of [`SystemParam`] but for dynamic systems, these are the kinds of things
@@ -308,7 +250,7 @@ pub struct DynamicScriptSystem<P: IntoScriptPluginParams> {
308
250
target_attachment : ScriptAttachment ,
309
251
archetype_generation : ArchetypeGeneration ,
310
252
system_param_descriptors : Vec < ScriptSystemParamDescriptor > ,
311
- state : Option < ScriptSystemState > ,
253
+ state : Option < ScriptSystemState < P > > ,
312
254
_marker : std:: marker:: PhantomData < fn ( ) -> P > ,
313
255
}
314
256
@@ -453,16 +395,17 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
453
395
// targetted scripts. Let's start with just calling the one targetted
454
396
// script.
455
397
456
- let handler_ctxt = DynamicHandlerContext :: < P > :: get_param ( & world ) ;
398
+ let script_context = & state . script_contexts . read ( ) ;
457
399
458
- if let Some ( context) = handler_ctxt . script_context . get ( & self . target_attachment ) {
459
- let result = handler_ctxt . call_dynamic_label (
460
- & state . callback_label ,
400
+ if let Some ( context) = script_context. get_context ( & self . target_attachment ) {
401
+ let mut context = context . lock ( ) ;
402
+ let result = context . call_context_dynamic (
461
403
& self . target_attachment ,
462
- Some ( context ) ,
404
+ & state . callback_label ,
463
405
payload,
464
406
guard. clone ( ) ,
465
407
) ;
408
+ drop ( context) ;
466
409
// TODO: Emit error events via commands, maybe accumulate in state
467
410
// instead and use apply.
468
411
match result {
@@ -552,9 +495,6 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
552
495
}
553
496
}
554
497
555
- // TODO: access to internal resources, i.e. handler state
556
- DynamicHandlerContext :: < P > :: init_param ( world, & mut self . component_access_set ) ;
557
-
558
498
self . state = Some ( ScriptSystemState {
559
499
type_registry : world. get_resource_or_init :: < AppTypeRegistry > ( ) . clone ( ) ,
560
500
function_registry : world
@@ -568,6 +508,7 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
568
508
subset,
569
509
callback_label : self . name . to_string ( ) . into ( ) ,
570
510
system_params,
511
+ script_contexts : world. get_resource_or_init :: < ScriptContext < P > > ( ) . clone ( ) ,
571
512
} )
572
513
}
573
514
0 commit comments