Decompiled source of BetterQuicksave v0.1.1

plugins/com.github.LengSword.BetterQuicksave.dll

Decompiled 3 days ago
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.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Peak;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.LengSword.BetterQuicksave")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+2ff138f3561560025585a801f927de7dd55d6f1c")]
[assembly: AssemblyProduct("com.github.LengSword.BetterQuicksave")]
[assembly: AssemblyTitle("BetterQuicksave")]
[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.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]
	[Microsoft.CodeAnalysis.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]
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BetterQuicksave
{
	[BepInPlugin("com.github.LengSword.BetterQuicksave", "BetterQuicksave", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Id = "com.github.LengSword.BetterQuicksave";

		internal static ManualLogSource Logger { get; private set; }

		public static string Name => "BetterQuicksave";

		public static string Version => "0.1.1";

		private void Awake()
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)("Plugin " + Name + " is loaded!"));
			PluginConfig.Initialize(((BaseUnityPlugin)this).Config, ((BaseUnityPlugin)this).Logger);
			Harmony val = new Harmony("com.github.LengSword.BetterQuicksave");
			val.PatchAll();
		}
	}
	public static class PluginConfig
	{
		internal static ConfigEntry<bool> EnableBetterQuicksave { get; private set; }

		internal static ConfigEntry<bool> UnlimitedLoadQuicksave { get; private set; }

		internal static ConfigEntry<bool> BackupQuicksave { get; private set; }

		internal static ConfigEntry<int> MaxQuicksaveBackups { get; private set; }

		public static void Initialize(ConfigFile config, ManualLogSource logger)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			EnableBetterQuicksave = config.Bind<bool>("General", "Enable BetterQuicksave", true, "Enable or disable the BetterQuicksave plugin");
			UnlimitedLoadQuicksave = config.Bind<bool>("General", "Unlimited Load Quicksave", true, "Enable or disable keeping quicksave data after loading a game");
			BackupQuicksave = config.Bind<bool>("General", "Backup Quicksave", false, "Enable or disable backup of quicksaves");
			MaxQuicksaveBackups = config.Bind<int>("General", "Max Quicksave Backups", 3, new ConfigDescription("The maximum number of quicksave backups to keep. Older backups will be deleted.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			logger.LogInfo((object)"Plugin Config Loaded.");
		}
	}
}
namespace BetterQuicksave.Patches
{
	[HarmonyWrapSafe]
	internal sealed class QuicksavePatches
	{
		private const string BackupSubfolder = "QuicksaveBackups";

		private const string BackupFilePattern = "quicksave_*.peak";

		private const string BackupFileNameFormat = "quicksave_{0:yyyy-MM-dd_HH-mm-ss}.peak";

		private static string QuicksaveDirectory
		{
			get
			{
				string savePath = Quicksave.SavePath;
				if (string.IsNullOrWhiteSpace(savePath))
				{
					throw new InvalidOperationException("Quicksave.SavePath is null or empty.");
				}
				string directoryName = Path.GetDirectoryName(savePath);
				if (string.IsNullOrWhiteSpace(directoryName))
				{
					throw new InvalidOperationException("Quicksave.SavePath has no directory: " + savePath);
				}
				return directoryName;
			}
		}

		private static DirectoryInfo GetBackupDirectory()
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(QuicksaveDirectory, "QuicksaveBackups"));
			if (!directoryInfo.Exists)
			{
				directoryInfo.Create();
			}
			return directoryInfo;
		}

		private static FileInfo GetBackupFileInfo()
		{
			DirectoryInfo backupDirectory = GetBackupDirectory();
			string path = $"quicksave_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.peak";
			return new FileInfo(Path.Combine(backupDirectory.FullName, path));
		}

		private static void CleanupOldBackups()
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(QuicksaveDirectory, "QuicksaveBackups"));
			if (!directoryInfo.Exists)
			{
				return;
			}
			List<FileInfo> list = (from file in directoryInfo.EnumerateFiles("quicksave_*.peak")
				orderby file.Name descending
				select file).Skip(PluginConfig.MaxQuicksaveBackups.Value).ToList();
			foreach (FileInfo item in list)
			{
				try
				{
					item.Delete();
					Plugin.Logger.LogInfo((object)("Deleted old quicksave backup: " + item.FullName));
				}
				catch (IOException ex)
				{
					Plugin.Logger.LogWarning((object)("Failed to delete old quicksave backup: " + item.FullName + ". " + ex.Message));
				}
			}
		}

		[HarmonyPatch(typeof(Quicksave), "SaveNow")]
		[HarmonyPostfix]
		private static void QuicksaveSaveNowPatch()
		{
			if (!PluginConfig.EnableBetterQuicksave.Value || !PluginConfig.BackupQuicksave.Value)
			{
				return;
			}
			string savePath = Quicksave.SavePath;
			if (string.IsNullOrWhiteSpace(savePath))
			{
				Plugin.Logger.LogWarning((object)"Quicksave.SavePath is null or empty.");
				return;
			}
			if (!File.Exists(savePath))
			{
				Plugin.Logger.LogWarning((object)("Quicksave file not found: " + savePath));
				return;
			}
			try
			{
				FileInfo backupFileInfo = GetBackupFileInfo();
				File.Copy(savePath, backupFileInfo.FullName);
				Plugin.Logger.LogInfo((object)("Quicksave backed up to " + backupFileInfo.FullName));
				CleanupOldBackups();
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogError((object)("Failed to create quicksave backup: " + ex.Message));
			}
		}

		[HarmonyPatch(typeof(Quicksave), "FinalizeRunSetupAndSelfDestruct")]
		[HarmonyPrefix]
		private static bool QuicksaveFinalizeRunSetupAndSelfDestructPatch()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			if (!PluginConfig.EnableBetterQuicksave.Value || !PluginConfig.UnlimitedLoadQuicksave.Value)
			{
				return true;
			}
			RunManager.SetUpFromQuicksave(Guid.Parse(Quicksave.SavedRunId), Quicksave.SavedRun.runTimer);
			DayNightManager.instance.timeOfDay = Quicksave.SavedRun.timeOfDay;
			return false;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}