Decompiled source of BLUBBWORLD SERVER v0.6.4

BepInEx/plugins/ValheimVRM.dll

Decompiled 2 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using HarmonyLib;
using UniGLTF;
using UnityEngine;
using VRM;
using VRMShaders;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ValheimVRM")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0.0")]
[assembly: AssemblyProduct("ValheimVRM")]
[assembly: AssemblyTitle("ValheimVRM")]
[assembly: AssemblyVersion("1.2.0.0")]
namespace ValheimVRM;

public static class AccessUtil
{
	public static Tout GetField<Tin, Tout>(this Tin self, string fieldName)
	{
		return AccessTools.FieldRefAccess<Tin, Tout>(fieldName).Invoke(self);
	}
}
[BepInPlugin("com.yoship1639.plugins.valheimvrm", "ValheimVRM", "1.2.0.0")]
[BepInProcess("valheim.exe")]
public class MainPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.yoship1639.plugins.valheimvrm";

	public const string PluginName = "ValheimVRM";

	public const string PluginVersion = "1.2.0.0";

	private void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("com.yoship1639.plugins.valheimvrm.patch").PatchAll();
		VRMShaders.Initialize();
	}
}
public class MToonColorSync : MonoBehaviour
{
	private class MatColor
	{
		public Material mat;

		public Color color;

		public Color shadeColor;

		public Color emission;

		public bool hasColor;

		public bool hasShadeColor;

		public bool hasEmission;
	}

	private int _SunColor;

	private int _AmbientColor;

	private List<MatColor> matColors = new List<MatColor>();

	private void Awake()
	{
		_SunColor = Shader.PropertyToID("_SunColor");
		_AmbientColor = Shader.PropertyToID("_AmbientColor");
	}

	public void Setup(GameObject vrm)
	{
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: 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)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		matColors.Clear();
		SkinnedMeshRenderer[] componentsInChildren = vrm.GetComponentsInChildren<SkinnedMeshRenderer>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			Material[] materials = ((Renderer)componentsInChildren[i]).materials;
			foreach (Material mat in materials)
			{
				if (!matColors.Exists((MatColor m) => (Object)(object)m.mat == (Object)(object)mat))
				{
					matColors.Add(new MatColor
					{
						mat = mat,
						color = (mat.HasProperty("_Color") ? mat.GetColor("_Color") : Color.white),
						shadeColor = (mat.HasProperty("_ShadeColor") ? mat.GetColor("_ShadeColor") : Color.white),
						emission = (mat.HasProperty("_EmissionColor") ? mat.GetColor("_EmissionColor") : Color.black),
						hasColor = mat.HasProperty("_Color"),
						hasShadeColor = mat.HasProperty("_ShadeColor"),
						hasEmission = mat.HasProperty("_EmissionColor")
					});
				}
			}
		}
	}

	private void Update()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: 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_0017: Unknown result type (might be due to invalid IL or missing references)
		//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_002c: 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_003f: 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)
		//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_0098: 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_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		Color globalColor = Shader.GetGlobalColor(_SunColor);
		Color globalColor2 = Shader.GetGlobalColor(_AmbientColor);
		Color val = globalColor + globalColor2;
		if (((Color)(ref val)).maxColorComponent > 0.7f)
		{
			val /= 0.3f + ((Color)(ref val)).maxColorComponent;
		}
		foreach (MatColor matColor in matColors)
		{
			Color val2 = matColor.color * val;
			val2.a = matColor.color.a;
			if (((Color)(ref val2)).maxColorComponent > 1f)
			{
				val2 /= ((Color)(ref val2)).maxColorComponent;
			}
			Color val3 = matColor.shadeColor * val;
			val3.a = matColor.shadeColor.a;
			if (((Color)(ref val3)).maxColorComponent > 1f)
			{
				val3 /= ((Color)(ref val3)).maxColorComponent;
			}
			Color val4 = matColor.emission * ((Color)(ref val)).grayscale;
			if (matColor.hasColor)
			{
				matColor.mat.SetColor("_Color", val2);
			}
			if (matColor.hasShadeColor)
			{
				matColor.mat.SetColor("_ShadeColor", val3);
			}
			if (matColor.hasEmission)
			{
				matColor.mat.SetColor("_EmissionColor", val4);
			}
		}
	}
}
internal static class Settings
{
	private static Dictionary<string, string[]> playerSettings = new Dictionary<string, string[]>();

