Decompiled source of LethalObjectiveMod v1.0.0

BepInEx/plugins/LethalObjectiveMod/LCObjectiveMod.dll

Decompiled 9 months 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 BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LCObjectiveMod.GameRound;
using LCObjectiveMod.Objectives;
using LCObjectiveMod.Patches;
using LethalNetworkAPI;
using LethalObjectiveMod;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LCObjectiveMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LCObjectiveMod")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("b25f8f8e-e5ca-4512-8fb2-ef9d17dd9635")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LethalObjectiveMod
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("LethalObjectiveMod", "LethalObjectiveMod", "0.0.1")]
	public class ObjectiveModBaseUnityPlugin : BaseUnityPlugin
	{
		private const string _sModGUID = "LethalObjectiveMod";

		private const string _sModName = "LethalObjectiveMod";

		private const string _sModVersion = "0.0.1";

		private readonly Harmony _harmony = new Harmony("LethalObjectiveMod");

		private static ObjectiveModBaseUnityPlugin _objectiveModBaseUnityPlugin;

		private static ManualLogSource _manualLogSource;

		private static TextMeshProUGUI _objectiveTextMeshProUGUI;

		private static ObjectiveManager _objectiveManager;

		private static GameRoundManager _gameRoundManager;

		private static LethalServerMessage<int> _objectiveFailureServerMessage;

		private static LethalClientMessage<int> _objectiveFailureClientMessage;

		private static LethalClientMessage<int> _objectiveTypeClientMessage;

		private void Awake()
		{
			if (!Object.op_Implicit((Object)(object)_objectiveModBaseUnityPlugin))
			{
				_objectiveModBaseUnityPlugin = this;
			}
			_objectiveManager = new ObjectiveManager();
			_gameRoundManager = new GameRoundManager();
			_manualLogSource = Logger.CreateLogSource("LethalObjectiveMod");
			_harmony.PatchAll(typeof(ObjectiveModBaseUnityPlugin));
			_harmony.PatchAll(typeof(HUDManagerPatch));
			_harmony.PatchAll(typeof(PlayerControllerBPatch));
			_harmony.PatchAll(typeof(StartOfRoundPatch));
			_objectiveFailureServerMessage = new LethalServerMessage<int>("objective_failure", (Action<int, ulong>)null);
			_objectiveFailureServerMessage.OnReceived += ObjectiveFailureReceivedFromClient;
			_objectiveFailureClientMessage = new LethalClientMessage<int>("objective_failure", (Action<int>)null, (Action<int, ulong>)null);
			_objectiveFailureClientMessage.OnReceived += ObjectiveFailureReceivedFromServer;
			_objectiveTypeClientMessage = new LethalClientMessage<int>("objective_type", (Action<int>)null, (Action<int, ulong>)null);
			_objectiveTypeClientMessage.OnReceivedFromClient += ObjectiveTypeReceivedFromClient;
			LogInfo("Mod has been loaded");
		}

		internal static LethalClientMessage<int> GetObjectiveFailureClientMessage()
		{
			return _objectiveFailureClientMessage;
		}

		internal static LethalClientMessage<int> GetObjectiveTypeClientMessage()
		{
			return _objectiveTypeClientMessage;
		}

		internal static ObjectiveManager GetObjectiveManager()
		{
			return _objectiveManager;
		}

		internal static TextMeshProUGUI GetObjectiveTextMeshProUGUI()
		{
			return _objectiveTextMeshProUGUI;
		}

		internal static GameRoundManager GetGameRoundManager()
		{
			return _gameRoundManager;
		}

		internal static void SetObjectiveTextMeshProUGUI(TextMeshProUGUI iTextMeshProUGUI)
		{
			_objectiveTextMeshProUGUI = iTextMeshProUGUI;
		}

		private void ObjectiveFailureReceivedFromClient(int inData, ulong clientId)
		{
			_objectiveFailureServerMessage.SendAllClients(inData, true);
		}

		private void ObjectiveFailureReceivedFromServer(int inData)
		{
			_objectiveManager.SetActiveObjectiveStatus(eObjectiveStatus.failed);
		}

		private void ObjectiveTypeReceivedFromClient(int inData, ulong iuClientId)
		{
			_objectiveManager.SetActiveObjective((eObjectiveType)inData, iuClientId);
		}

		private void ObjectiveUpdateReceivedFromClient(int inData, ulong iuClientId)
		{
			_objectiveManager.GetActiveObjective().Update();
		}

		internal static void LogInfo(string isLogMessage)
		{
			_manualLogSource.LogInfo((object)(isLogMessage ?? ""));
		}
	}
}
namespace LCObjectiveMod.GameRound
{
	internal class GameRoundManager
	{
		private bool _bDoorHasBeenOpened;

