hxp
2021-02-20 71a5b8c236d7538053a3a893adccaa0d59798521
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python
# -*- coding: GBK -*-
# ----------------------------------------------------------------------------------------------------
#
# ----------------------------------------------------------------------------------------------------
##@package NetPackManager
# @todo: ·â°ü´¦Àí
#
# @author: zfl
# @date 2011-10-17 19:50
# @version 1.6
# ÐÞ¸Äʱ¼ä ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ
# @change: "2013-05-17 14:30" Alee ·â°ü³ö´í²»ÔÙÊÜÀí£¬ÐÞ¸´BUG
# @change: "2014-03-11 20:00" Alee ·â°ü´íÎó²»Å׳öÒì³£
# @change: "2014-05-16 13:30" xmnathan Ôö¼Ó GameServerºÍMapServerÖ®¼äµÄ×Ô¶¨Òå·â°ü
# @change: "2014-08-19 14:00" xmnathan GameServerºÍMapServerÖ®¼äµÄ×Ô¶¨Òå·â°ü »ñÈ¡MapID±ØÐëÓÃGetRealMapID½Ó¿Ú 
# @change: "2014-12-09 17:30" xmnathan ÐÞ¸ÄÍæ¼ÒM->S×Ô¶¨Òå·â°ü·¢ËÍ
# @change: "2014-12-19 19:30" hxp DebugÏ·â°ü´íÎóÅ׳öÒì³£
#-------------------------------------------------------------------------------
"""Version = 2014-12-19 19:30"""
#-------------------------------------------------------------------------------
#---µ¼Èë---
import IPY_GameWorld
import ChConfig
import GameWorld
import CommFunc
import ConfigParser
import ChPyNetPack
import ChPyNetSendPack
import traceback
import ChGameToMapPyPack
#-------------------------------------------------------------------------------
#---È«¾Ö±äÁ¿---
 
PY_NAME = "NetPackCommon"
g_rootPath = ChConfig.GetAppPath()
# ÊÕ°ü×Öµä
RecievePackDict = ChPyNetPack.ChNetPackDict
# ·¢°ü×Öµä
SendPackDict = ChPyNetSendPack.ChNetPackDict
 
# Py·â°ü×¢²áÐÅÏ¢
PyPackTable = {}
 
 
## Ìæ»»×Ö·û´®ASCIIÂë
#IsRelpace = False
#ReplaceChar = chr(0xFF)
 
#-------------------------------------------------------------------------------
#---PyרÓõ÷ÊÔÊä³öÐÅÏ¢
## PyרÓã¬Êä³öµ÷ÊÔÐÅÏ¢
#  @param msg ÏûÏ¢
#  @return ·µ»ØÖµÎÞÒâÒå
#  @remarks Õý³£Èռǵ÷ÊÔÊä³öÐÅÏ¢
def Log(msg):
    
    GameWorld.Log(PY_NAME + "->" + msg)
    return
 
 
## PyרÓã¬Êä³ö´íÎóÐÅÏ¢
#  @param msg ÏûÏ¢
#  @return ·µ»ØÖµÎÞÒâÒå
#  @remarks Õý³£Èռǵ÷ÊÔÊä³öÐÅÏ¢
def ErrLog(msg):
    
    GameWorld.ErrLog(PY_NAME + "->" + msg)
    return
 
#-------------------------------------------------------------------------------
## ½âÎöPy·â°ü×¢²áÐÅÏ¢
#  @param tableName ±íÃû
#  @return ½âÎö½á¹û
#  @remarks
def ReadPyPackTable(tableName):
            
    tDict = {}  
    
#    scriptPath = g_rootPath + "\\Script\\"
    curPath = g_rootPath + "%s.ini"%(tableName)
    config = ConfigParser.ConfigParser()
    config.read(curPath)
    
    for section in config.sections():
        if config.getint(section, "RegType") != 0:
            continue
        
        regCnt = config.getint(section, "RegisterPackCount")
        scriptName = config.get(section, "ScriptName")
 
        moudle = __import__(scriptName.split("\\")[-1].split(".")[0])
        reload(moudle)
