示例#1
0
        private static bool Read_Header(
            XmlReader reader,
            ref ManifestHeader header)
        {
            try
            {
                var elementName = string.Empty;
                var isExit      = false;
                while (!isExit)
                {
                    {
                        var task = reader.ReadAsync();
                        task.Wait();
                        if (!task.Result)
                        {
                            break;
                        }
                    }
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        elementName = reader.Name;
                        break;

                    case XmlNodeType.EndElement:
                        if ("header" == reader.Name)
                        {
                            isExit = true;
                        }
                        break;

                    case XmlNodeType.Text:
                    {
                        var task = reader.GetValueAsync();
                        task.Wait();
                        switch (elementName)
                        {
                        case "version":
                            if (!float.TryParse(task.Result, out header.version))
                            {
                                Console.WriteLine("Manifest Error(header): \"version\" element is invalid");
                                goto Failed;
                            }
                            break;

                        case "structName":
                            header.structName = task.Result;
                            break;

                        default:
                            Console.WriteLine("Manifest Error(header): \"{0}\" element is unknown", elementName);
                            goto Failed;
                        }
                    }
                    break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Manifest Error(header): {0}", e.ToString());
                goto Failed;
            }

            if (1.0f != header.version)
            {
                Console.WriteLine("Manifest Error(header): \"version\" element is invalid\n");
                goto Failed;
            }
            if (null == header.structName)
            {
                Console.WriteLine("Manifest Error(header): \"structName\" element must be required\n");
                goto Failed;
            }

            return(true);

Failed:
            return(false);
        }
示例#2
0
        public static bool IsValid(
            ref ManifestHeader header,
            ref List <ManifestContent> contents)
        {
            if (1.0f != header.version)
            {
                Console.WriteLine("Manifest Error(header): version \"{0:F1}\" is invalid", header.version);
                return(false);
            }
            if (null == header.structName || string.Empty == header.structName)
            {
                Console.WriteLine("Manifest Error(header): structName is invalid");
                return(false);
            }

            var contentsCount = contents.Count;

            for (var i = 0; i < contentsCount; ++i)
            {
                {
                    var isValid = true;
                    if (ValueType.utf16 == contents[i].valueType)
                    {
                        if (0 >= contents[i].length)
                        {
                            isValid = false;
                        }
                    }
                    else if (ValueType.bits32 == contents[i].valueType)
                    {
                        if (0 > contents[i].length /*0はビットフィールド強制スプリットで許可*/ || 15 /*BitVector32のSection引数制限*/ / < contents[i].length)
                        {
                            isValid = false;
                        }
                    }
                    else
                    {
                        if (0 != contents[i].length)
                        {
                            isValid = false;
                        }
                    }

                    if (!isValid)
                    {
                        Console.WriteLine("Manifest Error(content No.{0}): length \"{1}\" is invalid", i + 1, contents[i].length);
                        return(false);
                    }
                }

                {
                    if (ValueType.bits32 != contents[i].valueType)
                    {
                        if (null != contents[i].structBitsName && contents[i].structBitsName != string.Empty)
                        {
                            Console.WriteLine("Manifest Error(content No.{0}): structBitsName \"{1}\" must be empty for bits32 type", i + 1, contents[i].structBitsName);
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
示例#3
0
        public static bool Read(
            string filePath,
            ref ManifestHeader header,
            ref List <ManifestContent> contents)
        {
            contents.Clear();
            try
            {
                var settings = new XmlReaderSettings();
                settings.Async = true;

                using (var streamReader = new StreamReader(filePath, Encoding.UTF8))
                {
                    using (var reader = XmlReader.Create(streamReader, settings))
                    {
                        while (true)
                        {
                            {
                                var task = reader.ReadAsync();
                                task.Wait();
                                if (!task.Result)
                                {
                                    break;
                                }
                            }
                            switch (reader.NodeType)
                            {
                            case XmlNodeType.Element:
                                switch (reader.Name)
                                {
                                case "header":
                                    if (!Read_Header(reader, ref header))
                                    {
                                        goto Failed;
                                    }
                                    break;

                                case "content":
                                {
                                    var content = new ManifestContent();
                                    if (!Read_Content(reader, ref content))
                                    {
                                        goto Failed;
                                    }
                                    contents.Add(content);
                                }
                                break;

                                case "root":
                                    // NOP
                                    break;

                                default:
                                    Console.WriteLine("Manifest Error(root): \"{0}\" element is unknown", reader.Name);
                                    goto Failed;
                                }
                                break;

                            case XmlNodeType.Text:
                            {
                                var task = reader.GetValueAsync();
                                task.Wait();
                            }
                            break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                goto Failed;
            }
            return(true);

Failed:
            contents.Clear();
            return(false);
        }
示例#4
0
文件: Program.cs 项目: Lazy-K/Csv2Bin
        static int Main(string[] args)
        {
            if (!AppCommandLine.Parser.Parse(args, ref _commandLineOption))
            {
                return(1);
            }

            if (null != _commandLineOption.outputLogFilePath)
            {
                if (File.Exists(_commandLineOption.outputLogFilePath))
                {
                    File.Delete(_commandLineOption.outputLogFilePath);
                }
                _logFile = File.CreateText(_commandLineOption.outputLogFilePath);
                Console.SetOut(_logFile);
            }

            var manifestHeader   = new ManifestHeader();
            var manifestContents = new List <ManifestContent>();

            {             // Read manifest file
                if (!Manifest.Read(
                        _commandLineOption.manifestFilePath,
                        ref manifestHeader,
                        ref manifestContents))
                {
                    goto Failed;
                }
            }

#if false     // TEST
            { // Write manifest file
                if (!Manifest.Write(
                        _commandLineOption.manifestFilePath + "_test.xml",
                        manifestHeader,
                        manifestContents))
                {
                    goto Failed;
                }
            }
            return(0);
#endif

            if (null != _commandLineOption.tableFilePath && null != _commandLineOption.outputBinaryFilePath)
            {
                List <byte> binary;
                UInt32      numRecords;
                {                 // Read table file and convert binary by manifest
                    if (!Manifest.GenerateBinary(
                            _commandLineOption.tableFilePath,
                            manifestContents,
                            out binary,
                            out numRecords))
                    {
                        goto Failed;
                    }
                }

                {                 // Write binary file
                    try
                    {
                        using (var writer = new BinaryWriter(new FileStream(_commandLineOption.outputBinaryFilePath, FileMode.Create)))
                        {
                            writer.Write(binary.ToArray());
                            if (_commandLineOption.isAppendSummary)
                            {                             // Append summary
                                UInt32 size = (UInt32)binary.Count / numRecords;
                                writer.Write(size);
                                writer.Write(numRecords);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Output Binary File Error: \"{0}\"", e.ToString());
                        goto Failed;
                    }
                }
            }

            if (null != _commandLineOption.outputCsFilePath)
            {
                // Generate code file
                try
                {
                    var code = Manifest.GenerateCode(
                        manifestHeader,
                        manifestContents);
                    File.WriteAllText(_commandLineOption.outputCsFilePath, code);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Output CShape File Error: \"{0}\"", e.ToString());
                    goto Failed;
                }
            }

            FinalizeLogFile();
            return(0);

Failed:
            FinalizeLogFile();
            return(1);
        }