		private bool _bPlayerHasBeenCrouch;

		private bool _bPlayerHasJump;

		private bool _bPlayerKilled;

		private bool _bDeadBodyInShip;

		public void Reset()
		{
			_bDoorHasBeenOpened = false;
			_bPlayerHasBeenCrouch = false;
			_bPlayerHasJump = false;
			_bPlayerKilled = false;
			_bDeadBodyInShip = false;
		}

		public bool GetDoorHasBeenOpened()
		{
			return _bDoorHasBeenOpened;
		}

		public bool GetPlayerHasBeenCrouch()
		{
			return _bPlayerHasBeenCrouch;
		}

		public bool GetPlayerHasJump()
		{
			return _bPlayerHasJump;
		}

		public bool GetPlayerKilled()
		{
			return _bPlayerKilled;
		}

		public bool GetPlayerDeadBodyInShip()
		{
			return _bDeadBodyInShip;
		}

		public void SetDoorHasBeenOpened(bool ibValue)
		{
			_bDoorHasBeenOpened = ibValue;
		}

		public void SetPlayerHasBeenCrouch(bool ibValue)
		{
			_bPlayerHasBeenCrouch = ibValue;
		}

		public void SetPlayerHasJump(bool ibValue)
		{
			_bPlayerHasJump = ibValue;
		}

		public void SetPlayerKilled(bool ibValue)
		{
			_bPlayerKilled = ibValue;
		}

