Decompiled source of HolsterResizer v1.1.0

Mods/HolsterResizer.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib.BoneMenu;
using BoneLib.BoneMenu.Elements;
using HarmonyLib;
using HolsterResizer;
using Il2CppSystem.Collections.Generic;
using LabFusion.Utilities;
using MelonLoader;
using MelonLoader.Preferences;
using SLZ.Interaction;
using SLZ.Marrow.Data;
using SLZ.Props;
using SLZ.Props.Weapons;
using SLZ.Rig;
using SLZ.VRMK;
using UnhollowerBaseLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("HolsterResizer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HolsterResizer")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a2ddb519-b45b-4fdf-9ec3-f1eb2cd4fd61")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: MelonInfo(typeof(global::HolsterResizer.HolsterResizer), "HolsterResizer", "1.1.0", "Toedtmanns", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonOptionalDependencies(new string[] { "BoneLib", "LabFusion" })]
[assembly: MelonColor(ConsoleColor.Cyan)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
namespace HolsterResizer;

public class HolsterResizer : MelonMod
{
	public const string BoneLibName = "BoneLib";

	public const string FusionName = "LabFusion";

	public const float DefaultSize = 1.8f;

	private static float _localRelativeSize;

	private object _sizeMultiplierElement;

	private MelonPreferences_Category _localPreferences;

	private MelonPreferences_Entry<float> _prefLocalSizeMultiplier;

	private MelonPreferences_Entry<bool> _prefScaleUp;

	private MelonPreferences_Entry<bool> _prefScaleDown;

	private MelonPreferences_Entry<bool> _prefScaleBodylog;

	public static HolsterResizer Instance => Melon<HolsterResizer>.Instance;

	public static Instance Logger => Melon<HolsterResizer>.Logger;

	public static float LocalRelativeSize
	{
		get
		{
			return _localRelativeSize * Melon<HolsterResizer>.Instance.SizeMultiplier;
		}
		set
		{
			_localRelativeSize = value / 1.8f;
		}
	}

	public static Vector3 RelativeSizeVec => new Vector3(_localRelativeSize, _localRelativeSize, _localRelativeSize);

	public RigManager LocalRig { get; private set; }

	public bool IsBoneLibLoaded { get; private set; }

	public bool IsFusionLoaded { get; private set; }

	public float SizeMultiplier { get; private set; }

	public bool ScaleUp { get; private set; }

	public bool ScaleDown { get; private set; }

	public bool ScaleBodylog { get; private set; }

	protected FloatElement SizeMultiplierElement
	{
		get
		{
			object sizeMultiplierElement = _sizeMultiplierElement;
			return (FloatElement)((sizeMultiplierElement is FloatElement) ? sizeMultiplierElement : null);
		}
	}

	public override void OnInitializeMelon()
	{
		((MelonBase)this).OnInitializeMelon();
		_localPreferences = MelonPreferences.CreateCategory("Holster Resizer");
		_prefLocalSizeMultiplier = _localPreferences.CreateEntry<float>("localSizeMultiplier", 1f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		_prefScaleUp = _localPreferences.CreateEntry<bool>("scaleUp", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		_prefScaleDown = _localPreferences.CreateEntry<bool>("scaleDown", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		_prefScaleBodylog = _localPreferences.CreateEntry<bool>("scaleBodylog", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		SizeMultiplier = _prefLocalSizeMultiplier.Value;
		ScaleUp = _prefScaleUp.Value;
		ScaleDown = _prefScaleDown.Value;
		ScaleBodylog = _prefScaleBodylog.Value;
		if (GetMelonByName("BoneLib") != null)
		{
			IsBoneLibLoaded = true;
			InitializeWithBonelib();
			if (GetMelonByName("LabFusion") != null)
			{
				IsFusionLoaded = true;
				InitializeWithFusion();
			}
		}
	}

	public override void OnApplicationQuit()
	{
		((MelonBase)this).OnApplicationQuit();
		_prefScaleUp.Value = ScaleUp;
		_prefScaleDown.Value = ScaleDown;
		_prefLocalSizeMultiplier.Value = SizeMultiplier;
		MelonPreferences.SaveCategory<MelonPreferences_Category>("localPreferences", true);
	}

	public override void OnSceneWasLoaded(int buildIndex, string sceneName)
	{
		((MelonMod)this).OnSceneWasLoaded(buildIndex, sceneName);
		if (!IsFusionLoaded)
		{
			LocalRig = Object.FindObjectOfType<RigManager>();
		}
	}

	private void InitializeWithFusion()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		MultiplayerHooking.OnLocalPlayerCreated += new RigManagerEvent(OnLocalPlayerCreated);
	}

	private void InitializeWithBonelib()
	{
		//IL_0005: 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_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		MenuCategory val = MenuManager.CreateCategory("Holster Resizer", Color.white);
		_sizeMultiplierElement = val.CreateFloatElement("Holster size multiplier", Color.white, SizeMultiplier, 0.05f, 0.05f, 2f, (Action<float>)OnSizeMultiplierChange);
		val.CreateFunctionElement("Reset multiplier", Color.white, (Action)OnSizeMultiplierReset);
		val.CreateBoolElement("Scale up with Avatar", Color.white, ScaleUp, (Action<bool>)OnScaleUpChange);
		val.CreateBoolElement("Scale down with Avatar", Color.white, ScaleDown, (Action<bool>)OnScaleDownChange);
		val.CreateBoolElement("Scale Bodylog", Color.white, ScaleBodylog, (Action<bool>)OnScaleBodylogChange);
	}

	private void OnSizeMultiplierChange(float multiplier)
	{
		SizeMultiplier = multiplier;
		ScaleHolsters(LocalRig, LocalRelativeSize);
	}

	private void OnSizeMultiplierReset()
	{
		SizeMultiplier = 1f;
		((GenericElement<float>)(object)SizeMultiplierElement).SetValue(SizeMultiplier);
		ScaleHolsters(LocalRig, LocalRelativeSize);
	}

	private void OnScaleUpChange(bool scaleUp)
	{
		ScaleUp = scaleUp;
	}

	private void OnScaleDownChange(bool scaleDown)
	{
		ScaleDown = scaleDown;
	}

	private void OnScaleBodylogChange(bool scaleBodylog)
	{
		ScaleBodylog = scaleBodylog;
		ScaleHolsters(LocalRig, LocalRelativeSize);
	}

	private void OnLocalPlayerCreated(RigManager rig)
	{
		LocalRig = rig;
	}

	public static void ToAvatarScale(Transform trans)
	{
		//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_0013: 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_0034: Unknown result type (might be due to invalid IL or missing references)
		if (trans.lossyScale != RelativeSizeVec)
		{
			float num = trans.localScale.x * (LocalRelativeSize / trans.lossyScale.x);
			trans.localScale = new Vector3(num, num, num);
		}
	}

	public static void ToNormalScale(Transform trans)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: 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_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		if (trans.lossyScale != new Vector3(1f, 1f, 1f))
		{
			float num = trans.localScale.x * (1f / trans.lossyScale.x);
			trans.localScale = new Vector3(num, num, num);
		}
	}

	[Conditional("DEBUG")]
	public static void DbgLog(string msg)
	{
		Logger.Msg(msg);
	}

	[Conditional("DEBUG")]
	public static void DbgLog(string msg, ConsoleColor color)
	{
		Logger.Msg(color, msg);
	}

	public static MelonBase GetMelonByName(string name)
	{
		foreach (MelonAssembly loadedAssembly in MelonAssembly.LoadedAssemblies)
		{
			foreach (MelonBase loadedMelon in loadedAssembly.LoadedMelons)
			{
				if (loadedMelon.Info.Name == name)
				{
					return loadedMelon;
				}
			}
		}
		return null;
	}

	public static void ScaleHolsters(RigManager rig, float size, bool scaleBodylog = true)
	{
		//IL_0041: 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_0071: 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)
		PullCordDevice componentInChildren = ((Component)rig.physicsRig).GetComponentInChildren<PullCordDevice>();
		if ((Object)(object)componentInChildren != (Object)null)
		{
			if (scaleBodylog)
			{
				((Component)componentInChildren).GetComponent<Transform>().localScale = new Vector3(size, size, size);
			}
			else
			{
				float sizeMultiplier = Instance.SizeMultiplier;
				((Component)componentInChildren).GetComponent<Transform>().localScale = new Vector3(sizeMultiplier, sizeMultiplier, sizeMultiplier);
			}
		}
		foreach (SlotContainer item in (Il2CppArrayBase<SlotContainer>)(object)rig.inventory.bodySlots)
		{
			((Component)item).GetComponent<Transform>().localScale = new Vector3(size, size, size);
			if (!((Object)item).name.Equals("BeltLf1"))
			{
				continue;
			}
			InventoryAmmoReceiver componentInChildren2 = ((Component)item).GetComponentInChildren<InventoryAmmoReceiver>();
			if ((Object)(object)componentInChildren2 == (Object)null)
			{
				Logger.Warning("Couldn't find iar!");
				continue;
			}
			Enumerator<Magazine> enumerator2 = componentInChildren2._magazineArts.GetEnumerator();
			while (enumerator2.MoveNext())
			{
				((Component)enumerator2.Current).GetComponent<Transform>().localScale = new Vector3(1f, 1f, 1f);
			}
		}
	}
}
[HarmonyPatch(typeof(RigManager), "SwitchAvatar")]
public static class SwitchAvatarPatch
{
	public static void Postfix(RigManager __instance, Avatar newAvatar)
	{
		if (!((Object)(object)HolsterResizer.Instance.LocalRig != (Object)null) || !((Object)(object)__instance != (Object)(object)HolsterResizer.Instance.LocalRig))
		{
			if ((HolsterResizer.Instance.ScaleDown || __instance.avatar.height > 1.8f) && (HolsterResizer.Instance.ScaleUp || __instance.avatar.height < 1.8f))
			{
				HolsterResizer.LocalRelativeSize = __instance.avatar.height;
			}
			else
			{
				HolsterResizer.LocalRelativeSize = 1.8f;
			}
			HolsterResizer.ScaleHolsters(__instance, HolsterResizer.LocalRelativeSize, HolsterResizer.Instance.ScaleBodylog);
		}
	}
}
[HarmonyPatch(typeof(InventorySlotReceiver), "OnHandDrop")]
public static class OnHandDropPatch
{
	public static void Postfix(InventorySlotReceiver __instance, IGrippable host)
	{
		GameObject hostGameObject = host.GetHostGameObject();
		if (__instance._weaponHost != null)
		{
			_ = HolsterResizer.LocalRelativeSize;
			HolsterResizer.ToAvatarScale(hostGameObject.GetComponent<Transform>());
		}
	}
}
[HarmonyPatch(typeof(InventorySlotReceiver), "OnHandGrab")]
public static class OnHandGrabPatch
{
	public static void Postfix(InventorySlotReceiver __instance, Hand hand)
	{
		HolsterResizer.ToNormalScale(hand.AttachedReceiver.Host.GetHostGameObject().GetComponent<Transform>());
	}
}
[HarmonyPatch(typeof(InventoryAmmoReceiver), "SwitchMagazine")]
public static class SwitchMagazinePatch
{
	public static void Postfix(InventoryAmmoReceiver __instance, MagazineData magazineData, CartridgeData cartridgeData)
	{
		//IL_0030: 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_0051: Unknown result type (might be due to invalid IL or missing references)
		Vector3 localScale = default(Vector3);
		((Vector3)(ref localScale))..ctor(1f, 1f, 1f);
		Enumerator<Magazine> enumerator = __instance._magazineArts.GetEnumerator();
		while (enumerator.MoveNext())
		{
			Magazine current = enumerator.Current;
			((Component)current).GetComponent<Transform>().localScale = localScale;
			((Component)current._firstCartridgeArt).GetComponent<Transform>().localScale = localScale;
			((Component)current._secondCartridgeArt).GetComponent<Transform>().localScale = localScale;
		}
	}
}
[HarmonyPatch(typeof(Magazine), "OnGrab")]
public static class OnGrabPatch
{
	public static void Postfix(Magazine __instance, Hand hand)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		Vector3 localScale = default(Vector3);
		((Vector3)(ref localScale))..ctor(1f, 1f, 1f);
		((Component)__instance).GetComponent<Transform>().localScale = localScale;
		((Component)__instance._firstCartridgeArt).GetComponent<Transform>().localScale = localScale;
		((Component)__instance._secondCartridgeArt).GetComponent<Transform>().localScale = localScale;
	}
}
[HarmonyPatch(typeof(Magazine), "OnSpawn")]
public static class OnSpawnMagPatch
{
	public static void Postfix(Magazine __instance, GameObject go)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		Vector3 localScale = default(Vector3);
		((Vector3)(ref localScale))..ctor(1f, 1f, 1f);
		go.GetComponent<Transform>().localScale = localScale;
	}
}