Decompiled source of HalloweenRunToggle v1.0.2

plugins/HalloweenRunToggle.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.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 KanOrn.HalloweenRunToggle
{
	[BepInPlugin("kan_orn.repo.halloweenruntoggle", "Halloween Run Toggle", "1.0.2")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "kan_orn.repo.halloweenruntoggle";

		private Harmony harmony;

		private GameObject lobbyUi;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			harmony = new Harmony("kan_orn.repo.halloweenruntoggle");
			harmony.PatchAll();
			lobbyUi = new GameObject("kan_orn.repo.halloweenruntoggle");
			Object.DontDestroyOnLoad((Object)(object)lobbyUi);
			lobbyUi.AddComponent<LobbyControl>();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Halloween Run Toggle ready.");
		}

		private void OnDestroy()
		{
			if (Object.op_Implicit((Object)(object)lobbyUi))
			{
				Object.Destroy((Object)(object)lobbyUi);
			}
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	internal static class LobbyEvent
	{
		private readonly struct RoomIdentity
		{
			private readonly Room room;

			internal RoomIdentity(Room room)
			{
				this.room = room;
			}

			internal bool Matches(Room current)
			{
				return room == current;
			}
		}

		private static Room selectionRoom;

		private static bool enabled;

		private static GameManager forcedManager;

		private static RoomIdentity forcedRoom;

		private static GameEvents previousEvent;

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

		private static bool IsHostLobby()
		{
			if (PhotonNetwork.InRoom && PhotonNetwork.IsMasterClient && Object.op_Implicit((Object)(object)RunManager.instance) && !Restarting.Invoke(RunManager.instance))
			{
				if (!SemiFunc.RunIsLobby())
				{
					return SemiFunc.RunIsLobbyMenu();
				}
				return true;
			}
			return false;
		}

		private static bool PrepareSelection()
		{
			if (!IsHostLobby())
			{
				return false;
			}
			Room currentRoom = PhotonNetwork.CurrentRoom;
			if (selectionRoom != currentRoom)
			{
				selectionRoom = currentRoom;
				enabled = false;
				Plugin.Log.LogInfo((object)"Halloween control available in the host lobby.");
			}
			return true;
		}

		internal static void HandleInput()
		{
			if (PrepareSelection() && Input.GetKeyDown((KeyCode)291))
			{
				Toggle();
			}
		}

		private static void Toggle()
		{
			enabled = !enabled;
			Plugin.Log.LogInfo((object)(enabled ? "Halloween selected for the next run." : "Seasonal default selected."));
		}

		internal static void Draw(LobbyControl control)
		{
			if (PrepareSelection() && control.DrawPanel(enabled))
			{
				Toggle();
			}
		}

		internal static void BeforeLevelChange(RunManager run, bool failed, ChangeLevelType change)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Invalid comparison between Unknown and I4
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Invalid comparison between Unknown and I4
			if (!(!enabled || failed) && IsHostLobby() && !((Object)(object)run != (Object)(object)RunManager.instance) && selectionRoom == PhotonNetwork.CurrentRoom && Object.op_Implicit((Object)(object)GameManager.instance) && ((int)change == 1 || (int)change == 0 || (int)change == 5))
			{
				GameManager instance = GameManager.instance;
				previousEvent = instance.currentGameEvent;
				instance.GameEventSet((GameEvents)1);
				forcedManager = instance;
				forcedRoom = new RoomIdentity(PhotonNetwork.CurrentRoom);
				enabled = false;
				Plugin.Log.LogInfo((object)"Halloween selected for this run. Vanilla event sync will send it during level loading.");
			}
		}

		internal static void RestoreAfterRun(RunManager run)
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Invalid comparison between Unknown and I4
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)forcedManager))
			{
				return;
			}
			Room currentRoom = PhotonNetwork.CurrentRoom;
			bool flag = forcedRoom.Matches(currentRoom);
			if (flag && !PhotonNetwork.IsMasterClient)
			{
				forcedManager = null;
			}
			else if ((!flag || !((Object)(object)run.levelCurrent != (Object)(object)run.levelLobby) || !((Object)(object)run.levelCurrent != (Object)(object)run.levelLobbyMenu) || !((Object)(object)run.levelCurrent != (Object)(object)run.levelMainMenu)) && (!flag || !Restarting.Invoke(run)))
			{
				if ((int)forcedManager.currentGameEvent == 1)
				{
					forcedManager.GameEventSet(previousEvent);
				}
				forcedManager = null;
				enabled = false;
				Plugin.Log.LogInfo((object)"Restored the seasonal event setting.");
			}
		}
	}
	public sealed class LobbyControl : MonoBehaviour
	{
		private static readonly Color Amber = new Color(1f, 0.71f, 0.27f);

		private static readonly Color Paper = new Color(0.96f, 0.92f, 0.85f);

		private static readonly Color Muted = new Color(0.73f, 0.7f, 0.63f);

		private Texture2D artwork;

		private GUIStyle title;

		private GUIStyle caption;

		private GUIStyle state;

		private GUIStyle key;

		private void Awake()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			using Stream stream = typeof(LobbyControl).Assembly.GetManifestResourceStream("HalloweenIcon.png");
			if (stream != null)
			{
				byte[] array = new byte[(int)stream.Length];
				using (BinaryReader binaryReader = new BinaryReader(stream))
				{
					array = binaryReader.ReadBytes(array.Length);
				}
				artwork = new Texture2D(2, 2);
				if (!ImageConversion.LoadImage(artwork, array, true))
				{
					Object.Destroy((Object)(object)artwork);
					artwork = null;
				}
			}
		}

		private void OnDestroy()
		{
			if (Object.op_Implicit((Object)(object)artwork))
			{
				Object.Destroy((Object)(object)artwork);
			}
		}

		private void Update()
		{
			LobbyEvent.HandleInput();
		}

		private void OnGUI()
		{
			LobbyEvent.Draw(this);
		}

		internal bool DrawPanel(bool enabled)
		{
			//IL_035a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0360: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0317: Unknown result type (might be due to invalid IL or missing references)
			//IL_031c: Unknown result type (might be due to invalid IL or missing references)
			//IL_032c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			if (title == null)
			{
				title = Label(20, (FontStyle)1, (TextAnchor)3, Paper);
				caption = Label(12, (FontStyle)1, (TextAnchor)3, Muted);
				state = Label(12, (FontStyle)1, (TextAnchor)5, Muted);
				key = Label(12, (FontStyle)1, (TextAnchor)4, Paper);
			}
			float num = Mathf.Min(Mathf.Clamp((float)Screen.height / 900f, 0.65f, 1.45f), (float)Screen.width / 380f);
			Rect val = default(Rect);
			((Rect)(ref val))..ctor((float)Screen.width / num - 358f, (float)Screen.height / num - 94f, 340f, 76f);
			Matrix4x4 matrix = GUI.matrix;
			Color color = GUI.color;
			int depth = GUI.depth;
			try
			{
				GUI.matrix = Matrix4x4.Scale(new Vector3(num, num, 1f));
				GUI.depth = -100;
				GUI.color = Color.white;
				bool flag = ((Rect)(ref val)).Contains(Event.current.mousePosition);
				Color color2 = (Color)(enabled ? Amber : new Color(0.49f, 0.45f, 0.36f));
				Fill(new Rect(((Rect)(ref val)).x + 3f, ((Rect)(ref val)).y + 4f, ((Rect)(ref val)).width, ((Rect)(ref val)).height), new Color(0f, 0f, 0f, 0.55f));
				Fill(val, color2);
				Fill(new Rect(((Rect)(ref val)).x + 4f, ((Rect)(ref val)).y + 1f, ((Rect)(ref val)).width - 5f, ((Rect)(ref val)).height - 2f), flag ? new Color(0.18f, 0.13f, 0.1f) : new Color(0.105f, 0.084f, 0.074f));
				if (Object.op_Implicit((Object)(object)artwork))
				{
					GUI.DrawTexture(new Rect(((Rect)(ref val)).x + 12f, ((Rect)(ref val)).y + 10f, 56f, 56f), (Texture)(object)artwork);
				}
				GUI.Label(new Rect(((Rect)(ref val)).x + 80f, ((Rect)(ref val)).y + 10f, 125f, 23f), "HALLOWEEN", title);
				GUI.Label(new Rect(((Rect)(ref val)).x + 80f, ((Rect)(ref val)).y + 34f, 142f, 21f), "Please the Moon", caption);
				state.normal.textColor = (enabled ? Amber : Muted);
				GUI.Label(new Rect(((Rect)(ref val)).x + 197f, ((Rect)(ref val)).y + 10f, 128f, 23f), enabled ? "ON FOR THIS RUN" : "SEASONAL DEFAULT", state);
				Rect val2 = new Rect(((Rect)(ref val)).xMax - 56f, ((Rect)(ref val)).y + 39f, 41f, 23f);
				Fill(val2, new Color(0.25f, 0.19f, 0.14f));
				GUI.Label(val2, "F10", key);
				return GUI.Button(val, GUIContent.none, GUIStyle.none);
			}
			finally
			{
				GUI.matrix = matrix;
				GUI.color = color;
				GUI.depth = depth;
			}
		}

		private static GUIStyle Label(int size, FontStyle weight, TextAnchor alignment, Color color)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			GUIStyle val = new GUIStyle(GUI.skin.label)
			{
				fontSize = size,
				fontStyle = weight,
				alignment = alignment,
				padding = new RectOffset(0, 0, 0, 0)
			};
			val.normal.textColor = color;
			return val;
		}

		private static void Fill(Rect rect, Color color)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			GUI.color = color;
			GUI.DrawTexture(rect, (Texture)(object)Texture2D.whiteTexture);
			GUI.color = Color.white;
		}
	}
	[HarmonyPatch(typeof(RunManager), "ChangeLevel")]
	internal static class BeginRunPatch
	{
		private static void Prefix(RunManager __instance, bool __1, ChangeLevelType __2)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			LobbyEvent.BeforeLevelChange(__instance, __1, __2);
		}
	}
	[HarmonyPatch(typeof(RunManager), "Update")]
	internal static class EndRunPatch
	{
		private static void Postfix(RunManager __instance)
		{
			LobbyEvent.RestoreAfterRun(__instance);
		}
	}
}