Ejemplo n.º 1
0
        private SaveInfo ReadSaveHeader(BinaryReader reader)
        {
            var headerLength = reader.ReadInt32();
            var headerBlob   = reader.ReadBytes(headerLength);

            return(SaveInfo.FromBlob(headerBlob));
        }
Ejemplo n.º 2
0
        public void NewGame(string playerName)
        {
            var slotName = "";

            lock (_mutex)
            {
                if (IsGameLoaded)
                {
                    throw new InvalidOperationException("A game is already active.");
                }

                var saveInfo = SaveInfo.MakeNew(playerName);
                slotName = GetNextSlotName();
                var savePath = GetPath(slotName);

                _slots.Add(slotName, saveInfo);

                using (var stream = File.Open(savePath, FileMode.OpenOrCreate))
                {
                    using (var writer = new BinaryWriter(stream, Encoding.UTF8))
                    {
                        writer.Write(SaveMagic);

                        var headerBlob = saveInfo.ToBlob();

                        writer.Write(headerBlob.Length);
                        writer.Write(headerBlob);

                        using (var ms = new MemoryStream())
                        {
                            var db = new LiteDatabase(ms);

                            db.Dispose();

                            var dbBytes = ms.ToArray();

                            using (var gzipTarget = new MemoryStream())
                            {
                                using (var gzip = new GZipStream(gzipTarget, CompressionLevel.Optimal))
                                {
                                    gzip.Write(dbBytes, 0, dbBytes.Length);
                                }

                                var zippedData = gzipTarget.ToArray();

                                writer.Write(zippedData.Length);
                                writer.Write(zippedData);
                            }
                        }
                    }
                }
            }

            LoadGame(slotName);
        }
Ejemplo n.º 3
0
        internal static SaveInfo MakeNew(string playerName)
        {
            var info = new SaveInfo();

            info.PlayerName      = playerName;
            info.LastMissionName = "";

            info.Created    = DateTime.Now;
            info.LastPlayed = DateTime.Now;

            return(info);
        }
Ejemplo n.º 4
0
        public void UnloadGame(bool save = true)
        {
            if (IsGameLoaded)
            {
                if (save)
                {
                    SaveGame();
                }

                _random = null;

                _dataStream.Dispose();
                _saveInfo = null;

                SaveUnloaded?.Invoke(this, EventArgs.Empty);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new <see cref="SaveInfo"/> instance from the raw SARS save header data.
        /// </summary>
        /// <param name="blob">A byte array containing the raw SARS data to parse.</param>
        /// <returns>The <see cref="SaveInfo"/> object represented within the given SARS data.</returns>
        public static SaveInfo FromBlob(byte[] blob)
        {
            var info = new SaveInfo();

            using (var ms = new MemoryStream(blob))
            {
                using (var reader = new BinaryReader(ms, Encoding.UTF8))
                {
                    info.Created         = new DateTime(reader.ReadInt64());
                    info.LastPlayed      = new DateTime(reader.ReadInt64());
                    info.PlayerName      = reader.ReadString();
                    info.LastMissionName = reader.ReadString();
                }
            }

            return(info);
        }
Ejemplo n.º 6
0
        public void LoadGame(string name)
        {
            lock (_mutex)
            {
                GameUtils.Log("loading save game in slot " + name);

                if (IsGameLoaded)
                {
                    throw new InvalidOperationException("A save game is already loaded and active.");
                }

                var path = GetPath(name);

                if (!File.Exists(path))
                {
                    throw new InvalidOperationException("Specified save game does not exist.");
                }

                using (var stream = File.OpenRead(path))
                {
                    ThrowIfInvalidFormat(stream);

                    using (var reader = new BinaryReader(stream, Encoding.UTF8))
                    {
                        _saveInfo = ReadSaveHeader(reader);

                        OpenMemoryDatabase(reader);

                        _random = new Random(PlayerName.GetHashCode());
                    }
                }

                _saveInfo.LastPlayed = DateTime.Now;

                _slot = name;
            }
        }