		public void SetPlayerDeadBodyInShip(bool ibValue)
		{
			_bDeadBodyInShip = ibValue;
		}
	}
}
namespace LCObjectiveMod.Patches
{
	[HarmonyPatch(typeof(DoorLock))]
	internal class DoorLockPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(DoorLock), "UnlockDoorSyncWithServer")]
		public static void UnlockDoorSyncWithServerPatch(DoorLock __instance)
		{
			ObjectiveModBaseUnityPlugin.GetGameRoundManager().SetDoorHasBeenOpened(ibValue: true);
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("StartGame")]
		[HarmonyPostfix]
		private static void StartGamePatch(StartOfRound __instance)
		{
			ObjectiveModBaseUnityPlugin.GetGameRoundManager().Reset();
			ObjectiveManager objectiveManager = ObjectiveModBaseUnityPlugin.GetObjectiveManager();
			if (0 < objectiveManager.GetObjectives().Count)
			{
				objectiveManager.ResetObjectives();
				int objectiveType = (int)objectiveManager.GetObjectives()[0].GetObjectiveType();
				ObjectiveModBaseUnityPlugin.GetObjectiveTypeClientMessage().SendAllClients(objectiveType, true, false);
			}
		}

		[HarmonyPatch("ShipLeave")]
		[HarmonyPrefix]
		private static void ShipLeavePatch()
		{
			ObjectiveModBaseUnityPlugin.GetObjectiveManager().GetActiveObjective().Update();
		}

		[HarmonyPatch("ShipHasLeft")]
		[HarmonyPostfix]
		private static void ShipHasLeftPatch()
		{
			IObjective activeObjective = ObjectiveModBaseUnityPlugin.GetObjectiveManager().GetActiveObjective();
			if (activeObjective != null)
			{
				ObjectiveModBaseUnityPlugin.GetObjectiveManager().UpdateActiveObjectiveUIDescription();
				if (activeObjective.GetClientIdWhoStartObjective() == PlayerControllerBPatch.GetClientId() && eObjectiveStatus.succeeded == activeObjective.GetStatus())
				{
					Terminal val = Object.FindObjectOfType<Terminal>();
					val.SyncGroupCreditsServerRpc(val.groupCredits + activeObjective.GetCredit(), val.numberOfItemsInDropship);
				}
			}
		}
	}
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDManagerPatch
	{
		[HarmonyPatch(typeof(HUDManager), "Start")]
		[HarmonyPostfix]
		private static void StartPatch(ref HUDManager __instance)
		{
			GameObject iParentGameObject = GameObject.Find("Systems/UI/Canvas/IngamePlayerHUD/HandsFullText");
			_InitObjectiveTextComponent(ref __instance, iParentGameObject);
		}

		private static void _InitObjectiveTextComponent(ref HUDManager iInstance, GameObject iParentGameObject)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI objectiveTextMeshProUGUI = ObjectiveModBaseUnityPlugin.GetObjectiveTextMeshProUGUI();
			if (!Object.op_Implicit((Object)(object)objectiveTextMeshProUGUI))
			{
				GameObject val = new GameObject("ObjectiveDisplay");
				val.transform.SetParent(iParentGameObject.transform, false);
				objectiveTextMeshProUGUI = val.AddComponent<TextMeshProUGUI>();
				((TMP_Text)objectiveTextMeshProUGUI).font = ((TMP_Text)iInstance.controlTipLines[0]).font;
				((TMP_Text)objectiveTextMeshProUGUI).fontSize = 12f;
				((Behaviour)objectiveTextMeshProUGUI).enabled = true;
				((TMP_Text)objectiveTextMeshProUGUI).alignment = (TextAlignmentOptions)514;
				((TMP_Text)objectiveTextMeshProUGUI).enableWordWrapping = false;
				ObjectiveModBaseUnityPlugin.SetObjectiveTextMeshProUGUI(objectiveTextMeshProUGUI);
				val.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, -35f);
				ObjectiveModBaseUnityPlugin.GetObjectiveManager().UpdateActiveObjectiveUIDescription();
			}
		}

		[HarmonyPatch(typeof(HUDManager), "DisplayDaysLeft")]
		[HarmonyPrefix]
		private static void DisplayDaysLeftPatch(ref HUDManager __instance)
		{
			ObjectiveModBaseUnityPlugin.GetObjectiveManager().ResetObjectives();
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		private static string _sName = "Player";

		private static ulong _uClientId = 0uL;

		[HarmonyPatch(typeof(PlayerControllerB), "Awake")]
		[HarmonyPostfix]
		public static void AwakePatch(PlayerControllerB __instance)
		{
			_sName = ((Object)__instance).name;
			_uClientId = __instance.actualClientId;
		}

		[HarmonyPatch(typeof(PlayerControllerB), "Update")]
		[HarmonyPostfix]
		public static void UpdatePatch()
		{
		}

		[HarmonyPatch(typeof(PlayerControllerB), "Crouch_performed")]
		[HarmonyPrefix]
		public static void Crouch_performedPatch(PlayerControllerB __instance, ref CallbackContext context)
		{
			ObjectiveModBaseUnityPlugin.GetGameRoundManager().SetPlayerHasBeenCrouch(ibValue: true);
		}

		[HarmonyPatch(typeof(PlayerControllerB), "Jump_performed")]
		[HarmonyPrefix]
		public static void Jump_performedPatch(ref CallbackContext context)
		{
			ObjectiveModBaseUnityPlugin.GetGameRoundManager().SetPlayerHasJump(ibValue: true);
		}

		[HarmonyPatch(typeof(PlayerControllerB), "KillPlayer")]
		[HarmonyPostfix]
		public static void KillPlayerPatch(PlayerControllerB __instance)
		{
			ObjectiveModBaseUnityPlugin.GetGameRoundManager().SetPlayerKilled(__instance.isPlayerDead);
		}

		public static ulong GetClientId()
		{
			return _uClientId;
		}
	}
}
namespace LCObjectiveMod.Objectives
{
	internal enum eObjectiveType
	{
		undefined,
		noCrouch,
		noDeathObjective,
		noDoorOpened,
		noJump,
		returnBodiesToShip
	}
	internal enum eObjectiveStatus
	{
		running,
		succeeded,
		failed
	}
	internal abstract class IObjective
	{
		internal string _sDescription;

		internal ulong _uClientIdWhoStartObjective;

		internal eObjectiveStatus _eObjectiveStatus = eObjectiveStatus.running;

