Decompiled source of Tonyify v1.0.0

BepInEx/plugins/PeakKilnLavaTexture/PeakKilnLavaTexture.dll

Decompiled 2 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PeakKilnLavaTexture;

[BepInPlugin("easton.peak.kilnlavatexture", "Peak Kiln Lava Texture", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	private const string TextureFileName = "Lava_Base_Color.png";

	private readonly HashSet<int> replacedMaterials = new HashSet<int>();

	private Texture2D replacementTexture;

	private float nextScanTime;

	private void Awake()
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Expected O, but got Unknown
		string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Lava_Base_Color.png");
		if (!File.Exists(path))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Missing Lava_Base_Color.png next to the plugin DLL.");
			((Behaviour)this).enabled = false;
			return;
		}
		replacementTexture = new Texture2D(2, 2, (TextureFormat)4, true);
		if (!ImageConversion.LoadImage(replacementTexture, File.ReadAllBytes(path)))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Could not load Lava_Base_Color.png.");
			((Behaviour)this).enabled = false;
			return;
		}
		((Object)replacementTexture).name = "PeakKilnLavaReplacement";
		((Texture)replacementTexture).wrapMode = (TextureWrapMode)0;
		((Texture)replacementTexture).filterMode = (FilterMode)1;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Kiln lava texture replacement is ready.");
	}

	private void Update()
	{
		if (!(Time.unscaledTime < nextScanTime))
		{
			nextScanTime = Time.unscaledTime + 1f;
			ApplyToLoadedLava();
		}
	}

	private void ApplyToLoadedLava()
	{
		Renderer[] array = Resources.FindObjectsOfTypeAll<Renderer>();
		foreach (Renderer val in array)
		{
			if ((Object)(object)val == (Object)null || !IsKilnLava(val))
			{
				continue;
			}
			Material[] sharedMaterials = val.sharedMaterials;
			foreach (Material val2 in sharedMaterials)
			{
				if (!((Object)(object)val2 == (Object)null) && !replacedMaterials.Contains(((Object)val2).GetInstanceID()) && SetLavaTexture(val2))
				{
					replacedMaterials.Add(((Object)val2).GetInstanceID());
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Replaced lava material: " + ((Object)val2).name));
				}
			}
		}
	}

	private static bool IsKilnLava(Renderer renderer)
	{
		Transform val = ((Component)renderer).transform;
		while ((Object)(object)val != (Object)null)
		{
			string text = ((Object)val).name.ToLowerInvariant();
			if (text.Contains("lava") || text.Contains("kiln"))
			{
				return true;
			}
			Component[] components = ((Component)val).GetComponents<Component>();
			foreach (Component val2 in components)
			{
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				string name = ((object)val2).GetType().Name;
				switch (name)
				{
				default:
					if (!(name == "LavaTides"))
					{
						continue;
					}
					break;
				case "Lava":
				case "LavaRiver":
				case "MovingLava":
					break;
				}
				return true;
			}
			val = val.parent;
		}
		Material[] sharedMaterials = renderer.sharedMaterials;
		for (int j = 0; j < sharedMaterials.Length; j++)
		{
			if ((Object)(object)sharedMaterials[j] != (Object)null && ((Object)sharedMaterials[j]).name.ToLowerInvariant().Contains("lava"))
			{
				return true;
			}
		}
		return false;
	}

	private bool SetLavaTexture(Material material)
	{
		bool result = false;
		string[] array = new string[4] { "_BaseMap", "_MainTex", "_BaseColorMap", "_Albedo" };
		for (int i = 0; i < array.Length; i++)
		{
			if (material.HasProperty(array[i]))
			{
				material.SetTexture(array[i], (Texture)(object)replacementTexture);
				result = true;
			}
		}
		return result;
	}
}