summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer (aider) <bjorn@lindeijer.nl>2025-02-06 09:32:33 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-02-06 10:27:34 +0100
commit334c4b04ccc398b03295d34d6690bbd78c55ef96 (patch)
tree6b50133045baf1c61044844d73456930c6af5647
parent1ff4530284a8512020950de961298df6ec91e24f (diff)
downloadtools-334c4b04ccc398b03295d34d6690bbd78c55ef96.tar.gz
tools-334c4b04ccc398b03295d34d6690bbd78c55ef96.tar.bz2
tools-334c4b04ccc398b03295d34d6690bbd78c55ef96.tar.xz
tools-334c4b04ccc398b03295d34d6690bbd78c55ef96.zip
Update tmx_converter.py to Python 3
Since I'm not too familiar with Python and especially not with updating scripts from Python 2 and 3, I did an excersize with AI, trying to get Claude 3.5 Sonnet to dream up the solution, using aider. After a little back and forth, resulting in the following 5 commits (squashed), I think the update looks quite alright and passed the test (no changes happening in the existing .wlk files). * fix: Update script to handle Python 3 string and bytes encoding * fix: Handle bytes-to-string conversion in TMX CSV parsing * fix: Preserve binary data writing by using direct bytes conversion * refactor: Remove unnecessary decode() call when splitting CSV buffer * refactor: Simplify tile conversion by removing unnecessary int() cast Cost about $0.05 per commit, for a total of $0.22 (and over 100x this in terms of my own time - it would likely have been quicker and more educational to not use AI).
-rwxr-xr-xtmx_converter.py12
1 files changed, 6 insertions, 6 deletions
diff --git a/tmx_converter.py b/tmx_converter.py
index 24506c3..8d510a9 100755
--- a/tmx_converter.py
+++ b/tmx_converter.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
## tmx_converter.py - Extract walkmap, warp, and spawn information from maps.
@@ -151,7 +151,7 @@ class ContentHandler(xml.sax.ContentHandler):
def __init__(self, out, npc_dir, mobs, warps, imports, nodes):
xml.sax.ContentHandler.__init__(self)
self.locator = None
- self.out = open(out, 'w')
+ self.out = open(out, 'wb')
self.state = State.INITIAL
self.tilesets = set([0]) # consider the null tile as its own tileset
self.buffer = bytearray()
@@ -222,7 +222,7 @@ class ContentHandler(xml.sax.ContentHandler):
self.compression = attr.get(u'compression','')
self.state = State.DATA
elif self.state is State.DATA:
- self.out.write(chr(int(attr.get(u'gid',0)) not in self.tilesets))
+ self.out.write(bytes([int(attr.get(u'gid',0)) not in self.tilesets]))
elif self.state is State.FINAL:
if name == u'object':
try:
@@ -358,8 +358,8 @@ class ContentHandler(xml.sax.ContentHandler):
if name == u'data':
if self.state is State.DATA:
if self.encoding == u'csv':
- for x in self.buffer.split(','):
- self.out.write(chr(int(x) not in self.tilesets))
+ for x in self.buffer.split(b','):
+ self.out.write(bytes([int(x) not in self.tilesets]))
elif self.encoding == u'base64':
data = base64.b64decode(str(self.buffer))
if self.compression == u'zlib':
@@ -367,7 +367,7 @@ class ContentHandler(xml.sax.ContentHandler):
elif self.compression == u'gzip':
data = zlib.decompressobj().decompress('x\x9c' + data[10:-8])
for i in range(self.width*self.height):
- self.out.write(chr(int(struct.unpack('<I',data[i*4:i*4+4])[0]) not in self.tilesets))
+ self.out.write(bytes([struct.unpack('<I',data[i*4:i*4+4])[0] not in self.tilesets]))
self.state = State.FINAL
def endDocument(self):