Decompiled source of MoreUpgradesCompatibility v0.4.2

plugins/MoreUpgradesCompatibility.dll

Decompiled 6 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using REPOLib.Modules;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("pavuk.moreupgrades.compatibility", "MoreUpgrades Compatibility", "0.4.2")]
[BepInDependency("bulletbot.moreupgrades", "1.7.3")]
public sealed class MoreUpgradesCompatibility : BaseUnityPlugin
{
	private static MoreUpgradesCompatibility plugin;

	private static Type managerType;

	private static FieldInfo managerInstance;

	private static FieldInfo upgrades;

	private static FieldInfo pluginInstance;

	private static FieldInfo upgradeBase;

	private static FieldInfo upgradeName;

	private static FieldInfo variables;

	private static FieldInfo ledObject;

	private static FieldInfo ledMaterial;

	private static FieldInfo box;

	private static FieldInfo color;

	private static FieldInfo extracted;

	private static FieldInfo destroyed;

	private static FieldInfo boxColor;

	private static Component trackedManager;

	private static object tracker;

	private static object valuableCounter;

	private static Type slotType;

	private static MethodInfo ignoreMoneyBags;

	private static FieldInfo missionText;

	private static readonly Dictionary<CosmeticWorldObject, float> blinkPhases = new Dictionary<CosmeticWorldObject, float>();

	private static FieldInfo previousMissionText;

	private static readonly HashSet<CosmeticWorldObject> displayed = new HashSet<CosmeticWorldObject>();

	private static readonly List<ValuableObject> liveValuables = new List<ValuableObject>();

	private static float nextScan;

	private static readonly HashSet<CosmeticWorldObject> assigned = new HashSet<CosmeticWorldObject>();

