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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
# -*- coding: GBK -*-
# µ¼³öPY°æ±¾ÓõĠIpyGameDataPY.py
import ConfigParser
import sys
import os
import re
 
cfg = ConfigParser.ConfigParser()
cfg.read("config.ini")
CodeProjectPath = "" # ´úÂëÏîĿ·¾¶
for i in range(1, 20):
    folderPath = cfg.get("config", "CodeProjectPath%s" % i)
    if os.path.isdir(folderPath):
        CodeProjectPath = folderPath
        break
IpyGameDataFile = "IpyGameDataPY.py" # Éú³ÉµÄpyÎļþÃû
 
# ±í½á¹¹¶¨Òå×ÖµäÄ£°å
TableDefKey = "                \"%s\":(%s"
TableDefValue = "                        (\"%s\", \"%s\", %s),%s"
TableDefEnd = "                        ),%s"
 
# Àà½á¹¹Ä£°å
ClassTemp = '''
# %s
class IPY_%s():
    
    def __init__(self):
%s
        return
        
%s
'''
# Àà³õʼ»¯ÊýÖµ¶ÔÏóÄ£°å
ClassInitTemp = "        self.%s = %s%s"
# »ñÈ¡Àà¶ÔÏóº¯ÊýÄ£°å
ClassFuncTemp = "    def Get%s(self): return self.%s%s %s%s"
 
# ¹ÜÀíÆ÷³õʼ»¯±íÊý¾Ý»º´æÁбí¶ÔÏóÄ£°å
MgrTableCacheInit = "        self.__LoadFileData(\"%s\", onlyCheck)%s"
# ¹ÜÀíÆ÷»ñÈ¡±íÊý¾ÝÌõÊý¡¢Êý¾Ýº¯ÊýÄ£°å
MgrGetCountFunc = '''
    def Get%sCount(self):
        self.CheckLoadData("%s")
        return self.ipy%sLen\r
    def Get%sByIndex(self, index):
        self.CheckLoadData("%s")
        return self.ipy%sCache[index]\r
'''
 
Def_RN = "\r\n"
 
