| #!/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:  | 
|         Log("!!!python×Ô¶¨Òå·â°ü½âÎöʧ°Ü~~~~~\r\n%s" % traceback.format_exc())  | 
|         if GameWorld.GetGameWorld().GetDebugLevel():  | 
|             raise Exception("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:  | 
|         Log("RecGamePyPack python×Ô¶¨Òå·â°ü½âÎöʧ°Ü~~~~~\r\n%s" % traceback.format_exc())  | 
|         if GameWorld.GetGameWorld().GetDebugLevel():  | 
|             raise Exception("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)  | 
|       | 
|       | 
|       |