	public static string ValheimVRMDir => Path.Combine(Environment.CurrentDirectory, "ValheimVRM");

	public static string PlayerSettingsPath(string playerName)
	{
		return Path.Combine(ValheimVRMDir, "settings_" + playerName + ".txt");
	}

	public static void AddSettings(string playerName, string[] settings)
	{
		playerSettings[playerName] = settings;
	}

	public static string[] GetSettings(string playerName)
	{
		if (!playerSettings.ContainsKey(playerName))
		{
			return null;
		}
		return playerSettings[playerName];
	}

	public static bool AddSettingsFromFile(string playerName)
	{
		string path = PlayerSettingsPath(playerName);
		if (File.Exists(path))
		{
			playerSettings[playerName] = File.ReadAllLines(path);
			return true;
		}
		return false;
	}

	public static bool ContainsSettings(string playerName)
	{
		return playerSettings.ContainsKey(playerName);
	}

	public static string ReadSettings(string playername, string key)
	{
		if (!playerSettings.ContainsKey(playername))
		{
			return null;
		}
		string text = null;
		try
		{
			string[] array = playerSettings[playername];
			foreach (string text2 in array)
			{
				try
				{
					if (text2.Length <= 1 || !(text2.Substring(0, 2) == "//"))
					{
						string[] array2 = text2.Split(new char[1] { '=' });
						if (array2.Length == 2 && array2[0] == key)
						{
							text = array2[1];
						}
					}
				}
				catch
				{
				}
			}
		}
		catch
		{
		}
		if (text == null)
		{
			Debug.LogWarning((object)("[ValheimVRM] 設定項目が見つかりません: " + key));
		}
		return text;
	}

