Decompiled source of TextureReplacer v1.0.2

EverythingCustomTexture.dll

Decompiled 3 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using PluginConfig.API;
using PluginConfig.API.Fields;
using PluginConfig.API.Functionals;
using UnityEngine;
using UnityEngine.SceneManagement;
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: AssemblyTitle("EverythingCustomTexture")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EverythingCustomTexture")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("13359917-e66d-46ee-9924-2c2ece2c5f33")]
[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 EverythingCustomTexture;

[BepInPlugin("everythingcustomtexture.banana.mod", "EverythingCustomTexture", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private enum PNGSelectionType
	{
		FirstPNG,
		RandomPNG,
		SelectedPNG
	}

	private StringListField pngsList;

	public List<string> pngs = new List<string>();

	private BoolField Modenabled;

	private string pluginFolder;

	private EnumField<PNGSelectionType> PNGSelection;

	private List<Texture2D> textures = new List<Texture2D>();

	private List<Sprite> sprites = new List<Sprite>();

	private void Awake()
	{
		pluginFolder = Assembly.GetExecutingAssembly().Location.Replace("EverythingCustomTexture.dll", "");
		Debug.Log((object)pluginFolder);
		string text = pluginFolder;
		LoadAllTextures(pluginFolder);
		MakeAllSprites();
		SceneManager.sceneLoaded += SceneManager_sceneLoaded;
	}

	private bool IsFontMaterial(Material material)
	{
		return ((Object)material.shader).name.ToLower().Contains("text");
	}

	public void AddFields()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Expected O, but got Unknown
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Expected O, but got Unknown
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Expected O, but got Unknown
		PluginConfigurator val = PluginConfigurator.Create("Texture Replacer", "ect.main");
		Modenabled = new BoolField(val.rootPanel, "enabled", "customtextures.enabled", true, true);
		pngsList = new StringListField(val.rootPanel, "pngs list", "pngs.list", pngs, pngs[0], false);
		PNGSelection = new EnumField<PNGSelectionType>(val.rootPanel, "PNGSelectionType", "png.selection", PNGSelectionType.FirstPNG);
		ButtonField val2 = new ButtonField(val.rootPanel, "Open Folder", "button.folderopener");
		val2.onClick += new OnClick(OpenFolder_onClick);
	}

	private void OpenFolder_onClick()
	{
		if (Directory.Exists(pluginFolder))
		{
			ProcessStartInfo processStartInfo = new ProcessStartInfo();
			processStartInfo.Arguments = Path.Combine(pluginFolder);
			processStartInfo.FileName = "explorer.exe";
			ProcessStartInfo startInfo = processStartInfo;
			Process.Start(startInfo);
		}
	}

	private void ChangeImage(Image image)
	{
		if ((Object)(object)image == (Object)null)
		{
			Debug.LogError((object)"Image is null");
			return;
		}
		Sprite val = null;
		switch (PNGSelection.value)
		{
		case PNGSelectionType.FirstPNG:
			val = sprites[0];
			break;
		case PNGSelectionType.RandomPNG:
			val = sprites[Random.Range(0, sprites.Count)];
			break;
		case PNGSelectionType.SelectedPNG:
			val = sprites[pngsList.valueIndex];
			break;
		}
		if ((Object)(object)val == (Object)null)
		{
			Debug.LogError((object)"Sprite is null");
			return;
		}
		Transform parent = ((Component)image).transform.parent;
		if (!((Object)(object)((parent != null) ? parent.parent : null) != (Object)null) || !((Object)((Component)((Component)image).transform.parent.parent).gameObject).name.ToLower().Contains("screen"))
		{
			image.sprite = val;
		}
	}

	private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
	{
		if (Modenabled.value)
		{
			ChangeAllMats();
			ChangeAllImages();
		}
	}

	private void ChangeAllMats()
	{
		Material[] array = Resources.FindObjectsOfTypeAll<Material>();
		Material[] array2 = array;
		foreach (Material material in array2)
		{
			if (!IsFontMaterial(material))
			{
				ChangeTexture(material);
			}
		}
	}

	private void ChangeAllImages()
	{
		Image[] array = Resources.FindObjectsOfTypeAll<Image>();
		Image[] array2 = array;
		foreach (Image image in array2)
		{
			ChangeImage(image);
		}
	}

	public void ChangeTexture(Material material)
	{
		string directoryPath = pluginFolder;
		Texture2D val = null;
		switch (PNGSelection.value)
		{
		case PNGSelectionType.FirstPNG:
			val = LoadFirstTextureFromDirectory(directoryPath);
			break;
		case PNGSelectionType.RandomPNG:
			val = LoadRandomTextureFromDirectory(directoryPath);
			break;
		case PNGSelectionType.SelectedPNG:
			val = LoadSelectedPNG(directoryPath);
			break;
		}
		if (!((Object)(object)val != (Object)null))
		{
			return;
		}
		string[] texturePropertyNames = material.GetTexturePropertyNames();
		foreach (string text in texturePropertyNames)
		{
			Texture texture = material.GetTexture(text);
			if ((Object)(object)texture != (Object)null && ((object)texture).GetType() == typeof(Texture2D))
			{
				material.SetTexture(text, (Texture)(object)val);
			}
		}
	}

	private void LoadAllTextures(string directoryPath)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		if (!Directory.Exists(directoryPath))
		{
			return;
		}
		string[] array = (from file in Directory.GetFiles(directoryPath, "*.*")
			where file.ToLower().EndsWith("jpg") || file.ToLower().EndsWith("jpeg") || file.ToLower().EndsWith("bmf") || file.ToLower().EndsWith("psd") || file.ToLower().EndsWith("png")
			select file).ToArray();
		if (array.Length == 0)
		{
			return;
		}
		string[] array2 = array;
		foreach (string path in array2)
		{
			byte[] array3 = File.ReadAllBytes(path);
			Texture2D val = new Texture2D(2, 2);
			ImageConversion.LoadImage(val, array3);
			((Object)val).name = Path.GetFileNameWithoutExtension(path);
			textures.Add(val);
		}
		foreach (Texture2D texture in textures)
		{
			pngs.Add(((Object)texture).name);
		}
		Debug.Log((object)pngs.Count);
		AddFields();
	}

	private void MakeAllSprites()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		Rect val = default(Rect);
		Vector2 val2 = default(Vector2);
		foreach (Texture2D texture in textures)
		{
			((Rect)(ref val))..ctor(0f, 0f, (float)((Texture)texture).width, (float)((Texture)texture).height);
			((Vector2)(ref val2))..ctor(0.5f, 0.5f);
			sprites.Add(Sprite.Create(texture, val, val2));
		}
	}

	private Texture2D LoadRandomTextureFromDirectory(string directoryPath)
	{
		if (textures.Count > 0)
		{
			return textures[Random.Range(0, textures.Count)];
		}
		return null;
	}

	private Texture2D LoadFirstTextureFromDirectory(string directoryPath)
	{
		if (textures.Count > 0)
		{
			return textures[0];
		}
		return null;
	}

	private Texture2D LoadSelectedPNG(string directoryPath)
	{
		return textures[pngsList.valueIndex];
	}
}