Decompiled source of RetryFromLastSave v1.0.1

RetryFromLastSave.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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("RetryFromLastSave")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("RetryFromLastSave")]
[assembly: AssemblyTitle("RetryFromLastSave")]
[assembly: AssemblyVersion("1.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 RetryFromLastSave
{
	[BepInPlugin("RetryFromLastSave", "RetryFromLastSave", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "RetryFromLastSave";

		public const string Name = "RetryFromLastSave";

		public const string Version = "1.0.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> SkipArena;

		private void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "When the whole team dies, keep the save and continue from it instead of losing the run.");
			SkipArena = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SkipArena", false, "Skip the arena after a team wipe and restart the level from the last save right away.");
			new Harmony("RetryFromLastSave").PatchAll();
		}
	}
	internal static class Retry
	{
		private static readonly FieldRef<StatsManager, string> SaveFileCurrent = AccessTools.FieldRefAccess<StatsManager, string>("saveFileCurrent");

		internal static bool SaveBlocked;

		internal static bool RestoreAfterSwitch;

		internal static string PendingSave;

		internal static bool Active
		{
			get
			{
				if (Plugin.Enabled.Value)
				{
					return SemiFunc.IsMasterClientOrSingleplayer();
				}
				return false;
			}
		}

		internal static string FindSave()
		{
			string text = SaveFileCurrent.Invoke(StatsManager.instance);
			if (string.IsNullOrEmpty(text) || !File.Exists(Path.Combine(Application.persistentDataPath, "saves", text, text + ".es3")))
			{
				return null;
			}
			return text;
		}

		internal static void Restore(string save)
		{
			StatsManager.instance.ResetAllStats();
			StatsManager.instance.LoadGame(save, (List<string>)null);
		}
	}
	[HarmonyPatch(typeof(RunManager), "ChangeLevel")]
	internal static class ChangeLevelPatch
	{
		private static readonly FieldRef<RunManager, bool> Restarting = AccessTools.FieldRefAccess<RunManager, bool>("restarting");

		private static readonly FieldRef<RunManager, bool> MoonLevelChanged = AccessTools.FieldRefAccess<RunManager, bool>("moonLevelChanged");

		private static void Prefix(RunManager __instance, ref bool _completedLevel, ref bool _levelFailed, ref ChangeLevelType _changeLevelType, out bool? __state)
		{
			__state = null;
			if (!Retry.Active || !_levelFailed || Restarting.Invoke(__instance))
			{
				return;
			}
			string text = Retry.FindSave();
			if (text == null)
			{
				return;
			}
			Level levelCurrent = __instance.levelCurrent;
			if (SemiFunc.IsLevelArena(levelCurrent))
			{
				__state = MoonLevelChanged.Invoke(__instance);
				Retry.PendingSave = text;
				Retry.RestoreAfterSwitch = true;
				Plugin.Log.LogInfo((object)("Arena over, continuing from save '" + text + "'."));
			}
			else if (SemiFunc.RunIsLevel(levelCurrent) && !((Object)(object)levelCurrent == (Object)(object)__instance.levelRecording))
			{
				Retry.SaveBlocked = true;
				if (!Plugin.SkipArena.Value)
				{
					Plugin.Log.LogInfo((object)("Team wiped, save '" + text + "' is kept for after the arena."));
					return;
				}
				Retry.Restore(text);
				Retry.PendingSave = text;
				Retry.RestoreAfterSwitch = true;
				_completedLevel = false;
				_levelFailed = false;
				_changeLevelType = (ChangeLevelType)1;
				Plugin.Log.LogInfo((object)$"Team wiped, restarting from save '{text}' at level {__instance.levelsCompleted + 1}.");
			}
		}

		private static void Finalizer(RunManager __instance, bool? __state)
		{
			Retry.SaveBlocked = false;
			if (Retry.RestoreAfterSwitch)
			{
				Retry.RestoreAfterSwitch = false;
				Retry.Restore(Retry.PendingSave);
				if (__state.HasValue)
				{
					__instance.UpdateMoonLevel();
					MoonLevelChanged.Invoke(__instance) = __state.Value;
				}
				if (SemiFunc.IsMultiplayer())
				{
					SemiFunc.StatSyncAll();
				}
			}
		}
	}
	[HarmonyPatch(typeof(StatsManager), "SaveFileSave")]
	internal static class SaveFileSavePatch
	{
		private static bool Prefix()
		{
			return !Retry.SaveBlocked;
		}
	}
	[HarmonyPatch(typeof(DataDirector), "SaveDeleteCheck")]
	internal static class SaveDeleteCheckPatch
	{
		private static bool Prefix(bool _leaveGame)
		{
			if (!Retry.Active)
			{
				return true;
			}
			if (_leaveGame && RunManager.instance.levelsCompleted == 0)
			{
				return !SemiFunc.RunIsArena();
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(LevelGenerator), "Start")]
	internal static class LevelGeneratorStartPatch
	{
		private static void Prefix()
		{
			if (Retry.PendingSave != null && SemiFunc.IsMasterClientOrSingleplayer())
			{
				Retry.Restore(Retry.PendingSave);
				Retry.PendingSave = null;
				if (SemiFunc.IsMultiplayer() && Object.op_Implicit((Object)(object)PunManager.instance))
				{
					SemiFunc.StatSyncAll();
				}
				Plugin.Log.LogInfo((object)"Save restored for the new level.");
			}
		}
	}
}