summaryrefslogtreecommitdiff
path: root/servergreps/hercules/src/manaplus.py
blob: 56fd11ce50d1911a28f99180091a2381fe35922c (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
#! /usr/bin/env python2
# -*- coding: utf8 -*-
#
# Copyright (C) 2015-2016  Evol Online
# Author: Andrei Karas (4144)

import os
import re

class ManaPlus:
    inPackets = dict()
    outPackets = dict()
    sizes = dict()
    manaplusUsedPacketsSet = set()
    outMsgNameToId = dict()
    protocolinre = re.compile(
        "packet[(](?P<name>[A-Z0-9_]+),([ ]*)0x(?P<packet>[0-9a-fA-F]+)" +
        ",([ ]*)(?P<len>[\w-]+),([ ]*)")
    protocolinverre = re.compile("^// (?P<ver>[0-9]+)$")
    protocoloutre = re.compile(
        "packet[(](?P<name>CMSG_[A-Z0-9_]+),([ ]*)0x(?P<packet>[0-9a-fA-F]+)," +
        "([ ]*)(?P<len>[\w-]+),([ ]*)(?P<function>[0-9a-zA-Z_>-]+)[)];")
    packetOutNametre = re.compile("(?P<name>(S|C)MSG_[A-Z0-9_]+)")

    def collectInPackets(self, fileName, packetVersion):
        version = 0
        with open(fileName, "r") as f:
            for line in f:
                m = self.protocolinverre.search(line)
                if m is not None:
                    version = int(m.group("ver"))
                    continue
                # skip bigger versions than requested
                if version > packetVersion:
                    continue
                m = self.protocolinre.search(line)
                if m is not None:
                    packet = int(m.group("packet"), 16)
                    if packet > 0xb00 or packet == 0:
                        continue
                    self.inPackets[m.group("packet").lower()] = \
                        (m.group("name"), int(m.group("len")), "nullptr")
                    self.sizes[m.group("packet").lower()] = m.group("len")


    def collectOutPackets(self, fileName, packetVersion):
        version = 0
        with open(fileName, "r") as f:
            for line in f:
                m = self.protocolinverre.search(line)
                if m is not None:
                    version = int(m.group("ver"))
                    continue
                # skip bigger versions than requested
                if version > packetVersion:
                    continue
                m = self.protocoloutre.search(line)
                if m is not None:
                    packet = int(m.group("packet"), 16)
                    if packet > 0xb00 or packet == 0:
                        continue
                    self.inPackets[m.group("packet").lower()] = \
                        (m.group("name"), int(m.group("len")), m.group("function"))
                    self.outPackets[m.group("packet").lower()] = \
                        (m.group("name"), int(m.group("len")), m.group("function"))
                    self.outMsgNameToId[m.group("name").strip()] = \
                        m.group("packet").lower()
                    # print "{0} = {1}".format(
                    #    m.group("name").strip(), m.group("packet").lower())


    def processCppFiles(self, parentDir):
        files = os.listdir(parentDir)
        for file1 in files:
            if file1[0] == ".":
                continue
            file2 = os.path.abspath(parentDir + os.path.sep + file1)
            if not os.path.isfile(file2):
                self.processCppFiles(file2)
            elif file1[-4:] == ".cpp":
                self.collectManaPlusUsedPackets(file2)


    def collectManaPlusUsedPackets(self, fileName):
        with open(fileName, "r") as f:
            for line in f:
                m = self.packetOutNametre.search(line)
                if m is not None:
                    self.manaplusUsedPacketsSet.add(m.group("name"))

    def processPackets(self, packetVersion):
        manaplusPath = "../../../manaplus/src/"
        protocolPath = manaplusPath + "net/eathena/packets"
        eathenaPath = manaplusPath + "net/eathena/"
        self.collectInPackets(protocolPath + "in.inc", int(packetVersion))
        self.collectOutPackets(protocolPath + "out.inc", int(packetVersion))
        self.processCppFiles(eathenaPath)