diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/generate-packet-lengths.py | 54 | ||||
-rw-r--r-- | utils/update-item-db.py | 42 | ||||
-rw-r--r-- | utils/update-map-db.py | 76 | ||||
-rw-r--r-- | utils/update-map-names.py | 32 | ||||
-rw-r--r-- | utils/update-monster-db.py | 42 |
5 files changed, 246 insertions, 0 deletions
diff --git a/utils/generate-packet-lengths.py b/utils/generate-packet-lengths.py new file mode 100644 index 0000000..a88882e --- /dev/null +++ b/utils/generate-packet-lengths.py @@ -0,0 +1,54 @@ +#!/usr/bin/python + +""" +Generate a dict of packet lenghts, based on packetsin.inc from ManaPlus. +Author: Joseph Botosh <rumly111@gmail.com> +Licence: GPLv2. +""" + +import os +import sys + + +def GeneratePacketLengths(infile): + plength = {} + with open(infile) as f: + for l in f: + if l.startswith('packet('): + w = l[7:-2].split(',') + opcode = int(w[1].strip(), 16) + length = int(w[2].strip()) + plength[opcode] = length + return plength + + +def PrettyPrint(plengths, width=80): + s = 'packet_lengths = {\n ' + curr_line_len = 4 + for opcode in sorted(plengths.keys()): + r = '0x{:04x}: {:d},'.format(opcode, plengths[opcode]).ljust(12) + if curr_line_len + len(r) > width: + curr_line_len = 4 + s += '\n ' + s += r + curr_line_len += len(r) + s += '}\n' + return s + + +def PrintHelp(): + print('Usage: {} /path/to/packetsin.inc'.format(sys.argv[0])) + +if __name__ == '__main__': + if len(sys.argv) == 1: + PrintHelp() + sys.exit(0) + filename = sys.argv[1] + if os.path.isfile(filename): + pl = GeneratePacketLengths(filename) + print(PrettyPrint(pl)) + # print('packet_lengths = ', + # GeneratePacketLengths(filename)) + else: + print('File not found:', filename, file=sys.stderr) + sys.exit(1) diff --git a/utils/update-item-db.py b/utils/update-item-db.py new file mode 100644 index 0000000..27969de --- /dev/null +++ b/utils/update-item-db.py @@ -0,0 +1,42 @@ +#!/usr/bin/python + +""" +Update text file containing item IDs and names. +Author: Joseph Botosh <rumly111@gmail.com> +Licence: GPLv2. +""" + +import os +import sys +from xml.etree.ElementTree import ElementTree + + +def ScanItemsXML(filename): + result = [] + file1 = ElementTree(file=filename) + for item1 in filter(lambda it: it.get('name'), file1.getroot()): + file2 = ElementTree(file=item1.get('name')) + for item2 in filter(lambda it: it.get('name'), file2.getroot()): + file3 = ElementTree(file=item2.get('name')) + for item3 in file3.getroot(): + name = item3.get('name') + id_ = item3.get('id') + if name is not None and id_ is not None and int(id_) > 0: + result.append((id_, name)) + return result + + +def PrintHelp(): + print('Usage: {} <items.xml>'.format(sys.argv[0])) + +if __name__ == '__main__': + if len(sys.argv) == 1: + PrintHelp() + sys.exit(0) + filename = sys.argv[1] + if os.path.isfile(filename): + for id_, name in ScanItemsXML(filename): + print(id_, name) + else: + print('File not found:', filename, file=sys.stderr) + sys.exit(1) diff --git a/utils/update-map-db.py b/utils/update-map-db.py new file mode 100644 index 0000000..a17f5f1 --- /dev/null +++ b/utils/update-map-db.py @@ -0,0 +1,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)) diff --git a/utils/update-map-names.py b/utils/update-map-names.py new file mode 100644 index 0000000..8274390 --- /dev/null +++ b/utils/update-map-names.py @@ -0,0 +1,32 @@ +#!/usr/bin/python2 + +""" +Author: Joseph Botosh <rumly111@gmail.com> +Licence: GPLv2. +""" + +import os +import sys +import pytmx + + +if __name__ == '__main__': + if len(sys.argv) < 2: + print('Usage: {} <maps-dir>'.format(sys.argv[0])) + sys.exit(0) + + dirname = sys.argv[1] + + for tmx in (filter(lambda f: f.endswith('.tmx'), + os.listdir(dirname))): + path = os.path.join(dirname, tmx) + maptag = tmx[:-4] + m = pytmx.TiledMap(filename=path) + try: + if 'name' in m.properties: + mapname = m.properties['name'] + else: + mapname = m.properties['Name'] + print maptag, mapname + except KeyError: + print '[error]', maptag diff --git a/utils/update-monster-db.py b/utils/update-monster-db.py new file mode 100644 index 0000000..a77cb81 --- /dev/null +++ b/utils/update-monster-db.py @@ -0,0 +1,42 @@ +#!/usr/bin/python + +""" +Update text file containing monster IDs and names. +Author: Joseph Botosh <rumly111@gmail.com> +Licence: GPLv2. +""" + +import os +import sys +from xml.etree.ElementTree import ElementTree + + +def ScanItemsXML(filename): + result = [] + file1 = ElementTree(file=filename) + for item1 in filter(lambda it: it.get('name'), file1.getroot()): + file2 = ElementTree(file=item1.get('name')) + for item2 in filter(lambda it: it.get('name'), file2.getroot()): + file3 = ElementTree(file=item2.get('name')) + for item3 in file3.getroot(): + name = item3.get('name') + id_ = item3.get('id') + if name is not None and id_ is not None and int(id_) > 0: + result.append((id_, name)) + return result + + +def PrintHelp(): + print('Usage: {} <monsters.xml>'.format(sys.argv[0])) + +if __name__ == '__main__': + if len(sys.argv) == 1: + PrintHelp() + sys.exit(0) + filename = sys.argv[1] + if os.path.isfile(filename): + for id_, name in ScanItemsXML(filename): + print(id_, name) + else: + print('File not found:', filename, file=sys.stderr) + sys.exit(1) |