	public static int ReadInt(string playername, string key, int defaultValue = 0, bool debugLog = true)
	{
		string s = ReadSettings(playername, key);
		int result = defaultValue;
		if (int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
		{
			if (debugLog)
			{
				Debug.Log((object)("[ValheimVRM] " + key + ": " + result));
			}
			return result;
		}
		return defaultValue;
	}

	public static float ReadFloat(string playername, string key, float defaultValue = 0f, bool debugLog = true)
	{
		string s = ReadSettings(playername, key);
		float result = defaultValue;
		if (float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
		{
			if (debugLog)
			{
				Debug.Log((object)("[ValheimVRM] " + key + ": " + result));
			}
			return result;
		}
		return defaultValue;
	}

	public static bool ReadBool(string playername, string key, bool defaultValue = false, bool debugLog = true)
	{
		string value = ReadSettings(playername, key);
		bool result = defaultValue;
		if (bool.TryParse(value, out result))
		{
			if (debugLog)
			{
				Debug.Log((object)("[ValheimVRM] " + key + ": " + result));
			}
			return result;
		}
		return defaultValue;
	}

	public static Vector3 ReadVector3(string playername, string key, Vector3 defaultValue = default(Vector3), bool debugLog = true)
	{
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: 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_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		string text = ReadSettings(playername, key);
		if (text == null)
		{
			return defaultValue;
		}
		Match match = new Regex("\\((?<x>[^,]*?),(?<y>[^,]*?),(?<z>[^,]*?)\\)").Match(text);
		if (!match.Success)
		{
			return defaultValue;
		}
		Vector3 val;
		try
		{
			val = default(Vector3);
			val.x = float.Parse(match.Groups["x"].Value);
			val.y = float.Parse(match.Groups["y"].Value);
			val.z = float.Parse(match.Groups["z"].Value);
			Vector3 val2 = val;
			if (debugLog)
			{
				val = val2;
				Debug.Log((object)("[ValheimVRM] " + key + ": " + ((object)(Vector3)(ref val)).ToString()));
			}
			val = val2;
		}
		catch (FormatException ex)
		{
			Debug.LogError((object)ex);
			val = defaultValue;
		}
		return val;
	}
}
[HarmonyPatch(typeof(Shader))]
[HarmonyPatch("Find")]
internal static class ShaderPatch
{
	private static bool Prefix(ref Shader __result, string name)
	{
		if (VRMShaders.Shaders.TryGetValue(name, out var value))
		{
			__result = value;
			return false;
		}
		return true;
	}
}
public static class VRMShaders
{
	public static Dictionary<string, Shader> Shaders { get; } = new Dictionary<string, Shader>();


	public static void Initialize()
	{
		string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ValheimVRM.shaders");
		if (File.Exists(text))
		{
			Shader[] array = AssetBundle.LoadFromFile(text).LoadAllAssets<Shader>();
			foreach (Shader val in array)
			{
				Debug.Log((object)("[ValheimVRM] Add Shader: " + ((Object)val).name));
				Shaders.Add(((Object)val).name, val);
			}
		}
	}
}
public static class VRMModels
{
	public static Dictionary<string, byte[]> VrmBufDic = new Dictionary<string, byte[]>();

	public static Dictionary<Player, GameObject> PlayerToVrmDic = new Dictionary<Player, GameObject>();

	public static Dictionary<Player, string> PlayerToNameDic = new Dictionary<Player, string>();
}
[HarmonyPatch(typeof(VisEquipment), "UpdateLodgroup")]
internal static class Patch_VisEquipment_UpdateLodgroup
{
	[HarmonyPostfix]
	private static void Postfix(VisEquipment __instance)
	{
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_021f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0225: 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_025e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0264: Unknown result type (might be due to invalid IL or missing references)
		//IL_026e: Unknown result type (might be due to invalid IL or missing references)
		if (!__instance.m_isPlayer)
		{
			return;
		}
		Player component = ((Component)__instance).GetComponent<Player>();
		if ((Object)(object)component == (Object)null || !VRMModels.PlayerToVrmDic.ContainsKey(component))
		{
			return;
		}
		GameObject field = __instance.GetField<VisEquipment, GameObject>("m_hairItemInstance");
		if ((Object)(object)field != (Object)null)
		{
			SetVisible(field, flag: false);
		}
		GameObject field2 = __instance.GetField<VisEquipment, GameObject>("m_beardItemInstance");
		if ((Object)(object)field2 != (Object)null)
		{
			SetVisible(field2, flag: false);
		}
		List<GameObject> field3 = __instance.GetField<VisEquipment, List<GameObject>>("m_chestItemInstances");
		if (field3 != null)
		{
			foreach (GameObject item in field3)
			{
				SetVisible(item, flag: false);
			}
		}
		List<GameObject> field4 = __instance.GetField<VisEquipment, List<GameObject>>("m_legItemInstances");
		if (field4 != null)
		{
			foreach (GameObject item2 in field4)
			{
				SetVisible(item2, flag: false);
			}
		}
		List<GameObject> field5 = __instance.GetField<VisEquipment, List<GameObject>>("m_shoulderItemInstances");
		if (field5 != null)
		{
			foreach (GameObject item3 in field5)
			{
				SetVisible(item3, flag: false);
			}
		}
		List<GameObject> field6 = __instance.GetField<VisEquipment, List<GameObject>>("m_utilityItemInstances");
		if (field6 != null)
		{
			foreach (GameObject item4 in field6)
			{
				SetVisible(item4, flag: false);
			}
		}
		GameObject field7 = __instance.GetField<VisEquipment, GameObject>("m_helmetItemInstance");
		if ((Object)(object)field7 != (Object)null)
		{
			SetVisible(field7, flag: false);
		}
		string playername = VRMModels.PlayerToNameDic[component];
		GameObject field8 = __instance.GetField<VisEquipment, GameObject>("m_leftItemInstance");
		if ((Object)(object)field8 != (Object)null)
		{
			field8.transform.localPosition = Settings.ReadVector3(playername, "LeftHandEuqipPos", Vector3.zero);
		}
		GameObject field9 = __instance.GetField<VisEquipment, GameObject>("m_rightItemInstance");
		if ((Object)(object)field9 != (Object)null)
		{
			field9.transform.localPosition = Settings.ReadVector3(playername, "RightHandEuqipPos", Vector3.zero);
		}
		GameObject field10 = __instance.GetField<VisEquipment, GameObject>("m_rightBackItemInstance");
		if ((Object)(object)field10 != (Object)null)
		{
			field10.transform.localPosition = Settings.ReadVector3(playername, "RightHandBackItemPos", Vector3.zero) / 100f;
		}
		GameObject field11 = __instance.GetField<VisEquipment, GameObject>("m_leftBackItemInstance");
		if ((Object)(object)field11 != (Object)null)
		{
			field11.transform.localPosition = Settings.ReadVector3(playername, "LeftHandBackItemPos", Vector3.zero) / 100f;
		}
	}

	private static void SetVisible(GameObject obj, bool flag)
	{
		MeshRenderer[] componentsInChildren = obj.GetComponentsInChildren<MeshRenderer>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			((Renderer)componentsInChildren[i]).enabled = flag;
		}
		SkinnedMeshRenderer[] componentsInChildren2 = obj.GetComponentsInChildren<SkinnedMeshRenderer>();
		for (int i = 0; i < componentsInChildren2.Length; i++)
		{
			((Renderer)componentsInChildren2[i]).enabled = flag;
		}
	}
}
[HarmonyPatch(typeof(Humanoid), "OnRagdollCreated")]
internal static class Patch_Humanoid_OnRagdollCreated
{
	[HarmonyPostfix]
	private static void Postfix(Humanoid __instance, Ragdoll ragdoll)
	{
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Expected O, but got Unknown
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		if (((Character)__instance).IsPlayer())
		{
			SkinnedMeshRenderer[] componentsInChildren = ((Component)ragdoll).GetComponentsInChildren<SkinnedMeshRenderer>();
			foreach (SkinnedMeshRenderer obj in componentsInChildren)
			{
				((Renderer)obj).forceRenderingOff = true;
				obj.updateWhenOffscreen = true;
			}
			Animator val = ((Component)ragdoll).gameObject.AddComponent<Animator>();
			val.keepAnimatorControllerStateOnDisable = true;
			val.cullingMode = (AnimatorCullingMode)0;
			Animator field = AccessUtil.GetField<Player, Animator>((Player)__instance, "m_animator");
			val.avatar = field.avatar;
			if (VRMModels.PlayerToVrmDic.TryGetValue((Player)__instance, out var value))
			{
				value.transform.SetParent(((Component)ragdoll).transform);
				value.GetComponent<VRMAnimationSync>().Setup(val, isRagdoll: true);
			}
		}
	}
}
[HarmonyPatch(typeof(Character), "SetVisible")]
internal static class Patch_Character_SetVisible
{
	[HarmonyPostfix]
	private static void Postfix(Character __instance, bool visible)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		if (__instance.IsPlayer() && VRMModels.PlayerToVrmDic.TryGetValue((Player)__instance, out var value))
		{
			LODGroup component = value.GetComponent<LODGroup>();
			if (visible)
			{
				component.localReferencePoint = __instance.GetField<Character, Vector3>("m_originalLocalRef");
			}
			else
			{
				component.localReferencePoint = new Vector3(999999f, 999999f, 999999f);
			}
		}
	}
}
[HarmonyPatch(typeof(Player), "OnDeath")]
internal static class Patch_Player_OnDeath
{
	[HarmonyPostfix]
	private static void Postfix(Player __instance)
	{
		string text = null;
		if (VRMModels.PlayerToNameDic.ContainsKey(__instance))
		{
			text = VRMModels.PlayerToNameDic[__instance];
		}
		if (text != null && Settings.ReadBool(text, "FixCameraHeight", defaultValue: true))
		{
			Object.Destroy((Object)(object)((Component)__instance).GetComponent<VRMEyePositionSync>());
		}
	}
}
[HarmonyPatch(typeof(Character), "GetHeadPoint")]
internal static class Patch_Character_GetHeadPoint
{
	[HarmonyPostfix]
	private static bool Prefix(Character __instance, ref Vector3 __result)
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		Player val = (Player)(object)((__instance is Player) ? __instance : null);
		if ((Object)(object)val == (Object)null)
		{
			return true;
		}
		if (VRMModels.PlayerToVrmDic.TryGetValue(val, out var value))
		{
			Animator componentInChildren = value.GetComponentInChildren<Animator>();
			if ((Object)(object)componentInChildren == (Object)null)
			{
				return true;
			}
			Transform boneTransform = componentInChildren.GetBoneTransform((HumanBodyBones)10);
			if ((Object)(object)boneTransform == (Object)null)
			{
				return true;
			}
			__result = boneTransform.position;
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(Player), "Awake")]
internal static class Patch_Player_Awake
{
	private static Dictionary<string, GameObject> vrmDic = new Dictionary<string, GameObject>();

