Decompiled source of QoLChestsShareItemsCompat v1.0.1

plugins\QoLChestsShareItemsCompat\QoLChestsShareItemsCompat.dll

Decompiled 18 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
using RoR2;
using UnityEngine;

[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("QoLChestsShareItemsCompat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("QoLChestsShareItemsCompat")]
[assembly: AssemblyTitle("QoLChestsShareItemsCompat")]
[assembly: AssemblyVersion("1.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 QoLChestsShareItemsCompat
{
	[BepInPlugin("local.qolchests.shareitems.compat", "QoLChestsShareItemsCompat", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		private const BindingFlags InstancePublic = BindingFlags.Instance | BindingFlags.Public;

		private const BindingFlags StaticPublic = BindingFlags.Static | BindingFlags.Public;

		private const float ChestCheckInterval = 0.5f;

		private const float ShopCheckInterval = 2f;

		private Type managedChestType;

		private FieldInfo managedInstances;

		private MethodInfo localOpened;

		private Type multiShopType;

		private MethodInfo localShopClosed;

		private bool resolved;

		private bool warningEmitted;

		private float nextChestCheck;

		private float nextShopCheck;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Highlight-only bridge initialized (idle outside runs).");
		}

		private void Update()
		{
			if ((Object)(object)Run.instance == (Object)null || (Object)(object)Stage.instance == (Object)null)
			{
				return;
			}
			float unscaledTime = Time.unscaledTime;
			if (unscaledTime < nextChestCheck)
			{
				return;
			}
			nextChestCheck = unscaledTime + 0.5f;
			if (!resolved)
			{
				resolved = true;
				ResolveShareItemsTypes();
			}
			try
			{
				DisableLocallyUsedInteractables();
				if (unscaledTime >= nextShopCheck)
				{
					nextShopCheck = unscaledTime + 2f;
					DisableLocallyFinishedShopRoots();
				}
			}
			catch (Exception ex)
			{
				if (!warningEmitted)
				{
					warningEmitted = true;
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Compatibility check skipped: " + ex));
				}
			}
		}

		private void ResolveShareItemsTypes()
		{
			managedChestType = Type.GetType("ShareItemsWithMoreFun.ManagedChest, ShareItemsWithMoreFun", throwOnError: false);
			if (managedChestType != null)
			{
				managedInstances = managedChestType.GetField("Instances", BindingFlags.Static | BindingFlags.Public);
				localOpened = managedChestType.GetMethod("OpenedByAnyLocalPlayer", BindingFlags.Instance | BindingFlags.Public);
			}
			Type type = Type.GetType("ShareItemsWithMoreFun.MultiShopHooks, ShareItemsWithMoreFun", throwOnError: false);
			if (type != null)
			{
				localShopClosed = type.GetMethod("ClosedForAllLocalPlayers", BindingFlags.Static | BindingFlags.Public);
				if (localShopClosed != null)
				{
					ParameterInfo[] parameters = localShopClosed.GetParameters();
					if (parameters.Length == 1)
					{
						multiShopType = parameters[0].ParameterType;
					}
				}
			}
			if (managedInstances == null || localOpened == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"ShareItemsWithMoreFun local chest API not found. No highlights will be changed.");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"ShareItemsWithMoreFun local chest state detected; bridge active in stages.");
			}
		}

		private void DisableLocallyUsedInteractables()
		{
			if (managedInstances == null || localOpened == null || !(managedInstances.GetValue(null) is IEnumerable enumerable))
			{
				return;
			}
			bool flag = default(bool);
			foreach (object item in enumerable)
			{
				Component val = (Component)((item is Component) ? item : null);
				if (!Object.op_Implicit((Object)(object)val))
				{
					continue;
				}
				Highlight component = val.GetComponent<Highlight>();
				if (Object.op_Implicit((Object)(object)component) && (((Behaviour)component).enabled || component.isOn))
				{
					object obj = localOpened.Invoke(val, null);
					int num;
					if (obj is bool)
					{
						flag = (bool)obj;
						num = 1;
					}
					else
					{
						num = 0;
					}
					if (((uint)num & (flag ? 1u : 0u)) != 0)
					{
						TurnOffHighlight(component);
					}
				}
			}
		}

		private void DisableLocallyFinishedShopRoots()
		{
			if (multiShopType == null || localShopClosed == null)
			{
				return;
			}
			Object[] array = Object.FindObjectsOfType(multiShopType);
			bool flag = default(bool);
			foreach (Object obj in array)
			{
				Component val = (Component)(object)((obj is Component) ? obj : null);
				if (!Object.op_Implicit((Object)(object)val))
				{
					continue;
				}
				Highlight component = val.GetComponent<Highlight>();
				if (Object.op_Implicit((Object)(object)component) && (((Behaviour)component).enabled || component.isOn))
				{
					object obj2 = localShopClosed.Invoke(null, new object[1] { val });
					int num;
					if (obj2 is bool)
					{
						flag = (bool)obj2;
						num = 1;
					}
					else
					{
						num = 0;
					}
					if (((uint)num & (flag ? 1u : 0u)) != 0)
					{
						TurnOffHighlight(component);
					}
				}
			}
		}

		private static void TurnOffHighlight(Highlight highlight)
		{
			highlight.isOn = false;
			((Behaviour)highlight).enabled = false;
		}
	}
}