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
#!/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