コード例 #1
0
ファイル: AutosaveController.cs プロジェクト: Bisa/Autosave
        private IEnumerator AutosaveCoroutine()
        {
            this.isSaving = true;

            if (IsPlayingPermaDeath() && !Entry.Config.AutoSavePermaDeath)
            {
                Entry.LogDebug("Will not autosave a game with permanent death.");                 // TODO: Translate

                UpdateTick();

                yield break;
            }

            Entry.DisplayMessage("AutosaveStarting".Translate());

            // ensure we do not autosave our own slots when a player loaded them,
            // just give them a new primary slot
            ChangeSlotIfOnAutosaveSlot();

#if DEBUG
            // Close ingame menu if open, used for testing
            IngameMenu.main.Close();
#endif

            yield return(null);

            // trigger original save
            IEnumerator saveGameAsync = (IEnumerator)typeof(IngameMenu).GetMethod(
                "SaveGameAsync", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(IngameMenu.main, null);

            yield return(saveGameAsync);

            Entry.LogDebug("Executed _SaveGameAsync");

            if (IsPlayingPermaDeath())
            {
                Entry.LogDebug("Playing with permanent death, won't create any new save slots.", true);
            }

            else
            {
                string currentSaveSlotPath = string.Empty;
                string saveGamesPath       = GetSavePath();

                if (!Directory.Exists(saveGamesPath))
                {
                    Entry.LogFatal(string.Format(
                                       "Unable to find save directory, the expected '{0}' does not exist.",
                                       saveGamesPath));
                    Entry.DisplayMessage("Could not find your SavedGames directory, see log for details!",                     // TODO: Translate
                                         Level.Fatal);

                    this.isSaving = false;
                    throw new FileNotFoundException(savePath);
                }

                currentSaveSlotPath = Path.Combine(saveGamesPath, SaveLoadManager.main.GetCurrentSlot());
                // rotate our current autosave slots
                string slot = SaveLoadManager.main.GetCurrentSlot();
                yield return(CoroutineHost.StartCoroutine(this.RotateAutosaveSlots(slot)));

                // figure out the next slot
                AutosaveSlot nextAutosaveSlot = GetNextAutosaveSlot(slot);
                string       nextAutosavePath = nextAutosaveSlot.GetDirectoryInfo().FullName;

                // make a copy of the current slot
                yield return(CoroutineHost.StartCoroutine(this.CopyDirCoroutine(
                                                              currentSaveSlotPath, nextAutosavePath)));

                Entry.LogDebug(string.Format(
                                   "Copied from '{0}' to '{1}'",
                                   currentSaveSlotPath,
                                   nextAutosavePath),
                               true);
            }

            UpdateTick();

            Entry.DisplayMessage("AutosaveEnding".FormatTranslate((Entry.Config.MinutesBetweenAutosaves * 60).ToString()));

            yield break;
        }
コード例 #2
0
ファイル: AutosaveController.cs プロジェクト: Bisa/Autosave
        private IEnumerator RotateAutosaveSlots(string slot)
        {
            // get the current slots
            List <AutosaveSlot> slots = GetAutosaveSlotsOldestFirst(slot);

            // break if theres nothing to do
            if (slots.Count == 0)
            {
                Entry.LogInfo(string.Format(
                                  "Found no autosave slots for {0}, nothing to rotate",
                                  slot,
                                  string.Format(AutosaveSlot.Format, 0)));

                yield break;
            }

            Entry.LogDebug(string.Format(
                               "Oldest slot: {0}",
                               slots.First().ToString()),
                           true);

            Entry.LogDebug(string.Format(
                               "Most recent slot: {0}",
                               slots.Last().ToString()),
                           true);

            // check if we've reached the maximum number of saves, remove oldest save
            if (slots.Count >= Entry.Config.MaxAutosaveSlots)
            {
                Entry.LogInfo(string.Format(
                                  "Reached maximum number of autosave slots ({0}) for {1}, removing the oldest {2}",
                                  Entry.Config.MaxAutosaveSlots,
                                  slot,
                                  string.Format(
                                      AutosaveSlot.Format,
                                      slots.First().GetId())));

                yield return(CoroutineHost.StartCoroutine(this.DelDirCoroutine(
                                                              slots.First().GetDirectoryInfo().FullName)));
            }

            // handle max ids for the next slot by restarting at 0
            AutosaveSlot nextSlot = GetNextAutosaveSlot(slot);

            yield return(null);

            if (nextSlot.GetId() > AutosaveSlot.MaxID)
            {
                // if there's already a 0 slot, remove it
                nextSlot = new AutosaveSlot(slot, 0);
                if (nextSlot.GetDirectoryInfo().Exists)
                {
                    yield return(CoroutineHost.StartCoroutine(this.DelDirCoroutine(
                                                                  nextSlot.GetDirectoryInfo().FullName)));
                }

                // move our latest save to id 0
                slots.Last().GetDirectoryInfo().MoveTo(nextSlot.GetDirectoryInfo().FullName);
            }

            yield break;
        }