		internal eObjectiveType _eObjectiveType = eObjectiveType.undefined;

		internal int _nCredit = 0;

		public abstract void Initialize();

		public abstract void Update();

		public void SetStatus(eObjectiveStatus ieObjectiveStatus)
		{
			_eObjectiveStatus = ieObjectiveStatus;
		}

		public void SetClientIdWhoStartObjective(ulong iuClientIdWhoStartObjective)
		{
			_uClientIdWhoStartObjective = iuClientIdWhoStartObjective;
		}

		public string GetDescription()
		{
			return _sDescription;
		}

		public ulong GetClientIdWhoStartObjective()
		{
			return _uClientIdWhoStartObjective;
		}

		public eObjectiveStatus GetStatus()
		{
			return _eObjectiveStatus;
		}

		public eObjectiveType GetObjectiveType()
		{
			return _eObjectiveType;
		}

		public int GetCredit()
		{
			return _nCredit;
		}
	}
	internal class NoJump : IObjective
	{
		public override void Initialize()
		{
			_sDescription = "It is forbidden to jump";
			_eObjectiveType = eObjectiveType.noJump;
			_eObjectiveStatus = eObjectiveStatus.running;
			_uClientIdWhoStartObjective = 0uL;
			_nCredit = 50;
		}

		public override void Update()
		{
			if (eObjectiveStatus.failed != _eObjectiveStatus && ObjectiveModBaseUnityPlugin.GetGameRoundManager().GetPlayerHasJump())
			{
				ObjectiveModBaseUnityPlugin.GetObjectiveFailureClientMessage().SendServer(1);
			}
		}
	}
	internal class NoCrouch : IObjective
	{
		public override void Initialize()
		{
			_sDescription = "It is forbidden to crouch";
			_eObjectiveType = eObjectiveType.noCrouch;
			_eObjectiveStatus = eObjectiveStatus.running;
			_uClientIdWhoStartObjective = 0uL;
			_nCredit = 30;
		}

		public override void Update()
		{
			if (eObjectiveStatus.failed != _eObjectiveStatus && ObjectiveModBaseUnityPlugin.GetGameRoundManager().GetPlayerHasBeenCrouch())
			{
				ObjectiveModBaseUnityPlugin.GetObjectiveFailureClientMessage().SendServer(1);
			}
		}
	}
	internal class NoDoorOpened : IObjective
	{
		public override void Initialize()
		{
			_sDescription = "No door should be opened";
			_eObjectiveType = eObjectiveType.noDoorOpened;
			_eObjectiveStatus = eObjectiveStatus.running;
			_uClientIdWhoStartObjective = 0uL;
			_nCredit = 25;
		}

		public override void Update()
		{
			if (eObjectiveStatus.failed != _eObjectiveStatus && ObjectiveModBaseUnityPlugin.GetGameRoundManager().GetDoorHasBeenOpened())
			{
				ObjectiveModBaseUnityPlugin.GetObjectiveFailureClientMessage().SendServer(1);
			}
		}
	}
	internal class ReturnBodiesToShip : IObjective
	{
		public override void Initialize()
		{
			_sDescription = "Return all the corpses to the ship";
			_eObjectiveType = eObjectiveType.returnBodiesToShip;
			_eObjectiveStatus = eObjectiveStatus.running;
			_uClientIdWhoStartObjective = 0uL;
			_nCredit = 65;
		}

		public override void Update()
		{
			if (eObjectiveStatus.failed != _eObjectiveStatus && ObjectiveModBaseUnityPlugin.GetGameRoundManager().GetPlayerKilled())
			{
				PlayerControllerB val = Object.FindObjectOfType<PlayerControllerB>();
				if ((Object)null != (Object)(object)val && !val.deadBody.isInShip)
				{
					ObjectiveModBaseUnityPlugin.GetObjectiveFailureClientMessage().SendServer(1);
				}
			}
		}
	}
	internal class NoDeathObjective : IObjective
	{
		public override void Initialize()
		{
			_sDescription = "No player should die";
			_eObjectiveType = eObjectiveType.noDeathObjective;
			_eObjectiveStatus = eObjectiveStatus.running;
			_uClientIdWhoStartObjective = 0uL;
			_nCredit = 50;
		}

