Пример #1
0
        public static SFXSaveGameFile Read(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            var save = new SFXSaveGameFile
            {
                _version = input.ReadUInt32()
            };

            /*if (save._version != 29 && save._version.Swap() != 29 &&
             *      save._version != 59 && save._version.Swap() != 59)*/
            if (save._version != 29 && save._version.Swap() != 29)
            {
                throw new FormatException("unexpected version");
            }

            //var endian = save._version == 29 || save._version == 59
            var endian = save._version == 29 ? ByteOrder.LittleEndian : ByteOrder.BigEndian;

            if (endian == ByteOrder.BigEndian)
            {
                save._version = save._version.Swap();
            }

            var reader = new FileReader(input, save._version, endian);

            save.Serialize(reader);

            if (save._version >= 27)
            {
                if (input.Position != input.Length - 4)
                {
                    throw new FormatException("bad checksum position");
                }

                save._checksum = input.ReadUInt32();
            }

            if (input.Position != input.Length)
            {
                throw new FormatException("did not consume entire file");
            }

            save.Endian = endian;

            return(save);
        }
Пример #2
0
        public static void Write(SFXSaveGameFile save, Stream output)
        {
            if (save == null)
            {
                throw new ArgumentNullException("save");
            }

            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            using (var memory = new MemoryStream())
            {
                memory.WriteUInt32(save.Version, save._endian);

                var writer = new FileWriter(memory, save._version, save._endian);
                save.Serialize(writer);

                if (save._version >= 27)
                {
                    memory.Position = 0;
                    uint checksum = 0;

                    var buffer = new byte[1024];

                    while (memory.Position < memory.Length)
                    {
                        var read = memory.Read(buffer, 0, 1024);
                        checksum = Crc32.Compute(buffer, 0, read, checksum);
                    }

                    save._checksum = checksum;
                    memory.WriteUInt32(checksum, save._endian);
                }

                memory.Position = 0;
                output.WriteFromStream(memory, memory.Length);
            }
        }