Decompiled source of DeathLink v1.2.0

DeathLink.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DeathLink")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DeathLink")]
[assembly: AssemblyTitle("DeathLink")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 DeathLink
{
	[BepInPlugin("DeathLink", "DeathLink", "1.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> ResetSave;

		private void Awake()
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Deathlink status (host's setting applies to the lobby).");
			ResetSave = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ResetSaveOnDeath", false, "On a DeathLink death, the host's save is reset to day 1 (quota, money, ship loot).");
			new Harmony("DeathLink").PatchAll();
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "Start")]
	internal static class HostSync
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static HandleNamedMessageDelegate <>9__2_1;

			internal void <Postfix>b__2_1(ulong _, FastBufferReader reader)
			{
				//IL_0009: Unknown result type (might be due to invalid IL or missing references)
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_001e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref HostEnabled, default(ForPrimitives));
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref HostReset, default(ForPrimitives));
			}
		}

		internal static bool HostEnabled;

		internal static bool HostReset;

		private unsafe static void Postfix()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager singleton = NetworkManager.Singleton;
			CustomMessagingManager msg = singleton.CustomMessagingManager;
			if (singleton.IsServer)
			{
				HostEnabled = Plugin.Enabled.Value;
				HostReset = Plugin.ResetSave.Value;
				msg.RegisterNamedMessageHandler("DeathLink_Request", (HandleNamedMessageDelegate)delegate(ulong sender, FastBufferReader _)
				{
					//IL_0013: Unknown result type (might be due to invalid IL or missing references)
					//IL_0019: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Unknown result type (might be due to invalid IL or missing references)
					//IL_002e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0040: Unknown result type (might be due to invalid IL or missing references)
					FastBufferWriter val3 = default(FastBufferWriter);
					((FastBufferWriter)(ref val3))..ctor(2, (Allocator)2, -1);
					try
					{
						((FastBufferWriter)(ref val3)).WriteValueSafe<bool>(ref HostEnabled, default(ForPrimitives));
						((FastBufferWriter)(ref val3)).WriteValueSafe<bool>(ref HostReset, default(ForPrimitives));
						msg.SendNamedMessage("DeathLink_State", sender, val3, (NetworkDelivery)3);
					}
					finally
					{
						((IDisposable)(*(FastBufferWriter*)(&val3))/*cast due to .constrained prefix*/).Dispose();
					}
				});
				return;
			}
			HostEnabled = (HostReset = false);
			CustomMessagingManager obj = msg;
			object obj2 = <>c.<>9__2_1;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val = delegate(ulong _, FastBufferReader reader)
				{
					//IL_0009: Unknown result type (might be due to invalid IL or missing references)
					//IL_000f: Unknown result type (might be due to invalid IL or missing references)
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Unknown result type (might be due to invalid IL or missing references)
					((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref HostEnabled, default(ForPrimitives));
					((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref HostReset, default(ForPrimitives));
				};
				<>c.<>9__2_1 = val;
				obj2 = (object)val;
			}
			obj.RegisterNamedMessageHandler("DeathLink_State", (HandleNamedMessageDelegate)obj2);
			FastBufferWriter val2 = default(FastBufferWriter);
			((FastBufferWriter)(ref val2))..ctor(1, (Allocator)2, -1);
			try
			{
				msg.SendNamedMessage("DeathLink_Request", 0uL, val2, (NetworkDelivery)3);
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val2))/*cast due to .constrained prefix*/).Dispose();
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "Update")]
	internal static class DeathWatcher
	{
		private static readonly bool[] wasDead = new bool[64];

		private static bool triggered;

		internal static bool resetPending;

		private static void Postfix(StartOfRound __instance)
		{
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			PlayerControllerB[] allPlayerScripts = __instance.allPlayerScripts;
			if (allPlayerScripts == null)
			{
				return;
			}
			if (__instance.inShipPhase)
			{
				triggered = false;
			}
			for (int i = 0; i < allPlayerScripts.Length && i < wasDead.Length; i++)
			{
				PlayerControllerB val = allPlayerScripts[i];
				bool flag = (Object)(object)val != (Object)null && val.isPlayerDead;
				bool num = flag && !wasDead[i];
				wasDead[i] = flag;
				if (num && !triggered && HostSync.HostEnabled && !__instance.inShipPhase && !__instance.shipIsLeaving)
				{
					triggered = true;
					if (HostSync.HostReset)
					{
						resetPending = true;
					}
					PlayerControllerB val2 = GameNetworkManager.Instance?.localPlayerController;
					if ((Object)(object)val2 != (Object)null && !val2.isPlayerDead && val2.isPlayerControlled)
					{
						val2.KillPlayer(Vector3.zero, true, (CauseOfDeath)0, 0, default(Vector3), false);
					}
					ShowMessage(val.playerUsername + " died.");
				}
			}
		}

		private static void ShowMessage(string text)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			HUDManager instance = HUDManager.Instance;
			GameObject val = new GameObject("DeathLinkMessage");
			Canvas obj = val.AddComponent<Canvas>();
			obj.renderMode = (RenderMode)0;
			obj.sortingOrder = 999;
			TextMeshProUGUI val2 = new GameObject("Text").AddComponent<TextMeshProUGUI>();
			((TMP_Text)val2).transform.SetParent(val.transform, false);
			if ((Object)(object)instance != (Object)null)
			{
				((TMP_Text)val2).font = ((TMP_Text)instance.tipsPanelBody).font;
			}
			((TMP_Text)val2).text = "<size=60%>DEATHLINK</size>\n" + text;
			((TMP_Text)val2).fontSize = 48f;
			((Graphic)val2).color = new Color(1f, 0.25f, 0.2f);
			((TMP_Text)val2).alignment = (TextAlignmentOptions)514;
			RectTransform rectTransform = ((TMP_Text)val2).rectTransform;
			rectTransform.anchorMin = new Vector2(0f, 0.6f);
			rectTransform.anchorMax = new Vector2(1f, 0.85f);
			Vector2 offsetMin = (rectTransform.offsetMax = Vector2.zero);
			rectTransform.offsetMin = offsetMin;
			if ((Object)(object)instance != (Object)null && instance.warningSFX.Length != 0)
			{
				instance.UIAudio.PlayOneShot(instance.warningSFX[0]);
			}
			Object.Destroy((Object)(object)val, 5f);
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "SetShipReadyToLand")]
	internal static class SaveReset
	{
		private static void Postfix(StartOfRound __instance)
		{
			if (DeathWatcher.resetPending)
			{
				DeathWatcher.resetPending = false;
				if (NetworkManager.Singleton.IsServer)
				{
					RoundManager.Instance.DespawnPropsAtEndOfRound(true);
					GameNetworkManager.Instance.ResetSavedGameValues();
				}
				Terminal obj = Object.FindObjectOfType<Terminal>();
				obj.ClearBoughtItems();
				__instance.ResetShip();
				obj.SetItemSales();
				EndOfGameStats gameStats = __instance.gameStats;
				gameStats.daysSpent = 0;
				gameStats.scrapValueCollected = 0;
				gameStats.deaths = 0;
				gameStats.allStepsTaken = 0;
				ShowReset();
			}
		}

		private static void ShowReset()
		{
			HUDManager instance = HUDManager.Instance;
			if (instance != null)
			{
				instance.DisplayTip("DeathLink", "Save reset. Back to day 1.", true, false, "LC_Tip1");
			}
		}
	}
	[HarmonyPatch(typeof(MenuManager), "Start")]
	internal static class MenuSettings
	{
		private static void Postfix(MenuManager __instance)
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Expected O, but got Unknown
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Expected O, but got Unknown
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			GameObject menu = __instance.menuButtons;
			if (__instance.isInitScene || (Object)(object)menu == (Object)null)
			{
				return;
			}
			Button val = null;
			Button[] componentsInChildren = menu.GetComponentsInChildren<Button>(true);
			foreach (Button val2 in componentsInChildren)
			{
				if ((Object)(object)((Component)val2).transform.parent == (Object)(object)menu.transform && ((Object)(object)val == (Object)null || ((Component)val2).transform.localPosition.y > ((Component)val).transform.localPosition.y))
				{
					val = val2;
				}
			}
			if (!((Object)(object)val == (Object)null))
			{
				Rect rect = ((RectTransform)((Component)val).transform).rect;
				float num = ((Rect)(ref rect)).height * 1.3f;
				GameObject panel = new GameObject("DeathLinkSettings", new Type[1] { typeof(RectTransform) });
				RectTransform val3 = (RectTransform)panel.transform;
				RectTransform val4 = (RectTransform)menu.transform;
				((Transform)val3).SetParent(((Transform)val4).parent, false);
				val3.anchorMin = val4.anchorMin;
				val3.anchorMax = val4.anchorMax;
				val3.pivot = val4.pivot;
				val3.anchoredPosition = val4.anchoredPosition;
				val3.sizeDelta = val4.sizeDelta;
				((Transform)val3).localScale = ((Transform)val4).localScale;
				((Transform)val3).localRotation = ((Transform)val4).localRotation;
				panel.SetActive(false);
				Toggle(val, panel.transform, 0f, "Deathlink status", Plugin.Enabled);
				Toggle(val, panel.transform, 0f - num, "Reset save on death", Plugin.ResetSave);
				Clone(val, panel.transform, (0f - num) * 2f, "> Back", delegate
				{
					panel.SetActive(false);
					menu.SetActive(true);
				});
				Clone(val, menu.transform, num, "> DeathLink Settings", delegate
				{
					menu.SetActive(false);
					panel.SetActive(true);
				});
			}
		}

		private static void Toggle(Button template, Transform parent, float y, string label, ConfigEntry<bool> entry)
		{
			TextMeshProUGUI text = null;
			text = Clone(template, parent, y, "", delegate
			{
				entry.Value = !entry.Value;
				Refresh();
			});
			Refresh();
			void Refresh()
			{
				((TMP_Text)text).text = "> " + label + ": " + (entry.Value ? "<color=#3CFF3C>[ ON ]</color>" : "<color=#FF3C3C>[ OFF ]</color>");
			}
		}

		private static TextMeshProUGUI Clone(Button template, Transform parent, float y, string label, Action onClick)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			GameObject obj = Object.Instantiate<GameObject>(((Component)template).gameObject, parent);
			((Object)obj).name = "DeathLink" + label;
			obj.transform.localPosition = ((Component)template).transform.localPosition + new Vector3(0f, y, 0f);
			obj.SetActive(true);
			Button component = obj.GetComponent<Button>();
			component.onClick = new ButtonClickedEvent();
			((UnityEvent)component.onClick).AddListener((UnityAction)delegate
			{
				onClick();
			});
			TextMeshProUGUI componentInChildren = obj.GetComponentInChildren<TextMeshProUGUI>();
			((TMP_Text)componentInChildren).text = label;
			return componentInChildren;
		}
	}
}