Decompiled source of TmbolTextureReplacer v1.3.2

BepInEx/plugins/custom-AISlopTextureReplacer/AISlopTextureReplacer.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AISlopTextureReplacer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AISlopTextureReplacer")]
[assembly: AssemblyTitle("AISlopTextureReplacer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Plguin
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "AISlopTextureReplacer";

		public const string PLUGIN_NAME = "AISlopTextureReplacer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace TextureReplacer
{
	[BepInPlugin("com.tmbolslop.texturereplacer", "TMBOL AI Slop Texture Replacer", "1.3.0")]
	public class Plugin : BaseUnityPlugin
	{
		private static ManualLogSource Log;

		private static readonly Dictionary<string, Texture2D> ReplacementTextures = new Dictionary<string, Texture2D>(StringComparer.OrdinalIgnoreCase);

		private static readonly HashSet<int> InspectedIds = new HashSet<int>();

		private void Awake()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Expected O, but got Unknown
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			LoadTexturesFromDisk();
			Harmony val = new Harmony("com.tmbolslop.texturereplacer");
			val.Patch((MethodBase)AccessTools.Method(typeof(GameObject), "SetActive", new Type[1] { typeof(bool) }, (Type[])null), new HarmonyMethod(typeof(Plugin), "OnSetActive_Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(RawImage), "OnEnable", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "RawImage_OnEnable", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(Image), "OnEnable", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "Image_OnEnable", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"Runtime Texture Replacer initialized.");
		}

		private void LoadTexturesFromDisk()
		{
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Expected O, but got Unknown
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			string text = Path.Combine(directoryName, "Textures");
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
				Log.LogInfo((object)("Created folder: " + text));
				return;
			}
			Log.LogInfo((object)("Loading textures from: " + text));
			IEnumerable<string> enumerable = from f in Directory.EnumerateFiles(text, "*.*", SearchOption.AllDirectories)
				where f.EndsWith(".png", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
				select f;
			foreach (string item in enumerable)
			{
				string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item);
				try
				{
					byte[] array = File.ReadAllBytes(item);
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					if (ImageConversion.LoadImage(val, array))
					{
						ReplacementTextures[fileNameWithoutExtension] = val;
						Log.LogInfo((object)("Loaded texture: '" + fileNameWithoutExtension + "'"));
					}
				}
				catch (Exception ex)
				{
					Log.LogError((object)("Error loading '" + fileNameWithoutExtension + "': " + ex.Message));
				}
			}
		}

		private static void OnSetActive_Prefix(GameObject __instance, bool value)
		{
			//IL_031a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0329: Unknown result type (might be due to invalid IL or missing references)
			if (!value || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			int instanceID = ((Object)__instance).GetInstanceID();
			if (!InspectedIds.Add(instanceID))
			{
				return;
			}
			try
			{
				Renderer[] componentsInChildren = __instance.GetComponentsInChildren<Renderer>(true);
				foreach (Renderer val in componentsInChildren)
				{
					Material[] materials = val.materials;
					foreach (Material val2 in materials)
					{
						if ((Object)(object)val2 == (Object)null)
						{
							continue;
						}
						string[] texturePropertyNames = val2.GetTexturePropertyNames();
						foreach (string text in texturePropertyNames)
						{
							Texture texture = val2.GetTexture(text);
							Texture2D val3 = (Texture2D)(object)((texture is Texture2D) ? texture : null);
							if (val3 != null)
							{
								Texture2D value3;
								if (ReplacementTextures.TryGetValue(((Object)val3).name, out var value2))
								{
									val2.SetTexture(text, (Texture)(object)value2);
									Log.LogWarning((object)("[3D] '" + ((Object)val3).name + "'→'" + text + "' on '" + ((Object)val).name + "'"));
								}
								else if (ReplacementTextures.TryGetValue(text, out value3))
								{
									val2.SetTexture(text, (Texture)(object)value3);
									Log.LogWarning((object)("[3D] replaced by prop '" + text + "' on '" + ((Object)val).name + "'"));
								}
							}
						}
					}
				}
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"3D replace error: {arg}");
			}
			try
			{
				RawImage[] componentsInChildren2 = __instance.GetComponentsInChildren<RawImage>(true);
				foreach (RawImage val4 in componentsInChildren2)
				{
					Texture texture2 = val4.texture;
					Texture2D val5 = (Texture2D)(object)((texture2 is Texture2D) ? texture2 : null);
					if (val5 != null && ReplacementTextures.TryGetValue(((Object)val5).name, out var value4))
					{
						val4.texture = (Texture)(object)value4;
						Log.LogWarning((object)("[UI RawImage] '" + ((Object)val5).name + "' swapped on '" + ((Object)val4).name + "'"));
					}
				}
			}
			catch (Exception arg2)
			{
				Log.LogError((object)$"RawImage replace error: {arg2}");
			}
			try
			{
				Image[] componentsInChildren3 = __instance.GetComponentsInChildren<Image>(true);
				foreach (Image val6 in componentsInChildren3)
				{
					Sprite sprite = val6.sprite;
					if ((Object)(object)sprite != (Object)null && ReplacementTextures.TryGetValue(((Object)sprite).name, out var value5))
					{
						Sprite sprite2 = Sprite.Create(value5, new Rect(0f, 0f, (float)((Texture)value5).width, (float)((Texture)value5).height), new Vector2(0.5f, 0.5f), sprite.pixelsPerUnit);
						val6.sprite = sprite2;
						Log.LogWarning((object)("[UI Image] '" + ((Object)sprite).name + "' swapped on '" + ((Object)val6).name + "'"));
					}
				}
			}
			catch (Exception arg3)
			{
				Log.LogError((object)$"Image replace error: {arg3}");
			}
		}

		public static void RawImage_OnEnable(RawImage __instance)
		{
			Texture texture = __instance.texture;
			Texture2D val = (Texture2D)(object)((texture is Texture2D) ? texture : null);
			if (val != null && ReplacementTextures.TryGetValue(((Object)val).name, out var value))
			{
				__instance.texture = (Texture)(object)value;
				Log.LogWarning((object)("[UI RawImage] OnEnable swapped '" + ((Object)val).name + "'"));
			}
		}

		public static void Image_OnEnable(Image __instance)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			Sprite sprite = __instance.sprite;
			if ((Object)(object)sprite != (Object)null && ReplacementTextures.TryGetValue(((Object)sprite).name, out var value))
			{
				Sprite sprite2 = Sprite.Create(value, new Rect(0f, 0f, (float)((Texture)value).width, (float)((Texture)value).height), new Vector2(0.5f, 0.5f), sprite.pixelsPerUnit);
				__instance.sprite = sprite2;
				Log.LogWarning((object)("[UI Image] OnEnable swapped '" + ((Object)sprite).name + "'"));
			}
		}
	}
}