private static TemplateLibrary LoadFromXml(TemplateLibraryXML.TemplateLibraryXML xml)
        {
            var lib = new TemplateLibrary();

            lib.FileTemplate         = new Template().LoadFromFile(xml.FileTemplate);
            lib.GenericTypeTemplates = new TypeTemplates
            {
                ReadCall          = new Template().LoadFromFile(xml.DefaultTypeTemplates.ReadCall),
                ReadFunction      = new Template().LoadFromFile(xml.DefaultTypeTemplates.ReadFunction),
                WriteCall         = new Template().LoadFromFile(xml.DefaultTypeTemplates.WriteCall),
                WriteFunction     = new Template().LoadFromFile(xml.DefaultTypeTemplates.WriteFunction),
                TypeFieldTemplate = new Template().LoadFromFile(xml.DefaultTypeTemplates.TypeFieldTemplate),
                TypeTemplate      = new Template().LoadFromFile(xml.DefaultTypeTemplates.TypeTemplate),
                TypeNameTemplate  = new Template().LoadFromFile(xml.DefaultTypeTemplates.TypeNameTemplate)
            };

            foreach (var customTypeTemplate in xml.CustomTypeTemplates)
            {
                lib.CustomTypeTemplates.Add(customTypeTemplate.Type, new TypeTemplates
                {
                    ReadCall          = new Template().LoadFromFile(customTypeTemplate.ReadCall),
                    ReadFunction      = new Template().LoadFromFile(customTypeTemplate.ReadFunction),
                    WriteCall         = new Template().LoadFromFile(customTypeTemplate.WriteCall),
                    WriteFunction     = new Template().LoadFromFile(customTypeTemplate.WriteFunction),
                    TypeFieldTemplate = new Template().LoadFromFile(customTypeTemplate.TypeFieldTemplate),
                    TypeTemplate      = new Template().LoadFromFile(customTypeTemplate.TypeTemplate),
                    TypeNameTemplate  = new Template().LoadFromFile(customTypeTemplate.TypeNameTemplate)
                });
            }

            return(lib);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            if (HasFlag("-h", args))
            {
                Console.WriteLine("Message protocol code generator");
                Console.WriteLine();
                Console.WriteLine("-h Show this help page");
                Console.WriteLine("-i [path] Path to protocol xml file");
                Console.WriteLine("-t [path] Path to template library xml file");
                Console.WriteLine("-o [path] Path to output file");
                return;
            }

            var protocolFile    = GetArgument("-i", args);
            var templateLibFile = GetArgument("-t", args);
            var outputFile      = GetArgument("-o", args);

            if (protocolFile == null || templateLibFile == null || outputFile == null)
            {
                Console.WriteLine("Missing parameters, use -h to show help");
                return;
            }

            try
            {
                Console.WriteLine($"Input file: {protocolFile}");
                Console.WriteLine($"Template lib: {templateLibFile}");
                Console.WriteLine($"Output file: {outputFile}");

                var protocol = ProtocolLoader.LoadProtocol(protocolFile);

                var testThing = new TestThing();
                var modules   = testThing.FromProtocol(protocol);

                var templateLib = TemplateLibrary.LoadFromFile(templateLibFile);


                using (var writer = new StreamWriter(outputFile, false, Encoding.UTF8))
                {
                    testThing.WriteModule(writer, templateLib, modules[0]);
                }
                Console.WriteLine("Done!");
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(e.Message);
            }
        }
Exemple #3
0
        public void WriteModule(TextWriter output, TemplateLibrary library, Data.Module module)
        {
            var baseDict = new Dictionary <string, IInsertable>()
            {
                { "module_name", new StringLiteral(module.Name) },
            };

            var objectsContainer = new InsertableContainer()
            {
                AutoIndentAndNewline = true
            };

            foreach (var type in module.Types.Values)
            {
                if (!library.CustomTypeTemplates.TryGetValue(type.Name, out TemplateLibrary.TypeTemplates typeTemplates))
                {
                    typeTemplates = library.GenericTypeTemplates;
                }

                var typeDict = new NestedDictionary <string, IInsertable>(baseDict)
                {
                    { "obj_name", new StringLiteral(type.Name) },
                };
                typeDict.Add("obj_type", new TemplateInsert(typeTemplates.TypeNameTemplate, typeDict));


                // Functions
                var functionsContainer = new InsertableContainer()
                {
                    AutoIndentAndNewline = true
                };

                // Read and write functions for this type

                var readCallContainer = new InsertableContainer()
                {
                    AutoIndentAndNewline = true
                };

                var writeCallContainer = new InsertableContainer()
                {
                    AutoIndentAndNewline = true
                };

                // Fields
                var fieldsContainer = new InsertableContainer()
                {
                    AutoIndentAndNewline = true
                };

                foreach (var field in type.Fields)
                {
                    if (!library.CustomTypeTemplates.TryGetValue(field.Type.Name, out TemplateLibrary.TypeTemplates fieldTypeTemplates))
                    {
                        fieldTypeTemplates = library.GenericTypeTemplates;
                    }

                    var fieldDict = new NestedDictionary <string, IInsertable>(typeDict)
                    {
                        { "field_type", new TemplateInsert(fieldTypeTemplates.TypeNameTemplate, new NestedDictionary <string, IInsertable>(baseDict)
                            {
                                { "obj_name", new StringLiteral(field.Type.Name) }
                            }) },
                        { "field_name", new StringLiteral(field.Name) },
                        { "field_byte_offset", new StringLiteral(field.ByteOffset.ToString()) },
                        { "field_bit_offset", new StringLiteral(field.BitOffset.ToString()) },
                    };

                    readCallContainer.Insertables.Add(new TemplateInsert(fieldTypeTemplates.ReadCall, fieldDict));
                    writeCallContainer.Insertables.Add(new TemplateInsert(fieldTypeTemplates.WriteCall, fieldDict));
                    fieldsContainer.Insertables.Add(new TemplateInsert(typeTemplates.TypeFieldTemplate, fieldDict));
                }

                var readFuncDict = new NestedDictionary <string, IInsertable>(typeDict)
                {
                    { "read_fields", readCallContainer }
                };
                functionsContainer.Insertables.Add(new TemplateInsert(typeTemplates.ReadFunction, readFuncDict));

                var writeFuncDict = new NestedDictionary <string, IInsertable>(typeDict)
                {
                    { "write_fields", writeCallContainer }
                };
                functionsContainer.Insertables.Add(new TemplateInsert(typeTemplates.WriteFunction, writeFuncDict));


                typeDict.Add("fields", fieldsContainer);
                typeDict.Add("functions", functionsContainer);

                // Write out
                objectsContainer.Insertables.Add(new TemplateInsert(typeTemplates.TypeTemplate, typeDict));
            }

            var fileDict = new NestedDictionary <string, IInsertable>(baseDict)
            {
                { "constants", new StringLiteral("") },
                { "objects", objectsContainer }
            };

            library.FileTemplate.Apply(output, fileDict, 0);
        }