Decompiled source of TextureSwap v1.1.0

BepInEx/plugins/InFalsusTextureSwap/InFalsusTextureSwap.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 InFalsusTextureSwap
{
	[BepInPlugin("com.infalsus.textureswap", "InFalsusTextureSwap", "1.1.0")]
	public class TextureSwapPlugin : BasePlugin
	{
		private const string ConfigName = "InFalsusTextureSwap.txt";

		internal static ManualLogSource LogSource;

		internal readonly Dictionary<string, Replacement> Wanted = new Dictionary<string, Replacement>();

		public override void Load()
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Expected O, but got Unknown
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Expected O, but got Unknown
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Expected O, but got Unknown
			//IL_026d: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Expected O, but got Unknown
			//IL_02d7: Unknown result type (might be due to invalid IL or missing references)
			LogSource = ((BasePlugin)this).Log;
			string path = Path.GetDirectoryName(typeof(TextureSwapPlugin).Assembly.Location) ?? Paths.PluginPath;
			string text = Path.Combine(path, "InFalsusTextureSwap.txt");
			if (!File.Exists(text))
			{
				string text2 = Path.Combine(Paths.GameRootPath, "Mods", "InFalsusTextureSwap.txt");
				if (File.Exists(text2))
				{
					text = text2;
				}
			}
			bool flag = default(bool);
			if (!File.Exists(text))
			{
				ManualLogSource logSource = LogSource;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(13, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("没有配置文件 ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(",什么也不换");
				}
				logSource.LogWarning(val);
				return;
			}
			string[] array = File.ReadAllLines(text);
			for (int i = 0; i < array.Length; i++)
			{
				string text3 = array[i].Trim();
				if (text3.Length == 0 || text3[0] == '#')
				{
					continue;
				}
				string[] array2 = text3.Split(new char[1] { '=' }, 2);
				if (array2.Length != 2)
				{
					ManualLogSource logSource2 = LogSource;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(11, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("看不懂这一行,跳过: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text3);
					}
					logSource2.LogWarning(val);
					continue;
				}
				string text4 = array2[0].Trim();
				string text5 = array2[1].Trim();
				BepInExInfoLogInterpolatedStringHandler val2;
				if (text5 == "#blank")
				{
					Wanted[text4] = new Replacement
					{
						Data = null,
						Format = (TextureFormat)4
					};
					ManualLogSource logSource3 = LogSource;
					val2 = new BepInExInfoLogInterpolatedStringHandler(23, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text4);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("  <-  #blank(置空,同尺寸全透明)");
					}
					logSource3.LogInfo(val2);
					continue;
				}
				string text6 = (Path.IsPathRooted(text5) ? text5 : (File.Exists(Path.Combine(path, text5)) ? Path.Combine(path, text5) : (File.Exists(Path.Combine(Paths.GameRootPath, "Mods", text5)) ? Path.Combine(Paths.GameRootPath, "Mods", text5) : Path.Combine(Paths.GameRootPath, text5))));
				if (!File.Exists(text6))
				{
					ManualLogSource logSource4 = LogSource;
					BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(6, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(text4);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": 找不到 ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(text6);
					}
					logSource4.LogError(val3);
					continue;
				}
				TextureFormat val4 = (TextureFormat)(text5.EndsWith(".dxt5", StringComparison.OrdinalIgnoreCase) ? 12 : (text5.EndsWith(".bc7", StringComparison.OrdinalIgnoreCase) ? 25 : 4));
				Wanted[text4] = new Replacement
				{
					Data = File.ReadAllBytes(text6),
					Format = val4
				};
				ManualLogSource logSource5 = LogSource;
				val2 = new BepInExInfoLogInterpolatedStringHandler(12, 4, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text4);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("  <-  ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text6);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("  ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<TextureFormat>(val4);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(Wanted[text4].Data.Length);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" 字节");
				}
				logSource5.LogInfo(val2);
				BlankBindGlow(text4);
			}
			if (Wanted.Count == 0)
			{
				LogSource.LogWarning((object)"配置里没有可换的贴图");
			}
			((BasePlugin)this).AddComponent<TextureSwapBehaviour>().Wanted = Wanted;
		}

		private void BlankBindGlow(string illustName)
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			string text = null;
			string[] array = new string[2] { "_UIcharacterillust", "_UI_characterillust" };
			foreach (string text2 in array)
			{
				if (illustName.EndsWith(text2, StringComparison.OrdinalIgnoreCase))
				{
					text = illustName.Substring(0, illustName.Length - text2.Length);
					break;
				}
			}
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			string text3 = text.ToLowerInvariant() + "_bind_glow";
			if (!Wanted.ContainsKey(text3))
			{
				Wanted[text3] = new Replacement
				{
					Data = null,
					Format = (TextureFormat)4
				};
				ManualLogSource logSource = LogSource;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(21, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text3);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("  <-  #blank(随 ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(illustName);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" 自动置空)");
				}
				logSource.LogInfo(val);
			}
		}
	}
	internal sealed class Replacement
	{
		public byte[] Data;

		public TextureFormat Format;
	}
	public class TextureSwapBehaviour : MonoBehaviour
	{
		private const float Interval = 1f;

		internal Dictionary<string, Replacement> Wanted;

		private readonly HashSet<IntPtr> _done = new HashSet<IntPtr>();

		private readonly List<Texture2D> _pending = new List<Texture2D>();

		private float _next;

		private void Update()
		{
			//IL_037c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0383: Expected O, but got Unknown
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Invalid comparison between Unknown and I4
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Invalid comparison between Unknown and I4
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Invalid comparison between Unknown and I4
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Invalid comparison between Unknown and I4
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Expected O, but got Unknown
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Expected O, but got Unknown
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0305: Expected O, but got Unknown
			//IL_033a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0341: Expected O, but got Unknown
			//IL_02b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0360: Unknown result type (might be due to invalid IL or missing references)
			if (Wanted == null || Wanted.Count == 0)
			{
				return;
			}
			if (_pending.Count > 0)
			{
				foreach (Texture2D item in _pending)
				{
					Object.Destroy((Object)(object)item);
				}
				_pending.Clear();
			}
			_next += Time.unscaledDeltaTime;
			if (_next < 1f)
			{
				return;
			}
			_next = 0f;
			bool flag = default(bool);
			foreach (Object item2 in (Il2CppArrayBase<Object>)(object)Resources.FindObjectsOfTypeAll(Il2CppType.Of<Texture2D>()))
			{
				Texture2D val = ((Il2CppObjectBase)item2).TryCast<Texture2D>();
				if ((Object)(object)val == (Object)null || !Wanted.TryGetValue(((Object)val).name, out var value) || !_done.Add(((Il2CppObjectBase)val).Pointer))
				{
					continue;
				}
				int width = ((Texture)val).width;
				int height = ((Texture)val).height;
				TextureFormat val2 = ((value.Data == null) ? val.format : value.Format);
				if ((int)val2 != 12 && (int)val2 != 25 && (int)val2 != 4)
				{
					ManualLogSource logSource = TextureSwapPlugin.LogSource;
					BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(36, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(((Object)val).name);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": 只支持 DXT5 / BC7 / RGBA32 目标,实际是 ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<TextureFormat>(val2);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(",跳过");
					}
					logSource.LogError(val3);
					continue;
				}
				int num = ExpectedLength(val2, width, height);
				byte[] array = value.Data;
				if (array == null)
				{
					if ((int)val2 == 25)
					{
						TextureSwapPlugin.LogSource.LogError((object)(((Object)val).name + ": BC7 目标不能用 #blank(全零块是不透明黑),请用 png_to_bc7.py 压一张全透明图"));
						continue;
					}
					array = new byte[num];
				}
				else if (array.Length != num)
				{
					ManualLogSource logSource2 = TextureSwapPlugin.LogSource;
					BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(22, 6, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(((Object)val).name);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<TextureFormat>(val2);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" 数据 ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<int>(array.Length);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" 字节,");
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("与 ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<int>(width);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("x");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<int>(height);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" 的 ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<int>(num);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" 不符,跳过");
					}
					logSource2.LogError(val3);
					continue;
				}
				try
				{
					if (val2 != val.format)
					{
						TextureSwapPlugin.LogSource.LogError((object)($"{((Object)val).name}: 目标格式是 {val.format},源数据是 {val2}。" + "压缩贴图要先用 png_to_dxt5.py / png_to_bc7.py 转成对应格式,放弃"));
						continue;
					}
					Texture2D val4 = new Texture2D(width, height, val2, false);
					val4.LoadRawTextureData(Il2CppStructArray<byte>.op_Implicit(array));
					val4.Apply(false, false);
					Graphics.CopyTexture((Texture)(object)val4, (Texture)(object)val);
					_pending.Add(val4);
					ManualLogSource logSource3 = TextureSwapPlugin.LogSource;
					BepInExInfoLogInterpolatedStringHandler val5 = new BepInExInfoLogInterpolatedStringHandler(6, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(((Object)val).name);
						((BepInExLogInterpolatedStringHandler)val5).AppendLiteral(": 已写入 ");
						((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<TextureFormat>(val2);
					}
					logSource3.LogInfo(val5);
				}
				catch (Exception ex)
				{
					ManualLogSource logSource4 = TextureSwapPlugin.LogSource;
					BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(9, 3, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(((Object)val).name);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": 替换失败 ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.GetType().Name);
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
					}
					logSource4.LogError(val3);
				}
			}
		}

		private static int ExpectedLength(TextureFormat format, int width, int height)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			if ((int)format != 4)
			{
				return (width + 3) / 4 * ((height + 3) / 4) * 16;
			}
			return width * height * 4;
		}
	}
}