Decompiled source of AutoRecharge v1.0.0

baer1.LethalAutoCharge.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using ChatCommandAPI;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("baer1.LethalAutoCharge")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+b1e78df2a8d48f02323c2771065dfbb23a6aa34a")]
[assembly: AssemblyProduct("LethalAutoCharge")]
[assembly: AssemblyTitle("baer1.LethalAutoCharge")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 LethalAutoCharge
{
	internal static class ChatIntegration
	{
		internal static void RegisterCommand()
		{
			try
			{
				new ChargeCommand();
				LethalAutoCharge.Logger.LogDebug((object)"Chat command registered successfully");
			}
			catch (Exception arg)
			{
				LethalAutoCharge.Logger.LogError((object)$"Error registering chat command: {arg}");
			}
		}

		internal static void Print(string text)
		{
			try
			{
				ChatCommandAPI.PrintError(text);
			}
			catch (Exception arg)
			{
				LethalAutoCharge.Logger.LogError((object)$"Error printing message to chat: {arg}");
			}
		}
	}
	public class ChargeCommand : Command
	{
		public override string Name => "ChargeItems";

		public override string Description => "Recharge all item batteries";

		public override string[] Commands => new string[3]
		{
			"charge",
			"recharge",
			((Command)this).Name
		};

		public override bool Invoke(string[] args, Dictionary<string, string> kwargs, out string error)
		{
			error = "The ship must be in orbit";
			return StartOfRound.Instance.inShipPhase && LethalAutoCharge.ChargeAllItems(out error);
		}
	}
	[BepInPlugin("baer1.LethalAutoCharge", "LethalAutoCharge", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class LethalAutoCharge : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(StartOfRound), "SetShipReadyToLand")]
		internal static class OnLeavePatch
		{
			private static void Postfix()
			{
				AutoChargeAllItems();
			}
		}

		[HarmonyPatch(typeof(StartOfRound), "openingDoorsSequence")]
		internal static class OnStartPatch
		{
			private static void Postfix()
			{
				AutoChargeAllItems();
			}
		}

		public static LethalAutoCharge Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		internal static bool ChatAPILoaded => Chainloader.PluginInfos.ContainsKey("baer1.ChatCommandAPI");

		private void Awake()
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			if (ChatAPILoaded)
			{
				ChatIntegration.RegisterCommand();
			}
			else
			{
				Logger.LogDebug((object)"Skipped registering chat command, ChatCommandAPI not loaded");
			}
			if (Harmony == null)
			{
				Harmony = new Harmony("baer1.LethalAutoCharge");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
			Logger.LogInfo((object)"baer1.LethalAutoCharge v1.0.0 has loaded!");
		}

		public static bool ChargeAllItems(out string error)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Expected O, but got Unknown
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			if (array == null)
			{
				error = "Items is null";
				return false;
			}
			int num = 0;
			GrabbableObject[] array2 = array;
			foreach (GrabbableObject val in array2)
			{
				try
				{
					if ((Object)(object)val == (Object)null)
					{
						throw new ArgumentNullException("i");
					}
					if (val.itemProperties.requiresBattery)
					{
						val.insertedBattery = new Battery(false, 1f);
						val.SyncBatteryServerRpc(100);
					}
				}
				catch (Exception arg)
				{
					Logger.LogError((object)string.Format("Exception occurred while attempting to recharge {0}: {1}", ((Object)(object)val == (Object)null) ? "null" : ((string)(object)val), arg));
					num++;
				}
			}
			error = $"{num} items could not be charged";
			return num == 0;
		}

		private static void AutoChargeAllItems()
		{
			if (!ChargeAllItems(out string error))
			{
				Logger.LogError((object)("Error while charging items: " + error));
				if (ChatAPILoaded)
				{
					ChatIntegration.Print("Error while charging items: " + error);
				}
				else
				{
					Logger.LogDebug((object)"Skipped printing to chat, ChatCommandAPI not loaded");
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "baer1.LethalAutoCharge";

		public const string PLUGIN_NAME = "LethalAutoCharge";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}