Decompiled source of Hoe Radius by Aedonthorn Fixed v1.0.0

HoeRadius.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+be3dd3a9c1040848b2b09ad4f2d0267474f174e5")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				if (instance == null)
				{
					return false;
				}
				return instance.HasFocus();
			}
			return true;
		}
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				TextViewer instance3 = TextViewer.instance;
				if (instance3 == null)
				{
					return false;
				}
				return instance3.IsVisible();
			}
		}
		return true;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				if (Input.GetKeyDown(array[i].ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool CheckKeyUp(string value)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				if (Input.GetKeyUp(array[i].ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				if (Input.GetKey(array[i].ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return !req;
	}

	public static void ShuffleList<T>(List<T> list)
	{
		int num = list.Count;
		while (num > 1)
		{
			num--;
			int index = Random.Range(0, num);
			T value = list[index];
			list[index] = list[num];
			list[num] = value;
		}
	}

	public static string GetAssetPath(object obj, bool create = false)
	{
		return GetAssetPath(obj.GetType().Namespace, create);
	}

	public static string GetAssetPath(string name, bool create = false)
	{
		string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), name);
		if (create && !Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		return text;
	}

	public static string GetTransformPath(Transform t)
	{
		if (!Object.op_Implicit((Object)(object)t.parent))
		{
			return ((Object)t).name;
		}
		return GetTransformPath(t.parent) + "/" + ((Object)t).name;
	}

	public static byte[] EncodeToPNG(Texture2D texture)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Expected O, but got Unknown
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Expected O, but got Unknown
		RenderTexture temporary = RenderTexture.GetTemporary(((Texture)texture).width, ((Texture)texture).height, 0, (RenderTextureFormat)7, (RenderTextureReadWrite)0);
		Graphics.Blit((Texture)(object)texture, temporary);
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = temporary;
		Texture2D val = new Texture2D(((Texture)texture).width, ((Texture)texture).height, (TextureFormat)4, true, false);
		val.ReadPixels(new Rect(0f, 0f, (float)((Texture)temporary).width, (float)((Texture)temporary).height), 0, 0);
		val.Apply();
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
		Texture2D val2 = new Texture2D(((Texture)texture).width, ((Texture)texture).height);
		val2.SetPixels(val.GetPixels());
		val2.Apply();
		return ImageConversion.EncodeToPNG(val2);
	}
}
namespace HoeRadius;

[BepInPlugin("aedenthorn.HoeRadius", "Hoe Radius", "0.4.1")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ZInput), "GetMouseScrollWheel")]
	public static class ZInput_GetMouseScrollWheel_Patch
	{
		public static bool Prefix(ref float __result)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || !Object.op_Implicit((Object)(object)Player.m_localPlayer) || !((Character)Player.m_localPlayer).InPlaceMode() || Hud.IsPieceSelectionVisible() || !useScrollWheel.Value || ((int)scrollModKey.Value != 0 && !Input.GetKey(scrollModKey.Value)))
			{
				return true;
			}
			__result = 0f;
			return false;
		}
	}

	[HarmonyPatch(typeof(TerrainOp), "Awake")]
	public static class TerrainOp_Patch
	{
		public static void Prefix(TerrainOp __instance)
		{
			if (modEnabled.Value)
			{
				if (__instance.m_settings.m_level)
				{
					Settings settings = __instance.m_settings;
					settings.m_levelRadius += lastTotalDelta;
					Dbgl($"Applying level radius {__instance.m_settings.m_levelRadius}");
				}
				if (__instance.m_settings.m_raise)
				{
					Settings settings2 = __instance.m_settings;
					settings2.m_raiseRadius += lastTotalDelta;
					Dbgl($"Applying raise radius {__instance.m_settings.m_raiseRadius}");
				}
				if (__instance.m_settings.m_smooth)
				{
					Settings settings3 = __instance.m_settings;
					settings3.m_smoothRadius += lastTotalDelta;
					Dbgl($"Applying smooth radius {__instance.m_settings.m_smoothRadius}");
				}
				if (__instance.m_settings.m_paintCleared)
				{
					Settings settings4 = __instance.m_settings;
					settings4.m_paintRadius += lastTotalDelta;
					Dbgl($"Applying paint radius {__instance.m_settings.m_paintRadius}");
				}
			}
		}
	}

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> isDebug;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<bool> useScrollWheel;

	public static ConfigEntry<KeyCode> scrollModKey;

	public static ConfigEntry<KeyCode> increaseHotKey;

	public static ConfigEntry<KeyCode> decreaseHotKey;

	public static ConfigEntry<float> scrollWheelScale;

	public static ConfigEntry<float> hotkeyScale;

	public static BepInExPlugin context;

	public static float lastOriginalRadius;

	public static float lastModdedRadius;

	public static float lastTotalDelta;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug.Value)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	public void Awake()
	{
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		isDebug = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IsDebug", true, "Enable debug logs");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1199, "Nexus mod ID for updates");
		useScrollWheel = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "UseScrollWheel", true, "Use scroll wheel to modify radius");
		scrollWheelScale = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "ScrollWheelScale", 0.3f, "Scroll wheel change scale");
		scrollModKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Settings", "ScrollModKey", (KeyCode)308, "Modifer key to allow scroll wheel change.");
		decreaseHotKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Settings", "DecreaseHotKey", (KeyCode)61, "Hotkey to decrease radius.");
		increaseHotKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Settings", "IncreaseHotKey", (KeyCode)45, "Hotkey to increase radius.");
		hotkeyScale = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "HotkeyScale", 0.03f, "Hotkey change scale");
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	public void Update()
	{
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		if (!modEnabled.Value || !Object.op_Implicit((Object)(object)Player.m_localPlayer) || !((Character)Player.m_localPlayer).InPlaceMode() || Hud.IsPieceSelectionVisible())
		{
			if (lastOriginalRadius != 0f)
			{
				lastOriginalRadius = 0f;
				lastModdedRadius = 0f;
				lastTotalDelta = 0f;
				SetRadius(0f);
			}
		}
		else if (useScrollWheel.Value && ((int)scrollModKey.Value == 0 || Input.GetKey(scrollModKey.Value)) && Input.mouseScrollDelta.y != 0f)
		{
			SetRadius(Input.mouseScrollDelta.y * scrollWheelScale.Value);
		}
		else if (Input.GetKey(increaseHotKey.Value))
		{
			SetRadius(hotkeyScale.Value);
		}
		else if (Input.GetKey(decreaseHotKey.Value))
		{
			SetRadius(0f - hotkeyScale.Value);
		}
	}

	public void SetRadius(float delta)
	{
		//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
		Traverse obj = Traverse.Create((object)Player.m_localPlayer).Field("m_buildPieces");
		object obj2;
		if (obj == null)
		{
			obj2 = null;
		}
		else
		{
			PieceTable value = obj.GetValue<PieceTable>();
			obj2 = ((value != null) ? value.GetSelectedPiece() : null);
		}
		Piece val = (Piece)obj2;
		if (val == null)
		{
			return;
		}
		TerrainOp val2 = ((val != null) ? ((Component)val).gameObject.GetComponent<TerrainOp>() : null);
		if ((Object)(object)val2 == (Object)null)
		{
			return;
		}
		float num = 0f;
		float num2 = Mathf.Max(lastModdedRadius + delta, 0f);
		lastTotalDelta += delta;
		if (lastOriginalRadius == 0f)
		{
			if (val2.m_settings.m_level && num < val2.m_settings.m_levelRadius)
			{
				num = val2.m_settings.m_levelRadius;
				num2 = Mathf.Max(val2.m_settings.m_levelRadius + delta, 0f);
			}
			if (val2.m_settings.m_raise && num < val2.m_settings.m_raiseRadius)
			{
				num = val2.m_settings.m_raiseRadius;
				num2 = Mathf.Max(val2.m_settings.m_raiseRadius + delta, 0f);
			}
			if (val2.m_settings.m_smooth && num < val2.m_settings.m_smoothRadius)
			{
				num = val2.m_settings.m_smoothRadius;
				num2 = Mathf.Max(val2.m_settings.m_smoothRadius + delta, 0f);
			}
			if (val2.m_settings.m_paintCleared && num < val2.m_settings.m_paintRadius)
			{
				num = val2.m_settings.m_paintRadius;
				num2 = Mathf.Max(val2.m_settings.m_paintRadius + delta, 0f);
			}
			lastOriginalRadius = num;
		}
		lastModdedRadius = num2;
		if (lastOriginalRadius > 0f && lastModdedRadius > 0f)
		{
			GameObject value2 = Traverse.Create((object)Player.m_localPlayer).Field("m_placementGhost").GetValue<GameObject>();
			Transform val3 = ((value2 != null) ? value2.transform.Find("_GhostOnly") : null);
			if ((Object)(object)val3 != (Object)null)
			{
				val3.localScale = new Vector3(lastModdedRadius / lastOriginalRadius, lastModdedRadius / lastOriginalRadius, lastModdedRadius / lastOriginalRadius);
			}
		}
	}
}