Decompiled source of TerminalStayAwake v1.0.0

TerminalStayAwake.dll

Decompiled 3 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 Agents;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using GTFO.API;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppSystem.Collections.Generic;
using LevelGeneration;
using Microsoft.CodeAnalysis;
using Player;
using SNetwork;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("TerminalStayAwake")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+b42ed5e7f7bb2b111ebaabc255b3ad545205c981")]
[assembly: AssemblyProduct("TerminalStayAwake")]
[assembly: AssemblyTitle("TerminalStayAwake")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 TerminalStayAwake
{
	[BepInPlugin("sourmonkis.TerminalStayAwake", "TerminalStayAwake", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		public const string GUID = "sourmonkis.TerminalStayAwake";

		public const string NAME = "TerminalStayAwake";

		public const string VERSION = "1.0.0";

		internal static ManualLogSource L;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<int> MaxAwake;

		public override void Load()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Expected O, but got Unknown
			L = ((BasePlugin)this).Log;
			Enabled = ((BasePlugin)this).Config.Bind<bool>("General", "Enabled", true, "Keep terminals awake when you walk away from them. Client-side: works as host or client, nobody else needs the mod.");
			MaxAwake = ((BasePlugin)this).Config.Bind<int>("General", "MaxAwakeTerminals", 2, new ConfigDescription("How many terminals may stay awake at once after you left them. When one more is kept, the oldest goes to sleep.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 8), Array.Empty<object>()));
			LevelAPI.OnLevelCleanup += KeepAwake.Clear;
			LevelAPI.OnBuildStart += KeepAwake.Clear;
			bool flag = default(bool);
			try
			{
				new Harmony("sourmonkis.TerminalStayAwake").PatchAll(typeof(Plugin).Assembly);
			}
			catch (Exception ex)
			{
				ManualLogSource log = ((BasePlugin)this).Log;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(31, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Patching failed, ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("TerminalStayAwake");
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is inactive: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				log.LogError(val);
				return;
			}
			ManualLogSource log2 = ((BasePlugin)this).Log;
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(22, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("TerminalStayAwake");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("1.0.0");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" loaded (max awake: ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(MaxAwake.Value);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(")");
			}
			log2.LogInfo(val2);
		}
	}
	internal static class KeepAwake
	{
		private const float OtherPlayerRadius = 4f;

		private static readonly List<LG_ComputerTerminal> s_kept = new List<LG_ComputerTerminal>();

		internal static void Clear()
		{
			s_kept.Clear();
		}

		private static bool Alive(LG_ComputerTerminal t)
		{
			try
			{
				return (Object)(object)t != (Object)null && !((Il2CppObjectBase)t).WasCollected && ((Il2CppObjectBase)t).Pointer != IntPtr.Zero && (Object)(object)((Component)t).gameObject != (Object)null;
			}
			catch
			{
				return false;
			}
		}

		private static bool Same(LG_ComputerTerminal a, LG_ComputerTerminal b)
		{
			try
			{
				return (Object)(object)a != (Object)null && !((Il2CppObjectBase)a).WasCollected && ((Il2CppObjectBase)a).Pointer == ((Il2CppObjectBase)b).Pointer;
			}
			catch
			{
				return false;
			}
		}

		private static void Remove(LG_ComputerTerminal t)
		{
			s_kept.RemoveAll((LG_ComputerTerminal k) => !Alive(k) || Same(k, t));
		}

		private static bool OtherPlayerNear(LG_ComputerTerminal t)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			List<PlayerAgent> playerAgentsInLevel = PlayerManager.PlayerAgentsInLevel;
			if (playerAgentsInLevel == null)
			{
				return false;
			}
			Vector3 position = ((Component)t).transform.position;
			float num = 16f;
			for (int i = 0; i < playerAgentsInLevel.Count; i++)
			{
				PlayerAgent val = playerAgentsInLevel[i];
				if ((Object)(object)val == (Object)null || ((Agent)val).IsLocallyOwned || !((Agent)val).Alive)
				{
					continue;
				}
				SNet_Player owner = val.Owner;
				if (!((Object)(object)owner == (Object)null) && !owner.IsBot)
				{
					Vector3 val2 = ((Agent)val).Position - position;
					if (((Vector3)(ref val2)).sqrMagnitude <= num)
					{
						return true;
					}
				}
			}
			return false;
		}

		private static void Sleep(LG_ComputerTerminal t)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Invalid comparison between Unknown and I4
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			if (!Alive(t) || (int)t.CurrentStateName != 1)
			{
				return;
			}
			bool flag = default(bool);
			BepInExDebugLogInterpolatedStringHandler val;
			if (OtherPlayerNear(t))
			{
				ManualLogSource l = Plugin.L;
				val = new BepInExDebugLogInterpolatedStringHandler(59, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Terminal ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(t.m_serialNumber);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" released without sleeping, another player is near");
				}
				l.LogDebug(val);
				return;
			}
			ManualLogSource l2 = Plugin.L;
			val = new BepInExDebugLogInterpolatedStringHandler(31, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Terminal ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(t.m_serialNumber);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" put to sleep (limit ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(Plugin.MaxAwake.Value);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
			}
			l2.LogDebug(val);
			t.ChangeState((TERM_State)0);
		}

		internal static bool TryKeep(LG_ComputerTerminal t)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Invalid comparison between Unknown and I4
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			if (!Plugin.Enabled.Value || !Alive(t) || (int)t.CurrentStateName != 1)
			{
				return false;
			}
			Remove(t);
			s_kept.Add(t);
			while (s_kept.Count > Plugin.MaxAwake.Value)
			{
				LG_ComputerTerminal t2 = s_kept[0];
				s_kept.RemoveAt(0);
				try
				{
					Sleep(t2);
				}
				catch (Exception ex)
				{
					Plugin.L.LogError((object)ex);
				}
			}
			ManualLogSource l = Plugin.L;
			bool flag = default(bool);
			BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(24, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Terminal ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(t.m_serialNumber);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" kept awake (");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(s_kept.Count);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("/");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(Plugin.MaxAwake.Value);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
			}
			l.LogDebug(val);
			return true;
		}

		internal static void Release(LG_ComputerTerminal t)
		{
			Remove(t);
		}
	}
	[HarmonyPatch(typeof(LG_ComputerTerminal), "OnProximityExit")]
	internal static class Patch_OnProximityExit
	{
		private static bool Prefix(LG_ComputerTerminal __instance)
		{
			try
			{
				return !KeepAwake.TryKeep(__instance);
			}
			catch (Exception ex)
			{
				Plugin.L.LogError((object)ex);
				return true;
			}
		}
	}
	[HarmonyPatch(typeof(LG_ComputerTerminal), "OnProximityEnter")]
	internal static class Patch_OnProximityEnter
	{
		private static void Postfix(LG_ComputerTerminal __instance)
		{
			try
			{
				KeepAwake.Release(__instance);
			}
			catch (Exception ex)
			{
				Plugin.L.LogError((object)ex);
			}
		}
	}
}