Decompiled source of EOSExt SecuritySensor v1.1.6

plugins/Inas07.EOSExt.SecuritySensor.dll

Decompiled 3 weeks 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 Agents;
using BepInEx;
using BepInEx.Unity.IL2CPP;
using ChainedPuzzles;
using EOSExt.SecuritySensor.Component;
using EOSExt.SecuritySensor.Definition;
using ExtraObjectiveSetup;
using ExtraObjectiveSetup.BaseClasses;
using ExtraObjectiveSetup.ExtendedWardenEvents;
using ExtraObjectiveSetup.Utils;
using FloLib.Networks.Replications;
using GTFO.API;
using GTFO.API.Extensions;
using GameData;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
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("Inas07.EOSExt.SecuritySensor")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Inas07.EOSExt.SecuritySensor")]
[assembly: AssemblyTitle("Inas07.EOSExt.SecuritySensor")]
[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 EOSExt.SecuritySensor
{
	internal static class Assets
	{
		public static GameObject CircleSensor { get; private set; }

		public static GameObject MovableSensor { get; private set; }

		public static void Init()
		{
			CircleSensor = AssetAPI.GetLoadedAsset<GameObject>("Assets/SecuritySensor/CircleSensor.prefab");
			MovableSensor = AssetAPI.GetLoadedAsset<GameObject>("Assets/SecuritySensor/MovableSensor.prefab");
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("Inas.EOSExt.SecuritySensor", "EOSExt.SecuritySensor", "1.1.6")]
	public class EntryPoint : BasePlugin
	{
		public const string AUTHOR = "Inas";

		public const string PLUGIN_NAME = "EOSExt.SecuritySensor";

		public const string VERSION = "1.1.6";

		private Harmony m_Harmony;

		public override void Load()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			SetupManagers();
			AssetAPI.OnAssetBundlesLoaded += Assets.Init;
			m_Harmony = new Harmony("EOSExt.SecuritySensor");
			m_Harmony.PatchAll();
			ClassInjector.RegisterTypeInIl2Cpp<SensorCollider>();
			EOSLogger.Log("ExtraObjectiveSetup.SecuritySensor loaded.");
		}

		private void SetupManagers()
		{
			((GenericExpeditionDefinitionManager<SensorGroupSettings>)SecuritySensorManager.Current).Init();
		}
	}
	internal class MovableSensor
	{
		public GameObject movableGO { get; private set; }

		public CP_BasicMovable movingComp { get; private set; }

		private GameObject graphicsGO
		{
			get
			{
				GameObject obj = movableGO;
				if (obj == null)
				{
					return null;
				}
				return ((Component)obj.transform.GetChild(0)).gameObject;
			}
		}

		public static MovableSensor Instantiate(SensorSettings sensorSetting)
		{
			//IL_0065: 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_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			if (sensorSetting.MovingPosition.Count < 1)
			{
				EOSLogger.Error("SensorGroup.Instantiate: At least 1 moving position required to setup T-Sensor!");
				return null;
			}
			if (EOSNetworking.AllotReplicatorID() == 0)
			{
				EOSLogger.Error("SensorGroup.Instantiate: replicator ID depleted, cannot create StateReplicator...");
				return null;
			}
			MovableSensor movableSensor = new MovableSensor();
			movableSensor.movableGO = Object.Instantiate<GameObject>(Assets.MovableSensor);
			movableSensor.movingComp = movableSensor.movableGO.GetComponent<CP_BasicMovable>();
			movableSensor.movingComp.Setup();
			Vector3 element = sensorSetting.Position.ToVector3();
			Vector3 val = sensorSetting.MovingPosition.First().ToVector3();
			Vector3 val2 = sensorSetting.MovingPosition.Last().ToVector3();
			IEnumerable<Vector3> source = sensorSetting.MovingPosition.ConvertAll((Vec3 e) => e.ToVector3()).AsEnumerable();
			if (!((Vector3)(ref element)).Equals(val))
			{
				source = source.Prepend(element);
			}
			if (!((Vector3)(ref element)).Equals(val2))
			{
				source = source.Append(element);
			}
			movableSensor.movingComp.ScanPositions = ListExtensions.ToIl2Cpp<Vector3>(source.ToList());
			movableSensor.movingComp.m_amountOfPositions = source.Count() - 1;
			if (sensorSetting.MovingSpeedMulti > 0f)
			{
				CP_BasicMovable obj = movableSensor.movingComp;
				obj.m_movementSpeed *= sensorSetting.MovingSpeedMulti;
			}
			return movableSensor;
		}

		public void StartMoving()
		{
			graphicsGO.SetActive(true);
			movingComp.SyncUpdate();
			movingComp.StartMoving();
		}

		public void ResumeMoving()
		{
			graphicsGO.SetActive(true);
			movingComp.ResumeMovement();
		}

		public void StopMoving()
		{
			movingComp.StopMoving();
			graphicsGO.SetActive(false);
		}

		public void PauseMoving()
		{
			movingComp.PauseMovement();
			graphicsGO.SetActive(false);
		}

		public void Destroy()
		{
			Object.Destroy((Object)(object)movableGO);
			movableGO = null;
			movingComp = null;
		}

		private MovableSensor()
		{
		}
	}
	public enum SensorEventType
	{
		ToggleSensorGroupState = 400
	}
	public sealed class SecuritySensorManager : GenericExpeditionDefinitionManager<SensorGroupSettings>
	{
		private List<(SensorGroupSettings settings, SensorGroup sensorGroup)> securitySensorGroups = new List<(SensorGroupSettings, SensorGroup)>();

		private Dictionary<IntPtr, int> sensorGroupIndex = new Dictionary<IntPtr, int>();

		public static SecuritySensorManager Current { get; private set; }

		protected override string DEFINITION_NAME => "SecuritySensor";

		public void BuildSensorGroup(SensorGroupSettings sensorGroupSettings)
		{
			int count = securitySensorGroups.Count;
			SensorGroup sensorGroup = SensorGroup.Instantiate(sensorGroupSettings, count);
			securitySensorGroups.Add((sensorGroupSettings, sensorGroup));
			foreach (GameObject basicSensor in sensorGroup.BasicSensors)
			{
				sensorGroupIndex[((Il2CppObjectBase)basicSensor).Pointer] = count;
			}
			foreach (MovableSensor movableSensor in sensorGroup.MovableSensors)
			{
				sensorGroupIndex[((Il2CppObjectBase)movableSensor.movableGO).Pointer] = count;
			}
			EOSLogger.Debug($"SensorGroup_{count} built");
		}

		internal void TriggerSensor(IntPtr pointer)
		{
			if (!sensorGroupIndex.ContainsKey(pointer))
			{
				EOSLogger.Error("Triggering a sensor but doesn't find its corresponding sensor group! This should not happen!");
				return;
			}
			int num = sensorGroupIndex[pointer];
			if (num < 0 || num >= securitySensorGroups.Count)
			{
				EOSLogger.Error($"TriggerSensor: invalid SensorGroup index {num}");
				return;
			}
			(SensorGroupSettings, SensorGroup) tuple = securitySensorGroups[num];
			if (tuple.Item2.StateReplicator.State.status == ActiveState.ENABLED)
			{
				tuple.Item1.EventsOnTrigger.ForEach(delegate(WardenObjectiveEventData e)
				{
					//IL_0002: Unknown result type (might be due to invalid IL or missing references)
					WardenObjectiveManager.CheckAndExecuteEventsOnTrigger(e, e.Trigger, true, 0f);
				});
				EOSLogger.Warning($"TriggerSensor: SensorGroup_{num} triggered");
			}
		}

		private void BuildSecuritySensor()
		{
			if (base.definitions.ContainsKey(RundownManager.ActiveExpedition.LevelLayoutData))
			{
				base.definitions[RundownManager.ActiveExpedition.LevelLayoutData].Definitions.ForEach(BuildSensorGroup);
			}
		}

		private void Clear()
		{
			securitySensorGroups.ForEach(delegate((SensorGroupSettings settings, SensorGroup sensorGroup) builtSensorGroup)
			{
				builtSensorGroup.sensorGroup.Destroy();
			});
			securitySensorGroups.Clear();
			sensorGroupIndex.Clear();
		}

		private void ToggleSensorGroup(WardenObjectiveEventData e)
		{
			int count = e.Count;
			bool enabled = e.Enabled;
			if (count < 0 || count >= securitySensorGroups.Count)
			{
				EOSLogger.Error($"ToggleSensorGroup: invalid SensorGroup index {count}");
			}
			else
			{
				securitySensorGroups[count].sensorGroup.ChangeState(enabled ? ActiveState.ENABLED : ActiveState.DISABLED);
			}
		}

		private SecuritySensorManager()
		{
			LevelAPI.OnBuildStart += delegate
			{
				Clear();
				BuildSecuritySensor();
			};
			LevelAPI.OnLevelCleanup += Clear;
			EventAPI.OnExpeditionStarted += delegate
			{
				securitySensorGroups.ForEach(delegate((SensorGroupSettings settings, SensorGroup sensorGroup) tuple)
				{
					tuple.sensorGroup.StartMovingMovables();
				});
			};
			EOSWardenEventManager.Current.AddEventDefinition(SensorEventType.ToggleSensorGroupState.ToString(), 400u, (Action<WardenObjectiveEventData>)ToggleSensorGroup);
		}

		static SecuritySensorManager()
		{
			Current = new SecuritySensorManager();
		}
	}
	internal class SensorGroup
	{
		private List<GameObject> basicSensors = new List<GameObject>();

		private List<MovableSensor> movableSensors = new List<MovableSensor>();

		public int sensorGroupIndex { get; private set; }

		public StateReplicator<SensorGroupState> StateReplicator { get; private set; }

		public IEnumerable<GameObject> BasicSensors => basicSensors;

		public IEnumerable<MovableSensor> MovableSensors => movableSensors;

		public void ChangeState(ActiveState status)
		{
			ChangeToStateUnsynced(new SensorGroupState
			{
				status = status
			});
			EOSLogger.Debug($"ChangeState: SecuritySensorGroup_{sensorGroupIndex} changed to state {status}");
			if (SNet.IsMaster)
			{
				StateReplicator.SetState(new SensorGroupState
				{
					status = status
				});
			}
		}

		private void OnStateChanged(SensorGroupState oldState, SensorGroupState newState, bool isRecall)
		{
			EOSLogger.Warning($"OnStateChanged: isRecall ? {isRecall}");
			if (isRecall)
			{
				EOSLogger.Debug($"Recalling: SecuritySensorGroup_{sensorGroupIndex} changed to state {newState.status}");
				ChangeToStateUnsynced(newState);
			}
		}

		private void ChangeToStateUnsynced(SensorGroupState newState)
		{
			switch (newState.status)
			{
			case ActiveState.ENABLED:
				basicSensors.ForEach(delegate(GameObject sensorGO)
				{
					sensorGO.SetActiveRecursively(true);
				});
				ResumeMovingMovables();
				break;
			case ActiveState.DISABLED:
				PauseMovingMovables();
				basicSensors.ForEach(delegate(GameObject sensorGO)
				{
					sensorGO.SetActiveRecursively(false);
				});
				break;
			}
		}

		public static SensorGroup Instantiate(SensorGroupSettings sensorGroupSettings, int sensorGroupIndex)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			SensorGroup sensorGroup = new SensorGroup();
			sensorGroup.sensorGroupIndex = sensorGroupIndex;
			foreach (SensorSettings item in sensorGroupSettings.SensorGroup)
			{
				Vector3 val = item.Position.ToVector3();
				if (val == Vector3.zeroVector)
				{
					continue;
				}
				GameObject val2 = null;
				switch (item.SensorType)
				{
				case SensorType.BASIC:
					val2 = Object.Instantiate<GameObject>(Assets.CircleSensor);
					sensorGroup.basicSensors.Add(val2);
					break;
				case SensorType.MOVABLE:
				{
					MovableSensor movableSensor = MovableSensor.Instantiate(item);
					if (movableSensor == null)
					{
						EOSLogger.Error("ERROR: failed to build movable sensor");
						continue;
					}
					val2 = movableSensor.movableGO;
					sensorGroup.movableSensors.Add(movableSensor);
					break;
				}
				default:
					EOSLogger.Error($"Unsupported SensorType {item.SensorType}, skipped");
					continue;
				}
				val2.transform.SetPositionAndRotation(val, Quaternion.identityQuaternion);
				float num = 0.16216217f;
				val2.transform.localScale = new Vector3(item.Radius, item.Radius, item.Radius);
				Transform transform = val2.transform;
				transform.localPosition += Vector3.up * num;
				val2.AddComponent<SensorCollider>().Setup(item);
				val2.SetActive(true);
			}
			uint num2 = EOSNetworking.AllotReplicatorID();
			if (num2 == 0)
			{
				EOSLogger.Error("SensorGroup.Instantiate: replicator ID depleted, cannot create StateReplicator...");
			}
			else
			{
				sensorGroup.StateReplicator = StateReplicator<SensorGroupState>.Create(num2, new SensorGroupState
				{
					status = ActiveState.ENABLED
				}, (LifeTimeType)1, (IStateReplicatorHolder<SensorGroupState>)null);
				sensorGroup.StateReplicator.OnStateChanged += sensorGroup.OnStateChanged;
			}
			return sensorGroup;
		}

		public void StartMovingMovables()
		{
			movableSensors.ForEach(delegate(MovableSensor movable)
			{
				movable.StartMoving();
			});
		}

		public void PauseMovingMovables()
		{
			movableSensors.ForEach(delegate(MovableSensor movable)
			{
				movable.PauseMoving();
			});
		}

		public void ResumeMovingMovables()
		{
			movableSensors.ForEach(delegate(MovableSensor movable)
			{
				movable.ResumeMoving();
			});
		}

		public void Destroy()
		{
			basicSensors.ForEach((Action<GameObject>)Object.Destroy);
			basicSensors.Clear();
			movableSensors.ForEach(delegate(MovableSensor m)
			{
				m.Destroy();
			});
			movableSensors.Clear();
			StateReplicator = null;
		}

		private SensorGroup()
		{
		}
	}
	public enum ActiveState
	{
		DISABLED,
		ENABLED
	}
	public struct SensorGroupState
	{
		public ActiveState status;

		public SensorGroupState()
		{
			status = ActiveState.DISABLED;
		}

		public SensorGroupState(SensorGroupState o)
		{
			status = o.status;
		}

		public SensorGroupState(ActiveState status)
		{
			this.status = status;
		}
	}
	public struct MovableSensorLerp
	{
		public float lerp;

		public MovableSensorLerp()
		{
			lerp = 0f;
		}

		public MovableSensorLerp(MovableSensorLerp o)
		{
			lerp = o.lerp;
		}

		public MovableSensorLerp(float lerp)
		{
			this.lerp = lerp;
		}
	}
}
namespace EOSExt.SecuritySensor.Component
{
	public class SensorCollider : MonoBehaviour
	{
		private float nextCheckTime = float.NaN;

