示例#1
0
        /*public void Serialize(DataWriter writer, uint version = 0)
         * {
         *      writer.Write(_DebugName);
         *      writer.Write(_SecondsPlayed);
         *      writer.Write(_Disc);
         *      writer.Write(_BaseLevelName);
         *      stream.Serialize(ref _BaseLevelNameDisplayOverrideAsRead, s => s.Version < 36, () => "None");
         *      stream.SerializeEnum(ref _Difficulty);
         *
         *      if (version >= 43 && version <= 46)
         *      {
         *              const byte unknown = 0;
         *              writer.Write(unknown);
         *      }
         *
         *      stream.SerializeEnum(ref _EndGameState);
         *      stream.Serialize(ref _TimeStamp);
         *      stream.Serialize(ref _Location);
         *      stream.Serialize(ref _Rotation);
         *      writer.Write(_CurrentLoadingTip);
         *      stream.Serialize(ref _Levels);
         *      stream.Serialize(ref _StreamingRecords);
         *      stream.Serialize(ref _KismetRecords);
         *      stream.Serialize(ref _Doors);
         *      stream.Serialize(ref _Placeables, s => s.Version < 46, () => new List<Placeable>());
         *      stream.Serialize(ref _Pawns);
         *      stream.Serialize(ref _Player);
         *      stream.Serialize(ref _Henchmen);
         *      stream.Serialize(ref _Plot);
         *      stream.Serialize(ref _Me1Plot);
         *      stream.Serialize(ref _PlayerVariables, s => s.Version < 34, () => new List<PlayerVariable>());
         *      stream.Serialize(ref _GalaxyMap);
         *      stream.Serialize(ref _DependentDLC);
         *      stream.Serialize(ref _Treasures, s => s.Version < 35, () => new List<LevelTreasure>());
         *      stream.Serialize(ref _UseModules, s => s.Version < 39, () => new List<Guid>());
         *      stream.SerializeEnum(ref _ConversationMode,
         *              s => s.Version < 49,
         *              () => AutoReplyModeOptions.AllDecisions);
         *      stream.Serialize(ref _ObjectiveMarkers, s => s.Version < 52, () => new List<ObjectiveMarker>());
         *      stream.Serialize(ref _SavedObjectiveText, s => s.Version < 52, () => 0);
         * }*/

        public static SFXSaveGameFile Read(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

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

            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
                                ? 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.ByteOrder = endian;
            return(save);
        }
示例#2
0
        public static void Write(SFXSaveGameFile save, Stream output)
        {
            if (save == null)
            {
                throw new ArgumentNullException(nameof(save));
            }

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

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

                var writer = new FileWriter(memory, save._version, save._byteOrder);
                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._byteOrder);
                }

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

            /*using (var writer = new DataWriter(output, save._byteOrder))
             * {
             *      writer.Write(save.Version);
             *      save.Serialize(writer);
             *
             *      if (save._Version >= 27)
             *      {
             *              writer.Seek(0);
             *
             *              uint checksum = 0;
             *              var buffer = new byte[1024];
             *              var baseStream = writer.BaseStream;
             *
             *              while (writer.Position < writer.Length)
             *              {
             *                      var read = baseStream.Read(buffer, 0, 1024);
             *                      checksum = Crc32.Compute(buffer, 0, read, checksum);
             *              }
             *
             *              save._Checksum = checksum;
             *              writer.Write(checksum);
             *      }
             * }*/
        }