#        Log("ReadPyPackTable: moudle: %s"%dir(moudle))
        
        for index in range(regCnt):
            if not config.has_option(section, "PacketCMD_%s"%(index + 1)):
                continue
            cmd = config.get(section, "PacketCMD_%s"%(index + 1))
            subCmd = config.get(section, "PacketSubCMD_%s"%(index + 1))
            callFunc = config.get(section, "PacketCallFunc_%s"%(index + 1))
            if not cmd or not subCmd or not callFunc:
                continue
            
            cmd = int(cmd, 16)
            subCmd = int(subCmd, 16)
#            Log("ReadPyPackTable: cmd = %s subCmd = %s"%(cmd, subCmd))
            evalStr = "moudle.%s"%callFunc
#            Log("ReadPyPackTable: %s"%evalStr)
            
            try:
                callFunc = eval(evalStr)
            except:
                ErrLog("ReadPyPackTable: Error--%s"%traceback.format_exc())
                continue
            
            if not callable(callFunc):
                ErrLog("ReadPyPackTable: callFunc = %s is not callable!"%callFunc)
                continue
            
            head = eval("0x%02x%02x"%(cmd, subCmd))
            tDict[head] = {"Head": head, "CallFunc": callFunc}
    
#    Log("ReadPyPackTable: tDict = %s"%tDict)
    return tDict
 
 
## »ñµÃPy·â°üÊý¾Ý
#  @param head °üÍ·
#  @return Py·â°ü
#  @remarks
def GetPyRecievePack(head):
    
    if not RecievePackDict.has_key(head):
        ErrLog("GetPyRecievePack: No PyRecievePack head = %s"%head)
        return None
    
    return RecievePackDict[head]
 
 
## »ñµÃPy·â°üÊý¾Ý
#  @param head °üÍ·
#  @return Py·â°ü
#  @remarks
def GetPySendPack(head):
    
    if not SendPackDict.has_key(head):
        ErrLog("GetPySendPack: No PySendPack head = %s"%head)
        return None
    
    return SendPackDict[head]
 
#-------------------------------------------------------------------------------
## ½ÓÊÕÍòÄÜ·â°ü
#  @param index Íæ¼ÒË÷Òý
#  @param tick Ê±¼ä´Á
#  @return ÎÞ·µ»ØÖµ
#  @remarks
def RecNetPack(index, tick):
    
    try:
    
    #    Log("½Óµ½Í¨Ó÷â°ü!!!! index = %s"%index)
        
        clientPack = IPY_GameWorld.IPY_CFakePack().GetMsg()
        
        #ûÓÐPY·â°üÍ·
        if len(clientPack) <= 1:
            return
        
        #Log("RevieveFakePack: clientPack = %s  %d"% (`[clientPack]`, IPY_GameWorld.IPY_CFakePack().GetMsgLen()))
        #Log("RevieveFakePack: %s  %d"%(type(clientPack), len(clientPack)))
        
        headData = clientPack[1] + clientPack[0]
        curPackHead = CommFunc.ReadWORD(headData, 0)[0]
        
    #    Log("RevieveFakePack: curPackHead = %s"%curPackHead)
        
        curPackData = RecievePackDict.get(curPackHead)
        if not curPackData:
            return
        
    #    if IsRelpace:
    #        clientPack = clientPack[:2] + clientPack[2:].replace(ReplaceChar, chr(0x0))
    #    Log("RevieveFakePack: after replace clientPack = %s"%[clientPack])
        
        # ³¤¶ÈУÑé
    #    if curPackData.GetLength() > len(clientPack):
    #        Log("PyPack Length Error! curLen = %s, normalLen = %s"%(len(clientPack), curPackData.GetLength()))
    #        return 
        
        curPackData.ReadData(clientPack)
        
        # ½Ø¶Ï×Ö·û´®
        for key in dir(curPackData):
            value = getattr(curPackData, key)
            if isinstance(value, str):
                setattr(curPackData, key, value.rstrip(chr(0x0)))
        
        # È¡´Ë°üÍ·×¢²áÐÅÏ¢
        curPackHeadRegDict = PyPackTable.get(curPackHead)
        # ÎÞ´Ë·â°ü×¢²áÐÅÏ¢
        if curPackHeadRegDict == None:
            #Log("RevieveFakePack: No Register curPackHead = %s"%curPackHead)
            return
        
        curPackHeadRegDict["CallFunc"](index, curPackData, tick)
        
    except Exception:
        GameWorld.RaiseException("!!!python×Ô¶¨Òå·â°ü½âÎöʧ°Ü\r\n%s" % traceback.format_exc())
        return 
    
    
