Beispiel #1
0
        public void generate(string protocolFolderPath, string templateXmlPath, string codeFolderPath)
        {

            var template = XElement.Load(templateXmlPath);
            codeTemplate = new CodeTemplate();
            codeTemplate.load(template);

            singleMode = codeTemplate.getConfig("singleMode") == "true";

            voFolder = codeTemplate.getConfig("voFolder");

            codeFolderPath += "/";
            this.codeFolderPath = codeFolderPath;

            if (Directory.Exists(codeFolderPath))
            {
                try
                {
                    Directory.Delete(codeFolderPath, true);
                }
                catch (Exception e)
                {
                    Console.WriteLine("删除目录失败,重试..");
                    Thread.Sleep(50);
                    Directory.Delete(codeFolderPath, true);
                }
            }
            Thread.Sleep(50);
            Directory.CreateDirectory(codeFolderPath);

            if (!singleMode)
            {
                if (String.IsNullOrEmpty(voFolder))
                    this.voFolderPath = this.codeFolderPath;
                else
                {
                    this.voFolderPath = codeFolderPath + voFolder + "/";
                    if (Directory.Exists(voFolderPath))
                        Directory.Delete(voFolderPath, true);

                    Directory.CreateDirectory(voFolderPath);
                }
            }


            var text = "";

            var list_ptlFile = Directory.GetFiles(protocolFolderPath, "*.ptl");
            foreach (var filePath in list_ptlFile)
            {
                text += File.ReadAllText(filePath, Encoding.UTF8);
                text += @"
";
            }
            text = clearComment(text);

            var match_structs = reg_struct.Matches(text);
            for (int m = 0; m < match_structs.Count; m++)
            {
                var match_struct = match_structs[m];
                var protoStruct = new ProtocolStruct()
                {
                    isMessage = false,
                    comment = cleanComment(match_struct.Groups[1].Value),
                    type = match_struct.Groups[2].Value,
                };
                protoStruct.memberList = parseMembers(match_struct.Groups[3].Value);

                if (dic_struct.ContainsKey(protoStruct.type))
                    throw new Exception("重复的struct定义:" + protoStruct.type);
                dic_struct.Add(protoStruct.type, protoStruct);

                // text += readParams(item);
            }

            match_structs = reg_message.Matches(text);
            for (int m = 0; m < match_structs.Count; m++)
            {
                var match_struct = match_structs[m];
                var protoStruct = new ProtocolStruct()
                {
                    isMessage = true,
                    comment = cleanComment(match_struct.Groups[1].Value),
                    type = match_struct.Groups[2].Value,
                };
                protoStruct.memberList = parseMembers(match_struct.Groups[4].Value);

                var idStr = match_struct.Groups[3].Value;
                try
                {
                    protoStruct.id = Convert.ToInt32(idStr);
                }
                catch
                {
                    throw new Exception(String.Format("转换消息id:{0} 失败", idStr));
                }

                if (dic_message.ContainsKey(protoStruct.id))
                    throw new Exception("重复的消息号定义:" + protoStruct.id);
                dic_message.Add(protoStruct.id, protoStruct);

                if (dic_struct.ContainsKey(protoStruct.type))
                    throw new Exception("重复的struct定义:" + protoStruct.type);
                dic_struct.Add(protoStruct.type, protoStruct);

                // text += readParams(item);
            }

            match_structs = reg_enum.Matches(text);
            for (int m = 0; m < match_structs.Count; m++)
            {
                var match_struct = match_structs[m];
                var protoStruct = new ProtocolEnum()
                {
                    comment = cleanComment(match_struct.Groups[1].Value),
                    type = match_struct.Groups[2].Value,
                };
                protoStruct.memberList = parseEnumMembers(match_struct.Groups[3].Value);

                if (dic_enum.ContainsKey(protoStruct.type))
                    throw new Exception("重复的enum定义:" + protoStruct.type);
                dic_enum.Add(protoStruct.type, protoStruct);

                // text += readParams(item);
            }

            var singleText = "";
            foreach (var item in dic_struct.Values)
            {
                singleText += generaterStruct(item);
            }

            foreach (var item in dic_enum.Values)
            {
                singleText += generaterEnum(item);
            }

            //Console.ReadLine();

            if (singleMode)
                FileUtil.writeFile(codeFolderPath + codeTemplate.element_SingleProtocolFile.Attribute("fileName").Value, Encoding.UTF8.GetBytes(codeTemplate.getSingleFileText(singleText)));


            var list_message = dic_message.Values.ToList();
            list_message = (from item in list_message orderby item.id select item).ToList();
            var str_enum = "";
            var str_registerMessage = "";
            var str_createMessage = "";
            var str_dispatchMessage = "";

            str_enum += codeTemplate.getEnumDefinition("None", "0", "");

            for (int i = 0; i < list_message.Count; i++)
            {
                var msg = list_message[i];
                str_enum += codeTemplate.getEnumDefinition(msg.type, msg.id.ToString(), msg.comment);

                str_registerMessage += codeTemplate.getMessageRegister(msg.type, msg.type);

                str_createMessage += codeTemplate.getMessageCreater(msg.type);
                str_dispatchMessage += codeTemplate.getMessageDispatcher(msg.type);
            }
            str_enum = codeTemplate.getProtocolEnumClass(codeTemplate.ProtocolEnumName, str_enum, "");
            str_registerMessage = codeTemplate.getMessageRegisterClass(str_registerMessage, str_createMessage, str_dispatchMessage);

            FileUtil.writeFile(codeFolderPath + codeTemplate.ProtocolEnumName + codeTemplate.ClassExtension, Encoding.UTF8.GetBytes(str_enum));
            FileUtil.writeFile(codeFolderPath + codeTemplate.element_MessageRegisterClass.Attribute("fileName").Value, Encoding.UTF8.GetBytes(str_registerMessage));


            var copyFiles = template.Element("CopyFiles");
            if (copyFiles != null)
            {
                var list_copy = copyFiles.Elements("CopyFile");
                foreach (var item in list_copy)
                {
                    var fileName = item.Attribute("fileName").Value.Trim();
                    var content = item.Value;
                    FileUtil.writeFile(codeFolderPath + fileName, Encoding.UTF8.GetBytes(content));
                }
            }

            var templateFileInfo = new FileInfo(templateXmlPath);
            var copyDirInfo = new DirectoryInfo(templateFileInfo.Directory + "/CopyFiles");
            if (copyDirInfo.Exists)
                FileUtil.copyFolder(copyDirInfo.FullName, codeFolderPath);


            Console.WriteLine("已生成代码至{0}", codeFolderPath);

        }
Beispiel #2
0
        public string generaterEnum(ProtocolEnum protoEnum)
        {
            if (dic_name.ContainsKey(protoEnum.type))
            {
                throw new Exception(String.Format("重复的类名:{0}", protoEnum.type));
            }

            dic_name.Add(protoEnum.type, true);

            string text = "";

            string singleVOText = "";

            var pathName = protoEnum.type;

            string str = "";

            var list_param = protoEnum.memberList;
            for (int i = 0; i < list_param.Count; i++)
            {
                var param = list_param[i];
                var memberName = param.name;

                str += codeTemplate.getEnumDefinition(param.name, param.value.ToString(), param.comment);

            }

            var fileClassName = codeTemplate.getFinalClassName(pathName);

            var typeName = protoEnum.type;
            singleVOText = codeTemplate.getProtocolEnumClass(typeName, str, protoEnum.comment);

            if (singleMode)
                text += singleVOText;
            else
                FileUtil.writeFile(voFolderPath + fileClassName + codeTemplate.ClassExtension, Encoding.UTF8.GetBytes(singleVOText));


            return text;
        }