Decompiled source of ScoresPhilsBenthicPuritySwap v1.0.3

ScoresPhilsBenthicPuritySwap.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using IL.RoR2;
using LookingGlass.ItemStatsNameSpace;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using R2API.Utils;
using RoR2;
using RoR2.ExpansionManagement;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("ScoresPhilsBenthicPuritySwap")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ad8680c5840bc87227e9b5e980d097f71b151dae")]
[assembly: AssemblyProduct("ScoresPhilsBenthicPuritySwap")]
[assembly: AssemblyTitle("ScoresPhilsBenthicPuritySwap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ScoresPhilsBenthicPuritySwap
{
	internal class Assets
	{
		public const string bundleName = "ScoresPhilsBenthicPuritySwap.benthicpurityswap";

		public static AssetBundle MainAssets { get; private set; }

		public static void Init()
		{
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ScoresPhilsBenthicPuritySwap.benthicpurityswap");
			if (stream != null)
			{
				MainAssets = AssetBundle.LoadFromStream(stream);
				return;
			}
			Log.Error("Critical error encountered when loading asset bundle. aborting procedures.");
			BenthicPuritySwap.abortPatching = true;
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.score.ScoresPhilsBenthicPuritySwap", "ScoresPhilsBenthicPuritySwap", "1.0.3")]
	public class BenthicPuritySwap : BaseUnityPlugin
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static Func<Instruction, bool> <>9__7_1;

			public static Manipulator <>9__7_0;

			internal void <Awake>b__7_0(ILContext il)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0007: Expected O, but got Unknown
				//IL_0040: Unknown result type (might be due to invalid IL or missing references)
				ILCursor val = new ILCursor(il);
				if (val.TryGotoNext((MoveType)0, new Func<Instruction, bool>[1]
				{
					(Instruction i) => ILPatternMatchingExt.MatchLdsfld(i, "RoR2.RoR2Content/Items", "LunarBadLuck")
				}))
				{
					val.Remove();
					val.Emit(OpCodes.Ldsfld, Reflection.GetFieldCached(typeof(Items), "CloverVoid"));
				}
				else
				{
					Log.Error("IL Hook failed for CharacterMaster_OnInventoryChanged. Unable to swap bad luck from Purity to Benthic Bloom.");
				}
			}

			internal bool <Awake>b__7_1(Instruction i)
			{
				return ILPatternMatchingExt.MatchLdsfld(i, "RoR2.RoR2Content/Items", "LunarBadLuck");
			}
		}

		public const string PluginGUID = "com.score.ScoresPhilsBenthicPuritySwap";

		public const string PluginAuthor = "score";

		public const string PluginName = "ScoresPhilsBenthicPuritySwap";

		public const string PluginVersion = "1.0.3";

		public static bool abortPatching;

		public static bool isLookingGlassInstalled => Chainloader.PluginInfos.ContainsKey("droppod.lookingglass");

		public void Awake()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			Log.Init(((BaseUnityPlugin)this).Logger);
			Assets.Init();
			if (abortPatching)
			{
				return;
			}
			Harmony val = new Harmony("com.score.ScoresPhilsBenthicPuritySwap");
			val.CreateClassProcessor(typeof(SetItemDefsFix)).Patch();
			val.CreateClassProcessor(typeof(SetItemRelationshipsFix)).Patch();
			object obj = <>c.<>9__7_0;
			if (obj == null)
			{
				Manipulator val2 = delegate(ILContext il)
				{
					//IL_0001: Unknown result type (might be due to invalid IL or missing references)
					//IL_0007: Expected O, but got Unknown
					//IL_0040: Unknown result type (might be due to invalid IL or missing references)
					ILCursor val3 = new ILCursor(il);
					if (val3.TryGotoNext((MoveType)0, new Func<Instruction, bool>[1]
					{
						(Instruction i) => ILPatternMatchingExt.MatchLdsfld(i, "RoR2.RoR2Content/Items", "LunarBadLuck")
					}))
					{
						val3.Remove();
						val3.Emit(OpCodes.Ldsfld, Reflection.GetFieldCached(typeof(Items), "CloverVoid"));
					}
					else
					{
						Log.Error("IL Hook failed for CharacterMaster_OnInventoryChanged. Unable to swap bad luck from Purity to Benthic Bloom.");
					}
				};
				<>c.<>9__7_0 = val2;
				obj = (object)val2;
			}
			CharacterMaster.OnInventoryChanged += (Manipulator)obj;
			if (isLookingGlassInstalled)
			{
				RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(Compat.LGCompat));
			}
		}
	}
	public static class Compat
	{
		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		public static void LGCompat()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected I4, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected I4, but got Unknown
			if (!BenthicPuritySwap.abortPatching)
			{
				Dictionary<int, ItemStatsDef> allItemDefinitions = ItemDefinitions.allItemDefinitions;
				if (allItemDefinitions != null && allItemDefinitions.Any())
				{
					if (ItemDefinitions.allItemDefinitions.TryGetValue((int)Items.LunarBadLuck.itemIndex, out var value))
					{
						value.descriptions.Remove("Luck: ");
						value.valueTypes.Remove((ValueType)11);
						value.measurementUnits.Remove((MeasurementUnits)4);
						value.calculateValuesNew = (float luck, int stackCount, float procChance) => new List<float>(1) { stackCount + 1 };
					}
					else
					{
						Log.Error("Purity index didnt match lookingglass stats");
					}
					if (ItemDefinitions.allItemDefinitions.TryGetValue((int)Items.CloverVoid.itemIndex, out var value2))
					{
						value2.descriptions.Add("Luck: ");
						value2.valueTypes.Add((ValueType)11);
						value2.measurementUnits.Add((MeasurementUnits)4);
						value2.calculateValuesNew = (float luck, int stackCount, float procChance) => new List<float>(2)
						{
							stackCount * 3,
							-stackCount
						};
					}
					else
					{
						Log.Error("Bloom index didnt match lookingglass stats");
					}
					return;
				}
			}
			Log.Error("Cant do compatibility stuff with LookingGlass");
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
	[HarmonyPatch(typeof(ItemCatalog), "SetItemDefs")]
	public class SetItemDefsFix
	{
		[HarmonyPrefix]
		private static void Prefix(ref ItemDef[] newItemDefs)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: 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: Invalid comparison between Unknown and I4
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			int num = -1;
			int num2 = -1;
			ItemTier val = (ItemTier)8;
			for (int i = 0; i < newItemDefs.Length; i++)
			{
				ItemDef val2 = newItemDefs[i];
				if (Object.op_Implicit((Object)(object)val2))
				{
					if (((Object)val2).name == ((Object)Items.AlienHead).name)
					{
						val = (ItemTier)(((int)val2.tier == 1) ? 7 : ((int)val));
					}
					else if (((Object)val2).name == ((Object)Items.LunarBadLuck).name)
					{
						num = i;
					}
					else if (((Object)val2).name == ((Object)Items.CloverVoid).name)
					{
						num2 = i;
					}
				}
			}
			if (num == -1)
			{
				Log.Error($"Benthic purity swap ran into a critical error! Purity index {num} is invalid! Aborting procedures.");
				BenthicPuritySwap.abortPatching = true;
				return;
			}
			ItemDef val3 = newItemDefs[num];
			val3.tier = val;
			val3.pickupIconSprite = Assets.MainAssets.LoadAsset<Sprite>("assets/import/benthicpurityswap_icons/voidbadluck.png");
			val3.requiredExpansion = ((IEnumerable<ExpansionDef>)(object)ExpansionCatalog.expansionDefs).FirstOrDefault((Func<ExpansionDef, bool>)((ExpansionDef ex) => ((Object)ex).name.Contains("DLC1")));
			ExpansionDef requiredExpansion = val3.requiredExpansion;
			Log.Info("expansion" + ((requiredExpansion != null) ? ((Object)requiredExpansion).name : null));
			newItemDefs[num] = val3;
			Items.LunarBadLuck = val3;
			if (num2 == -1)
			{
				Log.Error("Benthic bloom could not be found in the item catalog. Unintended behaviour may occur...");
				return;
			}
			ItemDef val4 = newItemDefs[num2];
			val4.tier = (ItemTier)3;
			val4.pickupIconSprite = Assets.MainAssets.LoadAsset<Sprite>("assets/import/benthicpurityswap_icons/cloverLunar.png");
			newItemDefs[num2] = val4;
			Items.CloverVoid = val4;
		}
	}
	[HarmonyPatch(typeof(ItemCatalog), "SetItemRelationships")]
	public class SetItemRelationshipsFix
	{
		[HarmonyPrefix]
		private static void Prefix(ref ItemRelationshipProvider[] newProviders)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: 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_00c8: Unknown result type (might be due to invalid IL or missing references)
			if (BenthicPuritySwap.abortPatching)
			{
				return;
			}
			Pair val = default(Pair);
			val.itemDef1 = Items.AlienHead;
			val.itemDef2 = Items.LunarBadLuck;
			Pair val2 = val;
			bool flag = false;
			for (int i = 0; i < newProviders.Length; i++)
			{
				if (!((Object)(object)newProviders[i].relationshipType == (Object)(object)ItemRelationshipTypes.ContagiousItem))
				{
					continue;
				}
				Pair[] array = newProviders[i].relationships;
				for (int j = 0; j < array.Length; j++)
				{
					Pair val3 = array[j];
					if (Object.op_Implicit((Object)(object)val3.itemDef1) && Object.op_Implicit((Object)(object)val3.itemDef2) && ((Object)val3.itemDef1).name == ((Object)Items.Clover).name && ((Object)val3.itemDef2).name == ((Object)Items.CloverVoid).name)
					{
						newProviders[i].relationships[j] = val2;
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					Log.Error("Unable to find original void pair.");
					Array.Resize(ref array, array.Length + 1);
					array[^1] = val2;
					newProviders[i].relationships = array;
				}
				break;
			}
		}
	}
}