Decompiled source of ForgeOfPotentialSafe v1.0.3

BepInEx/plugins/ForgeOfPotentialSafe.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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ForgeOfPotentialSafe")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ForgeOfPotentialSafe")]
[assembly: AssemblyTitle("ForgeOfPotentialSafe")]
[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 ForgeOfPotentialSafe
{
	[BepInPlugin("outsider.valheim.forgeofpotentialsafe", "Forge of Potential Safe", "1.0.3")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "outsider.valheim.forgeofpotentialsafe";

		public const string PluginName = "Forge of Potential Safe";

		public const string PluginVersion = "1.0.3";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> SuccessChance;

		internal static ConfigEntry<float> BreakChance;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Expected O, but got Unknown
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("ForgeOfPotential", "Enabled", true, "Enable the Forge of Potential safety patch.");
			SuccessChance = ((BaseUnityPlugin)this).Config.Bind<float>("ForgeOfPotential", "SuccessChance", 1f, "Success chance used during Forge of Potential refinement (0.0-1.0).");
			BreakChance = ((BaseUnityPlugin)this).Config.Bind<float>("ForgeOfPotential", "BreakChance", 0f, "Break chance used during Forge of Potential refinement (0.0-1.0).");
			harmony = new Harmony("outsider.valheim.forgeofpotentialsafe");
			if (Enabled.Value)
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(InventoryGui), "DoCrafting", (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					Log.LogError((object)"Could not find InventoryGui.DoCrafting.");
					return;
				}
				try
				{
					harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(ForgePatch), "Transpiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
					Log.LogInfo((object)"Forge of Potential IL patch installed on InventoryGui.DoCrafting.");
					Log.LogInfo((object)$"Configured SuccessChance={Clamp(SuccessChance.Value):0.###}, BreakChance={Clamp(BreakChance.Value):0.###}");
				}
				catch (Exception ex)
				{
					Log.LogError((object)("Failed to patch InventoryGui.DoCrafting: " + ex));
				}
			}
			Log.LogInfo((object)"Forge of Potential Safe 1.0.3 loaded.");
		}

		internal static float Clamp(float value)
		{
			return Math.Max(0f, Math.Min(1f, value));
		}

		private void OnDestroy()
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class ForgePatch
	{
		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Expected O, but got Unknown
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int num = 0;
			int num2 = 0;
			for (int i = 0; i < list.Count; i++)
			{
				CodeInstruction val = list[i];
				if (!(val.opcode != OpCodes.Ldfld) && val.operand is FieldInfo fieldInfo)
				{
					if (fieldInfo.Name == "m_upgradeChance")
					{
						val.opcode = OpCodes.Pop;
						val.operand = null;
						list.Insert(i + 1, new CodeInstruction(OpCodes.Ldc_R4, (object)Plugin.Clamp(Plugin.SuccessChance.Value)));
						num++;
						i++;
					}
					else if (fieldInfo.Name == "m_breakChance")
					{
						val.opcode = OpCodes.Pop;
						val.operand = null;
						list.Insert(i + 1, new CodeInstruction(OpCodes.Ldc_R4, (object)Plugin.Clamp(Plugin.BreakChance.Value)));
						num2++;
						i++;
					}
				}
			}
			Plugin.Log.LogInfo((object)$"IL Forge patch 1.0.3: replaced m_upgradeChance={num}, m_breakChance={num2} using stack-safe POP + constant.");
			if (num == 0 || num2 == 0)
			{
				Plugin.Log.LogWarning((object)"IL Forge patch did not find both chance fields. Refinement may still use vanilla odds.");
			}
			return list;
		}
	}
}