Decompiled source of aedenthorn HoeRadius v0.4.0

aedenthorn_HoeRadius/HoeRadius.dll

Decompiled 2 months 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 TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			int result;
			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;
				result = ((instance != null && instance.HasFocus()) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
		int result2;
		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;
				result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
				goto IL_00d2;
			}
		}
		result2 = 1;
		goto IL_00d2;
		IL_00d2:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			foreach (string text in array)
			{
				if (Input.GetKeyDown(text.ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool CheckKeyUp(string value)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			foreach (string text in array)
			{
				if (Input.GetKeyUp(text.ToLower()))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			string[] array = value.Split(new char[1] { ',' });
			foreach (string text in array)
			{
				if (Input.GetKey(text.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_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: 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.2.1")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(TerrainOp), "Awake")]
	private static class TerrainOp_Patch
	{
		private 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}");
				}
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue();
				return false;
			}
			return true;
		}
	}

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> isDebug;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<bool> useScrollWheel;

	public static ConfigEntry<string> scrollModKey;

	public static ConfigEntry<string> increaseHotKey;

	public static ConfigEntry<string> decreaseHotKey;

	public static ConfigEntry<float> scrollWheelScale;

	public static ConfigEntry<float> hotkeyScale;

	private static BepInExPlugin context;

	private static float lastOriginalRadius;

	private static float lastModdedRadius;

	private static float lastTotalDelta;

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

	private 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.1f, "Scroll wheel change scale");
		scrollModKey = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "ScrollModKey", "left alt", "Modifer key to allow scroll wheel change. Use https://docs.unity3d.com/Manual/class-InputManager.html");
		increaseHotKey = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "IncreaseHotKey", "", "Hotkey to increase radius. Use https://docs.unity3d.com/Manual/class-InputManager.html");
		decreaseHotKey = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "DecreaseHotKey", "", "Hotkey to decrease radius. Use https://docs.unity3d.com/Manual/class-InputManager.html");
		hotkeyScale = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "HotkeyScale", 0.1f, "Hotkey change scale");
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	private void Update()
	{
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		if (!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 && AedenthornUtils.CheckKeyHeld(scrollModKey.Value, req: false) && Input.mouseScrollDelta.y != 0f)
		{
			SetRadius(Input.mouseScrollDelta.y * scrollWheelScale.Value);
		}
		else if (AedenthornUtils.CheckKeyDown(increaseHotKey.Value))
		{
			SetRadius(hotkeyScale.Value);
		}
		else if (AedenthornUtils.CheckKeyDown(decreaseHotKey.Value))
		{
			SetRadius(0f - hotkeyScale.Value);
		}
	}

	private void SetRadius(float delta)
	{
		//IL_0255: 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);
			}
		}
	}
}