/// <summary> /// Applied before DelayedSave runs. /// </summary> internal static System.Collections.IEnumerator Postfix( System.Collections.IEnumerator result, string filename, bool isAutoSave, Game.SavingPostCB ___activatePostCB, bool updateSavePointer, Game.SavingActiveCB ___activateActiveCB) { var inst = PlayerController.Instance; if (isAutoSave) { // Wait for player to stop dragging while (inst.IsDragging()) { yield return(null); } inst.CancelDragging(); inst.AllowDragging(false); BackgroundAutosave.DisableSaving(); try { yield return(null); if (___activateActiveCB != null) { ___activateActiveCB(); yield return(null); } // Save in the background Game.Instance.timelapser?.SaveColonyPreview(filename); BackgroundAutosave.Instance.StartSave(filename); yield return(null); RetireColonyUtility.SaveColonySummaryData(); // Wait asynchronously for it while (!BackgroundAutosave.Instance.CheckSaveStatus()) { yield return(null); } if (updateSavePointer) { SaveLoader.SetActiveSaveFilePath(filename); } ___activatePostCB?.Invoke(); for (int i = 0; i < 5; i++) { yield return(null); } } finally { BackgroundAutosave.EnableSaving(); inst.AllowDragging(true); } yield break; } else { // Original method while (result.MoveNext()) { yield return(result.Current); } } }
/// <summary> /// Applied before RenderEveryTick runs. /// </summary> internal static bool Prefix(ColonyAchievementTracker __instance) { var mode = FastTrackOptions.Instance.DisableAchievements; if (mode != FastTrackOptions.AchievementDisable.Always && (mode == FastTrackOptions.AchievementDisable.Never || TrackAchievements())) { var allAchievements = Db.Get().ColonyAchievements.resources; var achievements = __instance.achievements; // Update one per frame int index = __instance.updatingAchievement; if (index >= achievements.Count || index >= allAchievements.Count) { index = 0; } string key = allAchievements[index].Id; __instance.updatingAchievement = index + 1; // If achievement has not already failed or succeeded if (achievements.TryGetValue(key, out ColonyAchievementStatus status) && !status.success && !status.failed) { status.UpdateAchievement(); if (status.success && !status.failed) { ColonyAchievementTracker.UnlockPlatformAchievement(key); __instance.completedAchievementsToDisplay.Add(key); __instance.TriggerNewAchievementCompleted(key); RetireColonyUtility.SaveColonySummaryData(); } } } return(false); }
/// <summary> /// Starts saving the game in the background. This function is not reentrant! /// </summary> /// <param name="filename">The file name where the save should be stored.</param> public void StartSave(string filename) { var buffer = new MemoryStream(BUFFER_SIZE); var inst = SaveLoader.Instance; bool save = true; if (inst != null) { var trLoader = Traverse.Create(inst); KSerialization.Manager.Clear(); #if DEBUG PUtil.LogDebug("Starting serialization of save"); #endif bool compress = true; // This field is currently always true try { compress = trLoader.GetField <bool>("compressSaveData"); } catch { } // Keep this part on the foreground try { trLoader.CallMethod("Save", new BinaryWriter(buffer)); } catch (Exception e) { buffer.Dispose(); PUtil.LogError("Error when saving game:"); PUtil.LogException(e); save = false; } // In Unity 4 GetComponent no longer works on background threads RetireColonyUtility.SaveColonySummaryData(); if (save) { StartSave(new BackgroundSaveData(buffer, compress, filename)); } } }
/// <summary> /// Cleans up old autosaves. /// </summary> /// <param name="filename">The file name that is being saved.</param> private void CleanAutosaves(string filename) { RetireColonyUtility.SaveColonySummaryData(); if (!Klei.GenericGameSettings.instance.keepAllAutosaves) { var saveFiles = SaveLoader.GetSaveFiles(Path.GetDirectoryName(filename)); // Clean up old autosaves and their preview images for (int i = saveFiles.Count - 1; i >= SaveLoader.MAX_AUTOSAVE_FILES - 1; i--) { string autoName = saveFiles[i], autoImage = Path.ChangeExtension( autoName, ".png"); try { PUtil.LogDebug("Deleting old autosave: " + autoName); File.Delete(autoName); } catch (Exception e) { PUtil.LogWarning("Problem deleting old autosave: " + autoName); PUtil.LogExcWarn(e); } try { if (File.Exists(autoImage)) { File.Delete(autoImage); } } catch (Exception e) { PUtil.LogWarning("Problem deleting old screenshot: " + autoImage); PUtil.LogExcWarn(e); } } } }
public static void ActivateRetiredColoniesScreen(GameObject parent, string colonyID = "", string[] newlyAchieved = null) { if ((UnityEngine.Object)RetiredColonyInfoScreen.Instance == (UnityEngine.Object)null) { Util.KInstantiateUI(ScreenPrefabs.Instance.RetiredColonyInfoScreen.gameObject, parent, true); } RetiredColonyInfoScreen.Instance.Show(true); if (!string.IsNullOrEmpty(colonyID)) { if ((UnityEngine.Object)SaveGame.Instance != (UnityEngine.Object)null) { RetireColonyUtility.SaveColonySummaryData(); } RetiredColonyInfoScreen.Instance.LoadColony(RetiredColonyInfoScreen.Instance.GetColonyDataByBaseName(colonyID)); } }
private void OnRetireConfirm() { RetireColonyUtility.SaveColonySummaryData(); }
public override void OnClick() { RetireColonyUtility.SaveColonySummaryData(); MainMenu.ActivateRetiredColoniesScreen(PauseScreen.Instance.transform.parent.gameObject, SaveGame.Instance.BaseName, null); }
public string Save(string filename, bool isAutoSave = false, bool updateSavePointer = true) { KSerialization.Manager.Clear(); ReportSaveMetrics(isAutoSave); RetireColonyUtility.SaveColonySummaryData(); if (isAutoSave && !GenericGameSettings.instance.keepAllAutosaves) { List <string> saveFiles = GetSaveFiles(Path.GetDirectoryName(filename)); for (int num = saveFiles.Count - 1; num >= 9; num--) { string text = saveFiles[num]; try { Debug.Log("Deleting old autosave: " + text); File.Delete(text); } catch (Exception ex) { Debug.LogWarning("Problem deleting autosave: " + text + "\n" + ex.ToString()); } string text2 = Path.ChangeExtension(text, ".png"); try { if (File.Exists(text2)) { File.Delete(text2); } } catch (Exception ex2) { Debug.LogWarning("Problem deleting autosave screenshot: " + text2 + "\n" + ex2.ToString()); } } } byte[] buffer = null; using (MemoryStream memoryStream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memoryStream)) { Save(writer); buffer = ((!compressSaveData) ? memoryStream.ToArray() : CompressContents(memoryStream.GetBuffer(), (int)memoryStream.Length)); } } try { using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(filename, FileMode.Create))) { SaveGame.Header header; byte[] saveHeader = SaveGame.Instance.GetSaveHeader(isAutoSave, compressSaveData, out header); binaryWriter.Write(header.buildVersion); binaryWriter.Write(header.headerSize); binaryWriter.Write(header.headerVersion); binaryWriter.Write(header.compression); binaryWriter.Write(saveHeader); KSerialization.Manager.SerializeDirectory(binaryWriter); binaryWriter.Write(buffer); Stats.Print(); } } catch (Exception ex3) { if (!(ex3 is UnauthorizedAccessException)) { if (!(ex3 is IOException)) { throw ex3; } DebugUtil.LogArgs("IOException (probably out of disk space) for " + filename); ConfirmDialogScreen confirmDialogScreen = (ConfirmDialogScreen)GameScreenManager.Instance.StartScreen(ScreenPrefabs.Instance.ConfirmDialogScreen.gameObject, GameScreenManager.Instance.ssOverlayCanvas.gameObject, GameScreenManager.UIRenderTarget.ScreenSpaceOverlay); confirmDialogScreen.PopupConfirmDialog(string.Format(UI.CRASHSCREEN.SAVEFAILED, "IOException. You may not have enough free space!"), null, null, null, null, null, null, null, null, true); return(GetActiveSaveFilePath()); } DebugUtil.LogArgs("UnauthorizedAccessException for " + filename); ConfirmDialogScreen confirmDialogScreen2 = (ConfirmDialogScreen)GameScreenManager.Instance.StartScreen(ScreenPrefabs.Instance.ConfirmDialogScreen.gameObject, GameScreenManager.Instance.ssOverlayCanvas.gameObject, GameScreenManager.UIRenderTarget.ScreenSpaceOverlay); confirmDialogScreen2.PopupConfirmDialog(string.Format(UI.CRASHSCREEN.SAVEFAILED, "Unauthorized Access Exception"), null, null, null, null, null, null, null, null, true); return(GetActiveSaveFilePath()); } if (updateSavePointer) { SetActiveSaveFilePath(filename); } Game.Instance.timelapser.SaveColonyPreview(filename); DebugUtil.LogArgs("Saved to", "[" + filename + "]"); GC.Collect(); return(filename); }