@@ -9,7 +9,7 @@ use bevy::{
9
9
} ;
10
10
use bevy_mod_scripting_core:: {
11
11
bindings:: {
12
- ReflectAllocator , ReflectRefIter , ReflectReference , ReflectionPathElem , Unproxy ,
12
+ Either , ReflectAllocator , ReflectRefIter , ReflectReference , ReflectionPathElem , Unproxy ,
13
13
WorldCallbackAccess ,
14
14
} ,
15
15
error:: ScriptError ,
@@ -54,99 +54,64 @@ impl LuaReflectReference {
54
54
pub fn to_lua_proxy ( self , lua : & Lua ) -> Result < Value < ' _ > , mlua:: Error > {
55
55
// note we do not need to refer to LuaWorld here, it does not matter what the proxy is, that's pretty neat,
56
56
let world = lua. get_world ( ) ;
57
- // TODO: i don't like the pingponging between errors here, need something more ergonomic
58
- // first we need the type id of the pointed to object to figure out how to work with it
59
- let type_id = self . 0 . with_reflect ( & world, |r, _, _| {
60
- r. get_represented_type_info ( ) . map ( |t| t. type_id ( ) )
61
- } ) ?;
62
-
63
- // convenience, ideally we probably should just avoid lookups when no type id is here, but for now we just use a dummy type nothing will ever
64
- // be registered for. If the type we're reflecting doesn't represent anything or a registered type, we use a generic reflect reference.
65
- struct Dummy ;
66
- let type_id_or_dummy = type_id. unwrap_or ( TypeId :: of :: < Dummy > ( ) ) ;
67
-
68
- if let Some ( type_data) = world. with_resource :: < AppTypeRegistry , _ , _ > ( |_, type_registry| {
69
- type_registry
70
- . read ( )
71
- . get_type_data :: < ReflectLuaValue > ( type_id_or_dummy)
72
- . cloned ( )
73
- } ) {
74
- self . 0
75
- . with_reflect ( & world, |r, _, _| ( type_data. into_value ) ( r, lua) ) ?
76
- } else if let Some ( type_data) =
77
- world. with_resource :: < AppTypeRegistry , _ , _ > ( |_, type_registry| {
78
- type_registry
79
- . read ( )
80
- . get_type_data :: < ReflectLuaProxied > ( type_id_or_dummy)
81
- . cloned ( )
82
- } )
83
- {
84
- Ok ( ( type_data. into_proxy ) ( self . 0 . clone ( ) , lua) ?)
85
- } else {
86
- Ok ( self . clone ( ) . into_lua ( lua) ?)
87
- }
57
+
58
+ self . 0 . map_type_data (
59
+ & world,
60
+ |self_, type_data : Option < Either < ReflectLuaValue , ReflectLuaProxied > > | match type_data {
61
+ Some ( Either :: Left ( value_data) ) => {
62
+ self_. with_reflect ( & world, |r, _, _| ( value_data. into_value ) ( r, lua) ) ?
63
+ }
64
+ Some ( Either :: Right ( proxy_data) ) => Ok ( ( proxy_data. into_proxy ) ( self_, lua) ?) ,
65
+ None => Ok ( LuaReflectReference ( self_) . into_lua ( lua) ?) ,
66
+ } ,
67
+ ) ?
88
68
}
89
69
90
- pub fn set_with_lua_proxy ( & self , lua : & Lua , value : Value ) -> Result < ( ) , mlua:: Error > {
70
+ pub fn set_with_lua_proxy ( self , lua : & Lua , value : Value ) -> Result < ( ) , mlua:: Error > {
91
71
bevy:: log:: debug!( "Setting lua reflect reference with value: {:?}" , value) ;
92
72
93
73
let world = lua. get_world ( ) ;
94
- let type_id = self . 0 . with_reflect ( & world, |r, _, _| {
95
- r. get_represented_type_info ( ) . map ( |t| t. type_id ( ) )
96
- } ) ?;
97
-
98
- // convenience, ideally we probably should just avoid lookups when no type id is here, but for now we just use a dummy type nothing will ever
99
- // be registered for. If the type we're reflecting doesn't represent anything or a registered type, we use a generic reflect reference.
100
- struct Unknown ;
101
- let type_id_or_dummy = type_id. unwrap_or ( TypeId :: of :: < Unknown > ( ) ) ;
102
-
103
- if let Some ( type_data) = world. with_resource :: < AppTypeRegistry , _ , _ > ( |_, type_registry| {
104
- type_registry
105
- . read ( )
106
- . get_type_data :: < ReflectLuaValue > ( type_id_or_dummy)
107
- . cloned ( )
108
- } ) {
109
- bevy:: log:: debug!( "Setting value with ReflectLuaValue registration" ) ;
110
- let other = ( type_data. from_value ) ( value, lua) ?;
111
- self . 0 . with_reflect_mut ( & world, |r, _, _| {
112
- r. try_apply ( other. as_partial_reflect ( ) )
113
- . map_err ( ScriptError :: new_reflection_error)
114
- } ) ??;
115
- } else if let Some ( type_data) =
116
- world. with_resource :: < AppTypeRegistry , _ , _ > ( |_, type_registry| {
117
- type_registry
118
- . read ( )
119
- . get_type_data :: < ReflectLuaProxied > ( type_id_or_dummy)
120
- . cloned ( )
121
- } )
122
- {
123
- bevy:: log:: debug!( "Setting value with ReflectLuaProxied registration" ) ;
124
- let other = ( type_data. from_proxy ) ( value, lua) ?;
125
- let other = other. with_reflect ( & world, |r, _, _| r. clone_value ( ) ) ?;
126
- // now we can set it
127
- self . 0 . with_reflect_mut ( & world, |r, _, _| {
128
- if let Some ( set) = type_data. opt_set {
129
- set ( r, other)
130
- } else {
131
- r. try_apply ( other. as_partial_reflect ( ) )
132
- . map_err ( ScriptError :: new_reflection_error) ?;
133
- Ok ( ( ) )
134
- }
135
- } ) ??;
136
- } else {
137
- bevy:: log:: debug!( "No registration found, throwing error" ) ;
138
- // we don't know how to assign the value
139
- // prod to see if it's a common container (i.e. Option or Vec)
140
- world. with_resource :: < AppTypeRegistry , _ , _ > ( |_, type_registry| {
141
- let type_registry = type_registry. read ( ) ;
142
- Err ( ScriptError :: new_runtime_error ( format ! (
143
- "Invalid assignment `{:?}` = `{:?}`. The underlying type does: `{}` not support assignment." ,
144
- self . 0 . print_with_type_registry( & type_registry) ,
145
- value,
146
- type_registry. get_type_info( type_id_or_dummy) . map( |t| t. type_path( ) ) . unwrap_or_else( || "Unknown" )
74
+
75
+ self . 0 . map_type_data (
76
+ & world. clone ( ) ,
77
+ move |self_, type_data : Option < Either < ReflectLuaValue , ReflectLuaProxied > > | {
78
+ // let world = world.clone(); // move copy into closure
79
+ match type_data {
80
+ Some ( Either :: Left ( value_data) ) => {
81
+ let other = ( value_data. from_value ) ( value, lua) ?;
82
+ self_. with_reflect_mut ( & world, |r, _, _| {
83
+ r. try_apply ( other. as_partial_reflect ( ) )
84
+ . map_err ( ScriptError :: new_reflection_error)
85
+ } ) ?
86
+ }
87
+ Some ( Either :: Right ( proxy_data) ) => {
88
+ let other = ( proxy_data. from_proxy ) ( value, lua) ?;
89
+ let other = other. with_reflect ( & world, |r, _, _| r. clone_value ( ) ) ?;
90
+ // now we can set it
91
+ self_. with_reflect_mut ( & world, |r, _, _| {
92
+ if let Some ( set) = proxy_data. opt_set {
93
+ set ( r, other)
94
+ } else {
95
+ r. try_apply ( other. as_partial_reflect ( ) )
96
+ . map_err ( ScriptError :: new_reflection_error) ?;
97
+ Ok ( ( ) )
98
+ }
99
+ } ) ?
100
+ }
101
+ None => {
102
+ world. with_resource ( |_, type_registry : Mut < AppTypeRegistry > | {
103
+ let type_registry = type_registry. read ( ) ;
104
+ Err ( ScriptError :: new_runtime_error ( format ! (
105
+ "Invalid assignment `{:?}` = `{:?}`. The underlying type does not support assignment." ,
106
+ self_. print_with_type_registry( & type_registry) ,
107
+ value
147
108
) ) )
148
- } ) ?;
149
- } ;
109
+ } ) ?;
110
+ Ok ( ( ) )
111
+ } ,
112
+ }
113
+ } ,
114
+ ) ??;
150
115
Ok ( ( ) )
151
116
}
152
117
@@ -250,8 +215,10 @@ impl TealData for LuaReflectReference {
250
215
251
216
m. add_function_mut (
252
217
"insert" ,
253
- |l, ( mut self_, key) : ( LuaReflectReference , usize , LuaReflect ) | {
254
- self_. 0 . insert_at ( key, l. get_world ( ) )
218
+ |l, ( mut self_, key) : ( LuaReflectReference , usize , Value ) | {
219
+ // check target type for a from_lua function
220
+ // let world = l.get_world();
221
+ // self.0.
255
222
} ,
256
223
) ;
257
224
0 commit comments