using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using BepInEx; using BepInEx.Configuration; using BepInEx.Core.Logging.Interpolation; using BepInEx.Logging; using Microsoft.CodeAnalysis; using SOD.Common; using SOD.Common.BepInEx; using SOD.Common.BepInEx.Configuration; using SOD.Common.Helpers; using UnityEngine; using UniverseLib; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] [assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")] [assembly: AssemblyCompany("Autosave")] [assembly: AssemblyConfiguration("Debug")] [assembly: AssemblyDescription("Adds in-game autosave functionality.")] [assembly: AssemblyFileVersion("0.1.1.0")] [assembly: AssemblyInformationalVersion("0.1.1-BE667+a019ef8caa3857786a0ef177154b466cff175813")] [assembly: AssemblyProduct("Autosave")] [assembly: AssemblyTitle("Autosave")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.1.1.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace Autosave { public interface IConfigBindings { [Binding(300, "The amount of time between autosaves in seconds. The minimum is 15 seconds.", null)] int AutosaveDelay { get; set; } [Binding(true, "If true, warn of upcoming autosaves 5 seconds and 30 seconds beforehand.", null)] bool ShowWarnings { get; set; } [Binding(true, "If true, then do not start the next autosave timer if the player was AFK during an autosave. The timer will start when the player returns from being AFK.", null)] bool AvoidConsecutiveAfkAutosaves { get; set; } [Binding(5, "The number of autosaves to keep. Old autosaves will be overwritten.", null)] int NumberOfAutosavesToKeep { get; set; } [Binding(true, "If true, autosaves will be named \" - AUTO #\". Otherwise, autosaves will be named \"Autosave - AUTO #\" instead.", null)] bool UseLastSaveName { get; set; } } [BepInPlugin("Autosave", "Autosave", "0.1.1-BE667")] [BepInProcess("Shadows of Doubt.exe")] [BepInDependency(/*Could not decode attribute arguments.*/)] public class Plugin : PluginController { public sealed class MaxAutosavesReachedException : Exception { public MaxAutosavesReachedException() : base($"Cannot exceed {500} autosaves per file name.") { } } private const string AUTOSAVE_FILE_NAME = "Autosave"; private const string AUTOSAVE_SUFFIX = " - AUTO #"; private const int PLAYER_AFK_TIME_THRESHOLD = 10; private const int AUTOSAVE_DIGITS = 3; private const int MAX_AUTOSAVES_PER_FILE_NAME = 500; private const int MIN_AUTOSAVE_DELAY = 15; private int timeSinceLastAutosave = 0; private bool isAutoSaving = false; private bool isGamePaused = false; private bool isGameTransitioning = false; private bool playerJustReturnedFromAFK = false; private bool wasPlayerAFKDuringLastAutosave = false; private bool sentAutosaveWarningImminent = false; private bool sentAutosaveWarningSoon = false; private bool warnedAboutAutosaveDelay = false; private string originalSaveGameFilePath; public bool IsPlayerAfk { get; private set; } = false; private int AutosaveDelay { get { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Expected O, but got Unknown if (base.Config.AutosaveDelay < 15) { if (!warnedAboutAutosaveDelay) { ManualLogSource log = PluginController.Log; bool flag = default(bool); BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(67, 1, ref flag); if (flag) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("The minimum autosave delay is "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(15); ((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" seconds, which will be used instead."); } log.LogWarning(val); warnedAboutAutosaveDelay = true; } return 15; } return base.Config.AutosaveDelay; } } private bool ShouldProgress => !isGamePaused && !isGameTransitioning && (!base.Config.AvoidConsecutiveAfkAutosaves || !IsPlayerAfk || !wasPlayerAFKDuringLastAutosave); public Coroutine Coroutine { get; private set; } public override void Load() { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Expected O, but got Unknown ManualLogSource log = PluginController.Log; bool flag = default(bool); BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag); if (flag) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted("Autosave"); ((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!"); } log.LogInfo(val); Lib.SaveGame.OnBeforeLoad += OnBeforeLoad; Lib.SaveGame.OnAfterLoad += OnAfterLoad; Lib.SaveGame.OnBeforeNewGame += OnBeforeNewGame; Lib.SaveGame.OnAfterNewGame += OnAfterNewGame; Lib.SaveGame.OnBeforeSave += OnBeforeSave; Lib.SaveGame.OnAfterSave += OnAfterSave; Lib.Time.OnGamePaused += OnGamePaused; Lib.Time.OnGameResumed += OnGameResumed; Lib.InputDetection.OnButtonStateChanged += OnButtonStateChanged; base.ConfigFile.SettingChanged += OnConfigSettingChanged; } private void OnConfigSettingChanged(object sender, SettingChangedEventArgs arg) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Expected O, but got Unknown ManualLogSource log = PluginController.Log; bool flag = default(bool); BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(52, 0, ref flag); if (flag) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Config setting changed, restarting autosave timer..."); } log.LogInfo(val); OnPlayerReturnFromAfk(); RestartCoroutine(); } public override bool Unload() { Lib.SaveGame.OnBeforeLoad -= OnBeforeLoad; Lib.SaveGame.OnAfterLoad -= OnAfterLoad; Lib.SaveGame.OnBeforeNewGame -= OnBeforeNewGame; Lib.SaveGame.OnAfterNewGame -= OnAfterNewGame; Lib.SaveGame.OnBeforeSave -= OnBeforeSave; Lib.SaveGame.OnAfterSave -= OnAfterSave; Lib.Time.OnGamePaused -= OnGamePaused; Lib.Time.OnGameResumed -= OnGameResumed; Lib.InputDetection.OnButtonStateChanged -= OnButtonStateChanged; return base.Unload(); } private void OnBeforeNewGame(object sender, EventArgs e) { isGameTransitioning = true; } private void OnGameResumed(object sender, EventArgs args) { isGamePaused = false; } private void OnGamePaused(object sender, EventArgs args) { isGamePaused = true; } private void OnAfterNewGame(object sender, EventArgs args) { isGameTransitioning = false; originalSaveGameFilePath = string.Empty; RestartCoroutine(); } private void OnBeforeLoad(object sender, SaveGameArgs args) { isGameTransitioning = true; } private void OnButtonStateChanged(object sender, InputDetectionEventArgs args) { OnPlayerReturnFromAfk(); } private void OnAfterSave(object sender, SaveGameArgs args) { if (!isAutoSaving) { SetOriginalSaveGameFilePath(args.FilePath); } RestartCoroutine(); isAutoSaving = false; isGameTransitioning = false; } private void OnBeforeSave(object sender, SaveGameArgs args) { isGameTransitioning = true; if (isAutoSaving) { wasPlayerAFKDuringLastAutosave = IsPlayerAfk; } } private void SetOriginalSaveGameFilePath(string filePath) { originalSaveGameFilePath = filePath; string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(originalSaveGameFilePath); if (fileNameWithoutExtension.Length >= 3 + " - AUTO #".Length) { string text = fileNameWithoutExtension.Remove(fileNameWithoutExtension.Length - 3, 3); if (text.EndsWith(" - AUTO #")) { string text2 = text.Remove(text.Length - " - AUTO #".Length, " - AUTO #".Length); originalSaveGameFilePath = Application.persistentDataPath + "/Save/" + text2 + ".sodb"; } } } private void OnAfterLoad(object sender, SaveGameArgs args) { isGameTransitioning = false; SetOriginalSaveGameFilePath(args.FilePath); OnPlayerReturnFromAfk(); RestartCoroutine(); } private void RestartCoroutine() { timeSinceLastAutosave = 0; if (Coroutine != null) { RuntimeHelper.StopCoroutine(Coroutine); } Coroutine = RuntimeHelper.StartCoroutine(UpdateAutosave()); } private void OnPlayerReturnFromAfk() { IsPlayerAfk = false; wasPlayerAFKDuringLastAutosave = false; playerJustReturnedFromAFK = true; } private void UpdatePlayerAFKStatus(ref int afkEntryCounter) { if (IsPlayerAfk) { afkEntryCounter = 0; } else if (!playerJustReturnedFromAFK) { afkEntryCounter++; } else { afkEntryCounter = 0; playerJustReturnedFromAFK = false; } if (afkEntryCounter >= 10) { IsPlayerAfk = true; } } private void BroadcastAutosaveWarnings(int timeSinceLastAutosave) { //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0081: Unknown result type (might be due to invalid IL or missing references) int num = AutosaveDelay - timeSinceLastAutosave; if (base.Config.ShowWarnings) { if (num == 30 && !sentAutosaveWarningSoon) { Lib.GameMessage.Broadcast("Warning: Autosaving in 30 seconds.", (GameMessageType)0, (Icon)27, (Color?)Color.cyan, 0f); sentAutosaveWarningSoon = true; } if (num == 5 && !sentAutosaveWarningImminent) { Lib.GameMessage.Broadcast("WARNING: Autosaving in 5 seconds!", (GameMessageType)0, (Icon)27, (Color?)Color.cyan, 0f); sentAutosaveWarningImminent = true; } } } private IEnumerator UpdateAutosave() { int afkEntryCounter = 0; while (timeSinceLastAutosave < AutosaveDelay) { yield return (object)new WaitForSecondsRealtime(1f); if (ShouldProgress) { UpdatePlayerAFKStatus(ref afkEntryCounter); timeSinceLastAutosave++; BroadcastAutosaveWarnings(timeSinceLastAutosave); } } isAutoSaving = true; sentAutosaveWarningImminent = false; sentAutosaveWarningSoon = false; PruneOldAutosaves(GetAutosaveFilename(includePaddedNumber: false)); string path = Application.persistentDataPath + "/Save/" + GetAutosaveFilename() + ".sod"; ManualLogSource log = PluginController.Log; bool flag = default(bool); BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag); if (flag) { ((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Autosaving, path: "); ((BepInExLogInterpolatedStringHandler)val).AppendFormatted(path); } log.LogInfo(val); SaveStateController.Instance.CaptureSaveStateAsync(path); } private void PruneOldAutosaves(string fileName) { string path = Application.persistentDataPath + "/Save/"; string searchPattern = fileName + "*"; List list = Directory.GetFiles(path, searchPattern).ToList(); if (list.Count == 1 && base.Config.NumberOfAutosavesToKeep == 1) { File.Delete(list[0]); } else if (list.Count >= base.Config.NumberOfAutosavesToKeep) { list.Sort(); int count = list.Count; for (int i = 0; i < count - 1; i++) { string destFileName = list[i]; string sourceFileName = list[i + 1]; File.Move(sourceFileName, destFileName, overwrite: true); } } } private string GetAutosaveFilename(bool includePaddedNumber = true) { string text; if (base.Config.UseLastSaveName && originalSaveGameFilePath != string.Empty) { text = Path.GetFileNameWithoutExtension(originalSaveGameFilePath); if (text == string.Empty) { text = "Autosave"; } text += " - AUTO #"; } else { text = "Autosave - AUTO #"; } string text2 = Application.persistentDataPath + "/Save/"; int num = 1; string text3 = $"{num}".PadLeft(3, '0'); string text4 = text2 + text + text3 + ".sod"; while (File.Exists(text4 ?? "") || File.Exists(text4 + "b")) { num++; text3 = $"{num}".PadLeft(3, '0'); text4 = text2 + text + text3 + ".sod"; if (num > 500) { throw new MaxAutosavesReachedException(); } } return includePaddedNumber ? (text + text3) : text; } } public static class MyPluginInfo { public const string PLUGIN_GUID = "Autosave"; public const string PLUGIN_NAME = "Autosave"; public const string PLUGIN_VERSION = "0.1.1-BE667"; } }