Decompiled source of Return to Bench v1.0.5

ReturnToBench.dll

Decompiled 14 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TeamCherry.Localization;
using UnityEngine;
using UnityEngine.UI;

[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("ReturnToBench")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyInformationalVersion("1.0.5+02962c0fb3561283f159dbe7dbd97e623b5bf397")]
[assembly: AssemblyProduct("ReturnToBench")]
[assembly: AssemblyTitle("Silksong.ReturnToBench")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.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 Silksong.ReturnToBench
{
	[HarmonyPatch]
	[BepInPlugin("silksong.returntobench", "Return to Bench", "1.0.5")]
	public class ReturnToBenchPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <GameManagerPauseGameToggleByMenu>d__15 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public IEnumerator __result;

			public GameManager __instance;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <GameManagerPauseGameToggleByMenu>d__15(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if (__result.MoveNext())
				{
					<>2__current = __result.Current;
					<>1__state = 1;
					return true;
				}
				if (returningToBench)
				{
					StopAllSceneMusic();
					HeroController.instance.cState.dead = true;
					__instance.needFirstFadeIn = true;
					__instance.ReadyForRespawn(false);
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private static ReturnToBenchPlugin instance;

		private static ManualLogSource logger;

		private static GameObject? returnButton;

		private static Entry? entry;

		private static bool returningToBench;

		private const string PAUSE_BENCH = "PAUSE_BENCH";

		private Harmony? harmony;

		public const string Id = "silksong.returntobench";

		public static string Name => "Return to Bench";

		public static string Version => "1.0.5";

		private void Awake()
		{
			instance = this;
			logger = ((BaseUnityPlugin)this).Logger;
			harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}

		private void Start()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method(typeof(Language), "SwitchLanguage", new Type[1] { typeof(LanguageCode) }, (Type[])null);
			HarmonyMethod val = new HarmonyMethod(typeof(ReturnToBenchPlugin), "LanguageSwitchLanguage", (Type[])null);
			Harmony? obj = harmony;
			if (obj != null)
			{
				obj.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private void OnDestroy()
		{
			Harmony? obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if (Object.op_Implicit((Object)(object)returnButton))
			{
				Object.Destroy((Object)(object)returnButton);
			}
			if (entry != null && Object.op_Implicit((Object)(object)UIManager._instance))
			{
				MenuButtonList component = ((Component)UIManager._instance.pauseMenuScreen).GetComponent<MenuButtonList>();
				List<Entry> list = component.entries.ToList();
				list.Remove(entry);
				component.entries = list.ToArray();
			}
		}

		private void Update()
		{
			if (!Object.op_Implicit((Object)(object)returnButton))
			{
				CreateReturnButton();
			}
			if (returningToBench && Object.op_Implicit((Object)(object)GameManager._instance) && GameManager._instance.sceneName == "Menu_Title")
			{
				returningToBench = false;
			}
		}

		private static void CreateReturnButton()
		{
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Expected O, but got Unknown
			MenuScreen val = UIManager._instance?.pauseMenuScreen;
			if (Object.op_Implicit((Object)(object)val))
			{
				Transform obj = ((Component)val).transform.Find("Container/Controls");
				Transform val2 = ((obj != null) ? obj.Find("ContinueButton") : null);
				Transform val3 = ((obj != null) ? obj.Find("ExitButton") : null);
				if (Object.op_Implicit((Object)(object)val2) && Object.op_Implicit((Object)(object)val3))
				{
					LanguageSwitchLanguage(Language._currentLanguage);
					returnButton = Object.Instantiate<GameObject>(((Component)val2).gameObject, val2.parent);
					((Object)returnButton).name = "ReturnToBenchButton";
					returnButton.transform.SetSiblingIndex(val3.GetSiblingIndex());
					((Component)val3).transform.SetSiblingIndex(val3.GetSiblingIndex() + 1);
					PauseMenuButton component = returnButton.GetComponent<PauseMenuButton>();
					MenuButtonList component2 = ((Component)val).GetComponent<MenuButtonList>();
					entry = new Entry
					{
						selectable = (Selectable)(object)component
					};
					List<Entry> list = component2.entries.ToList();
					list.Insert(list.Count - 1, entry);
					component2.entries = list.ToArray();
					AutoLocalizeTextUI componentInChildren = returnButton.GetComponentInChildren<AutoLocalizeTextUI>();
					componentInChildren.TextSheet = "silksong.returntobench";
					componentInChildren.TextKey = "PAUSE_BENCH";
				}
			}
		}

		private static void LanguageSwitchLanguage(LanguageCode code)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Invalid comparison between Unknown and I4
			Language._currentEntrySheets?.TryAdd("silksong.returntobench", new Dictionary<string, string> { 
			{
				"PAUSE_BENCH",
				((int)code == 199) ? "返回长椅" : "Return to Bench"
			} });
		}

		[HarmonyPatch(typeof(PauseMenuButton), "OnSubmit")]
		[HarmonyPostfix]
		private static void PauseMenuButtonOnSubmit(PauseMenuButton __instance)
		{
			if ((Object)(object)((Component)__instance).gameObject == (Object)(object)returnButton && ((Selectable)__instance).interactable && __instance.ih.PauseAllowed && !UIManager.instance.ignoreUnpause)
			{
				returningToBench = true;
			}
			else
			{
				returningToBench = false;
			}
		}

		[HarmonyPatch(typeof(HeroController), "CanTakeDamage")]
		[HarmonyPostfix]
		private static void HeroControllerCanTakeDamage(ref bool __result)
		{
			if (returningToBench)
			{
				__result = false;
			}
		}

		[IteratorStateMachine(typeof(<GameManagerPauseGameToggleByMenu>d__15))]
		[HarmonyPatch(typeof(GameManager), "PauseGameToggleByMenu")]
		[HarmonyPostfix]
		private static IEnumerator GameManagerPauseGameToggleByMenu(IEnumerator __result, GameManager __instance)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <GameManagerPauseGameToggleByMenu>d__15(0)
			{
				__result = __result,
				__instance = __instance
			};
		}

		[HarmonyPatch(typeof(HeroController), "FinishedEnteringScene")]
		[HarmonyPostfix]
		private static void HeroControllerFinishedEnteringScene(HeroController __instance)
		{
			if (returningToBench)
			{
				returningToBench = false;
				__instance.proxyFSM.SendEvent("HeroCtrl-EnteringScene");
			}
		}

		private static void StopAllSceneMusic()
		{
			GameManager obj = GameManager.instance;
			obj.AudioManager.StopAndClearMusic();
			obj.AudioManager.StopAndClearAtmos();
			Transform val = ((Component)obj.AudioManager).transform.Find("Music");
			if (Object.op_Implicit((Object)(object)val))
			{
				Transform obj2 = val.Find("RestArea");
				PlayMakerFSM val2 = ((obj2 != null) ? ((Component)obj2).GetComponent<PlayMakerFSM>() : null);
				if (Object.op_Implicit((Object)(object)val2))
				{
					val2.fsm.Finished = false;
					Extensions.SendEventSafe(val2, "REST AREA MUSIC STOP FAST");
				}
				Transform obj3 = val.Find("FleaCaravan");
				PlayMakerFSM val3 = ((obj3 != null) ? ((Component)obj3).GetComponent<PlayMakerFSM>() : null);
				if (Object.op_Implicit((Object)(object)val3))
				{
					val3.fsm.Finished = false;
					Extensions.SendEventSafe(val3, "FLEA MUSIC STOP FAST");
				}
			}
		}
	}
}