Decompiled source of ReRollUpgrades v1.0.0

BepInEx/plugins/ReRollUpgrades.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Pigeon.Movement;
using Pigeon.UI;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ReRollUpgrades")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ReRollUpgrades")]
[assembly: AssemblyTitle("ReRollUpgrades")]
[assembly: AssemblyVersion("1.0.0.0")]
[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;
		}
	}
}
[MycoMod(/*Could not decode attribute arguments.*/)]
[BepInPlugin("gelofrat.rerollupgrades", "ReRollUpgrades", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	private const string PluginGuid = "gelofrat.rerollupgrades";

	private const string PluginName = "ReRollUpgrades";

	private const string PluginVersion = "1.0.0";

	private static readonly FieldInfo StoredUpgradesField = typeof(OuroObjective).GetField("storedUpgrades", BindingFlags.Static | BindingFlags.NonPublic);

	private static readonly FieldInfo SelectedGearField = typeof(OuroGearWindow).GetField("selectedGear", BindingFlags.Instance | BindingFlags.NonPublic);

	internal static ManualLogSource Logger = null;

	private OuroGearWindow? currentWindow;

	private Button? rerollButton;

	private void Awake()
	{
		Logger = ((BaseUnityPlugin)this).Logger;
		Logger.LogInfo((object)"ReRollUpgrades loaded");
	}

	private void Update()
	{
		if (!CanUseWindow(out OuroGearWindow window, out OuroObjective _))
		{
			DestroyRerollButton();
		}
		else if (!((Object)(object)currentWindow == (Object)(object)window) || !((Object)(object)rerollButton != (Object)null))
		{
			DestroyRerollButton();
			if (!TryFindCloseButton(window, out Button closeButton))
			{
				Logger.LogWarning((object)"Could not find OuroGearWindow Close button.");
			}
			else
			{
				CreateRerollButton(window, closeButton);
			}
		}
	}

	private void OnDestroy()
	{
		DestroyRerollButton();
	}

	private void CreateRerollButton(OuroGearWindow window, Button closeButton)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Expected O, but got Unknown
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Expected O, but got Unknown
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Expected O, but got Unknown
		Button val = Object.Instantiate<Button>(closeButton, ((Component)closeButton).transform.parent);
		((Object)val).name = "RerollOuroButton";
		((Component)val).transform.SetSiblingIndex(((Component)closeButton).transform.GetSiblingIndex());
		RectTransform val2 = (RectTransform)((Component)val).transform;
		RectTransform val3 = (RectTransform)((Component)closeButton).transform;
		val2.anchorMin = val3.anchorMin;
		val2.anchorMax = val3.anchorMax;
		val2.pivot = val3.pivot;
		val2.sizeDelta = val3.sizeDelta;
		Vector2 anchoredPosition = val3.anchoredPosition;
		Rect rect = val3.rect;
		val2.anchoredPosition = anchoredPosition - new Vector2(((Rect)(ref rect)).width + 10f, 0f);
		SetButtonText(val, "REROLL");
		val.OnClickDown = new UnityEvent();
		val.OnClickUp = new UnityEvent();
		val.OnClickUp.AddListener((UnityAction)delegate
		{
			if (CanUseWindow(out OuroGearWindow window2, out OuroObjective objective))
			{
				TryReroll(window2, objective);
			}
		});
		currentWindow = window;
		rerollButton = val;
	}

	private static void SetButtonText(Button button, string text)
	{
		TMP_Text[] componentsInChildren = ((Component)button).GetComponentsInChildren<TMP_Text>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].text = text;
		}
		Text[] componentsInChildren2 = ((Component)button).GetComponentsInChildren<Text>(true);
		for (int j = 0; j < componentsInChildren2.Length; j++)
		{
			componentsInChildren2[j].text = text;
		}
	}

	private void DestroyRerollButton()
	{
		if ((Object)(object)rerollButton != (Object)null)
		{
			Object.Destroy((Object)(object)((Component)rerollButton).gameObject);
			rerollButton = null;
		}
		currentWindow = null;
	}

	private static bool TryFindCloseButton(OuroGearWindow window, out Button closeButton)
	{
		Button[] componentsInChildren = ((Component)window).GetComponentsInChildren<Button>(true);
		foreach (Button val in componentsInChildren)
		{
			int persistentEventCount = ((UnityEventBase)val.OnClickUp).GetPersistentEventCount();
			for (int j = 0; j < persistentEventCount; j++)
			{
				if (((UnityEventBase)val.OnClickUp).GetPersistentTarget(j) == (Object)(object)window && ((UnityEventBase)val.OnClickUp).GetPersistentMethodName(j) == "CloseMenu")
				{
					closeButton = val;
					return true;
				}
			}
		}
		foreach (Button val2 in componentsInChildren)
		{
			if (IsNamedLikeCloseButton(((Component)val2).transform))
			{
				closeButton = val2;
				return true;
			}
		}
		foreach (Button val3 in componentsInChildren)
		{
			if (HasCloseText(val3))
			{
				closeButton = val3;
				return true;
			}
		}
		closeButton = null;
		return false;
	}

	private static bool IsNamedLikeCloseButton(Transform transform)
	{
		Transform val = transform;
		while ((Object)(object)val != (Object)null)
		{
			if (((Object)val).name.IndexOf("close", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return true;
			}
			val = val.parent;
		}
		return false;
	}

	private static bool HasCloseText(Button button)
	{
		TMP_Text[] componentsInChildren = ((Component)button).GetComponentsInChildren<TMP_Text>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			if (IsCloseLabel(componentsInChildren[i].text))
			{
				return true;
			}
		}
		Text[] componentsInChildren2 = ((Component)button).GetComponentsInChildren<Text>(true);
		for (int j = 0; j < componentsInChildren2.Length; j++)
		{
			if (IsCloseLabel(componentsInChildren2[j].text))
			{
				return true;
			}
		}
		return false;
	}

	private static bool IsCloseLabel(string text)
	{
		return string.Equals(text?.Trim(), "close", StringComparison.OrdinalIgnoreCase);
	}

	private static bool CanUseWindow(out OuroGearWindow window, out OuroObjective objective)
	{
		window = null;
		objective = null;
		if ((Object)(object)Menu.Instance == (Object)null || !Menu.Instance.IsOpen || (Object)(object)Menu.Instance.WindowSystem == (Object)null)
		{
			return false;
		}
		Window top = Menu.Instance.WindowSystem.GetTop();
		OuroGearWindow val = (OuroGearWindow)(object)((top is OuroGearWindow) ? top : null);
		if (val == null)
		{
			return false;
		}
		window = val;
		if ((Object)(object)MissionManager.Instance == (Object)null || !(((MissionData)(ref MissionManager.CurrentMission)).Mission is OuroMission))
		{
			return false;
		}
		IObjective currentObjective = MissionManager.Instance.CurrentObjective;
		objective = (OuroObjective)(((object)((currentObjective is OuroObjective) ? currentObjective : null)) ?? ((object)MissionManager.Instance.GetObjective<OuroObjective>()));
		return (Object)(object)objective != (Object)null && GameManager.ThreatLevel > 5 && objective.RoomsCompleted == 0 && !objective.IsRoomActive;
	}

	private void TryReroll(OuroGearWindow window, OuroObjective objective)
	{
		try
		{
			ResetOuroInventory();
			GiveStartingOuroUpgrades(objective);
			RefreshWindow(window);
			OuroMission.ApplyUpgrades();
			Logger.LogInfo((object)"Ouro inventory rerolled");
		}
		catch (Exception ex)
		{
			Logger.LogError((object)ex);
		}
	}

	private static void ResetOuroInventory()
	{
		PlayerData.ResetRecentUnchosenUpgrades();
		for (int i = 0; i < Global.Instance.AllGear.Length; i++)
		{
			OuroObjective.UnequipGearUpgrades(Global.Instance.AllGear[i]);
		}
		OuroObjective.UnequipGearUpgrades((IUpgradable)(object)Player.LocalPlayer.Character);
		OuroObjective.UnequipGearUpgrades((IUpgradable)(object)Global.Instance);
		PlayerData.ClearMissionUpgrades();
		PlayerData.DeleteMissionUpgrades((Rarity)(-1));
		if (StoredUpgradesField?.GetValue(null) is IList list)
		{
			list.Clear();
		}
		OuroObjective.OnStoredUpgradesRemoved();
	}

	private static void GiveStartingOuroUpgrades(OuroObjective objective)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		int num = 2 + (GameManager.ThreatLevel - 5) * 5;
		int num2 = 2 + (GameManager.ThreatLevel - 5) * 2;
		Vector3 val = (((Object)(object)Player.LocalPlayer != (Object)null) ? Player.LocalPlayer.InterpolatedPosition : Vector3.zero);
		for (int i = 0; i < num2; i++)
		{
			objective.SpawnUpgrades_Client(val, 2, Random.Range(int.MinValue, int.MaxValue), num, false, false, false, false, false);
		}
	}

	private static void RefreshWindow(OuroGearWindow window)
	{
		object? obj = SelectedGearField?.GetValue(window);
		IUpgradable val = (IUpgradable)((obj is IUpgradable) ? obj : null);
		if (val != null)
		{
			window.SelectGear(val);
		}
	}
}