Skip to content

Commit 436c3e2

Browse files
committed
Initial Implementation of temp:// Asset Source
1 parent 11f0a2d commit 436c3e2

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

crates/bevy_asset/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ron = "0.8"
4141
serde = { version = "1", features = ["derive"] }
4242
thiserror = "1.0"
4343
uuid = { version = "1.0", features = ["v4"] }
44+
tempfile = "3.10.1"
4445

4546
[target.'cfg(target_os = "android")'.dependencies]
4647
bevy_winit = { path = "../bevy_winit", version = "0.14.0-dev" }

crates/bevy_asset/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod loader;
3030
mod path;
3131
mod reflect;
3232
mod server;
33+
mod temp;
3334

3435
pub use assets::*;
3536
pub use bevy_asset_macros::Asset;
@@ -46,6 +47,7 @@ pub use server::*;
4647

4748
/// Rusty Object Notation, a crate used to serialize and deserialize bevy assets.
4849
pub use ron;
50+
use temp::TempAssetPlugin;
4951

5052
use crate::{
5153
io::{embedded::EmbeddedAssetRegistry, AssetSourceBuilder, AssetSourceBuilders, AssetSourceId},
@@ -91,6 +93,9 @@ pub struct AssetPlugin {
9193
pub mode: AssetMode,
9294
/// How/If asset meta files should be checked.
9395
pub meta_check: AssetMetaCheck,
96+
/// The path to use for temporary assets (relative to the project root).
97+
/// If not provided, a platform specific folder will be created and deleted upon exit.
98+
pub temporary_file_path: Option<String>,
9499
}
95100

96101
#[derive(Debug)]
@@ -138,6 +143,7 @@ impl Default for AssetPlugin {
138143
processed_file_path: Self::DEFAULT_PROCESSED_FILE_PATH.to_string(),
139144
watch_for_changes_override: None,
140145
meta_check: AssetMetaCheck::default(),
146+
temporary_file_path: None,
141147
}
142148
}
143149
}
@@ -219,7 +225,10 @@ impl Plugin for AssetPlugin {
219225
.add_event::<UntypedAssetLoadFailedEvent>()
220226
.configure_sets(PreUpdate, TrackAssets.after(handle_internal_asset_events))
221227
.add_systems(PreUpdate, handle_internal_asset_events)
222-
.register_type::<AssetPath>();
228+
.register_type::<AssetPath>()
229+
.add_plugins(TempAssetPlugin {
230+
temporary_file_path: self.temporary_file_path.clone(),
231+
});
223232
}
224233
}
225234

crates/bevy_asset/src/temp.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::path::{Path, PathBuf};
2+
3+
use bevy_app::Plugin;
4+
use bevy_ecs::system::Resource;
5+
6+
use crate::io::{AssetSourceBuilder, AssetSourceBuilders};
7+
8+
#[derive(Resource)]
9+
struct TempAssetDirectory {
10+
folder: TempDirectory,
11+
}
12+
13+
impl TempAssetDirectory {
14+
fn path(&self) -> &Path {
15+
self.folder.path()
16+
}
17+
}
18+
19+
enum TempDirectory {
20+
Delete(tempfile::TempDir),
21+
Persist(PathBuf),
22+
}
23+
24+
impl TempDirectory {
25+
fn path(&self) -> &Path {
26+
match self {
27+
TempDirectory::Delete(x) => x.path(),
28+
TempDirectory::Persist(x) => x.as_ref(),
29+
}
30+
}
31+
}
32+
33+
/// Plugin providing a temporary asset source.
34+
#[derive(Default)]
35+
pub(crate) struct TempAssetPlugin {
36+
/// The path to use for temporary assets (relative to the project root).
37+
/// If not provided, a platform specific folder will be created and deleted upon exit.
38+
pub temporary_file_path: Option<String>,
39+
}
40+
41+
impl Plugin for TempAssetPlugin {
42+
fn build(&self, app: &mut bevy_app::App) {
43+
let source = {
44+
let temp_dir = app
45+
.world_mut()
46+
.get_resource_or_insert_with::<TempAssetDirectory>(|| {
47+
let folder = match &self.temporary_file_path {
48+
Some(path) => TempDirectory::Persist(path.into()),
49+
None => TempDirectory::Delete(tempfile::tempdir().expect("todo")),
50+
};
51+
52+
TempAssetDirectory { folder }
53+
});
54+
55+
let path = temp_dir.path().to_str().expect("todo");
56+
57+
AssetSourceBuilder::platform_default(path, None)
58+
};
59+
60+
let mut sources = app
61+
.world_mut()
62+
.get_resource_or_insert_with::<AssetSourceBuilders>(Default::default);
63+
64+
sources.insert("temp", source);
65+
}
66+
}

0 commit comments

Comments
 (0)