		public override void Update()
		{
			if (eObjectiveStatus.failed != _eObjectiveStatus && ObjectiveModBaseUnityPlugin.GetGameRoundManager().GetPlayerKilled())
			{
				ObjectiveModBaseUnityPlugin.GetObjectiveFailureClientMessage().SendServer(1);
			}
		}
	}
	internal class ObjectiveManager
	{
		private IObjective _activeObjective;

		private List<IObjective> _listObjectives = new List<IObjective>();

		public ObjectiveManager()
		{
			_listObjectives.Add(new NoCrouch());
			_listObjectives.Add(new NoJump());
			_listObjectives.Add(new NoDoorOpened());
			_listObjectives.Add(new NoDeathObjective());
			_listObjectives.Add(new ReturnBodiesToShip());
		}

		public IObjective GetActiveObjective()
		{
			return _activeObjective;
		}

		public List<IObjective> GetObjectives()
		{
			return _listObjectives;
		}

		public void SetActiveObjective(eObjectiveType ieObjectiveType, ulong iuClientIdWhoStartObjective)
		{
			ResetObjectives();
			if (ieObjectiveType != 0)
			{
				foreach (IObjective listObjective in _listObjectives)
				{
					if (ieObjectiveType == listObjective.GetObjectiveType())
					{
						_activeObjective = listObjective;
						_activeObjective.SetClientIdWhoStartObjective(iuClientIdWhoStartObjective);
						break;
					}
				}
			}
			UpdateActiveObjectiveUIDescription();
			SetActiveObjectiveStatus(eObjectiveStatus.succeeded);
		}

		public void SetActiveObjectiveStatus(eObjectiveStatus ieObjectiveStatus)
		{
			GetActiveObjective()?.SetStatus(ieObjectiveStatus);
		}

		public void UpdateActiveObjectiveUIDescription()
		{
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			IObjective activeObjective = GetActiveObjective();
			if (activeObjective != null)
			{
				if (activeObjective.GetStatus() == eObjectiveStatus.running)
				{
					Color color = default(Color);
					ColorUtility.TryParseHtmlString("#dba502", ref color);
					((Graphic)ObjectiveModBaseUnityPlugin.GetObjectiveTextMeshProUGUI()).color = color;
				}
				else if (eObjectiveStatus.succeeded == activeObjective.GetStatus())
				{
					Color color2 = default(Color);
					ColorUtility.TryParseHtmlString("#2cc92c", ref color2);
					((Graphic)ObjectiveModBaseUnityPlugin.GetObjectiveTextMeshProUGUI()).color = color2;
				}
				else if (eObjectiveStatus.failed == activeObjective.GetStatus())
				{
					Color color3 = default(Color);
					ColorUtility.TryParseHtmlString("#c70000", ref color3);
					((Graphic)ObjectiveModBaseUnityPlugin.GetObjectiveTextMeshProUGUI()).color = color3;
				}
				((TMP_Text)ObjectiveModBaseUnityPlugin.GetObjectiveTextMeshProUGUI()).text = $"Objective: {activeObjective.GetDescription()} [{activeObjective.GetCredit()} crédits]";
			}
			else
			{
				TextMeshProUGUI objectiveTextMeshProUGUI = ObjectiveModBaseUnityPlugin.GetObjectiveTextMeshProUGUI();
				Color color4 = default(Color);
				ColorUtility.TryParseHtmlString("#dba502", ref color4);
				((Graphic)objectiveTextMeshProUGUI).color = color4;
				((TMP_Text)objectiveTextMeshProUGUI).text = "Objective : Not yet available";
			}
		}

		public void UpdateActiveObjective()
		{
			if (_activeObjective != null)
			{
				_activeObjective.Update();
			}
		}

		public void ResetObjectives()
		{
			foreach (IObjective listObjective in _listObjectives)
			{
				listObjective.Initialize();
			}
			_Shuffle(_listObjectives);
			_activeObjective = null;
			UpdateActiveObjectiveUIDescription();
		}

		private static void _Shuffle<T>(IList<T> list)
		{
			Random random = new Random();
			int num = list.Count;
			while (num > 1)
			{
				num--;
				int index = random.Next(num + 1);
				T value = list[index];
				list[index] = list[num];
				list[num] = value;
			}
		}
	}
}