Decompiled source of SwimChargeInventory v2.0.1

plugins/SwimChargeInventory (BepInEx)/SwimChargeInventory.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
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 BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Nautilus.Handlers;
using Nautilus.Json;
using Nautilus.Options.Attributes;
using SwimChargeInventory.Configuration;
using SwimChargeInventory.Patches;
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("SwimChargeInventory")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SwimChargeInventory")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("74594095-8254-46a1-99cb-5e1516d1ea14")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SwimChargeInventory
{
	[BepInPlugin("SwimChargeInventory", "SwimChargeInventory", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class SwimChargeInventory : BaseUnityPlugin
	{
		public const string PLUGIN_GUID = "com.telmo.swimchargeinventory";

		public const string PLUGIN_NAME = "Swim Charge Inventory";

		public const string PLUGIN_VERSION = "1.0.0";

		internal static SwimChargeConfig ModConfig { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			ModConfig = OptionsPanelHandler.RegisterModOptions<SwimChargeConfig>();
			Harmony.CreateAndPatchAll(typeof(SwimChargeInventoryPatch), (string)null);
			Logger.LogInfo((object)"{PLUGIN_GUID} is loaded!");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "SwimChargeInventory";

		public const string PLUGIN_NAME = "SwimChargeInventory";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace SwimChargeInventory.Patches
{
	[HarmonyPatch(typeof(UpdateSwimCharge), "FixedUpdate")]
	internal class SwimChargeInventoryPatch
	{
		private static void Prefix(UpdateSwimCharge __instance)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			SwimChargeConfig modConfig = SwimChargeInventory.ModConfig;
			if (modConfig == null)
			{
				return;
			}
			if (Player.main.IsUnderwater())
			{
				Vector3 velocity = ((Component)Player.main).GetComponent<Rigidbody>().velocity;
				if (!(((Vector3)(ref velocity)).magnitude <= 2f) && Inventory.Get().equipment.GetCount((TechType)806) >= 1)
				{
					float chargeAmount = __instance.chargePerSecond * Time.deltaTime * modConfig.ChargeSpeedMultiplier;
					bool flag = false;
					flag = ChargeHeldTool(__instance, chargeAmount);
					if (!flag || modConfig.ChargeMultipleItems)
					{
						flag = ChargeInventoryTools(chargeAmount, modConfig);
					}
					if ((!flag || modConfig.ChargeMultipleItems) && modConfig.ChargeBatteries)
					{
						flag = ChargeLooseBatteries(chargeAmount, modConfig);
					}
					if (flag)
					{
						__instance.swimChargeLoop.Play();
					}
					else
					{
						__instance.swimChargeLoop.Stop();
					}
					return;
				}
			}
			__instance.swimChargeLoop.Stop();
		}

		private static bool ChargeHeldTool(UpdateSwimCharge instance, float chargeAmount)
		{
			PlayerTool heldTool = Inventory.main.GetHeldTool();
			EnergyMixin val = default(EnergyMixin);
			if ((Object)(object)heldTool != (Object)null && ((Component)heldTool).TryGetComponent<EnergyMixin>(ref val))
			{
				return val.AddEnergy(chargeAmount);
			}
			return false;
		}

		private static bool ChargeInventoryTools(float chargeAmount, SwimChargeConfig config)
		{
			bool result = false;
			EnergyMixin val = default(EnergyMixin);
			foreach (InventoryItem item in (IEnumerable<InventoryItem>)Inventory.Get().container)
			{
				if (((Component)item.item).TryGetComponent<EnergyMixin>(ref val) && val.AddEnergy(chargeAmount))
				{
					result = true;
					if (!config.ChargeMultipleItems)
					{
						break;
					}
				}
			}
			return result;
		}

		private static bool ChargeLooseBatteries(float chargeAmount, SwimChargeConfig config)
		{
			bool result = false;
			IBattery val = default(IBattery);
			foreach (InventoryItem item in (IEnumerable<InventoryItem>)Inventory.Get().container)
			{
				if (((Component)item.item).TryGetComponent<IBattery>(ref val) && val.charge < val.capacity)
				{
					val.charge = Mathf.Min(val.charge + chargeAmount, val.capacity);
					result = true;
					if (!config.ChargeMultipleItems)
					{
						break;
					}
				}
			}
			return result;
		}
	}
}
namespace SwimChargeInventory.Configuration
{
	[Menu("Swim Charge Inventory options")]
	public class SwimChargeConfig : ConfigFile
	{
		[Toggle("Charge loose batteries in inventory")]
		public bool ChargeBatteries = false;

		[Toggle("Charge Multiple Items")]
		public bool ChargeMultipleItems = true;

		[Slider("Charge Speed Multiplier", 0.1f, 5f, DefaultValue = 1f, Format = "{0:F2}")]
		public float ChargeSpeedMultiplier = 1f;
	}
}