Decompiled source of LethalTraitor v0.1.0

LethalTraitor.dll

Decompiled 33 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

[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("LethalTraitor")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LethalTraitor")]
[assembly: AssemblyTitle("LethalTraitor")]
[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 LethalTraitor
{
	[BepInPlugin("pl.lethaltraitor.core", "Lethal Traitor", "0.1.0")]
	public sealed class TraitorPlugin : BaseUnityPlugin
	{
		internal static TraitorPlugin Instance;

		private Harmony _harmony;

		private NetworkManager _registeredManager;

		private const string RoleMessage = "LethalTraitor.Role.v1";

		private const string ResetMessage = "LethalTraitor.Reset.v1";

		private int _roundNumber;

		private bool _roundActive;

		private bool _roleAssigned;

		private bool _isTraitor;

		private float _nextRegistrationAttempt;

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			Instance = this;
			_harmony = new Harmony("pl.lethaltraitor.core");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Lethal Traitor v0.1.0 loaded");
		}

		private void Update()
		{
			if (!(Time.unscaledTime < _nextRegistrationAttempt))
			{
				_nextRegistrationAttempt = Time.unscaledTime + 0.5f;
				RegisterMessages();
			}
		}

		private void RegisterMessages()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null) && singleton.IsListening && singleton.CustomMessagingManager != null && !((Object)(object)_registeredManager == (Object)(object)singleton))
			{
				UnregisterMessages();
				singleton.CustomMessagingManager.RegisterNamedMessageHandler("LethalTraitor.Role.v1", new HandleNamedMessageDelegate(OnRoleMessage));
				singleton.CustomMessagingManager.RegisterNamedMessageHandler("LethalTraitor.Reset.v1", new HandleNamedMessageDelegate(OnResetMessage));
				_registeredManager = singleton;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Network message handlers registered");
			}
		}

		private void UnregisterMessages()
		{
			if ((Object)(object)_registeredManager == (Object)null)
			{
				return;
			}
			try
			{
				CustomMessagingManager customMessagingManager = _registeredManager.CustomMessagingManager;
				if (customMessagingManager != null)
				{
					customMessagingManager.UnregisterNamedMessageHandler("LethalTraitor.Role.v1");
				}
				CustomMessagingManager customMessagingManager2 = _registeredManager.CustomMessagingManager;
				if (customMessagingManager2 != null)
				{
					customMessagingManager2.UnregisterNamedMessageHandler("LethalTraitor.Reset.v1");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)ex);
			}
			_registeredManager = null;
		}

		internal void OnMissionStarted()
		{
			_roundActive = true;
			_roleAssigned = false;
			_isTraitor = false;
			_roundNumber++;
			RegisterMessages();
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null) && singleton.IsServer)
			{
				((MonoBehaviour)this).StartCoroutine(AssignAfterDelay(_roundNumber));
			}
		}

		private unsafe IEnumerator AssignAfterDelay(int round)
		{
			yield return (object)new WaitForSeconds(3f);
			if (!_roundActive || round != _roundNumber)
			{
				yield break;
			}
			NetworkManager manager = NetworkManager.Singleton;
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)manager == (Object)null || !manager.IsServer || (Object)(object)instance == (Object)null)
			{
				yield break;
			}
			RegisterMessages();
			List<ulong> list = (from id in (from p in instance.allPlayerScripts
					where (Object)(object)p != (Object)null && p.isPlayerControlled && !p.isPlayerDead
					select p.actualClientId).Distinct()
				where manager.ConnectedClientsIds.Contains(id)
				select id).ToList();
			if (list.Count < 2)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Need at least two connected players for traitor mode.");
				ShowMessage("LETHAL TRAITOR", "Potrzeba co najmniej 2 graczy.");
				yield break;
			}
			ulong num = list[Random.Range(0, list.Count)];
			FastBufferWriter val = default(FastBufferWriter);
			foreach (ulong item in list)
			{
				((FastBufferWriter)(ref val))..ctor(5, (Allocator)2, -1);
				try
				{
					((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref round, default(ForPrimitives));
					byte b = ((item == num) ? ((byte)1) : ((byte)0));
					((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
					manager.CustomMessagingManager.SendNamedMessage("LethalTraitor.Role.v1", item, val, (NetworkDelivery)3);
				}
				finally
				{
					((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Private roles sent to " + list.Count + " players (round " + round + ")."));
		}

		private void OnRoleMessage(ulong senderClientId, FastBufferReader reader)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)NetworkManager.Singleton == (Object)null) && senderClientId == 0L)
			{
				int num = default(int);
				((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
				byte b = default(byte);
				((FastBufferReader)(ref reader)).ReadValueSafe<byte>(ref b, default(ForPrimitives));
				if (_roundActive && num == _roundNumber)
				{
					_roleAssigned = true;
					_isTraitor = b == 1;
					ShowMessage(_isTraitor ? "JESTES ZDRAJCA" : "JESTES W ZALODZE", _isTraitor ? "Utrudnij zalodze powrot. Nie zdradz swojej roli!" : "Uwazaj. W zalodze ukrywa sie zdrajca.");
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Private role received for round " + num));
				}
			}
		}

		internal void OnMissionEnded()
		{
			_roundActive = false;
			_roleAssigned = false;
			_isTraitor = false;
		}

		private void OnResetMessage(ulong senderClientId, FastBufferReader reader)
		{
		}

		private void ShowMessage(string title, string message)
		{
			if ((Object)(object)HUDManager.Instance != (Object)null)
			{
				HUDManager.Instance.DisplayTip(title, message, false, false, "LC_Tip1");
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)(title + ": " + message));
		}

		private void OnDestroy()
		{
			UnregisterMessages();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "StartGame")]
	internal static class MissionStartPatch
	{
		[HarmonyPostfix]
		private static void Postfix()
		{
			TraitorPlugin.Instance?.OnMissionStarted();
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "ShipLeave")]
	internal static class MissionEndPatch
	{
		[HarmonyPostfix]
		private static void Postfix()
		{
			TraitorPlugin.Instance?.OnMissionEnded();
		}
	}
}