示例#1
0
        public string GenerateCode(BinaryFileSchema schema)
        {
            StringBuilder b = new StringBuilder();
            List<CodeClass> codeClasses = new List<CodeClass>();
            FormatName = schema.FormatBlock.Name;

            b.AppendLine("using System;");
            b.AppendLine("using System.Collections.Generic;");
            b.AppendLine("using System.IO;");
            b.AppendLine("using System.Text;");
            b.AppendLine("namespace " + schema.FormatBlock.Name);
            b.AppendLine("{");

            CodeClass mainClass = new CodeClass(schema.FormatBlock.Name + "Parser");
            codeClasses.Add(mainClass);
            rootParser = mainClass;

            CodeMethod mainMethod = new CodeMethod("void Main(string[] args)");
            mainMethod.CodeLines.Add("try{");
            mainMethod.CodeLines.Add("\t" + schema.FormatBlock.Name + " ps = " + schema.FormatBlock.Name + "Parser.ReadFile(args[0]);");

            mainMethod.CodeLines.Add("\tConsole.WriteLine(ps.ToString());");

            mainMethod.CodeLines.Add("}");
            mainMethod.CodeLines.Add("catch(Exception e){");
            mainMethod.CodeLines.Add("\tConsole.WriteLine(e.Message);");
            mainMethod.CodeLines.Add("\tConsole.WriteLine(e.StackTrace);");
            mainMethod.CodeLines.Add("}");
            mainClass.CodeMethods.Add(mainMethod);

            CodeMethod readFile = new CodeMethod( schema.FormatBlock.Name + " ReadFile(string filename)");
            mainClass.CodeMethods.Add(readFile);

            readFile.CodeLines.Add("FileStream fstream = new FileStream(filename, FileMode.Open);");
            readFile.CodeLines.Add("BinaryReader reader = new BinaryReader(fstream);");
            string endianness = ((schema.ByteOrder.ByteOrder == BfsByteOrderEnum.BigEndian) ? BfsByteOrderEnum.BigEndian : BfsByteOrderEnum.LittleEndian).ToString();
            readFile.CodeLines.Add("BfsBinaryReader bfsReader = new BfsBinaryReader(reader, BfsBinaryReader.Endianness." + endianness + ");");
            readFile.CodeLines.Add("return " + schema.FormatBlock.Name + ".Read(bfsReader);");

            foreach (IBfsDataBlock dataBlock in schema.DatablockList)
            {
                CodeClass newClass = MakeClass(dataBlock);
                if(newClass != null)
                    codeClasses.Add(newClass);
            }

            //Output the classes
            foreach (CodeClass cc in codeClasses)
                b.Append(cc.ToString());

            b.AppendLine("}");
            return b.ToString();
        }
示例#2
0
        public void Check( BinaryFileSchema schema )
        {
            this.schema = schema;

            foreach (IBfsDataBlock block in schema.DatablockList)
            {
                //Generate map over unresolved expression variables;
                foreach (BfsLocalField field in block.LocalFields.Values)
                    if (field.AssignmentExpression != null)
                        CheckExpression(field.AssignmentExpression, block);

                if (block is IBfsStructType)
                {
                    IBfsStructType structType = (IBfsStructType)block;

                    foreach (BfsStructField field in structType.StructFields.Values)
                    {
                        //Check conditional expressions
                        if (field.Conditional != null)
                            CheckExpression(field.Conditional, block);

                        if (field.FieldType.ArrayExtension != null)
                            if(field.FieldType.ArrayExtension is BfsKnownArray)
                                CheckExpression((field.FieldType.ArrayExtension as BfsKnownArray).Expression, block);
                            //Check that no unknown sized arrays are located within any structure that is compressed (seeking not allowed)
                            else if(field.FieldType.ArrayExtension is BfsUnknownArray && structType.CompressionMethod != null)
                                BfsCompiler.ReportError(field.FieldType.ArrayExtension.SourceRange,
                                    "Cannot have unknown sized arrays within compressed blocks: '" + field.Name + "'");

                    }
                }
                else if (block is BfsEnum)
                {
                    foreach( BfsEnumField field in (block as BfsEnum).EnumFields)
                        if(field.Actions != null)
                            foreach( IBfsAction action in field.Actions )
                                if( action is BfsActionAssignment )
                                    CheckExpression((action as BfsActionAssignment).Expression, block);
                }
                else if (block is BfsBitfield)
                {
                    foreach (BfsBitfieldField field in (block as BfsBitfield).BitFieldFields)
                        if (field.Actions != null)
                            foreach (IBfsAction action in field.Actions)
                                if (action is BfsActionAssignment)
                                    CheckExpression((action as BfsActionAssignment).Expression, block);
                }
            }
        }