	[HarmonyPostfix]
	private static void Postfix(Player __instance)
	{
		//IL_05de: Unknown result type (might be due to invalid IL or missing references)
		//IL_075c: Unknown result type (might be due to invalid IL or missing references)
		//IL_025f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0264: Unknown result type (might be due to invalid IL or missing references)
		//IL_0294: Unknown result type (might be due to invalid IL or missing references)
		//IL_0309: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_04f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_030e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0353: Unknown result type (might be due to invalid IL or missing references)
		//IL_035a: Expected O, but got Unknown
		//IL_0420: Unknown result type (might be due to invalid IL or missing references)
		//IL_036c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0371: Unknown result type (might be due to invalid IL or missing references)
		//IL_0373: Unknown result type (might be due to invalid IL or missing references)
		//IL_0378: Unknown result type (might be due to invalid IL or missing references)
		//IL_037a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0398: Unknown result type (might be due to invalid IL or missing references)
		//IL_039d: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ab: Unknown result type (might be due to invalid IL or missing references)
		string text = null;
		if ((Object)(object)Game.instance != (Object)null)
		{
			text = __instance.GetPlayerName();
			if (text == "" || text == "...")
			{
				text = Game.instance.GetPlayerProfile().GetName();
			}
		}
		else
		{
			int field = FejdStartup.instance.GetField<FejdStartup, int>("m_profileIndex");
			List<PlayerProfile> field2 = FejdStartup.instance.GetField<FejdStartup, List<PlayerProfile>>("m_profiles");
			if (field >= 0 && field < field2.Count)
			{
				text = field2[field].GetName();
			}
		}
		SkinnedMeshRenderer[] componentsInChildren;
		if (!string.IsNullOrEmpty(text) && !vrmDic.ContainsKey(text))
		{
			string text2 = Path.Combine(Environment.CurrentDirectory, "ValheimVRM", text + ".vrm");
			_ = ref AccessTools.FieldRefAccess<Player, ZNetView>("m_nview").Invoke(__instance);
			if (!File.Exists(text2))
			{
				Debug.LogError((object)"[ValheimVRM] VRMファイルが見つかりません.");
				Debug.LogError((object)("[ValheimVRM] 読み込み予定だったVRMファイルパス: " + text2));
			}
			else
			{
				if (!Settings.ContainsSettings(text) && !Settings.AddSettingsFromFile(text))
				{
					Debug.LogWarning((object)("[ValheimVRM] 設定ファイルが見つかりません.以下の設定ファイルが存在するか確認してください: " + Settings.PlayerSettingsPath(text)));
				}
				float scale = Settings.ReadFloat(text, "ModelScale", 1.1f);
				GameObject val = ImportVRM(text2, scale);
				if ((Object)(object)val != (Object)null)
				{
					Object.DontDestroyOnLoad((Object)(object)val);
					vrmDic[text] = val;
					VRMModels.VrmBufDic[text] = File.ReadAllBytes(text2);
					float num = Settings.ReadFloat(text, "ModelBrightness", 0.8f);
					List<Material> list = new List<Material>();
					componentsInChildren = val.GetComponentsInChildren<SkinnedMeshRenderer>();
					for (int i = 0; i < componentsInChildren.Length; i++)
					{
						Material[] materials = ((Renderer)componentsInChildren[i]).materials;
						foreach (Material item in materials)
						{
							if (!list.Contains(item))
							{
								list.Add(item);
							}
						}
					}
					MeshRenderer[] componentsInChildren2 = val.GetComponentsInChildren<MeshRenderer>();
					for (int i = 0; i < componentsInChildren2.Length; i++)
					{
						Material[] materials = ((Renderer)componentsInChildren2[i]).materials;
						foreach (Material item2 in materials)
						{
							if (!list.Contains(item2))
							{
								list.Add(item2);
							}
						}
					}
					if (Settings.ReadBool(text, "UseMToonShader"))
					{
						foreach (Material item3 in list)
						{
							if (item3.HasProperty("_Color"))
							{
								Color color = item3.GetColor("_Color");
								color.r *= num;
								color.g *= num;
								color.b *= num;
								item3.SetColor("_Color", color);
							}
						}
					}
					else
					{
						Shader val2 = Shader.Find("Custom/Player");
						float num2 = default(float);
						float num3 = default(float);
						float num4 = default(float);
						foreach (Material item4 in list)
						{
							if ((Object)(object)item4.shader == (Object)(object)val2)
							{
								continue;
							}
							Color val3 = (item4.HasProperty("_Color") ? item4.GetColor("_Color") : Color.white);
							Texture2D val4 = (Texture2D)(item4.HasProperty("_MainTex") ? /*isinst with value type is only supported in some contexts*/: null);
							Texture2D val5 = val4;
							if ((Object)(object)val4 != (Object)null)
							{
								val5 = new Texture2D(((Texture)val4).width, ((Texture)val4).height);
								Color[] pixels = val4.GetPixels();
								for (int k = 0; k < pixels.Length; k++)
								{
									Color val6 = pixels[k] * val3;
									Color.RGBToHSV(val6, ref num2, ref num3, ref num4);
									num4 *= num;
									pixels[k] = Color.HSVToRGB(num2, num3, num4);
									pixels[k].a = val6.a;
								}
								val5.SetPixels(pixels);
								val5.Apply();
							}
							Texture val7 = (item4.HasProperty("_BumpMap") ? item4.GetTexture("_BumpMap") : null);
							item4.shader = val2;
							item4.SetTexture("_MainTex", (Texture)(object)val5);
							item4.SetTexture("_SkinBumpMap", val7);
							item4.SetColor("_SkinColor", val3);
							item4.SetTexture("_ChestTex", (Texture)(object)val5);
							item4.SetTexture("_ChestBumpMap", val7);
							item4.SetTexture("_LegsTex", (Texture)(object)val5);
							item4.SetTexture("_LegsBumpMap", val7);
							item4.SetFloat("_Glossiness", 0.2f);
							item4.SetFloat("_MetalGlossiness", 0f);
						}
					}
					LODGroup val8 = val.AddComponent<LODGroup>();
					Renderer[] componentsInChildren3 = (Renderer[])(object)val.GetComponentsInChildren<SkinnedMeshRenderer>();
					LOD val9 = default(LOD);
					((LOD)(ref val9))..ctor(0.1f, componentsInChildren3);
					if (Settings.ReadBool(text, "EnablePlayerFade", defaultValue: true))
					{
						val8.SetLODs((LOD[])(object)new LOD[1] { val9 });
					}
					val8.RecalculateBounds();
					LODGroup componentInChildren = ((Component)__instance).GetComponentInChildren<LODGroup>();
					val8.fadeMode = componentInChildren.fadeMode;
					val8.animateCrossFading = componentInChildren.animateCrossFading;
					val.SetActive(false);
				}
			}
		}
		if (string.IsNullOrEmpty(text) || !vrmDic.ContainsKey(text))
		{
			return;
		}
		GameObject val10 = Object.Instantiate<GameObject>(vrmDic[text]);
		VRMModels.PlayerToVrmDic[__instance] = val10;
		VRMModels.PlayerToNameDic[__instance] = text;
		val10.SetActive(true);
		val10.transform.SetParent(((Component)((Component)__instance).GetComponentInChildren<Animator>()).transform.parent, false);
		componentsInChildren = ((Character)__instance).GetVisual().GetComponentsInChildren<SkinnedMeshRenderer>();
		foreach (SkinnedMeshRenderer obj in componentsInChildren)
		{
			((Renderer)obj).forceRenderingOff = true;
			obj.updateWhenOffscreen = true;
		}
		Animator val11 = AccessTools.FieldRefAccess<Player, Animator>(__instance, "m_animator");
		val11.keepAnimatorControllerStateOnDisable = true;
		val11.cullingMode = (AnimatorCullingMode)0;
		val10.transform.localPosition = ((Component)val11).transform.localPosition;
		float offset = Settings.ReadFloat(text, "ModelOffsetY");
		if ((Object)(object)val10.GetComponent<VRMAnimationSync>() == (Object)null)
		{
			val10.AddComponent<VRMAnimationSync>().Setup(val11, isRagdoll: false, offset);
		}
		else
		{
			val10.GetComponent<VRMAnimationSync>().Setup(val11, isRagdoll: false, offset);
		}
		if (Settings.ReadBool(text, "FixCameraHeight", defaultValue: true))
		{
			Transform boneTransform = val10.GetComponent<Animator>().GetBoneTransform((HumanBodyBones)21);
			if ((Object)(object)boneTransform == (Object)null)
			{
				boneTransform = val10.GetComponent<Animator>().GetBoneTransform((HumanBodyBones)10);
			}
			if ((Object)(object)boneTransform == (Object)null)
			{
				boneTransform = val10.GetComponent<Animator>().GetBoneTransform((HumanBodyBones)9);
			}
			if ((Object)(object)boneTransform != (Object)null)
			{
				if ((Object)(object)((Component)__instance).gameObject.GetComponent<VRMEyePositionSync>() == (Object)null)
				{
					((Component)__instance).gameObject.AddComponent<VRMEyePositionSync>().Setup(boneTransform);
				}
				else
				{
					((Component)__instance).gameObject.GetComponent<VRMEyePositionSync>().Setup(boneTransform);
				}
			}
		}
		if (Settings.ReadBool(text, "UseMToonShader"))
		{
			if ((Object)(object)val10.GetComponent<MToonColorSync>() == (Object)null)
			{
				val10.AddComponent<MToonColorSync>().Setup(val10);
			}
			else
			{
				val10.GetComponent<MToonColorSync>().Setup(val10);
			}
		}
		float num5 = Settings.ReadFloat(text, "SpringBoneStiffness", 1f);
		float num6 = Settings.ReadFloat(text, "SpringBoneGravityPower", 1f);
		VRMSpringBone[] componentsInChildren4 = val10.GetComponentsInChildren<VRMSpringBone>();
		foreach (VRMSpringBone obj2 in componentsInChildren4)
		{
			obj2.m_stiffnessForce *= num5;
			obj2.m_gravityPower *= num6;
			obj2.m_updateType = (SpringBoneUpdateType)1;
			obj2.m_center = null;
		}
	}

