Exemplo n.º 1
0
        /// <summary>
        /// Saves the game on persistent storage.
        /// </summary>
        /// <param name="savedGame">Combination of metadata and game specific data
        /// that is to be saved.</param>
        /// <param name="callback">Callback which is called when the saving has finished.</param>
        public static void SaveGame(SavedGame savedGame, Action callback)
        {
            if (savedGame.metadata == null)
            {
                throw new ArgumentNullException(
                          "savedGame",
                          "Metadata on SavedGame can't be null."
                          );
            }

            if (savedGame.gameSpecificData == null)
            {
                throw new ArgumentNullException(
                          "savedGame",
                          "Game specific data on SavedGame can't be null."
                          );
            }

            savedGame.metadata.timeStamp = DateTime.Now;

            var gameDataPath = SavedGameRootPath + savedGame.metadata.savedGameFileName +
                               savedGameFileSuffix;
            var metadataPath = SavedGameRootPath + savedGame.metadata.savedGameFileName +
                               metadataFileSuffix;

            if (Async)
            {
                SaveGameAsync(savedGame, gameDataPath, metadataPath, callback);
            }
            else
            {
                SerializeAndWrite(savedGame.gameSpecificData, gameDataPath);
                SerializeAndWrite(savedGame.metadata, metadataPath);

                if (callback != null)
                {
                    callback();
                }
            }
        }
Exemplo n.º 2
0
        private static void SaveGameAsync(SavedGame savedGame, string gameDataPath, string metadataPath,
                                          Action callback)
        {
            if (backgroundWorker == null)
            {
                backgroundWorker = InitializeBackgroundWorker();

                backgroundWorker.DoWork += (sender, args) =>
                {
                    SerializeAndWrite(savedGame.gameSpecificData, gameDataPath);
                    SerializeAndWrite(savedGame.metadata, metadataPath);
                };

                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                {
                    if (callback != null)
                    {
                        AsyncActionHelper.RunInGameThread(() =>
                        {
                            callback();
                        });
                    }

                    backgroundWorker = null;
                };

                backgroundWorker.RunWorkerAsync();
            }
            else if (backgroundWorker != null && backgroundWorker.IsBusy)
            {
                EnqueueAction(() =>
                {
                    SaveGameAsync(savedGame, gameDataPath, metadataPath, callback);
                });
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Deletes a single saved game permanently.
 /// </summary>
 /// <param name="savedGame">Combination of metadata and game specific data
 /// that is to be saved.</param>
 /// <param name="callback">Callback which is called when the deleting is done.</param>
 public static void DeleteSave(SavedGame savedGame, Action callback)
 {
     DeleteSave(savedGame.metadata, callback);
 }