From ef830485dc1c62cb6a55c329804fb8f2e9dfde99 Mon Sep 17 00:00:00 2001 From: Michael de Villiers Date: Thu, 20 Jun 2024 21:45:06 +0200 Subject: [PATCH] Added optional support for zstd compression --- pyproject.toml | 3 ++- pytmx/pytmx.py | 15 +++++++++++++++ readme.md | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7ec4cd6..89a9517 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ packages = ["pytmx"] [project.optional-dependencies] pygame = ["pygame>=2.0.0"] pygame-ce = ["pygame-ce>=2.1.3"] +zstd = ["zstd>=1.5.5.1"] [tool.black] line-length = 88 @@ -42,4 +43,4 @@ target-version = ["py39"] [tool.isort] line_length = 88 profile = "black" -skip_gitignore = true \ No newline at end of file +skip_gitignore = true diff --git a/pytmx/pytmx.py b/pytmx/pytmx.py index 7d40621..2649870 100644 --- a/pytmx/pytmx.py +++ b/pytmx/pytmx.py @@ -24,6 +24,7 @@ import logging import os import struct +import sys import zlib from base64 import b64decode from collections import defaultdict, namedtuple @@ -41,6 +42,12 @@ except ImportError: pygame = None +# optional zstd compression support +try: + import zstd +except ImportError: + zstd = False + __all__ = ( "TileFlags", "TiledElement", @@ -175,6 +182,14 @@ def unpack_gids( data = gzip.decompress(data) elif compression == "zlib": data = zlib.decompress(data) + elif compression == "zstd": + if zstd: + data = zstd.decompress(data) + else: + raise ValueError( + "layer compression zstd is not supported. Install zstd " + "to enable support." + ) elif compression: raise ValueError(f"layer compression {compression} is not supported.") fmt = "<%dL" % (len(data) // 4) diff --git a/readme.md b/readme.md index 34ae48f..c4a2bad 100644 --- a/readme.md +++ b/readme.md @@ -100,7 +100,7 @@ Design Goals and Features * API with many handy functions * Memory efficient and performant * Loads data, "properties" metadata, and images from Tiled's TMX format -* Supports base64, csv, gzip, zlib and uncompressed XML formats +* Supports base64, csv, gzip, zlib, zstd and uncompressed XML formats * Properties for all native Tiled object types * Point data for polygon and polyline objects * Automatic flipping and rotation of tiles