Decompiled source of SprinklerExtension v1.0.0

BepInEx/plugins/OldMarket.SprinklerExtension.dll

Decompiled 2 weeks 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 BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("OldMarket.SprinklerExtension")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OldMarket.SprinklerExtension")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("31b273a3-7eae-4a91-8f3d-a8c104ed87ae")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace OldMarket.SprinklerExtension;

[BepInPlugin("oldmarket.sprinklerextension", "Old Market Sprinkler Extension", "1.0.0")]
public class SprinklerExtensionPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "oldmarket.sprinklerextension";

	public const string PluginName = "Old Market Sprinkler Extension";

	public const string PluginVersion = "1.0.0";

	internal static ConfigEntry<float> ConfigRadius;

	internal static ConfigEntry<Key> ConfigToggleKey;

	internal static bool RangeVisible;

	internal static ManualLogSource Log;

	private Harmony _harmony;

	private void Awake()
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Expected O, but got Unknown
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		ConfigRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Radius", 5f, "Sprinkler watering radius in world units.");
		ConfigToggleKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Keybinds", "ToggleKey", (Key)98, "Key to toggle the display of sprinkler range indicator.");
		RangeVisible = false;
		_harmony = new Harmony("oldmarket.sprinklerextension");
		_harmony.PatchAll();
		GameObject val = new GameObject("SprinklerExtensionController");
		Object.DontDestroyOnLoad((Object)(object)val);
		((Object)val).hideFlags = (HideFlags)61;
		val.AddComponent<SprinklerExtensionController>();
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
[HarmonyPatch(typeof(BlockSprinkler))]
internal static class BlockSprinkler_OnNetworkSpawn_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch("OnNetworkSpawn")]
	private static void Prefix(BlockSprinkler __instance)
	{
		if (!((Object)(object)__instance == (Object)null))
		{
			float num = Mathf.Max(0.1f, SprinklerExtensionPlugin.ConfigRadius.Value);
			if (__instance.radius < num)
			{
				__instance.radius = num;
			}
		}
	}
}
public class SprinklerExtensionController : MonoBehaviour
{
	private readonly HashSet<MonoBehaviour> _knownSprinklers = new HashSet<MonoBehaviour>();

	private float _nextScanTime;

	private void Awake()
	{
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	private void Update()
	{
		HandleToggleInput();
		if (Time.time >= _nextScanTime)
		{
			_nextScanTime = Time.time + 2f;
			ScanSprinklers();
		}
	}

	private void HandleToggleInput()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: 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_0025: Invalid comparison between Unknown and I4
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		Keyboard current = Keyboard.current;
		if (current == null)
		{
			return;
		}
		ConfigEntry<Key> configToggleKey = SprinklerExtensionPlugin.ConfigToggleKey;
		Key val = (Key)((configToggleKey != null) ? ((int)configToggleKey.Value) : 0);
		if ((int)val != 0)
		{
			KeyControl val2 = current[val];
			if (val2 != null && ((ButtonControl)val2).wasPressedThisFrame)
			{
				SprinklerExtensionPlugin.RangeVisible = !SprinklerExtensionPlugin.RangeVisible;
			}
		}
	}

	private void ScanSprinklers()
	{
		MonoBehaviour[] array = Object.FindObjectsOfType<MonoBehaviour>(true);
		if (array == null || array.Length == 0)
		{
			return;
		}
		float num = Math.Max(0.1f, SprinklerExtensionPlugin.ConfigRadius.Value);
		MonoBehaviour[] array2 = array;
		foreach (MonoBehaviour val in array2)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			Type type = ((object)val).GetType();
			if (!string.Equals(type.Name, "BlockSprinkler", StringComparison.Ordinal))
			{
				continue;
			}
			_knownSprinklers.Add(val);
			FieldInfo field = type.GetField("radius", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				try
				{
					float num3 = ((field.GetValue(val) is float num2) ? num2 : 0f);
					if (num3 < num - 0.001f)
					{
						field.SetValue(val, num);
					}
				}
				catch
				{
				}
			}
			SprinklerRangeIndicator sprinklerRangeIndicator = ((Component)val).gameObject.GetComponent<SprinklerRangeIndicator>();
			if ((Object)(object)sprinklerRangeIndicator == (Object)null)
			{
				sprinklerRangeIndicator = ((Component)val).gameObject.AddComponent<SprinklerRangeIndicator>();
			}
			sprinklerRangeIndicator.BindTarget(val, field);
		}
	}
}
public class SprinklerRangeIndicator : MonoBehaviour
{
	private GameObject _ring;

