1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Saving and loading to the filesystem. [`save_scene`](crate::prelude::save_scene)
//! and [`load_scene`](crate::prelude::load_scene) produce and read the bytes;
//! these put them on disk. For a deterministic location (a save slot, a level
//! path) use [`save_scene_to_path`] and [`load_scene_from_path`]. For an
//! interactive save or open, the engine's [`save_file`] shows a native dialog (a
//! browser download on the web) and [`request_file_load`] opens a file picker and
//! hands back the bytes through a [`PendingFileLoad`] the caller polls, the same
//! way the editor does it.
use nightshade::prelude::{Entity, World};
pub use nightshade::filesystem::{FileError, FileFilter, LoadedFile, PendingFileLoad};
#[cfg(not(target_arch = "wasm32"))]
pub use nightshade::filesystem::{
pick_file, read_file, request_file_load, save_file, save_file_dialog, write_file,
};
#[cfg(target_arch = "wasm32")]
pub use nightshade::filesystem::{request_file_load, save_file};
/// Serializes the scene and writes it to `path`, returning whether it wrote. The
/// browser has no synchronous filesystem, so this is a no-op there; use
/// [`save_file`] for a download instead.
#[cfg(not(target_arch = "wasm32"))]
pub fn save_scene_to_path(world: &World, path: &str) -> bool {
match crate::serialize::save_scene(world, "scene") {
Ok(bytes) => write_file(std::path::Path::new(path), &bytes).is_ok(),
Err(_) => false,
}
}
#[cfg(target_arch = "wasm32")]
pub fn save_scene_to_path(_world: &World, _path: &str) -> bool {
false
}
/// Reads a scene file at `path` and spawns it, returning the spawned root
/// entities, or an empty list if the file is missing or invalid. The browser has
/// no synchronous filesystem, so this returns empty there; use
/// [`request_file_load`] and [`load_scene`](crate::prelude::load_scene) instead.
#[cfg(not(target_arch = "wasm32"))]
pub fn load_scene_from_path(world: &mut World, path: &str) -> Vec<Entity> {
match read_file(std::path::Path::new(path)) {
Ok(bytes) => crate::serialize::load_scene(world, &bytes).unwrap_or_default(),
Err(_) => Vec::new(),
}
}
#[cfg(target_arch = "wasm32")]
pub fn load_scene_from_path(_world: &mut World, _path: &str) -> Vec<Entity> {
Vec::new()
}