mapeditor / rs-tiled

Reads files from the Tiled editor into Rust
https://crates.io/crates/tiled
MIT License
268 stars 102 forks source link

Improve ResourceReader's API, add better docs #272

Closed aleokdev closed 1 year ago

aleokdev commented 1 year ago

Closes #266. Implements ResourceReader for functions so that you can do stuff like this:

use tiled::{DefaultResourceCache, Loader};

let mut loader = Loader::with_cache_and_reader(
    DefaultResourceCache::new(),
    // Specify the reader to use. We can use anything that implements `ResourceReader`, e.g. FilesystemResourceReader.
    // Any function that has the same signature as `ResourceReader::read_from` also implements it.
    // Here we define a reader that embeds the map at "assets/tiled_xml.csv" into the executable, and allow
    // accessing it only through "/my-map.tmx"
    // ALL maps, tilesets and templates will be read through this function, even if you don't explicitly load them
    // (They can be dependencies of one you did want to load in the first place).
    // Doing this embedding is useful for places where the OS filesystem is not available (e.g. WASM applications).
    |path: &std::path::Path| -> std::io::Result<_> {
        if path == std::path::Path::new("/my-map.tmx") {
            Ok(std::io::Cursor::new(include_bytes!("../assets/tiled_csv.tmx")))
        } else {
            Err(std::io::ErrorKind::NotFound.into())
        }
    }
);

Also adds a FAQ section on loading embedded maps and improves Loader::with_cache_and_reader docs.

aleokdev commented 1 year ago

Not sure why current merge commits are still showing up in the timeline. Edit: Nevermind, fixed

aleokdev commented 1 year ago

Reminder: also change WASM section in readme