Decompiled source of SurvivorRandomizer v1.1.1

BepInEx/plugins/SurvivorRandomizer/SurvivorRandomizer.dll

Decompiled 14 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using Microsoft.CodeAnalysis;
using On.RoR2.UI;
using RoR2;
using RoR2.UI;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
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("SurvivorRandomizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1+76fd8f13a78c3c6521b42e6c8f6fed26bec9cf66")]
[assembly: AssemblyProduct("SurvivorRandomizer")]
[assembly: AssemblyTitle("SurvivorRandomizer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 SurvivorRandomizer
{
	[BepInPlugin("com.cirillom.survivorrandomizer", "Survivor Randomizer", "1.1.1")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.cirillom.survivorrandomizer";

		public const string PluginName = "Survivor Randomizer";

		public const string PluginVersion = "1.1.1";

		private CharacterSelectController? _characterSelectController;

		private void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Survivor Randomizer loaded!");
			CharacterSelectController.Awake += new hook_Awake(CharacterSelectControllerAwake);
		}

		private void CharacterSelectControllerAwake(orig_Awake orig, CharacterSelectController self)
		{
			orig.Invoke(self);
			_characterSelectController = self;
			((MonoBehaviour)this).StartCoroutine(CreateButtons(self));
		}

		private IEnumerator CreateButtons(CharacterSelectController controller)
		{
			yield return null;
			yield return null;
			if (!Object.op_Implicit((Object)(object)controller) || (Object)(object)controller.readyButton == (Object)null)
			{
				yield break;
			}
			Transform parent = ((Component)controller.readyButton).transform.parent;
			RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find ReadyPanel.");
				yield break;
			}
			GameObject val2 = new GameObject("SurvivorRandomizerPanel", new Type[1] { typeof(RectTransform) })
			{
				layer = ((Component)val).gameObject.layer
			};
			val2.transform.SetParent(((Transform)val).parent, false);
			RectTransform component = val2.GetComponent<RectTransform>();
			component.anchorMin = val.anchorMin;
			component.anchorMax = val.anchorMax;
			component.pivot = val.pivot;
			component.anchoredPosition = val.anchoredPosition + new Vector2(0f, 85f);
			component.sizeDelta = new Vector2(500f, 54f);
			((Transform)component).localScale = Vector3.one;
			if (IsEclipseRun())
			{
				CreateLobbyButton(controller, component, "SurvivorRandomizerRandom", "RANDOM SURVIVOR", new Vector2(-120f, 0f), new Color(0.1f, 0.3f, 0.48f, 1f), new UnityAction(RandomizeSurvivor));
				CreateLobbyButton(controller, component, "SurvivorRandomizerLowest", "LOWEST ECLIPSE", new Vector2(120f, 0f), new Color(0.48f, 0.3f, 0.08f, 1f), new UnityAction(RandomizeLowestEclipse));
			}
			else
			{
				CreateLobbyButton(controller, component, "SurvivorRandomizerRandom", "RANDOM SURVIVOR", Vector2.zero, new Color(0.1f, 0.3f, 0.48f, 1f), new UnityAction(RandomizeSurvivor));
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Created survivor randomizer panel.");
		}

		private void CreateLobbyButton(CharacterSelectController controller, RectTransform parent, string objectName, string text, Vector2 position, Color normalColor, UnityAction action)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			MPButton readyButton = controller.readyButton;
			Image component = ((Component)readyButton).GetComponent<Image>();
			TextMeshProUGUI componentInChildren = ((Component)readyButton).GetComponentInChildren<TextMeshProUGUI>(true);
			GameObject val = new GameObject(objectName, new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			val.layer = ((Component)readyButton).gameObject.layer;
			val.transform.SetParent((Transform)(object)parent, false);
			RectTransform component2 = val.GetComponent<RectTransform>();
			component2.anchorMin = new Vector2(0.5f, 0.5f);
			component2.anchorMax = new Vector2(0.5f, 0.5f);
			component2.pivot = new Vector2(0.5f, 0.5f);
			component2.anchoredPosition = position;
			component2.sizeDelta = new Vector2(225f, 50f);
			((Transform)component2).localScale = Vector3.one;
			Image component3 = val.GetComponent<Image>();
			component3.sprite = component.sprite;
			component3.type = component.type;
			val.AddComponent<MPEventSystemLocator>();
			MPButton obj = val.AddComponent<MPButton>();
			((Selectable)obj).targetGraphic = (Graphic)(object)component3;
			((Selectable)obj).transition = ((Selectable)readyButton).transition;
			Navigation navigation = default(Navigation);
			((Navigation)(ref navigation)).mode = (Mode)0;
			((Selectable)obj).navigation = navigation;
			((Selectable)obj).interactable = true;
			((UnityEvent)((Button)obj).onClick).AddListener(action);
			ColorBlock colors = ((Selectable)readyButton).colors;
			((ColorBlock)(ref colors)).normalColor = normalColor;
			((ColorBlock)(ref colors)).highlightedColor = Color.Lerp(normalColor, Color.white, 0.3f);
			((ColorBlock)(ref colors)).selectedColor = ((ColorBlock)(ref colors)).highlightedColor;
			((ColorBlock)(ref colors)).pressedColor = Color.Lerp(normalColor, Color.black, 0.25f);
			((ColorBlock)(ref colors)).disabledColor = new Color(normalColor.r, normalColor.g, normalColor.b, 0.35f);
			((Selectable)obj).colors = colors;
			GameObject val2 = new GameObject("Label", new Type[2]
			{
				typeof(RectTransform),
				typeof(TextMeshProUGUI)
			})
			{
				layer = val.layer
			};
			val2.transform.SetParent(val.transform, false);
			TextMeshProUGUI component4 = val2.GetComponent<TextMeshProUGUI>();
			if ((Object)(object)componentInChildren != (Object)null)
			{
				((TMP_Text)component4).font = ((TMP_Text)componentInChildren).font;
				((TMP_Text)component4).fontSharedMaterial = ((TMP_Text)componentInChildren).fontSharedMaterial;
				((TMP_Text)component4).fontStyle = ((TMP_Text)componentInChildren).fontStyle;
			}
			((TMP_Text)component4).text = text;
			((Graphic)component4).color = Color.white;
			((TMP_Text)component4).alignment = (TextAlignmentOptions)514;
			((TMP_Text)component4).enableAutoSizing = true;
			((TMP_Text)component4).fontSizeMin = 14f;
			((TMP_Text)component4).fontSizeMax = 22f;
			((Graphic)component4).raycastTarget = false;
			RectTransform rectTransform = ((TMP_Text)component4).rectTransform;
			rectTransform.anchorMin = Vector2.zero;
			rectTransform.anchorMax = Vector2.one;
			rectTransform.offsetMin = new Vector2(10f, 4f);
			rectTransform.offsetMax = new Vector2(-10f, -4f);
		}

		private void RandomizeSurvivor()
		{
			SurvivorDef[] eligibleSurvivors = GetEligibleSurvivors();
			if (eligibleSurvivors.Length == 0)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"No eligible survivors found.");
				return;
			}
			SurvivorDef val = eligibleSurvivors[Random.Range(0, eligibleSurvivors.Length)];
			SelectSurvivor(val);
			((BaseUnityPlugin)this).Logger.LogInfo((object)(IsEclipseRun() ? $"Random survivor: {val.cachedName} — Eclipse {GetNextEclipseLevel(val)}" : ("Random survivor: " + val.cachedName)));
		}

		private void RandomizeLowestEclipse()
		{
			if (!IsEclipseRun())
			{
				return;
			}
			LocalUser localUser = LocalUserManager.GetFirstLocalUser();
			if (localUser == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find local user.");
				return;
			}
			var array = (from survivor in GetEligibleSurvivors()
				select new
				{
					Survivor = survivor,
					CompletedLevel = EclipseRun.GetLocalUserSurvivorCompletedEclipseLevel(localUser, survivor)
				} into entry
				where entry.CompletedLevel < 8
				select entry).ToArray();
			if (array.Length == 0)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Eclipse 8 completed on every available survivor.");
				return;
			}
			int lowestNextLevel = array.Min(entry => entry.CompletedLevel + 1);
			SurvivorDef[] array2 = (from entry in array
				where entry.CompletedLevel + 1 == lowestNextLevel
				select entry.Survivor).ToArray();
			SurvivorDef val = array2[Random.Range(0, array2.Length)];
			SelectSurvivor(val);
			((BaseUnityPlugin)this).Logger.LogInfo((object)($"Lowest Eclipse: selected {val.cachedName} at E{lowestNextLevel} " + $"from {array2.Length} candidate(s)."));
		}

		private SurvivorDef[] GetEligibleSurvivors()
		{
			LocalUser localUser = LocalUserManager.GetFirstLocalUser();
			if (localUser == null)
			{
				return Array.Empty<SurvivorDef>();
			}
			return SurvivorCatalog.orderedSurvivorDefs.Where((SurvivorDef survivor) => (Object)(object)survivor != (Object)null && !survivor.hidden && SurvivorCatalog.SurvivorIsUnlockedOnThisClient(survivor.survivorIndex) && survivor.CheckRequiredExpansionEnabled((NetworkUser)null) && survivor.CheckUserHasRequiredEntitlement(localUser)).ToArray();
		}

		private int GetNextEclipseLevel(SurvivorDef survivor)
		{
			LocalUser firstLocalUser = LocalUserManager.GetFirstLocalUser();
			if (firstLocalUser == null)
			{
				return 1;
			}
			return Mathf.Clamp(EclipseRun.GetLocalUserSurvivorCompletedEclipseLevel(firstLocalUser, survivor) + 1, 1, 8);
		}

		private void SelectSurvivor(SurvivorDef survivor)
		{
			if (Object.op_Implicit((Object)(object)_characterSelectController) && Object.op_Implicit((Object)(object)PreGameController.instance) && PreGameController.instance.IsCharacterSwitchingCurrentlyAllowed())
			{
				CharacterSelectBarController componentInChildren = ((Component)_characterSelectController).GetComponentInChildren<CharacterSelectBarController>();
				if (!Object.op_Implicit((Object)(object)componentInChildren))
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find CharacterSelectBarController.");
					return;
				}
				componentInChildren.PickIconBySurvivorDef(survivor);
				_characterSelectController.SetSurvivorInfoPanelActive(true);
			}
		}

		private static bool IsEclipseRun()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)PreGameController.instance))
			{
				return PreGameController.instance.gameModeIndex == GameModeCatalog.FindGameModeIndex("EclipseRun");
			}
			return false;
		}
	}
}