ProtocolBuffers.MessageCode.GenerateClass C# (CSharp) Method

GenerateClass() public method

public GenerateClass ( Message m ) : string
m Message
return string
        public virtual string GenerateClass(Message m)
        {
            string code = "";

            //Default class
            if (m.Comments != null) {
                code += "/// <summary>\n";
                code += Code.Prefix ("/// ", m.Comments) + "\n";
                code += "/// </summary>\n";
            }
            code += m.OptionAccess + " partial class " + m.CSType + "\n";
            code += "{\n";

            string enums = GenerateEnums (m);
            if (enums.Length > 0) {
                code += Code.Indent (enums);
                code += "\n";
            }

            code += Code.Indent (GenerateProperties (m));
            code += "\n";

            if (m.OptionTriggers) {
                code += Code.Indent (Code.Comment (
                    "protected virtual void BeforeSerialize() {}\n" +
                    "protected virtual void AfterDeserialize() {}\n"));
                code += "\n";
            }

            foreach (Message sub in m.Messages) {
                code += Code.Indent (GenerateClass (sub));
                code += "\n";
            }
            code = code.TrimEnd ('\n');
            code += "\n}\n";
            return code;
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Generate code for reading and writing protocol buffer messages
        /// </summary>
        public static void Save(Proto proto, MessageCode codeGen, string csPath)
        {
            string ext = Path.GetExtension (csPath);
            string prefix = csPath.Substring (0, csPath.Length - ext.Length);

            //Basic structures
            using (TextWriter codeWriter = new StreamWriter(csPath, false, Encoding.UTF8)) {
                codeWriter.WriteLine (@"//
            //	You may customize this code as you like
            //	Report bugs to: https://silentorbit.com/protobuf/
            //
            //	Generated by ProtocolBuffer
            //	- a pure c# code generation implementation of protocol buffers
            //

            using System;
            using System.Collections.Generic;
            ");

                foreach (Message m in proto.Messages) {
                    codeWriter.WriteLine ("namespace " + m.Namespace);
                    codeWriter.WriteLine ("{");
                    codeWriter.WriteLine (Code.Indent (1, codeGen.GenerateClass (m)));
                    codeWriter.WriteLine ("}");
                }
            }

            //.Serializer.cs
            //Code for Reading/Writing
            using (TextWriter codeWriter = new StreamWriter(prefix + ".Serializer" + ext, false, Encoding.UTF8)) {
                codeWriter.WriteLine (@"//
            //	This is the backend code for reading and writing
            //	Report bugs to: https://silentorbit.com/protobuf/
            //
            //	Generated by ProtocolBuffer
            //	- a pure c# code generation implementation of protocol buffers
            //

            using System;
            using System.IO;
            using System.Text;
            using System.Collections.Generic;
            using ProtocolBuffers;");

                foreach (Message m in proto.Messages) {
                    codeWriter.WriteLine ("namespace " + m.Namespace);
                    codeWriter.WriteLine ("{");
                    codeWriter.WriteLine (Code.Indent (1, SerializerCode.GenerateClassSerializer (m)));
                    codeWriter.WriteLine ("}");
                }

                codeWriter.WriteLine (@"
            namespace ProtocolBuffers
            {
            public static partial class Serializer
            {");

                foreach (Message m in proto.Messages)
                    codeWriter.WriteLine (Code.Indent (2, SerializerCode.GenerateGenericClassSerializer (m)));

                codeWriter.WriteLine (@"
            }
            }");
            }

            string libPath = Path.Combine (Path.GetDirectoryName (csPath), "ProtocolParser.cs");
            using (TextWriter codeWriter = new StreamWriter(libPath, false, Encoding.UTF8)) {
                ReadCode (codeWriter, "ProtocolParser", true);
                ReadCode (codeWriter, "ProtocolParserFixed", false);
                ReadCode (codeWriter, "ProtocolParserKey", false);
                ReadCode (codeWriter, "ProtocolParserVarInt", false);
                ReadCode (codeWriter, "ProtocolParserCustom", false);
            }
        }