Skip to content

Commit f1b93bd

Browse files
authored
docs: Update docs (#235)
* update docs * change asset path mappers to use actual asset path * run on pr events too * fix up more docs * add badges to docs.io and deny missing docs lint * only run PR check on opened * fix for more workflows * enable rhai for doc gen * get rid of dupplicate CI jobs * fix missing docs lints
1 parent 9629224 commit f1b93bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+644
-161
lines changed

.github/workflows/bevy_mod_scripting.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
on:
22
push:
3+
branches:
4+
- main
5+
- staging
6+
paths-ignore:
7+
- '.github/workflows/release-plz.yml'
8+
- 'docs/**'
9+
pull_request:
310
branches:
411
- "**"
512
paths-ignore:

.github/workflows/mdbook.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ name: Deploy mdBook to GitHub Pages
22

33
on:
44
push:
5+
branches:
6+
- master
7+
- staging
8+
paths:
9+
- 'docs/**'
10+
- '.github/workflows/mdbook.yml'
11+
pull_request:
512
branches:
613
- "**"
714
paths:

Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ homepage = "https://github.com/makspll/bevy_mod_scripting"
1010
keywords = ["bevy", "gamedev", "scripting", "lua"]
1111
categories = ["game-development"]
1212
readme = "readme.md"
13-
include = ["readme.md", "/src", "/examples", "/assets", "LICENSE"]
13+
include = ["readme.md", "/src", "/examples", "/assets", "LICENSE", "/badges"]
1414

1515
[lib]
1616
name = "bevy_mod_scripting"
1717
path = "src/lib.rs"
1818

1919
[package.metadata."docs.rs"]
20-
features = ["lua54"]
20+
features = ["lua54", "rhai"]
2121

2222
[features]
2323
default = ["core_functions", "bevy_bindings"]
@@ -58,7 +58,7 @@ bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", v
5858
# bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.9.0-alpha.2", optional = true }
5959
bevy_mod_scripting_functions = { workspace = true }
6060
[workspace.dependencies]
61-
profiling = {version = "1.0" }
61+
profiling = { version = "1.0" }
6262
bevy = { version = "0.15.0", default-features = false }
6363
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.9.0-alpha.9" }
6464
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.9", default-features = false }
@@ -127,3 +127,6 @@ panic = "deny"
127127
unwrap_used = "deny"
128128
expect_used = "deny"
129129
todo = "deny"
130+
131+
[workspace.lints.rust]
132+
missing_docs = "deny"

crates/bevy_mod_scripting_core/src/asset.rs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Systems and resources for handling script assets and events
2+
13
use crate::{
24
commands::{CreateOrUpdateScript, DeleteScript},
35
error::ScriptError,
@@ -6,7 +8,7 @@ use crate::{
68
};
79
use bevy::{
810
app::{App, PreUpdate},
9-
asset::{Asset, AssetEvent, AssetId, AssetLoader, Assets},
11+
asset::{Asset, AssetEvent, AssetId, AssetLoader, AssetPath, Assets},
1012
ecs::system::Resource,
1113
log::{debug, error, info, trace},
1214
prelude::{
@@ -16,17 +18,18 @@ use bevy::{
1618
reflect::TypePath,
1719
utils::HashMap,
1820
};
19-
use std::{
20-
borrow::Cow,
21-
path::{Path, PathBuf},
22-
};
21+
use std::borrow::Cow;
2322

2423
/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
2524
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2625
pub enum Language {
26+
/// The Rhai scripting language
2727
Rhai,
28+
/// The Lua scripting language
2829
Lua,
30+
/// The Rune scripting language
2931
Rune,
32+
/// An external scripting language
3033
External(Cow<'static, str>),
3134
/// Set if none of the asset path to language mappers match
3235
Unknown,
@@ -47,9 +50,10 @@ impl std::fmt::Display for Language {
4750
/// Represents a script loaded into memory as an asset
4851
#[derive(Asset, TypePath, Clone)]
4952
pub struct ScriptAsset {
53+
/// The body of the script
5054
pub content: Box<[u8]>,
5155
/// The virtual filesystem path of the asset, used to map to the script Id for asset backed scripts
52-
pub asset_path: PathBuf,
56+
pub asset_path: AssetPath<'static>,
5357
}
5458

5559
#[derive(Event, Debug, Clone)]
@@ -60,6 +64,7 @@ pub(crate) enum ScriptAssetEvent {
6064
}
6165

6266
#[derive(Default)]
67+
/// A loader for script assets
6368
pub struct ScriptAssetLoader {
6469
/// The file extensions this loader should handle
6570
pub extensions: &'static [&'static str],
@@ -90,7 +95,7 @@ impl AssetLoader for ScriptAssetLoader {
9095
}
9196
let asset = ScriptAsset {
9297
content: content.into_boxed_slice(),
93-
asset_path: load_context.path().to_owned(),
98+
asset_path: load_context.asset_path().to_owned(),
9499
};
95100
Ok(asset)
96101
}
@@ -101,13 +106,17 @@ impl AssetLoader for ScriptAssetLoader {
101106
}
102107

103108
#[derive(Clone, Resource)]
109+
/// Settings to do with script assets and how they are handled
104110
pub struct ScriptAssetSettings {
111+
/// Strategy for mapping asset paths to script ids, by default this is the identity function
105112
pub script_id_mapper: AssetPathToScriptIdMapper,
113+
/// Strategies for mapping asset paths to languages
106114
pub script_language_mappers: Vec<AssetPathToLanguageMapper>,
107115
}
108116

109117
impl ScriptAssetSettings {
110-
pub fn select_script_language(&self, path: &Path) -> Language {
118+
/// Selects the language for a given asset path
119+
pub fn select_script_language(&self, path: &AssetPath) -> Language {
111120
for mapper in &self.script_language_mappers {
112121
let language = (mapper.map)(path);
113122
match language {
@@ -124,7 +133,7 @@ impl Default for ScriptAssetSettings {
124133
fn default() -> Self {
125134
Self {
126135
script_id_mapper: AssetPathToScriptIdMapper {
127-
map: (|path: &Path| path.to_string_lossy().into_owned().into()),
136+
map: (|path: &AssetPath| path.path().to_string_lossy().into_owned().into()),
128137
},
129138
script_language_mappers: vec![],
130139
}
@@ -134,41 +143,53 @@ impl Default for ScriptAssetSettings {
134143
/// Strategy for mapping asset paths to script ids, by default this is the identity function
135144
#[derive(Clone, Copy)]
136145
pub struct AssetPathToScriptIdMapper {
137-
pub map: fn(&Path) -> ScriptId,
146+
/// The mapping function
147+
pub map: fn(&AssetPath) -> ScriptId,
138148
}
139149

140150
#[derive(Clone, Copy)]
151+
/// Strategy for mapping asset paths to languages
141152
pub struct AssetPathToLanguageMapper {
142-
pub map: fn(&Path) -> Language,
153+
/// The mapping function
154+
pub map: fn(&AssetPath) -> Language,
143155
}
144156

145157
/// 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.
146158
#[derive(Default, Debug, Resource)]
147159
pub struct ScriptMetadataStore {
160+
/// The map of asset id's to their metadata
148161
pub map: HashMap<AssetId<ScriptAsset>, ScriptMetadata>,
149162
}
150163

151164
#[derive(Debug, Clone, PartialEq, Eq)]
165+
/// Metadata for a script asset
152166
pub struct ScriptMetadata {
167+
/// The asset id of the script
153168
pub asset_id: AssetId<ScriptAsset>,
169+
/// The script id of the script
154170
pub script_id: ScriptId,
171+
/// The language of the script
155172
pub language: Language,
156173
}
157174

158175
impl ScriptMetadataStore {
176+
/// Inserts a new metadata entry
159177
pub fn insert(&mut self, id: AssetId<ScriptAsset>, meta: ScriptMetadata) {
160178
// TODO: new generations of assets are not going to have the same ID as the old one
161179
self.map.insert(id, meta);
162180
}
163181

182+
/// Gets a metadata entry
164183
pub fn get(&self, id: AssetId<ScriptAsset>) -> Option<&ScriptMetadata> {
165184
self.map.get(&id)
166185
}
167186

187+
/// Removes a metadata entry
168188
pub fn remove(&mut self, id: AssetId<ScriptAsset>) -> Option<ScriptMetadata> {
169189
self.map.remove(&id)
170190
}
171191

192+
/// Checks if the store contains a metadata entry
172193
pub fn contains(&self, id: AssetId<ScriptAsset>) -> bool {
173194
self.map.contains_key(&id)
174195
}
@@ -333,6 +354,8 @@ pub(crate) fn configure_asset_systems_for_plugin<P: IntoScriptPluginParams>(
333354

334355
#[cfg(test)]
335356
mod tests {
357+
use std::path::{Path, PathBuf};
358+
336359
use bevy::{
337360
app::{App, Update},
338361
asset::{AssetApp, AssetPlugin, AssetServer, Assets, Handle, LoadState},
@@ -352,12 +375,12 @@ mod tests {
352375
fn make_test_settings() -> ScriptAssetSettings {
353376
ScriptAssetSettings {
354377
script_id_mapper: AssetPathToScriptIdMapper {
355-
map: |path| path.to_string_lossy().into_owned().into(),
378+
map: |path| path.path().to_string_lossy().into_owned().into(),
356379
},
357380
script_language_mappers: vec![
358381
AssetPathToLanguageMapper {
359382
map: |path| {
360-
if path.extension().unwrap() == "lua" {
383+
if path.path().extension().unwrap() == "lua" {
361384
Language::Lua
362385
} else {
363386
Language::Unknown
@@ -366,7 +389,7 @@ mod tests {
366389
},
367390
AssetPathToLanguageMapper {
368391
map: |path| {
369-
if path.extension().unwrap() == "rhai" {
392+
if path.path().extension().unwrap() == "rhai" {
370393
Language::Rhai
371394
} else {
372395
Language::Unknown
@@ -427,7 +450,7 @@ mod tests {
427450

428451
assert_eq!(
429452
asset.asset_path,
430-
PathBuf::from("test_assets/test_script.script")
453+
AssetPath::from_path(&PathBuf::from("test_assets/test_script.script"))
431454
);
432455

433456
assert_eq!(
@@ -457,7 +480,7 @@ mod tests {
457480

458481
assert_eq!(
459482
asset.asset_path,
460-
PathBuf::from("test_assets/test_script.script")
483+
AssetPath::from(PathBuf::from("test_assets/test_script.script"))
461484
);
462485
assert_eq!(
463486
String::from_utf8(asset.content.clone().to_vec()).unwrap(),
@@ -485,14 +508,14 @@ mod tests {
485508
fn test_script_asset_settings_select_language() {
486509
let settings = make_test_settings();
487510

488-
let path = Path::new("test.lua");
489-
assert_eq!(settings.select_script_language(path), Language::Lua);
511+
let path = AssetPath::from(Path::new("test.lua"));
512+
assert_eq!(settings.select_script_language(&path), Language::Lua);
490513
assert_eq!(
491-
settings.select_script_language(Path::new("test.rhai")),
514+
settings.select_script_language(&AssetPath::from(Path::new("test.rhai"))),
492515
Language::Rhai
493516
);
494517
assert_eq!(
495-
settings.select_script_language(Path::new("test.blob")),
518+
settings.select_script_language(&AssetPath::from(Path::new("test.blob"))),
496519
Language::Unknown
497520
);
498521
}

0 commit comments

Comments
 (0)