| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| # ----------------------------------------------------------------------------------------------------  | 
| #  | 
| # ----------------------------------------------------------------------------------------------------  | 
| ##@package NetPackManager  | 
| # @todo: ·â°ü´¦Àí  | 
| #  | 
| # @author: zfl  | 
| # @date 2011-10-17 19:50  | 
| # @version 1.5  | 
| #   | 
| # ÐÞ¸Äʱ¼ä ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ  | 
| # @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-12-23 15:30" xmnathan GameServerÔö¼Ó¸ù¾ÝLineNO·¢ËÍPy×Ô¶¨Òå°ü½Ó¿Ú  | 
| # @change: "2015-10-22 23:00" hxp Ôö¼ÓRecv_MergerChildToCenter  | 
| #-------------------------------------------------------------------------------  | 
| #"""Version = 2015-10-22 23:00"""  | 
| #-------------------------------------------------------------------------------  | 
| #---µ¼Èë---  | 
| import IPY_GameServer  | 
| import ChConfig  | 
| import GameWorld  | 
| import CommFunc  | 
| import ConfigParser  | 
| import ChPyNetPack  | 
| import ChPyNetSendPack  | 
| import traceback  | 
| import ChMapToGamePyPack  | 
| import CrossRealmMsg  | 
| import PlayerControl  | 
| #-------------------------------------------------------------------------------  | 
| #---È«¾Ö±äÁ¿---  | 
|   | 
| 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.get(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_GameServer.IPY_CGameServerGeneralPack().GetData()  | 
|           | 
|         #ûÓÐPY·â°üÍ·  | 
|         if len(clientPack) <= 1:  | 
|             return  | 
|           | 
|         #Log("RevieveFakePack: clientPack = %s  %d"% (`[clientPack]`, IPY_GameServer.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()  | 
|     curPlayer.Sync_GeneralPack(len(innerPackData), innerPackData)  | 
|       | 
| def SendPackByPlayer(clientPack, curPlayer=None):  | 
|     ## ·¢ËÍ·â°ü£¬µ±curPlayerΪNoneʱÔò·¢ËÍÈ«·þÍæ¼Ò  | 
|     if not curPlayer:  | 
|         # È«·þ¹ã²¥ÔÚÏßÍæ¼Ò  | 
|         playerManager = GameWorld.GetPlayerManager()  | 
|         for i in xrange(playerManager.GetActivePlayerCount()):  | 
|             curPlayer = playerManager.GetActivePlayerAt(i)  | 
|             if curPlayer == None:  | 
|                 continue  | 
|             if PlayerControl.GetIsTJG(curPlayer):  | 
|                 continue  | 
|             SendFakePack(curPlayer, clientPack)  | 
|     else:  | 
|         if PlayerControl.GetIsTJG(curPlayer):  | 
|             return  | 
|         SendFakePack(curPlayer, clientPack)  | 
|     return  | 
|   | 
| #-------------------------------------------------------------------------------  | 
| #---Py·â°ü×¢²áÐÅÏ¢  | 
| PyPackTable = ReadPyPackTable("PyNetPack")  | 
|   | 
|   | 
| #-------------------------------------------------------------------------------  | 
| #-------------------------------------------------------------------------------  | 
| #MapServer Py·â°ü×¢²áÐÅÏ¢  | 
| MapServerPyPackTable = {}  | 
| MapServerPyPackTable = ReadPyPackTable("MapServerPyPack")  | 
| # ÊÕ°ü×Öµä  | 
| RecMapServerPyPackDict = ChMapToGamePyPack.ChNetPackDict  | 
| ## ½ÓÊÕMapServerPyPack  | 
| #  @param index Íæ¼ÒË÷Òý  | 
| #  @param tick Ê±¼ä´Á  | 
| #  @return ÎÞ·µ»ØÖµ  | 
| #  @remarks  | 
| def RecvMapPyPack(index, tick):  | 
|       | 
|     try:         | 
|         mapPack = IPY_GameServer.IPY_GGeneralPack().GetData()  | 
|         #ûÓÐPY·â°üÍ·  | 
|         if len(mapPack) < 1 + 4 + 1 + 1:#BYTE + DWORD + tagHead(BYTE + BYTE)  | 
|             return  | 
|         pos = 0  | 
|         routeIndex, pos = CommFunc.ReadBYTE(mapPack, pos)  | 
|         mapID, pos = CommFunc.ReadDWORD(mapPack, pos)  | 
|                   | 
|         headData = mapPack[pos + 1] + mapPack[pos]  | 
|         curPackHead = CommFunc.ReadWORD(headData, 0)[0]  | 
|           | 
|         #Log("RecvMapPyPack: curPackHead = %s"%curPackHead)  | 
|         curPackData = RecMapServerPyPackDict.get(curPackHead)  | 
|           | 
|         if not curPackData:  | 
|             #Log("RecvMapPyPack: not curPackData curPackHead = %s"%curPackHead)  | 
|             return  | 
|           | 
|         curPackData.ReadData(mapPack, pos)  | 
|           | 
|         # ½Ø¶Ï×Ö·û´®  | 
|         for key in dir(curPackData):  | 
|             value = getattr(curPackData, key)  | 
|             if isinstance(value, str):  | 
|                 setattr(curPackData, key, value.rstrip(chr(0x0)))  | 
|           | 
|         # È¡´Ë°üÍ·×¢²áÐÅÏ¢  | 
|         curPackHeadRegDict = MapServerPyPackTable.get(curPackHead)  | 
|         # ÎÞ´Ë·â°ü×¢²áÐÅÏ¢  | 
|         if curPackHeadRegDict == None:  | 
|           | 
|             return  | 
|           | 
|         curPackHeadRegDict["CallFunc"](routeIndex, mapID, curPackData, tick)  | 
|         #Log("RecvMapPyPack: CallFunc curPackHead = %s"%curPackHead)  | 
|     except Exception:  | 
|         GameWorld.RaiseException("RecvMapPyPack python×Ô¶¨Òå·â°ü½âÎöʧ°Ü\r\n%s" % traceback.format_exc())  | 
|         return   | 
|       | 
| #-------------------------------------------------------------------------------  | 
| ##·¢ËÍGameµ½MapServerµÄ¹µÍ¨°ü  | 
| #  | 
| def SendPyPackToMapServer(LineNO, mapID, sendPack):  | 
|       | 
|     GameWorld.GetGameWorld().SendMapServerGeneralPackByLineNO(LineNO, mapID, sendPack.GetLength(), sendPack.GetBuffer())  | 
|   | 
| #-------------------------------------------------------------------------------  | 
| ## ½ÓÊÕtagMGMergerChildToCenter  | 
| #  @param index ÎÞЧ  | 
| #  @param tick Ê±¼ä´Á  | 
| #  @return ÎÞ·µ»ØÖµ  | 
| #  @remarks  | 
| def Recv_MergerChildToCenter(index, tick):  | 
|     try:  | 
|         packData = IPY_GameServer.IPY_MGMergerChildToCenter().GetData()  | 
|         # ²»ÒªÊä³ö¾ßÌå·â°üÄÚÈÝ£¬¿ÉÄÜ»áÒòΪÄÚÈݵ¼ÖÂÈÕÖ¾Êä³ö±¨´í  | 
|         Log("ÊÕµ½¿ç·þ×Ó·þ×Ô¶¨Òå·â°üÏûÏ¢£º³¤¶È=%s" % len(packData))  | 
|         #ÒÔÏÂÌí¼ÓºóÐø´¦Àíº¯Êý  | 
|         #...  | 
|         #...  | 
|         CrossRealmMsg.OnCrossServerReceiveMsg(packData, tick)  | 
|     except Exception:  | 
|         Log("¿ç·þ×Ó·þ×Ô¶¨Òå·â°üÏûÏ¢´¦Àíʧ°Ü")  | 
|     return  | 
|   |