summaryrefslogtreecommitdiff
path: root/servergreps/hercules/src/utils.py
blob: de42a36db9fc5c40ccd39d72d02b9ba5c7c9a83e (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
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/env python2
# -*- coding: utf8 -*-
#
# Copyright (C) 2015-2016  Evol Online
# Author: Andrei Karas (4144)

import re

class Utils:
    casere = re.compile("^case 0x(?P<packet>[0-9a-fA-F]+)[:]")
    charParseFunctionre = re.compile(
        "(?P<function>chr->[0-9a-zA-Z_>-]+)([(]|[ ][(])");
    ourPacketre = re.compile(
        "(WFIFOW|WBUFW)([ ]*)[(]([ ]*)([\w>_-]+),([ ]*)" +
        "(?P<offset>0)([ ]*)[)]([ ]*)=([ ]*)0x(?P<packet>[0-9a-fA-F]+)([ ]*)[;]")
    ourPacketre2 = re.compile("PacketType([ ]*)=([ ]*)(?P<name>[\w_]+);")
    ourPacketre3 = re.compile(
        "(WFIFOW|WBUFW)([ ]*)[(]([ ]*)([\w>_-]+),([ ]*)" +
        "(?P<offset>0)([ ]*)[)]([ ]*)=([ ]*)(?P<packet>[0-9\w]+)([ ]*)[;]")
    ourPacketre4 = re.compile(" cmd([ ]*)=([ ]*)0x(?P<packet>[0-9a-fA-F]+);")
    ourPacketre5 = re.compile(
        "(WFIFOW|WBUFW)([ ]*)[(]([ ]*)([\w>_-]+),([ ]*)" +
        "(count[*]p_len)([ ]*)[)]([ ]*)=([ ]*)(?P<packet>[0-9\w]+)([ ]*)[;]")
    ourPacketre6 = re.compile("int cmde([ ]*)=([ ]*)0x(?P<packet>[0-9a-fA-F]+);")
    ourPacketre7 = re.compile(" (packet|packet_num|PacketType|packet_type)([ ]*)=([ ]*)0x(?P<packet>[0-9a-fA-F]+);")

    @staticmethod
    def enumCasePackets(fileName, startCode):
        startCode = startCode + "\n"
        endCode = "}\n"
        breakCode = "break;"
        with open(fileName, "r") as f:
            for line in f:
                if line == startCode:
                    packets = []
                    for line in f:
                        line = line.strip()
                        m = Utils.casere.search(line)
                        if m is not None:
                            data = m.group("packet").lower()
                            while len(data) < 4:
                                data = "0" + data
                            if int(data, 16) < 4096:
                                packets.append(data)
                        if line == breakCode:
                            packets = []
                        if line == endCode:
                            break
                        if len(packets) > 0:
                            m = Utils.charParseFunctionre.search(line)
                            if m is not None:
                                func = m.group("function")
                                if len(packets) > 1:
                                    for packet in packets:
                                        fname = func + "_" + str(packet)
                                        yield (fname, packet)
                                else:
                                    yield (func, packets[0])
                    break


    @staticmethod
    def getOutPackets(line, server):
        packets = []
        m = Utils.ourPacketre4.findall(line)
        if len(m) > 0:
            for str in m:
                data = str[2]
                while len(data) < 4:
                    data = "0" + data
                server.addServerPacket(data)
        m = Utils.ourPacketre5.findall(line)
        if len(m) > 0:
            for str in m:
                data = str[9]
                while len(data) < 4:
                    data = "0" + data
                server.addServerPacket(data)
        m = Utils.ourPacketre.findall(line)
        if len(m) == 0:
            m = Utils.ourPacketre3.findall(line)
        if len(m) > 0:
            for str in m:
                if str[9] == "0":
                    continue
                data = str[9]
                if data == "cmd":
                    continue
                while len(data) < 4:
                    data = "0" + data
                server.addServerPacket(data)
        m = Utils.ourPacketre2.findall(line)
        if len(m) > 0:
            for str in m:
                if str[2] == "0":
                    continue
                data = str[2]
                if len(data) > 2 and data[0:2] == "0x":
                    data = data[2:]
                while len(data) < 4:
                    data = "0" + data
                server.addServerPacket(data)
        m = Utils.ourPacketre6.findall(line)
        if len(m) > 0:
            for str in m:
                data = str[2]
                while len(data) < 4:
                    data = "0" + data
                server.addServerPacket(data)
        m = Utils.ourPacketre7.findall(line)
        if len(m) > 0:
            for str in m:
                data = str[3]
                while len(data) < 4:
                    data = "0" + data
                server.addServerPacket(data)