public void SaveGame(int saveSlot) { Debug.Log("Saving game"); if (saveSlot >= 0) { if (this.gameCams.saveIcon != null) { this.gameCams.saveIcon.SendEvent("GAME SAVED"); } else { GameObject gameObject = GameObject.FindGameObjectWithTag("Save Icon"); if (gameObject != null) { PlayMakerFSM playMakerFSM = FSMUtility.LocateFSM(gameObject, "Checkpoint Control"); if (playMakerFSM != null) { playMakerFSM.SendEvent("GAME SAVED"); } } } this.SaveLevelState(); if (!this.gameConfig.disableSaveGame) { if (this.achievementHandler != null) { this.achievementHandler.FlushRecordsToDisk(); } else { Debug.Log("Error saving achievements (PlayerAchievements is null)"); } if (this.playerData != null) { this.playerData.playTime += this.sessionPlayTimer; this.ResetGameTimer(); this.playerData.version = Constants.GAME_VERSION; this.playerData.profileID = saveSlot; this.playerData.CountGameCompletion(); } else { Debug.Log("Error updating PlayerData before save (PlayerData is null)"); } try { SaveGameData obj = new SaveGameData(this.playerData, this.sceneData); ModHooks.Instance.OnBeforeSaveGameSave(obj); string text = JsonUtility.ToJson(obj); bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected; if (flag) { string graph = Encryption.Encrypt(text); BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); binaryFormatter.Serialize(memoryStream, graph); Platform.Current.WriteSaveSlot(saveSlot, memoryStream.ToArray()); memoryStream.Close(); } else { Platform.Current.WriteSaveSlot(saveSlot, Encoding.UTF8.GetBytes(text)); } } catch (Exception arg) { Debug.Log("GM Save - There was an error saving the game: " + arg); } Modding.ModHooks.Instance.OnSavegameSave(saveSlot); } else { Debug.Log("Saving game disabled. No save file written."); } } else { Debug.Log("Save game slot not valid: " + saveSlot); } Debug.Log("Finished saving game!"); }
public void GetSaveStatsForSlot(int saveSlot, Action <global::SaveStats> callback) { if (!Platform.IsSaveSlotIndexValid(saveSlot)) { Debug.LogErrorFormat("Cannot get save stats for invalid slot {0}", new object[] { saveSlot }); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(null); }); } return; } Platform.Current.ReadSaveSlot(saveSlot, delegate(byte[] fileBytes) { if (fileBytes == null) { if (callback != null) { CoreLoop.InvokeNext(delegate { callback(null); }); } return; } try { bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected; string json; if (flag) { BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream serializationStream = new MemoryStream(fileBytes); string encryptedString = (string)binaryFormatter.Deserialize(serializationStream); json = Encryption.Decrypt(encryptedString); } else { json = Encoding.UTF8.GetString(fileBytes); } SaveGameData saveGameData = JsonUtility.FromJson <SaveGameData>(json); global::PlayerData playerData = saveGameData.playerData; SaveStats saveStats = new SaveStats(playerData.maxHealthBase, playerData.geo, playerData.mapZone, playerData.playTime, playerData.MPReserveMax, playerData.permadeathMode, playerData.bossRushMode, playerData.completionPercentage, playerData.unlockedCompletionRate); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(saveStats); }); } } catch (Exception ex) { Debug.LogError(string.Concat(new object[] { "Error while loading save file for slot ", saveSlot, " Exception: ", ex })); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(null); }); } } }); }
public void SaveGame(int saveSlot, Action <bool> callback) { if (saveSlot >= 0) { this.SaveLevelState(); if (!this.gameConfig.disableSaveGame) { this.ShowSaveIcon(); if (this.achievementHandler != null) { this.achievementHandler.FlushRecordsToDisk(); } else { Debug.LogError("Error saving achievements (PlayerAchievements is null)"); } if (this.playerData != null) { this.playerData.playTime += this.sessionPlayTimer; this.ResetGameTimer(); this.playerData.version = Constants.GAME_VERSION; this.playerData.profileID = saveSlot; this.playerData.CountGameCompletion(); } else { Debug.LogError("Error updating PlayerData before save (PlayerData is null)"); } try { SaveGameData obj = new SaveGameData(this.playerData, this.sceneData); ModHooks.Instance.OnBeforeSaveGameSave(obj); string text = null; try { text = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings() { ContractResolver = ShouldSerializeContractResolver.Instance, TypeNameHandling = TypeNameHandling.Auto }); } catch (Exception e) { Logger.LogError("Failed to serialize save using Json.NET, trying fallback."); Logger.APILogger.LogError(e); // If this dies, not much we can do about it. text = JsonUtility.ToJson(obj); } bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected; if (flag) { string graph = Encryption.Encrypt(text); BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); binaryFormatter.Serialize(memoryStream, graph); byte[] binary = memoryStream.ToArray(); memoryStream.Close(); Platform.Current.WriteSaveSlot ( saveSlot, binary, delegate(bool didSave) { this.HideSaveIcon(); callback(didSave); } ); } else { Platform.Current.WriteSaveSlot ( saveSlot, Encoding.UTF8.GetBytes(text), delegate(bool didSave) { this.HideSaveIcon(); if (callback != null) { callback(didSave); } } ); } } catch (Exception arg) { Debug.LogError("GM Save - There was an error saving the game: " + arg); this.HideSaveIcon(); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } } ModHooks.Instance.OnSavegameSave(saveSlot); } else { Debug.Log("Saving game disabled. No save file written."); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } } } else { Debug.LogError("Save game slot not valid: " + saveSlot); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } } }
public void LoadGame(int saveSlot, Action <bool> callback) { if (!Platform.IsSaveSlotIndexValid(saveSlot)) { Debug.LogErrorFormat("Cannot load from invalid save slot index {0}", new object[] { saveSlot }); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } return; } Platform.Current.ReadSaveSlot(saveSlot, delegate(byte[] fileBytes) { bool obj; try { bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected; string json; if (flag) { BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream serializationStream = new MemoryStream(fileBytes); string encryptedString = (string)binaryFormatter.Deserialize(serializationStream); json = Encryption.Decrypt(encryptedString); } else { json = Encoding.UTF8.GetString(fileBytes); } SaveGameData saveGameData = JsonUtility.FromJson <SaveGameData>(json); global::PlayerData instance = saveGameData.playerData; SceneData instance2 = saveGameData.sceneData; global::PlayerData.instance = instance; this.playerData = instance; SceneData.instance = instance2; ModHooks.Instance.OnAfterSaveGameLoad(saveGameData); this.sceneData = instance2; this.profileID = saveSlot; this.inputHandler.RefreshPlayerData(); ModHooks.Instance.OnSavegameLoad(saveSlot); obj = true; } catch (Exception ex) { Debug.LogFormat("Error loading save file for slot {0}: {1}", new object[] { saveSlot, ex }); obj = false; } if (callback != null) { callback(obj); } }); }
internal static void OnBeforeSaveGame(SaveGameData data) => Mod.OnBeforeSaveGame(data);
// Custom attribute patching doesn't know about new classes internal static void OnAfterLoadGame(SaveGameData data) => Mod.OnAfterLoadGame(data);
public void SaveGame(int saveSlot, Action <bool> callback) { if (saveSlot >= 0) { this.SaveLevelState(); if (!this.gameConfig.disableSaveGame) { this.ShowSaveIcon(); if (this.achievementHandler != null) { this.achievementHandler.FlushRecordsToDisk(); } else { Debug.LogError("Error saving achievements (PlayerAchievements is null)"); } if (this.playerData != null) { this.playerData.playTime += this.sessionPlayTimer; this.ResetGameTimer(); this.playerData.version = "1.4.2.4"; this.playerData.profileID = saveSlot; this.playerData.CountGameCompletion(); } else { Debug.LogError("Error updating PlayerData before save (PlayerData is null)"); } try { SaveGameData obj = new SaveGameData(this.playerData, this.sceneData); ModHooks.Instance.OnBeforeSaveGameSave(obj); string text = JsonUtility.ToJson(obj); bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected; if (flag) { string graph = Encryption.Encrypt(text); BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); binaryFormatter.Serialize(memoryStream, graph); byte[] binary = memoryStream.ToArray(); memoryStream.Close(); Platform.Current.WriteSaveSlot(saveSlot, binary, delegate(bool didSave) { this.HideSaveIcon(); callback(didSave); }); } else { Platform.Current.WriteSaveSlot(saveSlot, Encoding.UTF8.GetBytes(text), delegate(bool didSave) { this.HideSaveIcon(); if (callback != null) { callback(didSave); } }); } } catch (Exception arg) { Debug.LogError("GM Save - There was an error saving the game: " + arg); this.HideSaveIcon(); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } } ModHooks.Instance.OnSavegameSave(saveSlot); } else { Debug.Log("Saving game disabled. No save file written."); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } } } else { Debug.LogError("Save game slot not valid: " + saveSlot); if (callback != null) { CoreLoop.InvokeNext(delegate { callback(false); }); } } }
public void SaveGame(int saveSlot) { if (saveSlot >= 0) { if (this.gameCams.saveIcon != null) { this.gameCams.saveIcon.SendEvent("GAME SAVED"); } else { GameObject gameObject = GameObject.FindGameObjectWithTag("Save Icon"); if (gameObject != null) { PlayMakerFSM playMakerFSM = global::FSMUtility.LocateFSM(gameObject, "Checkpoint Control"); if (playMakerFSM != null) { playMakerFSM.SendEvent("GAME SAVED"); } } } this.SaveLevelState(); if (!this.gameConfig.disableSaveGame) { if (this.achievementHandler != null) { this.achievementHandler.FlushRecordsToDisk(); } else { Debug.LogError("Error saving achievements (PlayerAchievements is null)"); } if (this.playerData != null) { if (this.gameState != GameState.PAUSED) { this.UpdateSessionPlayTime(); } this.playerData.playTime += this.sessionTotalPlayTime; this.sessionTotalPlayTime = 0f; this.intervalStartTime = Time.realtimeSinceStartup; this.playerData.version = "1.2.2.1"; this.playerData.profileID = saveSlot; this.playerData.CountGameCompletion(); } else { Debug.LogError("Error updating PlayerData before save (PlayerData is null)"); } string saveFilename = this.GetSaveFilename(saveSlot); string text = Application.persistentDataPath + saveFilename; string text2 = Application.persistentDataPath + saveFilename + ".bak"; int num = 3; string[] files = Directory.GetFiles(Application.persistentDataPath); List <string> list = new List <string>(); foreach (string text3 in files) { if (text3.Contains(text2)) { list.Add(text3); } } int num2 = this.CheckOldBackups(ref list, text2, false); while (list.Count >= num) { num2 = this.CheckOldBackups(ref list, text2, true); } if (File.Exists(text)) { try { if (File.Exists(text2 + num2)) { File.Delete(text2 + num2); } File.Move(text, text2 + num2); } catch (Exception arg) { Debug.LogError("Unable to move save game to backup file: " + arg); } } try { SaveGameData saveGameData = new SaveGameData(this.playerData, this.sceneData); ModHooks.Instance.OnBeforeSaveGameSave(saveGameData); string text4 = JsonUtility.ToJson(saveGameData, !this.gameConfig.useSaveEncryption); Logger.LogFine("[API] - About to Serialize Save Data\n" + text4); string graph = StringEncrypt.EncryptData(text4); BinaryFormatter binaryFormatter = new BinaryFormatter(); FileStream fileStream = File.Create(Application.persistentDataPath + saveFilename); if (this.gameConfig.useSaveEncryption) { binaryFormatter.Serialize(fileStream, graph); } else { binaryFormatter.Serialize(fileStream, text4); } fileStream.Close(); } catch (Exception arg2) { Debug.LogError("GM Save - There was an error saving the game: " + arg2); } Modding.ModHooks.Instance.OnSavegameSave(saveSlot); } else { Debug.Log("Saving game disabled. No save file written."); } } else { Debug.LogError("Save game slot not valid: " + saveSlot); } }