#!/usr/bin/python # -*- coding: GBK -*- # µ¼³öPY°æ±¾ÓÃµÄ IpyGameDataPY.py import ConfigParser import sys import os import re cfg = ConfigParser.ConfigParser() cfg.read("config.ini") CodeProjectPath = "" # ´úÂëÏîĿ·¾¶ for i in range(1, 20): folderPath = cfg.get("config", "CodeProjectPath%s" % i) if os.path.isdir(folderPath): CodeProjectPath = folderPath break IpyGameDataFile = "IpyGameDataPY.py" # Éú³ÉµÄpyÎļþÃû # ±í½á¹¹¶¨Òå×ÖµäÄ£°å TableDefKey = " \"%s\":(%s" TableDefValue = " (\"%s\", \"%s\", %s),%s" TableDefEnd = " ),%s" # Àà½á¹¹Ä£°å ClassTemp = ''' # %s class IPY_%s(): def __init__(self): %s return %s ''' # Àà³õʼ»¯ÊýÖµ¶ÔÏóÄ£°å ClassInitTemp = " self.%s = %s%s" # »ñÈ¡Àà¶ÔÏóº¯ÊýÄ£°å ClassFuncTemp = " def Get%s(self): return self.%s%s %s%s" # ¹ÜÀíÆ÷³õʼ»¯±íÊý¾Ý»º´æÁбí¶ÔÏóÄ£°å MgrTableCacheInit = " self.__LoadFileData(\"%s\", onlyCheck)%s" # ¹ÜÀíÆ÷»ñÈ¡±íÊý¾ÝÌõÊý¡¢Êý¾Ýº¯ÊýÄ£°å MgrGetCountFunc = ''' def Get%sCount(self): self.CheckLoadData("%s") return self.ipy%sLen\r def Get%sByIndex(self, index): self.CheckLoadData("%s") return self.ipy%sCache[index]\r ''' Def_RN = "\r\n" def DoCreateIpyGameDataPY(): print "=================================================" print "Number of arguments:", len(sys.argv), "arguments." StructFileName = sys.argv[1] if StructFileName == "PySysDBPY.h": IpyGameDataPYFile = CodeProjectPath + "\\ZoneServerGroup\\map1_8G\\MapServer\\MapServerData\\Script\\" LoadStructPath = "ChConfig.GetDBPath()" elif StructFileName == "PySysDBG.h": IpyGameDataPYFile = CodeProjectPath + "\\CoreServerGroup\\GameServer\\Script\\" LoadStructPath = "ChConfig.GetAppPath()" else: print "²»Ö§³Ö¸Ã±í½á¹¹Îļþ!%s" % StructFileName raise IpyGameDataPYFile += IpyGameDataFile # ¶ÁÈ¡±í½á¹¹¶¨Òå print "¼ÓÔØÅäÖñí½á¹¹: %s" % StructFileName fileObj = open("../%s" % StructFileName, 'rb') content = fileObj.read() fileObj.close() # ½âÎö±í½á¹¹¶¨Òå Def_FuncConfigName = "FuncConfig" # ¹¦ÄÜÅäÖñí, ´Ë±íÌØÊâ´¦Àí TableNameList = [] # È·±£±í½á¹¹µ¼³ö˳ÐòÒ»ÖÂ, ·ÀÖ¹°æ±¾¶Ô±È»ìÂÒ Def_IpyTable = {} Def_FieldTypeList = ["DWORD", "WORD", "BYTE", "float", "char", "dict", "list", "eval"] structList = re.findall("\/\/.*?struct.*?\{.*?\};", content, re.S) for structStr in structList: lineList = structStr.split(Def_RN) tableInfoStr = lineList[0] if "#tag" in tableInfoStr: tagIndex = tableInfoStr.index("#tag") tableNameCh = tableInfoStr[2:tagIndex].replace(" ", "") # ±íÃûÖÐÎÄÃû else: tableNameCh = tableInfoStr[2:].replace(" ", "") # ±íÃûÖÐÎÄÃû tableName = "" # ±íÃûÓ¢ÎÄÃû fieldInfoList = [] for line in lineList[1:]: if not line: continue if "struct" in line: tagIndex = line.index("tag") if "{" in line: tableName = line[tagIndex + 3:line.index("{")] else: tableName = line[tagIndex + 3:] tableName = tableName.replace(" ", "") # ±íÃûÓ¢ÎÄÃû continue noteInfo = " #" # ×Ö¶Î×¢ÊÍ˵Ã÷ if "//" in line: signIndex = line.index("//") noteInfo = " # %s" % line[signIndex+2:].rstrip() line = line[:signIndex] fieldType = "" for ft in Def_FieldTypeList: if ft in line: fieldType = ft break if not fieldType: if "struct" not in line and "{" not in line and "}" not in line: print "### ÀàÐͶ¨Òå´íÎó±í ###" print "struct tag%s" % tableName print line raise continue nameIndex = line.index(fieldType) + len(fieldType) if "[" in line: endIndex = line.index("[") elif ";" in line: endIndex = line.index(";") else: continue isIndex = 0 # ÊÇ·ñËÑË÷Ë÷Òý fieldName = line[nameIndex:endIndex].replace(" ", "").replace("\t", "") if fieldName.startswith("_"): fieldName = fieldName[1:] isIndex = 1 fieldInfoList.append([fieldType, fieldName, isIndex, noteInfo]) Def_IpyTable[tableName] = [tableNameCh, fieldInfoList] TableNameList.append(tableName) # Ä£°åËùÐèÌæ»»µÄÄÚÈݲÎÊý ipyTableDef = "" # ±í½á¹¹¶¨Òå classContent = "" # ±í¶ÔÓ¦µÄÀà mgrTableCacheObjInit = "" # ±íÊý¾Ý¶ÔÏ󻺴æ mgrTableFunc = "" # ±íÊý¾Ý¶ÔÏó»ñÈ¡º¯Êý for i, tableName in enumerate(TableNameList): tableNameCh, fieldInfoList = Def_IpyTable[tableName] classInitInfo = "" classFuncInfo = "" ipyTableDef += TableDefKey % (tableName, Def_RN) for j, fieldInfo in enumerate(fieldInfoList): lineEnd = "" if j == len(fieldInfoList) - 1 else Def_RN fieldType, fieldName, isIndex, noteInfo = fieldInfo ipyTableDef += TableDefValue % (fieldType, fieldName, isIndex, Def_RN) if fieldType == "char": classInitInfo += ClassInitTemp % (fieldName, "\"\"", lineEnd) elif fieldType == "dict": classInitInfo += ClassInitTemp % (fieldName, "{}", lineEnd) elif fieldType == "list": classInitInfo += ClassInitTemp % (fieldName, "[]", lineEnd) elif fieldType == "float": classInitInfo += ClassInitTemp % (fieldName, "0.0", lineEnd) else: classInitInfo += ClassInitTemp % (fieldName, "0", lineEnd) callAttrValue = "attrTuple[%s]" % j # fieldName classFuncInfo += ClassFuncTemp % (fieldName, callAttrValue, noteInfo, fieldType, lineEnd) ipyTableDef += TableDefEnd % (Def_RN if i == len(Def_IpyTable) - 1 else (Def_RN * 2)) classInitInfo = ClassInitTemp % ("attrTuple", "None", "") # ÓÅ»¯Äڴ棬²»Ê¹ÓÃÄÚÖà __dict__ ·ÃÎÊÊôÐÔ£¬¸ÄΪʹÓÃtuple´ævalue classContent += ClassTemp % (tableNameCh, tableName, classInitInfo, classFuncInfo) # ±íÁÐ±í¡¢³¤¶È¶ÔÏ󻺴æ mgrTableCacheObjInit += MgrTableCacheInit % (tableName, Def_RN) # ±íÁÐ±í¡¢³¤¶È»ñÈ¡º¯Êý mgrTableFunc += MgrGetCountFunc % (tableName, tableName, tableName, tableName, tableName, tableName) # ¶Áȡģ°åÉú³Épy´úÂë createPYContent = "" template = open("IpyGameDataPYTemp.py", 'rb') for line in template: if "#<%Table_Def%>" in line: line = ipyTableDef elif "#<%Table_Class%>" in line: line = classContent elif "#<%Ipy_Cache_Init%>" in line: line = mgrTableCacheObjInit elif "\"<%LoadStructPath%>\"" in line: line = line.replace("\"<%LoadStructPath%>\"", LoadStructPath) elif "#<%Ipy_Cache_Func%>" in line: line = mgrTableFunc createPYContent += line template.close() pyFile = open(IpyGameDataPYFile, "wb") pyFile.write("%s" % createPYContent) pyFile.close() print "³É¹¦Éú³É: %s" % IpyGameDataFile print "µ¼³ö·¾¶: %s" % IpyGameDataPYFile return DoCreateIpyGameDataPY()