@@ -55,16 +55,18 @@ impl std::fmt::Display for Language {
55
55
#[ derive( Asset , TypePath , Clone ) ]
56
56
pub struct ScriptAsset {
57
57
/// The body of the script
58
- pub content : Box < [ u8 ] > ,
58
+ pub content : Box < [ u8 ] > , // Any chance a Cow<'static, ?> could work here?
59
59
/// The language of the script
60
60
pub language : Language ,
61
61
}
62
62
63
- #[ derive( Event , Debug , Clone ) ]
64
- pub ( crate ) enum ScriptAssetEvent {
65
- Added ( ScriptMetadata ) ,
66
- Removed ( ScriptMetadata ) ,
67
- Modified ( ScriptMetadata ) ,
63
+ impl From < String > for ScriptAsset {
64
+ fn from ( s : String ) -> ScriptAsset {
65
+ ScriptAsset {
66
+ content : s. into_bytes ( ) . into_boxed_slice ( ) ,
67
+ language : Language :: default ( ) ,
68
+ }
69
+ }
68
70
}
69
71
70
72
/// Script settings
@@ -112,6 +114,7 @@ impl AssetLoader for ScriptAssetLoader {
112
114
match load_context. path ( ) . extension ( ) . and_then ( |e| e. to_str ( ) ) . unwrap_or_default ( ) {
113
115
"lua" => Language :: Lua ,
114
116
"rhai" => Language :: Rhai ,
117
+ "rn" => Language :: Rune ,
115
118
x => {
116
119
warn ! ( "Unknown language for {:?}" , load_context. path( ) . display( ) ) ;
117
120
Language :: Unknown
@@ -129,178 +132,58 @@ impl AssetLoader for ScriptAssetLoader {
129
132
}
130
133
}
131
134
132
-
133
- /// A cache of asset id's to their script id's. Necessary since when we drop an asset we won't have the ability to get the path from the asset.
134
- #[ derive( Default , Debug , Resource ) ]
135
- pub struct ScriptMetadataStore {
136
- /// The map of asset id's to their metadata
137
- pub map : HashMap < AssetId < ScriptAsset > , ScriptMetadata > ,
138
- }
139
-
140
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
141
- /// Metadata for a script asset
142
- pub struct ScriptMetadata {
143
- /// The asset id of the script
144
- pub asset_id : AssetId < ScriptAsset > ,
145
- // The script id of the script
146
- // pub script_id: ScriptId,
147
- /// The language of the script
148
- pub language : Language ,
149
- }
150
-
151
- #[ profiling:: all_functions]
152
- impl ScriptMetadataStore {
153
- /// Inserts a new metadata entry
154
- pub fn insert ( & mut self , id : ScriptId , meta : ScriptMetadata ) {
155
- // TODO: new generations of assets are not going to have the same ID as the old one
156
- self . map . insert ( id, meta) ;
157
- }
158
-
159
- /// Gets a metadata entry
160
- pub fn get ( & self , id : ScriptId ) -> Option < & ScriptMetadata > {
161
- self . map . get ( & id)
162
- }
163
-
164
- /// Removes a metadata entry
165
- pub fn remove ( & mut self , id : ScriptId ) -> Option < ScriptMetadata > {
166
- self . map . remove ( & id)
167
- }
168
-
169
- /// Checks if the store contains a metadata entry
170
- pub fn contains ( & self , id : ScriptId ) -> bool {
171
- self . map . contains_key ( & id)
172
- }
173
- }
174
-
175
- /// Converts incoming asset events, into internal script asset events, also loads and inserts metadata for newly added scripts
176
- #[ profiling:: function]
177
- pub ( crate ) fn dispatch_script_asset_events (
178
- mut events : EventReader < AssetEvent < ScriptAsset > > ,
179
- mut script_asset_events : EventWriter < ScriptAssetEvent > ,
180
- assets : Res < Assets < ScriptAsset > > ,
181
- mut metadata_store : ResMut < ScriptMetadataStore > ,
182
- // settings: Res<ScriptAssetSettings>,
183
- ) {
184
- for event in events. read ( ) {
185
- match event {
186
- AssetEvent :: LoadedWithDependencies { id } | AssetEvent :: Added { id } => {
187
- // these can occur multiple times, we only send one added event though
188
- if !metadata_store. contains ( * id) {
189
- let asset = assets. get ( * id) ;
190
- if let Some ( asset) = asset {
191
- // let path = &asset.asset_path;
192
- // let converter = settings.script_id_mapper.map;
193
- // let script_id = converter(path);
194
-
195
- // let language = settings.select_script_language(path);
196
- let language = & asset. language ;
197
- if * language == Language :: Unknown {
198
- // let extension = path
199
- // .path()
200
- // .extension()
201
- // .and_then(|ext| ext.to_str())
202
- // .unwrap_or_default();
203
- // warn!("A script {:?} was added but its language is unknown. Consider adding the {:?} extension to the `ScriptAssetSettings`.", &script_id, extension);
204
- }
205
- let metadata = ScriptMetadata {
206
- asset_id : * id,
207
- // script_id,
208
- language : language. clone ( ) ,
209
- } ;
210
- debug ! ( "Script loaded, populating metadata: {:?}:" , metadata) ;
211
- script_asset_events. send ( ScriptAssetEvent :: Added ( metadata. clone ( ) ) ) ;
212
- metadata_store. insert ( * id, metadata) ;
213
- } else {
214
- warn ! ( "A script was added but it's asset was not found, failed to compute metadata. This script will not be loaded. Did you forget to store `Handle<ScriptAsset>` somewhere?. {}" , id) ;
215
- }
216
- }
217
- }
218
- AssetEvent :: Removed { id } => {
219
- if let Some ( metadata) = metadata_store. get ( * id) {
220
- debug ! ( "Script removed: {:?}" , metadata) ;
221
- script_asset_events. send ( ScriptAssetEvent :: Removed ( metadata. clone ( ) ) ) ;
222
- } else {
223
- warn ! ( "Script metadata not found for removed script asset: {}. Cannot properly clean up script" , id) ;
224
- }
225
- }
226
- AssetEvent :: Modified { id } => {
227
- if let Some ( metadata) = metadata_store. get ( * id) {
228
- debug ! ( "Script modified: {:?}" , metadata) ;
229
- script_asset_events. send ( ScriptAssetEvent :: Modified ( metadata. clone ( ) ) ) ;
230
- } else {
231
- warn ! ( "Script metadata not found for modified script asset: {}. Cannot properly update script" , id) ;
232
- }
233
- }
234
- _ => { }
235
- }
236
- }
237
- }
238
-
239
- /// Listens to [`ScriptAssetEvent::Removed`] events and removes the corresponding script metadata.
240
- #[ profiling:: function]
241
- pub ( crate ) fn remove_script_metadata (
242
- mut events : EventReader < ScriptAssetEvent > ,
243
- mut asset_path_map : ResMut < ScriptMetadataStore > ,
244
- ) {
245
- for event in events. read ( ) {
246
- if let ScriptAssetEvent :: Removed ( metadata) = event {
247
- let previous = asset_path_map. remove ( metadata. asset_id ) ;
248
- if let Some ( previous) = previous {
249
- debug ! ( "Removed script metadata: {:?}" , previous) ;
250
- }
251
- }
252
- }
253
- }
254
-
255
- /// Listens to [`ScriptAssetEvent`] events and dispatches [`CreateOrUpdateScript`] and [`DeleteScript`] commands accordingly.
135
+ /// Listens to [`AssetEvent`] events and dispatches [`CreateOrUpdateScript`] and [`DeleteScript`] commands accordingly.
256
136
///
257
137
/// Allows for hot-reloading of scripts.
258
138
#[ profiling:: function]
259
139
pub ( crate ) fn sync_script_data < P : IntoScriptPluginParams > (
260
- mut events : EventReader < ScriptAssetEvent > ,
140
+ mut events : EventReader < AssetEvent < ScriptAsset > > ,
261
141
script_assets : Res < Assets < ScriptAsset > > ,
262
142
static_scripts : Res < StaticScripts > ,
143
+ asset_server : Res < AssetServer > ,
263
144
mut commands : Commands ,
264
145
) {
265
146
for event in events. read ( ) {
266
- let metadata = match event {
267
- ScriptAssetEvent :: Added ( script_metadata)
268
- | ScriptAssetEvent :: Removed ( script_metadata)
269
- | ScriptAssetEvent :: Modified ( script_metadata) => script_metadata,
270
- } ;
271
-
272
- if metadata. language != P :: LANGUAGE {
273
- continue ;
274
- }
275
147
276
148
trace ! ( "{}: Received script asset event: {:?}" , P :: LANGUAGE , event) ;
277
149
match event {
278
150
// emitted when a new script asset is loaded for the first time
279
- ScriptAssetEvent :: Added ( _) | ScriptAssetEvent :: Modified ( _) => {
280
- if metadata. language != P :: LANGUAGE {
281
- trace ! (
282
- "{}: Script asset {} is for a different langauge than this sync system. Skipping." ,
283
- P :: LANGUAGE ,
284
- metadata. asset_id
285
- ) ;
286
- continue ;
287
- }
151
+ AssetEvent :: LoadedWithDependencies { id } | AssetEvent :: Added { id } | AssetEvent :: Modified { id } => {
152
+ if let Some ( asset) = script_assets. get ( * id) {
153
+ if asset. language != P :: LANGUAGE {
154
+ match asset_server. get_path ( * id) {
155
+ Some ( path) => {
156
+ trace ! (
157
+ "{}: Script path {} is for a different langauge than this sync system. Skipping." ,
158
+ P :: LANGUAGE ,
159
+ path) ;
160
+ }
161
+ None => {
162
+ trace ! (
163
+ "{}: Script id {} is for a different langauge than this sync system. Skipping." ,
164
+ P :: LANGUAGE ,
165
+ id) ;
166
+ }
167
+ }
168
+ continue ;
169
+ }
288
170
289
171
290
- if static_scripts. iter ( ) . any ( |handle| handle. id ( ) == metadata. asset_id ) {
291
- info ! ( "{}: Loading static script: {:?}" , P :: LANGUAGE , metadata. asset_id, ) ;
292
- if let Some ( asset) = script_assets. get ( metadata. asset_id ) {
172
+ if static_scripts. iter ( ) . any ( |handle| handle. id ( ) == * id) {
173
+ info ! ( "{}: Loading static script: {:?}" , P :: LANGUAGE , id) ;
293
174
commands. queue ( CreateOrUpdateScript :: < P > :: new (
294
- Handle :: Weak ( metadata . asset_id . clone ( ) ) ,
175
+ Handle :: Weak ( * id ) ,
295
176
asset. content . clone ( ) ,
296
177
Some ( script_assets. reserve_handle ( ) . clone_weak ( ) ) ,
297
178
) ) ;
298
179
}
299
180
}
300
181
}
301
- ScriptAssetEvent :: Removed ( _) => {
302
- info ! ( "{}: Deleting Script: {:?}" , P :: LANGUAGE , metadata. asset_id, ) ;
303
- commands. queue ( DeleteScript :: < P > :: new ( metadata. asset_id . clone ( ) ) ) ;
182
+ AssetEvent :: Removed { id } => {
183
+ info ! ( "{}: Deleting Script: {:?}" , P :: LANGUAGE , id) ;
184
+ commands. queue ( DeleteScript :: < P > :: new ( id. clone ( ) ) ) ;
185
+ }
186
+ AssetEvent :: Unused { id } => {
304
187
}
305
188
} ;
306
189
}
@@ -364,13 +247,14 @@ pub(crate) fn eval_script<P: IntoScriptPluginParams>(
364
247
pub ( crate ) fn configure_asset_systems ( app : & mut App ) -> & mut App {
365
248
// these should be in the same set as bevy's asset systems
366
249
// currently this is in the PreUpdate set
367
- app. add_systems (
368
- PreUpdate ,
369
- (
370
- dispatch_script_asset_events. in_set ( ScriptingSystemSet :: ScriptAssetDispatch ) ,
371
- remove_script_metadata. in_set ( ScriptingSystemSet :: ScriptMetadataRemoval ) ,
372
- ) ,
373
- )
250
+ app
251
+ // .add_systems(
252
+ // PreUpdate,
253
+ // (
254
+ // dispatch_script_asset_events.in_set(ScriptingSystemSet::ScriptAssetDispatch),
255
+ // remove_script_metadata.in_set(ScriptingSystemSet::ScriptMetadataRemoval),
256
+ // ),
257
+ // )
374
258
. configure_sets (
375
259
PreUpdate ,
376
260
(
@@ -379,10 +263,7 @@ pub(crate) fn configure_asset_systems(app: &mut App) -> &mut App {
379
263
. after ( ScriptingSystemSet :: ScriptAssetDispatch )
380
264
. before ( ScriptingSystemSet :: ScriptMetadataRemoval ) ,
381
265
) ,
382
- )
383
- . init_resource :: < ScriptMetadataStore > ( )
384
- // .init_resource::<ScriptAssetSettings>()
385
- . add_event :: < ScriptAssetEvent > ( ) ;
266
+ ) ;
386
267
387
268
app
388
269
}
0 commit comments