Decompiled source of SkinCloneCompatibility v0.2.2

BepInEx/plugins/SkinCloneCompatibility/SkinCloneCompatibility.dll

Decompiled a month ago
using System;
using System.Collections.Concurrent;
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.Cryptography;
using BepInEx;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using RoR2;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SkinCloneCompatibility")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.2.0")]
[assembly: AssemblyInformationalVersion("0.2.2")]
[assembly: AssemblyProduct("SkinCloneCompatibility")]
[assembly: AssemblyTitle("SkinCloneCompatibility")]
[assembly: AssemblyVersion("0.2.2.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 SkinCloneCompatibility
{
	public static class BaseSkinRule
	{
		public static bool Matches(string sourceName, string sourceToken, string displayName, string displayToken)
		{
			if (!string.IsNullOrEmpty(sourceName) && !string.IsNullOrEmpty(sourceToken) && sourceToken == displayToken)
			{
				if (!(displayName == sourceName))
				{
					return displayName == sourceName + "(Clone)";
				}
				return true;
			}
			return false;
		}
	}
	public static class CloneRule
	{
		public static bool Matches(int candidateIndex, int registeredIndex, string candidateToken, string registeredToken, string candidateName, string registeredName)
		{
			if (candidateIndex >= 0 && candidateIndex == registeredIndex && !string.IsNullOrEmpty(candidateToken) && candidateToken == registeredToken)
			{
				return candidateName == registeredName + "(Clone)";
			}
			return false;
		}
	}
	public enum ColorAdapter
	{
		Skins1411,
		Skins1414
	}
	public static class ColorAdapterSelector
	{
		public static ColorAdapter Select(string name, Version version, string sha256)
		{
			if (name != "R2API.Skins")
			{
				throw new InvalidOperationException("Unexpected color target assembly: " + name);
			}
			if (version == new Version(1, 4, 11, 0) && sha256 == "9A67B1D407604A5AA3D710DEA3DF248152B62E7D5CA029F80127F96937F47C08")
			{
				return ColorAdapter.Skins1411;
			}
			if (version == new Version(1, 4, 14, 0) && sha256 == "D47FD41D65319D07EDA4D7173AF4016783D691F153C44342E6425828406C6B1B")
			{
				return ColorAdapter.Skins1414;
			}
			throw new InvalidOperationException("Unaudited R2API.Skins version/hash: version=" + version?.ToString() + " SHA256=" + sha256);
		}
	}
	public sealed class DeferredBaseMapping<T> where T : class
	{
		public sealed class Outcome
		{
			public T Source;

			public T Clone;

			public T Intended;

			public T Fallback;

			public T Mapped;

			public string Reason;

			public bool Applied;
		}

		private sealed class Entry
		{
			public T Source;

			public T Clone;

			public T Intended;

			public T Fallback;

			public T Candidate;

			public int Slot;

			public string Failure;

			public bool HasDeclaredBase;
		}

		private sealed class ReferenceComparer : IEqualityComparer<T>
		{
			public bool Equals(T a, T b)
			{
				return a == b;
			}

			public int GetHashCode(T x)
			{
				return RuntimeHelpers.GetHashCode(x);
			}
		}

		private readonly T[] body;

		private readonly T[] ordered;

		private readonly T[][] declarations;

		private readonly Func<T, bool> valid;

		private readonly Func<T, T, bool> matches;

		private readonly Func<T, T[]> getBases;

		private readonly Action<T, T[]> setBases;

		private readonly List<Entry> entries = new List<Entry>();

		private readonly Dictionary<T, Entry> byClone = new Dictionary<T, Entry>(new ReferenceComparer());

		private bool finished;

		public DeferredBaseMapping(T[] body, T[] ordered, Func<T, bool> valid, Func<T, T, bool> matches, Func<T, T[]> getBases, Action<T, T[]> setBases)
		{
			this.body = (T[])(body ?? throw new ArgumentNullException("body")).Clone();
			this.ordered = ordered ?? throw new ArgumentNullException("ordered");
			if (body.Length != ordered.Length)
			{
				throw new InvalidOperationException("Ordered/body lengths differ");
			}
			this.valid = valid;
			this.matches = matches;
			this.getBases = getBases;
			this.setBases = setBases;
			declarations = new T[body.Length][];
			for (int i = 0; i < body.Length; i++)
			{
				T[] array = (valid(body[i]) ? getBases(body[i]) : null);
				declarations[i] = ((array == null) ? null : ((T[])array.Clone()));
			}
		}

		public void Capture(int sourceIndex, T clone, T[] incoming, int slot, T fallback)
		{
			if (finished || sourceIndex < 0 || sourceIndex >= body.Length || !valid(body[sourceIndex]) || !valid(clone) || incoming == null || slot < 0 || slot >= incoming.Length || byClone.ContainsKey(clone))
			{
				throw new InvalidOperationException("Invalid/duplicate clone capture");
			}
			T[] array = body;
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] == clone)
				{
					throw new InvalidOperationException("Refusing to mutate a body skin");
				}
			}
			if (incoming != getBases(clone))
			{
				throw new InvalidOperationException("Clone base array changed before capture");
			}
			T[] array2 = declarations[sourceIndex];
			bool flag = array2 != null && slot < array2.Length && valid(array2[slot]);
			Entry entry = new Entry
			{
				Source = body[sourceIndex],
				Clone = clone,
				Intended = (flag ? array2[slot] : null),
				HasDeclaredBase = flag,
				Fallback = fallback,
				Slot = slot
			};
			T[] array3 = (T[])incoming.Clone();
			array3[slot] = fallback;
			setBases(clone, array3);
			entries.Add(entry);
			byClone.Add(clone, entry);
		}

		private T FindCandidate(T intended, out string failure)
		{
			failure = null;
			for (int i = 0; i < body.Length; i++)
			{
				if (body[i] == intended && valid(ordered[i]) && matches(intended, ordered[i]))
				{
					return ordered[i];
				}
			}
			T val = null;
			T[] array = ordered;
			foreach (T val2 in array)
			{
				if (valid(val2) && matches(intended, val2))
				{
					if (val != null && val != val2)
					{
						failure = "ambiguous-target";
						return null;
					}
					val = val2;
				}
			}
			if (val == null)
			{
				failure = "missing-or-mismatched-target";
			}
			return val;
		}

		private bool ValidateGraph(T node, HashSet<T> visiting, HashSet<T> done, ref int budget, out string reason)
		{
			reason = null;
			if (!valid(node))
			{
				return true;
			}
			if (--budget < 0)
			{
				reason = "graph-budget";
				return false;
			}
			if (visiting.Contains(node))
			{
				reason = "inheritance-cycle";
				return false;
			}
			if (done.Contains(node))
			{
				return true;
			}
			if (visiting.Count >= 128)
			{
				reason = "graph-depth";
				return false;
			}
			byClone.TryGetValue(node, out var value);
			if (value != null && value.Failure != null)
			{
				reason = "unmapped-dependency:" + value.Failure;
				return false;
			}
			visiting.Add(node);
			T[] array = getBases(node);
			if (value != null && (array == null || value.Slot >= array.Length))
			{
				reason = "clone-bases-changed";
				return false;
			}
			if (array != null)
			{
				for (int i = 0; i < array.Length; i++)
				{
					T node2 = ((value != null && value.HasDeclaredBase && value.Slot == i) ? value.Candidate : array[i]);
					if (!ValidateGraph(node2, visiting, done, ref budget, out reason))
					{
						return false;
					}
				}
			}
			visiting.Remove(node);
			done.Add(node);
			return true;
		}

		public IReadOnlyList<Outcome> Complete()
		{
			if (finished)
			{
				throw new InvalidOperationException("Conversion already finalized");
			}
			finished = true;
			foreach (Entry entry in entries)
			{
				if (entry.HasDeclaredBase)
				{
					entry.Candidate = FindCandidate(entry.Intended, out entry.Failure);
				}
				else
				{
					entry.Candidate = entry.Fallback;
				}
			}
			Dictionary<T, string> dictionary = new Dictionary<T, string>(new ReferenceComparer());
			foreach (Entry entry2 in entries)
			{
				int budget = 4096;
				string reason;
				if (!valid(entry2.Clone))
				{
					dictionary.Add(entry2.Clone, "clone-unavailable");
				}
				else if (!ValidateGraph(entry2.Clone, new HashSet<T>(new ReferenceComparer()), new HashSet<T>(new ReferenceComparer()), ref budget, out reason))
				{
					dictionary.Add(entry2.Clone, reason);
				}
			}
			List<Outcome> list = new List<Outcome>();
			foreach (Entry entry3 in entries)
			{
				string value;
				bool flag = !dictionary.TryGetValue(entry3.Clone, out value);
				T[] array = ((flag && entry3.HasDeclaredBase) ? getBases(entry3.Clone) : null);
				if (flag && entry3.HasDeclaredBase && (array == null || entry3.Slot >= array.Length))
				{
					flag = false;
					value = "clone-bases-changed";
				}
				if (flag && entry3.HasDeclaredBase)
				{
					T[] array2 = (T[])array.Clone();
					array2[entry3.Slot] = entry3.Candidate;
					setBases(entry3.Clone, array2);
				}
				T[] array3 = (valid(entry3.Clone) ? getBases(entry3.Clone) : null);
				list.Add(new Outcome
				{
					Source = entry3.Source,
					Clone = entry3.Clone,
					Intended = entry3.Intended,
					Fallback = entry3.Fallback,
					Mapped = ((array3 != null && entry3.Slot < array3.Length) ? array3[entry3.Slot] : null),
					Applied = (flag && entry3.HasDeclaredBase),
					Reason = (flag ? (entry3.HasDeclaredBase ? "verified" : "no-declared-base") : value)
				});
			}
			return list;
		}
	}
	public static class DisplayBasePatchRules
	{
		private static bool LoadLocal(Instruction instruction, int index)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (instruction != null && (instruction.OpCode == OpCodes.Ldloc_S || instruction.OpCode == OpCodes.Ldloc))
			{
				object operand = instruction.Operand;
				VariableDefinition val = (VariableDefinition)((operand is VariableDefinition) ? operand : null);
				if (val != null)
				{
					return ((VariableReference)val).Index == index;
				}
			}
			return false;
		}

		public static Instruction FindStore(MethodDefinition method)
		{
			if (!method.HasBody || ((MethodReference)method).Parameters.Count != 0 || ((MemberReference)((MethodReference)method).ReturnType).FullName != "System.Void" || method.Body.Variables.Count <= 11 || ((MemberReference)((VariableReference)method.Body.Variables[5]).VariableType).FullName != "RoR2.ModelSkinController" || ((MemberReference)((VariableReference)method.Body.Variables[7]).VariableType).FullName != "RoR2.ModelSkinController" || ((MemberReference)((VariableReference)method.Body.Variables[9]).VariableType).FullName != "System.Int32" || ((MemberReference)((VariableReference)method.Body.Variables[10]).VariableType).FullName != "RoR2.SkinDef" || ((MemberReference)((VariableReference)method.Body.Variables[11]).VariableType).FullName != "RoR2.SkinDef")
			{
				throw new InvalidOperationException("Unexpected display converter signature or locals: name=" + ((MemberReference)method).Name + " parameters=" + ((MethodReference)method).Parameters.Count + " locals=" + (method.HasBody ? string.Join(";", ((IEnumerable<VariableDefinition>)method.Body.Variables).Select((VariableDefinition v) => ((VariableReference)v).Index + ":" + ((MemberReference)((VariableReference)v).VariableType).FullName)) : "<no body>"));
			}
			Instruction[] array = ((IEnumerable<Instruction>)method.Body.Instructions).Where(delegate(Instruction i)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_0041: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: 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)
				//IL_0060: Unknown result type (might be due to invalid IL or missing references)
				//IL_0065: Unknown result type (might be due to invalid IL or missing references)
				//IL_008f: Unknown result type (might be due to invalid IL or missing references)
				//IL_009a: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
				if (i.OpCode == OpCodes.Stelem_Ref && LoadLocal(i.Previous, 11))
				{
					Instruction previous = i.Previous.Previous;
					OpCode? val = ((previous != null) ? new OpCode?(previous.OpCode) : ((OpCode?)null));
					OpCode ldc_I4_ = OpCodes.Ldc_I4_0;
					if (val.HasValue && val.GetValueOrDefault() == ldc_I4_)
					{
						Instruction previous2 = i.Previous.Previous.Previous;
						val = ((previous2 != null) ? new OpCode?(previous2.OpCode) : ((OpCode?)null));
						ldc_I4_ = OpCodes.Ldfld;
						if (val.HasValue && val.GetValueOrDefault() == ldc_I4_)
						{
							object operand = i.Previous.Previous.Previous.Operand;
							FieldReference val2 = (FieldReference)((operand is FieldReference) ? operand : null);
							if (val2 != null && ((MemberReference)val2).FullName == "RoR2.SkinDef[] RoR2.SkinDef::baseSkins")
							{
								return LoadLocal(i.Previous.Previous.Previous.Previous, 10);
							}
						}
					}
				}
				return false;
			}).ToArray();
			if (array.Length != 1)
			{
				throw new InvalidOperationException("Expected exactly one audited base-skin store");
			}
			Instruction site = array[0];
			if (((IEnumerable<Instruction>)method.Body.Instructions).Any((Instruction i) => i.Operand == site || (i.Operand is Instruction[] source && source.Contains(site))))
			{
				throw new InvalidOperationException("Unexpected branch target at store");
			}
			return site;
		}

		public static void Rewrite(MethodDefinition method, MethodReference helper)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			Instruction val = FindStore(method);
			ILProcessor iLProcessor = method.Body.GetILProcessor();
			iLProcessor.InsertBefore(val, Instruction.Create(OpCodes.Ldloc, method.Body.Variables[5]));
			iLProcessor.InsertBefore(val, Instruction.Create(OpCodes.Ldloc, method.Body.Variables[7]));
			iLProcessor.InsertBefore(val, Instruction.Create(OpCodes.Ldloc, method.Body.Variables[9]));
			val.OpCode = OpCodes.Call;
			val.Operand = helper;
			method.Body.MaxStackSize = Math.Max(method.Body.MaxStackSize, 6);
		}
	}
	public static class DisplayBasePatchRules1414
	{
		public sealed class Plan
		{
			public Instruction Store;

			public Instruction PublishStart;

			public VariableDefinition Body;

			public VariableDefinition Display;

			public VariableDefinition Ordered;

			public VariableDefinition SourceIndex;

			public VariableDefinition Clone;

			public VariableDefinition Fallback;
		}

		private const string Skin = "RoR2.SkinDef";

		private const string Controller = "RoR2.ModelSkinController";

		private static VariableDefinition Local(MethodDefinition method, Instruction i, bool store = false)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Invalid comparison between Unknown and I4
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Invalid comparison between Unknown and I4
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Invalid comparison between Unknown and I4
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Invalid comparison between Unknown and I4
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Invalid comparison between Unknown and I4
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Invalid comparison between Unknown and I4
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Invalid comparison between Unknown and I4
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Invalid comparison between Unknown and I4
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Invalid comparison between Unknown and I4
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Invalid comparison between Unknown and I4
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Invalid comparison between Unknown and I4
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Invalid comparison between Unknown and I4
			if (i == null)
			{
				return null;
			}
			OpCode opCode = i.OpCode;
			Code code = ((OpCode)(ref opCode)).Code;
			if ((!store && ((int)code == 202 || (int)code == 17)) || (store && ((int)code == 204 || (int)code == 19)))
			{
				object operand = i.Operand;
				return (VariableDefinition)((operand is VariableDefinition) ? operand : null);
			}
			int num = ((!store) ? (((int)code != 6) ? (((int)code == 7) ? 1 : (((int)code == 8) ? 2 : (((int)code == 9) ? 3 : (-1)))) : 0) : (((int)code != 10) ? (((int)code == 11) ? 1 : (((int)code == 12) ? 2 : (((int)code == 13) ? 3 : (-1)))) : 0));
			if (num < 0 || num >= method.Body.Variables.Count)
			{
				return null;
			}
			return method.Body.Variables[num];
		}

		private static bool Field(Instruction i, string name)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			if (i != null && i.OpCode == OpCodes.Ldfld)
			{
				object operand = i.Operand;
				FieldReference val = (FieldReference)((operand is FieldReference) ? operand : null);
				if (val != null)
				{
					return ((MemberReference)val).FullName == name;
				}
			}
			return false;
		}

		private static bool Typed(VariableDefinition v, string type)
		{
			if (v != null)
			{
				return ((MemberReference)((VariableReference)v).VariableType).FullName == type;
			}
			return false;
		}

		private static bool Loads(MethodDefinition m, Instruction i, VariableDefinition v)
		{
			if (v != null)
			{
				return Local(m, i) == v;
			}
			return false;
		}

		private static bool BranchesTo(MethodDefinition m, Instruction target)
		{
			return ((IEnumerable<Instruction>)m.Body.Instructions).Any(delegate(Instruction i)
			{
				if (i.Operand != target && (!(i.Operand is Instruction[] source) || !source.Contains(target)))
				{
					object operand = i.Operand;
					ILLabel val = (ILLabel)((operand is ILLabel) ? operand : null);
					if (val == null || val.Target != target)
					{
						if (i.Operand is ILLabel[] source2)
						{
							return source2.Any((ILLabel l) => l.Target == target);
						}
						return false;
					}
				}
				return true;
			});
		}

		public static Plan FindPlan(MethodDefinition method)
		{
			//IL_0352: Unknown result type (might be due to invalid IL or missing references)
			//IL_0357: Unknown result type (might be due to invalid IL or missing references)
			if (!method.HasBody || ((MethodReference)method).Parameters.Count != 0 || ((MemberReference)((MethodReference)method).ReturnType).FullName != "System.Void" || ((MethodReference)method).HasThis || method.Body.HasExceptionHandlers)
			{
				throw new InvalidOperationException("Unexpected 1.4.14 converter signature/control flow");
			}
			Instruction[] array = ((IEnumerable<Instruction>)method.Body.Instructions).Where(delegate(Instruction i)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: 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_0046: Unknown result type (might be due to invalid IL or missing references)
				//IL_004b: Unknown result type (might be due to invalid IL or missing references)
				//IL_005a: Unknown result type (might be due to invalid IL or missing references)
				//IL_005f: Unknown result type (might be due to invalid IL or missing references)
				if (i.OpCode == OpCodes.Stelem_Ref)
				{
					Instruction previous = i.Previous;
					OpCode? obj;
					if (previous == null)
					{
						obj = null;
					}
					else
					{
						Instruction previous2 = previous.Previous;
						obj = ((previous2 != null) ? new OpCode?(previous2.OpCode) : ((OpCode?)null));
					}
					OpCode? val8 = obj;
					OpCode ldc_I4_ = OpCodes.Ldc_I4_0;
					if (val8.HasValue && val8.GetValueOrDefault() == ldc_I4_)
					{
						return Field(i.Previous.Previous.Previous, "RoR2.SkinDef[] RoR2.SkinDef::baseSkins");
					}
				}
				return false;
			}).ToArray();
			if (array.Length != 1)
			{
				throw new InvalidOperationException("Expected one 1.4.14 base inheritance store");
			}
			Instruction val = array[0];
			VariableDefinition clone = Local(method, val.Previous.Previous.Previous.Previous);
			VariableDefinition val2 = Local(method, val.Previous);
			if (!Typed(clone, "RoR2.SkinDef") || !Typed(val2, "RoR2.SkinDef") || clone == val2)
			{
				throw new InvalidOperationException("Unexpected clone/fallback producer");
			}
			Instruction[] array2 = ((IEnumerable<Instruction>)method.Body.Instructions).Where(delegate(Instruction i)
			{
				//IL_002d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Unknown result type (might be due to invalid IL or missing references)
				//IL_003d: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: 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)
				//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
				//IL_0109: 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)
				if (Local(method, i, store: true) == clone)
				{
					Instruction previous = i.Previous;
					OpCode? val8 = ((previous != null) ? new OpCode?(previous.OpCode) : ((OpCode?)null));
					OpCode call = OpCodes.Call;
					if (val8.HasValue && val8.GetValueOrDefault() == call)
					{
						object operand = i.Previous.Operand;
						GenericInstanceMethod val9 = (GenericInstanceMethod)((operand is GenericInstanceMethod) ? operand : null);
						if (val9 != null && ((MemberReference)((MemberReference)val9).DeclaringType).FullName == "UnityEngine.Object" && ((MemberReference)val9).Name == "Instantiate" && val9.GenericArguments.Count == 1 && ((MemberReference)val9.GenericArguments[0]).FullName == "RoR2.SkinDef")
						{
							Instruction previous2 = i.Previous.Previous;
							val8 = ((previous2 != null) ? new OpCode?(previous2.OpCode) : ((OpCode?)null));
							call = OpCodes.Ldelem_Ref;
							if (val8.HasValue && val8.GetValueOrDefault() == call)
							{
								Instruction previous3 = i.Previous.Previous.Previous;
								return Field((previous3 != null) ? previous3.Previous : null, "RoR2.SkinDef[] RoR2.ModelSkinController::skins");
							}
						}
					}
				}
				return false;
			}).ToArray();
			if (array2.Length != 1)
			{
				throw new InvalidOperationException("Cannot prove clone source");
			}
			Instruction val3 = array2[0];
			VariableDefinition sourceIndex = Local(method, val3.Previous.Previous.Previous);
			VariableDefinition body = Local(method, val3.Previous.Previous.Previous.Previous.Previous);
			if (!Typed(body, "RoR2.ModelSkinController") || !Typed(sourceIndex, "System.Int32"))
			{
				throw new InvalidOperationException("Cannot prove body/index types");
			}
			Instruction[] array3 = ((IEnumerable<Instruction>)method.Body.Instructions).Where((Instruction i) => i.OpCode == OpCodes.Stelem_Ref && Loads(method, i.Previous, clone) && Loads(method, i.Previous.Previous, sourceIndex) && Typed(Local(method, i.Previous.Previous.Previous), "RoR2.SkinDef[]")).ToArray();
			if (array3.Length != 1)
			{
				throw new InvalidOperationException("Cannot prove ordered clone slot");
			}
			Instruction val4 = array3[0];
			VariableDefinition ordered = Local(method, val4.Previous.Previous.Previous);
			Instruction[] array4 = ((IEnumerable<Instruction>)method.Body.Instructions).Where(delegate(Instruction i)
			{
				//IL_002d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Unknown result type (might be due to invalid IL or missing references)
				//IL_003d: Unknown result type (might be due to invalid IL or missing references)
				//IL_004c: 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)
				//IL_00a2: 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_00c1: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
				//IL_0100: Unknown result type (might be due to invalid IL or missing references)
				//IL_010f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0114: Unknown result type (might be due to invalid IL or missing references)
				if (Local(method, i, store: true) == ordered)
				{
					Instruction previous = i.Previous;
					OpCode? val8 = ((previous != null) ? new OpCode?(previous.OpCode) : ((OpCode?)null));
					OpCode newarr = OpCodes.Newarr;
					if (val8.HasValue && val8.GetValueOrDefault() == newarr)
					{
						object operand = i.Previous.Operand;
						TypeReference val9 = (TypeReference)((operand is TypeReference) ? operand : null);
						if (val9 != null && ((MemberReference)val9).FullName == "RoR2.SkinDef")
						{
							Instruction previous2 = i.Previous.Previous;
							val8 = ((previous2 != null) ? new OpCode?(previous2.OpCode) : ((OpCode?)null));
							newarr = OpCodes.Conv_I4;
							if (val8.HasValue && val8.GetValueOrDefault() == newarr)
							{
								Instruction previous3 = i.Previous.Previous.Previous;
								val8 = ((previous3 != null) ? new OpCode?(previous3.OpCode) : ((OpCode?)null));
								newarr = OpCodes.Ldlen;
								if (val8.HasValue && val8.GetValueOrDefault() == newarr && Field(i.Previous.Previous.Previous.Previous, "RoR2.SkinDef[] RoR2.ModelSkinController::skins"))
								{
									return Loads(method, i.Previous.Previous.Previous.Previous.Previous, body);
								}
							}
						}
					}
				}
				return false;
			}).ToArray();
			if (array4.Length != 1)
			{
				throw new InvalidOperationException("Cannot prove ordered array allocation");
			}
			Instruction[] array5 = ((IEnumerable<Instruction>)method.Body.Instructions).Where(delegate(Instruction i)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				if (i.OpCode == OpCodes.Stfld)
				{
					object operand = i.Operand;
					FieldReference val8 = (FieldReference)((operand is FieldReference) ? operand : null);
					if (val8 != null && ((MemberReference)val8).FullName == "RoR2.SkinDef[] RoR2.ModelSkinController::skins" && Loads(method, i.Previous, ordered))
					{
						return Typed(Local(method, i.Previous.Previous), "RoR2.ModelSkinController");
					}
				}
				return false;
			}).ToArray();
			if (array5.Length != 1)
			{
				throw new InvalidOperationException("Cannot prove final display publication");
			}
			Instruction val5 = array5[0];
			VariableDefinition val6 = Local(method, val5.Previous.Previous);
			if (val6 == body)
			{
				throw new InvalidOperationException("Refusing to publish to body controller");
			}
			Collection<Instruction> instructions = method.Body.Instructions;
			if (instructions.IndexOf(array4[0]) >= instructions.IndexOf(val3) || instructions.IndexOf(val3) >= instructions.IndexOf(val) || instructions.IndexOf(val) >= instructions.IndexOf(val4) || instructions.IndexOf(val4) >= instructions.IndexOf(val5))
			{
				throw new InvalidOperationException("Unexpected conversion data-flow order");
			}
			Enumerator<Instruction> enumerator = instructions.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Instruction current = enumerator.Current;
					int num = instructions.IndexOf(current);
					VariableDefinition val7 = Local(method, current, store: true);
					if (num > instructions.IndexOf(array4[0]) && num < instructions.IndexOf(val5) && (val7 == body || val7 == ordered))
					{
						throw new InvalidOperationException("Conversion context reassigned before publication");
					}
					if (num > instructions.IndexOf(val3) && num < instructions.IndexOf(val4) && (val7 == sourceIndex || val7 == clone))
					{
						throw new InvalidOperationException("Clone/index reassigned before publication");
					}
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			if (BranchesTo(method, val) || BranchesTo(method, val5.Previous.Previous) || BranchesTo(method, val5.Previous) || BranchesTo(method, val5))
			{
				throw new InvalidOperationException("Unexpected branch into capture/publication sequence");
			}
			return new Plan
			{
				Store = val,
				PublishStart = val5.Previous.Previous,
				Body = body,
				Display = val6,
				Ordered = ordered,
				SourceIndex = sourceIndex,
				Clone = clone,
				Fallback = val2
			};
		}

		public static void Rewrite(MethodDefinition method, MethodReference capture, MethodReference finalize)
		{
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			Plan plan = FindPlan(method);
			if (!Signature(capture, new string[7] { "RoR2.SkinDef[]", "System.Int32", "RoR2.SkinDef", "RoR2.ModelSkinController", "RoR2.SkinDef[]", "System.Int32", "RoR2.SkinDef" }) || !Signature(finalize, new string[2] { "RoR2.ModelSkinController", "RoR2.SkinDef[]" }))
			{
				throw new InvalidOperationException("Unexpected color helper signature");
			}
			ILProcessor iLProcessor = method.Body.GetILProcessor();
			VariableDefinition[] array = (VariableDefinition[])(object)new VariableDefinition[4] { plan.Body, plan.Ordered, plan.SourceIndex, plan.Clone };
			foreach (VariableDefinition val in array)
			{
				iLProcessor.InsertBefore(plan.Store, Instruction.Create(OpCodes.Ldloc, val));
			}
			plan.Store.OpCode = OpCodes.Call;
			plan.Store.Operand = capture;
			array = (VariableDefinition[])(object)new VariableDefinition[2] { plan.Body, plan.Ordered };
			foreach (VariableDefinition val2 in array)
			{
				iLProcessor.InsertBefore(plan.PublishStart, Instruction.Create(OpCodes.Ldloc, val2));
			}
			iLProcessor.InsertBefore(plan.PublishStart, Instruction.Create(OpCodes.Call, finalize));
			method.Body.MaxStackSize = Math.Max(method.Body.MaxStackSize, 7);
			static bool Signature(MethodReference m, params string[] types)
			{
				if (m != null && !m.HasThis && ((MemberReference)m.ReturnType).FullName == "System.Void")
				{
					return ((IEnumerable<ParameterDefinition>)m.Parameters).Select((ParameterDefinition p) => ((MemberReference)((ParameterReference)p).ParameterType).FullName).SequenceEqual(types);
				}
				return false;
			}
		}
	}
	public static class PatchRules
	{
		private const string Inequality = "System.Boolean UnityEngine.Object::op_Inequality(UnityEngine.Object,UnityEngine.Object)";

		public static Instruction[] FindSites(MethodDefinition method, string owner, int expected)
		{
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			if (!method.HasBody || ((MemberReference)((MethodReference)method).ReturnType).FullName != "System.Void" || ((MethodReference)method).Parameters.Count != 3 || ((MemberReference)((ParameterReference)((MethodReference)method).Parameters[1]).ParameterType).FullName != "RoR2.SkinDef" || ((MemberReference)((ParameterReference)((MethodReference)method).Parameters[2]).ParameterType).FullName != "UnityEngine.GameObject")
			{
				throw new InvalidOperationException("Callback signature does not match audited target");
			}
			Instruction[] array = ((IEnumerable<Instruction>)method.Body.Instructions).Where(delegate(Instruction i)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				if (i.OpCode == OpCodes.Call)
				{
					object operand = i.Operand;
					MethodReference val3 = (MethodReference)((operand is MethodReference) ? operand : null);
					if (val3 != null)
					{
						return ((MemberReference)val3).FullName == "System.Boolean UnityEngine.Object::op_Inequality(UnityEngine.Object,UnityEngine.Object)";
					}
				}
				return false;
			}).ToArray();
			if (array.Length != expected)
			{
				throw new InvalidOperationException("Unexpected identity comparison count: " + array.Length);
			}
			Instruction[] array2 = array;
			foreach (Instruction obj in array2)
			{
				Instruction previous = obj.Previous;
				object obj2 = ((previous != null) ? previous.Operand : null);
				MethodReference val = (MethodReference)((obj2 is MethodReference) ? obj2 : null);
				Instruction previous2 = obj.Previous;
				OpCode? val2 = ((previous2 != null) ? new OpCode?(previous2.OpCode) : ((OpCode?)null));
				OpCode call = OpCodes.Call;
				if (!val2.HasValue || val2.GetValueOrDefault() != call || val == null || ((MemberReference)((MemberReference)val).DeclaringType).FullName != owner || ((MemberReference)val.ReturnType).FullName != "RoR2.SkinDef" || val.HasThis || val.Parameters.Count != 0 || !((MemberReference)val).Name.StartsWith("get_SkinDef", StringComparison.Ordinal) || !int.TryParse(((MemberReference)val).Name.Substring(11), out var result) || result < 1)
				{
					throw new InvalidOperationException("Unexpected registered-skin getter");
				}
			}
			return array;
		}
	}
	[BepInPlugin("local.skin.clonecompatibility", "Skin Clone Compatibility", "0.2.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		private sealed class ColorConversion
		{
			public ModelSkinController Body;

			public SkinDef[] BodySkins;

			public DeferredBaseMapping<SkinDef> Mapping;
		}

		private readonly ConcurrentQueue<Assembly> pending = new ConcurrentQueue<Assembly>();

		private readonly HashSet<Assembly> inspected = new HashSet<Assembly>();

		private readonly List<ILHook> hooks = new List<ILHook>();

		private readonly HashSet<string> accepted = new HashSet<string>();

		private readonly Dictionary<SkinDef[], ColorConversion> colorConversions = new Dictionary<SkinDef[], ColorConversion>();

		private string colorPatchStatus = "waiting for R2API.Skins";

		private static Plugin instance;

		private void Awake()
		{
			instance = this;
			AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly item in assemblies)
			{
				pending.Enqueue(item);
			}
			Drain();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Initialization complete; color patch status=" + colorPatchStatus + ". Model PATCH records are separate; visual results require verification."));
		}

		private void OnAssemblyLoad(object sender, AssemblyLoadEventArgs e)
		{
			pending.Enqueue(e.LoadedAssembly);
		}

		private void Update()
		{
			Drain();
		}

		private void Drain()
		{
			while (true)
			{
				if (!pending.TryDequeue(out var assembly))
				{
					break;
				}
				if (!inspected.Add(assembly) || assembly.IsDynamic)
				{
					continue;
				}
				if (assembly.GetName().Name == "R2API.Skins")
				{
					InstallDisplayBaseFix(assembly);
				}
				Target[] array = Targets.All.Where((Target t) => t.Assembly == assembly.GetName().Name).ToArray();
				if (array.Length == 0)
				{
					continue;
				}
				try
				{
					string hash;
					using (SHA256 sHA = SHA256.Create())
					{
						using FileStream inputStream = File.OpenRead(assembly.Location);
						hash = BitConverter.ToString(sHA.ComputeHash(inputStream)).Replace("-", "");
					}
					if (array.Any((Target t) => t.Sha256 != hash))
					{
						((BaseUnityPlugin)this).Logger.LogWarning((object)("SKIP " + assembly.GetName().Name + " SHA256=" + hash + "; this build has not been audited. Do not combine with the earlier replacement Commando DLL."));
					}
					else
					{
						Target[] array2 = array;
						foreach (Target target in array2)
						{
							Install(assembly, target);
						}
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Could not inspect " + assembly.GetName().Name + ": " + ex));
				}
			}
		}

		private void InstallDisplayBaseFix(Assembly assembly)
		{
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Expected O, but got Unknown
			ILHook val = null;
			try
			{
				string text;
				using (SHA256 sHA = SHA256.Create())
				{
					using FileStream inputStream = File.OpenRead(assembly.Location);
					text = BitConverter.ToString(sHA.ComputeHash(inputStream)).Replace("-", "");
				}
				ColorAdapter adapter = ColorAdapterSelector.Select(assembly.GetName().Name, assembly.GetName().Version, text);
				MethodInfo? method = assembly.GetType("R2API.Skins", throwOnError: true).GetMethod("OnMainMenuInitialized", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (method == null)
				{
					throw new MissingMethodException("R2API.Skins.OnMainMenuInitialized");
				}
				val = new ILHook((MethodBase)method, (Manipulator)delegate(ILContext il)
				{
					if (adapter == ColorAdapter.Skins1411)
					{
						DisplayBasePatchRules.Rewrite(il.Method, ((MemberReference)il.Method).Module.ImportReference((MethodBase)typeof(Plugin).GetMethod("StoreDisplayBase", BindingFlags.Static | BindingFlags.Public)));
					}
					else
					{
						DisplayBasePatchRules1414.Rewrite(il.Method, ((MemberReference)il.Method).Module.ImportReference((MethodBase)typeof(Plugin).GetMethod("CaptureDisplayBase1414", BindingFlags.Static | BindingFlags.Public)), ((MemberReference)il.Method).Module.ImportReference((MethodBase)typeof(Plugin).GetMethod("FinalizeDisplayBases1414", BindingFlags.Static | BindingFlags.Public)));
					}
				});
				hooks.Add(val);
				colorPatchStatus = "installed " + assembly.GetName().Version;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("COLOR PATCH INSTALLED R2API.Skins::OnMainMenuInitialized version=" + assembly.GetName().Version?.ToString() + " SHA256=" + text + " adapter=" + adapter));
			}
			catch (Exception ex)
			{
				if (val != null)
				{
					val.Dispose();
				}
				colorPatchStatus = "FAILED";
				((BaseUnityPlugin)this).Logger.LogError((object)("COLOR PATCH FAILED (SKIP display base-skin fix): " + ex));
			}
		}

		public static void StoreDisplayBase(SkinDef[] bases, int slot, SkinDef fallback, ModelSkinController body, ModelSkinController display, int sourceIndex)
		{
			SkinDef val = fallback;
			SkinDef val2 = null;
			SkinDef val3 = null;
			if (Object.op_Implicit((Object)(object)body) && Object.op_Implicit((Object)(object)display) && body.skins != null && display.skins != null && sourceIndex >= 0 && sourceIndex < body.skins.Length)
			{
				val2 = body.skins[sourceIndex];
				if (Object.op_Implicit((Object)(object)val2) && val2.baseSkins != null && slot >= 0 && slot < val2.baseSkins.Length)
				{
					val3 = val2.baseSkins[slot];
					int num = (Object.op_Implicit((Object)(object)val3) ? Array.IndexOf(body.skins, val3) : (-1));
					if (num >= 0 && num < display.skins.Length)
					{
						SkinDef val4 = display.skins[num];
						if (Object.op_Implicit((Object)(object)val4) && ((Object)(object)val4 == (Object)(object)val3 || BaseSkinRule.Matches(((Object)val3).name, val3.nameToken, ((Object)val4).name, val4.nameToken)))
						{
							val = val4;
						}
					}
				}
			}
			bases[slot] = val;
			Plugin plugin = instance;
			if ((Object)(object)plugin != (Object)null && (Object)(object)val != (Object)(object)fallback)
			{
				((BaseUnityPlugin)plugin).Logger.LogInfo((object)("BASE INHERIT skin=" + ((Object)val2).name + " intended=" + ((Object)val3).name + " fallback=" + (Object.op_Implicit((Object)(object)fallback) ? ((Object)fallback).name : "<null>") + " mapped=" + ((Object)val).name));
			}
			else if ((Object)(object)plugin != (Object)null && Object.op_Implicit((Object)(object)val3) && (Object)(object)val3 != (Object)(object)fallback && !BaseSkinRule.Matches(((Object)val3).name, val3.nameToken, Object.op_Implicit((Object)(object)fallback) ? ((Object)fallback).name : null, Object.op_Implicit((Object)(object)fallback) ? fallback.nameToken : null))
			{
				((BaseUnityPlugin)plugin).Logger.LogWarning((object)("BASE MAP SKIP skin=" + ((Object)val2).name + " intended=" + ((Object)val3).name + "; keeping converter fallback"));
			}
		}

		private void Install(Assembly assembly, Target target)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			ILHook val = null;
			try
			{
				val = new ILHook((MethodBase)assembly.GetType(target.Type, throwOnError: true).GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).Single((MethodInfo m) => m.Name == target.Method && m.GetParameters().Length == 3), (Manipulator)delegate(ILContext il)
				{
					Instruction[] array = PatchRules.FindSites(il.Method, target.Type, target.Count);
					MethodReference operand = ((MemberReference)il.Method).Module.ImportReference((MethodBase)typeof(Plugin).GetMethod("AreDifferentSkins", BindingFlags.Static | BindingFlags.Public));
					Instruction[] array2 = array;
					for (int i = 0; i < array2.Length; i++)
					{
						array2[i].Operand = operand;
					}
				});
				hooks.Add(val);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("PATCH " + target.Type + "::" + target.Method + " comparisons=" + target.Count));
			}
			catch (Exception ex)
			{
				if (val != null)
				{
					val.Dispose();
				}
				((BaseUnityPlugin)this).Logger.LogError((object)("SKIP " + target.Type + "::" + target.Method + ": " + ex));
			}
		}

		public static bool AreDifferentSkins(Object candidate, Object registered)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected I4, but got Unknown
			//IL_005c: Expected I4, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected I4, but got Unknown
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected I4, but got Unknown
			if (candidate == registered)
			{
				return false;
			}
			if (!Object.op_Implicit(candidate) || !Object.op_Implicit(registered))
			{
				return true;
			}
			SkinDef val = (SkinDef)(object)((candidate is SkinDef) ? candidate : null);
			if (val != null)
			{
				SkinDef val2 = (SkinDef)(object)((registered is SkinDef) ? registered : null);
				if (val2 != null)
				{
					if (!CloneRule.Matches((int)val.skinIndex, (int)val2.skinIndex, val.nameToken, val2.nameToken, ((Object)val).name, ((Object)val2).name))
					{
						return true;
					}
					Plugin plugin = instance;
					if ((Object)(object)plugin != (Object)null && plugin.accepted.Add(val2.nameToken + "|" + (int)val2.skinIndex))
					{
						((BaseUnityPlugin)plugin).Logger.LogInfo((object)("CLONE MATCH index=" + (int)val2.skinIndex + " registered=" + ((Object)val2).name + " candidate=" + ((Object)val).name + " token=" + val2.nameToken));
					}
					return false;
				}
			}
			return true;
		}

		private static string SkinName(SkinDef skin)
		{
			if (!Object.op_Implicit((Object)(object)skin))
			{
				return "<null>";
			}
			return ((Object)skin).name;
		}

		private static bool MatchingDisplaySkin(SkinDef original, SkinDef display)
		{
			if (Object.op_Implicit((Object)(object)original) && Object.op_Implicit((Object)(object)display))
			{
				if (!((Object)(object)original == (Object)(object)display))
				{
					return BaseSkinRule.Matches(((Object)original).name, original.nameToken, ((Object)display).name, display.nameToken);
				}
				return true;
			}
			return false;
		}

		public static void CaptureDisplayBase1414(SkinDef[] bases, int slot, SkinDef fallback, ModelSkinController body, SkinDef[] ordered, int sourceIndex, SkinDef clone)
		{
			Plugin plugin = instance;
			try
			{
				if ((Object)(object)plugin == (Object)null || !Object.op_Implicit((Object)(object)body) || body.skins == null || ordered == null)
				{
					throw new InvalidOperationException("Missing conversion context");
				}
				if (!plugin.colorConversions.TryGetValue(ordered, out var value))
				{
					value = new ColorConversion
					{
						Body = body,
						BodySkins = body.skins,
						Mapping = new DeferredBaseMapping<SkinDef>(body.skins, ordered, (SkinDef skin) => Object.op_Implicit((Object)(object)skin), MatchingDisplaySkin, (SkinDef skin) => skin.baseSkins, delegate(SkinDef skin, SkinDef[] baseSkins)
						{
							skin.baseSkins = baseSkins;
						})
					};
					plugin.colorConversions.Add(ordered, value);
				}
				if ((Object)(object)value.Body != (Object)(object)body || value.BodySkins != body.skins)
				{
					throw new InvalidOperationException("Conversion body context changed");
				}
				value.Mapping.Capture(sourceIndex, clone, bases, slot, fallback);
				((BaseUnityPlugin)plugin).Logger.LogDebug((object)("COLOR MAP DEFERRED skin=" + SkinName(clone) + " sourceIndex=" + sourceIndex));
			}
			catch (Exception ex)
			{
				if (Object.op_Implicit((Object)(object)clone) && Object.op_Implicit((Object)(object)body) && body.skins != null && !body.skins.Any((SkinDef skin) => (Object)(object)skin == (Object)(object)clone) && bases != null && slot >= 0 && slot < bases.Length)
				{
					SkinDef[] array = (SkinDef[])bases.Clone();
					array[slot] = fallback;
					clone.baseSkins = array;
				}
				if (plugin != null)
				{
					((BaseUnityPlugin)plugin).Logger.LogError((object)("COLOR MAP FALLBACK capture skin=" + SkinName(clone) + " reason=" + ex));
				}
			}
		}

		public static void FinalizeDisplayBases1414(ModelSkinController body, SkinDef[] ordered)
		{
			Plugin plugin = instance;
			if ((Object)(object)plugin == (Object)null || ordered == null || !plugin.colorConversions.TryGetValue(ordered, out var value))
			{
				return;
			}
			plugin.colorConversions.Remove(ordered);
			try
			{
				if (!Object.op_Implicit((Object)(object)body) || (Object)(object)value.Body != (Object)(object)body || value.BodySkins != body.skins || body.skins.Length != ordered.Length)
				{
					throw new InvalidOperationException("Body/ordered context changed before publication");
				}
				foreach (DeferredBaseMapping<SkinDef>.Outcome item in value.Mapping.Complete())
				{
					string text = "skin=" + SkinName(item.Source) + " intended=" + SkinName(item.Intended) + " mapped=" + SkinName(item.Mapped) + " fallback=" + SkinName(item.Fallback) + " reason=" + item.Reason;
					if (!item.Applied && item.Reason != "no-declared-base")
					{
						((BaseUnityPlugin)plugin).Logger.LogWarning((object)("COLOR MAP FALLBACK " + text));
					}
					else if ((Object)(object)item.Mapped != (Object)(object)item.Fallback)
					{
						((BaseUnityPlugin)plugin).Logger.LogInfo((object)("COLOR MAP RESTORED " + text));
					}
					else
					{
						((BaseUnityPlugin)plugin).Logger.LogDebug((object)("COLOR MAP VERIFIED " + text));
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)plugin).Logger.LogError((object)("COLOR MAP FALLBACK finalize: " + ex));
			}
		}

		private void OnDestroy()
		{
			AppDomain.CurrentDomain.AssemblyLoad -= OnAssemblyLoad;
			foreach (ILHook hook in hooks)
			{
				hook.Dispose();
			}
			hooks.Clear();
			colorConversions.Clear();
			if (instance == this)
			{
				instance = null;
			}
		}
	}
	public sealed class Target
	{
		public readonly string Assembly;

		public readonly string Type;

		public readonly string Method;

		public readonly string Sha256;

		public readonly int Count;

		public Target(string assembly, string type, string method, int count, string sha256)
		{
			Assembly = assembly;
			Type = type;
			Method = method;
			Count = count;
			Sha256 = sha256;
		}
	}
	internal static class Targets
	{
		internal static readonly Target[] All = new Target[16]
		{
			new Target("FemmeBandit12G", "FemmeBandit12G.FemmeBandit12GPlugin", "SkinDefApply", 4, "826FE2BEF819E7875F3F2AB63DAFA4B85FEB5C7E97F85E68F645BEF2705E578D"),
			new Target("RisqueFemmeBandit12G", "RisqueFemmeBandit12G.RisqueFemmeBandit12GPlugin", "SkinDefApply", 1, "2BE48FF084699FE71E6C80A0B842FE47422A975831538F65332B54F988364E26"),
			new Target("RisqueFemCaptain12G", "RisqueFemCaptain12G.RisqueFemCaptain12GPlugin", "SkinDefApply", 2, "76264ED38A24D4AEE018AB86A526C71D1B166B3B50B72D801D1EC4DCA34B9BB2"),
			new Target("RisqueFemCommando12G", "RisqueFemCommando12G.RisqueFemCommando12GPlugin", "SkinDefApply", 10, "47AC0CD310E381FEEA3BA2F63DF6C83F0A2B24D5AAEFF7208B7DD1A44E38E480"),
			new Target("RisqueFemMerc12G", "RisqueFemMerc12G.RisqueFemMerc12GPlugin", "SkinDefApply", 12, "FC767B33193E12F74A934115ECADEAB0CB1D2E45E10F58A3ECE46D5B418100A7"),
			new Target("RisqueHuntress12G", "RisqueHuntress12G.RisqueHuntress12GPlugin", "SkinDefApply", 4, "5A7B624518CB713B24A78DCC92F10F4D3F6965C6DC45AA1CE10A509FA1788E95"),
			new Target("RisqueRailgunner12G", "RisqueRailgunner12G.RisqueRailgunner12GPlugin", "SkinDefApply", 2, "FED86B990F0767C717056BECBED61879EF656B9F19D40641190538B4B129EC58"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply1", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply2", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply3", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply4", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply5", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply6", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply7", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("ThiccArti12G", "ThiccArti12G.ThiccArti12GPlugin", "SkinDefApply8", 8, "70A2382B49F32DC21FBE8C5E9FA17DD288BCA0E98EBD2BFE25BE9F595D6F5686"),
			new Target("NSFWHuntress", "NSFWHuntress.NSFWHuntressPlugin", "SkinDefApply", 2, "FF87DCC0432D75A88F403CCB14CC3F51274C05D29826418B2EA553494CA1B949")
		};
	}
}