1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#!/usr/bin/python2
"""
Update pickle file containing maps information.
It is needed, because for some reason pytmx doesn't work
properly on android.
Author: Joseph Botosh <rumly111@gmail.com>
Licence: GPLv2.
"""
import os
import sys
import zipfile
import pytmx
try:
import cPickle as pickle
except:
import pickle
try:
import zlib
compression = zipfile.ZIP_DEFLATED
del zlib
except:
compression = zipfile.ZIP_STORED
modes = { zipfile.ZIP_DEFLATED: 'deflated',
zipfile.ZIP_STORED: 'stored' }
def LoadTmxMap(filename, maptag):
m = pytmx.TiledMap(filename=filename)
tmx = {
'tag': maptag,
'name': m.properties['name'],
'width': m.width,
'height': m.height,
'collisions': m.get_layer_by_name('Collision').data
}
return tmx
def PrintHelp():
print('Usage: {} <maps-dir> <outfile.pickle>'.format(sys.argv[0]))
if __name__ == '__main__':
if len(sys.argv) < 3:
PrintHelp()
sys.exit(0)
dirname = sys.argv[1]
outfile = sys.argv[2]
n = 0
zf = zipfile.ZipFile(outfile, 'w')
for tmx in (filter(lambda f: f.endswith('.tmx'),
os.listdir(dirname))):
path = os.path.join(dirname, tmx)
maptag = tmx[:-4]
print("Loading map {} ...".format(path))
try:
c = LoadTmxMap(path, maptag)
print("\tname={} tag={} size=({},{})".format(
c['name'], c['tag'], c['width'], c['height']))
zf.writestr(maptag + '.pickle', pickle.dumps(c), compression)
n += 1
except KeyError as e:
print("Error loading {}: {}".format(tmx, e))
zf.close()
print("Done processing {} maps".format(n))
|