Пример #1
0
        public static string Decode(string path, out EncodeType encodeType, bool wellFormatted = false)
        {
            var packedFileName = UnifiedPath.GetFileName(path).ToLower();

            using (var stream = UnifiedFile.OpenRead(path))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    Int32 header = reader.ReadInt32();
                    if (header == PackedSection.PackedHeader)
                    {
                        reader.ReadSByte();

                        var document = new XmlDocument();

                        var packedSection = new PackedSection();
                        var dictionary    = packedSection.readDictionary(reader);
                        var xmlroot       = document.CreateNode(XmlNodeType.Element, packedFileName, "");
                        packedSection.readElement(reader, xmlroot, document, dictionary);
                        document.AppendChild(xmlroot);

                        encodeType = EncodeType.Packed;

                        if (wellFormatted)
                        {
                            return(XmlDecoder.FormatXml(document.OuterXml));
                        }
                        else
                        {
                            return(document.OuterXml);
                        }
                    }
                    else if (header == BinaryHeader)
                    {
                        stream.Seek(0, SeekOrigin.Begin);

                        var document = new XmlDocument();

                        var xmlPrimitives   = document.CreateNode(XmlNodeType.Element, "primitives", "");
                        var primitiveReader = new PrimitiveReader();
                        primitiveReader.ReadPrimitives(reader, xmlPrimitives, document);
                        document.AppendChild(xmlPrimitives);

                        encodeType = EncodeType.Binary;

                        if (wellFormatted)
                        {
                            return(XmlDecoder.FormatXml(document.OuterXml));
                        }
                        else
                        {
                            return(document.OuterXml);
                        }
                    }
                    else
                    {
                        var extension = UnifiedPath.GetExtension(path);
                        if (extension == ".xml" || extension == ".def" || extension == ".visual" || extension == ".chunk" || extension == ".settings" || extension == ".model")
                        {
                            encodeType = EncodeType.Plain;
                            return(File.ReadAllText(path));
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                }
            }
        }