Beispiel #1
0
        private string GenStructCsCode(ProtoStruct cstruct)
        {
            #region template
            var template =
                @"    /// <summary>
    /// [NODE]
    /// </summary>
    public class [NAME] : Proto.ISerializerable
    {
        public [NAME]()
        {
[INITS]
        }
[FIELDS]
        public void ParseFormBinary(BinaryReader reader)
        {
[BRS]             
        }

        public void ToBinary(BinaryWriter writer)
        {
[BWS]            
        }

    }";
            #endregion

            var structCode = template.Replace("[NAME]", cstruct.Name);
            var sbFields   = new StringBuilder();
            var sbInits    = new StringBuilder();
            var sbBr       = new StringBuilder();
            var sbBw       = new StringBuilder();

            foreach (var i in cstruct.Fields)
            {
                sbFields.AppendLine(GenFileCsCode(i));
                if (i.IsArray)
                {
                    sbInits.AppendLine(
                        @"            [NAME] = new List<[TYPE]>();"
                        .Replace("[NAME]", i.Name).Replace("[TYPE]", i.Type));
                }
                else
                {
                    switch (i.Type)
                    {
                    case ProtoTypes.STRING:
                        sbInits.AppendLine("            [NAME] = string.Empty;".Replace("[NAME]", i.Name));
                        break;

                    case ProtoTypes.INT_32:
                    case ProtoTypes.BOOL:
                    case ProtoTypes.BYTE:
                    case ProtoTypes.CHAR:
                    case ProtoTypes.FLOAT:
                    case ProtoTypes.SHORT:
                    case ProtoTypes.INT_64:
                        break;

                    default:
                        if (!searchenums.Contains(i.Type))
                        {
                            sbInits.AppendLine((@"            [NAME] = new [TYPE]();".Replace("[NAME]", i.Name)
                                                .Replace("[TYPE]", i.Type)));
                        }
                        break;
                    }
                }
                sbBr.AppendLine(GenFieldBrCode(i));
                sbBw.AppendLine(GenFieldBwCode(i));
            }
            return(structCode.Replace("[INITS]", sbInits.ToString())
                   .Replace("[FIELDS]", sbFields.ToString())
                   .Replace("[BRS]", sbBr.ToString())
                   .Replace("[BWS]", sbBw.ToString())
                   .Replace("[NODE]", cstruct.Node));
        }
Beispiel #2
0
        public void ReadLine(string line)
        {
            var code = line.Trim();

            if (code.StartsWith("{"))
            {
                return;                      //开始
            }
            if (code.StartsWith("/"))
            {
                Comm = code.Replace("/", "");
                return;               //注释
            }
            if (code.StartsWith("}")) //结束
            {
                CurrentEnum   = null;
                CurrentStruct = null;
                return;
            }


            #region Enum
            if (CurrentEnum != null)
            {
                int    indexEqual = code.IndexOf("=");
                string enumFileName, enumFileValue, enumFieldNote;
                if (indexEqual > -1)
                {
                    enumFileName  = code.Split('=')[0].Trim();
                    enumFileValue = code.Substring(indexEqual + 1).Split("//".ToArray())[0].Trim("\t ;".ToArray());
                    var tempNotes = code.Split("//".ToArray());
                    var tempSb    = new StringBuilder();
                    for (var i = 1; i < tempNotes.Length; i++)
                    {
                        tempSb.Append(tempNotes[i].Trim("\t".ToArray()));
                    }
                    enumFieldNote = tempSb.ToString();
                    CurrentEnum.AddField(new ProtoEnumField {
                        Name = enumFileName, Value = enumFileValue, Note = enumFieldNote
                    });
                }
            }
            #endregion

            var args = code.Split(" \t\\;".ToArray());
            //Console.WriteLine(code);
            var notes       = code.Split("//".ToArray());
            var tempFieldSb = new StringBuilder();
            for (var i = 1; i < notes.Length; i++)
            {
                tempFieldSb.Append(notes[i].Trim());
            }
            //Console.WriteLine(tempFieldSb.ToString());
            switch (args[0])
            {
            case "package":
                Package = args[1].Trim();
                break;

            case "import":
                var importFile = args[1].Trim("\'\" \t\\;".ToArray());
                LoadEnumTypes(importFile);
                break;

            case "message":
                CurrentStruct = new ProtoStruct(args[1], Comm);
                Structs.Add(CurrentStruct);
                break;

            case "enum":
                CurrentEnum = new ProtoEnum(args[1], Comm);
                Enums.Add(CurrentEnum);
                break;

            case "optional":
            case "required":
            case "repeated":
                var fieldType = args[1];
                var fieldName = args[2].Split("\t\\;".ToArray())[0].Trim();
                var fieldNote = string.Empty;
                fieldNote = tempFieldSb.ToString();
                if (CurrentStruct == null)
                {
                    throw new Exception("解析错误:" + code);
                }
                CurrentStruct.AddField(new ProtoField
                {
                    IsArray = args[0] == "repeated",
                    Name    = fieldName,
                    Type    = fieldType,
                    Note    = fieldNote
                });
                break;
            }
            Comm = string.Empty;
        }