		private SensorSettings sensorSettings;

		private int last_playersInSensor;

		public static float CHECK_INTERVAL { get; } = 0.1f;


		private Vector3 Position => ((Component)this).gameObject.transform.position;

		internal void Setup(SensorSettings sensorSettings)
		{
			this.sensorSettings = sensorSettings;
		}

		private void Update()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if ((int)GameStateManager.CurrentStateName != 10 || (!float.IsNaN(nextCheckTime) && Clock.Time < nextCheckTime))
			{
				return;
			}
			nextCheckTime = Clock.Time + CHECK_INTERVAL;
			int num = 0;
			Enumerator<PlayerAgent> enumerator = PlayerManager.PlayerAgentsInLevel.GetEnumerator();
			while (enumerator.MoveNext())
			{
				PlayerAgent current = enumerator.Current;
				if (!current.Owner.IsBot && ((Agent)current).Alive)
				{
					Vector3 val = Position - ((Agent)current).Position;
					if (((Vector3)(ref val)).magnitude < sensorSettings.Radius)
					{
						num++;
					}
				}
			}
			if (num > last_playersInSensor)
			{
				SecuritySensorManager.Current.TriggerSensor(((Il2CppObjectBase)((Component)this).gameObject).Pointer);
			}
			last_playersInSensor = num;
		}
	}
}
namespace EOSExt.SecuritySensor.Patches
{
	[HarmonyPatch]
	internal class Debug
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(LG_CollisionWorldEventTrigger), "Update")]
		private static void Post(LG_CollisionWorldEventTrigger __instance)
		{
		}

		static Debug()
		{
		}
	}
}
namespace EOSExt.SecuritySensor.Definition
{
	public class SensorColor
	{
		public float r { get; set; }

		public float g { get; set; }

		public float b { get; set; }

		public float a { get; set; }

		public Color toColor()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			return new Color(r, g, b, a);
		}
	}
	public class SensorGroupSettings
	{
		public List<SensorSettings> SensorGroup { get; set; } = new List<SensorSettings>
		{
			new SensorSettings()
		};


		public List<WardenObjectiveEventData> EventsOnTrigger { get; set; } = new List<WardenObjectiveEventData>();

	}
	public enum SensorType
	{
		BASIC,
		MOVABLE
	}
	public class SensorSettings
	{
		public Vec3 Position { get; set; } = new Vec3();


		public float Radius { get; set; } = 2.3f;


		public SensorType SensorType { get; set; }

		public float MovingSpeedMulti { get; set; } = 1f;


		public List<Vec3> MovingPosition { get; set; } = new List<Vec3>
		{
			new Vec3()
		};

	}
}