Decompiled source of MiningMode v1.0.2

MiningMode.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using MiningMode.Behaviors;
using MiningMode.Configs;
using MiningMode.Patches;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MiningMode")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MiningMode")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
namespace MiningMode
{
	[BepInPlugin("com.tafoo85.MiningMode", "MiningMode", "1.0.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	internal class MiningMode : BaseUnityPlugin
	{
		public const string PluginGUID = "com.tafoo85.MiningMode";

		public const string PluginName = "MiningMode";

		public const string PluginVersion = "1.0.2";

		private Harmony lootListHarmony;

		private void Awake()
		{
			Logger.LogDebug((object)"MiningMode has been loaded.");
			InitializeHarmony();
			InitializeConfigs();
		}

		private void OnDestroy()
		{
			Logger.LogDebug((object)"MiningMode has been unloaded.");
			lootListHarmony.UnpatchSelf();
		}

		private void Update()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKeyDown(MiningModeConfig.ToggleKey))
			{
				MiningModeConfig.ToggleMingingMode();
				bool flag = MiningModeConfig.IsMiningModeEnabled();
				MessageHud.instance.ShowMessage((MessageType)1, "Mining Mode: " + (flag ? "$hud_on" : "$hud_off"), 0, (Sprite)null);
			}
		}

		private void InitializeHarmony()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			lootListHarmony = new Harmony("com.tafoo85.MiningMode");
			lootListHarmony.PatchAll(typeof(PlayerAutoPickupPatch));
		}

		private void InitializeConfigs()
		{
			MiningModeConfig.Intialize(((BaseUnityPlugin)this).Config);
			MiningModeConfig.OnConfigChanged += OnConfigChanged;
		}

		private void OnConfigChanged()
		{
			RebootHarmony();
		}

		private void RebootHarmony()
		{
			Logger.LogDebug((object)"Rebooting Harmony.");
			lootListHarmony.UnpatchSelf();
			lootListHarmony.PatchAll(typeof(PlayerAutoPickupPatch));
		}
	}
}
namespace MiningMode.Configs
{
	internal static class MiningModeConfig
	{
		public delegate void ConfigChanged();

		private static ConfigEntry<bool> miningModeEnabled;

		private static ConfigEntry<string> oreNames;

		private static ConfigEntry<string> whiteListedComponents;

		private static ConfigEntry<bool> whiteListEnabled;

		private static ConfigEntry<string> toggleButton;

		private static FileSystemWatcher configFileWatcher;

		private static ConfigFile configFile;

		private static readonly string configFileName = "com.tafoo85.MiningMode.cfg";

		public static KeyCode ToggleKey
		{
			get
			{
				//IL_001e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0023: Unknown result type (might be due to invalid IL or missing references)
				//IL_0033: Unknown result type (might be due to invalid IL or missing references)
				//IL_0043: Unknown result type (might be due to invalid IL or missing references)
				//IL_0046: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					return (KeyCode)Enum.Parse(typeof(KeyCode), toggleButton.Value.ToUpper());
				}
				catch (ArgumentException)
				{
					Logger.LogError((object)"Invalid key code found in config. Defaulting to Z.");
					return (KeyCode)122;
				}
				catch (OverflowException)
				{
					Logger.LogError((object)"Invalid key code found in config. Defaulting to Z.");
					return (KeyCode)122;
				}
			}
			set
			{
				toggleButton.Value = ((object)(KeyCode)(ref value)).ToString().ToUpper();
			}
		}

		public static event ConfigChanged OnConfigChanged;

		public static void Intialize(ConfigFile config)
		{
			miningModeEnabled = config.Bind<bool>("General", "MiningModeEnabled", false, "Enable mining mode which prevents anything other than ore from being autolooted.");
			toggleButton = config.Bind<string>("General", "ToggleButton", "Z", "Key to toggle mining mode.");
			whiteListedComponents = config.Bind<string>("WhiteList", "WhiteListedComponents", "", "List of additional components that should be autolooted.");
			whiteListEnabled = config.Bind<bool>("WhiteList", "WhiteListEnabled", false, "Enable the autoloot whitelist feature.");
			oreNames = config.Bind<string>("WhiteList", "OreNames", "$item_copperore,$item_tinore,$item_ironore,$item_silverore,$item_blackmetalscrap,$item_ironscrap,$item_tar", "List of ore item names to autoloot.");
			configFile = config;
			InitializeListeners();
		}

