예제 #1
0
        /// <summary>
        /// Loads a game save.
        /// </summary>
        /// <typeparam name="TGameSave">The game save class type.</typeparam>
        /// <param name="name">The name of the gamesave to load.</param>
        /// <param name="forceFromDisk">
        /// If <c>true</c> the save will always be read from disk. If <c>false</c> the system will return a cached instance if available.
        /// </param>
        /// <returns>An <see cref="IFuture<GameSaveLoadResult<TGameSave>>" /> that can be used to track and examine the load results.</returns>
        public static IFuture <GameSaveLoadResult <TGameSave> > Load <TGameSave>(string name, bool forceFromDisk = false)
            where TGameSave : class, IGameSave, new()
        {
            ThrowIfNotInitialized();

            Future <GameSaveLoadResult <TGameSave> > future = new Future <GameSaveLoadResult <TGameSave> >();

            TGameSave save = null;

            if (GameSaveCache <TGameSave> .TryGetSave(name, out save) && !forceFromDisk)
            {
                future.Assign(new GameSaveLoadResult <TGameSave>(save, true, false));
            }
            else
            {
                if (save == null)
                {
                    save = new TGameSave();
                    GameSaveCache <TGameSave> .Set(name, save);
                }
                else
                {
                    save.Reset();
                }

                future.Process(() =>
                {
                    bool usedBackup = false;

                    if (_settings.useRollingBackups)
                    {
                        usedBackup = DoLoadWithBackups(save, name);
                    }
                    else
                    {
                        using (var stream = File.OpenRead(GetGameSavePath(name))) {
                            save.Load(stream);
                        }
                    }

                    return(new GameSaveLoadResult <TGameSave>(save, false, usedBackup));
                });
            }

            return(future);
        }
예제 #2
0
        /// <summary>
        /// Saves a game save.
        /// </summary>
        /// <typeparam name="TGameSave">The game save class type.</typeparam>
        /// <param name="name">The name of the gamesave to load.</param>
        /// <param name="save">The game save to save.</param>
        /// <returns>An <see cref="IFuture{bool}"/> that can be used to track completion of the save operation.</returns>
        public static IFuture <bool> Save <TGameSave>(string name, TGameSave save)
            where TGameSave : class, IGameSave, new()
        {
            ThrowIfNotInitialized();

            GameSaveCache <TGameSave> .Set(name, save);

            return(new Future <bool>().Process(() =>
            {
                if (_settings.useRollingBackups)
                {
                    DoSaveWithBackups(save, name);
                }
                else
                {
                    using (var stream = File.Create(GetGameSavePath(name))) {
                        save.Save(stream);
                    }
                }
                return true;
            }));
        }