Skip to content

Commit

Permalink
Merge pull request #300 from mapeditor/next
Browse files Browse the repository at this point in the history
Merge next into current
  • Loading branch information
aleokdev authored Jun 14, 2024
2 parents c8ac751 + a0bc45f commit f86e079
Show file tree
Hide file tree
Showing 21 changed files with 583 additions and 108 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04 # Version 24 required for last version of CSFML

steps:
- name: Checkout
Expand All @@ -33,7 +33,7 @@ jobs:
run: cargo test --verbose

rustfmt:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -50,7 +50,7 @@ jobs:
run: cargo fmt --all -- --check

clippy:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -73,7 +73,7 @@ jobs:
run: cargo clippy --all-targets --package tiled

docs:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
24 changes: 22 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.12.0]
### Added
- Add `text`, `width` and `height` members to `ObjectShape::Text`. (#278)
- Implement `ResourceReader` for appropiate functions. (#272) **Read the README's FAQ for more information about this change.**
- Support for custom type properties. (#283)

### Changed
- Underlying reader for maps now uses a BufReader, which should give a large performance boost. (#286)
- Update `base64` to `0.22.1`. (#294)
- Update `libflate` to `2.1.0`. (#294)
- Update `zstd` to `0.13.1`. (#294)

### Fixed
- `ObjectShape::Text::kerning`'s default value, which should have been set to `true` instead of `false`. (#278)
- Unhandled u32 parsing panic in `decode_csv`. (#288)
- Panic in `<Color as FromStr>::from_str` when parsing non-ascii input. (#290)
- Index out of bounds in `InfiniteTileLayerData` when parsing a chunk. (#289)
- Panic on unexpected XML in `ObjectData` content. (#291)
- Divide by zero when parsing a tileset with height/width dimension of 0. (#292)

## [0.11.3]
## Changed
### Changed
- Replace `libflate` with `flate2`. (#281)

## [0.11.2]
## Changed
### Changed
- Updated `Image` docs. (#270)
- Update `libflate` dependency to `2.0.0`. (#279)
- Fix some doc links. (#273)
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiled"
version = "0.11.3"
version = "0.12.0"
description = "A rust crate for loading maps created by the Tiled editor"
categories = ["game-development"]
keywords = ["gamedev", "tiled", "tmx", "map"]
Expand Down Expand Up @@ -32,13 +32,13 @@ name = "ggez"
path = "examples/ggez/main.rs"

[dependencies]
base64 = "0.21.0"
base64 = "0.22.1"
xml-rs = "0.8.4"
zstd = { version = "0.13.1", optional = true, default-features = false }
flate2 = "1.0.28"
zstd = { version = "0.12.0", optional = true, default-features = false }

[dev-dependencies.sfml]
version = "0.20.0"
version = "0.21.0"
features = ["graphics"]

[dev-dependencies.ggez]
Expand Down
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rs-tiled
```toml
tiled = "0.11.2"
tiled = "0.12.0"
```

[![Rust](https://github.com/mapeditor/rs-tiled/actions/workflows/rust.yml/badge.svg)](https://github.com/mapeditor/rs-tiled/actions/workflows/rust.yml)
Expand All @@ -17,7 +17,7 @@ Code contributions are welcome as are bug reports, documentation, suggestions an

The minimum supported TMX version is 0.13.

### Example
## Example

```rust
use tiled::Loader;
Expand All @@ -34,7 +34,34 @@ fn main() {

```

### WASM
## FAQ
### How do I embed a map into my executable? / How do I read a file from anywhere else that isn't the filesystem's OS?
The crate does all of its reading through the `read_from` function of the [`ResourceReader`](https://docs.rs/tiled/latest/tiled/trait.ResourceReader.html) that you create the loader with. By default, this reader is set to [`FilesystemResourceReader`](https://docs.rs/tiled/latest/tiled/struct.FilesystemResourceReader.html) and all files are read through the OS's filesystem. You can however change this.

Here's an example mostly taken from `Loader::with_reader`'s documentation:
```rust
use tiled::{DefaultResourceCache, Loader};

let mut loader = Loader::with_reader(
// 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())
}
}
);
```
If the closure approach confuses you or you need more flexibility, you can always implement [`ResourceReader`](https://docs.rs/tiled/latest/tiled/trait.ResourceReader.html) on your own structure.

### How do I get the crate to work on WASM targets?
The crate supports WASM, but since it does not currently support asynchronous loading, there are some gotchas.

- First, to make it work on any WASM target, **enable the wasm feature**, like so:
Expand Down Expand Up @@ -66,7 +93,8 @@ impl tiled::ResourceReader for MyReader {
}
}
```
Check the `ResourceReader` docs for more information.
You can also use a function with the same signature as `tiled::ResourceReader::read_from`; check the
`ResourceReader` docs for more information.

### Licences

Expand Down
21 changes: 21 additions & 0 deletions assets/tiled_class_property.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="2" height="2" tilewidth="32" tileheight="32" infinite="0" nextlayerid="3" nextobjectid="4">
<layer id="1" name="Tile Layer 1" width="2" height="2">
<data encoding="csv">
0,0,
0,0
</data>
</layer>
<objectgroup id="2" name="Object Layer 1">
<object id="2" x="0" y="0" width="32" height="32">
<properties>
<property name="class property" type="class" propertytype="test_type">
<properties>
<property name="test_property_1" type="int" value="3"/>
</properties>
</property>
<property name="empty property" type="class" propertytype="empty_type"/>
</properties>
</object>
</objectgroup>
</map>
8 changes: 8 additions & 0 deletions assets/tiled_text_object.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="1" height="1" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="2">
<objectgroup id="2" name="Object Layer 1">
<object id="1" x="-24.1094" y="-2.39844" width="87.7188" height="21.7969">
<text color="#6455ff7f" bold="1" italic="1" underline="1" strikeout="1" halign="center" valign="bottom">Test</text>
</object>
</objectgroup>
</map>
5 changes: 1 addition & 4 deletions examples/ggez/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ struct Game {
impl Game {
fn new(ctx: &mut ggez::Context) -> GameResult<Self> {
// Load the map, using a loader with `GgezResourceReader` for reading from the ggez filesystem
let mut loader = tiled::Loader::with_cache_and_reader(
tiled::DefaultResourceCache::new(),
GgezResourceReader(&mut ctx.fs),
);
let mut loader = tiled::Loader::with_reader(GgezResourceReader(&mut ctx.fs));
let map = loader.load_tmx_map("/tiled_base64_external.tmx").unwrap();

let map_handler = MapHandler::new(map, ctx).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
24 changes: 24 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "tiled-fuzz"
version = "0.0.0"
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[build-dependencies]
glob = "0.3.1"

[dependencies.tiled]
path = ".."

[[bin]]
name = "tiled"
path = "fuzz_targets/tiled.rs"
test = false
doc = false
bench = false
28 changes: 28 additions & 0 deletions fuzz/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::path::PathBuf;

use glob::glob;

macro_rules! p {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}

fn main() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let corpus_dir = path.join("corpus/tiled");
std::fs::create_dir_all(&corpus_dir).expect("failed creating corpus dir");

let tmx_assets_path = path.join("../assets/*.tmx");
for entry in glob(tmx_assets_path.to_str().unwrap()).unwrap() {
match entry {
Ok(src) => {
let dest = corpus_dir.join(src.file_name().unwrap());
std::fs::copy(src, dest).unwrap();
}
Err(e) => {
p!("{:?}", e)
}
}
}
}
32 changes: 32 additions & 0 deletions fuzz/fuzz_targets/tiled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_main]

use libfuzzer_sys::fuzz_target;

use std::path::Path;

use tiled::{DefaultResourceCache, Loader, ResourceReader};

struct FuzzResourceReader<'a> {
data: &'a [u8],
}

impl<'a> FuzzResourceReader<'a> {
fn new(data: &'a [u8]) -> Self {
FuzzResourceReader { data }
}
}

impl<'a> ResourceReader for FuzzResourceReader<'a> {
type Resource = &'a [u8];
type Error = std::io::Error;

fn read_from(&mut self, _path: &Path) -> Result<Self::Resource, Self::Error> {
Ok(self.data)
}
}

fuzz_target!(|data: &[u8]| {
let mut loader =
Loader::with_cache_and_reader(DefaultResourceCache::new(), FuzzResourceReader::new(data));
let _ = loader.load_tmx_map("fuzz.tmx");
});
Loading

0 comments on commit f86e079

Please sign in to comment.