Decompiled source of BetterRandomStart PreventLogbookDuplicates v1.0.1

MT2_BETTER_RANDOM.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ShinyShoe;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MT2_BETTER_RANDOM")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ef6553252efe9e078f70c779203597fe477239c5")]
[assembly: AssemblyProduct("MT2_BETTER_RANDOM")]
[assembly: AssemblyTitle("MT2_BETTER_RANDOM")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
namespace MT2_BETTER_RANDOM
{
	[BepInPlugin("com.bokeher.better_random", "Better Random", "1.0.0")]
	public class BetterRandom : BaseUnityPlugin
	{
		private readonly int[] championIndexes = new int[2] { 0, 1 };

		private IReadOnlyList<(string mainClan, string subClan, int mainChampIndex)>? uncompleted;

		private IReadOnlyList<ClassData>? allClassDatas;

		private AllGameManagers? allGameManagers;

		private List<string>? clanIds;

		private MetagameSaveData? metagameSave;

		public static BetterRandom? Instance { get; private set; }

		private void Awake()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			Instance = this;
			Harmony val = new Harmony("com.bokeher.better_random");
			val.PatchAll(Assembly.GetExecutingAssembly());
		}

		public void onRunSetupScreenOpened()
		{
			allGameManagers = Object.FindObjectOfType<AllGameManagers>();
			if ((Object)(object)allGameManagers == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"AllGameManagers is null");
				return;
			}
			allClassDatas = getAllClans();
			if (allClassDatas == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"AllClassDatas is null");
				return;
			}
			clanIds = getClanIds();
			if (clanIds == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"ClanIds is null");
				return;
			}
			uncompleted = GetUncompletedCombinations() ?? new List<(string, string, int)>();
			AddCustomButton();
		}

		private List<string> getClanIds()
		{
			List<string> list = new List<string>();
			foreach (ClassData allClassData in allClassDatas)
			{
				list.Add(((GameData)allClassData).GetID());
			}
			return list;
		}

		private IReadOnlyList<ClassData>? getAllClans()
		{
			AllGameData allGameData = allGameManagers.GetAllGameData();
			if ((Object)(object)allGameData == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"ClassManager not found.");
				return null;
			}
			return allGameData.GetAllClassDatas();
		}

		public List<(string mainClan, string subClan, int champ)>? GetUncompletedCombinations()
		{
			SaveManager saveManager = allGameManagers.GetSaveManager();
			if ((Object)(object)saveManager == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"SaveManager is null.");
				return null;
			}
			metagameSave = saveManager.GetMetagameSave();
			if (metagameSave == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"MetagameSaveData is null.");
				return null;
			}
			if (allClassDatas == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"AllClassDatas is null.");
				return null;
			}
			FieldInfo field = typeof(MetagameSaveData).GetField("unlockedClassIDs", BindingFlags.Instance | BindingFlags.NonPublic);
			List<string> list = field.GetValue(metagameSave) as List<string>;
			List<string> list2 = new List<string>();
			if (list == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"unlockedClassIDs is null");
				return null;
			}
			if (list.Count < 8)
			{
				foreach (ClassData allClassData in allClassDatas)
				{
					if (allClassData.GetTitle() == "Banished" || allClassData.GetTitle() == "Pyreborne")
					{
						list2.Add(((GameData)allClassData).GetID());
					}
					if (list.Contains(((GameData)allClassData).GetID()))
					{
						list2.Add(((GameData)allClassData).GetID());
					}
				}
			}
			List<(string, string, int)> list3 = new List<(string, string, int)>();
			foreach (string clanId in clanIds)
			{
				if (!list2.Contains(clanId) && list.Count < 8)
				{
					continue;
				}
				foreach (string clanId2 in clanIds)
				{
					if ((!list2.Contains(clanId2) && list.Count < 8) || clanId == clanId2)
					{
						continue;
					}
					int[] array = championIndexes;
					foreach (int num in array)
					{
						int level = metagameSave.GetLevel(clanId);
						if (level >= 5 || num != 1)
						{
							ClassCombinationWinData classCombinationWinAscensionLevel = metagameSave.GetClassCombinationWinAscensionLevel(clanId, clanId2, num);
							if (classCombinationWinAscensionLevel.highestAscensionLevel < 10 || !classCombinationWinAscensionLevel.isTfbVictory)
							{
								list3.Add((clanId, clanId2, num));
							}
						}
					}
				}
			}
			return list3;
		}

		private int getClanLevel(string classId)
		{
			SaveManager saveManager = allGameManagers.GetSaveManager();
			if ((Object)(object)saveManager == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"SaveManager is null.");
				return 1;
			}
			MetagameSaveData val = saveManager.GetMetagameSave();
			if (val == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"MetagameSaveData is null.");
				return 1;
			}
			return val.GetLevel(classId);
		}

		public void AddCustomButton()
		{
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: 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_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Expected O, but got Unknown
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: 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_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: Unknown result type (might be due to invalid IL or missing references)
			//IL_023f: Expected O, but got Unknown
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Expected O, but got Unknown
			//IL_0294: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
			GameUISelectableButton val = null;
			GameUISelectableButton[] array = Object.FindObjectsOfType<GameUISelectableButton>();
			foreach (GameUISelectableButton val2 in array)
			{
				if (((Object)((Component)val2).gameObject).name == "SettingsButton")
				{
					val = val2;
					break;
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"SettingsButton not found");
				return;
			}
			Canvas componentInParent = ((Component)val).gameObject.GetComponentInParent<Canvas>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Canvas not found");
				return;
			}
			GameObject val3 = new GameObject("ButtonBorder");
			val3.transform.SetParent(((Component)componentInParent).transform, false);
			RectTransform val4 = val3.AddComponent<RectTransform>();
			val4.sizeDelta = new Vector2(112f, 52f);
			RectTransform component = ((Component)val).GetComponent<RectTransform>();
			val4.anchorMin = component.anchorMin;
			val4.anchorMax = component.anchorMax;
			val4.pivot = component.pivot;
			Vector2 anchoredPosition = component.anchoredPosition;
			ref float y = ref anchoredPosition.y;
			float num = y;
			Rect rect = component.rect;
			y = num - (((Rect)(ref rect)).height + 10f);
			anchoredPosition.x -= 9f;
			val4.anchoredPosition = anchoredPosition;
			Image val5 = val3.AddComponent<Image>();
			((Graphic)val5).color = Color.white;
			GameObject val6 = new GameObject("CustomRandomButton");
			val6.transform.SetParent(val3.transform, false);
			RectTransform val7 = val6.AddComponent<RectTransform>();
			val7.sizeDelta = new Vector2(110f, 50f);
			val7.anchorMin = new Vector2(0.5f, 0.5f);
			val7.anchorMax = new Vector2(0.5f, 0.5f);
			val7.pivot = new Vector2(0.5f, 0.5f);
			val7.anchoredPosition = Vector2.zero;
			Image val8 = val6.AddComponent<Image>();
			((Graphic)val8).color = new Color(0.19215687f, 18f / 85f, 11f / 51f, 1f);
			Button val9 = val6.AddComponent<Button>();
			((UnityEvent)val9.onClick).AddListener(new UnityAction(onRandomButtonClick));
			GameObject val10 = new GameObject("ButtonText");
			val10.transform.SetParent(val6.transform, false);
			Text val11 = val10.AddComponent<Text>();
			val11.text = "Better Random";
			val11.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
			val11.alignment = (TextAnchor)4;
			((Graphic)val11).color = Color.white;
			RectTransform component2 = ((Component)val11).GetComponent<RectTransform>();
			component2.anchorMin = Vector2.zero;
			component2.anchorMax = Vector2.one;
			component2.offsetMin = Vector2.zero;
			component2.offsetMax = Vector2.zero;
		}

		private void onRandomButtonClick()
		{
			if (IEnumerableUtility.IsNullOrEmpty<(string, string, int)>((IEnumerable<(string, string, int)>)uncompleted))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"'uncompleted' is null or empty!");
				return;
			}
			if (uncompleted.Count == 0)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"All combinations are completed!");
				return;
			}
			Random random = new Random();
			(string mainClan, string subClan, int mainChampIndex) chosen = uncompleted[random.Next(uncompleted.Count)];
			ClassData val = allClassDatas?.FirstOrDefault((Func<ClassData, bool>)((ClassData cd) => ((GameData)cd).GetID() == chosen.mainClan));
			ClassData val2 = allClassDatas?.FirstOrDefault((Func<ClassData, bool>)((ClassData cd) => ((GameData)cd).GetID() == chosen.subClan));
			if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"MainClan or SubClan is null");
				return;
			}
			string title = val.GetTitle();
			string title2 = val2.GetTitle();
			RunSetupClassLevelInfoUI[] array = Object.FindObjectsOfType<RunSetupClassLevelInfoUI>();
			if (array == null || array.Length < 2)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"RunSetupClassLevelInfoUI is null or doesnt contain main and sub clans");
				return;
			}
			RunSetupClassLevelInfoUI obj = array[0];
			RunSetupClassLevelInfoUI val3 = array[1];
			RunSetupClassLevelInfoUI val4 = obj;
			int clanLevel = getClanLevel(((GameData)val).GetID());
			int clanLevel2 = getClanLevel(((GameData)val2).GetID());
			int num = Random.Range(0, 2);
			int level = metagameSave.GetLevel(((GameData)val2).GetID());
			if (level < 5 && level == 1)
			{
				num = 0;
			}
			val4.SetClass(val, clanLevel, chosen.mainChampIndex);
			val3.SetClass(val2, clanLevel2, num);
			RunSetupScreen obj2 = Object.FindObjectOfType<RunSetupScreen>();
			typeof(RunSetupScreen).GetMethod("RefreshCharacters", BindingFlags.Instance | BindingFlags.NonPublic)?.Invoke(obj2, new object[1] { false });
			typeof(RunSetupScreen).GetMethod("RefreshClanCovenantUI", BindingFlags.Instance | BindingFlags.NonPublic)?.Invoke(obj2, null);
			val4.ShowCardPreview();
			val3.ShowCardPreview();
		}
	}
	[HarmonyPatch(typeof(RunSetupScreen), "Start")]
	public class RunSetupScreen_Start_Patch
	{
		private static void Postfix()
		{
			BetterRandom.Instance?.onRunSetupScreenOpened();
		}
	}
}