Decompiled source of GenericObjectRemover v1.1.5

GenericObjectRemover.dll

Decompiled 8 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("GenericObjectDestroyer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GenericObjectDestroyer")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("215d162e-dea6-4c04-b4fa-bfcc7d711102")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName = ".NET Framework 4.6.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace GenericObjectRemover;

[BepInProcess("valheim.exe")]
[BepInPlugin("juliandeclercq.GenericObjectRemover", "Generic Object Remover", "1.1.4")]
public class GenericObjectRemover : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Terminal), "InitTerminal")]
	private static class InputText_Patch
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static ConsoleEvent <>9__2_0;

			public static ConsoleEvent <>9__2_1;

			internal void <Postfix>b__2_0(ConsoleEventArgs args)
			{
				switch (args.Length)
				{
				case 1:
				{
					ZNetView val = HoveringObjectRemovable();
					ConsolePrint(((Object)(object)val == (Object)null) ? "Player is not hovering over a removable object." : ("Player is hovering over " + CustomFormat(((Object)((Component)val).gameObject).name) + "."));
					break;
				}
				case 2:
				{
					if (int.TryParse(args[1], out var result))
					{
						InspectRadius(result);
					}
					break;
				}
				default:
					ConsolePrint("Invalid amount of arguments for GORinspect. Expected syntax: |GORinspect| or |GORinspect [radius]|.");
					break;
				}
			}

			internal void <Postfix>b__2_1(ConsoleEventArgs args)
			{
				switch (args.Length)
				{
				case 2:
					RemoveObject(args[1].ToLower());
					break;
				case 3:
				{
					if (int.TryParse(args[2], out var result))
					{
						RemoveObjectRadius(args[1], result);
					}
					break;
				}
				default:
					ConsolePrint("Invalid amount of arguments for GORremove. Expected syntax: |GORremove [objectname]| or |GORremove [objectname] [radius]|.");
					break;
				}
			}
		}

		private const string inspect = "GORinspect";

		private const string remove = "GORremove";

		private static void Postfix(Terminal __instance)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//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_0029: Expected O, but got Unknown
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			object obj = <>c.<>9__2_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					switch (args.Length)
					{
					case 1:
					{
						ZNetView val3 = HoveringObjectRemovable();
						ConsolePrint(((Object)(object)val3 == (Object)null) ? "Player is not hovering over a removable object." : ("Player is hovering over " + CustomFormat(((Object)((Component)val3).gameObject).name) + "."));
						break;
					}
					case 2:
					{
						if (int.TryParse(args[1], out var result2))
						{
							InspectRadius(result2);
						}
						break;
					}
					default:
						ConsolePrint("Invalid amount of arguments for GORinspect. Expected syntax: |GORinspect| or |GORinspect [radius]|.");
						break;
					}
				};
				<>c.<>9__2_0 = val;
				obj = (object)val;
			}
			new ConsoleCommand("GORinspect", "Check the currently looked at object for removables. \nGORinspect [radius] - Check for removables in given radius around the player.", (ConsoleEvent)obj, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
			object obj2 = <>c.<>9__2_1;
			if (obj2 == null)
			{
				ConsoleEvent val2 = delegate(ConsoleEventArgs args)
				{
					switch (args.Length)
					{
					case 2:
						RemoveObject(args[1].ToLower());
						break;
					case 3:
					{
						if (int.TryParse(args[2], out var result))
						{
							RemoveObjectRadius(args[1], result);
						}
						break;
					}
					default:
						ConsolePrint("Invalid amount of arguments for GORremove. Expected syntax: |GORremove [objectname]| or |GORremove [objectname] [radius]|.");
						break;
					}
				};
				<>c.<>9__2_1 = val2;
				obj2 = (object)val2;
			}
			new ConsoleCommand("GORremove", "[objectname] - Remove an object. (use GORinspect to retrieve a removable object's name) \nGORremove [objectname] [radius] - Remove all objects with given name in given radius around the player.", (ConsoleEvent)obj2, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
		}
	}

	private void Awake()
	{
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	private static ZNetView HoveringObjectRemovable()
	{
		object value = Traverse.Create((object)Player.m_localPlayer).Field("m_hovering").GetValue();
		object obj = ((value is GameObject) ? value : null);
		if (obj == null)
		{
			return null;
		}
		return ((GameObject)obj).GetComponentInParent<ZNetView>();
	}

	private static HashSet<ZNetView> RemovablesInRadius(int radius)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		HashSet<ZNetView> hashSet = new HashSet<ZNetView>();
		Collider[] array = Physics.OverlapSphere(((Component)Player.m_localPlayer).transform.position, (float)radius, -1);
		for (int i = 0; i < array.Length; i++)
		{
			ZNetView componentInParent = ((Component)array[i]).GetComponentInParent<ZNetView>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				hashSet.Add(componentInParent);
			}
		}
		return hashSet;
	}

	private static void RemoveObject(string objectName)
	{
		ZNetView val = HoveringObjectRemovable();
		if ((Object)(object)val == (Object)null)
		{
			ConsolePrint("Player is not hovering over a removable object.");
			return;
		}
		string text = CustomFormat(objectName);
		string text2 = CustomFormat(((Object)((Component)val).gameObject).name);
		if (!text.Equals(text2))
		{
			ConsolePrint("Couldn't remove object. Received |" + text + "| expected |" + text2 + "|");
		}
		else
		{
			val.Destroy();
			ConsolePrint("Removed: " + objectName);
		}
	}

	private static void InspectRadius(int radius)
	{
		Dictionary<string, int> dictionary = new Dictionary<string, int>();
		foreach (ZNetView item in RemovablesInRadius(radius))
		{
			string key = CustomFormat(((Object)((Component)item).gameObject).name);
			dictionary.TryGetValue(key, out var value);
			dictionary[key] = value + 1;
		}
		foreach (KeyValuePair<string, int> item2 in dictionary)
		{
			ConsolePrint($"Found removable: {item2.Value}x {item2.Key} (radius = {radius})");
		}
	}

	private static void RemoveObjectRadius(string objectName, int radius)
	{
		string target = CustomFormat(objectName);
		IEnumerable<ZNetView> enumerable = from x in RemovablesInRadius(radius)
			where CustomFormat(((Object)((Component)x).gameObject).name).Equals(target)
			select x;
		foreach (ZNetView item in enumerable)
		{
			item.Destroy();
		}
		ConsolePrint($"Removed {enumerable.Count()}x {target} (radius = {radius})");
	}

	private static string CustomFormat(string input)
	{
		int num = input.IndexOf('(');
		if (num == -1)
		{
			return input.ToLower();
		}
		if (input.IndexOf(')') == -1)
		{
			return input.ToLower();
		}
		return input.Substring(0, num).ToLower();
	}

	private static void ConsolePrint(string line)
	{
		Traverse.Create((object)Console.instance).Method("Print", new object[1] { line }).GetValue();
	}
}