Decompiled source of SharedDevotion v0.1.1

plugins\SharedDevotion.dll

Decompiled 3 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using On.RoR2;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[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("SharedDevotion")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SharedDevotion")]
[assembly: AssemblyTitle("SharedDevotion")]
[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.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 SharedDevotion
{
	[BepInPlugin("markj.shareddevotion", "Shared Devotion", "0.1.0")]
	public sealed class SharedDevotionPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_GetOrCreateDevotionInventoryController <0>__RegisterController;

			public static hook_GiveItem <1>__MirrorGivenItem;

			public static hook_RemoveItem <2>__MirrorRemovedItem;

			public static hook_UpdateAllMinions <3>__MirrorEvolutionChanges;

			public static Action<Run> <4>__ClearControllers;
		}

		public const string PluginGuid = "markj.shareddevotion";

		public const string PluginName = "Shared Devotion";

		public const string PluginVersion = "0.1.0";

		private static readonly HashSet<DevotionInventoryController> Controllers = new HashSet<DevotionInventoryController>();

		private static bool isSynchronizing;

		private static ConfigEntry<bool> sharingEnabled = null;

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			sharingEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable shared Devotion inventory", true, "When enabled, every player's Devotion item pool is kept identical. The host and all clients need this mod.");
			object obj = <>O.<0>__RegisterController;
			if (obj == null)
			{
				hook_GetOrCreateDevotionInventoryController val = RegisterController;
				<>O.<0>__RegisterController = val;
				obj = (object)val;
			}
			DevotionInventoryController.GetOrCreateDevotionInventoryController += (hook_GetOrCreateDevotionInventoryController)obj;
			object obj2 = <>O.<1>__MirrorGivenItem;
			if (obj2 == null)
			{
				hook_GiveItem val2 = MirrorGivenItem;
				<>O.<1>__MirrorGivenItem = val2;
				obj2 = (object)val2;
			}
			DevotionInventoryController.GiveItem += (hook_GiveItem)obj2;
			object obj3 = <>O.<2>__MirrorRemovedItem;
			if (obj3 == null)
			{
				hook_RemoveItem val3 = MirrorRemovedItem;
				<>O.<2>__MirrorRemovedItem = val3;
				obj3 = (object)val3;
			}
			DevotionInventoryController.RemoveItem += (hook_RemoveItem)obj3;
			object obj4 = <>O.<3>__MirrorEvolutionChanges;
			if (obj4 == null)
			{
				hook_UpdateAllMinions val4 = MirrorEvolutionChanges;
				<>O.<3>__MirrorEvolutionChanges = val4;
				obj4 = (object)val4;
			}
			DevotionInventoryController.UpdateAllMinions += (hook_UpdateAllMinions)obj4;
			Run.onRunDestroyGlobal += ClearControllers;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Shared Devotion loaded.");
		}

		private void OnDestroy()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			object obj = <>O.<0>__RegisterController;
			if (obj == null)
			{
				hook_GetOrCreateDevotionInventoryController val = RegisterController;
				<>O.<0>__RegisterController = val;
				obj = (object)val;
			}
			DevotionInventoryController.GetOrCreateDevotionInventoryController -= (hook_GetOrCreateDevotionInventoryController)obj;
			object obj2 = <>O.<1>__MirrorGivenItem;
			if (obj2 == null)
			{
				hook_GiveItem val2 = MirrorGivenItem;
				<>O.<1>__MirrorGivenItem = val2;
				obj2 = (object)val2;
			}
			DevotionInventoryController.GiveItem -= (hook_GiveItem)obj2;
			object obj3 = <>O.<2>__MirrorRemovedItem;
			if (obj3 == null)
			{
				hook_RemoveItem val3 = MirrorRemovedItem;
				<>O.<2>__MirrorRemovedItem = val3;
				obj3 = (object)val3;
			}
			DevotionInventoryController.RemoveItem -= (hook_RemoveItem)obj3;
			object obj4 = <>O.<3>__MirrorEvolutionChanges;
			if (obj4 == null)
			{
				hook_UpdateAllMinions val4 = MirrorEvolutionChanges;
				<>O.<3>__MirrorEvolutionChanges = val4;
				obj4 = (object)val4;
			}
			DevotionInventoryController.UpdateAllMinions -= (hook_UpdateAllMinions)obj4;
			Run.onRunDestroyGlobal -= ClearControllers;
		}

		private static void ClearControllers(Run _)
		{
			Controllers.Clear();
			isSynchronizing = false;
		}

		private static DevotionInventoryController RegisterController(orig_GetOrCreateDevotionInventoryController orig, Interactor summoner)
		{
			DevotionInventoryController val = orig.Invoke(summoner);
			if (!ShouldSynchronize())
			{
				return val;
			}
			PruneControllers();
			if (Controllers.Add(val) && TryGetReferenceController(val, out DevotionInventoryController source))
			{
				isSynchronizing = true;
				try
				{
					CopyInventory(source, val);
				}
				finally
				{
					isSynchronizing = false;
				}
			}
			return val;
		}

		private static void MirrorGivenItem(orig_GiveItem orig, DevotionInventoryController self, ItemIndex itemIndex, int count)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self, itemIndex, count);
			if (ShouldSynchronize() && !isSynchronizing)
			{
				Controllers.Add(self);
				SynchronizeFrom(self);
			}
		}

		private static void MirrorRemovedItem(orig_RemoveItem orig, DevotionInventoryController self, ItemIndex itemIndex, int count)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self, itemIndex, count);
			if (ShouldSynchronize() && !isSynchronizing)
			{
				Controllers.Add(self);
				SynchronizeFrom(self);
			}
		}

		private static void MirrorEvolutionChanges(orig_UpdateAllMinions orig, DevotionInventoryController self, bool shouldEvolve)
		{
			orig.Invoke(self, shouldEvolve);
			if (shouldEvolve && ShouldSynchronize() && !isSynchronizing)
			{
				Controllers.Add(self);
				SynchronizeFrom(self);
			}
		}

		private static bool ShouldSynchronize()
		{
			if (sharingEnabled.Value)
			{
				return NetworkServer.active;
			}
			return false;
		}

		private static void SynchronizeFrom(DevotionInventoryController source)
		{
			PruneControllers();
			isSynchronizing = true;
			try
			{
				foreach (DevotionInventoryController controller in Controllers)
				{
					if ((Object)(object)controller != (Object)(object)source)
					{
						CopyInventory(source, controller);
					}
				}
				foreach (DevotionInventoryController controller2 in Controllers)
				{
					controller2.UpdateAllMinions(false);
				}
			}
			finally
			{
				isSynchronizing = false;
			}
		}

		private static void CopyInventory(DevotionInventoryController source, DevotionInventoryController target)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			Inventory component = ((Component)source).GetComponent<Inventory>();
			Inventory component2 = ((Component)target).GetComponent<Inventory>();
			if (!Object.op_Implicit((Object)(object)component) || !Object.op_Implicit((Object)(object)component2))
			{
				return;
			}
			for (int i = 0; i < 0; i++)
			{
				ItemIndex val = (ItemIndex)i;
				if (val != Items.LemurianHarness.itemIndex)
				{
					int num = component.GetItemCountPermanent(val) - component2.GetItemCountPermanent(val);
					if (num > 0)
					{
						target.GiveItem(val, num);
					}
					else if (num < 0)
					{
						target.RemoveItem(val, -num);
					}
				}
			}
		}

		private static bool TryGetReferenceController(DevotionInventoryController excluded, out DevotionInventoryController source)
		{
			foreach (DevotionInventoryController controller in Controllers)
			{
				if (Object.op_Implicit((Object)(object)controller) && (Object)(object)controller != (Object)(object)excluded)
				{
					source = controller;
					return true;
				}
			}
			source = null;
			return false;
		}

		private static void PruneControllers()
		{
			Controllers.RemoveWhere((DevotionInventoryController controller) => !Object.op_Implicit((Object)(object)controller));
		}
	}
}