Skip to content

Commit 84f5095

Browse files
committed
excise: Remove ScriptMetadata and ScriptEvent.
We don't need them since asset's know their language.
1 parent 77ce7a9 commit 84f5095

File tree

1 file changed

+48
-167
lines changed
  • crates/bevy_mod_scripting_core/src

1 file changed

+48
-167
lines changed

crates/bevy_mod_scripting_core/src/asset.rs

Lines changed: 48 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,18 @@ impl std::fmt::Display for Language {
5555
#[derive(Asset, TypePath, Clone)]
5656
pub struct ScriptAsset {
5757
/// The body of the script
58-
pub content: Box<[u8]>,
58+
pub content: Box<[u8]>, // Any chance a Cow<'static, ?> could work here?
5959
/// The language of the script
6060
pub language: Language,
6161
}
6262

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+
}
6870
}
6971

7072
/// Script settings
@@ -112,6 +114,7 @@ impl AssetLoader for ScriptAssetLoader {
112114
match load_context.path().extension().and_then(|e| e.to_str()).unwrap_or_default() {
113115
"lua" => Language::Lua,
114116
"rhai" => Language::Rhai,
117+
"rn" => Language::Rune,
115118
x => {
116119
warn!("Unknown language for {:?}", load_context.path().display());
117120
Language::Unknown
@@ -129,178 +132,58 @@ impl AssetLoader for ScriptAssetLoader {
129132
}
130133
}
131134

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.
256136
///
257137
/// Allows for hot-reloading of scripts.
258138
#[profiling::function]
259139
pub(crate) fn sync_script_data<P: IntoScriptPluginParams>(
260-
mut events: EventReader<ScriptAssetEvent>,
140+
mut events: EventReader<AssetEvent<ScriptAsset>>,
261141
script_assets: Res<Assets<ScriptAsset>>,
262142
static_scripts: Res<StaticScripts>,
143+
asset_server: Res<AssetServer>,
263144
mut commands: Commands,
264145
) {
265146
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-
}
275147

276148
trace!("{}: Received script asset event: {:?}", P::LANGUAGE, event);
277149
match event {
278150
// 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+
}
288170

289171

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);
293174
commands.queue(CreateOrUpdateScript::<P>::new(
294-
Handle::Weak(metadata.asset_id.clone()),
175+
Handle::Weak(*id),
295176
asset.content.clone(),
296177
Some(script_assets.reserve_handle().clone_weak()),
297178
));
298179
}
299180
}
300181
}
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 } => {
304187
}
305188
};
306189
}
@@ -364,13 +247,14 @@ pub(crate) fn eval_script<P: IntoScriptPluginParams>(
364247
pub(crate) fn configure_asset_systems(app: &mut App) -> &mut App {
365248
// these should be in the same set as bevy's asset systems
366249
// 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+
// )
374258
.configure_sets(
375259
PreUpdate,
376260
(
@@ -379,10 +263,7 @@ pub(crate) fn configure_asset_systems(app: &mut App) -> &mut App {
379263
.after(ScriptingSystemSet::ScriptAssetDispatch)
380264
.before(ScriptingSystemSet::ScriptMetadataRemoval),
381265
),
382-
)
383-
.init_resource::<ScriptMetadataStore>()
384-
// .init_resource::<ScriptAssetSettings>()
385-
.add_event::<ScriptAssetEvent>();
266+
);
386267

387268
app
388269
}

0 commit comments

Comments
 (0)