Decompiled source of ScaleInCart v1.0.7

ScaleInCart.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using REPOLib.Modules;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ShrinkItemsInCart")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ShrinkItemsInCart")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5e12a72d-c200-488d-940a-653d1003d96e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ScaleInCart
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("bulletbot.scaleincart", "ScaleInCart", "1.0.7")]
	internal class Plugin : BaseUnityPlugin
	{
		internal static class REPOLib
		{
			internal const string modGuid = "REPOLib";

			internal static bool IsLoaded()
			{
				return Chainloader.PluginInfos.ContainsKey("REPOLib");
			}

			internal static void OnAwake()
			{
				List<ValuableObject> list = new List<ValuableObject>();
				foreach (GameObject registeredValuable in Valuables.RegisteredValuables)
				{
					ValuableObject component = registeredValuable.GetComponent<ValuableObject>();
					if ((Object)(object)component != (Object)null && !list.Contains(component))
					{
						instance.CreateConfigForValuable(component, isModded: true);
						list.Add(component);
					}
				}
				instance.LogMessages(list.Count);
			}
		}

		private const string modGUID = "bulletbot.scaleincart";

		private const string modName = "ScaleInCart";

		private const string modVer = "1.0.7";

		internal static Plugin instance;

		internal ManualLogSource logger;

		private readonly Harmony harmony = new Harmony("bulletbot.scaleincart");

		internal Dictionary<PhysGrabCart, PhysGrabObject[]> grabObjectsInCarts;

		internal Dictionary<PhysGrabObject, Vector3> currentGrabObjects;

		internal ConfigEntry<bool> useMassScaling;

		internal ConfigEntry<bool> revertBack;

		internal ConfigEntry<bool> scaleEverything;

		internal ConfigEntry<float> scaleSpeed;

		private ConfigEntry<float> defaultScale;

		private ConfigEntry<string> massScaleFactors;

		private Dictionary<float, float> massScaleFactorsDict = new Dictionary<float, float>();

		internal Dictionary<string, ConfigEntry<float>> valuableConfigs;

		private void CreateConfigForValuable(ValuableObject valuable, bool isModded)
		{
			string text = ((Object)valuable).name.Trim();
			valuableConfigs[text] = ((BaseUnityPlugin)this).Config.Bind<float>((isModded ? "Modded " : "") + "Valuable Scaling (" + text + ")", "Scale", 1f, $"Mass: {valuable.physAttributePreset.mass}");
		}

		internal void LogMessages(int moddedCount = -1)
		{
			logger.LogMessage((object)$"Found {massScaleFactorsDict.Count} mass scale factors.");
			logger.LogMessage((object)$"Found {valuableConfigs.Count} valuables.");
			if (moddedCount != -1)
			{
				logger.LogMessage((object)$"Found {moddedCount} modded valuables.");
			}
			logger.LogMessage((object)"ScaleInCart has started.");
		}

		private void Awake()
		{
			instance = this;
			logger = Logger.CreateLogSource("ScaleInCart");
			grabObjectsInCarts = new Dictionary<PhysGrabCart, PhysGrabObject[]>();
			currentGrabObjects = new Dictionary<PhysGrabObject, Vector3>();
			useMassScaling = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Use Mass Scaling", true, "If enabled, valuables will scale based on their mass.\nIf disabled, valuables will scale based on their valuable setting.");
			revertBack = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Revert Back To Original Scale", true, "If enabled, valuables will revert to their original scale when not in the cart.\nIf disabled, the valuables will remain at their modified scale.");
			scaleEverything = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Scale Everything", false, "If enabled, every grabbable object will be scaled.\nIf disabled, only valuables will be scaled.");
			scaleSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Scale Speed", 0.005f, "The speed at which valuables scale down/up per update.\nHigher values make valuables shrink/expand faster.");
			defaultScale = ((BaseUnityPlugin)this).Config.Bind<float>("Mass Scaling", "Default Scale", 0.8f, "Default scale factor if no mass threshold is exceeded");
			massScaleFactors = ((BaseUnityPlugin)this).Config.Bind<string>("Mass Scaling", "Scale Factors", "1.0=0.75, 1.5=0.7, 2.0=0.65, 2.5=0.6, 3.0=0.55, 3.5=0.5, 4.0=0.45, 4.5=0.4, 5.0=0.35", "Scaling factors for mass >= values.\nFormat: 'mass=scale', separated by commas.");
			try
			{
				string[] array = massScaleFactors.Value.Trim().Split(new char[1] { ',' });
				foreach (string text in array)
				{
					string[] array2 = (from x in text.Split(new char[1] { '=' })
						select x.Trim()).ToArray();
					if (array2.Length == 2)
					{
						float num = float.Parse(array2[0]);
						float value = float.Parse(array2[1]);
						if (massScaleFactorsDict.ContainsKey(num))
						{
							throw new InvalidOperationException($"Duplicate found for mass {num}.");
						}
						massScaleFactorsDict[num] = value;
					}
				}
			}
			catch (Exception ex)
			{
				logger.LogError((object)("Error parsing mass scale factors: " + ex.Message));
				logger.LogError((object)"Failed to start ScaleInCart.");
				return;
			}
			valuableConfigs = new Dictionary<string, ConfigEntry<float>>();
			ValuableObject[] array3 = Resources.LoadAll<ValuableObject>("Valuables/");
			ValuableObject[] array4 = array3;
			foreach (ValuableObject valuable in array4)
			{
				CreateConfigForValuable(valuable, isModded: false);
			}
			harmony.PatchAll();
		}

		public float GetScaleByMass(float mass)
		{
			foreach (float item in massScaleFactorsDict.Keys.OrderByDescending((float x) => x))
			{
				if (mass >= item)
				{
					return massScaleFactorsDict[item];
				}
			}
			return defaultScale.Value;
		}
	}
}
namespace ScaleInCart.Patches
{
	[HarmonyPatch(typeof(RunManager))]
	internal class RunManagerPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		[HarmonyAfter(new string[] { "REPOLib" })]
		private static void Awake()
		{
			if (Plugin.REPOLib.IsLoaded())
			{
				Plugin.REPOLib.OnAwake();
			}
			else
			{
				Plugin.instance.LogMessages();
			}
		}

