Decompiled source of BG GnomiumPayoutFix v1.0.0

BG-GnomiumPayoutFix-Joive.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Joive")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Host-only gnomium payout fix for Burglin Gnomes.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BG Gnomium Payout Fix")]
[assembly: AssemblyTitle("BG-GnomiumPayoutFix-Joive")]
[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 BGGnomiumPayoutFixJoive
{
	[BepInPlugin("joive.bg.gnomiumpayoutfix", "BG Gnomium Payout Fix", "1.0.0")]
	public sealed class GnomiumPayoutFixPlugin : BaseUnityPlugin
	{
		private const string Version = "1.0.0";

		private static GnomiumPayoutFixPlugin instance;

		private static ConfigEntry<bool> enableMod;

		private static ConfigEntry<bool> debugLogs;

		private Harmony harmony;

		private void Awake()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			instance = this;
			enableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Do not subtract dead gnomes from completed-task gnomium payout.");
			debugLogs = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugLogs", false, "Write payout changes to BepInEx LogOutput.log.");
			harmony = new Harmony("joive.bg.gnomiumpayoutfix");
			PatchPayoutMethod();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BG Gnomium Payout Fix 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if ((Object)(object)instance == (Object)(object)this)
			{
				instance = null;
			}
		}

		private void PatchPayoutMethod()
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method(typeof(GnomiumDeposit), "GetRewardFromCompletedTasks", (Type[])null, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(typeof(GnomiumPayoutFixPlugin), "GetRewardFromCompletedTasksPostfix", (Type[])null, (Type[])null);
			if (methodInfo == null || methodInfo2 == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Patch target not found: GnomiumDeposit.GetRewardFromCompletedTasks");
			}
			else
			{
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		public static void GetRewardFromCompletedTasksPostfix(GnomiumDeposit __instance, ref int __result)
		{
			if (enableMod != null && enableMod.Value && !((Object)(object)__instance == (Object)null) && IsHostOrServer(__instance))
			{
				int num = Math.Max(0, __instance.CompletedTasks);
				if (__result != num)
				{
					int num2 = __result;
					__result = num;
					Log("Gnomium payout fixed: " + num2 + " -> " + num + ".");
				}
			}
		}

		private static bool IsHostOrServer(GnomiumDeposit deposit)
		{
			NetworkManager singleton = NetworkManager.Singleton;
			if ((Object)(object)singleton == (Object)null || !singleton.IsListening)
			{
				return true;
			}
			if (!((NetworkBehaviour)deposit).IsServer && !singleton.IsServer)
			{
				return singleton.IsHost;
			}
			return true;
		}

		private static void Log(string message)
		{
			if (debugLogs != null && debugLogs.Value)
			{
				GnomiumPayoutFixPlugin gnomiumPayoutFixPlugin = instance;
				if (gnomiumPayoutFixPlugin != null)
				{
					((BaseUnityPlugin)gnomiumPayoutFixPlugin).Logger.LogInfo((object)message);
				}
			}
		}
	}
}