Ejemplo n.º 1
0
        private static void GenerateHead(StringBuilder sb, HeadFlag flag, string opcodeClassName)
        {
            sb.AppendLine("#if SERVER");
            sb.AppendLine("namespace Model\n{");
            foreach (string parentClass in parentMsg.GetDictionary().Keys)
            {
                if ((flag & HeadFlag.Bson) != 0)
                {
                    foreach (string s in parentMsg.GetAll(parentClass))
                    {
                        sb.Append($"\t[BsonKnownTypes(typeof({s}))]\n");
                    }
                }


                sb.Append($"\tpublic partial class {parentClass} {{}}\n\n");
            }
            sb.AppendLine("}");
            sb.AppendLine("#endif");

            sb.AppendLine("namespace Model\n{");
            foreach (string parentClass in parentMsg.GetDictionary().Keys)
            {
                if ((flag & HeadFlag.Proto) != 0)
                {
                    foreach (string s in parentMsg.GetAll(parentClass))
                    {
                        sb.Append($"\t[ProtoInclude({opcodeClassName}.{s}, typeof({s}))]\n");
                    }
                }

                sb.Append($"\tpublic partial class {parentClass} {{}}\n\n");
            }
            sb.AppendLine("}");
        }
Ejemplo n.º 2
0
        private static void GenerateHead(StringBuilder sb, string ns, HeadFlag flag, string opcodeClassName)
        {
            sb.AppendLine($"namespace {ns}");
            sb.AppendLine("{");
            foreach (string parentClass in parentMsg.GetDictionary().Keys)
            {
                if ((flag & HeadFlag.Bson) != 0)
                {
                    foreach (string s in parentMsg.GetAll(parentClass))
                    {
                        sb.Append($"\t[BsonKnownTypes(typeof({s}))]\n");
                    }
                }


                sb.Append($"\tpublic partial class {parentClass} {{}}\n\n");
            }
            sb.AppendLine("}");
        }
Ejemplo n.º 3
0
        private static void GenerateHead(StringBuilder sb, HeadFlag flag)
        {
            foreach (string parentClass in parentMsg.GetDictionary().Keys)
            {
                if ((flag & HeadFlag.Proto) != 0)
                {
                    foreach (string s in parentMsg.GetAll(parentClass))
                    {
                        sb.Append($"\t[ProtoInclude((int)Opcode.{s}, typeof({s}))]\n");
                    }
                }

                if ((flag & HeadFlag.Bson) != 0)
                {
                    foreach (string s in parentMsg.GetAll(parentClass))
                    {
                        sb.Append($"\t[BsonKnownTypes(typeof({s}))]\n");
                    }
                }


                sb.Append($"\tpublic partial class {parentClass} {{}}\n\n");
            }
        }
