Skip to content

Commit 923149c

Browse files
committed
Better object template loading
1 parent 22d7631 commit 923149c

File tree

4 files changed

+65
-26
lines changed

4 files changed

+65
-26
lines changed

pytiled_parser/parsers/json/tiled_object.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Object parsing for the JSON Map Format.
22
"""
33
import json
4+
import xml.etree.ElementTree as etree
45
from pathlib import Path
56
from typing import Any, Callable, Dict, List, Optional
67

@@ -19,7 +20,7 @@
1920
Tile,
2021
TiledObject,
2122
)
22-
from pytiled_parser.util import parse_color
23+
from pytiled_parser.util import load_object_template, parse_color
2324

2425

2526
class RawText(TypedDict):
@@ -300,20 +301,19 @@ def parse(
300301
"A parent directory must be specified when using object templates."
301302
)
302303
template_path = Path(parent_dir / raw_object["template"])
303-
with open(template_path) as raw_template_file:
304-
template = json.load(raw_template_file)
305-
if "tileset" in template:
306-
tileset_path = Path(
307-
template_path.parent / template["tileset"]["source"]
308-
)
309-
with open(tileset_path) as raw_tileset_file:
310-
new_tileset = json.load(raw_tileset_file)
311-
new_tileset_path = tileset_path.parent
304+
template, new_tileset, new_tileset_path = load_object_template(template_path)
312305

306+
if isinstance(template, dict):
313307
loaded_template = template["object"]
314308
for key in loaded_template:
315309
if key != "id":
316310
raw_object[key] = loaded_template[key] # type: ignore
311+
elif isinstance(template, etree.Element):
312+
# load the XML object into the JSON object
313+
raise NotImplementedError(
314+
"Loading TMX object templates inside a JSON map is currently not supported, "
315+
"but will be in a future release."
316+
)
317317

318318
if raw_object.get("gid"):
319319
return _parse_tile(raw_object, new_tileset, new_tileset_path)

pytiled_parser/parsers/tmx/tiled_map.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def parse(file: Path) -> TiledMap:
9494
break
9595

9696
if not already_loaded:
97+
print("here")
9798
highest_firstgid = max(map_.tilesets.keys())
9899
last_tileset_count = map_.tilesets[highest_firstgid].tile_count
99100
new_firstgid = highest_firstgid + last_tileset_count

pytiled_parser/parsers/tmx/tiled_object.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import xml.etree.ElementTree as etree
23
from pathlib import Path
34
from typing import Callable, Optional
@@ -14,7 +15,7 @@
1415
Tile,
1516
TiledObject,
1617
)
17-
from pytiled_parser.util import parse_color
18+
from pytiled_parser.util import load_object_template, parse_color
1819

1920

2021
def _parse_common(raw_object: etree.Element) -> TiledObject:
@@ -264,18 +265,9 @@ def parse(raw_object: etree.Element, parent_dir: Optional[Path] = None) -> Tiled
264265
"A parent directory must be specified when using object templates."
265266
)
266267
template_path = Path(parent_dir / raw_object.attrib["template"])
267-
with open(template_path) as template_file:
268-
template = etree.parse(template_file).getroot()
269-
270-
tileset_element = template.find("./tileset")
271-
if tileset_element:
272-
tileset_path = Path(
273-
template_path.parent / tileset_element.attrib["source"]
274-
)
275-
with open(tileset_path) as tileset_file:
276-
new_tileset = etree.parse(tileset_file).getroot()
277-
new_tileset_path = tileset_path.parent
268+
template, new_tileset, new_tileset_path = load_object_template(template_path)
278269

270+
if isinstance(template, etree.Element):
279271
new_object = template.find("./object")
280272
if new_object is not None:
281273
if raw_object.attrib.get("id") is not None:
@@ -288,11 +280,14 @@ def parse(raw_object: etree.Element, parent_dir: Optional[Path] = None) -> Tiled
288280
new_object.attrib["y"] = raw_object.attrib["y"]
289281

290282
raw_object = new_object
291-
292-
if raw_object.attrib.get("gid"):
293-
return _parse_tile(raw_object, new_tileset, new_tileset_path)
283+
elif isinstance(template, dict):
284+
# load the JSON object into the XML object
285+
raise NotImplementedError(
286+
"Loading JSON object templates inside a TMX map is currently not supported, "
287+
"but will be in a future release."
288+
)
294289

295290
if raw_object.attrib.get("gid"):
296-
return _parse_tile(raw_object)
291+
return _parse_tile(raw_object, new_tileset, new_tileset_path)
297292

298293
return _get_parser(raw_object)(raw_object)

pytiled_parser/util.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Utility Functions for PyTiled"""
2+
import json
3+
import xml.etree.ElementTree as etree
24
from pathlib import Path
5+
from typing import Any
36

47
from pytiled_parser.common_types import Color
58

@@ -37,3 +40,43 @@ def check_format(file_path: Path) -> str:
3740
return "tmx"
3841
else:
3942
return "json"
43+
44+
45+
def load_object_template(file_path: Path) -> Any:
46+
template_format = check_format(file_path)
47+
48+
new_tileset = None
49+
new_tileset_path = None
50+
51+
if template_format == "tmx":
52+
with open(file_path) as template_file:
53+
template = etree.parse(template_file).getroot()
54+
55+
tileset_element = template.find("./tileset")
56+
if tileset_element is not None:
57+
tileset_path = Path(file_path.parent / tileset_element.attrib["source"])
58+
new_tileset = load_object_tileset(tileset_path)
59+
new_tileset_path = tileset_path.parent
60+
elif template_format == "json":
61+
with open(file_path) as template_file:
62+
template = json.load(template_file)
63+
if "tileset" in template:
64+
tileset_path = Path(file_path.parent / template["tileset"]["source"]) # type: ignore
65+
new_tileset = load_object_tileset(tileset_path)
66+
new_tileset_path = tileset_path.parent
67+
68+
return (template, new_tileset, new_tileset_path)
69+
70+
71+
def load_object_tileset(file_path: Path) -> Any:
72+
tileset_format = check_format(file_path)
73+
74+
new_tileset = None
75+
76+
with open(file_path) as tileset_file:
77+
if tileset_format == "tmx":
78+
new_tileset = etree.parse(tileset_file).getroot()
79+
elif tileset_format == "json":
80+
new_tileset = json.load(tileset_file)
81+
82+
return new_tileset

0 commit comments

Comments
 (0)