Decompiled source of FF7 Customers v0.5.0

FF7Customers.dll

Decompiled 2 hours ago
using System;
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 BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using StarterAssets;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.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 FF7Customers
{
	internal static class BipedAvatar
	{
		private static readonly Dictionary<string, string> Map = BuildMap();

		private static readonly Dictionary<(Mesh, string), Avatar> Cache = new Dictionary<(Mesh, string), Avatar>();

		private static Dictionary<string, string> BuildMap()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>
			{
				{ "Hips", "Bip01 Pelvis" },
				{ "Spine", "Bip01 Spine" },
				{ "Chest", "Bip01 Spine1" },
				{ "UpperChest", "Bip01 Spine2" },
				{ "Neck", "Bip01 Neck" },
				{ "Head", "Bip01 Head" }
			};
			(string, string)[] array = new(string, string)[2]
			{
				("L", "Left"),
				("R", "Right")
			};
			for (int i = 0; i < array.Length; i++)
			{
				var (text, text2) = array[i];
				dictionary[text2 + "Shoulder"] = "Bip01 " + text + " Clavicle";
				dictionary[text2 + "UpperArm"] = "Bip01 " + text + " UpperArm";
				dictionary[text2 + "LowerArm"] = "Bip01 " + text + " Forearm";
				dictionary[text2 + "Hand"] = "Bip01 " + text + " Hand";
				dictionary[text2 + "UpperLeg"] = "Bip01 " + text + " Thigh";
				dictionary[text2 + "LowerLeg"] = "Bip01 " + text + " Calf";
				dictionary[text2 + "Foot"] = "Bip01 " + text + " Foot";
				dictionary[text2 + "Toes"] = "Bip01 " + text + " Toe0";
				string[] array2 = new string[5] { "Thumb", "Index", "Middle", "Ring", "Little" };
				for (int j = 0; j < 5; j++)
				{
					dictionary[text2 + " " + array2[j] + " Proximal"] = $"Bip01 {text} Finger{j}";
					dictionary[text2 + " " + array2[j] + " Intermediate"] = $"Bip01 {text} Finger{j}1";
				}
			}
			return dictionary;
		}

		internal static Avatar For(GameObject root)
		{
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_0295: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0325: Unknown result type (might be due to invalid IL or missing references)
			//IL_0351: Unknown result type (might be due to invalid IL or missing references)
			//IL_0356: Unknown result type (might be due to invalid IL or missing references)
			//IL_0433: Unknown result type (might be due to invalid IL or missing references)
			//IL_0441: Unknown result type (might be due to invalid IL or missing references)
			//IL_044d: Unknown result type (might be due to invalid IL or missing references)
			//IL_046b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0384: Unknown result type (might be due to invalid IL or missing references)
			//IL_038b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0390: Unknown result type (might be due to invalid IL or missing references)
			//IL_0395: Unknown result type (might be due to invalid IL or missing references)
			//IL_0397: Unknown result type (might be due to invalid IL or missing references)
			//IL_039e: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0521: Unknown result type (might be due to invalid IL or missing references)
			//IL_0523: Unknown result type (might be due to invalid IL or missing references)
			//IL_0526: Unknown result type (might be due to invalid IL or missing references)
			SkinnedMeshRenderer val = ((IEnumerable<SkinnedMeshRenderer>)root.GetComponentsInChildren<SkinnedMeshRenderer>(true)).FirstOrDefault((Func<SkinnedMeshRenderer, bool>)((SkinnedMeshRenderer r) => (Object)(object)r.sharedMesh != (Object)null && r.bones.Any((Transform b) => (Object)(object)b != (Object)null && ((Object)b).name == "Bip01 Pelvis")));
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			(Mesh, string) key = (val.sharedMesh, ((Object)root).name);
			if (Cache.TryGetValue(key, out var value))
			{
				return value;
			}
			Dictionary<Transform, Matrix4x4> dictionary = new Dictionary<Transform, Matrix4x4>();
			Matrix4x4[] bindposes = val.sharedMesh.bindposes;
			for (int num = 0; num < val.bones.Length && num < bindposes.Length; num++)
			{
				if ((Object)(object)val.bones[num] != (Object)null)
				{
					dictionary[val.bones[num]] = ((Component)val).transform.localToWorldMatrix * ((Matrix4x4)(ref bindposes[num])).inverse;
				}
			}
			Transform[] all = root.GetComponentsInChildren<Transform>(true);
			HashSet<string> names = new HashSet<string>(all.Select((Transform t) => ((Object)t).name));
			(Vector3, Quaternion, Vector3)[] array = all.Select((Transform t) => (localPosition: t.localPosition, localRotation: t.localRotation, localScale: t.localScale)).ToArray();
			Transform[] array2 = all;
			foreach (Transform val2 in array2)
			{
				if ((Object)(object)val2 != (Object)(object)root.transform && dictionary.TryGetValue(val2, out var value2))
				{
					val2.SetPositionAndRotation(Vector4.op_Implicit(((Matrix4x4)(ref value2)).GetColumn(3)), Quaternion.LookRotation(Vector4.op_Implicit(((Matrix4x4)(ref value2)).GetColumn(2)), Vector4.op_Implicit(((Matrix4x4)(ref value2)).GetColumn(1))));
				}
			}
			(string, float)[] array3 = new(string, float)[2]
			{
				("L", -1f),
				("R", 1f)
			};
			for (int num2 = 0; num2 < array3.Length; num2++)
			{
				(string, float) tuple = array3[num2];
				string item = tuple.Item1;
				float item2 = tuple.Item2;
				Vector3 item3 = root.transform.TransformDirection(new Vector3(item2, 0f, 0f));
				Vector3 item4 = -root.transform.up;
				(string, string, Vector3)[] array4 = new(string, string, Vector3)[5]
				{
					("Bip01 " + item + " UpperArm", "Bip01 " + item + " Forearm", item3),
					("Bip01 " + item + " Forearm", "Bip01 " + item + " Hand", item3),
					("Bip01 " + item + " Hand", "Bip01 " + item + " Finger2", item3),
					("Bip01 " + item + " Thigh", "Bip01 " + item + " Calf", item4),
					("Bip01 " + item + " Calf", "Bip01 " + item + " Foot", item4)
				};
				for (int num3 = 0; num3 < array4.Length; num3++)
				{
					(string, string, Vector3) tuple2 = array4[num3];
					string item5 = tuple2.Item1;
					string item6 = tuple2.Item2;
					Vector3 item7 = tuple2.Item3;
					Transform val3 = Find(item5);
					Transform val4 = Find(item6);
					if (!((Object)(object)val3 == (Object)null) && !((Object)(object)val4 == (Object)null))
					{
						val3.rotation = Quaternion.FromToRotation(val4.position - val3.position, item7) * val3.rotation;
					}
				}
			}
			List<SkeletonBone> list = ((IEnumerable<Transform>)all).Select((Func<Transform, SkeletonBone>)((Transform t) => new SkeletonBone
			{
				name = ((Object)t).name,
				position = t.localPosition,
				rotation = t.localRotation,
				scale = t.localScale
			})).ToList();
			for (int num4 = 0; num4 < all.Length; num4++)
			{
				Transform val5 = all[num4];
				Transform val6 = all[num4];
				Transform obj = all[num4];
				(Vector3, Quaternion, Vector3) tuple3 = array[num4];
				val5.localPosition = tuple3.Item1;
				val6.localRotation = tuple3.Item2;
				obj.localScale = tuple3.Item3;
			}
			HumanDescription val7 = new HumanDescription
			{
				human = Map.Where((KeyValuePair<string, string> kv) => names.Contains(kv.Value)).Select(delegate(KeyValuePair<string, string> kv)
				{
					//IL_0002: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: 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_0037: Unknown result type (might be due to invalid IL or missing references)
					//IL_003c: Unknown result type (might be due to invalid IL or missing references)
					HumanBone result = default(HumanBone);
					((HumanBone)(ref result)).humanName = kv.Key;
					((HumanBone)(ref result)).boneName = kv.Value;
					HumanLimit limit = default(HumanLimit);
					((HumanLimit)(ref limit)).useDefaultValues = true;
					result.limit = limit;
					return result;
				}).ToArray(),
				skeleton = list.ToArray()
			};
			((HumanDescription)(ref val7)).upperArmTwist = 0.5f;
			((HumanDescription)(ref val7)).lowerArmTwist = 0.5f;
			((HumanDescription)(ref val7)).upperLegTwist = 0.5f;
			((HumanDescription)(ref val7)).lowerLegTwist = 0.5f;
			((HumanDescription)(ref val7)).armStretch = 0.05f;
			((HumanDescription)(ref val7)).legStretch = 0.05f;
			((HumanDescription)(ref val7)).feetSpacing = 0f;
			((HumanDescription)(ref val7)).hasTranslationDoF = false;
			HumanDescription val8 = val7;
			Avatar val9 = AvatarBuilder.BuildHumanAvatar(root, val8);
			if ((Object)(object)val9 == (Object)null || !val9.isValid || !val9.isHuman)
			{
				val9 = null;
			}
			Cache[key] = val9;
			return val9;
			Transform Find(string n)
			{
				return ((IEnumerable<Transform>)all).FirstOrDefault((Func<Transform, bool>)((Transform t) => ((Object)t).name == n));
			}
		}
	}
	[BepInPlugin("local.ff7customers", "FF7 Customers", "0.5.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private const string GenshinGuid = "GenshinImpactOverhaul";

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> ReplacePercent;

		internal static ConfigEntry<int> CrowdSize;

		internal static ConfigEntry<int> EmployeePercent;

		internal static ConfigEntry<string> PlayerModel;

		internal static ConfigEntry<int> VanillaCharacter;

		internal static ConfigEntry<KeyCode> CycleKey;

		private float nextSync;

		private float noteUntil;

		internal static readonly List<GameObject> Prefabs = new List<GameObject>();

		private void Awake()
		{
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Expected O, but got Unknown
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			ReplacePercent = ((BaseUnityPlugin)this).Config.Bind<int>("General", "ReplacePercent", 70, "Chance (0-100) that a customer is an FF7 character, if one of them is free. The rest use the Genshin Impact Overhaul if it is installed, otherwise the normal customers.");
			EmployeePercent = ((BaseUnityPlugin)this).Config.Bind<int>("General", "EmployeePercent", 70, "Chance (0-100) that an employee is an FF7 character, if one of them is free (employees count towards the one-of-each limit).");
			CrowdSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "CrowdSize", 25, "Only one of each FF7 character is in the store at a time, unless at least this many customers are in at once.");
			PlayerModel = ((BaseUnityPlugin)this).Config.Bind<string>("Player", "PlayerModel", "Normal", new ConfigDescription("Your own character model. Cycle in game with CycleKey.", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[4] { "Normal", "Aerith", "Cloud", "Tifa" }), Array.Empty<object>()));
			VanillaCharacter = ((BaseUnityPlugin)this).Config.Bind<int>("Player", "VanillaCharacter", 0, "Normal character to go back to (remembered automatically).");
			CycleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Player", "CycleKey", (KeyCode)289, "Key that cycles your player model.");
			string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "ff7customers");
			AssetBundle val = AssetBundle.LoadFromFile(text);
			if ((Object)(object)val == (Object)null)
			{
				Log.LogError((object)("Bundle not found: " + text));
				return;
			}
			GameObject[] array = val.LoadAllAssets<GameObject>();
			foreach (GameObject item in array)
			{
				Prefabs.Add(item);
			}
			Prefabs.Sort((GameObject a, GameObject b) => string.CompareOrdinal(((Object)a).name, ((Object)b).name));
			Log.LogInfo((object)("Loaded FF7 characters: " + string.Join(", ", Prefabs.ConvertAll((GameObject p) => ((Object)p).name))));
			Harmony val2 = new Harmony("local.ff7customers");
			val2.PatchAll();
			if (Chainloader.PluginInfos.ContainsKey("GenshinImpactOverhaul"))
			{
				MethodInfo methodInfo = AccessTools.Method("GenshinImpactOverhaulSupermarketTogether.NPCModification:ReplaceModel", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					val2.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(CustomerPatch), "SkipGenshin", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					Log.LogInfo((object)"Sharing customers with Genshin Impact Overhaul");
				}
				else
				{
					Log.LogWarning((object)"Genshin Impact Overhaul found but its ReplaceModel method wasn't; the two mods will overlap");
				}
			}
		}

		private int ModelIndex()
		{
			return Prefabs.FindIndex((GameObject p) => ((Object)p).name == PlayerModel.Value);
		}

		private void Update()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			if (Prefabs.Count == 0 || (Object)(object)FirstPersonController.Instance == (Object)null)
			{
				return;
			}
			PlayerNetwork component = ((Component)FirstPersonController.Instance).GetComponent<PlayerNetwork>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			int num = component.playerCharacter.Length;
			bool flag = PlayerSkins.Ff7Index(component.characterID, num) >= 0;
			if (Input.GetKeyDown(CycleKey.Value))
			{
				if (!flag)
				{
					VanillaCharacter.Value = component.characterID;
				}
				int num2 = ModelIndex() + 1;
				PlayerModel.Value = ((num2 >= Prefabs.Count) ? "Normal" : ((Object)Prefabs[num2]).name);
				noteUntil = Time.time + 2.5f;
				nextSync = 0f;
			}
			if (!(Time.time < nextSync))
			{
				nextSync = Time.time + 1f;
				int num3 = ModelIndex();
				if (num3 >= 0 && component.characterID != num + num3)
				{
					component.CmdChangeCharacter(num + num3);
				}
				else if (num3 < 0 && flag)
				{
					component.CmdChangeCharacter(Mathf.Clamp(VanillaCharacter.Value, 0, num - 1));
				}
			}
		}

		private void OnGUI()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			if (!(Time.time > noteUntil))
			{
				GUIStyle val = new GUIStyle(GUI.skin.label)
				{
					fontSize = 22,
					alignment = (TextAnchor)1
				};
				GUI.Label(new Rect(0f, 40f, (float)Screen.width, 40f), "Player model: " + PlayerModel.Value, val);
			}
		}
	}
	internal static class Skin
	{
		private static readonly Dictionary<(Material, Shader), Material> Converted = new Dictionary<(Material, Shader), Material>();

		private static bool loggedShader;

		private static bool loggedNpc;

		internal static Shader HideOriginal(GameObject original, bool keepProps = false, bool hideHats = false)
		{
			Shader val = null;
			Renderer[] componentsInChildren = original.GetComponentsInChildren<Renderer>();
			foreach (Renderer val2 in componentsInChildren)
			{
				if (!keepProps || val2 is SkinnedMeshRenderer || (hideHats && OnHead(((Component)val2).transform)))
				{
					if ((Object)(object)val == (Object)null && (Object)(object)val2.sharedMaterial != (Object)null)
					{
						val = val2.sharedMaterial.shader;
					}
					val2.enabled = false;
				}
			}
			if (!loggedShader && (Object)(object)val != (Object)null)
			{
				loggedShader = true;
				Plugin.Log.LogInfo((object)("Game character shader: " + ((Object)val).name));
			}
			return val;
		}

		private static bool OnHead(Transform t)
		{
			while ((Object)(object)t != (Object)null)
			{
				if (((Object)t).name == "Bip01 Head")
				{
					return true;
				}
				t = t.parent;
			}
			return false;
		}

		internal static GameObject Spawn(GameObject prefab, Transform parent, Shader shader)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = Object.Instantiate<GameObject>(prefab, parent);
			val.transform.localPosition = Vector3.zero;
			val.transform.localRotation = prefab.transform.localRotation;
			if ((Object)(object)shader == (Object)null)
			{
				return val;
			}
			Renderer[] componentsInChildren = val.GetComponentsInChildren<Renderer>();
			foreach (Renderer val2 in componentsInChildren)
			{
				Material[] sharedMaterials = val2.sharedMaterials;
				for (int j = 0; j < sharedMaterials.Length; j++)
				{
					if ((Object)(object)sharedMaterials[j] != (Object)null)
					{
						sharedMaterials[j] = Convert(sharedMaterials[j], shader);
					}
				}
				val2.sharedMaterials = sharedMaterials;
			}
			return val;
		}

		internal static void ReplaceNpc(Component owner, GameObject prefab, bool keepProps, bool hideHats = false)
		{
			//IL_00e6: 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)
			Traverse val = Traverse.Create((object)owner);
			GameObject value = val.Field("characterOBJ").GetValue<GameObject>();
			Animator value2 = val.Field("npcAnimator").GetValue<Animator>();
			if (!((Object)(object)value == (Object)null) && !((Object)(object)value2 == (Object)null))
			{
				Avatar val2 = (((Object)(object)((Component)value2).gameObject == (Object)(object)value) ? HumanAvatar(value2) : null);
				if (!loggedNpc)
				{
					loggedNpc = true;
					Plugin.Log.LogInfo((object)string.Format("NPC swap: {0}, game character scale {1}", ((Object)(object)val2 != (Object)null) ? "pose mirror" : "direct animator", value.transform.lossyScale));
				}
				if ((Object)(object)val2 != (Object)null)
				{
					ReplaceMirrored(value, prefab, shadowsOnly: false, keepProps, hideHats);
					return;
				}
				Animator component = Spawn(prefab, owner.transform, HideOriginal(value, keepProps, hideHats)).GetComponent<Animator>();
				component.runtimeAnimatorController = value2.runtimeAnimatorController;
				component.applyRootMotion = value2.applyRootMotion;
				component.cullingMode = value2.cullingMode;
				val.Field("npcAnimator").SetValue((object)component);
				((Behaviour)value2).enabled = false;
			}
		}

		internal static Avatar HumanAvatar(Animator a)
		{
			if ((Object)(object)a.avatar != (Object)null && a.avatar.isHuman)
			{
				return a.avatar;
			}
			return BipedAvatar.For(((Component)a).gameObject);
		}

		internal static void ReplaceMirrored(GameObject character, GameObject prefab, bool shadowsOnly, bool keepProps = false, bool hideHats = false)
		{
			Animator component = character.GetComponent<Animator>();
			Avatar val = (((Object)(object)component != (Object)null) ? HumanAvatar(component) : null);
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Log.LogWarning((object)("Game character isn't a readable humanoid; can't apply FF7 model to " + ((Object)character).name));
				return;
			}
			SkinnedMeshRenderer componentInChildren = character.GetComponentInChildren<SkinnedMeshRenderer>(true);
			int layer = ((componentInChildren != null) ? ((Component)componentInChildren).gameObject.layer : character.layer);
			GameObject val2 = Spawn(prefab, character.transform, HideOriginal(character, keepProps, hideHats));
			component.cullingMode = (AnimatorCullingMode)0;
			Transform[] componentsInChildren = val2.GetComponentsInChildren<Transform>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				((Component)componentsInChildren[i]).gameObject.layer = layer;
			}
			if (shadowsOnly)
			{
				Renderer[] componentsInChildren2 = val2.GetComponentsInChildren<Renderer>();
				for (int i = 0; i < componentsInChildren2.Length; i++)
				{
					componentsInChildren2[i].shadowCastingMode = (ShadowCastingMode)3;
				}
			}
			val2.AddComponent<PoseMirror>().Init(component, val, keepProps);
		}

		private static Material Convert(Material src, Shader shader)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			if (Converted.TryGetValue((src, shader), out var value))
			{
				return value;
			}
			value = new Material(shader)
			{
				name = ((Object)src).name
			};
			Texture mainTexture = src.mainTexture;
			string[] array = new string[2] { "_BaseMap", "_MainTex" };
			foreach (string text in array)
			{
				if (value.HasProperty(text))
				{
					value.SetTexture(text, mainTexture);
				}
			}
			array = new string[2] { "_BaseColor", "_Color" };
			foreach (string text2 in array)
			{
				if (value.HasProperty(text2))
				{
					value.SetColor(text2, Color.white);
				}
			}
			if (((Object)src).name.EndsWith("_cutout") && value.HasProperty("_Cutoff"))
			{
				value.SetFloat("_AlphaClip", 1f);
				value.SetFloat("_Cutoff", 0.5f);
				value.EnableKeyword("_ALPHATEST_ON");
				value.SetFloat("_Cull", 0f);
			}
			Converted[(src, shader)] = value;
			return value;
		}
	}
	internal class PoseMirror : MonoBehaviour
	{
		private Animator source;

		private Animator own;

		private HumanPoseHandler from;

		private HumanPoseHandler to;

		private HumanPose pose;

		private Transform srcLFoot;

		private Transform srcRFoot;

		private readonly Dictionary<Transform, (Transform srcBone, Transform ownBone, Vector3 local)> props = new Dictionary<Transform, (Transform, Transform, Vector3)>();

		private bool followProps;

		private float nextScan;

		private static readonly (string biped, HumanBodyBones bone)[] PropBones = new(string, HumanBodyBones)[3]
		{
			("Bip01 L Hand", (HumanBodyBones)17),
			("Bip01 R Hand", (HumanBodyBones)18),
			("Bip01 Head", (HumanBodyBones)10)
		};

		internal void Init(Animator src, Avatar srcAvatar, bool props)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			source = src;
			followProps = props;
			own = ((Component)this).GetComponent<Animator>();
			((Behaviour)own).enabled = false;
			from = new HumanPoseHandler(srcAvatar, ((Component)src).transform);
			to = new HumanPoseHandler(own.avatar, ((Component)this).transform);
			bool flag = (Object)(object)src.avatar != (Object)null && src.avatar.isHuman;
			srcLFoot = (flag ? src.GetBoneTransform((HumanBodyBones)5) : FindIn(((Component)src).transform, "Bip01 L Foot"));
			srcRFoot = (flag ? src.GetBoneTransform((HumanBodyBones)6) : FindIn(((Component)src).transform, "Bip01 R Foot"));
		}

		private static Transform FindIn(Transform root, string name)
		{
			Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
			foreach (Transform val in componentsInChildren)
			{
				if (((Object)val).name == name)
				{
					return val;
				}
			}
			return null;
		}

		private void LateUpdate()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)source == (Object)null))
			{
				from.GetHumanPose(ref pose);
				((Component)this).transform.localPosition = Vector3.zero;
				to.SetHumanPose(ref pose);
				if ((Object)(object)srcLFoot != (Object)null && (Object)(object)srcRFoot != (Object)null)
				{
					float num = Mathf.Min(own.GetBoneTransform((HumanBodyBones)5).position.y, own.GetBoneTransform((HumanBodyBones)6).position.y);
					Transform transform = ((Component)this).transform;
					transform.position += Vector3.up * (Mathf.Min(srcLFoot.position.y, srcRFoot.position.y) - num);
				}
				if (followProps)
				{
					MoveProps();
				}
			}
		}

		private void MoveProps()
		{
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			if (Time.time >= nextScan)
			{
				nextScan = Time.time + 1f;
				Renderer[] componentsInChildren = ((Component)source).GetComponentsInChildren<Renderer>(true);
				foreach (Renderer val in componentsInChildren)
				{
					if (val is SkinnedMeshRenderer || ((Component)val).transform.IsChildOf(((Component)this).transform) || props.ContainsKey(((Component)val).transform))
					{
						continue;
					}
					Transform p = ((Component)val).transform.parent;
					while ((Object)(object)p != (Object)null && (Object)(object)p != (Object)(object)((Component)source).transform)
					{
						int num = Array.FindIndex(PropBones, ((string biped, HumanBodyBones bone) b) => b.biped == ((Object)p).name);
						if (num >= 0)
						{
							props[((Component)val).transform] = (p, own.GetBoneTransform(PropBones[num].bone), ((Component)val).transform.localPosition);
							break;
						}
						p = p.parent;
					}
				}
			}
			foreach (KeyValuePair<Transform, (Transform, Transform, Vector3)> prop in props)
			{
				Transform key = prop.Key;
				if (!((Object)(object)key == (Object)null) && !((Object)(object)prop.Value.Item2 == (Object)null))
				{
					key.localPosition = prop.Value.Item3;
					key.position += prop.Value.Item2.position - prop.Value.Item1.position;
				}
			}
		}

		private void OnDestroy()
		{
			HumanPoseHandler obj = from;
			if (obj != null)
			{
				obj.Dispose();
			}
			HumanPoseHandler obj2 = to;
			if (obj2 != null)
			{
				obj2.Dispose();
			}
		}
	}
	internal class InPlay : MonoBehaviour
	{
		internal static readonly List<InPlay> All = new List<InPlay>();

		internal int Index;

		private void OnEnable()
		{
			All.Add(this);
		}

		private void OnDisable()
		{
			All.Remove(this);
		}

		internal static int Pick(uint start, bool always)
		{
			int count = Plugin.Prefabs.Count;
			int result = -1;
			int num = int.MaxValue;
			for (int i = 0; i < count; i++)
			{
				int num2 = (int)((uint)((int)start + i) % (uint)count);
				int num3 = 0;
				foreach (InPlay item in All)
				{
					if ((Object)(object)item != (Object)null && item.Index == num2)
					{
						num3++;
					}
				}
				if (num3 < num)
				{
					result = num2;
					num = num3;
				}
			}
			if (num == 0 || always)
			{
				return result;
			}
			NPC_Manager instance = NPC_Manager.Instance;
			if ((((Object)(object)instance != (Object)null && (Object)(object)instance.customersnpcParentOBJ != (Object)null) ? instance.customersnpcParentOBJ.transform.childCount : 0) < Plugin.CrowdSize.Value)
			{
				return -1;
			}
			return result;
		}

		internal static void Apply(Component owner, int index, bool hideHats)
		{
			owner.gameObject.AddComponent<InPlay>().Index = index;
			Skin.ReplaceNpc(owner, Plugin.Prefabs[index], keepProps: true, hideHats);
		}
	}
	[HarmonyPatch(typeof(NPC_Info), "CreateNPCCharacter")]
	internal static class CustomerPatch
	{
		private static readonly Dictionary<int, int> Choice = new Dictionary<int, int>();

		private static int Decide(NPC_Info npc)
		{
			if ((Object)(object)npc == (Object)null || Plugin.Prefabs.Count == 0)
			{
				return -1;
			}
			int instanceID = ((Object)npc).GetInstanceID();
			if (Choice.TryGetValue(instanceID, out var value))
			{
				return value;
			}
			uint num = (uint)((npc.isEmployee ? (npc.NPCID + 7919) : ((int)((NetworkBehaviour)npc).netId)) * -1640531535);
			int value2 = (npc.isEmployee ? Plugin.EmployeePercent : Plugin.ReplacePercent).Value;
			value = (((num >> 16) % 100 < (uint)value2) ? InPlay.Pick(num >> 8, always: false) : (-1));
			Choice[instanceID] = value;
			return value;
		}

		internal static bool SkipGenshin(NPC_Info __0)
		{
			return Decide(__0) < 0;
		}

		private static void Postfix(NPC_Info __instance)
		{
			int num = Decide(__instance);
			if (num >= 0)
			{
				InPlay.Apply((Component)(object)__instance, num, hideHats: false);
			}
		}
	}
	[HarmonyPatch(typeof(DebtCollector), "CreateNPCCharacter")]
	internal static class DebtCollectorPatch
	{
		private static void Postfix(DebtCollector __instance)
		{
			if (Plugin.Prefabs.Count != 0)
			{
				InPlay.Apply((Component)(object)__instance, InPlay.Pick(((NetworkBehaviour)__instance).netId, always: true), hideHats: true);
			}
		}
	}
	internal static class PlayerSkins
	{
		internal static int Ff7Index(int id, int vanillaCount)
		{
			int num = id - vanillaCount;
			if (num < 0 || num >= Plugin.Prefabs.Count)
			{
				return -1;
			}
			return num;
		}
	}
	[HarmonyPatch(typeof(PlayerNetwork), "SavePlayerSkins")]
	internal static class SaveSkinsPatch
	{
		private static void Prefix(PlayerNetwork __instance, out int __state)
		{
			__state = __instance.characterID;
			if (PlayerSkins.Ff7Index(__state, __instance.playerCharacter.Length) >= 0)
			{
				__instance.characterID = Plugin.VanillaCharacter.Value;
			}
		}

		private static void Postfix(PlayerNetwork __instance, int __state)
		{
			__instance.characterID = __state;
		}
	}
	[HarmonyPatch(typeof(PlayerNetwork), "OnChangeCharacter")]
	internal static class PlayerChangeCharacterPatch
	{
		private static void Prefix(PlayerNetwork __instance, ref int newID, out int __state)
		{
			__state = PlayerSkins.Ff7Index(newID, __instance.playerCharacter.Length);
			if (__state >= 0)
			{
				newID = 0;
			}
		}

		private static void Postfix(PlayerNetwork __instance, int __state)
		{
			if (__state >= 0)
			{
				Transform transform = ((Component)__instance).transform;
				Transform child = transform.GetChild(transform.childCount - 1);
				if (!(((Object)child).name != "Character"))
				{
					Skin.ReplaceMirrored(((Component)child).gameObject, Plugin.Prefabs[__state], ((NetworkBehaviour)__instance).isLocalPlayer);
				}
			}
		}
	}
}