summaryrefslogtreecommitdiff
path: root/herculesversion/packetversion.py
blob: e75db5706bb21323b3183df43f01a2badff64512 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env python
# -*- coding: utf8 -*-
#
# Copyright (C) 2014  Evol Online
# Author: Andrei Karas (4144)

import os
import re
import sys

defStart = "#if PACKETVER >= "
defEnd = "#endif"
packetStart = "packet(";
packetRe = re.compile("\tpacket[(](?P<id>([^,]+)),(?P<size>([^,])+),(?P<func>([^,]+))")

def makeDir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def saveFile(fileName, data):
    with open(fileName, "w") as w:
        w.write(data)

def parsePacket(line):
    m = packetRe.search(line)
    if m is not None:
        return (m.group("id").lower(), int(m.group("size")), m.group("func"))
    return None

def readPackets(path, oldVersion, newVersion):
    oldPackets = dict()
    newPackets = dict()
    with open(path, "r") as f:
        searchState = 0
        newBlock = False
        for line in f:
            if searchState == 0: # search for #if PACKETVER
                if line.find(defStart) == 0:
                    ver = line[len(defStart):]
                    idx = ver.find("//");
                    if idx > 0:
                        ver = ver[:idx]
                    ver = int(ver)
                    if ver <= newVersion:
                        if ver > oldVersion:
                            newBlock = True
                        else:
                            newBlock = False
                        searchState = 1
            elif searchState == 1: # read block body
                if line.find(packetStart) == 1:
                    data = parsePacket(line)
                    if data is not None:
                        if newBlock == True:
                            newPackets[data[0]] = data
                        else:
                            oldPackets[data[0]] = data
                elif line.find(defEnd) == 0:
                    searchState = 0
    return (oldPackets, newPackets)

def findChangedPackets(data):
    old = data[0]
    new = data[1]
    ret = dict()
    for line in old.iteritems():
        if line[0] in new:
            ret[line[0]] = line[1]
    return (ret, new)

def showPlan(data):
    oldFunc = dict()
    newFunc = dict()

    for line in data[0].iteritems():
        if line[1][2] not in oldFunc:
            oldFunc[line[1][2]] = []
        oldFunc[line[1][2]].append(line[1])

    for line in data[1].iteritems():
        if line[1][2] not in newFunc:
            newFunc[line[1][2]] = []
        newFunc[line[1][2]].append(line[1])

    for line in oldFunc:
        if line in oldFunc and line in newFunc:
            for line2 in oldFunc[line]:
                id1 = line2[0]
                id2 = newFunc[line][0][0]
                if id1 != id2:
                    print "{0:30} {1:4}  ->  {2:4}".format(line, id1, id2)

def showHelp():
    print "Show difference between packet versions."
    print "Usage: {0} old new".format(sys.argv[0])

def main():
    if len(sys.argv) != 3:
        showHelp();
        exit();

    data = readPackets("hercules/src/map/packets.h", int(sys.argv[1]), int(sys.argv[2]))
    changed = findChangedPackets(data)
    showPlan(changed)

main()