	private void Awake()
	{
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Expected O, but got Unknown
		plugin = this;
		managerType = AccessTools.TypeByName("MoreUpgrades.Classes.MoreUpgradesManager");
		managerInstance = ((managerType == null) ? null : AccessTools.Field(managerType, "instance"));
		Type type = AccessTools.TypeByName("LevelGenerator");
		MethodInfo methodInfo = ((type == null) ? null : AccessTools.Method(type, "Start", Type.EmptyTypes, (Type[])null));
		if (managerInstance == null || methodInfo == null || !typeof(MonoBehaviour).IsAssignableFrom(managerType))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Unsupported game or MoreUpgrades build.");
			return;
		}
		new Harmony("pavuk.moreupgrades.compatibility").Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(MoreUpgradesCompatibility), "EnsureManager", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Installed MoreUpgrades manager recovery.");
		InstallCosmeticsPatch();
	}

	private static void EnsureManager(Component __instance)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Expected O, but got Unknown
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		Scene scene = __instance.gameObject.scene;
		if (!((Scene)(ref scene)).IsValid() || !((Scene)(ref scene)).isLoaded || (Object)/*isinst with value type is only supported in some contexts*/ != (Object)null)
		{
			return;
		}
		GameObject val = null;
		try
		{
			val = new GameObject("More Upgrades Manager");
			SceneManager.MoveGameObjectToScene(val, scene);
			val.AddComponent(managerType);
			object? value = managerInstance.GetValue(null);
			if ((Object)((value is Component) ? value : null) == (Object)null)
			{
				throw new InvalidOperationException("MoreUpgrades manager did not initialize.");
			}
			((BaseUnityPlugin)plugin).Logger.LogInfo((object)("Restored MoreUpgrades manager in scene " + ((Scene)(ref scene)).name + "."));
		}
		catch (Exception ex)
		{
			if ((Object)(object)val != (Object)null)
			{
				Object.Destroy((Object)(object)val);
			}
			((BaseUnityPlugin)plugin).Logger.LogError((object)ex);
		}
	}

	private void InstallCosmeticsPatch()
	{
		Type type = AccessTools.TypeByName("MoreUpgrades.Plugin");
		Type type2 = AccessTools.TypeByName("MoreUpgrades.Classes.UpgradeItem");
		Type type3 = AccessTools.TypeByName("MoreUpgrades.Classes.CosmeticLEDInfo");
		if (!(type == null) && !(type2 == null) && !(type3 == null))
		{
			slotType = type3;
			missionText = AccessTools.Field(typeof(MissionUI), "Text");
			previousMissionText = AccessTools.Field(typeof(MissionUI), "messagePrev");
			ignoreMoneyBags = AccessTools.Method(type2, "GetConfig", (Type[])null, (Type[])null).MakeGenericMethod(typeof(bool));
			pluginInstance = AccessTools.Field(type, "instance");
			upgrades = AccessTools.Field(type, "upgradeItems");
			upgradeBase = AccessTools.Field(type2, "upgradeBase");
			upgradeName = AccessTools.Field(upgradeBase.FieldType, "name");
			variables = AccessTools.Field(type2, "variables");
			ledObject = AccessTools.Field(type3, "ledObject");
			ledMaterial = AccessTools.Field(type3, "ledMaterial");
			box = AccessTools.Field(type3, "cosmeticWorldObject");
			color = AccessTools.Field(type3, "color");
			extracted = AccessTools.Field(type3, "isExtracted");
			destroyed = AccessTools.Field(type3, "isDestroyed");
			boxColor = AccessTools.Field(typeof(CosmeticWorldObject), "blinkMaterialDefault");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Installed cosmetics and valuable registry repair.");
		}
	}

	private void LateUpdate()
	{
		//IL_027c: Unknown result type (might be due to invalid IL or missing references)
		//IL_026f: 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_0373: Unknown result type (might be due to invalid IL or missing references)
		//IL_0378: Unknown result type (might be due to invalid IL or missing references)
		//IL_037a: Unknown result type (might be due to invalid IL or missing references)
		//IL_037c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0391: Unknown result type (might be due to invalid IL or missing references)
		//IL_0393: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_048b: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0437: Unknown result type (might be due to invalid IL or missing references)
		//IL_0467: Unknown result type (might be due to invalid IL or missing references)
		//IL_046c: Unknown result type (might be due to invalid IL or missing references)
		//IL_04af: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d3: Unknown result type (might be due to invalid IL or missing references)
		if (slotType == null || !SemiFunc.RunIsLevel())
		{
			return;
		}
		object? value = managerInstance.GetValue(null);
		Component val = (Component)((value is Component) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		if ((Object)(object)trackedManager != (Object)(object)val)
		{
			trackedManager = val;
			blinkPhases.Clear();
			tracker = null;
			valuableCounter = null;
			nextScan = 0f;
			foreach (object item in (IEnumerable)upgrades.GetValue(pluginInstance.GetValue(null)))
			{
				string text = (string)upgradeName.GetValue(upgradeBase.GetValue(item));
				if (text == "Map Cosmetics Tracker")
				{
					tracker = item;
				}
				if (text == "Valuable Count")
				{
					valuableCounter = item;
				}
			}
		}
		PlayerAvatar val2 = SemiFunc.PlayerAvatarLocal();
		if ((Object)(object)val2 == (Object)null)
		{
			return;
		}
		bool flag = Time.unscaledTime >= nextScan;
		if (flag)
		{
			nextScan = Time.unscaledTime + 1f;
			RecoverValuables();
		}
		KeepLatestValuableLine();
		if (tracker == null || (Object)(object)MapToolController.instance == (Object)null)
		{
			return;
		}
		PlayerUpgrade upgrade = Upgrades.GetUpgrade("Map Cosmetics Tracker");
		if (upgrade == null || upgrade.GetLevel(val2) <= 0)
		{
			return;
		}
		IDictionary dictionary = (IDictionary)variables.GetValue(tracker);
		IList list = ((dictionary == null) ? null : (dictionary["Cosmetic LED Infos"] as IList));
		if (list == null)
		{
			return;
		}
		if (flag)
		{
			RecoverIndicatorSlots(list, MapToolController.instance.displaySpringTransform);
			RecoverMissingBoxes(list);
		}
		PlayerController instance = PlayerController.instance;
		GameObject val3 = (((Object)(object)instance == (Object)null) ? null : instance.cameraGameObjectLocal);
		Vector3 val4 = (((Object)(object)val3 == (Object)null) ? ((Component)val2).transform.position : val3.transform.position);
		displayed.Clear();
		foreach (object item2 in list)
		{
			object? value2 = ledObject.GetValue(item2);
			GameObject val5 = (GameObject)((value2 is GameObject) ? value2 : null);
			object? value3 = ledMaterial.GetValue(item2);
			Material val6 = (Material)((value3 is Material) ? value3 : null);
			if ((Object)(object)val5 == (Object)null || (Object)(object)val6 == (Object)null)
			{
				continue;
			}
			object? value4 = box.GetValue(item2);
			CosmeticWorldObject val7 = (CosmeticWorldObject)((value4 is CosmeticWorldObject) ? value4 : null);
			bool flag2 = (bool)extracted.GetValue(item2);
			bool flag3 = (bool)destroyed.GetValue(item2);
			bool flag4 = (((Object)(object)val7 != (Object)null) ? displayed.Add(val7) : (flag2 || flag3));
			val5.SetActive(flag4);
			if (flag4)
			{
				Color val8 = (Color)color.GetValue(item2);
				Color val9 = val8;
				if (!flag2 && flag3)
				{
					val9 = Color.Lerp(val8, Color.red, Pulse(1.25f));
				}
				else if (!flag2 && (Object)(object)val7 != (Object)null)
				{
					float num = Vector3.Distance(val4, ((Component)val7).transform.position);
					float num2 = Mathf.Lerp(0.4f, 1.8f, Mathf.InverseLerp(45f, 2f, num));
					blinkPhases.TryGetValue(val7, out var value5);
					value5 = Mathf.Repeat(value5 + num2 * Time.deltaTime, 1f);
					blinkPhases[val7] = value5;
					val9 *= Mathf.SmoothStep(0f, 1f, (Mathf.Sin(value5 * 2f * (float)Math.PI) + 1f) * 0.5f);
				}
				if (val6.HasProperty("_Color"))
				{
					val6.SetColor("_Color", val9);
				}
				if (val6.HasProperty("_BaseColor"))
				{
					val6.SetColor("_BaseColor", val9);
				}
				if (val6.HasProperty("_EmissionColor"))
				{
					val6.SetColor("_EmissionColor", val9);
				}
			}
		}
	}

	private static void KeepLatestValuableLine()
	{
		MissionUI instance = MissionUI.instance;
		if ((Object)(object)instance == (Object)null || missionText == null)
		{
			return;
		}
		object? value = missionText.GetValue(instance);
		TMP_Text val = (TMP_Text)((value is TMP_Text) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		string text = val.text;
		if (string.IsNullOrEmpty(text))
		{
			return;
		}
		int num = text.IndexOf("\nValuables: ", StringComparison.Ordinal);
		int num2 = text.LastIndexOf("\nValuables: ", StringComparison.Ordinal);
		if (num < 0 || num == num2)
		{
			return;
		}
		string text2 = text;
		for (int num3 = text2.LastIndexOf("\nValuables: ", num2 - 1, StringComparison.Ordinal); num3 >= 0; num3 = text2.LastIndexOf("\nValuables: ", num3 - 1, StringComparison.Ordinal))
		{
			int num4 = text2.IndexOf('\n', num3 + 1);
			text2 = text2.Remove(num3, num4 - num3);
			if (num3 == 0)
			{
				break;
			}
		}
		val.text = text2;
		if (previousMissionText != null && object.Equals(previousMissionText.GetValue(instance), text))
		{
			previousMissionText.SetValue(instance, text2);
		}
	}

	private static void RecoverIndicatorSlots(IList slots, Transform display)
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		if ((Object)(object)display == (Object)null)
		{
			return;
		}
		for (int num = slots.Count - 1; num >= 0; num--)
		{
			object? value = ledObject.GetValue(slots[num]);
			if ((Object)((value is GameObject) ? value : null) == (Object)null)
			{
				slots.RemoveAt(num);
			}
		}
		foreach (Transform item in display)
		{
			Transform val = item;
			if (!((Object)val).name.StartsWith("Left Side LED - Cosmetics Indicator ", StringComparison.Ordinal))
			{
				continue;
			}
			bool flag = false;
			foreach (object slot in slots)
			{
				object? value2 = ledObject.GetValue(slot);
				if ((Object)((value2 is GameObject) ? value2 : null) == (Object)(object)((Component)val).gameObject)
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				Renderer component = ((Component)val).GetComponent<Renderer>();
				if (!((Object)(object)component == (Object)null))
				{
					object obj = Activator.CreateInstance(slotType, nonPublic: true);
					ledObject.SetValue(obj, ((Component)val).gameObject);
					ledMaterial.SetValue(obj, component.material);
					slots.Add(obj);
					((BaseUnityPlugin)plugin).Logger.LogInfo((object)("Recovered cosmetics slot " + ((Object)val).name + "."));
				}
			}
		}
	}

	private static void RecoverValuables()
	{
		if (valuableCounter == null)
		{
			return;
		}
		IDictionary dictionary = (IDictionary)variables.GetValue(valuableCounter);
		List<ValuableObject> list = ((dictionary == null) ? null : (dictionary["Current Valuables"] as List<ValuableObject>));
		if (list == null)
		{
			return;
		}
		bool flag = (bool)ignoreMoneyBags.Invoke(valuableCounter, new object[1] { "Ignore Money Bags" });
		liveValuables.Clear();
		ValuableObject[] array = Object.FindObjectsByType<ValuableObject>((FindObjectsSortMode)1);
		foreach (ValuableObject val in array)
		{
			if (!flag || !((Object)(object)((Component)val).GetComponent<SurplusValuable>() != (Object)null))
			{
				liveValuables.Add(val);
			}
		}
		if (list.Count != liveValuables.Count || !liveValuables.TrueForAll(list.Contains))
		{
			bool flag2 = list.Count == 0;
			list.Clear();
			list.AddRange(liveValuables);
			dictionary["Changed"] = true;
			if (flag2 && list.Count > 0)
			{
				((BaseUnityPlugin)plugin).Logger.LogInfo((object)("Recovered valuable registry: " + list.Count + " objects."));
			}
		}
	}

	private static float Pulse(float frequency)
	{
		return Mathf.SmoothStep(0f, 1f, (Mathf.Sin(Time.time * frequency * 2f * (float)Math.PI) + 1f) * 0.5f);
	}

	private static void RecoverMissingBoxes(IList slots)
	{
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
		assigned.Clear();
		int num = 0;
		foreach (object slot in slots)
		{
			object? value = box.GetValue(slot);
			CosmeticWorldObject val = (CosmeticWorldObject)((value is CosmeticWorldObject) ? value : null);
			if ((Object)(object)val != (Object)null)
			{
				assigned.Add(val);
			}
			else if (!(bool)extracted.GetValue(slot) && !(bool)destroyed.GetValue(slot))
			{
				num++;
			}
		}
		if (num == 0)
		{
			return;
		}
		CosmeticWorldObject[] array = Object.FindObjectsByType<CosmeticWorldObject>((FindObjectsSortMode)1);
		foreach (CosmeticWorldObject val in array)
		{
			if (assigned.Contains(val))
			{
				continue;
			}
			Color val2 = (Color)boxColor.GetValue(val);
			if (Mathf.Max(val2.r, Mathf.Max(val2.g, val2.b)) <= 0f)
			{
				continue;
			}
			foreach (object slot2 in slots)
			{
				object? value2 = box.GetValue(slot2);
				if ((Object)((value2 is CosmeticWorldObject) ? value2 : null) != (Object)null || (bool)extracted.GetValue(slot2) || (bool)destroyed.GetValue(slot2))
				{
					continue;
				}
				box.SetValue(slot2, val);
				color.SetValue(slot2, val2);
				((BaseUnityPlugin)plugin).Logger.LogInfo((object)string.Concat("Recovered cosmetics indicator for ", ((Object)val).name, " with color ", val2, "."));
				break;
			}
			if (--num == 0)
			{
				break;
			}
		}
	}
}