#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
# µ¼³öPY°æ±¾ÓõĠIpyGameDataPY.py  
 | 
import ConfigParser  
 | 
import sys  
 | 
import os  
 | 
import re  
 | 
  
 | 
cfg = ConfigParser.ConfigParser()  
 | 
cfg.read("config.ini")  
 | 
CodeProjectPath = cfg.get("config", "CodeProjectPath") # ´úÂëÏîĿ·¾¶  
 | 
  
 | 
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"  
 | 
  
 | 
# ¹ÜÀíÆ÷³õʼ»¯±íÊý¾Ý»º´æÁбí¶ÔÏóÄ£°å  
 | 
MgrTableCacheInit = "        self.ipy%sCache = self.__LoadFileData(\"%s\", IPY_%s)%s"  
 | 
MgrTableLenInit = "        self.ipy%sLen = len(self.ipy%sCache)%s"  
 | 
# ¹ÜÀíÆ÷»ñÈ¡±íÊý¾ÝÌõÊýº¯ÊýÄ£°å  
 | 
MgrGetCountFunc = "    def Get%sCount(self): return self.ipy%sLen%s"  
 | 
# ¹ÜÀíÆ÷¸ù¾ÝË÷Òý»ñÈ¡±íÊý¾Ýº¯ÊýÄ£°å  
 | 
MgrGetByIndexFunc = "    def Get%sByIndex(self, index): return self.ipy%sCache[index]%s"  
 | 
  
 | 
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", "char", "dict", "list", "eval", "float"]  
 | 
    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)  
 | 
            classFuncInfo += ClassFuncTemp % (fieldName, fieldName, noteInfo, lineEnd)  
 | 
              
 | 
        ipyTableDef += TableDefEnd % (Def_RN if i == len(Def_IpyTable) - 1 else (Def_RN * 2))  
 | 
        classContent += ClassTemp % (tableNameCh, tableName, classInitInfo, classFuncInfo)  
 | 
          
 | 
        # ±íÁÐ±í¡¢³¤¶È¶ÔÏó»º´æ  
 | 
        mgrTableCacheObjInit += MgrTableCacheInit % (tableName, tableName, tableName, Def_RN)  
 | 
        mgrTableCacheObjInit += MgrTableLenInit % (tableName, tableName, Def_RN)  
 | 
          
 | 
        # ±íÁÐ±í¡¢³¤¶È»ñÈ¡º¯Êý  
 | 
        mgrTableFunc += MgrGetCountFunc % (tableName, tableName, Def_RN)  
 | 
        mgrTableFunc += MgrGetByIndexFunc % (tableName, tableName, Def_RN)  
 | 
          
 | 
      
 | 
    # ¶Áȡģ°åÉú³É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()  
 | 
  
 | 
  
 | 
  
 |