using System;
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.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Il2CppSystem.IO;
using Il2CppSystem.Threading.Tasks;
using Microsoft.CodeAnalysis;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SoDAutosave")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("SoDAutosave")]
[assembly: AssemblyTitle("SoDAutosave")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
[CompilerGenerated]
[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 SoDAutosave
{
internal static class ConfigWatch
{
private static ConfigFile? file;
private static ManualLogSource? log;
private static DateTime seen;
private static DateTime pending;
private static float next;
private static bool reading;
private static readonly List<string> changed = new List<string>();
internal static void Start(BasePlugin plugin)
{
if (file != null)
{
return;
}
try
{
file = plugin.Config;
log = plugin.Log;
seen = Stamp();
file.SettingChanged += delegate(object _, SettingChangedEventArgs e)
{
if (reading)
{
changed.Add(e.ChangedSetting.Definition.Section + "/" + e.ChangedSetting.Definition.Key);
}
};
plugin.AddComponent<ConfigWatchPump>();
}
catch (Exception ex)
{
plugin.Log.LogWarning((object)("[CONFIG] live settings off: " + ex.Message));
}
}
private static DateTime Stamp()
{
try
{
if (file == null)
{
return default(DateTime);
}
DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(file.ConfigFilePath);
return (lastWriteTimeUtc.Year < 1700) ? default(DateTime) : lastWriteTimeUtc;
}
catch
{
return default(DateTime);
}
}
internal static void Tick()
{
if (file == null)
{
return;
}
if (next == 0f)
{
next = Time.unscaledTime + 1f + (float)(file.ConfigFilePath.GetHashCode() & 0xFF) / 256f;
}
if (Time.unscaledTime < next)
{
return;
}
next = Time.unscaledTime + 1f;
DateTime dateTime = Stamp();
if (dateTime == default(DateTime) || dateTime == seen)
{
pending = default(DateTime);
return;
}
if (dateTime != pending)
{
pending = dateTime;
return;
}
pending = default(DateTime);
bool saveOnConfigSet = file.SaveOnConfigSet;
reading = true;
changed.Clear();
try
{
file.SaveOnConfigSet = false;
file.Reload();
seen = dateTime;
}
catch (Exception ex)
{
ManualLogSource? obj = log;
if (obj != null)
{
obj.LogWarning((object)("[CONFIG] could not read the settings: " + ex.Message));
}
}
finally
{
reading = false;
file.SaveOnConfigSet = saveOnConfigSet;
}
if (changed.Count > 0)
{
ManualLogSource? obj2 = log;
if (obj2 != null)
{
obj2.LogInfo((object)("[CONFIG] settings changed while playing: " + string.Join(", ", changed)));
}
}
}
}
public sealed class ConfigWatchPump : MonoBehaviour
{
public ConfigWatchPump(IntPtr ptr)
: base(ptr)
{
}
private void Update()
{
try
{
ConfigWatch.Tick();
}
catch
{
}
}
}
[BepInPlugin("sm3gm.sod.autosave", "Autosave", "1.0.1")]
public sealed class Plugin : BasePlugin
{
public const string VER = "1.0.1";
internal static ManualLogSource L;
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<bool> SkipWhenIdle;
internal static ConfigEntry<bool> ShowMessage;
internal static ConfigEntry<bool> UseSaveName;
internal static ConfigEntry<bool> OnCaseBoard;
internal static ConfigEntry<float> Minutes;
internal static ConfigEntry<int> Slots;
internal static ConfigEntry<string> Prefix;
public override void Load()
{
//IL_012a: Unknown result type (might be due to invalid IL or missing references)
//IL_0130: Expected O, but got Unknown
ConfigWatch.Start((BasePlugin)(object)this);
L = ((BasePlugin)this).Log;
Enabled = ((BasePlugin)this).Config.Bind<bool>("Autosave", "Enabled", true, "Save the game automatically.");
Minutes = ((BasePlugin)this).Config.Bind<float>("Autosave", "IntervalMinutes", 5f, "Real minutes between autosaves (paused time does not count). Also in the Quick Menu.");
Slots = ((BasePlugin)this).Config.Bind<int>("Autosave", "Slots", 5, "How many autosaves to keep in total, across all cities and saves (2 to 20). When there are more, the oldest autosaves move to the 'Old autosaves' folder next to your saves, which keeps the newest 10. Your own saves are never touched.");
SkipWhenIdle = ((BasePlugin)this).Config.Bind<bool>("Autosave", "SkipWhenIdle", true, "Skip an autosave when you have not moved or looked around since the last one, so standing away from the keyboard does not fill every slot with the same moment.");
ShowMessage = ((BasePlugin)this).Config.Bind<bool>("Autosave", "ShowMessage", true, "Show a short message after each autosave.");
Prefix = ((BasePlugin)this).Config.Bind<string>("Autosave", "Name", "Autosave", "Word used in autosave names, for example 'My case (Autosave 2, City)'.");
UseSaveName = ((BasePlugin)this).Config.Bind<bool>("Autosave", "UseSaveName", true, "Name autosaves after the save you loaded or last saved by hand, for example 'My case (Autosave 2, City)'. Off: 'Autosave 2 (City)'.");
OnCaseBoard = ((BasePlugin)this).Config.Bind<bool>("Autosave", "SaveOnCaseBoard", true, "Count time and autosave while the case board is open. Off: wait until you leave the board.");
((BasePlugin)this).AddComponent<Runner>();
ManualLogSource l = L;
bool flag = default(bool);
BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(60, 4, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] Autosave ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.0.1");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loaded: every ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<float>(Minutes.Value);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" min, ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(Slots.Value);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" slots, save names ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(UseSaveName.Value ? "on" : "off");
}
l.LogInfo(val);
}
internal static void Toast(string text)
{
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
//IL_0019: Unknown result type (might be due to invalid IL or missing references)
try
{
InterfaceController instance = InterfaceController.Instance;
if (instance != null)
{
instance.NewGameMessage((GameMessageType)0, 0, text, (Icon)3, (AudioEvent)null, false, default(Color), -1, 0f, (RectTransform)null, (PingOnComplete)0, (Evidence)null, (List<DataKey>)null, (Sprite)null);
}
}
catch
{
}
}
}
public sealed class Runner : MonoBehaviour
{
internal static Runner? Me;
private float playedSince;
private float nextCheck;
private float lastActive = -1f;
private Vector3 lastPos;
private Quaternion lastLook;
private bool movedSinceSave;
private bool toldWhy;
private Task? running;
private string runningName = "";
private float runningAt;
private bool manual;
private DateTime sessionStart;
private static readonly string[] Exts = new string[2] { ".sod", ".sodb" };
internal const string OldFolder = "Old autosaves";
private const int OldKeep = 10;
private string runningDir = "";
private string runningWord = "";
private int runningSlots;
private string runningPath = "";
private double callMs;
private float longest;
public Runner(IntPtr ptr)
: base(ptr)
{
}
private void Awake()
{
Me = this;
}
private static bool InGame()
{
SessionData instance = SessionData.Instance;
if ((Object)(object)instance != (Object)null && instance.startedGame && !instance.isFloorEdit && !instance.isTestScene && !instance.isDialogEdit)
{
return (Object)(object)Player.Instance != (Object)null;
}
return false;
}
private static bool OnBoard()
{
try
{
InterfaceController instance = InterfaceController.Instance;
MainMenuController instance2 = MainMenuController.Instance;
return Plugin.OnCaseBoard.Value && (Object)(object)instance != (Object)null && instance.desktopMode && ((Object)(object)instance2 == (Object)null || !instance2.mainMenuActive);
}
catch
{
return false;
}
}
private static string? Blocked()
{
try
{
MainMenuController instance = MainMenuController.Instance;
if ((Object)(object)instance != (Object)null && !instance.IsSaveGameAllowed())
{
return "not allowed by the game";
}
}
catch
{
}
if (!SessionData.Instance.play && !OnBoard())
{
return "paused";
}
Player instance2 = Player.Instance;
try
{
ModifiersController instance3 = ModifiersController.Instance;
if ((Object)(object)instance3 != (Object)null && instance3.ironmanModifierEnabled)
{
return "ironman game";
}
}
catch
{
}
if (instance2.transitionActive)
{
return "moving";
}
if (instance2.playerKOInProgress)
{
return "knocked out";
}
if (instance2.isLockpicking)
{
return "lockpicking";
}
try
{
if (((Actor)instance2).isDead)
{
return "dead";
}
}
catch
{
}
InteractionController instance4 = InteractionController.Instance;
if ((Object)(object)instance4 != (Object)null && instance4.dialogMode)
{
return "in a conversation";
}
if (instance2.persuedProgress > 0.01f)
{
return "being chased";
}
return null;
}
private void Update()
{
try
{
Tick();
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("[AUTOSAVE] " + ex.Message));
}
}
private void Tick()
{
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
//IL_013a: Unknown result type (might be due to invalid IL or missing references)
//IL_013f: Unknown result type (might be due to invalid IL or missing references)
//IL_0145: Unknown result type (might be due to invalid IL or missing references)
//IL_0146: Unknown result type (might be due to invalid IL or missing references)
//IL_0101: Unknown result type (might be due to invalid IL or missing references)
//IL_0103: Unknown result type (might be due to invalid IL or missing references)
if (running != null)
{
Watch();
return;
}
if (!InGame())
{
playedSince = 0f;
movedSinceSave = false;
sessionStart = default(DateTime);
return;
}
if (sessionStart == default(DateTime))
{
sessionStart = DateTime.UtcNow;
}
SessionData instance = SessionData.Instance;
bool flag = OnBoard();
if (instance.play || flag)
{
playedSince += Time.unscaledDeltaTime;
}
if (flag)
{
movedSinceSave = true;
}
if (Time.unscaledTime < nextCheck)
{
return;
}
nextCheck = Time.unscaledTime + 1f;
Player instance2 = Player.Instance;
Camera main = Camera.main;
Quaternion val = (((Object)(object)main != (Object)null) ? ((Component)main).transform.rotation : Quaternion.identity);
if (!(lastActive < 0f))
{
Vector3 val2 = ((Component)instance2).transform.position - lastPos;
if (!(((Vector3)(ref val2)).sqrMagnitude > 0.25f) && !(Quaternion.Angle(val, lastLook) > 5f))
{
goto IL_014b;
}
}
if (lastActive >= 0f)
{
movedSinceSave = true;
}
lastActive = Time.unscaledTime;
lastPos = ((Component)instance2).transform.position;
lastLook = val;
goto IL_014b;
IL_014b:
if (!manual && (!Plugin.Enabled.Value || Plugin.Minutes.Value <= 0f || playedSince < Mathf.Max(1f, Plugin.Minutes.Value) * 60f))
{
return;
}
if (!manual && Plugin.SkipWhenIdle.Value && !movedSinceSave)
{
Plugin.L.LogInfo((object)"[AUTOSAVE] skipped: nothing has happened since the last autosave");
playedSince = 0f;
return;
}
string text = Blocked();
if (text != null)
{
if (!toldWhy)
{
toldWhy = true;
Plugin.L.LogInfo((object)("[AUTOSAVE] waiting: " + text));
}
}
else
{
toldWhy = false;
Save();
}
}
internal void SaveNow()
{
if (running != null)
{
Plugin.Toast("Already saving...");
return;
}
string text = (InGame() ? Blocked() : "not in a game");
if (text != null && text != "paused")
{
Plugin.Toast("Cannot save right now: " + text);
return;
}
manual = true;
Save();
}
private static string Clean(string s)
{
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
foreach (char c in invalidFileNameChars)
{
s = s.Replace(c.ToString(), "");
}
return s.Trim();
}
private static Regex RxNew(string w)
{
return new Regex("^(.+) \\(" + Regex.Escape(w) + " (\\d+), (.+)\\)$");
}
private static Regex RxOld(string w)
{
return new Regex("^" + Regex.Escape(w) + " (\\d+) \\((.+)\\)$");
}
private static Regex RxV020(string w)
{
return new Regex("^(.+) \\(" + Regex.Escape(w) + " (\\d+)\\)$");
}
private static bool IsAutosave(string name, string word)
{
if (!RxNew(word).IsMatch(name) && !RxOld(word).IsMatch(name))
{
return RxV020(word).IsMatch(name);
}
return true;
}
private static int SlotOf(string name, string word)
{
Match match = RxNew(word).Match(name);
if (match.Success)
{
return int.Parse(match.Groups[2].Value);
}
match = RxOld(word).Match(name);
if (match.Success)
{
return int.Parse(match.Groups[1].Value);
}
match = RxV020(word).Match(name);
if (match.Success)
{
return int.Parse(match.Groups[2].Value);
}
return 0;
}
private static List<(FileInfo f, string name, int slot)> AllAutosaves(string dir, string word)
{
List<(FileInfo, string, int)> list = new List<(FileInfo, string, int)>();
try
{
FileInfo[] files = new DirectoryInfo(dir).GetFiles();
foreach (FileInfo fileInfo in files)
{
if (Exts.Contains(fileInfo.Extension.ToLowerInvariant()))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.Name);
int num = SlotOf(fileNameWithoutExtension, word);
if (num > 0)
{
list.Add((fileInfo, fileNameWithoutExtension, num));
}
}
}
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("[AUTOSAVE] could not list the save folder: " + ex.Message));
}
return list.OrderBy(((FileInfo, string, int) x) => x.Item1.LastWriteTimeUtc).ToList();
}
private static string StripAutosave(string name, string word)
{
Regex regex = new Regex(" \\(" + Regex.Escape(word) + " \\d+(, [^()]*)?\\)$| " + Regex.Escape(word) + " \\d+$", RegexOptions.IgnoreCase);
for (int i = 0; i < 10; i++)
{
string text = regex.Replace(name, "").TrimEnd();
if (text == name)
{
break;
}
name = text;
}
if (RxOld(word).IsMatch(name))
{
name = "";
}
return name;
}
private string BaseName(string dir, string word)
{
string text = "";
try
{
FileInfo fileInfo = null;
FileInfo[] files = new DirectoryInfo(dir).GetFiles();
foreach (FileInfo fileInfo2 in files)
{
if (Exts.Contains(fileInfo2.Extension.ToLowerInvariant()) && !(fileInfo2.LastWriteTimeUtc < sessionStart))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo2.Name);
if (!IsAutosave(fileNameWithoutExtension, word) && !fileNameWithoutExtension.StartsWith("[") && (fileInfo == null || fileInfo2.LastWriteTimeUtc > fileInfo.LastWriteTimeUtc))
{
fileInfo = fileInfo2;
}
}
}
if (fileInfo != null)
{
text = Path.GetFileNameWithoutExtension(fileInfo.Name);
}
}
catch
{
}
if (text.Length == 0)
{
try
{
RestartSafeController instance = RestartSafeController.Instance;
if ((Object)(object)instance != (Object)null && instance.loadSaveGame && instance.saveStateFileInfo != null)
{
text = Path.GetFileNameWithoutExtension(((FileSystemInfo)instance.saveStateFileInfo).Name);
}
}
catch
{
}
}
text = StripAutosave(text, word);
if (text.StartsWith("["))
{
text = "";
}
text = Clean(text).Replace("(", "").Replace(")", "").Trim();
if (text.Length > 80)
{
text = text.Substring(0, 80).Trim();
}
return text;
}
private static bool MoveToOld(string dir, string file)
{
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_0048: Expected O, but got Unknown
try
{
string text = Path.Combine(dir, "Old autosaves");
Directory.CreateDirectory(text);
string text2 = Path.Combine(text, Path.GetFileName(file));
if (File.Exists(text2))
{
File.Delete(text2);
}
File.Move(file, text2);
return true;
}
catch (Exception ex)
{
ManualLogSource l = Plugin.L;
bool flag = default(bool);
BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(51, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] could not move '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(Path.GetFileName(file));
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' to '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("': ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", left in place");
}
l.LogWarning(val);
return false;
}
}
private static void MoveStoreToOld(string dir, string name)
{
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_0065: Expected O, but got Unknown
string text = Path.Combine(dir, name + "_mod_savestore");
if (!Directory.Exists(text) || FileOf(dir, name) != null)
{
return;
}
try
{
string text2 = Path.Combine(dir, "Old autosaves", name + "_mod_savestore");
if (Directory.Exists(text2))
{
Directory.Delete(text2, recursive: true);
}
Directory.Move(text, text2);
}
catch (Exception ex)
{
ManualLogSource l = Plugin.L;
bool flag = default(bool);
BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(50, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] could not move '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(name);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("_mod_savestore' to '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("': ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
}
l.LogWarning(val);
}
}
private static void TrimOld(string dir)
{
//IL_0115: Unknown result type (might be due to invalid IL or missing references)
//IL_011c: Expected O, but got Unknown
//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
//IL_01b9: Expected O, but got Unknown
//IL_0231: Unknown result type (might be due to invalid IL or missing references)
//IL_0238: Expected O, but got Unknown
//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
//IL_00aa: Expected O, but got Unknown
string text = Path.Combine(dir, "Old autosaves");
if (!Directory.Exists(text))
{
return;
}
bool flag = default(bool);
try
{
foreach (FileInfo item in (from f in new DirectoryInfo(text).GetFiles()
where Exts.Contains(f.Extension.ToLowerInvariant())
orderby f.LastWriteTimeUtc descending
select f).ToList().Skip(10))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item.Name);
try
{
item.Delete();
ManualLogSource l = Plugin.L;
BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(57, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] deleted '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(item.Name);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' from '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' (keeping the newest ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(10);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" there)");
}
l.LogInfo(val);
}
catch (Exception ex)
{
ManualLogSource l2 = Plugin.L;
BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(40, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[AUTOSAVE] could not delete '");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item.Name);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("' from '");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("': ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
}
l2.LogWarning(val2);
continue;
}
string path = Path.Combine(text, fileNameWithoutExtension + "_mod_savestore");
if (!Directory.Exists(path) || FileOf(text, fileNameWithoutExtension) != null)
{
continue;
}
try
{
Directory.Delete(path, recursive: true);
}
catch (Exception ex2)
{
ManualLogSource l3 = Plugin.L;
BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(54, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[AUTOSAVE] could not delete '");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(fileNameWithoutExtension);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("_mod_savestore' from '");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("': ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex2.Message);
}
l3.LogWarning(val2);
}
}
}
catch (Exception ex3)
{
ManualLogSource l4 = Plugin.L;
BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(30, 2, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[AUTOSAVE] could not tidy '");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("': ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex3.Message);
}
l4.LogWarning(val2);
}
}
private static void Cap(string dir, string word, int slots, string keep)
{
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Expected O, but got Unknown
List<(FileInfo f, string name, int slot)> list = AllAutosaves(dir, word);
int num = list.Count - slots;
bool flag = false;
bool flag2 = default(bool);
foreach (var item in list)
{
if (num <= 0)
{
break;
}
if (item.name == keep)
{
continue;
}
num--;
if (MoveToOld(dir, item.f.FullName))
{
flag = true;
ManualLogSource l = Plugin.L;
BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(58, 3, ref flag2);
if (flag2)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] moved old autosave '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(item.f.Name);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' to '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' (keeping ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(slots);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" in total)");
}
l.LogInfo(val);
MoveStoreToOld(dir, item.name);
}
}
if (flag)
{
TrimOld(dir);
}
}
private static string? FileOf(string dir, string name)
{
string[] exts = Exts;
foreach (string text in exts)
{
string text2 = Path.Combine(dir, name + text);
if (File.Exists(text2))
{
return text2;
}
}
return null;
}
private void Save()
{
//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
//IL_02eb: Expected O, but got Unknown
//IL_0270: Unknown result type (might be due to invalid IL or missing references)
//IL_0277: Expected O, but got Unknown
manual = false;
playedSince = 0f;
movedSinceSave = false;
string text = Path.Combine(Application.persistentDataPath, "Save");
Directory.CreateDirectory(text);
string text2 = (((Object)(object)Game.Instance != (Object)null && Game.Instance.useSaveGameCompression) ? ".sodb" : ".sod");
string s = "";
try
{
s = (((Object)(object)CityData.Instance != (Object)null) ? CityData.Instance.cityName : "");
}
catch
{
}
s = Clean(s).Replace("(", "").Replace(")", "").Replace(",", "")
.Trim();
if (s.Length == 0)
{
s = "city";
}
int num = Mathf.Clamp(Plugin.Slots.Value, 2, 20);
string text3 = Clean(Plugin.Prefix.Value).Replace("(", "").Replace(")", "").Replace(",", "")
.Trim();
if (text3.Length == 0)
{
text3 = "Autosave";
}
string text4 = (Plugin.UseSaveName.Value ? BaseName(text, text3) : "");
List<(FileInfo, string, int)> list = AllAutosaves(text, text3);
List<(FileInfo, string, int)> source = list.Skip(Math.Max(0, list.Count - (num - 1))).ToList();
HashSet<int> used = new HashSet<int>(source.Select<(FileInfo, string, int), int>(((FileInfo f, string name, int slot) x) => x.slot));
int num2 = Enumerable.Range(1, num).First((int i) => !used.Contains(i));
string text5 = ((text4.Length > 0) ? $"{text4} ({text3} {num2}, {s})" : $"{text3} {num2} ({s})");
string text6 = Path.Combine(text, text5 + text2);
string[] exts = Exts;
bool flag = default(bool);
BepInExInfoLogInterpolatedStringHandler val;
foreach (string text7 in exts)
{
if (text7 == text2)
{
continue;
}
string text8 = Path.Combine(text, text5 + text7);
if (File.Exists(text8) && MoveToOld(text, text8))
{
ManualLogSource l = Plugin.L;
val = new BepInExInfoLogInterpolatedStringHandler(45, 2, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] moved '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(Path.GetFileName(text8));
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' (other save format) to '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("Old autosaves");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'");
}
l.LogInfo(val);
}
}
bool flag2 = File.Exists(text6);
ManualLogSource l2 = Plugin.L;
val = new BepInExInfoLogInterpolatedStringHandler(39, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[AUTOSAVE] saving '");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text5);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(flag2 ? " (overwriting)" : "");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(list.Count);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" autosaves before");
}
l2.LogInfo(val);
runningName = text5;
runningAt = Time.unscaledTime;
runningPath = text6;
longest = 0f;
runningDir = text;
runningWord = text3;
runningSlots = num;
Stopwatch stopwatch = Stopwatch.StartNew();
running = SaveStateController.Instance.CaptureSaveStateAsync(text6, flag2);
callMs = stopwatch.Elapsed.TotalMilliseconds;
}
private void Watch()
{
//IL_009f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a6: Expected O, but got Unknown
//IL_0191: Unknown result type (might be due to invalid IL or missing references)
//IL_0198: Expected O, but got Unknown
//IL_010c: Unknown result type (might be due to invalid IL or missing references)
//IL_0113: Expected O, but got Unknown
Task val = running;
longest = Mathf.Max(longest, Time.unscaledDeltaTime);
if (!val.IsCompleted)
{
if (Time.unscaledTime - runningAt > 120f)
{
Plugin.L.LogWarning((object)"[AUTOSAVE] save still not finished after 2 minutes, giving up waiting");
running = null;
}
return;
}
running = null;
float num = Time.unscaledTime - runningAt;
bool flag = default(bool);
if (val.IsFaulted)
{
string text = "";
try
{
AggregateException exception = val.Exception;
text = ((exception != null) ? ((Object)exception).ToString() : null) ?? "";
}
catch
{
}
ManualLogSource l = Plugin.L;
BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(31, 3, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[AUTOSAVE] '");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(runningName);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("' failed after ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(num, "0.0");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" s: ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text);
}
l.LogWarning(val2);
Plugin.Toast("Autosave failed, see the log.");
return;
}
ManualLogSource l2 = Plugin.L;
BepInExInfoLogInterpolatedStringHandler val3 = new BepInExInfoLogInterpolatedStringHandler(25, 2, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("[AUTOSAVE] saved '");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(runningName);
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("' in ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(num, "0.0");
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" s");
}
l2.LogInfo(val3);
double num2 = 0.0;
try
{
num2 = (double)new FileInfo(runningPath).Length / 1048576.0;
}
catch
{
}
ManualLogSource l3 = Plugin.L;
val3 = new BepInExInfoLogInterpolatedStringHandler(80, 5, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("[SAVETIME] '");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(runningName);
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("': call ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<double>(callMs, "0");
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" ms, longest frame after it ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(longest * 1000f, "0");
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" ms, finished after ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(num, "0.0");
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" s, file ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<double>(num2, "0.0");
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" MB");
}
l3.LogInfo(val3);
try
{
Cap(runningDir, runningWord, runningSlots, runningName);
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("[AUTOSAVE] clean-up failed: " + ex.Message));
}
if (Plugin.ShowMessage.Value)
{
Plugin.Toast("Autosaved: " + runningName);
}
}
}
public static class QuickMenuHook
{
private static readonly float[] Steps = new float[5] { 5f, 10f, 15f, 30f, 0f };
private static bool InGame
{
get
{
if ((Object)(object)SessionData.Instance != (Object)null && SessionData.Instance.startedGame)
{
return (Object)(object)Player.Instance != (Object)null;
}
return false;
}
}
private static string Name(float m)
{
if (!(m <= 0f) && Plugin.Enabled.Value)
{
return $"every {m:0} min";
}
return "off";
}
public static IEnumerable<(string, Func<string?>, Action)> Entries()
{
yield return ("autosave.now", () => (!InGame) ? null : "Save now (autosave slot)", delegate
{
Runner.Me?.SaveNow();
});
yield return ("autosave.interval", () => (!InGame) ? null : ("Autosave: " + Name(Plugin.Minutes.Value)), delegate
{
float cur = (Plugin.Enabled.Value ? Plugin.Minutes.Value : 0f);
int num = Array.FindIndex(Steps, (float s) => Math.Abs(s - cur) < 0.01f);
float num2 = Steps[(num + 1) % Steps.Length];
Plugin.Enabled.Value = num2 > 0f;
if (num2 > 0f)
{
Plugin.Minutes.Value = num2;
}
Plugin.Toast("Autosave: " + Name(Plugin.Minutes.Value));
});
}
}
}