Decompiled source of TossingQualityScrapFix v1.0.0

TossingQualityScrapFix.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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("TossingQualityScrapFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TossingQualityScrapFix")]
[assembly: AssemblyTitle("TossingQualityScrapFix")]
[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 TossingQualityScrapFix
{
	[BepInPlugin("com.videowiz92.tossingqualityscrapfix", "TossingQualityScrapFix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.videowiz92.tossingqualityscrapfix";

		public const string Name = "TossingQualityScrapFix";

		public const string Version = "1.0.0";

		public const string ZetGuid = "com.TPDespair.ZetArtifacts";

		private const string DropifactType = "TPDespair.ZetArtifacts.ZetDropifact";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> FixQualityScrap;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			FixQualityScrap = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FixQualityScrap", true, "When true, scrapping a Quality item with the Artifact of Tossing (ZetArtifacts) drops the matching Quality scrap (e.g. an Epic white item gives Epic White Scrap) instead of plain vanilla scrap.");
			if (!Chainloader.PluginInfos.TryGetValue("com.TPDespair.ZetArtifacts", out var value) || (Object)(object)value.Instance == (Object)null)
			{
				Log.LogError((object)"ZetArtifacts is not loaded; nothing to patch.");
				((Behaviour)this).enabled = false;
				return;
			}
			string text = value.Metadata.Version.ToString();
			if (text != "1.6.6")
			{
				Log.LogWarning((object)("ZetArtifacts " + text + " is installed but this fix was written against 1.6.6. It is skipped (with a warning) if the code it patches has changed."));
			}
			_harmony = new Harmony("com.videowiz92.tossingqualityscrapfix");
			try
			{
				ScrapPatch.Apply(_harmony, ((object)value.Instance).GetType().Assembly.GetType("TPDespair.ZetArtifacts.ZetDropifact", throwOnError: false));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Quality scrap patch failed and was skipped: " + ex));
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class QualityBridge
	{
		public const string PluginGuid = "com.Gorakh.ItemQualities";

		private static bool _resolved;

		private static MethodInfo _getTier;

		private static MethodInfo _getItemOfQuality;

		private static object _tierNone;

		public static bool Available
		{
			get
			{
				if (!_resolved)
				{
					Resolve();
				}
				if (_getTier != null)
				{
					return _getItemOfQuality != null;
				}
				return false;
			}
		}

		private static void Resolve()
		{
			_resolved = true;
			try
			{
				if (!Chainloader.PluginInfos.TryGetValue("com.Gorakh.ItemQualities", out var value) || (Object)(object)value.Instance == (Object)null)
				{
					return;
				}
				Assembly assembly = ((object)value.Instance).GetType().Assembly;
				Type type = assembly.GetType("ItemQualities.QualityCatalog", throwOnError: false);
				Type type2 = assembly.GetType("ItemQualities.QualityTier", throwOnError: false);
				if (type == null || type2 == null)
				{
					Plugin.Log.LogWarning((object)"ItemQualities.QualityCatalog / QualityTier not found; Quality scrap fix disabled.");
					return;
				}
				_getTier = type.GetMethod("GetQualityTier", new Type[1] { typeof(ItemIndex) });
				_getItemOfQuality = type.GetMethod("GetItemIndexOfQuality", new Type[2]
				{
					typeof(ItemIndex),
					type2
				});
				_tierNone = Enum.ToObject(type2, -1);
				if (_getTier == null || _getItemOfQuality == null)
				{
					Plugin.Log.LogWarning((object)"QualityCatalog.GetQualityTier / GetItemIndexOfQuality not found; Quality scrap fix disabled.");
					_getTier = null;
					_getItemOfQuality = null;
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Could not bind to the Quality mod; Quality scrap fix disabled: " + ex.Message));
				_getTier = null;
				_getItemOfQuality = null;
			}
		}

		public static ItemIndex MapScrap(ItemIndex source, ItemIndex scrap)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: 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: Invalid comparison between Unknown and I4
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			if (!Available)
			{
				return scrap;
			}
			object obj = _getTier.Invoke(null, new object[1] { source });
			if (obj == null || obj.Equals(_tierNone))
			{
				return scrap;
			}
			if (!(_getItemOfQuality.Invoke(null, new object[2] { scrap, obj }) is ItemIndex val) || (int)val == -1)
			{
				return scrap;
			}
			return val;
		}
	}
	internal static class ScrapPatch
	{
		private static MethodInfo _createDropletInfo;

		private static Action<ItemIndex, Vector3, float> _createDroplet;

		public static void Apply(Harmony harmony, Type dropifact)
		{
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Expected O, but got Unknown
			if (dropifact == null)
			{
				Plugin.Log.LogWarning((object)"ZetArtifacts type ZetDropifact not found; Quality scrap fix not applied.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(dropifact, "DropItem", new Type[5]
			{
				typeof(CharacterBody),
				typeof(Inventory),
				typeof(ItemIndex),
				typeof(float),
				typeof(bool)
			}, (Type[])null);
			_createDropletInfo = AccessTools.Method(dropifact, "CreateDroplet", new Type[3]
			{
				typeof(ItemIndex),
				typeof(Vector3),
				typeof(float)
			}, (Type[])null);
			if (methodInfo == null || _createDropletInfo == null)
			{
				Plugin.Log.LogWarning((object)"ZetDropifact.DropItem / CreateDroplet(ItemIndex, ...) not found; Quality scrap fix not applied. ZetArtifacts may have changed.");
				return;
			}
			_createDroplet = (Action<ItemIndex, Vector3, float>)Delegate.CreateDelegate(typeof(Action<ItemIndex, Vector3, float>), _createDropletInfo);
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(ScrapPatch), "Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo replacement = AccessTools.Method(typeof(ScrapPatch), "CreateDropletForScrap", (Type[])null, (Type[])null);
			int replaced = 0;
			foreach (CodeInstruction instruction in instructions)
			{
				if ((instruction.opcode == OpCodes.Call || instruction.opcode == OpCodes.Callvirt) && instruction.operand as MethodInfo == _createDropletInfo)
				{
					yield return new CodeInstruction(OpCodes.Ldarg_2, (object)null);
					yield return new CodeInstruction(OpCodes.Ldarg_S, (object)(byte)4);
					yield return new CodeInstruction(OpCodes.Call, (object)replacement);
					replaced++;
				}
				else
				{
					yield return instruction;
				}
			}
			if (replaced == 0)
			{
				Plugin.Log.LogWarning((object)"ZetDropifact.DropItem no longer calls CreateDroplet(ItemIndex, ...); Quality scrap fix has no effect. ZetArtifacts may have changed.");
			}
			else
			{
				Plugin.Log.LogInfo((object)"Quality scrap fix applied to the Artifact of Tossing.");
			}
		}

		private static void CreateDropletForScrap(ItemIndex dropIndex, Vector3 position, float angle, ItemIndex scrappedItem, bool scrap)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			if (scrap && Plugin.FixQualityScrap.Value)
			{
				try
				{
					dropIndex = QualityBridge.MapScrap(scrappedItem, dropIndex);
				}
				catch (Exception ex)
				{
					Plugin.Log.LogWarning((object)("Quality scrap remap failed; dropping vanilla scrap: " + ex.Message));
				}
			}
			_createDroplet(dropIndex, position, angle);
		}
	}
}