	private static GameObject ImportVRM(string path, float scale)
	{
		//IL_0001: 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_0018: Expected O, but got Unknown
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		//IL_002f: 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)
		try
		{
			RuntimeGltfInstance obj = ImporterContextExtensions.Load((ImporterContext)new VRMImporterContext(new VRMData(new GlbFileParser(path).Parse()), (IReadOnlyDictionary<SubAssetKey, Object>)null, (ITextureDeserializer)null, (IMaterialDescriptorGenerator)null));
			obj.ShowMeshes();
			Transform transform = obj.Root.transform;
			transform.localScale *= scale;
			Debug.Log((object)"[ValheimVRM] VRM読み込み成功");
			Debug.Log((object)("[ValheimVRM] VRMファイルパス: " + path));
			return obj.Root;
		}
		catch (Exception ex)
		{
			Debug.LogError((object)ex);
		}
		return null;
	}
}
[DefaultExecutionOrder(int.MaxValue)]
public class VRMAnimationSync : MonoBehaviour
{
	private Animator orgAnim;

	private Animator vrmAnim;

	private HumanPoseHandler orgPose;

	private HumanPoseHandler vrmPose;

	private HumanPose hp;

	private bool ragdoll;

	private float offset;

	private static List<int> alreadyHashes = new List<int>();

