#!/usr/bin/python
|
# -*- coding: GBK -*-
|
#---------------------------------------------------------------------
|
#
|
#---------------------------------------------------------------------
|
##@package ReadChConfig
|
# @todo: ²ß»®txt¶Á±í¹ÜÀíÆ÷
|
#
|
# @author: panwei
|
# @date 2010-04-30
|
# @version 1.9
|
#
|
# @note: ´Ëº¯Êý¶ÁÈ¡¸ùĿ¼ÏµÄMapServerConfigÎļþÖеÄËùÓÐ.txtÎļþ
|
#
|
# @change: "2010-08-16 15:20" panwei ÓÅ»¯Evel¶ÁÈ¡, µ¼ÈëÄ£¿éFormulaControl
|
# @change: "2010-08-17 10:20" panwei ÓÅ»¯Evel¶ÁÈ¡, µ¼ÈëÄ£¿éFormulaControl
|
# @change: "2010-08-18 10:20" panwei ÓÅ»¯Evel¶ÁÈ¡, Ö±½Ó±àÒëºó´¢´æ
|
# @change: "2010-12-22 18:00" Alee ÐÞ¸Ä×ÖµäÃû
|
# @change: "2012-11-30 19:00" wdb ÐÞ¸ÄÖØ¶ÁÅäÖÃ
|
# @change: "2013-06-27 14:30" wdb ÐÞ¸Äevalºó¼ÓÈ뻺´æ
|
# @change: "2014-12-28 21:00" 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 ConfigParser
|
import ShareDefine
|
import MapConfig
|
#---------------------------------------------------------------------
|
#È«¾Ö±äÁ¿
|
#---------------------------------------------------------------------
|
|
#---------------------------------------------------------------------
|
#Â߼ʵÏÖ
|
|
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().GetGameWorldDictByKey(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 = {}
|
|
MapConfig.ClearConfig()
|
|
GameWorld.Log('MapServer_Reload_ChConfig')
|
#¹Ø±Õ×Öµä
|
GameWorld.GetGameWorld().SetGameWorldDict(ShareDefine.Def_Notify_WorldKey_ReloadConfig , 0)
|
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() + "\\" + "MapServerConfig" + '%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:
|
# ±àÒëºó´¢´æ
|
AllChConfig.update({fileName:compile(strMsg, 'ReadChConfig', 'eval')})
|
return
|
#---------------------------------------------------------------------
|
## ¶ÁÈ¡×ܸÙ,ÓÃÓÚ½âÎöÀ©Õ¹ÎļþÃû
|
# @param ÎÞ
|
# @return None
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def ReadConfigData_GeneralProgramme():
|
global GeneralProgramme
|
|
curPath = ChConfig.GetAppPath() + "\\" + "MapServerConfig\\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().GetGameWorldDictByKey(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:
|
#±àÒëºó´¢´æ
|
return compile(strMsg, 'ReadChConfig', 'eval')
|
|