| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #---------------------------------------------------------------------  | 
| #  | 
| #---------------------------------------------------------------------  | 
| ##@package ReadChConfig  | 
| # @todo: ²ß»®txt¶Á±í¹ÜÀíÆ÷  | 
| #  | 
| # @author: panwei  | 
| # @date 2010-04-30  | 
| # @version 1.4  | 
| #  | 
| # @note: ´Ëº¯Êý¶ÁÈ¡¸ùĿ¼ÏµÄGameServerConfigÎļþÖеÄËùÓÐ.txtÎļþ  | 
| #  | 
| # @change: "2013-06-27 14:30" wdb ÐÞ¸Äevalºó¼ÓÈ뻺´æ  | 
| # @change: "2013-07-31 18:00" Alee Í¬²½Mapserver  | 
| # @change: "2015-01-14 00:30" hxp PyMongoDataServer.iniÅäÖöÁÈ¡  | 
| # @change: "2017-07-03 20:00" hxp DB·¾¶ÏÂÅäÖÃÎļþ¶Áȡ֧³Ö  | 
| #---------------------------------------------------------------------  | 
| #"""Version = 2017-07-03 20:00"""  | 
| #---------------------------------------------------------------------  | 
| import ChConfig  | 
| import os  | 
| import GameWorld  | 
| import md5  | 
| import FormulaControl  | 
| import ShareDefine  | 
| import GameConfig  | 
| import ConfigParser  | 
| #import ReloadModule  | 
| import IpyGameDataPY  | 
| #---------------------------------------------------------------------  | 
| #È«¾Ö±äÁ¿  | 
| #---------------------------------------------------------------------  | 
|   | 
| #---------------------------------------------------------------------  | 
| #Â߼ʵÏÖ  | 
|   | 
| AllChConfig = {}  | 
| GeneralProgramme = {}  | 
| PyMongoDataServerConfig = None  | 
| DBAllChConfig = {}  | 
|   | 
| # db·¾¶Ï¶ÔÓ¦µÄÅäÖÃÎļþ·¾¶ {key:path, ...}  | 
| Def_DBCfgFilePath = {  | 
|                      "DBPlatformNum":"\\Config\\DBPlatformNum.txt",  | 
|                      }  | 
|   | 
| #---------------------------------------------------------------------  | 
| ## »ñÈ¡ÔÑùµÄConfig´Ë·½·¨½öÓÃÓÚ¹«Ê½ !!!!  | 
| #  @param key ±íÃû  | 
| #  @return None  | 
| #  @remarks º¯ÊýÏêϸ˵Ã÷.  | 
| def GetChConfig(key):  | 
|     return __DoLogic_GetConfig(key)  | 
|   | 
| #---------------------------------------------------------------------  | 
| ## ¶ÔÍâ½Ó¿Ú(»ñÈ¡evalºóµÄConfig),´Ë·½·¨ÓÃÓÚµ¥Ò»ÁбíµÈ,²»ÄÜÓù«Ê½!!!!  | 
| #  @param key ±íÃû  | 
| #  @return None  | 
| #  @remarks º¯ÊýÏêϸ˵Ã÷.  | 
| def GetEvalChConfig(key):      | 
|     return __DoLogic_GetConfig(key, True)  | 
|   | 
| #---------------------------------------------------------------------  | 
| ## Í¨¹ýkey²éÕҲ߻®±í£¬Èç¹û»¹Î´¼ÓÔØ£¬Ôò½øÐмÓÔØ  | 
| #  @param key ±íÃû  | 
| #  @return None  | 
| #  @remarks º¯ÊýÏêϸ˵Ã÷.  | 
| def __DoLogic_GetConfig(key, needEval=False):  | 
|     global AllChConfig  | 
|     global GeneralProgramme  | 
|       | 
|     #ÊÇ·ñÖØ¶ÁËùÓеÄÅäÖÃ±í  | 
|     if GameWorld.GetGameWorld().GetDictByKey(ShareDefine.Def_Notify_WorldKey_ReloadConfig):  | 
|         ClearAllConfig()  | 
|     else:  | 
|         #ÕÒµ½ÁË,¾Í·µ»Ø  | 
|         if AllChConfig.has_key(key):  | 
|             return AllChConfig[key]  | 
|           | 
|     if not GeneralProgramme:  | 
|         #GameWorld.Log('×Ô¶¯ÖضÁ×ܸÖ')  | 
|         ReadConfigData_GeneralProgramme()  | 
|       | 
|     #»ñȡʧ°Ü, ×Ô¶¯ÖضÁÅäÖÃÐÅÏ¢  | 
|     #GameWorld.Log( '×Ô¶¯ÖضÁÅäÖÃÐÅÏ¢Key = %s'%(key) )  | 
|     #ÖØ¶ÁÅäÖà  | 
|     ReadConfigData(key, needEval)  | 
|     #ÕÒµ½ÁË,¾Í·µ»Ø  | 
|     if AllChConfig.has_key(key):  | 
|         return AllChConfig[key]  | 
|       | 
|     GameWorld.Log('ÎÞ·¨»ñÈ¡ÅäÖÃÐÅÏ¢Key = %s, Å׳ö'%(key))  | 
|     raise Exception('ÎÞ·¨»ñÈ¡ÅäÖÃÐÅÏ¢Key = %s, Å׳ö'%(key))  | 
|       | 
|     return  | 
|   | 
|   | 
| ## ÖضÁ£¬Çå³ýÅäÖà  | 
| #  @param key ±íÃû  | 
| #  @return None  | 
| def ClearAllConfig():  | 
|     global AllChConfig  | 
|     global GeneralProgramme  | 
|     global PyMongoDataServerConfig  | 
|     global DBAllChConfig  | 
|       | 
|     #ÖØ¶ÁËùÓеÄÅäÖÃ±í  | 
|     AllChConfig = {}  | 
|     GeneralProgramme = {}  | 
|     FormulaControl.ClearCompileFormulaDist()  | 
|     PyMongoDataServerConfig = None  | 
|     DBAllChConfig = {}  | 
|       | 
|     GameConfig.ClearConfig()  | 
|     GameWorld.Log('GameServer_Reload_ChConfig')  | 
|     #¹Ø±Õ×Öµä  | 
|     GameWorld.GetGameWorld().SetDict(ShareDefine.Def_Notify_WorldKey_ReloadConfig , 0)  | 
|     #ReloadModule.DoModuleHotUpdate()  | 
|     return  | 
|   | 
|           | 
| #---------------------------------------------------------------------  | 
| ## ¶ÁÈ¡ËùÓеÄÀà±ð×ÖµäÐÅÏ¢  | 
| #  @param fileName ÎļþÃû  | 
| #  @return None  | 
| #  @remarks º¯ÊýÏêϸ˵Ã÷.  | 
| def ReadConfigData(fileName, needEval):  | 
|     global AllChConfig  | 
|     global GeneralProgramme  | 
|       | 
|     #²éÕÒ·¾¶ GeneralProgramme ×ֵ乹³É { ¹¦ÄÜÃû £º [·¾¶Ãû , md5] }  | 
|     curFileList = GeneralProgramme.get(fileName)  | 
|       | 
|     if not curFileList:  | 
|         GameWorld.Log('×ܸÙÖÐδ·¢ÏÖ¹¦ÄÜÃû = %s'%(fileName))  | 
|         raise Exception('×ܸÙÖÐδ·¢ÏÖ¹¦ÄÜÃû = %s'%(fileName))  | 
|       | 
|     curPath = ChConfig.GetAppPath() + "\\" + "GameServerConfig" + '%s.txt'%(curFileList[0])  | 
|       | 
|     if not os.path.isfile(curPath):  | 
|         GameWorld.Log('δ·¢ÏÖÄ¿±êÎļþ = %s,%s,%s'%(fileName , curFileList[0] , curPath))  | 
|         raise Exception ('δ·¢ÏÖÄ¿±êÎļþ = %s,%s,%s'%(fileName , curFileList[0] , curPath))  | 
|       | 
| #===============================================================================  | 
| #    #-------¶ÁÈ¡Îļþ(ÒÑrbģʽ´ò¿ªÑéÖ¤Md5)  | 
| #    file = open( curPath , 'rb' )  | 
| #    strMsg = file.read()  | 
| #    file.close()  | 
| #      | 
| #    #--------ÑéÖ¤Md5  | 
| #    if md5.md5(strMsg).hexdigest() != curFileList[1]:  | 
| #        GameWorld.Log( 'md5²»·ûºÏ = %s,%s,%s,%s'%(fileName , curFileList[0] , md5.md5(strMsg).hexdigest() , curFileList[1] ))  | 
| #        raise Exception( 'md5²»·ûºÏ = %s,%s,%s,%s'%(fileName , curFileList[0] , md5.md5(strMsg).hexdigest() , curFileList[1] ))  | 
| #      | 
| #===============================================================================  | 
|     #-------¶ÁÈ¡Îļþ(ÒÑrģʽ´ò¿ª´æÈëÊý¾Ý,·½±ãeval½âÎö)  | 
|     file = open(curPath, 'r')  | 
|     strMsg = file.read()  | 
|     file.close()  | 
|       | 
|     if needEval:  | 
|         try:  | 
|             result = eval(strMsg)  | 
|         except BaseException , e:  | 
|             GameWorld.Log('»ñÈ¡ÅäÖÃÐÅÏ¢Key = %s¶Áȡʧ°Ü,ÎÞ·¨½âÎöÔÒò:%s, Å׳ö,%s'%(fileName, e, strMsg))  | 
|             raise Exception('»ñÈ¡ÅäÖÃÐÅÏ¢Key = %s¶Áȡʧ°Ü'%fileName)  | 
|           | 
|         AllChConfig.update({fileName:result})  | 
|           | 
|     else:  | 
|         #±àÒëºó´¢´æ  | 
|         compileStrMsg = FormulaControl.CompileFormula(fileName, strMsg)  | 
|         AllChConfig.update({fileName:compileStrMsg})  | 
|     return  | 
| #---------------------------------------------------------------------  | 
| ## ¶ÁÈ¡×ܸÙ,ÓÃÓÚ½âÎöÀ©Õ¹ÎļþÃû  | 
| #  @param ÎÞ  | 
| #  @return None  | 
| #  @remarks º¯ÊýÏêϸ˵Ã÷.  | 
| def ReadConfigData_GeneralProgramme():  | 
|     global GeneralProgramme  | 
|       | 
|     curPath = ChConfig.GetAppPath() + "\\" + "GameServerConfig\\GeneralProgramme.txt"  | 
|       | 
|     if not os.path.isfile(curPath):  | 
|         GameWorld.Log('ÎÞ·¨·¢ÏÖ×ܸ٠= %s'%(curPath))  | 
|         raise Exception ('ÎÞ·¨·¢ÏÖ×ܸ٠= %s'%(curPath))  | 
|       | 
|     file = open(curPath, 'r')  | 
|       | 
|     #¶ÁÈ¡Êý¾Ý -----ÔÝʱ¹Ø±Õmd5¶ÁÈ¡¹¦ÄÜ£¬ÈçÒª¿ªÆô[:2] -> [:3]  | 
|     date = [line.split('\t')[:2] for line in file.readlines()]  | 
|       | 
|     file.close()  | 
|       | 
|     if not date:  | 
|         GameWorld.Log('###×ܸÙΪ¿Õ = %s'%(curPath))  | 
|         return  | 
|       | 
|     #ɾ³ýµÚÒ»ÐÐ×¢ÊÍÁÐ  | 
|     date.pop(0)  | 
|       | 
|     for dateList in date:  | 
|         #×ֵ乹³É { ¹¦ÄÜÃû £º [·¾¶Ãû , md5] }  | 
|         GeneralProgramme.update({dateList[0]:dateList[1:]})  | 
|       | 
|     return  | 
|   | 
| ## -----------------------------------------------------------  | 
|   | 
| ## PyMongoDataServer.iniÅäÖöÁÈ¡  | 
| #  @param section: ·Ö¶ÎÇø¿éÃû  | 
| #  @param option: ÅäÖÃÏîÃû  | 
| #  @return  | 
| #  @remarks \db\PyMongoDataServer\PyMongoDataServer.iniÅäÖöÁÈ¡  | 
| def GetPyMongoConfig(section, option, raw=False):  | 
|     global PyMongoDataServerConfig  | 
|       | 
|     if not PyMongoDataServerConfig:  | 
|         dbAPPath = ChConfig.GetDBPath() + "\\PyMongoDataServer.ini"  | 
|           | 
|         if not os.path.isfile(dbAPPath):  | 
|             GameWorld.Log('ÎÞ·¨ÕÒµ½Îļþ = %s'%(dbAPPath))  | 
|             raise Exception('ÎÞ·¨ÕÒµ½Îļþ = %s'%(dbAPPath))  | 
|           | 
|         PyMongoDataServerConfig = ConfigParser.ConfigParser()  | 
|         PyMongoDataServerConfig.read(dbAPPath)  | 
|         GameWorld.DebugLog("Reload dbAPPath=%s" % str(dbAPPath))  | 
|       | 
|     if not PyMongoDataServerConfig.has_option(section, option):  | 
|         GameWorld.ErrLog("PyMongoDataServer.iniÕÒ²»µ½ÅäÖÃ: section=%s,option=%s" % (section, option))  | 
|         return ""  | 
|       | 
|     strParam = PyMongoDataServerConfig.get(section, option, raw)  | 
|     return strParam  | 
|   | 
| def GetDBConfig(key): return __DoLogic_GetDBConfig(key)  | 
| def GetDBEvalChConfig(key): return __DoLogic_GetDBConfig(key, True)  | 
|   | 
| ## Í¨¹ýkey²éÕҲ߻®±í£¬Èç¹û»¹Î´¼ÓÔØ£¬Ôò½øÐмÓÔØ  | 
| #  @param key ±íÃû  | 
| #  @return None  | 
| #  @remarks º¯ÊýÏêϸ˵Ã÷.  | 
| def __DoLogic_GetDBConfig(key, needEval=False):  | 
|     global DBAllChConfig  | 
|       | 
|     #ÊÇ·ñÖØ¶ÁËùÓеÄÅäÖÃ±í  | 
|     if GameWorld.GetGameWorld().GetDictByKey(ShareDefine.Def_Notify_WorldKey_ReloadConfig):  | 
|         ClearAllConfig()  | 
|     else:  | 
|         #ÕÒµ½ÁË,¾Í·µ»Ø  | 
|         if DBAllChConfig.has_key(key):  | 
|             return DBAllChConfig[key]  | 
|           | 
|     if key not in Def_DBCfgFilePath:  | 
|         GameWorld.Log('DBÅäÖÃÖÐδ·¢ÏÖKeyÎļþ = %s' % (key))  | 
|         return  | 
|     filePath = ChConfig.GetDBPath() + Def_DBCfgFilePath[key]  | 
|       | 
|     retData = __ReadConfigDataEx(key, filePath, needEval)  | 
|     if not retData:  | 
|         return  | 
|     DBAllChConfig.update({key:retData})  | 
|     return retData  | 
|   | 
| def __ReadConfigDataEx(fileName, filePath, needEval):  | 
|       | 
|     if not os.path.isfile(filePath):  | 
|         GameWorld.Log('δ·¢ÏÖÄ¿±êÎļþ = %s' % (filePath))  | 
|         raise Exception ('δ·¢ÏÖÄ¿±êÎļþ = %s' % (filePath))  | 
|       | 
|     file = open(filePath, 'r')  | 
|     strMsg = file.read()  | 
|     file.close()  | 
|       | 
|     if needEval:  | 
|         try:  | 
|             result = eval(strMsg)  | 
|         except BaseException, e:  | 
|             GameWorld.Log('»ñÈ¡ÅäÖÃÐÅÏ¢Key = %s ¶Áȡʧ°Ü,ÎÞ·¨½âÎöÔÒò:%s, Å׳ö,%s' % (fileName, e, strMsg))  | 
|             raise Exception('»ñÈ¡ÅäÖÃÐÅÏ¢Key = %s ¶Áȡʧ°Ü' % fileName)  | 
|           | 
|         return result  | 
|       | 
|     else:  | 
|         #±àÒëºó´¢´æ  | 
|         compileStrMsg = FormulaControl.CompileFormula(fileName, strMsg)  | 
|         return compileStrMsg  | 
|   | 
| # C++ÖØ¶ÁÅäÖÃ֪ͨ  | 
| def ReloadConfig():  | 
|     GameWorld.DebugLog("ReloadConfig")  | 
|     ClearAllConfig()  | 
|     GameWorld.SendMapServerMsgEx(ShareDefine.Def_Notify_WorldKey_ReloadConfig, 1)  | 
|     IpyGameDataPY.IPYData.IpyDataClear()  | 
|     import GameWorldProcess  | 
|     GameWorldProcess.OnReloadConfig()  | 
|     return  | 
|   | 
|   |