Decompiled source of Outward Vanilla Armors 2K Retex v1.0.0

plugins/Outward Vanilla Armors 2K RETEX/OutwardArmor2kRetex.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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 System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("OutwardArmor2kRetex")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("OutwardArmor2kRetex")]
[assembly: AssemblyTitle("OutwardArmor2kRetex")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 OutwardArmor2kRetex
{
	internal static class Copal
	{
		private class MeshData
		{
			public int nv;

			public uint hash;

			public int[] counts;

			public Dictionary<int, int[][]> moved = new Dictionary<int, int[][]>();
		}

		private static readonly Dictionary<string, Color> specColor = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase)
		{
			{
				"mat_cha_armorCopal",
				new Color(1f, 0.82f, 0.35f, 1f)
			},
			{
				"mat_cha_armorCopalMonsoon",
				new Color(0.45f, 0.95f, 0.5f, 1f)
			}
		};

		private static readonly Dictionary<string, string> crystalTex = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
		{
			{ "tex_cha_copalArmor", "tex_cha_copalArmor_crystal" },
			{ "tex_cha_copalArmor_Monsoon", "tex_cha_copalArmor_Monsoon_crystal" },
			{ "tex_cha_copalArmor_n", "tex_cha_copalArmor_crystal_n" },
			{ "tex_cha_copalArmor_s", "tex_cha_copalArmor_crystal_s" }
		};

		private static readonly Dictionary<int, float[]> islands = new Dictionary<int, float[]>();

		private static readonly Dictionary<string, MeshData> meshes = new Dictionary<string, MeshData>(StringComparer.Ordinal);

		private static readonly Dictionary<int, Mesh> built = new Dictionary<int, Mesh>();

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

		private static readonly Dictionary<int, Material> crystalMat = new Dictionary<int, Material>();

		internal static readonly List<string> report = new List<string>();

		private static bool loaded;

		internal static bool IsCopal(Material m)
		{
			if ((Object)(object)m != (Object)null)
			{
				return specColor.ContainsKey(((Object)m).name);
			}
			return false;
		}

		private static void Load(Action<string> problem)
		{
			loaded = true;
			using Stream stream = typeof(Copal).Assembly.GetManifestResourceStream("OutwardArmor2kRetex.copal_crystal.txt");
			if (stream == null)
			{
				problem("copal_crystal.txt missing from the DLL");
				return;
			}
			MeshData meshData = null;
			string[] array = new StreamReader(stream).ReadToEnd().Split(new char[1] { '\n' });
			for (int i = 0; i < array.Length; i++)
			{
				string[] array2 = array[i].Trim().Split(new char[1] { ' ' });
				if (array2[0] == "island")
				{
					islands[int.Parse(array2[1])] = (from x in UnityEngineExtensions.Skip<string>(array2, 2).Take(6)
						select float.Parse(x, CultureInfo.InvariantCulture)).ToArray();
				}
				else if (array2[0] == "mesh")
				{
					MeshData meshData2 = new MeshData();
					meshData2.nv = int.Parse(array2[2]);
					meshData2.hash = uint.Parse(array2[3]);
					meshData2.counts = array2[4].Split(new char[1] { ',' }).Select(int.Parse).ToArray();
					meshData = meshData2;
					meshes[array2[1]] = meshData;
				}
				else if (array2[0] == "sub" && meshData != null)
				{
					meshData.moved[int.Parse(array2[1])] = (from p in UnityEngineExtensions.Skip<string>(array2, 2)
						select p.Split(new char[1] { ':' }).Select(int.Parse).ToArray()).ToArray();
				}
			}
		}

		private static uint Fnv(Mesh m)
		{
			uint num = 2166136261u;
			for (int i = 0; i < m.subMeshCount; i++)
			{
				int[] indices = m.GetIndices(i);
				foreach (int num2 in indices)
				{
					for (int k = 0; k < 4; k++)
					{
						num ^= (uint)((num2 >> 8 * k) & 0xFF);
						num *= 16777619;
					}
				}
			}
			return num;
		}

		internal static void Apply(Renderer r, Func<string, Texture> tex, Action<string> problem)
		{
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Expected O, but got Unknown
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			SkinnedMeshRenderer val = (SkinnedMeshRenderer)(object)((r is SkinnedMeshRenderer) ? r : null);
			Material[] sharedMaterials = r.sharedMaterials;
			if ((Object)(object)val == (Object)null || sharedMaterials.Length == 0 || !sharedMaterials.Any(IsCopal))
			{
				return;
			}
			if (!loaded)
			{
				Load(problem);
			}
			Mesh sharedMesh = val.sharedMesh;
			if ((Object)(object)sharedMesh == (Object)null || ours.Contains(((Object)sharedMesh).GetInstanceID()))
			{
				return;
			}
			if (sharedMaterials.Length != 1 || !IsCopal(sharedMaterials[0]))
			{
				problem($"{((Object)r).name}: Copal renderer with {sharedMaterials.Length} materials (1 measured): left vanilla");
				return;
			}
			if (!built.TryGetValue(((Object)sharedMesh).GetInstanceID(), out var value))
			{
				value = Build(sharedMesh, problem);
				built[((Object)sharedMesh).GetInstanceID()] = value;
				if ((Object)(object)value != (Object)null)
				{
					ours.Add(((Object)value).GetInstanceID());
				}
			}
			if ((Object)(object)value == (Object)null)
			{
				return;
			}
			Material val2 = sharedMaterials[0];
			if (!crystalMat.TryGetValue(((Object)val2).GetInstanceID(), out var value2))
			{
				value2 = new Material(val2)
				{
					name = ((Object)val2).name + "_crystal"
				};
				List<string> list = new List<string>();
				string[] texturePropertyNames = value2.GetTexturePropertyNames();
				foreach (string text in texturePropertyNames)
				{
					Texture texture = value2.GetTexture(text);
					if (!((Object)(object)texture == (Object)null) && crystalTex.TryGetValue(((Object)texture).name, out var value3))
					{
						Texture val3 = tex(value3);
						if ((Object)(object)val3 == (Object)null)
						{
							problem(((Object)value2).name + "." + text + ": " + value3 + " not in the bundle");
						}
						else
						{
							value2.SetTexture(text, val3);
							list.Add(text + "=" + value3);
						}
					}
				}
				if (value2.HasProperty("_SpecColor"))
				{
					Color val4 = specColor[((Object)val2).name];
					value2.SetColor("_SpecColor", val4);
					list.Add($"_SpecColor={val4.r:0.00},{val4.g:0.00},{val4.b:0.00}");
				}
				crystalMat[((Object)val2).GetInstanceID()] = value2;
				report.Add("material " + ((Object)value2).name + " (copy of " + ((Object)val2).name + "): " + string.Join(", ", list));
			}
			val.sharedMesh = value;
			r.sharedMaterials = (Material[])(object)new Material[2] { val2, value2 };
			report.Add("renderer " + ((Object)r).name + ": " + ((Object)sharedMesh).name + " -> " + ((Object)value).name + ", materials " + ((Object)val2).name + " + " + ((Object)value2).name);
		}

		private static Mesh Build(Mesh src, Action<string> problem)
		{
			//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02af: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0333: Unknown result type (might be due to invalid IL or missing references)
			//IL_033b: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0515: Unknown result type (might be due to invalid IL or missing references)
			//IL_0527: Unknown result type (might be due to invalid IL or missing references)
			//IL_052c: Unknown result type (might be due to invalid IL or missing references)
			//IL_053b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0540: Unknown result type (might be due to invalid IL or missing references)
			//IL_0542: Unknown result type (might be due to invalid IL or missing references)
			//IL_0548: Unknown result type (might be due to invalid IL or missing references)
			//IL_0550: Unknown result type (might be due to invalid IL or missing references)
			//IL_056a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0572: Unknown result type (might be due to invalid IL or missing references)
			//IL_090c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0911: Unknown result type (might be due to invalid IL or missing references)
			//IL_092e: Expected O, but got Unknown
			//IL_0944: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0709: Unknown result type (might be due to invalid IL or missing references)
			//IL_0719: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a59: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a5b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0734: Unknown result type (might be due to invalid IL or missing references)
			//IL_07c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_07cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_07cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_07df: Unknown result type (might be due to invalid IL or missing references)
			//IL_081f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0826: Unknown result type (might be due to invalid IL or missing references)
			//IL_082d: Unknown result type (might be due to invalid IL or missing references)
			//IL_083f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0848: Unknown result type (might be due to invalid IL or missing references)
			//IL_084f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0ae7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0756: Unknown result type (might be due to invalid IL or missing references)
			//IL_0790: Unknown result type (might be due to invalid IL or missing references)
			if (!meshes.TryGetValue(((Object)src).name, out var value))
			{
				problem(((Object)src).name + ": Copal mesh not in copal_crystal.txt: left vanilla");
				return null;
			}
			if (!src.isReadable)
			{
				problem(((Object)src).name + ": not readable: left vanilla");
				return null;
			}
			int[] array = (from i in Enumerable.Range(0, src.subMeshCount)
				select (int)src.GetIndexCount(i)).ToArray();
			uint num = Fnv(src);
			if (src.vertexCount != value.nv || !array.SequenceEqual(value.counts) || num != value.hash)
			{
				problem(string.Format("{0}: game mesh differs from the checked one ({1} points, {2} indices, hash {3}; ", ((Object)src).name, src.vertexCount, string.Join("+", array), num) + string.Format("expected {0}, {1}, {2}): left vanilla", value.nv, string.Join("+", value.counts), value.hash));
				return null;
			}
			if (src.blendShapeCount > 0)
			{
				problem(((Object)src).name + ": has blend shapes: left vanilla");
				return null;
			}
			if (value.moved.Keys.Any((int k) => k != 0))
			{
				report.Add(((Object)src).name + ": split triangles of undrawn submeshes " + string.Join(",", value.moved.Keys.Where((int k) => k != 0)) + " ignored (vanilla never draws them)");
			}
			if (!value.moved.TryGetValue(0, out var value2) || value2.Length == 0)
			{
				problem(((Object)src).name + ": no crystal triangles in submesh 0");
				return null;
			}
			List<Vector3> list = new List<Vector3>();
			src.GetVertices(list);
			List<Vector3> list2 = new List<Vector3>();
			src.GetNormals(list2);
			List<Vector4> list3 = new List<Vector4>();
			src.GetTangents(list3);
			List<Color32> list4 = new List<Color32>();
			if (src.HasVertexAttribute((VertexAttribute)3))
			{
				src.GetColors(list4);
			}
			NativeArray<byte> bonesPerVertex = src.GetBonesPerVertex();
			NativeArray<BoneWeight1> allBoneWeights = src.GetAllBoneWeights();
			bool flag = bonesPerVertex.Length > 0;
			List<byte> list5 = new List<byte>(bonesPerVertex.ToArray());
			List<BoneWeight1> list6 = new List<BoneWeight1>(allBoneWeights.ToArray());
			int[] array2 = new int[list5.Count];
			for (int num2 = 1; num2 < list5.Count; num2++)
			{
				array2[num2] = array2[num2 - 1] + list5[num2 - 1];
			}
			List<Vector4>[] array3 = new List<Vector4>[8];
			int[] array4 = new int[8];
			for (int num3 = 0; num3 < 8; num3++)
			{
				VertexAttribute val = (VertexAttribute)(4 + num3);
				if (src.HasVertexAttribute(val))
				{
					array4[num3] = src.GetVertexAttributeDimension(val);
					array3[num3] = new List<Vector4>();
					src.GetUVs(num3, array3[num3]);
				}
			}
			if (array3[0] == null || list3.Count != list.Count || list2.Count != list.Count)
			{
				problem(((Object)src).name + ": no UV0 / normals / tangents: left vanilla");
				return null;
			}
			int count = list.Count;
			int[] indices = src.GetIndices(0);
			Dictionary<int, int> dictionary = new Dictionary<int, int>();
			int[][] array5 = value2;
			foreach (int[] array6 in array5)
			{
				dictionary[array6[0]] = array6[1];
			}
			bool[] array7 = new bool[count];
			for (int num5 = 0; num5 < src.subMeshCount; num5++)
			{
				int[] array8 = ((num5 == 0) ? indices : src.GetIndices(num5));
				for (int num6 = 0; num6 < array8.Length / 3; num6++)
				{
					if (num5 != 0 || !dictionary.ContainsKey(num6))
					{
						for (int num7 = 0; num7 < 3; num7++)
						{
							array7[array8[3 * num6 + num7]] = true;
						}
					}
				}
			}
			Dictionary<int, HashSet<long>> dictionary2 = new Dictionary<int, HashSet<long>>();
			Dictionary<int, (int, int, int)> dictionary3 = new Dictionary<int, (int, int, int)>();
			foreach (KeyValuePair<int, int> item2 in dictionary)
			{
				int key = item2.Key;
				if (3 * key + 2 >= indices.Length || !islands.ContainsKey(item2.Value))
				{
					problem($"{((Object)src).name}: crystal triangle {key} / island {item2.Value} out of range: left vanilla");
					return null;
				}
				Vector4 val2 = array3[0][indices[3 * key]];
				Vector4 val3 = array3[0][indices[3 * key + 1]];
				Vector4 val4 = array3[0][indices[3 * key + 2]];
				int num8 = Mathf.FloorToInt((val2.x + val3.x + val4.x) / 3f);
				int num9 = Mathf.FloorToInt((val2.y + val3.y + val4.y) / 3f);
				dictionary3[key] = (item2.Value, num8, num9);
				for (int num10 = 0; num10 < 3; num10++)
				{
					int key2 = indices[3 * key + num10];
					if (!dictionary2.TryGetValue(key2, out var value3))
					{
						value3 = (dictionary2[key2] = new HashSet<long>());
					}
					value3.Add(Key(item2.Value, num8, num9));
				}
			}
			Dictionary<(int, long), int> dictionary4 = new Dictionary<(int, long), int>();
			List<int> list7 = new List<int>();
			List<int> list8 = new List<int>();
			int num11 = 0;
			int num12 = 0;
			for (int num13 = 0; num13 < indices.Length / 3; num13++)
			{
				if (!dictionary3.TryGetValue(num13, out var value4))
				{
					list7.Add(indices[3 * num13]);
					list7.Add(indices[3 * num13 + 1]);
					list7.Add(indices[3 * num13 + 2]);
					continue;
				}
				long item = Key(value4.Item1, value4.Item2, value4.Item3);
				for (int num14 = 0; num14 < 3; num14++)
				{
					int num15 = indices[3 * num13 + num14];
					if (!dictionary4.TryGetValue((num15, item), out var value5))
					{
						if (!array7[num15] && dictionary2[num15].Count == 1)
						{
							value5 = num15;
							num12++;
						}
						else
						{
							value5 = list.Count;
							num11++;
							list.Add(list[num15]);
							list2.Add(list2[num15]);
							list3.Add(list3[num15]);
							if (list4.Count == count)
							{
								list4.Add(list4[num15]);
							}
							for (int num16 = 0; num16 < 8; num16++)
							{
								if (array3[num16] != null)
								{
									array3[num16].Add(array3[num16][num15]);
								}
							}
							if (flag)
							{
								list5.Add(list5[num15]);
								for (int num17 = 0; num17 < list5[num15]; num17++)
								{
									list6.Add(list6[array2[num15] + num17]);
								}
							}
						}
						float[] array9 = islands[value4.Item1];
						Vector4 val5 = array3[0][num15];
						float num18 = val5.x - (float)value4.Item2;
						float num19 = val5.y - (float)value4.Item3;
						array3[0][value5] = new Vector4(array9[0] * num18 + array9[1] * num19 + array9[2], array9[3] * num18 + array9[4] * num19 + array9[5], val5.z, val5.w);
						list3[value5] = Turn(list2[value5], list3[num15], array9);
						dictionary4[(num15, item)] = value5;
					}
					list8.Add(value5);
				}
			}
			if (flag && (bonesPerVertex.Length != count || list5.Count != list.Count))
			{
				problem($"{((Object)src).name}: bone weights for {bonesPerVertex.Length} points, mesh has {count} (+{num11} copies, weights now {list5.Count}): left vanilla");
				return null;
			}
			Mesh val6 = new Mesh
			{
				name = ((Object)src).name + "_crystal"
			};
			val6.indexFormat = (IndexFormat)((list.Count > 65535) ? 1 : ((int)src.indexFormat));
			val6.SetVertices(list);
			val6.SetNormals(list2);
			val6.SetTangents(list3);
			if (list4.Count == list.Count)
			{
				val6.SetColors(list4);
			}
			for (int num20 = 0; num20 < 8; num20++)
			{
				if (array3[num20] == null)
				{
					continue;
				}
				if (array4[num20] == 2)
				{
					val6.SetUVs(num20, array3[num20].Select((Vector4 x) => Vector4.op_Implicit(x)).ToList());
				}
				else if (array4[num20] == 3)
				{
					val6.SetUVs(num20, array3[num20].Select((Vector4 x) => Vector4.op_Implicit(x)).ToList());
				}
				else
				{
					val6.SetUVs(num20, array3[num20]);
				}
			}
			if (flag)
			{
				NativeArray<byte> val7 = default(NativeArray<byte>);
				val7..ctor(list5.ToArray(), (Allocator)2);
				NativeArray<BoneWeight1> val8 = default(NativeArray<BoneWeight1>);
				val8..ctor(list6.ToArray(), (Allocator)2);
				val6.SetBoneWeights(val7, val8);
				val7.Dispose();
				val8.Dispose();
			}
			val6.bindposes = src.bindposes;
			val6.subMeshCount = src.subMeshCount + 1;
			val6.SetTriangles(list7, 0, false);
			val6.SetTriangles(list8, 1, false);
			for (int num21 = 1; num21 < src.subMeshCount; num21++)
			{
				val6.SetTriangles(src.GetIndices(num21), num21 + 1, false);
			}
			val6.bounds = src.bounds;
			val6.UploadMeshData(false);
			report.Add($"mesh {((Object)src).name}: {list8.Count / 3} crystal triangles of {indices.Length / 3} (submesh 0), {num12} points re-mapped in place, " + $"{num11} points copied ({count} -> {list.Count}); submeshes {src.subMeshCount} -> {val6.subMeshCount}");
			return val6;
		}

		private static long Key(int isl, int fu, int fv)
		{
			return ((long)isl << 40) ^ ((long)(fu + 32768) << 20) ^ (fv + 32768);
		}

		private static Vector4 Turn(Vector3 n, Vector4 t4, float[] A)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: 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_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: 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: 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_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor(t4.x, t4.y, t4.z);
			float num = ((t4.w < 0f) ? (-1f) : 1f);
			Vector3 val2 = Vector3.Cross(n, val) * num;
			float num2 = A[0] * A[4] - A[1] * A[3];
			float num3 = A[4] / num2;
			float num4 = (0f - A[1]) / num2;
			float num5 = (0f - A[3]) / num2;
			float num6 = A[0] / num2;
			Vector3 val3 = val * num3 + val2 * num5;
			Vector3 val4 = val * num4 + val2 * num6;
			Vector3 val5 = val3 - n * Vector3.Dot(n, val3);
			Vector3 normalized = ((Vector3)(ref val5)).normalized;
			float num7 = ((Vector3.Dot(Vector3.Cross(n, normalized), val4) >= 0f) ? 1f : (-1f));
			return new Vector4(normalized.x, normalized.y, normalized.z, num7);
		}
	}
	[BepInPlugin("nhu.outwardarmor2kretex", "Outward Vanilla Armors 2K RETEX", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "nhu.outwardarmor2kretex";

		public const string Name = "Outward Vanilla Armors 2K RETEX";

		public const string Version = "1.0.0";

		private const string BundleFile = "outward_armor_2k_retex.bundle";

		private static ManualLogSource log;

		private static string folder;

		private static AssetBundle bundle;

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

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

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

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

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

		private static readonly List<string> patchedMaterials = new List<string>();

		private static readonly List<string> problems = new List<string>();

		private static int prefabsChecked;

		private static int charactersChecked;

		private static string patchingCharacter;

		private void Awake()
		{
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			log = ((BaseUnityPlugin)this).Logger;
			folder = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			string text = Path.Combine(folder, "outward_armor_2k_retex.bundle");
			if (!File.Exists(text))
			{
				problems.Add("bundle not found: " + text);
				log.LogError((object)("[2K RETEX] outward_armor_2k_retex.bundle not found beside the DLL (" + folder + "); nothing changed."));
				WriteReport();
				return;
			}
			bundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)bundle == (Object)null)
			{
				problems.Add("bundle failed to load: " + text);
				log.LogError((object)("[2K RETEX] could not load " + text + "; nothing changed."));
				WriteReport();
				return;
			}
			string[] allAssetNames = bundle.GetAllAssetNames();
			foreach (string text2 in allAssetNames)
			{
				string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text2);
				if (assetPath.ContainsKey(fileNameWithoutExtension))
				{
					problems.Add("two bundle assets share the name " + fileNameWithoutExtension + " (" + assetPath[fileNameWithoutExtension] + ", " + text2 + ")");
				}
				else
				{
					assetPath[fileNameWithoutExtension] = text2;
				}
			}
			new Harmony("nhu.outwardarmor2kretex").PatchAll(typeof(Plugin));
			log.LogInfo((object)("[2K RETEX] 1.0.0: " + assetPath.Count + " textures in outward_armor_2k_retex.bundle (" + ((double)new FileInfo(text).Length / 1048576.0).ToString("0.0") + " MB); hook on ResourcesPrefabManager.GetItemVisualPrefab; report: " + Path.Combine(folder, "report.txt")));
			WriteReport();
		}

		private void OnApplicationQuit()
		{
			WriteReport();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ResourcesPrefabManager), "GetItemVisualPrefab")]
		private static void AfterGetItemVisualPrefab(Transform __result)
		{
			if ((Object)(object)__result == (Object)null || (Object)(object)bundle == (Object)null || !seenPrefabs.Add(((Object)__result).GetInstanceID()))
			{
				return;
			}
			try
			{
				PatchPrefab(__result);
			}
			catch (Exception ex)
			{
				problems.Add(((Object)__result).name + ": " + ex.Message);
				log.LogError((object)("[2K RETEX] " + ((Object)__result).name + ": " + ex));
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Character), "Awake")]
		private static void AfterCharacterAwake(Character __instance)
		{
			if ((Object)(object)__instance == (Object)null || (Object)(object)bundle == (Object)null)
			{
				return;
			}
			charactersChecked++;
			patchingCharacter = ((Object)__instance).name;
			try
			{
				PatchPrefab(((Component)__instance).transform, item: false);
			}
			catch (Exception ex)
			{
				problems.Add(((Object)__instance).name + ": " + ex.Message);
				log.LogError((object)("[2K RETEX] " + ((Object)__instance).name + ": " + ex));
			}
			finally
			{
				patchingCharacter = null;
			}
		}

		private static void PatchPrefab(Transform prefab, bool item = true)
		{
			if (item)
			{
				prefabsChecked++;
			}
			bool flag = false;
			Renderer[] componentsInChildren = ((Component)prefab).GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				Material[] sharedMaterials = val.sharedMaterials;
				foreach (Material val2 in sharedMaterials)
				{
					if (!((Object)(object)val2 == (Object)null) && seenMaterials.Add(((Object)val2).GetInstanceID()) && PatchMaterial(val2))
					{
						flag = true;
					}
				}
				int count = Copal.report.Count;
				Copal.Apply(val, Applied, delegate(string p)
				{
					problems.Add(p);
				});
				if (Copal.report.Count != count)
				{
					flag = true;
				}
			}
			if (flag)
			{
				WriteReport();
			}
		}

		private static bool PatchMaterial(Material m)
		{
			List<string> list = new List<string>();
			string[] texturePropertyNames = m.GetTexturePropertyNames();
			foreach (string text in texturePropertyNames)
			{
				Texture texture = m.GetTexture(text);
				if ((Object)(object)texture == (Object)null || !assetPath.ContainsKey(((Object)texture).name))
				{
					continue;
				}
				Texture val = Load(((Object)texture).name);
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val == (Object)(object)texture))
				{
					m.SetTexture(text, val);
					list.Add(text + "=" + ((Object)texture).name);
					if (!appliedTo.TryGetValue(((Object)texture).name, out var value))
					{
						value = (appliedTo[((Object)texture).name] = new List<string>());
					}
					value.Add(((Object)m).name + "." + text);
				}
			}
			string name = ((Object)m).name;
			if (list.Count == 0)
			{
				return false;
			}
			patchedMaterials.Add(name + " (" + ((Object)m.shader).name + "): " + string.Join(", ", list) + ((patchingCharacter != null) ? ("  [character " + patchingCharacter + "]") : ""));
			log.LogDebug((object)("[2K RETEX] " + name + ": " + string.Join(", ", list)));
			return true;
		}

		private static Texture Applied(string name)
		{
			if (!assetPath.ContainsKey(name))
			{
				return null;
			}
			Texture obj = Load(name);
			if ((Object)(object)obj != (Object)null && !appliedTo.ContainsKey(name))
			{
				appliedTo[name] = new List<string> { "Copal crystal material" };
			}
			return obj;
		}

		private static Texture Load(string name)
		{
			if (loaded.TryGetValue(name, out var value))
			{
				return value;
			}
			value = (Texture)(object)bundle.LoadAsset<Texture2D>(assetPath[name]);
			if ((Object)(object)value == (Object)null)
			{
				problems.Add("bundle asset did not load as Texture2D: " + assetPath[name]);
			}
			loaded[name] = value;
			return value;
		}

		private static void WriteReport()
		{
			if (folder == null)
			{
				return;
			}
			try
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Outward Vanilla Armors 2K RETEX 1.0.0 - report " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
				stringBuilder.AppendLine("Textures in bundle: " + assetPath.Count + " | loaded: " + loaded.Count + " | applied: " + appliedTo.Count + " | item visual prefabs checked: " + prefabsChecked + " | characters checked: " + charactersChecked + " | materials patched: " + patchedMaterials.Count);
				if (problems.Count > 0)
				{
					stringBuilder.AppendLine();
					stringBuilder.AppendLine("PROBLEMS (" + problems.Count + "):");
					foreach (string problem in problems)
					{
						stringBuilder.AppendLine("  " + problem);
					}
				}
				stringBuilder.AppendLine();
				stringBuilder.AppendLine("MATERIALS PATCHED (" + patchedMaterials.Count + "):");
				foreach (string item in patchedMaterials.OrderBy((string x) => x))
				{
					stringBuilder.AppendLine("  " + item);
				}
				stringBuilder.AppendLine();
				stringBuilder.AppendLine("COPAL CRYSTAL SUBMESH (" + Copal.report.Count + " lines):");
				foreach (string item2 in Copal.report)
				{
					stringBuilder.AppendLine("  " + item2);
				}
				List<string> list = (from n in assetPath.Keys
					where !appliedTo.ContainsKey(n)
					orderby n
					select n).ToList();
				stringBuilder.AppendLine();
				stringBuilder.AppendLine("NEVER MET YET (" + list.Count + "; normal until that armour has been seen this session):");
				foreach (string item3 in list)
				{
					stringBuilder.AppendLine("  " + item3);
				}
				File.WriteAllText(Path.Combine(folder, "report.txt"), stringBuilder.ToString());
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("[2K RETEX] report.txt not written: " + ex.Message));
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}