Decompiled source of AutomaticShipSystems v0.0.0

AutomaticShipSystems.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CG;
using CG.Client.Ship.Interactions;
using CG.Game;
using CG.Ship.Modules;
using CG.Space;
using Gameplay.Enhancements;
using Gameplay.PowerSystem;
using Gameplay.Ship;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;
using VoidManager;
using VoidManager.CustomGUI;
using VoidManager.MPModChecks;
using VoidManager.Utilities;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("AutomaticShipSystems")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Template")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AutomaticShipSystems")]
[assembly: AssemblyTitle("AutomaticShipSystems")]
[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 AutomaticShipSystems
{
	internal static class MyPluginInfo
	{
		internal const string PLUGIN_GUID = "id107.automaticshipsystems";

		internal const string PLUGIN_NAME = "AutomaticShipSystems";

		internal const string PLUGIN_VERSION = "0.0.0";
	}
	[BepInPlugin("id107.automaticshipsystems", "AutomaticShipSystems", "0.0.0")]
	[BepInProcess("Void Crew.exe")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BepinPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Configs.Load(this);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "id107.automaticshipsystems");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin id107.automaticshipsystems is loaded!");
		}
	}
	internal class Configs
	{
		internal const double CircuitBreakerDelay = 30000.0;

		internal const double ThrusterBoosterDelay = 60000.0;

		internal const double TrimDelay = 300000.0;

		internal static ConfigEntry<bool> CircuitBreakerConfig;

		internal static ConfigEntry<bool> ThrusterBoosterConfig;

		internal static ConfigEntry<bool> TrimConfig;

		internal static void Load(BepinPlugin plugin)
		{
			CircuitBreakerConfig = ((BaseUnityPlugin)plugin).Config.Bind<bool>("AutomaticShipSystems", "CircuitBreakers", true, (ConfigDescription)null);
			ThrusterBoosterConfig = ((BaseUnityPlugin)plugin).Config.Bind<bool>("AutomaticShipSystems", "ThrusterBooster", true, (ConfigDescription)null);
			TrimConfig = ((BaseUnityPlugin)plugin).Config.Bind<bool>("AutomaticShipSystems", "Trim", true, (ConfigDescription)null);
		}
	}
	[HarmonyPatch(typeof(Enhancement))]
	internal class EnhancementPatch
	{
		[HarmonyPrepare]
		private static void HookEvents()
		{
			Configs.TrimConfig.SettingChanged += ToggleAutomaticTrims;
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetState")]
		private static void SetState(Enhancement __instance, EnhancementState newState)
		{
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Invalid comparison between Unknown and I4
			if (!PhotonNetwork.IsMasterClient || !Configs.TrimConfig.Value)
			{
				return;
			}
			ClientGame current = ClientGame.Current;
			if (current == null)
			{
				return;
			}
			AbstractPlayerControlledShip playerShip = current.PlayerShip;
			bool? obj;
			if (playerShip == null)
			{
				obj = null;
			}
			else
			{
				Helm module = playerShip.GetModule<Helm>();
				if (module == null)
				{
					obj = null;
				}
				else
				{
					ShipEngine engine = module.Engine;
					obj = ((engine == null) ? null : ((Component)engine).GetComponentsInChildren<Enhancement>()?.Contains(__instance));
				}
			}
			if (obj == true && (int)newState == 0)
			{
				Tools.DelayDoUnique((object)__instance, (Action)delegate
				{
					ResetTrim(__instance);
				}, 300000.0);
			}
		}

		private static void ResetTrim(Enhancement trim)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Invalid comparison between Unknown and I4
			if (PhotonNetwork.IsMasterClient && Tools.PlayerShipExists && (int)((ChangeResponsive<EnhancementState>)(object)trim.CurrentState).Value == 0)
			{
				trim.RequestStateChange((EnhancementState)1, 1f);
			}
		}

		internal static void ToggleAutomaticTrims(object sender, EventArgs e)
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Invalid comparison between Unknown and I4
			if (!PhotonNetwork.IsMasterClient)
			{
				return;
			}
			ClientGame current = ClientGame.Current;
			object obj;
			if (current == null)
			{
				obj = null;
			}
			else
			{
				AbstractPlayerControlledShip playerShip = current.PlayerShip;
				if (playerShip == null)
				{
					obj = null;
				}
				else
				{
					Helm module = playerShip.GetModule<Helm>();
					if (module == null)
					{
						obj = null;
					}
					else
					{
						ShipEngine engine = module.Engine;
						obj = ((engine != null) ? ((Component)engine).GetComponentsInChildren<Enhancement>() : null);
					}
				}
			}
			Enhancement[] array = (Enhancement[])obj;
			if (array == null || array.Length == 0)
			{
				return;
			}
			if (Configs.TrimConfig.Value)
			{
				Enhancement[] array2 = array;
				foreach (Enhancement trim in array2)
				{
					if ((int)((ChangeResponsive<EnhancementState>)(object)trim.CurrentState).Value == 0)
					{
						Tools.DelayDoUnique((object)trim, (Action)delegate
						{
							ResetTrim(trim);
						}, 300000.0);
					}
				}
			}
			else
			{
				Enhancement[] array3 = array;
				foreach (Enhancement val in array3)
				{
					Tools.CancelDelayDoUnique((object)val);
				}
			}
		}
	}
	internal class GUI : ModSettingsMenu
	{
		public override string Name()
		{
			return "Automatic Ship Systems";
		}

		public override void Draw()
		{
			GUITools.DrawCheckbox("Circuit Breakers", ref Configs.CircuitBreakerConfig);
			GUITools.DrawCheckbox("Thruster Boosters", ref Configs.ThrusterBoosterConfig);
			GUITools.DrawCheckbox("Trims", ref Configs.TrimConfig);
		}
	}
	[HarmonyPatch(typeof(PowerBreaker))]
	internal class PowerBreakerPatch
	{
		[HarmonyPrepare]
		private static void HookEvents()
		{
			Configs.CircuitBreakerConfig.SettingChanged += ToggleAutomaticBreakers;
		}

		[HarmonyPostfix]
		[HarmonyPatch("OnPowerStateChange")]
		private static void OnPowerStateChange(PowerBreaker __instance, bool isOn)
		{
			if (PhotonNetwork.IsMasterClient && !isOn && Configs.CircuitBreakerConfig.Value)
			{
				Tools.DelayDoUnique((object)__instance, (Action)delegate
				{
					ResetCircuitBreakers(__instance);
				}, 30000.0);
			}
		}

		private static void ResetCircuitBreakers(PowerBreaker breaker)
		{
			if (PhotonNetwork.IsMasterClient && Tools.PlayerShipExists)
			{
				((ChangeResponsive<bool>)(object)breaker.IsOn).RequestChange(true, false, (Action)null, (Action)null);
			}
		}

		internal static void ToggleAutomaticBreakers(object sender, EventArgs e)
		{
			if (!PhotonNetwork.IsMasterClient)
			{
				return;
			}
			ClientGame current = ClientGame.Current;
			object obj;
			if (current == null)
			{
				obj = null;
			}
			else
			{
				AbstractPlayerControlledShip playerShip = current.PlayerShip;
				obj = ((playerShip == null) ? null : ((Component)playerShip).GetComponentInChildren<ProtectedPowerSystem>()?.Breakers);
			}
			List<PowerBreaker> list = (List<PowerBreaker>)obj;
			if (list == null)
			{
				return;
			}
			if (Configs.CircuitBreakerConfig.Value)
			{
				foreach (PowerBreaker powerBreaker in list)
				{
					if (!((ChangeResponsive<bool>)(object)powerBreaker.IsOn).Value)
					{
						Tools.DelayDoUnique((object)powerBreaker, (Action)delegate
						{
							((ChangeResponsive<bool>)(object)powerBreaker.IsOn).RequestChange(true, false, (Action)null, (Action)null);
						}, 30000.0);
					}
				}
				return;
			}
			foreach (PowerBreaker item in list)
			{
				Tools.CancelDelayDoUnique((object)item);
			}
		}
	}
	[HarmonyPatch(typeof(ThrusterBooster))]
	internal class ThrusterBoosterPatch
	{
		private static readonly MethodInfo SetLeverPositionMethod = AccessTools.Method(typeof(Lever), "set_LeverPosition", (Type[])null, (Type[])null);

		[HarmonyPrepare]
		private static void HookEvents()
		{
			Configs.ThrusterBoosterConfig.SettingChanged += ToggleAutomaticThrusterBoosters;
		}

		[HarmonyPostfix]
		[HarmonyPatch("ChangeState")]
		private static void ChangeState(ThrusterBooster __instance, ThrusterBoosterState state)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			if (PhotonNetwork.IsMasterClient && (int)state == 0 && Configs.ThrusterBoosterConfig.Value)
			{
				Tools.DelayDoUnique((object)__instance, (Action)delegate
				{
					ChargeThrusterBooster(__instance);
				}, 60000.0);
			}
		}

		private static void ChargeThrusterBooster(ThrusterBooster booster)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Invalid comparison between Unknown and I4
			if (PhotonNetwork.IsMasterClient && Tools.PlayerShipExists && (int)booster.state == 0)
			{
				SetLeverPositionMethod.Invoke(booster.ChargeLever, new object[1] { 1f });
			}
		}

		internal static void ToggleAutomaticThrusterBoosters(object sender, EventArgs e)
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Invalid comparison between Unknown and I4
			if (!PhotonNetwork.IsMasterClient)
			{
				return;
			}
			AbstractPlayerControlledShip playerShip = ClientGame.Current.PlayerShip;
			object obj;
			if (playerShip == null)
			{
				obj = null;
			}
			else
			{
				Transform transform = ((AbstractCloneStarObject)playerShip).Transform;
				if (transform == null)
				{
					obj = null;
				}
				else
				{
					ThrusterBoosterController component = ((Component)transform).GetComponent<ThrusterBoosterController>();
					obj = ((component != null) ? component.ThrusterBoosters : null);
				}
			}
			List<ThrusterBooster> list = (List<ThrusterBooster>)obj;
			if (list == null)
			{
				return;
			}
			if (Configs.ThrusterBoosterConfig.Value)
			{
				foreach (ThrusterBooster booster in list)
				{
					if ((int)booster.state == 0)
					{
						Tools.DelayDoUnique((object)booster, (Action)delegate
						{
							ChargeThrusterBooster(booster);
						}, 60000.0);
					}
				}
				return;
			}
			foreach (ThrusterBooster item in list)
			{
				Tools.CancelDelayDoUnique((object)item);
			}
		}
	}
	public class VoidManagerPlugin : VoidPlugin
	{
		public override MultiplayerType MPType => (MultiplayerType)4;

		public override string Author => "18107";

		public override string Description => "Makes circuit breakers, thruster boosters, and trims self reset";

		public VoidManagerPlugin()
		{
			Events.Instance.MasterClientSwitched += delegate
			{
				if (PhotonNetwork.IsMasterClient)
				{
					PowerBreakerPatch.ToggleAutomaticBreakers(null, null);
					ThrusterBoosterPatch.ToggleAutomaticThrusterBoosters(null, null);
					EnhancementPatch.ToggleAutomaticTrims(null, null);
				}
			};
		}
	}
}