| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package ReloadModule  | 
| #  | 
| # @todo:ÖØ¶Á½Å±¾Ä£¿é  | 
| # @author hxp  | 
| # @date 2016-8-5  | 
| # @version 1.0  | 
| #  | 
| # ÏêϸÃèÊö: ÖضÁ½Å±¾Ä£¿é£¬½ö¶Ô±ä¸üµÄ½Å±¾ÖضÁ£¬ÓÅ»¯ÈȲ¿Êð  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #"""Version = 2016-8-5 19:00"""  | 
| #-------------------------------------------------------------------------------  | 
|   | 
| import GameWorld  | 
| import ShareDefine  | 
| import QuestManager  | 
| import EventShell  | 
| import ChConfig  | 
|   | 
| import sys  | 
| import os  | 
|   | 
| g_moduleFileCMTimeDict = {} # Ä£¿éÎļþ´´½¨¼°ÐÞ¸Äʱ¼ä  | 
|   | 
| ## Ë¢Ð½ű¾Ä£¿é´´½¨¼°ÐÞ¸Äʱ¼ä  | 
| def __RefreshModuleFileCMTime():  | 
|     global g_moduleFileCMTimeDict  | 
|   | 
|     g_moduleFileCMTimeDict = {}  | 
|     GameWorld.Log("ˢнű¾Îļþʱ¼ä...")  | 
|     for parent, dirnames, filenames in os.walk(ChConfig.ScriptPath):  | 
|         #DebugLog("parent=%s, dirnames=%s" % (parent, dirnames))  | 
|         for filename in filenames:  | 
|             if "py" not in filename:  | 
|                 continue  | 
|             fullPath = os.path.join(parent, filename)  | 
|             ctime = os.path.getctime(fullPath) # »ñÈ¡ÎļþµÄ´´½¨Ê±¼ä  | 
|             mtime = os.path.getmtime(fullPath) # »ñÈ¡ÎļþµÄÐÞ¸Äʱ¼ä   | 
|             #DebugLog("    filename=%s, fullPath=%s" % (filename, fullPath))  | 
|             g_moduleFileCMTimeDict[filename] = [ctime, mtime]  | 
|     GameWorld.Log("    ½Å±¾×ÜÊý: %s" % len(g_moduleFileCMTimeDict))  | 
|               | 
|     return  | 
|   | 
| ## Ä£¿é½Å±¾Èȸü, ÓÉGameServer¿ØÖƹ㲥´¥·¢  | 
| def DoMapServerScriptReload(updVersion, tick):  | 
|       | 
|     curVersion = GameWorld.GetGameWorld().GetGameWorldDictByKey(ShareDefine.Def_Notify_WorldKey_MapServerScriptReloadVersion)  | 
|     # ÕâÀïg_moduleFileCMTimeDict»á±ä¿ÕÒ»°ãÊÇÒòΪdebugÆÚ¼äʹÓÃÁËÖØ¶ÁËùÓнű¾µ¼Ö£¬Ò»°ãÕýʽ»·¾³²»ÔÙʹÓÃ֮ǰµÄÖØ¶Á½Å±¾£¬ËùÒÔ²»»áÓпյÄÇé¿ö  | 
|     if not curVersion or not g_moduleFileCMTimeDict:  | 
|         __RefreshModuleFileCMTime()  | 
|         return  | 
|       | 
|     if curVersion == updVersion:  | 
|         return  | 
|       | 
|     GameWorld.Log("¿ªÊ¼½Å±¾ÖضÁ curVersion=%s,updVersion=%s" % (curVersion, updVersion))  | 
|     moduleNameDict = {}  | 
|     isReloadMission = False  | 
|       | 
|     for moduleKey, module in sys.modules.items():  | 
|         if "py" not in str(module):  | 
|             #GameWorld.DebugLog("    not py module: %s=%s" % (moduleKey, module))  | 
|             continue  | 
|           | 
|         moduleName = moduleKey.split(".")[-1]  | 
|         moduleList = moduleNameDict.get(moduleName, [])  | 
|         moduleList.append(moduleKey) # ÓÐЩģ¿é»á²úÉú¶à¸ö...£¿  | 
|         moduleNameDict[moduleName] = moduleList  | 
|           | 
|     for parent, dirnames, filenames in os.walk(ChConfig.ScriptPath):  | 
|         #DebugLog("parent=%s, dirnames=%s" % (parent, dirnames))  | 
|         for filename in filenames:  | 
|             if "py" not in filename:  | 
|                 continue  | 
|               | 
|             curModuleName = filename.split('.')[0]  | 
|             if curModuleName == __name__:  | 
|                 #GameWorld.DebugLog("    ±¾½Å±¾²»¸üÐÂ: %s" % curModuleName)  | 
|                 continue  | 
|   | 
|             fullPath = os.path.join(parent, filename)  | 
|             curCTime = os.path.getctime(fullPath) # »ñÈ¡ÎļþµÄ´´½¨Ê±¼ä  | 
|             curMTime = os.path.getmtime(fullPath) # »ñÈ¡ÎļþµÄÐÞ¸Äʱ¼ä  | 
|             if filename in g_moduleFileCMTimeDict:  | 
|                 ctime, mtime = g_moduleFileCMTimeDict[filename]  | 
|                 # Ê±¼äÏàͬ´ú±í½Å±¾Ã»±ä£¬²»½øÐÐÖØ¶Á  | 
|                 if ctime == curCTime and mtime == curMTime:  | 
|                     #GameWorld.DebugLog("    not change %s : %s" % (filename, fullPath))  | 
|                     continue  | 
|             g_moduleFileCMTimeDict[filename] = curCTime, curMTime  | 
|               | 
|             if curModuleName in moduleNameDict:  | 
|                 for moduleKey in moduleNameDict[curModuleName]:  | 
|                     __DoModuleReload(moduleKey)  | 
|                       | 
|                 if curModuleName in ["QuestCommon", "QuestManager", "QuestRunner"]:  | 
|                     isReloadMission = True  | 
|       | 
|     # ÅжϽű¾ÎļþÊÇ·ñÓÐÐ޸ģ¬ÈçÓÐÐÞ¸ÄÔò½øÐÐÖØ¶ÁÈÎÎñ  | 
|       | 
|     # ÖضÁÈÎÎñ¿ÉÄÜÓÐÒ»¶¨·çÏÕ£¬ÐèÏêϸ²âÊÔ  | 
|     if isReloadMission:  | 
|         QuestManager.ReloadQuests(tick)  | 
|         EventShell.DoReloadRefresh()  | 
|           | 
|     GameWorld.Log("    ½Å±¾ÖضÁ OK! isReloadMission=%s" % isReloadMission)     | 
|     return  | 
|   | 
| def __DoModuleReload(moduleKey):  | 
|     module = sys.modules.get(moduleKey)  | 
|     if not module:  | 
|         return  | 
|     try:  | 
|         # ÕâÀïÏȽøÐмòµ¥µÄÖ±½ÓÖØ¶Á£¬ÈçÓÐÐèÒª£¬¿ÉÓÅ»¯ÖضÁÂß¼£¬ÊµÏÖ²»Í¬µÄÖØ¶ÁÐèÇó£¬Èç±£ÁôÈ«¾Ö±äÁ¿ÖµµÈ  | 
|         # Ä¿Ç°Ö±½ÓÖØ¶ÁÒѾ¿ÉÒÔÓ¦¶Ô´ó²¿·ÖÐèÇ󣬹ÊÔݲ»×öÖ§³Ö  | 
|         reload(module)  | 
|         GameWorld.Log("    reload module %s, %s" % (moduleKey, module))  | 
|     except Exception:  | 
|         GameWorld.ErrLog("    reload module err! %s, %s" % (moduleKey, module))  | 
|     return  | 
|   | 
|   | 
|   | 
|   | 
|   | 
|   |