Decompiled source of ExtraSlots Misc Without QuickSlots v1.0.0

BepInEx/plugins/RageClanExtraSlotsMiscPatch.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;

[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("RageClanExtraSlotsMiscPatch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RageClanExtraSlotsMiscPatch")]
[assembly: AssemblyTitle("RageClanExtraSlotsMiscPatch")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RageClanExtraSlotsMiscPatch;

[BepInPlugin("rageclan.valheim.extraslots.miscwithoutquickslots", "RageClan ExtraSlots Misc Without QuickSlots", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class RageClanExtraSlotsMiscPatchPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "rageclan.valheim.extraslots.miscwithoutquickslots";

	public const string PluginName = "RageClan ExtraSlots Misc Without QuickSlots";

	public const string PluginVersion = "1.0.0";

	public const string ExtraSlotsGuid = "shudnal.ExtraSlots";

	private Harmony harmony;

	private void Awake()
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Expected O, but got Unknown
		try
		{
			MiscSlotAvailabilityPatch.Initialize();
			harmony = new Harmony("rageclan.valheim.extraslots.miscwithoutquickslots");
			harmony.PatchAll(typeof(RageClanExtraSlotsMiscPatchPlugin).Assembly);
			ExtraSlotsSettings.Enforce();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"RageClan ExtraSlots Misc Without QuickSlots v1.0.0 loaded. Quick Slots are disabled and Misc slots remain available.");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Unable to patch ExtraSlots: " + ex));
		}
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
internal static class ExtraSlotsSettings
{
	private static readonly Type ExtraSlotsType = AccessTools.TypeByName("ExtraSlots.ExtraSlots");

	internal static void Enforce()
	{
		if (ExtraSlotsType == null)
		{
			throw new TypeLoadException("ExtraSlots.ExtraSlots was not found.");
		}
		SetConfigEntryValue("quickSlotsAmount", 0);
		SetConfigEntryValue("miscSlotsEnabled", true);
	}

	private static void SetConfigEntryValue(string fieldName, object value)
	{
		FieldInfo fieldInfo = AccessTools.Field(ExtraSlotsType, fieldName);
		if (fieldInfo == null)
		{
			throw new MissingFieldException(ExtraSlotsType.FullName, fieldName);
		}
		object value2 = fieldInfo.GetValue(null);
		if (value2 == null)
		{
			throw new InvalidOperationException("ExtraSlots config entry is not initialized: " + fieldName);
		}
		PropertyInfo property = value2.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public);
		if (property == null || !property.CanWrite)
		{
			throw new MissingMemberException(value2.GetType().FullName, "Value");
		}
		if (!object.Equals(property.GetValue(value2, null), value))
		{
			property.SetValue(value2, value, null);
		}
	}
}
[HarmonyPatch]
internal static class MiscSlotAvailabilityPatch
{
	private static FieldInfo quickSlotsCountField;

	internal static void Initialize()
	{
		Type type = AccessTools.TypeByName("ExtraSlots.EquipmentPanel");
		if (type == null)
		{
			throw new TypeLoadException("ExtraSlots.EquipmentPanel was not found.");
		}
		quickSlotsCountField = AccessTools.Field(type, "quickSlotsCount");
		if (quickSlotsCountField == null)
		{
			throw new MissingFieldException("ExtraSlots.EquipmentPanel", "quickSlotsCount");
		}
		Type type2 = AccessTools.TypeByName("ExtraSlots.Slots");
		if (type2 == null || AccessTools.Method(type2, "IsFirstMiscSlotAvailable", (Type[])null, (Type[])null) == null || AccessTools.Method(type2, "IsSecondMiscSlotAvailable", (Type[])null, (Type[])null) == null)
		{
			throw new MissingMethodException("ExtraSlots misc-slot availability methods were not found.");
		}
	}

	private static IEnumerable<MethodBase> TargetMethods()
	{
		Type slotsType = AccessTools.TypeByName("ExtraSlots.Slots");
		yield return AccessTools.Method(slotsType, "IsFirstMiscSlotAvailable", (Type[])null, (Type[])null);
		yield return AccessTools.Method(slotsType, "IsSecondMiscSlotAvailable", (Type[])null, (Type[])null);
	}

	private static void Prefix(out int __state)
	{
		__state = (int)quickSlotsCountField.GetValue(null);
		if (__state == 0)
		{
			quickSlotsCountField.SetValue(null, 1);
		}
	}

	private static void Postfix(int __state)
	{
		if (__state == 0)
		{
			quickSlotsCountField.SetValue(null, 0);
		}
	}

	private static Exception Finalizer(Exception __exception, int __state)
	{
		if (__state == 0)
		{
			quickSlotsCountField.SetValue(null, 0);
		}
		return __exception;
	}
}