Beispiel #1
0
        private void RenderProtocol(MessageDic mdic, Config config)
        {
            string protocolLiquid = string.Empty;

            if (string.IsNullOrEmpty(config.LiquidProtocol))
            {
                protocolLiquid = ReadFromResource("protocol.liquid");
            }
            else
            {
                protocolLiquid = File.ReadAllText(config.LiquidInterface);
            }

            // render protocol
            Template transferTemplate = Template.Parse(protocolLiquid);
            Template libraryTemplate  = Template.Parse(ReadFromResource("ProtocolLibrary.cs"));

            foreach (string dir in new string[] { config.OutputProtocol, config.OutputDir })
            {
                if (string.IsNullOrWhiteSpace(dir))
                {
                    continue;
                }

                Directory.CreateDirectory(dir);
                foreach (KeyValuePair <string, MessagePack> m in mdic._dic)
                {
                    string name = m.Key;
                    string mmm  = transferTemplate.Render(Hash.FromAnonymousObject(new
                    {
                        name,
                        message_namespace   = config.NamespaceMessage,
                        interface_namespace = config.NamespaceInterface,
                        protocol_namespace  = config.NamespaceProtocol,
                        q = m.Value.Request,
                        r = m.Value.Response
                    }));
                    string outputFpath = $"{dir}/{name}.protocol.autogen.cs";
                    File.WriteAllText(outputFpath, mmm);
                }


                string lll = libraryTemplate.Render(Hash.FromAnonymousObject(new
                {
                    interface_namespace = config.NamespaceInterface,
                    protocol_namespace  = config.NamespaceProtocol,
                }));
                File.WriteAllText($"{dir}/ProtocolLibrary.cs", lll);
            }
        }
Beispiel #2
0
        private void Run(Options opt)
        {
            string messageDll      = opt.MessageDll;
            string outputInterface = opt.InterfaceOutput;
            string outputProtocol  = opt.ProtocolOutput;
            string outDir          = opt.Output;

            if (string.IsNullOrEmpty(outputInterface) &&
                string.IsNullOrEmpty(outputProtocol) &&
                string.IsNullOrEmpty(outDir))
            {
                Console.Error.WriteLine("output directory required.");
                return;
            }

            Config config;

            if (string.IsNullOrEmpty(opt.Config))
            {
                config = new Config();
            }
            else
            {
                Deserializer deserializer =
                    new DeserializerBuilder()
                    .WithNamingConvention(new CamelCaseNamingConvention())
                    .Build();
                config = deserializer.Deserialize <Config>(File.ReadAllText(opt.Config));
            }

            if (!string.IsNullOrWhiteSpace(opt.MessageDll))
            {
                config.MessageDll = messageDll;
            }
            if (!string.IsNullOrWhiteSpace(opt.InterfaceOutput))
            {
                config.OutputInterface = opt.InterfaceOutput;
            }
            if (!string.IsNullOrWhiteSpace(opt.ProtocolOutput))
            {
                config.OutputProtocol = opt.ProtocolOutput;
            }
            if (!string.IsNullOrWhiteSpace(opt.Output))
            {
                config.OutputDir = opt.Output;
            }
            // get mdic
            List <Type> types = new List <Type>();

            foreach (Type type in Assembly.LoadFrom(messageDll).GetTypes())
            {
                if (!type.IsClass)
                {
                    continue;
                }

                Type imessage = type.GetInterface("IMessage");
                if (imessage == null)
                {
                    continue;
                }

                if (imessage.FullName != "Google.Protobuf.IMessage")
                {
                    continue;
                }

                types.Add(type);
            }

            if (string.IsNullOrEmpty(config.NamespaceMessage))
            {
                config.NamespaceMessage = types.First().Namespace;
            }

            List <MessageInfo> messageInfos =
                types
                .Select(x => ExtractRequestMessageInfo(x, config.RegRequest))
                .Where(x => x != null)
                .Concat(
                    types
                    .Select(x => ExtractResponseMessageInfo(x, config.RegResponse))
                    .Where(x => x != null)
                    )
                .ToList();


            MessageDic mdic = new MessageDic(messageInfos);

            // render interface
            RenderInterface(mdic, config);
            RenderProtocol(mdic, config);
        }