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

Added optional support for zstd compression #187

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,4 +43,4 @@ target-version = ["py39"]
[tool.isort]
line_length = 88
profile = "black"
skip_gitignore = true
skip_gitignore = true
15 changes: 15 additions & 0 deletions pytmx/pytmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import os
import struct
import sys
import zlib
from base64 import b64decode
from collections import defaultdict, namedtuple
Expand All @@ -41,6 +42,12 @@
except ImportError:
pygame = None

# optional zstd compression support
try:
import zstd
except ImportError:
zstd = False

__all__ = (
"TileFlags",
"TiledElement",
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down