Decompiled source of RetexturePlugin v2.5.1

RetexturePlugin.dll

Decompiled 5 months 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 SRF;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Video;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RetexturePlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RetexturePlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("RetexturePlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("2.5.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("2.5.1.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.retexture", "Retexture Plug-In", "2.5.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class RetexturePlugin : BaseUnityPlugin
{
	public const string Name = "Retexture Plug-In";

	public const string Guid = "org.lordashes.plugins.retexture";

	public const string Version = "2.5.1.0";

	public const string Author = "Lord Ashes";

	private Dictionary<CreatureGuid, Texture> originalMaterials = new Dictionary<CreatureGuid, Texture>();

	private Guid subscriptionId = System.Guid.Empty;

	private ConfigEntry<KeyboardShortcut> triggerKey { get; set; }

	private ConfigEntry<KeyboardShortcut> triggerResetKey { get; set; }

	private void Awake()
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		Debug.Log((object)("Retexture Plug-In " + ((object)this).GetType().AssemblyQualifiedName + " Active."));
		triggerKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Repaint Asset Activation", new KeyboardShortcut((KeyCode)120, (KeyCode[])(object)new KeyCode[1] { (KeyCode)305 }), (ConfigDescription)null);
		triggerResetKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Transform (Recall) Repainted", new KeyboardShortcut((KeyCode)121, (KeyCode[])(object)new KeyCode[1] { (KeyCode)305 }), (ConfigDescription)null);
		AssetDataPlugin.Subscribe("org.lordashes.plugins.retexture", (Action<DatumChange>)RequestHandler, (Func<DatumChange, bool>)Checker.CheckSourceAsCreature);
		Utility.Initialize(((object)this).GetType());
	}

	private void Update()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		KeyboardShortcut value = triggerKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			Debug.Log((object)"Retexture Plug-In: Make Retexture Request For Selected Mini");
			CreatureBoardAsset asset = default(CreatureBoardAsset);
			CreaturePresenter.TryGetAsset(LocalClient.SelectedCreatureId, ref asset);
			if ((Object)(object)asset != (Object)null)
			{
				SystemMessage.AskForTextInput("Replacement Texture...", "\r\nSource:", "Texture", (Action<string>)delegate(string source)
				{
					//IL_0007: Unknown result type (might be due to invalid IL or missing references)
					//IL_000c: Unknown result type (might be due to invalid IL or missing references)
					CreatureGuid creatureId2 = asset.CreatureId;
					AssetDataPlugin.SetInfo(((object)(CreatureGuid)(ref creatureId2)).ToString(), "org.lordashes.plugins.retexture", source, false);
				}, (Action)null, "Original", (Action)delegate
				{
					//IL_0007: Unknown result type (might be due to invalid IL or missing references)
					//IL_000c: Unknown result type (might be due to invalid IL or missing references)
					CreatureGuid creatureId = asset.CreatureId;
					AssetDataPlugin.ClearInfo(((object)(CreatureGuid)(ref creatureId)).ToString(), "org.lordashes.plugins.retexture", false);
				}, "");
			}
			else
			{
				SystemMessage.DisplayInfoText("Retexture Plugin Requires A Selected Mini", 2.5f, 0f);
			}
		}
		value = triggerResetKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			Debug.Log((object)"Retexture Plug-In: Reset Retexture Subscription");
			if (subscriptionId != System.Guid.Empty)
			{
				AssetDataPlugin.Unsubscribe(subscriptionId);
				AssetDataPlugin.Subscribe("org.lordashes.plugins.retexture", (Action<DatumChange>)RequestHandler, (Func<DatumChange, bool>)Checker.CheckSourceAsCreature);
			}
		}
	}

	private void RequestHandler(DatumChange change)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Invalid comparison between Unknown and I4
		Debug.Log((object)("Retexture Plug-In Request To Retexture " + change.source + " To " + change.value));
		CreatureBoardAsset val = null;
		CreaturePresenter.TryGetAsset(new CreatureGuid(change.source), ref val);
		CreatureGuid creatureId = val.CreatureId;
		GameObject val2 = GameObject.Find("Effect:" + ((object)(CreatureGuid)(ref creatureId)).ToString());
		Material val3 = null;
		if ((Object)(object)val2 != (Object)null)
		{
			Debug.Log((object)"Retexture Plug-In Retexturing Effect");
			creatureId = val.CreatureId;
			val3 = FindMaterial(GameObject.Find("Effect:" + ((object)(CreatureGuid)(ref creatureId)).ToString()), "RETEXTURE_MAT");
		}
		else
		{
			Debug.Log((object)"Retexture Plug-In Retexturing Mini");
			val3 = FindMaterial(Utility.GetAssetLoader(val.CreatureId), "RETEXTURE_MAT");
		}
		if ((int)change.action != 1)
		{
			ApplyTexture(val, val3, change.value.ToString());
		}
		else
		{
			RestoreTexture(val, val3);
		}
	}

	private void ApplyTexture(CreatureBoardAsset asset, Material mat, string source)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		if (!originalMaterials.ContainsKey(asset.CreatureId))
		{
			Debug.Log((object)("Retexture Plug-In Storing orignal " + asset.Name + " material"));
			originalMaterials.Add(asset.CreatureId, mat.mainTexture);
		}
		CreatureGuid creatureId = asset.CreatureId;
		GameObject val = GameObject.Find("Effect:" + ((object)(CreatureGuid)(ref creatureId)).ToString());
		if ((Object)(object)val == (Object)null)
		{
			val = Utility.GetAssetLoader(asset.CreatureId);
		}
		if (source.EndsWith(".mp4") || source.EndsWith(".mov;") || source.EndsWith(".webm;") || source.EndsWith(".wmv;"))
		{
			ApplyVideo(val, "RETEXTURE_MAT", source);
			return;
		}
		SRFGameObjectExtensions.RemoveComponentIfExists<VideoPlayer>(val);
		IEnumerable<GameObject> enumerable = from v in val.GetComponentsInChildren<VideoPlayer>()
			select ((Component)v).gameObject;
		foreach (GameObject item in enumerable)
		{
			SRFGameObjectExtensions.RemoveComponentIfExists<VideoPlayer>(item);
		}
		Debug.Log((object)("Retexture Plug-In Retexturing " + asset.Name + " main texture (" + ((Object)mat).name + ":" + ((Object)mat.mainTexture).name + ") with  " + source));
		mat.mainTexture = (Texture)(object)Image.LoadTexture(source, (CacheType)999);
	}

	private void RestoreTexture(CreatureBoardAsset asset, Material mat)
	{
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		GameObject gameObject = ((Component)asset).gameObject;
		SRFGameObjectExtensions.RemoveComponentIfExists<VideoPlayer>(gameObject);
		IEnumerable<GameObject> enumerable = from v in gameObject.GetComponentsInChildren<VideoPlayer>()
			select ((Component)v).gameObject;
		foreach (GameObject item in enumerable)
		{
			SRFGameObjectExtensions.RemoveComponentIfExists<VideoPlayer>(item);
		}
		Debug.Log((object)("Retexture Plug-In Restoring " + asset.Name + " original material"));
		mat.mainTexture = originalMaterials[asset.CreatureId];
		Debug.Log((object)("Retexture Plug-In Removing stored material for " + asset.Name));
		originalMaterials.Remove(asset.CreatureId);
	}

	private Material FindMaterial(GameObject asset, string name, bool useDefault = true)
	{
		List<Renderer> list = new List<Renderer>();
		MeshRenderer[] componentsInChildren = asset.GetComponentsInChildren<MeshRenderer>();
		foreach (MeshRenderer val in componentsInChildren)
		{
			Debug.Log((object)("Retexture Plug-In Adding MeshRenderer " + ((Object)val).name));
			list.Add((Renderer)(object)val);
		}
		SkinnedMeshRenderer[] componentsInChildren2 = asset.GetComponentsInChildren<SkinnedMeshRenderer>();
		foreach (SkinnedMeshRenderer val2 in componentsInChildren2)
		{
			Debug.Log((object)("Retexture Plug-In Adding SkinnedMeshRenderer " + ((Object)val2).name));
			list.Add((Renderer)(object)val2);
		}
		foreach (Renderer item in list)
		{
			Material[] materials = item.materials;
			foreach (Material val3 in materials)
			{
				Debug.Log((object)("Retexture Plug-In Looking At Material " + ((Object)item).name + "." + ((Object)val3).name));
				if (((Object)val3).name.Contains(name))
				{
					Debug.Log((object)("Retexture Plug-In Found " + ((Object)item).name + "." + ((Object)val3).name));
					return val3;
				}
			}
		}
		Debug.Log((object)"Retexture Plug-In Using Default/Null");
		return useDefault ? list[0].material : null;
	}

	private void ApplyVideo(GameObject asset, string name, string source)
	{
		List<Renderer> list = new List<Renderer>();
		MeshRenderer[] componentsInChildren = asset.GetComponentsInChildren<MeshRenderer>();
		foreach (MeshRenderer val in componentsInChildren)
		{
			Debug.Log((object)("Retexture Plug-In Adding MeshRenderer " + ((Object)val).name));
			list.Add((Renderer)(object)val);
		}
		SkinnedMeshRenderer[] componentsInChildren2 = asset.GetComponentsInChildren<SkinnedMeshRenderer>();
		foreach (SkinnedMeshRenderer val2 in componentsInChildren2)
		{
			Debug.Log((object)("Retexture Plug-In Adding SkinnedMeshRenderer " + ((Object)val2).name));
			list.Add((Renderer)(object)val2);
		}
		VideoPlayer val4 = default(VideoPlayer);
		foreach (Renderer item in list)
		{
			Material[] materials = item.materials;
			foreach (Material val3 in materials)
			{
				Debug.Log((object)("Retexture Plug-In Looking At Material " + ((Object)item).name + "." + ((Object)val3).name));
				if (((Object)val3).name.Contains(name))
				{
					Debug.Log((object)("Retexture Plug-In Found " + ((Object)item).name + "." + ((Object)val3).name));
					GameObject gameObject = ((Component)item).gameObject;
					if (!gameObject.TryGetComponent<VideoPlayer>(ref val4))
					{
						val4 = gameObject.AddComponent<VideoPlayer>();
					}
					val4.playOnAwake = true;
					val4.source = (VideoSource)1;
					if (!source.StartsWith("http"))
					{
						source = "file://" + source;
					}
					val4.url = source;
					val4.isLooping = true;
					return;
				}
			}
		}
	}
}
public static class Utility
{
	public static void Initialize(MemberInfo plugin)
	{
		SceneManager.sceneLoaded += delegate(Scene scene, LoadSceneMode mode)
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			try
			{
				if (((Scene)(ref scene)).name == "UI")
				{
					TextMeshProUGUI uITextByName = GetUITextByName("BETA");
					if (Object.op_Implicit((Object)(object)uITextByName))
					{
						((TMP_Text)uITextByName).text = "INJECTED BUILD - unstable mods";
					}
				}
				else
				{
					TextMeshProUGUI uITextByName2 = GetUITextByName("TextMeshPro Text");
					if (Object.op_Implicit((Object)(object)uITextByName2))
					{
						BepInPlugin val = (BepInPlugin)Attribute.GetCustomAttribute(plugin, typeof(BepInPlugin));
						if (((TMP_Text)uITextByName2).text.EndsWith("</size>"))
						{
							((TMP_Text)uITextByName2).text = ((TMP_Text)uITextByName2).text + "\n\nMods Currently Installed:\n";
						}
						TextMeshProUGUI val2 = uITextByName2;
						((TMP_Text)val2).text = ((TMP_Text)val2).text + "\nLord Ashes' " + val.Name + " - " + val.Version;
					}
				}
			}
			catch (Exception ex)
			{
				Debug.Log((object)ex);
			}
		};
	}

	public static TextMeshProUGUI GetUITextByName(string name)
	{
		TextMeshProUGUI[] array = Object.FindObjectsOfType<TextMeshProUGUI>();
		for (int i = 0; i < array.Length; i++)
		{
			if (((Object)array[i]).name == name)
			{
				return array[i];
			}
		}
		return null;
	}

	public static GameObject GetAssetLoader(CreatureGuid cid)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		CreatureBoardAsset val = null;
		CreaturePresenter.TryGetAsset(cid, ref val);
		if ((Object)(object)val != (Object)null)
		{
			Transform match = null;
			StartWith(val, "_creatureRoot", ref match);
			Transform match2 = null;
			Traverse(match, "AssetLoader", ref match2);
			if ((Object)(object)match2 != (Object)null)
			{
				Debug.Log((object)("Custom Assets Library Plugin Integrated Extension:  Asset Loader '" + ((Object)match2.GetChild(0)).name + "' Found"));
				return ((Component)match2.GetChild(0)).gameObject;
			}
			Debug.LogWarning((object)"Custom Assets Library Plugin Integrated Extension: Could Not Find Asset Loader");
			return null;
		}
		return null;
	}

	public static void StartWith(CreatureBoardAsset asset, string seek, ref Transform match)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Expected O, but got Unknown
		Type typeFromHandle = typeof(CreatureBoardAsset);
		match = null;
		foreach (FieldInfo runtimeField in typeFromHandle.GetRuntimeFields())
		{
			if (runtimeField.Name == seek)
			{
				match = (Transform)runtimeField.GetValue(asset);
				break;
			}
		}
	}

	public static void Traverse(Transform root, string seek, ref Transform match)
	{
		if ((Object)(object)match != (Object)null)
		{
			return;
		}
		if (((Object)root).name == seek)
		{
			match = root;
			return;
		}
		foreach (Transform item in ExtensionMethods.Children(root))
		{
			Traverse(item, seek, ref match);
		}
	}
}