Esempio n. 1
0
        public static SFXSaveGameFile Read(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            var save = new SFXSaveGameFile()
            {
                _Version = input.ReadValueU32(Endian.Little)
            };

            if (save._Version != 29 && save._Version.Swap() != 29 &&
                save._Version != 59 && save._Version.Swap() != 59)
            {
                throw new FormatException("unexpected version");
            }
            var endian = save._Version == 29 || save._Version == 59
                             ? Endian.Little
                             : Endian.Big;

            if (endian == Endian.Big)
            {
                save._Version = save._Version.Swap();
            }

            var reader = new Unreal.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.ReadValueU32();
            }

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

            save.Endian = endian;
            return(save);
        }
Esempio n. 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.WriteValueU32(save.Version, save._Endian);

                var writer = new Unreal.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)
                    {
                        int read = memory.Read(buffer, 0, 1024);
                        checksum = Crc32.Compute(buffer, 0, read, checksum);
                    }

                    save._Checksum = checksum;
                    memory.WriteValueU32(checksum, save._Endian);
                }

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