Decompiled source of ScaleInCart v1.1.8

ScaleInCart.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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;
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("ScaleInCart")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ScaleInCart")]
[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.1.8")]
	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()
			{
				BundleLoader.OnAllBundlesLoaded += delegate
				{
					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.SetupValuableObject(component, isModded: true);
							list.Add(component);
						}
					}
					instance.LogMessages();
				};
			}
		}

		private const string modGUID = "bulletbot.scaleincart";

		private const string modName = "ScaleInCart";

		private const string modVer = "1.1.8";

		internal static Plugin instance;

		internal ManualLogSource logger;

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

		internal Dictionary<PhysGrabCart, PhysGrabObject[]> grabObjectsInCarts;

		internal Dictionary<string, Vector3> grabObjectScales;

		internal Dictionary<PhysGrabObject, (float, bool)> grabObjectLastUpdates;

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

		internal ConfigEntry<bool> useMassScaling;

		internal ConfigEntry<bool> revertBack;

		internal ConfigEntry<bool> scaleEverything;

		internal ConfigEntry<bool> smoothScaling;

		internal ConfigEntry<float> scaleSpeed;

		internal ConfigEntry<float> scaleDelay;

		private ConfigEntry<float> defaultScale;

		private ConfigEntry<string> massScaleFactors;

		internal Dictionary<string, (ConfigEntry<bool>, ConfigEntry<float>)> valuableConfigs;

		internal bool isLoaded;

		internal int vanillaCount;

		internal int moddedCount = -1;

		private void SetupValuableObject(ValuableObject valuableObject, bool isModded)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			string name = ((Object)valuableObject).name;
			grabObjectScales.Add(name, ((Component)valuableObject).transform.localScale);
			string text = (isModded ? "Modded " : "") + "Valuable Scaling (" + name + ")";
			valuableConfigs[name] = (((BaseUnityPlugin)this).Config.Bind<bool>(text, "Use Valuable Scale", false, "If enabled, this will force the valuable scale to be used no matter what the 'Use Mass Scaling' value would be."), ((BaseUnityPlugin)this).Config.Bind<float>(text, "Valuable Scale", 1f, "Mass: " + valuableObject.physAttributePreset.mass.ToString(CultureInfo.InvariantCulture)));
			if (isModded)
			{
				if (moddedCount == -1)
				{
					moddedCount = 0;
				}
				moddedCount++;
			}
			else
			{
				vanillaCount++;
			}
		}

		internal void LogMessages()
		{
			logger.LogMessage((object)$"Found {massScaleFactorsDict.Count} mass scale factors.");
			logger.LogMessage((object)$"Found {vanillaCount} vanilla valuables.");
			if (moddedCount != -1)
			{
				logger.LogMessage((object)$"Found {moddedCount} modded valuables.");
			}
			logger.LogMessage((object)$"Found {valuableConfigs.Count} valuables in total.");
		}

		private void Awake()
		{
			instance = this;
			logger = Logger.CreateLogSource("ScaleInCart");
			grabObjectsInCarts = new Dictionary<PhysGrabCart, PhysGrabObject[]>();
			grabObjectScales = new Dictionary<string, Vector3>();
			grabObjectLastUpdates = new Dictionary<PhysGrabObject, (float, bool)>();
			massScaleFactorsDict = new Dictionary<float, float>();
			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 scale 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.");
			smoothScaling = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Smooth Scaling", true, "If enabled, valuables will smoothly resize over time.\nIf disabled, valuables will resize without any transition.");
			scaleSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Scale Speed", 0.45f, "The speed at which valuables scale down/up per update. Higher values make valuables shrink/expand faster.");
			scaleDelay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Scale Delay", 0f, "The initial delay in seconds before valuables begin scaling.");
			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.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], CultureInfo.InvariantCulture);
						float value = float.Parse(array2[1], CultureInfo.InvariantCulture);
						if (massScaleFactorsDict.ContainsKey(num))
						{
							instance.logger.LogWarning((object)$"Duplicate found for mass '{num}'.");
						}
						else
						{
							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<bool>, ConfigEntry<float>)>();
			ValuableObject[] array3 = Resources.LoadAll<ValuableObject>("Valuables/");
			foreach (ValuableObject valuableObject in array3)
			{
				SetupValuableObject(valuableObject, isModded: false);
			}
			harmony.PatchAll();
			logger.LogMessage((object)"ScaleInCart has started.");
			if (REPOLib.IsLoaded())
			{
				REPOLib.OnAwake();
			}
			else
			{
				LogMessages();
			}
			isLoaded = true;
		}

		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("ChangeLevel")]
		[HarmonyPrefix]
		private static void ChangeLevel()
		{
			if (Plugin.instance.isLoaded)
			{
				Plugin.instance.grabObjectsInCarts.Clear();
				Plugin.instance.grabObjectScales.Clear();
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabObject))]
	internal class PhysGrabObjectPatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void Update(PhysGrabObject __instance, bool ___isValuable)
		{
			//IL_027e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0283: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: 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_02cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0310: Unknown result type (might be due to invalid IL or missing references)
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_0314: Unknown result type (might be due to invalid IL or missing references)
			//IL_0319: Unknown result type (might be due to invalid IL or missing references)
			//IL_038e: 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_0394: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			//IL_0348: Unknown result type (might be due to invalid IL or missing references)
			//IL_034a: Unknown result type (might be due to invalid IL or missing references)
			//IL_034f: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c2: 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_037e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0437: Unknown result type (might be due to invalid IL or missing references)
			//IL_0439: Unknown result type (might be due to invalid IL or missing references)
			//IL_043d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0421: Unknown result type (might be due to invalid IL or missing references)
			//IL_0423: Unknown result type (might be due to invalid IL or missing references)
			//IL_0427: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.instance.isLoaded || (!Plugin.instance.scaleEverything.Value && !___isValuable))
			{
				return;
			}
			string text = ((Object)((Component)__instance).gameObject).name.Replace("(Clone)", "");
			float num = -1f;
			bool flag = false;
			foreach (KeyValuePair<string, (ConfigEntry<bool>, ConfigEntry<float>)> valuableConfig in Plugin.instance.valuableConfigs)
			{
				if (valuableConfig.Key == text)
				{
					(ConfigEntry<bool>, ConfigEntry<float>) value = valuableConfig.Value;
					num = ((value.Item1.Value || !Plugin.instance.useMassScaling.Value) ? value.Item2.Value : Plugin.instance.GetScaleByMass(__instance.massOriginal));
					flag = true;
				}
			}
			if (!flag && !___isValuable)
			{
				num = Plugin.instance.GetScaleByMass(__instance.massOriginal);
			}
			if (num == -1f)
			{
				return;
			}
			bool flag2 = false;
			foreach (KeyValuePair<PhysGrabCart, PhysGrabObject[]> grabObjectsInCart in Plugin.instance.grabObjectsInCarts)
			{
				PhysGrabObject[] value2 = grabObjectsInCart.Value;
				foreach (PhysGrabObject val in value2)
				{
					if ((Object)(object)val == (Object)(object)__instance)
					{
						flag2 = true;
						break;
					}
				}
			}
			float value3 = Plugin.instance.scaleDelay.Value;
			if (value3 > 0f)
			{
				if (!Plugin.instance.grabObjectLastUpdates.TryGetValue(__instance, out var value4))
				{
					value4 = (Time.time, flag2);
					Plugin.instance.grabObjectLastUpdates[__instance] = value4;
				}
				var (num2, flag3) = value4;
				if (flag3 != flag2)
				{
					num2 = Time.time + value3;
					Plugin.instance.grabObjectLastUpdates[__instance] = (num2, flag2);
				}
				if (Time.time < num2)
				{
					return;
				}
				Plugin.instance.grabObjectLastUpdates[__instance] = (Time.time, flag2);
			}
			Transform transform = ((Component)__instance).transform;
			Vector3 localScale = transform.localScale;
			if (!Plugin.instance.grabObjectScales.ContainsKey(text))
			{
				Plugin.instance.grabObjectScales[text] = localScale;
			}
			Vector3 val2 = Plugin.instance.grabObjectScales[text];
			Vector3 val3 = val2 * num;
			float num3 = Plugin.instance.scaleSpeed.Value * Mathf.Pow(val2.x * val2.y * val2.z, 1f / 3f) * Time.deltaTime;
			float num4 = 0.0001f;
			Vector3 val4;
			if (flag2)
			{
				val4 = localScale - val3;
				if (((Vector3)(ref val4)).sqrMagnitude > num4)
				{
					if (Plugin.instance.smoothScaling.Value)
					{
						val4 = localScale - val3;
						float magnitude = ((Vector3)(ref val4)).magnitude;
						float num5 = ((magnitude > 0f) ? Mathf.Clamp01(num3 / magnitude) : 1f);
						transform.localScale = Vector3.Lerp(localScale, val3, num5);
					}
					else
					{
						transform.localScale = Vector3.MoveTowards(localScale, val3, num3);
					}
					return;
				}
			}
			if (flag2 || !Plugin.instance.revertBack.Value)
			{
				return;
			}
			val4 = localScale - val2;
			if (((Vector3)(ref val4)).sqrMagnitude > num4)
			{
				if (Plugin.instance.smoothScaling.Value)
				{
					val4 = localScale - val2;
					float magnitude2 = ((Vector3)(ref val4)).magnitude;
					float num6 = ((magnitude2 > 0f) ? Mathf.Clamp01(num3 / magnitude2) : 1f);
					transform.localScale = Vector3.Lerp(localScale, val2, num6);
				}
				else
				{
					transform.localScale = Vector3.MoveTowards(localScale, val2, num3);
				}
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabCart))]
	internal class PhysGrabCartPatch
	{
		[HarmonyPatch("ObjectsInCart")]
		[HarmonyPostfix]
		private static void ObjectsInCart(PhysGrabCart __instance, List<PhysGrabObject> ___itemsInCart)
		{
			if (Plugin.instance.isLoaded)
			{
				Plugin.instance.grabObjectsInCarts[__instance] = ___itemsInCart.Where((PhysGrabObject x) => Plugin.instance.scaleEverything.Value || (bool)AccessTools.Field(typeof(PhysGrabObject), "isValuable").GetValue(x)).ToArray();
			}
		}
	}
}