def DoCreateIpyGameDataPY():
    print "================================================="
    print "Number of arguments:", len(sys.argv), "arguments."
    StructFileName = sys.argv[1]
    if StructFileName == "PySysDBPY.h":
        IpyGameDataPYFile = CodeProjectPath + "\\ZoneServerGroup\\map1_8G\\MapServer\\MapServerData\\Script\\"
        LoadStructPath = "ChConfig.GetServerConfigPath()"
    elif StructFileName == "PySysDBG.h":
        IpyGameDataPYFile = CodeProjectPath + "\\CoreServerGroup\\GameServer\\Script\\"
        LoadStructPath = "ChConfig.GetAppPath()"
    else:
        print "²»Ö§³Ö¸Ã±í½á¹¹Îļþ!%s" % StructFileName
        raise
    IpyGameDataPYFile += IpyGameDataFile
    
    # ¶ÁÈ¡±í½á¹¹¶¨Òå
    print "¼ÓÔØÅäÖñí½á¹¹: %s" % StructFileName
    fileObj = open("../%s" % StructFileName, 'rb')
    content = fileObj.read()
    fileObj.close()
    
    # ½âÎö±í½á¹¹¶¨Òå
    Def_FuncConfigName = "FuncConfig" # ¹¦ÄÜÅäÖñí, ´Ë±íÌØÊâ´¦Àí
    TableNameList = [] # È·±£±í½á¹¹µ¼³ö˳ÐòÒ»ÖÂ, ·ÀÖ¹°æ±¾¶Ô±È»ìÂÒ
    Def_IpyTable = {}
    Def_FieldTypeList = ["DWORD", "WORD", "BYTE", "float", "char", "dict", "list", "eval"]
    structList = re.findall("\/\/.*?struct.*?\{.*?\};", content, re.S)
    for structStr in structList:
        lineList = structStr.split(Def_RN)
        
        tableInfoStr = lineList[0]
        if "#tag" in tableInfoStr:
            tagIndex = tableInfoStr.index("#tag")
            tableNameCh = tableInfoStr[2:tagIndex].replace(" ", "") # ±íÃûÖÐÎÄÃû
        else:
            tableNameCh = tableInfoStr[2:].replace(" ", "") # ±íÃûÖÐÎÄÃû
        tableName = "" # ±íÃûÓ¢ÎÄÃû
        fieldInfoList = []
        for line in lineList[1:]:
            if not line:
                continue
            
            if "struct" in line:
                line = line.strip()
                if "{" in line:
                    tableName = line[6:line.index("{")]
                else:
                    tableName = line[6:]
                tableName = tableName.replace("\t", "").replace(" ", "") # ±íÃûÓ¢ÎÄÃû
                if tableName.startswith("tag"):
                    tableName = tableName[3:]
                continue
            
            noteInfo = " #" # ×Ö¶Î×¢ÊÍ˵Ã÷
            if "//" in line:
                signIndex = line.index("//")
                noteInfo = " # %s" % line[signIndex+2:].rstrip()
                line = line[:signIndex]
            fieldType = ""
            for ft in Def_FieldTypeList:
                if ft in line:
                    fieldType = ft
                    break
            if not fieldType:
                if "struct" not in line and "{" not in line and "}" not in line:
                    print "### ÀàÐͶ¨Òå´íÎó±í ###"
                    print "struct %s" % tableName
                    print line
                    raise
                continue
            nameIndex = line.index(fieldType) + len(fieldType)
            if "[" in line:
                endIndex = line.index("[")
            elif ";" in line:
                endIndex = line.index(";")
            else:
                continue
            
            isIndex = 0 # ÊÇ·ñËÑË÷Ë÷Òý
            fieldName = line[nameIndex:endIndex].replace(" ", "").replace("\t", "")
            if fieldName.startswith("_"):
                fieldName = fieldName[1:]
                isIndex = 1
            fieldInfoList.append([fieldType, fieldName, isIndex, noteInfo])
        Def_IpyTable[tableName] = [tableNameCh, fieldInfoList]
        TableNameList.append(tableName)
        
    # Ä£°åËùÐèÌæ»»µÄÄÚÈݲÎÊý
    ipyTableDef = "" # ±í½á¹¹¶¨Òå
    classContent = "" # ±í¶ÔÓ¦µÄÀà
    mgrTableCacheObjInit = "" # ±íÊý¾Ý¶ÔÏ󻺴æ
    mgrTableFunc = "" # ±íÊý¾Ý¶ÔÏó»ñÈ¡º¯Êý
    
    for i, tableName in enumerate(TableNameList):
        tableNameCh, fieldInfoList = Def_IpyTable[tableName]
        classInitInfo = ""
        classFuncInfo = ""
        
        ipyTableDef += TableDefKey % (tableName, Def_RN)
        
        for j, fieldInfo in enumerate(fieldInfoList):
            lineEnd = "" if j == len(fieldInfoList) - 1 else Def_RN
            fieldType, fieldName, isIndex, noteInfo = fieldInfo
            ipyTableDef += TableDefValue % (fieldType, fieldName, isIndex, Def_RN)
            if fieldType == "char":
                classInitInfo += ClassInitTemp % (fieldName, "\"\"", lineEnd)
            elif fieldType == "dict":
                classInitInfo += ClassInitTemp % (fieldName, "{}", lineEnd)
            elif fieldType == "list":
                classInitInfo += ClassInitTemp % (fieldName, "[]", lineEnd)
            elif fieldType == "float":
                classInitInfo += ClassInitTemp % (fieldName, "0.0", lineEnd)
            else:
                classInitInfo += ClassInitTemp % (fieldName, "0", lineEnd)
            callAttrValue = "attrTuple[%s]" % j # fieldName
            classFuncInfo += ClassFuncTemp % (fieldName, callAttrValue, noteInfo, fieldType, lineEnd)
            
        ipyTableDef += TableDefEnd % (Def_RN if i == len(Def_IpyTable) - 1 else (Def_RN * 2))
        classInitInfo = ClassInitTemp % ("attrTuple", "None", "") # ÓÅ»¯Äڴ棬²»Ê¹ÓÃÄÚÖà__dict__ ·ÃÎÊÊôÐÔ£¬¸ÄΪʹÓÃtuple´ævalue
        classContent += ClassTemp % (tableNameCh, tableName, classInitInfo, classFuncInfo)
        
        # ±íÁÐ±í¡¢³¤¶È¶ÔÏ󻺴æ
        mgrTableCacheObjInit += MgrTableCacheInit % (tableName, Def_RN)
        
        # ±íÁÐ±í¡¢³¤¶È»ñÈ¡º¯Êý
        mgrTableFunc += MgrGetCountFunc % (tableName, tableName, tableName, tableName, tableName, tableName)
    
    # ¶Áȡģ°åÉú³Épy´úÂë
    createPYContent = ""
    template = open("IpyGameDataPYTemp.py", 'rb')
    for line in template:
        if "#<%Table_Def%>" in line:
            line = ipyTableDef
        elif "#<%Table_Class%>" in line:
            line = classContent
        elif "#<%Ipy_Cache_Init%>" in line:
            line = mgrTableCacheObjInit
        elif "\"<%LoadStructPath%>\"" in line:
            line = line.replace("\"<%LoadStructPath%>\"", LoadStructPath)
        elif "#<%Ipy_Cache_Func%>" in line:
            line = mgrTableFunc
        createPYContent += line
    template.close()
    
    pyFile = open(IpyGameDataPYFile, "wb")
    pyFile.write("%s" % createPYContent)
    pyFile.close()
    print "³É¹¦Éú³É: %s" % IpyGameDataFile
    print "µ¼³ö·¾¶: %s" % IpyGameDataPYFile
    return
 
DoCreateIpyGameDataPY()