Example #1
0
        public static PssgFile ReadPssg(Stream fileStream, PssgFileType fileType)
        {
            PssgFile file = new PssgFile(fileType);

            using (PssgBinaryReader reader = new PssgBinaryReader(EndianBitConverter.Big, fileStream, true))
            {
                reader.ReadPSSGString(4); // "PSSG"
                int size = reader.ReadInt32();

                // Load all the pssg node/attribute names
                PssgSchema.ClearSchemaIds();
                PssgSchema.LoadFromPssg(reader);
                long positionAfterInfo = reader.BaseStream.Position;

                file.RootNode = new PssgNode(reader, file, null, true);
                if (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    reader.BaseStream.Position = positionAfterInfo;
                    file.RootNode = new PssgNode(reader, file, null, false);
                    if (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        throw new Exception("This file is improperly saved and not supported by this version of the PSSG editor." + Environment.NewLine + Environment.NewLine +
                                            "Get an older version of the program if you wish to take out its contents, but put it back together using this program and the original version of the pssg file.");
                    }
                }
            }

            return(file);
        }
        public static PssgFile ReadPssg(Stream fileStream, PssgFileType fileType)
        {
            PssgFile file = new PssgFile(fileType);

            using (PssgBinaryReader reader = new PssgBinaryReader(new BigEndianBitConverter(), fileStream))
            {
                reader.ReadPSSGString(4); // "PSSG"
                int size = reader.ReadInt32();

                // Load all the pssg node/attribute names
                PssgSchema.ClearSchemaIds();
                PssgSchema.LoadFromPssg(reader);
                long positionAfterInfo = reader.BaseStream.Position;

                file.RootNode = new PssgNode(reader, file, null, true);
                if (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    reader.BaseStream.Position = positionAfterInfo;
                    file.RootNode = new PssgNode(reader, file, null, false);
                    if (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        throw new Exception("This file is improperly saved and not supported by this version of the PSSG editor." + Environment.NewLine + Environment.NewLine +
                            "Get an older version of the program if you wish to take out its contents, but put it back together using this program and the original version of the pssg file.");
                    }
                }
            }

            return file;
        }
Example #3
0
        public static PssgFile Open(Stream stream)
        {
            PssgFileType fileType = PssgFile.GetPssgType(stream);

            if (fileType == PssgFileType.Pssg)
            {
                return(PssgFile.ReadPssg(stream, fileType));
            }
            else if (fileType == PssgFileType.Xml)
            {
                return(PssgFile.ReadXml(stream, fileType));
            }
            else // CompressedPssg
            {
                string tempPath = "temp.pssg";
                try
                {
                    using (var fs = File.Open(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                    {
                        // Decompress stream into temp file
                        using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress, true))
                        {
                            gZipStream.CopyTo(fs);
                        }

                        // Determine the file type after inflate, add CompressedPssg to make compressed
                        fs.Seek(0, SeekOrigin.Begin);
                        fileType = GetPssgType(fs) + (int)PssgFileType.CompressedPssg;
                        PssgFile pFile = fileType switch
                        {
                            PssgFileType.CompressedPssg => PssgFile.ReadPssg(fs, fileType),
                            PssgFileType.CompressedXml => PssgFile.ReadXml(fs, fileType),
                            _ => throw new FileFormatException("This is not a pssg file.")
                        };

                        return(pFile);
                    }
                }
                finally
                {
                    // Attempt to delete the temporary file
                    try
                    {
                        File.Delete(tempPath);
                    }
                    catch { }
                }
            }
        }
Example #4
0
        public static PssgFile ReadXml(Stream fileStream, PssgFileType fileType)
        {
            PssgFile  file = new PssgFile(fileType);
            XDocument xDoc = XDocument.Load(fileStream);

            var docElem = xDoc.FirstNode as XElement ??
                          throw new InvalidDataException("The pssg xml does not have a root element.");

            var firstNode = docElem.FirstNode as XElement ??
                            throw new InvalidDataException("The pssg xml does not have an element within the root element.");

            file.RootNode = new PssgNode(firstNode, file, null);

            return(file);
        }
Example #5
0
        public static PssgFile Open(Stream stream)
        {
            PssgFileType fileType = PssgFile.GetPssgType(stream);

            if (fileType == PssgFileType.Pssg)
            {
                return(PssgFile.ReadPssg(stream, fileType));
            }
            else if (fileType == PssgFileType.Xml)
            {
                return(PssgFile.ReadXml(stream));
            }
            else // CompressedPssg
            {
                string tempPath = "temp.pssg";
                try
                {
                    FileStream fs = File.Open(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

                    // Decompress stream into temp file
                    using (stream)
                    {
                        using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            gZipStream.CopyTo(fs);
                        }
                    }

                    // Read temp file, and close it
                    fs.Seek(0, SeekOrigin.Begin);
                    PssgFile pFile = PssgFile.ReadPssg(fs, fileType);

                    return(pFile);
                }
                finally
                {
                    // Attempt to delete the temporary file
                    try
                    {
                        File.Delete(tempPath);
                    }
                    catch { }
                }
            }
        }
Example #6
0
 public PssgFile(PssgFileType fileType)
 {
     this.FileType = fileType;
     this.RootNode = new PssgNode("PSSGDATABASE", this, null);
 }
 public PssgFile(PssgFileType fileType)
 {
     this.FileType = fileType;
 }
Example #8
0
 public PssgFile(PssgFileType fileType)
 {
     this.FileType = fileType;
 }