Decompiled source of Homunculus Safety v0.0.2

HomunculusSafety.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.Logging;
using CG;
using CG.Game;
using CG.Objects;
using CG.Ship.Hull;
using CG.Ship.Object;
using CG.Space;
using Gameplay.Carryables;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UI.ObjectMarker;
using UnityEngine;
using VoidManager;
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("HomunculusSafety")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.0.2.0")]
[assembly: AssemblyInformationalVersion("0.0.2+422ac378975f1ae2c2e554b4299b07c0ef6e87f2")]
[assembly: AssemblyProduct("HomunculusSafety")]
[assembly: AssemblyTitle("Recreates the homunculus if it gets lost. Host side.")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.2.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 HomunculusSafety
{
	[BepInPlugin("18107.HomunculusSafety", "Homunculus Safety", "0.0.2")]
	[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;
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "18107.HomunculusSafety");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin 18107.HomunculusSafety is loaded!");
		}
	}
	[HarmonyPatch(typeof(HomunculusAndBiomassSocket), "DispenseHomunculusNow")]
	internal class HomunculusAndBiomassSocketPatch
	{
		private static readonly FieldInfo socketModeField = AccessTools.Field(typeof(HomunculusAndBiomassSocket), "_socketMode");

		private static bool Prefix(HomunculusAndBiomassSocket __instance)
		{
			if ((Object)(object)HomunculusManager.homunculus != (Object)null)
			{
				socketModeField.SetValue(__instance, (byte)1);
				return false;
			}
			return true;
		}

		private static void Postfix(HomunculusAndBiomassSocket __instance)
		{
			if (!((Object)(object)((CarryablesSocket)__instance).Payload == (Object)null))
			{
				HomunculusManager.homunculus = ((CarryablesSocket)__instance).Payload;
				if (HomunculusManager.UrgencyMarker != null)
				{
					((MarkerPosition)HomunculusManager.UrgencyMarker).isFollowingTransform = true;
					((MarkerPosition)HomunculusManager.UrgencyMarker).FollowTarget = ((Component)HomunculusManager.GetUMT(HomunculusManager.homunculus)).transform;
					HomunculusManager.UrgencyMarker = null;
				}
				HomunculusManager.homunculus.OnDropped += HomunculusManager.StartTimer;
				HomunculusManager.homunculus.OnCarried += HomunculusManager.StopTimer;
			}
		}
	}
	internal class HomunculusManager
	{
		internal static ObjectMarkerData UrgencyMarker;

		internal static AbstractCarryableObject homunculus;

		private static float time;

		private const float homunculusTimeoutSeconds = 15f;

		internal static void StartTimer(ICarrier carrier)
		{
			time = Time.time + 15f;
			Events.Instance.LateUpdate += CheckTimer;
		}

		internal static void StopTimer(ICarrier carrier)
		{
			Events.Instance.LateUpdate -= CheckTimer;
		}

		internal static UrgencyMarkerTarget GetUMT(AbstractCarryableObject Homunculus)
		{
			return ((Component)Homunculus).GetComponentInChildren<UrgencyMarkerTarget>();
		}

		internal static void CheckTimer(object sender, EventArgs e)
		{
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Expected O, but got Unknown
			if (time > Time.time)
			{
				return;
			}
			Events.Instance.LateUpdate -= CheckTimer;
			UrgencyMarker = (ObjectMarkerData)((IEnumerable<MarkerPosition>)ObjectMarkerViewer.Instance.visMarkers.Keys).FirstOrDefault((Func<MarkerPosition, bool>)((MarkerPosition thing) => thing is ObjectMarkerData && (Object)(object)thing.FollowTarget == (Object)(object)((Component)GetUMT(homunculus)).transform));
			if (UrgencyMarker != null)
			{
				((MarkerPosition)UrgencyMarker).FollowTarget = null;
				((MarkerPosition)UrgencyMarker).isFollowingTransform = false;
			}
			Messaging.Echo("Homunculus lost", false);
			((AbstractCloneStarObject)homunculus).Destroy();
			homunculus = null;
			ClientGame current = ClientGame.Current;
			object obj;
			if (current == null)
			{
				obj = null;
			}
			else
			{
				AbstractPlayerControlledShip playerShip = current.PlayerShip;
				if (playerShip == null)
				{
					obj = null;
				}
				else
				{
					GameObject gameObject = ((AbstractCloneStarObject)playerShip).GameObject;
					obj = ((gameObject != null) ? gameObject.GetComponentInChildren<HomunculusAndBiomassSocket>() : null);
				}
			}
			HomunculusAndBiomassSocket socket = (HomunculusAndBiomassSocket)obj;
			if ((Object)(object)socket == (Object)null)
			{
				StopTimer(null);
				homunculus = null;
			}
			else
			{
				Tools.DelayDo((Action)tryDispenseHomunculus, 1000.0);
			}
			void tryDispenseHomunculus()
			{
				if ((Object)(object)((CarryablesSocket)socket).Payload == (Object)null)
				{
					socket.SwitchToHomunculusSocket();
					socket.DispenseHomunculusNow();
					Messaging.Echo("New Homunculus dispensed", false);
				}
				else
				{
					Tools.DelayDo((Action)tryDispenseHomunculus, 1000.0);
				}
			}
		}
	}
	public class MyPluginInfo
	{
		public const string PLUGIN_GUID = "18107.HomunculusSafety";

		public const string PLUGIN_NAME = "HomunculusSafety";

		public const string USERS_PLUGIN_NAME = "Homunculus Safety";

		public const string PLUGIN_VERSION = "0.0.2";

		public const string PLUGIN_DESCRIPTION = "Recreates the homunculus if it gets lost. Host side.";

		public const string PLUGIN_ORIGINAL_AUTHOR = "18107";

		public const string PLUGIN_AUTHORS = "18107, Dragon";

		public const string PLUGIN_THUNDERSTORE_ID = "VoidCrewModdingTeam/Homunculus_Safety";
	}
	public class VoidManagerPlugin : VoidPlugin
	{
		public override MultiplayerType MPType => (MultiplayerType)14;

		public override string Author => "18107, Dragon";

		public override string Description => "Recreates the homunculus if it gets lost. Host side.";

		public override string ThunderstoreID => "VoidCrewModdingTeam/Homunculus_Safety";

		public VoidManagerPlugin()
		{
			Events.Instance.HostStartSession += delegate
			{
				HomunculusManager.StopTimer(null);
				HomunculusManager.homunculus = null;
			};
			Events.Instance.MasterClientSwitched += delegate
			{
				if (PhotonNetwork.IsMasterClient)
				{
					HomunculusManager.homunculus = null;
					List<ResourceContainer> list = (from obj in Object.FindObjectsOfType<ResourceContainer>()
						where ((Object)obj).name.Contains("Item_Homunculus")
						select obj).ToList();
					if (list.Count > 1)
					{
						ResourceContainer val = null;
						foreach (ResourceContainer item in list)
						{
							ICarrier carrier = ((AbstractCarryableObject)item).Carrier;
							ICarrier obj2 = ((carrier is CarryablesSocket) ? carrier : null);
							if (obj2 != null && (((Object)obj2).name?.Contains("CentralShipComputer")).GetValueOrDefault())
							{
								val = item;
							}
						}
						if ((Object)(object)val != (Object)null)
						{
							list.Remove(val);
							HomunculusManager.homunculus = (AbstractCarryableObject)(object)val;
						}
						for (int num = list.Count - 1; num >= 0; num--)
						{
							ResourceContainer h = list.ElementAt(num);
							ICarrier carrier2 = ((AbstractCarryableObject)h).Carrier;
							if (carrier2 != null)
							{
								carrier2.TryReleaseCarryable();
							}
							Tools.DelayDo((Action)delegate
							{
								((AbstractCloneStarObject)h).Destroy();
							}, 1000.0);
						}
						if ((Object)(object)val == (Object)null)
						{
							ClientGame current2 = ClientGame.Current;
							object obj3;
							if (current2 == null)
							{
								obj3 = null;
							}
							else
							{
								AbstractPlayerControlledShip playerShip = current2.PlayerShip;
								if (playerShip == null)
								{
									obj3 = null;
								}
								else
								{
									GameObject gameObject = ((AbstractCloneStarObject)playerShip).GameObject;
									obj3 = ((gameObject != null) ? gameObject.GetComponentInChildren<HomunculusAndBiomassSocket>() : null);
								}
							}
							HomunculusAndBiomassSocket val2 = (HomunculusAndBiomassSocket)obj3;
							val2.DispenseHomunculusNow();
							Messaging.Echo("New Homunculus dispensed", false);
						}
					}
					else if (list.Count == 0)
					{
						ClientGame current3 = ClientGame.Current;
						object obj4;
						if (current3 == null)
						{
							obj4 = null;
						}
						else
						{
							AbstractPlayerControlledShip playerShip2 = current3.PlayerShip;
							if (playerShip2 == null)
							{
								obj4 = null;
							}
							else
							{
								GameObject gameObject2 = ((AbstractCloneStarObject)playerShip2).GameObject;
								obj4 = ((gameObject2 != null) ? gameObject2.GetComponentInChildren<HomunculusAndBiomassSocket>() : null);
							}
						}
						HomunculusAndBiomassSocket val3 = (HomunculusAndBiomassSocket)obj4;
						val3.DispenseHomunculusNow();
						Messaging.Echo("New Homunculus dispensed", false);
					}
					else
					{
						HomunculusManager.homunculus = (AbstractCarryableObject)(object)list.First();
					}
					HomunculusManager.homunculus.OnDropped += HomunculusManager.StartTimer;
					HomunculusManager.homunculus.OnCarried += HomunculusManager.StopTimer;
				}
			};
		}

		public override SessionChangedReturn OnSessionChange(SessionChangedInput input)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			SessionChangedReturn result = default(SessionChangedReturn);
			result.SetMod_Session = true;
			return result;
		}
	}
}