		private static void InitializeListeners()
		{
			Logger.LogDebug((object)$"Initializing config listeners with path: {configFile.ConfigFilePath}");
			configFileWatcher = new FileSystemWatcher(Paths.ConfigPath, configFileName);
			configFileWatcher.Changed += OnConfigFileChanged;
			configFileWatcher.Created += OnConfigFileChanged;
			configFileWatcher.Renamed += OnConfigFileChanged;
			configFileWatcher.EnableRaisingEvents = true;
			configFileWatcher.IncludeSubdirectories = true;
		}

		private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
		{
			configFile.Reload();
			MiningModeConfig.OnConfigChanged?.Invoke();
		}

		public static bool IsMiningModeEnabled()
		{
			return miningModeEnabled.Value;
		}

		public static void ToggleMingingMode()
		{
			miningModeEnabled.Value = !miningModeEnabled.Value;
		}

		public static bool IsOreItem(string name)
		{
			return oreNames.Value.Contains(name);
		}

		public static bool IsWhiteListed(string name)
		{
			return whiteListedComponents.Value.Contains(name);
		}

		public static bool IsWhitelistEnabled()
		{
			return whiteListEnabled.Value;
		}

		public static void AddWhiteListed(string name)
		{
			HashSet<string> hashSet = whiteListedComponents.Value.Split(new char[1] { ',' }).ToHashSet();
			if (hashSet.Add(name))
			{
				whiteListedComponents.Value = hashSet.Aggregate((string i, string j) => i + "," + j);
			}
		}

		public static void RemoveWhiteListed(string name)
		{
			HashSet<string> hashSet = whiteListedComponents.Value.Split(new char[1] { ',' }).ToHashSet();
			if (hashSet.Remove(name))
			{
				whiteListedComponents.Value = hashSet.Aggregate((string i, string j) => i + "," + j);
			}
		}
	}
}
namespace MiningMode.Patches
{
	[HarmonyPatch(typeof(Player))]
	public class PlayerAutoPickupPatch
	{
		public static bool PatchedComponents { get; set; }

		[HarmonyPatch("AutoPickup")]
		[HarmonyPrefix]
		public static bool ItemAutoPickupPrefix(Player __instance)
		{
			if (!MiningModeConfig.IsMiningModeEnabled())
			{
				if (PatchedComponents)
				{
					PatchComponents(__instance, delegate(ItemDrop component)
					{
						if ((Object)(object)((Component)component).gameObject.GetComponent<AutolootableBehavior>() != (Object)null)
						{
							Object.Destroy((Object)(object)((Component)component).gameObject.GetComponent<AutolootableBehavior>());
							component.m_autoPickup = true;
						}
					});
				}
				PatchedComponents = false;
				return true;
			}
			PatchComponents(__instance, delegate(ItemDrop component)
			{
				if (!IsLootableItem(component.m_itemData.m_shared.m_name) && component.m_autoPickup)
				{
					((Component)component).gameObject.AddComponent<AutolootableBehavior>();
					component.m_autoPickup = false;
					PatchedComponents = true;
				}
			});
			return true;
		}

		private static void PatchComponents(Player __instance, Action<ItemDrop> workFunction)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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)
			Collider[] array = Physics.OverlapSphere(((Component)__instance).transform.position + Vector3.up, __instance.m_autoPickupRange, __instance.m_autoPickupMask);
			foreach (Collider val in array)
			{
				if (!((Object)(object)val.attachedRigidbody == (Object)null))
				{
					ItemDrop component = ((Component)val.attachedRigidbody).GetComponent<ItemDrop>();
					if (!((Object)(object)component == (Object)null))
					{
						workFunction(component);
					}
				}
			}
		}

		private static bool IsLootableItem(string name)
		{
			Logger.LogDebug((object)$"Checking status for item: {name}");
			if (MiningModeConfig.IsOreItem(name) || WhitelistApplies(name))
			{
				Logger.LogDebug((object)$"Item should be lootable: {name}");
				return true;
			}
			Logger.LogDebug((object)$"Item should not be lootable: {name}");
			return false;
		}

		private static bool WhitelistApplies(string name)
		{
			Logger.LogDebug((object)$"Item should be lootable from whitelist: {name}");
			if (MiningModeConfig.IsWhitelistEnabled())
			{
				return MiningModeConfig.IsWhiteListed(name);
			}
			return false;
		}
	}
}
namespace MiningMode.Behaviors
{
	public class AutolootableBehavior : MonoBehaviour
	{
	}
}