## ·¢ËÍÍòÄÜ·â°ü
#  @param curPlayer Íæ¼ÒʵÀý
#  @param clientPack Òª°ü×°µÄ·â°ü
#  @return ÎÞ·µ»ØÖµ
#  @remarks
def SendFakePack(curPlayer, clientPack):
#    Log("SendFakePack: clientPack = %s"%[clientPack.GetBuffer()])
    innerPackData = clientPack.GetBuffer()
    if len(innerPackData) < clientPack.GetLength():
        Log("SendFakePack: clientPack Len = %s > %s"%(clientPack.GetLength(), len(innerPackData)))
    #curPlayer.SendFakePack(innerPackData, len(innerPackData))
    curPlayer.SendFakePack(clientPack.GetBuffer(), clientPack.GetLength())
    
 
#-------------------------------------------------------------------------------
#---Py·â°ü×¢²áÐÅÏ¢
PyPackTable = ReadPyPackTable("PyNetPack")
 
 
 
 
 
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#GameServer Py·â°ü×¢²áÐÅÏ¢
GameServerPyPackTable = {}
GameServerPyPackTable = ReadPyPackTable("GameServerPyPack")
# ÊÕ°ü×Öµä
RecGameServerPyPackDict = ChGameToMapPyPack.ChNetPackDict
#-------------------------------------------------------------------------------
## ½ÓÊÕGameServerPyPack
#  @param index Íæ¼ÒË÷Òý
#  @param tick Ê±¼ä´Á
#  @return ÎÞ·µ»ØÖµ
#  @remarks
def RecGamePyPack(index, tick):
    
    try:       
        gamePack = IPY_GameWorld.IPY_MGeneralPack().GetData()
        
        #ûÓÐPY·â°üÍ·
        if len(gamePack) <= 1:
            return        
        headData = gamePack[1] + gamePack[0]
        curPackHead = CommFunc.ReadWORD(headData, 0)[0]
        #Log("RecGamePyPack: curPackHead = %s"%curPackHead)
        curPackData = RecGameServerPyPackDict.get(curPackHead)
        if not curPackData:
            #Log("RecGamePyPack: not curPackData curPackHead = %s"%curPackHead)
            return
        
        curPackData.ReadData(gamePack)
        
        # ½Ø¶Ï×Ö·û´®
        for key in dir(curPackData):
            value = getattr(curPackData, key)
            if isinstance(value, str):
                setattr(curPackData, key, value.rstrip(chr(0x0)))
        
        # È¡´Ë°üÍ·×¢²áÐÅÏ¢
        curPackHeadRegDict = GameServerPyPackTable.get(curPackHead)
        # ÎÞ´Ë·â°ü×¢²áÐÅÏ¢
        if curPackHeadRegDict == None:
            return
        
        curPackHeadRegDict["CallFunc"](curPackData, tick)
        #Log("RecGamePyPack: curPackHead = %s"%curPackHead)
    except Exception:
        GameWorld.RaiseException("RecGamePyPack python×Ô¶¨Òå·â°ü½âÎöʧ°Ü\r\n%s" % traceback.format_exc())
        return 
#-------------------------------------------------------------------------------
##·¢ËÍMapµ½GameServerµÄ¹µÍ¨°ü
#
def SendPyPackToGameServer(sendPack):
    routeIndex = GameWorld.GetGameWorld().GetLineNO()
    #ÕâÀï±ØÐëÓÃGetRealMapID,GetMapID Êµ¼ÊÈ¡µ½µÄÊÇDataMapID£¬ÔÚÓзÖÏߵĵØÍ¼»áÎÊÌâ
    mapID = GameWorld.GetGameWorld().GetRealMapID()  
    #ÏÈдÈ뵱ǰµØÍ¼±êʶ£¬·½±ãGameServer»Ø°üʱ¿ÉÒÔÕýȷ֪ͨµ½¶ÔÓ¦µØÍ¼
    data = ''
    data = CommFunc.WriteBYTE(data, routeIndex)         #1
    data = CommFunc.WriteDWORD(data, mapID)             #4
    data = CommFunc.WriteString(data, sendPack.GetLength(), sendPack.GetBuffer())
    
    dataLen = 1 + 4 + sendPack.GetLength()
    
    GameWorld.GetGameWorld().SendGameServerGeneralPack(dataLen, data)