string tName(Type t)
        {
            var tname = TypeTranslator.getTypeName(t);

            if (!group.havingTypes.Contains(t))
            {
                foreach (var dep in group.dependencies)
                {
                    if (dep.havingTypes.Contains(t))
                    {
                        return($"{dep.Name}::{tname}");
                    }
                }
            }

            return(tname);
        }
        public void PrintEnumType()
        {
            string title = $"enum class {enumSchema.Name}";

            var underlying = TypeTranslator.getTypeName(enumSchema.underlying);

            if (underlying != null)
            {
                title += $" : {underlying}";
            }

            using (c.type(title))
            {
                foreach (var enumName in enumSchema.EnumNames)
                {
                    c.prn($"{enumName}, ");
                }
            }
        } //PrintEnumType
        public void PrintMessage()
        {
            using (c.type($"struct {msg.Name}"))
            {
                if (msg.Id >= 0)
                {
                    var uint32 = TypeTranslator.getTypeName(typeof(UInt32));

                    c.prn($"const static {uint32} PacketId = ({uint32})PacketType::{msg.Name};");
                    c.prn();
                }

                prnConstructors();
                c.prn();

                prnMembers();
                c.prn();

                prnCereal();
            }
        } //PrintMessage
Exemple #4
0
        public void GenerateCode(string dirpath, string includeFile = null)
        {
            string dirFullPath = Path.GetFullPath(dirpath);

            if (!Directory.Exists(dirFullPath))
            {
                Directory.CreateDirectory(dirFullPath);
            }

            string includeContents = null;

            if (includeFile != null)
            {
                using (var fs = File.Open(includeFile, FileMode.Open))
                {
                    using (var sr = new StreamReader(fs))
                    {
                        includeContents = sr.ReadToEnd();
                    }
                }
            }

            foreach (var group in SchemaBox.GetSchemas())
            {
                var filepath = dirFullPath + "/" + getFileName(group.Name);
                using (var fs = File.Create(filepath))
                    using (var sw = new StreamWriter(fs))
                    {
                        var c = new CodeWriter(sw);
                        c.prn("#pragma once");
                        c.prn();

                        // include
                        if (includeContents != null)
                        {
                            c.prn(includeContents);
                            c.prn();
                        }

                        // dependencies
                        foreach (var dep in group.dependencies)
                        {
                            c.prn($"#include \"{getFileName(dep.Name)}\"");
                        }
                        c.prn();

                        // file
                        using (c.func($"namespace {group.Name}"))
                        {
                            var uint32 = TypeTranslator.getTypeName(typeof(UInt32));
                            // PacketType
                            using (c.type($"enum class PacketType : {uint32}"))
                            {
                                foreach (var msgT in group.messageMap)
                                {
                                    c.prn($"{msgT.Value.Name}, ");
                                }
                                c.prn("MAX");
                            }
                            c.prn();

                            // enums
                            foreach (var eT in group.enumTypes)
                            {
                                EnumWriter writer = new EnumWriter(c, eT);
                                writer.PrintEnumType();
                                c.prn("");
                            }

                            // messages
                            foreach (var msgT in group.messageMap.Values)
                            {
                                MessageWriter writer = new MessageWriter(c, group, msgT);
                                writer.PrintMessage();
                                c.prn("");
                            }
                        }
                    }

                Console.WriteLine($"{group.Name} generated ({filepath})");
            }
        } //GenerateCode