		[HarmonyPatch("ChangeLevel")]
		[HarmonyPrefix]
		private static void ChangeLevel()
		{
			Plugin.instance.grabObjectsInCarts.Clear();
			Plugin.instance.currentGrabObjects.Clear();
		}
	}
	[HarmonyPatch(typeof(PhysGrabObject))]
	internal class PhysGrabObjectPatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void Update(PhysGrabObject __instance, bool ___isValuable)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: 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_02db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_030c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_0380: 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_0379: Unknown result type (might be due to invalid IL or missing references)
			//IL_0328: Unknown result type (might be due to invalid IL or missing references)
			//IL_033d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0381: Unknown result type (might be due to invalid IL or missing references)
			//IL_0386: Unknown result type (might be due to invalid IL or missing references)
			//IL_0247: Unknown result type (might be due to invalid IL or missing references)
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_0393: Unknown result type (might be due to invalid IL or missing references)
			//IL_039a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0275: 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_03e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			//IL_0418: Unknown result type (might be due to invalid IL or missing references)
			//IL_041f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0403: Unknown result type (might be due to invalid IL or missing references)
			//IL_040a: Unknown result type (might be due to invalid IL or missing references)
			//IL_043b: Unknown result type (might be due to invalid IL or missing references)
			if (!(Plugin.instance.scaleEverything.Value || ___isValuable))
			{
				return;
			}
			if (!Plugin.instance.currentGrabObjects.ContainsKey(__instance))
			{
				Plugin.instance.currentGrabObjects.Add(__instance, ((Component)__instance.rb).transform.localScale);
			}
			bool flag = false;
			foreach (KeyValuePair<PhysGrabCart, PhysGrabObject[]> grabObjectsInCart in Plugin.instance.grabObjectsInCarts)
			{
				PhysGrabObject[] value = grabObjectsInCart.Value;
				foreach (PhysGrabObject val in value)
				{
					if ((Object)(object)val == (Object)(object)__instance)
					{
						flag = true;
						break;
					}
				}
			}
			float value2 = Plugin.instance.scaleSpeed.Value;
			Vector3 val2 = default(Vector3);
			((Vector3)(ref val2))..ctor(value2, value2, value2);
			Vector3 val3 = Plugin.instance.currentGrabObjects[__instance];
			float num = 1f;
			if (Plugin.instance.useMassScaling.Value || !___isValuable)
			{
				num = Plugin.instance.GetScaleByMass(__instance.massOriginal);
			}
			else
			{
				string text = ((Object)__instance).name.Replace("(Clone)", "").Trim();
				foreach (KeyValuePair<string, ConfigEntry<float>> valuableConfig in Plugin.instance.valuableConfigs)
				{
					if (valuableConfig.Key == text)
					{
						num = valuableConfig.Value.Value;
					}
				}
			}
			if (flag && !Mathf.Approximately(((Component)__instance.rb).transform.localScale.y, num))
			{
				Vector3 val4 = ((Component)__instance.rb).transform.localScale + ((num < 1f) ? (-val2) : val2);
				val4.x = ((num < 1f) ? Mathf.Max(val4.x, num) : Mathf.Min(val4.x, num));
				val4.y = ((num < 1f) ? Mathf.Max(val4.y, num) : Mathf.Min(val4.y, num));
				val4.z = ((num < 1f) ? Mathf.Max(val4.z, num) : Mathf.Min(val4.z, num));
				((Component)__instance.rb).transform.localScale = val4;
			}
			else if (!flag && Plugin.instance.revertBack.Value && (!Mathf.Approximately(((Component)__instance.rb).transform.localScale.x, Plugin.instance.currentGrabObjects[__instance].x) || !Mathf.Approximately(((Component)__instance.rb).transform.localScale.y, Plugin.instance.currentGrabObjects[__instance].y) || !Mathf.Approximately(((Component)__instance.rb).transform.localScale.z, Plugin.instance.currentGrabObjects[__instance].z)))
			{
				Vector3 val5 = ((Component)__instance.rb).transform.localScale + ((num < 1f) ? val2 : (-val2));
				val5.x = ((num < 1f) ? Mathf.Min(val5.x, val3.x) : Mathf.Max(val5.x, val3.x));
				val5.y = ((num < 1f) ? Mathf.Min(val5.y, val3.y) : Mathf.Max(val5.y, val3.y));
				val5.z = ((num < 1f) ? Mathf.Min(val5.z, val3.z) : Mathf.Max(val5.z, val3.z));
				((Component)__instance.rb).transform.localScale = val5;
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabCart))]
	internal class PhysGrabCartPatch
	{
		[HarmonyPatch("ObjectsInCart")]
		[HarmonyPostfix]
		private static void ObjectsInCart(PhysGrabCart __instance, List<PhysGrabObject> ___itemsInCart)
		{
			Plugin.instance.grabObjectsInCarts[__instance] = ___itemsInCart.Where((PhysGrabObject x) => Plugin.instance.scaleEverything.Value || (bool)AccessTools.Field(typeof(PhysGrabObject), "isValuable").GetValue(x)).ToArray();
		}
	}
}