	private MonoBehaviour _sprinklerComponent;

	private FieldInfo _radiusField;

	private Renderer _renderer;

	public void BindTarget(MonoBehaviour sprinklerComponent, FieldInfo radiusField)
	{
		_sprinklerComponent = sprinklerComponent;
		_radiusField = radiusField;
		UpdateScale();
		UpdateVisibility();
	}

	private void Awake()
	{
		CreateRing();
	}

	private void CreateRing()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Expected O, but got Unknown
		//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		_ring = GameObject.CreatePrimitive((PrimitiveType)2);
		((Object)_ring).name = "SprinklerRangeIndicator";
		_ring.transform.SetParent(((Component)this).transform, false);
		_ring.transform.localPosition = new Vector3(0f, 0.12f, 0f);
		_ring.transform.localRotation = Quaternion.identity;
		Collider component = _ring.GetComponent<Collider>();
		if ((Object)(object)component != (Object)null)
		{
			Object.Destroy((Object)(object)component);
		}
		_renderer = _ring.GetComponent<Renderer>();
		if ((Object)(object)_renderer != (Object)null)
		{
			try
			{
				Material val = null;
				Renderer val2 = ((IEnumerable<Renderer>)Object.FindObjectsOfType<Renderer>()).FirstOrDefault((Func<Renderer, bool>)((Renderer r) => (Object)(object)r != (Object)(object)_renderer && (Object)(object)r.sharedMaterial != (Object)null));
				if ((Object)(object)val2 != (Object)null)
				{
					val = val2.sharedMaterial;
				}
				else if ((Object)(object)_renderer.sharedMaterial != (Object)null)
				{
					val = _renderer.sharedMaterial;
				}
				if ((Object)(object)val != (Object)null)
				{
					Material val3 = new Material(val);
					if (val3.HasProperty("_MainTex"))
					{
						val3.SetTexture("_MainTex", (Texture)(object)Texture2D.whiteTexture);
					}
					if (val3.HasProperty("_BaseMap"))
					{
						val3.SetTexture("_BaseMap", (Texture)(object)Texture2D.whiteTexture);
					}
					Color val4 = default(Color);
					((Color)(ref val4))..ctor(0f, 0.6f, 1f, 0.25f);
					if (val3.HasProperty("_BaseColor"))
					{
						val3.SetColor("_BaseColor", val4);
					}
					else if (val3.HasProperty("_Color"))
					{
						val3.SetColor("_Color", val4);
					}
					if (val3.HasProperty("_SrcBlend"))
					{
						val3.SetInt("_SrcBlend", 5);
					}
					if (val3.HasProperty("_DstBlend"))
					{
						val3.SetInt("_DstBlend", 10);
					}
					if (val3.HasProperty("_ZWrite"))
					{
						val3.SetInt("_ZWrite", 0);
					}
					if (val3.HasProperty("_Surface"))
					{
						val3.SetFloat("_Surface", 1f);
					}
					val3.DisableKeyword("_ALPHATEST_ON");
					val3.EnableKeyword("_ALPHABLEND_ON");
					val3.DisableKeyword("_ALPHAPREMULTIPLY_ON");
					val3.renderQueue = 3200;
					_renderer.material = val3;
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log = SprinklerExtensionPlugin.Log;
				if (log != null)
				{
					log.LogError((object)("[SprinklerExtension] Failed to setup material: " + ex));
				}
			}
		}
		UpdateScale();
		UpdateVisibility();
	}

	private void Update()
	{
		UpdateScale();
		UpdateVisibility();
	}

	private float GetRadiusValue()
	{
		if ((Object)(object)_sprinklerComponent != (Object)null && _radiusField != null)
		{
			try
			{
				object value = _radiusField.GetValue(_sprinklerComponent);
				if (value is float)
				{
					float num = (float)value;
					if (num > 0f)
					{
						return num;
					}
				}
			}
			catch
			{
			}
		}
		return Mathf.Max(0.1f, SprinklerExtensionPlugin.ConfigRadius.Value);
	}

	private void UpdateScale()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_ring == (Object)null))
		{
			float radiusValue = GetRadiusValue();
			_ring.transform.localScale = new Vector3(radiusValue * 2f, 0.02f, radiusValue * 2f);
		}
	}

	private void UpdateVisibility()
	{
		if (!((Object)(object)_ring == (Object)null))
		{
			_ring.SetActive(SprinklerExtensionPlugin.RangeVisible);
		}
	}

	private void OnDestroy()
	{
		if ((Object)(object)_ring != (Object)null)
		{
			Object.Destroy((Object)(object)_ring);
		}
	}
}