Ejemplo n.º 4
0
        public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode, HeadFlag flag, bool isClient = true)
        {
            msgOpcode.Clear();
            parentMsg = new MultiMap <string, string>();
            string proto  = Path.Combine(protoPath, protoName);
            string csPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(proto) + ".cs");

            string s = File.ReadAllText(proto);

            StringBuilder sb = new StringBuilder();

            sb.Append("using ProtoBuf;\n");
            sb.Append("using ETModel;\n");
            sb.Append("using System.Collections.Generic;\n");
            sb.Append("using MongoDB.Bson.Serialization.Attributes;\n");
            sb.Append($"namespace {ns}\n");
            sb.Append("{\n");

            bool   isMsgStart  = false;
            string parentClass = "";

            foreach (string line in s.Split('\n'))
            {
                string newline = line.Trim();

                if (newline == "")
                {
                    continue;
                }

                if (newline.StartsWith("//"))
                {
                    sb.Append($"{newline}\n");
                }

                if (newline.StartsWith("message"))
                {
                    parentClass = "";
                    isMsgStart  = true;
                    string   msgName = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
                    string[] ss      = newline.Split(new [] { "//" }, StringSplitOptions.RemoveEmptyEntries);

                    if (ss.Length == 2)
                    {
                        parentClass = ss[1].Trim();
                    }

                    msgOpcode.Add(new OpcodeInfo()
                    {
                        Name = msgName, Opcode = ++startOpcode
                    });

                    sb.Append($"\t[Message({opcodeClassName}.{msgName})]\n");
                    sb.Append($"\t[ProtoContract]\n");
                    sb.Append($"\tpublic partial class {msgName}");
                    if (parentClass == "IActorMessage" || parentClass == "IActorRequest" || parentClass == "IActorResponse" || parentClass == "IFrameMessage")
                    {
                        sb.Append($": {parentClass}\n");
                    }
                    else if (parentClass != "")
                    {
                        sb.Append($": {parentClass}\n");
                    }
                    else
                    {
                        sb.Append("\n");
                    }
                }

                if (isMsgStart && newline == "{")
                {
                    sb.Append("\t{\n");

                    if (parentClass == "IRequest" || parentClass == "IActorRequest")
                    {
                        sb.AppendLine("\t\t[ProtoMember(90, IsRequired = true)]");
                        sb.AppendLine("\t\tpublic int RpcId { get; set; }");
                    }

                    if (parentClass == "IResponse" || parentClass == "IActorResponse")
                    {
                        sb.AppendLine("\t\t[ProtoMember(90, IsRequired = true)]");
                        sb.AppendLine("\t\tpublic int Error { get; set; }");
                        sb.AppendLine("\t\t[ProtoMember(91, IsRequired = true)]");
                        sb.AppendLine("\t\tpublic string Message { get; set; }");
                        sb.AppendLine("\t\t[ProtoMember(92, IsRequired = true)]");
                        sb.AppendLine("\t\tpublic int RpcId { get; set; }");
                    }

                    if (parentClass == "IFrameMessage")
                    {
                        sb.AppendLine("\t\t[ProtoMember(92, IsRequired = true)]");
                        sb.AppendLine("\t\tpublic long Id { get; set; }");
                    }
                }

                // 成员
                if (newline.StartsWith("required"))
                {
                    Members(sb, newline, true);
                }

                if (newline.StartsWith("optional"))
                {
                    Members(sb, newline, false);
                }

                if (newline.StartsWith("repeated"))
                {
                    Repeated(sb, ns, newline, isClient);
                }

                if (isMsgStart && newline == "}")
                {
                    isMsgStart = false;
                    sb.Append("\t}\n\n");
                }
            }
            sb.Append("}\n");

            //if (!isClient)
            //{
            //GenerateHead(sb, ns, flag, opcodeClassName);
            //}

            File.WriteAllText(csPath, sb.ToString());
        }
Ejemplo n.º 5
0
        public static void Proto2CS(string protoName, string outputPath, int startOpcode, HeadFlag flag)
        {
            parentMsg = new MultiMap <string, string>();
            string proto  = Path.Combine(protoPath, protoName);
            string csPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(proto) + ".cs");

            string s = File.ReadAllText(proto);

            StringBuilder sb = new StringBuilder();

            sb.Append("using ProtoBuf;\n");
            sb.Append("using System.Collections.Generic;\n");
            sb.Append("using MongoDB.Bson.Serialization.Attributes;\n");
            sb.Append("namespace Model\n");
            sb.Append("{\n");

            bool isMsgStart = false;

            foreach (string line in s.Split('\n'))
            {
                string newline = line.Trim();

                if (newline == "")
                {
                    continue;
                }

                if (newline.StartsWith("//"))
                {
                    sb.Append($"{newline}\n");
                }

                if (newline.StartsWith("message"))
                {
                    isMsgStart = true;
                    string   msgName     = newline.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)[1];
                    string[] ss          = newline.Split(new [] { "//" }, StringSplitOptions.RemoveEmptyEntries);
                    string   parentClass = "";
                    if (ss.Length == 2)
                    {
                        parentClass = ss[1];
                        parentMsg.Add(parentClass, msgName);
                    }

                    msgOpcode.Add(new OpcodeInfo()
                    {
                        Name = msgName, Opcode = ++startOpcode
                    });

                    sb.Append($"\t[Message(Opcode.{msgName})]\n");
                    sb.Append($"\t[ProtoContract]\n");
                    sb.Append($"\tpublic partial class {msgName}");
                    if (parentClass != "")
                    {
                        sb.Append($": {parentClass}\n");
                    }
                    else
                    {
                        sb.Append("\n");
                    }
                }

                if (isMsgStart && newline == "{")
                {
                    sb.Append("\t{\n");
                }

                // 成员
                if (newline.StartsWith("required"))
                {
                    Members(sb, newline, true);
                }

                if (newline.StartsWith("optional"))
                {
                    Members(sb, newline, false);
                }

                if (newline.StartsWith("repeated"))
                {
                    Repeated(sb, newline);
                }

                if (isMsgStart && newline == "}")
                {
                    isMsgStart = false;
                    sb.Append("\t}\n\n");
                }
            }
            GenerateHead(sb, flag);
            sb.Append("}\n");

            File.WriteAllText(csPath, sb.ToString());
        }