Decompiled source of Sosig Hat Realism v1.0.0

SosigHatRealism.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace BitWizrd.SosigHatRealism;

[BepInProcess("h3vr.exe")]
[BepInPlugin("BitWizrd.SosigHatRealism", "Sosig Hat Realism", "1.0.0")]
public class SosigHatRealismPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(SosigWearable), "RegisterWearable")]
	private static class SosigWearableRegisterPatch
	{
		private static void Postfix(SosigWearable __instance, SosigLink l)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d5: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)__instance == (Object)null)
				{
					Logger.LogError((object)"Wearable instance is null");
				}
				else if ((Object)(object)l == (Object)null)
				{
					Logger.LogError((object)"SosigLink is null");
				}
				else
				{
					if ((int)l.BodyPart != 0)
					{
						return;
					}
					if ((Object)(object)heavyFabricMatDef == (Object)null)
					{
						Logger.LogError((object)"HeavyFabric MatDef is null");
						return;
					}
					if (debug)
					{
						Logger.LogInfo((object)$"Starting recursive PMat search on wearable: {((Object)__instance).name}");
						Logger.LogInfo((object)"Hierarchy structure:");
					}
					foreach (KeyValuePair<string, float> hatYOffset in hatYOffsets)
					{
						if (((Object)__instance).name.StartsWith(hatYOffset.Key))
						{
							Vector3 localPosition = ((Component)__instance).transform.localPosition;
							((Component)__instance).transform.localPosition = new Vector3(localPosition.x, localPosition.y + hatYOffset.Value, localPosition.z);
							if (debug)
							{
								Logger.LogInfo((object)$"Adjusted Y position of {((Object)__instance).name} by {hatYOffset.Value} units");
							}
							break;
						}
					}
					List<PMat> allPMatsInHierarchy = GetAllPMatsInHierarchy(((Component)__instance).gameObject);
					if (allPMatsInHierarchy.Count == 0)
					{
						if (debug)
						{
							Logger.LogWarning((object)$"Wearable {((Object)__instance).name} has no PMat components, skipping");
						}
						return;
					}
					if (debug)
					{
						Logger.LogInfo((object)$"Found total of {allPMatsInHierarchy.Count} PMat components in wearable {((Object)__instance).name}");
					}
					bool flag = false;
					foreach (PMat item in allPMatsInHierarchy)
					{
						if ((Object)(object)item.MatDef == (Object)null)
						{
							if (debug)
							{
								Logger.LogWarning((object)$"PMat component on {((Object)((Component)item).gameObject).name} has null MatDef, skipping");
							}
							continue;
						}
						if (debug)
						{
							Logger.LogInfo((object)$"Processing PMat on {((Object)((Component)item).gameObject).name} (Path: {GetGameObjectPath(((Component)item).gameObject)})");
							Logger.LogInfo((object)$"  MatDef: {((Object)item.MatDef).name} (BallisticType: {item.MatDef.BallisticType})");
						}
						if ((Object)(object)item.MatDef == (Object)(object)heavyFabricMatDef || ((Object)item.MatDef).name == "HeavyFabric")
						{
							flag = true;
							MatDef matDef = item.MatDef;
							item.MatDef = heavyFabricMatDef;
							if (debug)
							{
								Logger.LogInfo((object)$"  Changed MatDef from {((Object)matDef).name} (BallisticType: {matDef.BallisticType}) to HeavyFabric (BallisticType: {heavyFabricMatDef.BallisticType})");
							}
						}
					}
					if (!flag && debug)
					{
						Logger.LogInfo((object)$"No HeavyFabric material found in any PMat components of wearable: {((Object)__instance).name}");
					}
				}
			}
			catch (Exception ex)
			{
				Logger.LogError((object)$"Error in SosigWearableRegisterPatch: {ex.Message}\n{ex.StackTrace}");
			}
		}

		private static string GetGameObjectPath(GameObject obj)
		{
			string text = ((Object)obj).name;
			Transform parent = obj.transform.parent;
			while ((Object)(object)parent != (Object)null)
			{
				text = ((Object)parent).name + "/" + text;
				parent = parent.parent;
			}
			return text;
		}
	}

	private static ManualLogSource _logger;

	private Harmony harmony;

	private static MatDef heavyFabricMatDef;

	private static bool debug = false;

	private static Dictionary<string, float> hatYOffsets;

	internal static ManualLogSource Logger
	{
		get
		{
			return _logger;
		}
		private set
		{
			_logger = value;
		}
	}

	private void Awake()
	{
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Expected O, but got Unknown
		try
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			if (debug)
			{
				Logger.LogInfo((object)"Initializing Sosig Hat Realism Plugin");
			}
			InitializeHatOffsets();
			MatDef[] array = Resources.LoadAll<MatDef>("");
			if (debug)
			{
				Logger.LogInfo((object)$"Found {array.Length} total MatDefs");
			}
			heavyFabricMatDef = ((IEnumerable<MatDef>)array).FirstOrDefault((Func<MatDef, bool>)((MatDef m) => ((Object)m).name == "HeavyFabric"));
			if ((Object)(object)heavyFabricMatDef == (Object)null)
			{
				Logger.LogError((object)"Could not find HeavyFabric MatDef in available MatDefs");
				return;
			}
			if (debug)
			{
				Logger.LogInfo((object)"Found HeavyFabric MatDef successfully");
			}
			heavyFabricMatDef.BallisticType = (MatBallisticType)1;
			if (debug)
			{
				Logger.LogInfo((object)"Changed HeavyFabric MatDef's BallisticType to Air");
			}
			harmony = new Harmony("BitWizrd.SosigHatRealism");
			harmony.PatchAll(typeof(SosigHatRealismPlugin).Assembly);
			if (debug)
			{
				Logger.LogInfo((object)"Harmony patches applied successfully");
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)$"Error in Awake: {ex.Message}\n{ex.StackTrace}");
		}
	}

	private void InitializeHatOffsets()
	{
		hatYOffsets = new Dictionary<string, float>();
		hatYOffsets.Add("sosigaccessory_desperado_hat_gambler", -0.035f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_bowler", -0.033f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_pinchedfront", -0.016f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_boater", 0f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_trilby", 0f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_fisherman", 0f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_porkpie", 0f);
		hatYOffsets.Add("sosigaccessory_desperado_hat_opencrown", 0f);
	}

	private static List<PMat> GetAllPMatsInHierarchy(GameObject obj, int depth = 0)
	{
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Expected O, but got Unknown
		List<PMat> list = new List<PMat>();
		string arg = new string(' ', depth * 2);
		if (debug)
		{
			Logger.LogInfo((object)$"{arg}Searching GameObject: {((Object)obj).name}");
		}
		PMat[] components = obj.GetComponents<PMat>();
		if (components.Length > 0 && debug)
		{
			Logger.LogInfo((object)$"{arg}Found {components.Length} PMat components on {((Object)obj).name}");
			PMat[] array = components;
			foreach (PMat val in array)
			{
				Logger.LogInfo((object)string.Format("{0}  PMat on {1} has MatDef: {2}", arg, ((Object)obj).name, ((Object)(object)val.MatDef != (Object)null) ? ((Object)val.MatDef).name : "null"));
			}
		}
		list.AddRange(components);
		foreach (Transform item in obj.transform)
		{
			Transform val2 = item;
			if (debug)
			{
				Logger.LogInfo((object)$"{arg}Checking child: {((Object)val2).name}");
			}
			list.AddRange(GetAllPMatsInHierarchy(((Component)val2).gameObject, depth + 1));
		}
		return list;
	}

	private void OnDestroy()
	{
		try
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
				if (debug)
				{
					Logger.LogInfo((object)"Unpatched Harmony patches");
				}
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)$"Error in OnDestroy: {ex.Message}");
		}
	}
}