nightshade-api 0.46.0

Procedural high level API for the nightshade game engine
Documentation
//! 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()
}