Decompiled source of ModSaveBackups v1.0.2

ModSaveBackups.dll

Decompiled 2 months ago
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using SailwindModdingHelper;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ModSaveBackups")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModSaveBackups")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("144ea274-5a8d-4bce-917f-b42e2d203bc2")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.2.0")]
namespace ModSaveBackups;

internal class ModSaveSlots
{
	public static void PushBackups(PluginInfo pluginInfo)
	{
		int num = 5;
		string backupPath = GetBackupPath(pluginInfo, SaveSlots.currentSlot, num);
		if (File.Exists(backupPath))
		{
			File.Delete(backupPath);
		}
		for (int num2 = num - 1; num2 > 0; num2--)
		{
			string backupPath2 = GetBackupPath(pluginInfo, SaveSlots.currentSlot, num2);
			if (File.Exists(backupPath2))
			{
				File.Move(backupPath2, GetBackupPath(pluginInfo, SaveSlots.currentSlot, num2 + 1));
			}
		}
		string currentSaveModFile = GetCurrentSaveModFile(pluginInfo);
		if (File.Exists(currentSaveModFile))
		{
			File.Move(currentSaveModFile, GetBackupPath(pluginInfo, SaveSlots.currentSlot, 1));
		}
	}

	private static string GetBackupPath(PluginInfo pluginInfo, int slot, int backupIndex)
	{
		return Path.Combine(ModSave.GetSaveDirectory(slot), pluginInfo.Metadata.GUID + "_backup" + backupIndex + ".save");
	}

	public static string GetCurrentSaveModFile(PluginInfo pluginInfo)
	{
		return Path.Combine(ModSave.GetSaveDirectory(SaveSlots.currentSlot), pluginInfo.Metadata.GUID + ".save");
	}

	public static string GetBackupSaveModFile(PluginInfo pluginInfo, int slot, int backupIndex)
	{
		for (int num = backupIndex; num > 0; num--)
		{
			string backupPath = GetBackupPath(pluginInfo, slot, num);
			if (File.Exists(backupPath))
			{
				Plugin.logger.LogDebug((object)("Loading SaveMod backup file path: " + backupPath));
				return backupPath;
			}
		}
		Plugin.logger.LogDebug((object)$"Loading current {pluginInfo} SaveMod file");
		return GetCurrentSaveModFile(pluginInfo);
	}
}
[BepInPlugin("com.raddude82.modsavebackups", "ModSaveBackups", "1.0.2")]
[BepInDependency("com.app24.sailwindmoddinghelper", "2.1.1")]
public class Plugin : BaseUnityPlugin
{
	public const string PLUGIN_GUID = "com.raddude82.modsavebackups";

	public const string PLUGIN_NAME = "ModSaveBackups";

	public const string PLUGIN_VERSION = "1.0.2";

	public const string SMH_GUID = "com.app24.sailwindmoddinghelper";

	public const string SMH_VERSION = "2.1.1";

	internal static Plugin instance;

	internal static ManualLogSource logger;

	private void Awake()
	{
		instance = this;
		logger = ((BaseUnityPlugin)this).Logger;
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.raddude82.modsavebackups");
	}
}
internal class SaveLoadPatches
{
	[HarmonyPatch(typeof(ModSave))]
	private class ModSavePatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("Save")]
		public static void SavePatch(PluginInfo pluginInfo)
		{
			ModSaveSlots.PushBackups(pluginInfo);
			backupIndex = 0;
		}

		[HarmonyPrefix]
		[HarmonyPatch("GetSaveModFile")]
		public static bool GetSaveModFilePatch(ref string __result, int slot, PluginInfo pluginInfo)
		{
			if (backupIndex == 0)
			{
				return true;
			}
			__result = ModSaveSlots.GetBackupSaveModFile(pluginInfo, slot, backupIndex);
			return false;
		}
	}

	[HarmonyPatch(typeof(SaveLoadManager))]
	private class SaveLoadManagerPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void AwakePatch()
		{
			backupIndex = 0;
		}

		[HarmonyPrefix]
		[HarmonyPatch("LoadGame")]
		public static void LoadGamePatch(int backupIndex)
		{
			SaveLoadPatches.backupIndex = backupIndex;
		}
	}

	private static int backupIndex;
}