Decompiled source of ForgeReforgedCompat v1.1.0

ForgeReforgedCompat.dll

Decompiled a day 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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.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 OblitusEs.ForgeReforgedCompat
{
	[BepInPlugin("OblitusEs.ForgeReforgedCompat", "ForgeReforgedCompat", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private sealed class Carry
		{
			public ItemData Source;

			public Dictionary<string, string> Data;

			public Vector2i Pos;

			public string Name;
		}

		public const string Guid = "OblitusEs.ForgeReforgedCompat";

		public const string Name = "ForgeReforgedCompat";

		public const string Version = "1.1.0";

		private const string RpHooks = "ReforgedPotential.ReforgedPotential+InventoryGuiPatch";

		private const string FoeStation = "ForgeOfEnchantment.Integration.ForgeStation";

		private static ManualLogSource _log;

		private static Func<CraftingStation, bool> _isForge;

		private static FieldInfo _craftUpgradeItem;

		private static bool _warnedForge;

		private static bool _warnedCarry;

		private void Awake()
		{
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Expected O, but got Unknown
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Expected O, but got Unknown
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Expected O, but got Unknown
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Expected O, but got Unknown
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Expected O, but got Unknown
			_log = ((BaseUnityPlugin)this).Logger;
			Type type = AccessTools.TypeByName("ReforgedPotential.ReforgedPotential+InventoryGuiPatch");
			if (type == null)
			{
				_log.LogInfo((object)"Reforged Potential is not installed - nothing to patch.");
				return;
			}
			_craftUpgradeItem = AccessTools.Field(typeof(InventoryGui), "m_craftUpgradeItem");
			if (_craftUpgradeItem == null)
			{
				_log.LogWarning((object)"InventoryGui.m_craftUpgradeItem not found; item data will not be carried over.");
			}
			Type type2 = AccessTools.TypeByName("ForgeOfEnchantment.Integration.ForgeStation");
			if (type2 != null)
			{
				MethodInfo methodInfo = AccessTools.Method(type2, "IsForge", new Type[1] { typeof(CraftingStation) }, (Type[])null);
				if (methodInfo != null)
				{
					_isForge = (Func<CraftingStation, bool>)Delegate.CreateDelegate(typeof(Func<CraftingStation, bool>), methodInfo);
				}
				else
				{
					_log.LogWarning((object)"ForgeStation.IsForge not found; falling back to a marker-component name check.");
					_isForge = FallbackIsForge;
				}
			}
			else
			{
				_log.LogInfo((object)"Forge of Enchantment is not installed - forge skip disabled, item-data fix still active.");
			}
			Harmony harmony = new Harmony("OblitusEs.ForgeReforgedCompat");
			HarmonyMethod prefix = new HarmonyMethod(typeof(Plugin), "SkipWhenForge_Bool", (Type[])null)
			{
				priority = 800
			};
			HarmonyMethod prefix2 = new HarmonyMethod(typeof(Plugin), "SkipWhenForge_Void", (Type[])null)
			{
				priority = 800
			};
			HarmonyMethod prefix3 = new HarmonyMethod(typeof(Plugin), "DoCrafting_Guard", (Type[])null)
			{
				priority = 800
			};
			HarmonyMethod postfix = new HarmonyMethod(typeof(Plugin), "DoCrafting_Carry", (Type[])null)
			{
				priority = 0
			};
			int num = 0;
			num += PatchHook(harmony, type, "DoCraftingPrefix", prefix3, postfix);
			num += PatchHook(harmony, type, "OnCraftPressedPrefix", prefix, null);
			num += PatchHook(harmony, type, "SetupCraftingPrefix", prefix2, null);
			num += PatchHook(harmony, type, "SetRecipePostfix", prefix2, null);
			_log.LogInfo((object)$"{num}/4 Reforged Potential hooks patched.");
		}

		private static int PatchHook(Harmony harmony, Type type, string method, HarmonyMethod prefix, HarmonyMethod postfix)
		{
			MethodInfo methodInfo = AccessTools.Method(type, method, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				_log.LogWarning((object)("Reforged Potential hook '" + method + "' not found - its version may have changed."));
				return 0;
			}
			harmony.Patch((MethodBase)methodInfo, prefix, postfix, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			return 1;
		}

		private static bool SkipWhenForge_Bool(ref bool __result)
		{
			if (!OnForgeStation())
			{
				return true;
			}
			__result = true;
			return false;
		}

		private static bool SkipWhenForge_Void()
		{
			return !OnForgeStation();
		}

		private static bool DoCrafting_Guard(ref bool __result, InventoryGui __0, out Carry __state)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			__state = null;
			if (OnForgeStation())
			{
				__result = true;
				return false;
			}
			try
			{
				object? obj = _craftUpgradeItem?.GetValue(__0);
				ItemData val = (ItemData)((obj is ItemData) ? obj : null);
				if (val != null && val.m_customData != null && val.m_customData.Count > 0)
				{
					__state = new Carry
					{
						Source = val,
						Data = new Dictionary<string, string>(val.m_customData),
						Pos = val.m_gridPos,
						Name = ((val.m_shared != null) ? val.m_shared.m_name : null)
					};
				}
			}
			catch (Exception e)
			{
				WarnCarry(e);
			}
			return true;
		}

		private static void DoCrafting_Carry(Carry __state, Player __1)
		{
			if (__state == null || (Object)(object)__1 == (Object)null)
			{
				return;
			}
			try
			{
				Inventory inventory = ((Humanoid)__1).GetInventory();
				if (inventory == null)
				{
					return;
				}
				ItemData itemAt = inventory.GetItemAt(__state.Pos.x, __state.Pos.y);
				if (itemAt == null || itemAt == __state.Source)
				{
					return;
				}
				string text = ((itemAt.m_shared != null) ? itemAt.m_shared.m_name : null);
				if (text != __state.Name)
				{
					return;
				}
				if (itemAt.m_customData == null)
				{
					itemAt.m_customData = new Dictionary<string, string>();
				}
				foreach (KeyValuePair<string, string> datum in __state.Data)
				{
					if (!itemAt.m_customData.ContainsKey(datum.Key))
					{
						itemAt.m_customData[datum.Key] = datum.Value;
					}
				}
			}
			catch (Exception e)
			{
				WarnCarry(e);
			}
		}

		private static bool OnForgeStation()
		{
			if (_isForge == null)
			{
				return false;
			}
			try
			{
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null)
				{
					return false;
				}
				CraftingStation currentCraftingStation = localPlayer.GetCurrentCraftingStation();
				if ((Object)(object)currentCraftingStation == (Object)null)
				{
					return false;
				}
				return _isForge(currentCraftingStation);
			}
			catch (Exception ex)
			{
				if (!_warnedForge)
				{
					_warnedForge = true;
					_log.LogWarning((object)("Forge check failed, leaving Reforged Potential active: " + ex.Message));
				}
				return false;
			}
		}

		private static bool FallbackIsForge(CraftingStation station)
		{
			Component[] components = ((Component)station).GetComponents<Component>();
			foreach (Component val in components)
			{
				if ((Object)(object)val != (Object)null && ((object)val).GetType().Name == "ForgeStationMarker")
				{
					return true;
				}
			}
			return false;
		}

		private static void WarnCarry(Exception e)
		{
			if (!_warnedCarry)
			{
				_warnedCarry = true;
				_log.LogWarning((object)("Carrying item data across an upgrade failed: " + e.Message));
			}
		}
	}
}