Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiline string properties #110

Merged
merged 2 commits into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/tiled_base64.tmx

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions assets/tiled_base64_external.tmx

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions assets/tiled_base64_gzip.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<properties>
<property name="prop1" value="12"/>
<property name="prop2" value="some text"/>
<property name="prop3">Line 1
Line 2
Line 3,
etc
</property>
</properties>
<data encoding="base64" compression="gzip">
H4sIAAAAAAAACu3XMQrDMAwFUF0hJwglc9L7364NjUG4dulQu8t7IMiQSR8heYuIrapbpxhvuWq/6njWvVNymaNkUs/JqZURc5wZLOm7rpKLGRlvjVfP107t8T47jJH7vTcqZ3KETEYrfa/7XO/6vEtkMk7u87kbcq9LJjV5jFPfUOWube30CFnM0Lpr6/dGyaE3M/zep1zy210e47Tu2FYuW3gTzvDtLrAz5ujt7d5/zPUpG3n837fzw3zyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgewAvf+EvQJwAAA==
Expand Down
5 changes: 5 additions & 0 deletions assets/tiled_base64_zlib.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<properties>
<property name="prop1" value="12"/>
<property name="prop2" value="some text"/>
<property name="prop3">Line 1
Line 2
Line 3,
etc
</property>
</properties>
<data encoding="base64" compression="zlib">
eJzt1zEKwzAMBVBdIScIJXPS+9+uDY1BuHbpULvLeyDIkEkfIXmLiK2qW6cYb7lqv+p41r1TcpmjZFLPyamVEXOcGSzpu66SixkZb41Xz9dO7fE+O4yR+703KmdyhExGK32v+1zv+rxLZDJO7vO5G3KvSyY1eYxT31Dlrm3t9AhZzNC6a+v3RsmhNzP83qdc8ttdHuO07thWLlt4E87w7S6wM+bo7e3ef8z1KRt5/N+388N88gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIHsArPIXTA==
Expand Down
5 changes: 5 additions & 0 deletions assets/tiled_base64_zstandard.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<properties>
<property name="prop1" value="12"/>
<property name="prop2" value="some text"/>
<property name="prop3">Line 1
Line 2
Line 3,
etc
</property>
</properties>
<data encoding="base64" compression="zstd">
KLUv/WBAm6UFAPgjAAAAIREtLgAAAC8RACMfHy0tLi0tLSMjIyMjIyMjSaBguwAAg0JmtQEFA2qvb+Al6lHUO91VvxpMveegXVabfkkjaEj1wZVZeNWPbSrdISdZk1b145uYEEFAvQmwiF5zTMi2QsOb9Vhv7ZxsK27ZJBXC9lYf2mVdhuqWjb8/hITgZAMw1ctxfXIOXBaofKbranfWDTtJ/Xyz7E2ZNWzVKauthC4r26Ry1TnnDqtmvUfyDw==
Expand Down
5 changes: 5 additions & 0 deletions assets/tiled_csv.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<properties>
<property name="prop1" value="12"/>
<property name="prop2" value="some text"/>
<property name="prop3">Line 1
Line 2
Line 3,
etc
</property>
</properties>
<data encoding="csv">
35,35,35,35,35,33,33,33,33,33,33,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Expand Down
17 changes: 14 additions & 3 deletions src/properties.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, io::Read, str::FromStr};

use xml::{attribute::OwnedAttribute, EventReader};
use xml::{EventReader, attribute::OwnedAttribute, reader::XmlEvent};

use crate::{
error::{ParseTileError, TiledError},
Expand Down Expand Up @@ -87,18 +87,29 @@ pub(crate) fn parse_properties<R: Read>(
let mut p = HashMap::new();
parse_tag!(parser, "properties", {
"property" => |attrs:Vec<OwnedAttribute>| {
let (t, (k, v)) = get_attrs!(
let ((t, v_attr), k) = get_attrs!(
attrs,
optionals: [
("type", property_type, |v| Some(v)),
("value", value, |v| Some(v)),
],
required: [
("name", key, |v| Some(v)),
("value", value, |v| Some(v)),
],
TiledError::MalformedAttributes("property must have a name and a value".to_string())
);
let t = t.unwrap_or("string".into());

let v = match v_attr {
Some(val) => val,
None => {
// if the "value" attribute was missing, might be a multiline string
match parser.next().map_err(TiledError::XmlDecodingError)? {
XmlEvent::Characters(s) => Ok(s),
_ => Err(TiledError::MalformedAttributes(format!("property '{}' is missing a value", k))),
}?
}
};

p.insert(k, PropertyValue::new(t, v)?);
Ok(())
Expand Down
13 changes: 13 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ fn test_tile_property() {
assert_eq!("123", prop_value);
}

#[test]
fn test_layer_property() {
let r = read_from_file(&Path::new("assets/tiled_base64.tmx")).unwrap();
let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) =
r.layers[0].properties.get("prop3")
{
v.clone()
} else {
String::new()
};
assert_eq!("Line 1\r\nLine 2\r\nLine 3,\r\n etc\r\n ", prop_value);
}

#[test]
fn test_object_group_property() {
let r = read_from_file(&Path::new("assets/tiled_object_groups.tmx")).unwrap();
Expand Down