	private static List<int> adjustHipHashes = new List<int> { -1536343465, 890925016, -1544306596, -1829310159, 337039637, -1603096, -496559199 };

	public void Setup(Animator orgAnim, bool isRagdoll = false, float offset = 0f)
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		ragdoll = isRagdoll;
		this.offset = offset;
		this.orgAnim = orgAnim;
		vrmAnim = ((Component)this).GetComponent<Animator>();
		vrmAnim.applyRootMotion = true;
		vrmAnim.updateMode = orgAnim.updateMode;
		vrmAnim.feetPivotActive = orgAnim.feetPivotActive;
		vrmAnim.layersAffectMassCenter = orgAnim.layersAffectMassCenter;
		vrmAnim.stabilizeFeet = orgAnim.stabilizeFeet;
		PoseHandlerCreate(orgAnim, vrmAnim);
	}

	private void PoseHandlerCreate(Animator org, Animator vrm)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Expected O, but got Unknown
		OnDestroy();
		orgPose = new HumanPoseHandler(org.avatar, ((Component)org).transform);
		vrmPose = new HumanPoseHandler(vrm.avatar, ((Component)vrm).transform);
	}

	private void OnDestroy()
	{
		if (orgPose != null)
		{
			orgPose.Dispose();
		}
		if (vrmPose != null)
		{
			vrmPose.Dispose();
		}
	}

	private float CalcFootSub()
	{
		//IL_000c: 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_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: 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)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: 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_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		Vector3 position = orgAnim.GetBoneTransform((HumanBodyBones)6).position;
		Vector3 position2 = orgAnim.GetBoneTransform((HumanBodyBones)5).position;
		Vector3 val = (position + position2) * 0.5f;
		Vector3 position3 = vrmAnim.GetBoneTransform((HumanBodyBones)6).position;
		Vector3 position4 = vrmAnim.GetBoneTransform((HumanBodyBones)5).position;
		Vector3 val2 = (position3 + position4) * 0.5f;
		return (val - val2).y;
	}

	private void Update()
	{
		//IL_000b: 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_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_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: 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)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		((Component)vrmAnim).transform.localPosition = Vector3.zero;
		if (!ragdoll)
		{
			for (int i = 0; i < 55; i++)
			{
				Transform boneTransform = orgAnim.GetBoneTransform((HumanBodyBones)i);
				Transform boneTransform2 = vrmAnim.GetBoneTransform((HumanBodyBones)i);
				if (i > 0 && (Object)(object)boneTransform != (Object)null && (Object)(object)boneTransform2 != (Object)null)
				{
					if (i == 5 || i == 6)
					{
						boneTransform.position = boneTransform2.position;
					}
					else
					{
						boneTransform.position = boneTransform2.position + Vector3.up * offset;
					}
				}
			}
		}
		Transform transform = ((Component)vrmAnim).transform;
		transform.localPosition += Vector3.up * offset;
	}

	private void LateUpdate()
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: 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_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: 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)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		((Component)vrmAnim).transform.localPosition = Vector3.zero;
		Vector3 position = orgAnim.GetBoneTransform((HumanBodyBones)0).position;
		orgPose.GetHumanPose(ref hp);
		vrmPose.SetHumanPose(ref hp);
		AnimatorStateInfo currentAnimatorStateInfo = orgAnim.GetCurrentAnimatorStateInfo(0);
		int shortNameHash = ((AnimatorStateInfo)(ref currentAnimatorStateInfo)).shortNameHash;
		bool num = adjustHipHashes.Contains(shortNameHash);
		Transform boneTransform = vrmAnim.GetBoneTransform((HumanBodyBones)0);
		if (num)
		{
			boneTransform.position = position;
		}
		float num2 = 0f;
		if (shortNameHash == 890925016 || shortNameHash == -1544306596 || shortNameHash == -1829310159)
		{
			num2 += 0.1f;
		}
		if (!num)
		{
			num2 = CalcFootSub();
		}
		Vector3 position2 = boneTransform.position;
		position2.y += num2;
		boneTransform.position = position2;
		if (!ragdoll)
		{
			for (int i = 0; i < 55; i++)
			{
				Transform boneTransform2 = orgAnim.GetBoneTransform((HumanBodyBones)i);
				Transform boneTransform3 = vrmAnim.GetBoneTransform((HumanBodyBones)i);
				if (i > 0 && (Object)(object)boneTransform2 != (Object)null && (Object)(object)boneTransform3 != (Object)null)
				{
					if (i == 5 || i == 6)
					{
						boneTransform2.position = boneTransform3.position;
					}
					else
					{
						boneTransform2.position = boneTransform3.position + Vector3.up * offset;
					}
				}
			}
		}
		Transform transform = ((Component)vrmAnim).transform;
		transform.localPosition += Vector3.up * offset;
	}
}
public class VRMEyePositionSync : MonoBehaviour
{
	private Transform vrmEye;

	private Transform orgEye;

	public void Setup(Transform vrmEye)
	{
		this.vrmEye = vrmEye;
		orgEye = ((Character)((Component)this).GetComponent<Player>()).m_eye;
	}

	private void Update()
	{
		//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_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		Vector3 position = orgEye.position;
		position.y = vrmEye.position.y;
		orgEye.position = position;
	}
}