diff options
author | Livio Recchia <recchialivio@libero.it> | 2020-02-10 23:06:34 +0100 |
---|---|---|
committer | Livio Recchia <recchialivio@libero.it> | 2020-02-10 23:06:34 +0100 |
commit | 9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch) | |
tree | 9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /utils/generate-packet-lengths.py | |
parent | 11cc316b74d5f3f283413a33e7693b314741aa4a (diff) | |
download | manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2 manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip |
Initial commit
Diffstat (limited to 'utils/generate-packet-lengths.py')
-rw-r--r-- | utils/generate-packet-lengths.py | 54 |
1 files changed, 54 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) |