Decompiled source of InstantCosmeticCrates v1.0.0

InstantCosmeticCrates.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using ExitGames.Client.Photon;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("InstantCosmeticCrates")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("InstantCosmeticCrates")]
[assembly: AssemblyTitle("InstantCosmeticCrates")]
[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 InstantCosmeticCrates
{
	[BepInPlugin("local.instantcosmeticcrates", "Instant Cosmetic Crates", "1.0.0")]
	public class InstantCosmeticCratesPlugin : BaseUnityPlugin
	{
		private const string PluginGuid = "local.instantcosmeticcrates";

		private const string PluginName = "Instant Cosmetic Crates";

		private const string PluginVersion = "1.0.0";

		private const byte EventRequest = 177;

		private const byte EventCashIn = 178;

		private ConfigEntry<InputKey> _key;

		private FieldInfo _grabbedField;

		private bool _subscribed;

		private RoundDirector _lastRound;

		private readonly HashSet<int> _requested = new HashSet<int>();

		private readonly HashSet<int> _cashedIn = new HashSet<int>();

		private void Awake()
		{
			_key = ((BaseUnityPlugin)this).Config.Bind<InputKey>("General", "Key", (InputKey)2, "Spieltaste, mit der die gehaltene Cosmetic Crate eingezahlt wird (Interact = standardmäßig E).");
			_grabbedField = typeof(PhysGrabber).GetField("grabbedPhysGrabObject", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (_grabbedField == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"InstantCosmeticCrates: Feld PhysGrabber.grabbedPhysGrabObject nicht gefunden - Spielversion geändert?");
			}
		}

		private void OnDestroy()
		{
			try
			{
				if (_subscribed && PhotonNetwork.NetworkingClient != null)
				{
					PhotonNetwork.NetworkingClient.EventReceived -= OnPhotonEvent;
				}
			}
			catch (Exception)
			{
			}
		}

		private void Update()
		{
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			if (!_subscribed)
			{
				TrySubscribe();
			}
			if ((Object)(object)RoundDirector.instance != (Object)(object)_lastRound)
			{
				_lastRound = RoundDirector.instance;
				_requested.Clear();
				_cashedIn.Clear();
			}
			if (_grabbedField == null || (Object)(object)InputManager.instance == (Object)null || (Object)(object)PhysGrabber.instance == (Object)null || !InputManager.instance.KeyDown(_key.Value))
			{
				return;
			}
			object? value = _grabbedField.GetValue(PhysGrabber.instance);
			Component val = (Component)((value is Component) ? value : null);
			if (!((Object)(object)val == (Object)null))
			{
				CosmeticWorldObject val2 = val.GetComponent<CosmeticWorldObject>();
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = val.GetComponentInParent<CosmeticWorldObject>();
				}
				if (!((Object)(object)val2 == (Object)null))
				{
					RequestCashIn(val2);
				}
			}
		}

		private void TrySubscribe()
		{
			try
			{
				if (PhotonNetwork.NetworkingClient != null)
				{
					PhotonNetwork.NetworkingClient.EventReceived += OnPhotonEvent;
					_subscribed = true;
				}
			}
			catch (Exception)
			{
			}
		}

		private static bool IsNetworked()
		{
			if (PhotonNetwork.InRoom)
			{
				return !PhotonNetwork.OfflineMode;
			}
			return false;
		}

		private void RequestCashIn(CosmeticWorldObject crate)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Expected O, but got Unknown
			if (!IsNetworked())
			{
				ApplyCashIn(crate, removeCrate: true);
				return;
			}
			PhotonView val = ((Component)crate).GetComponent<PhotonView>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)crate).GetComponentInParent<PhotonView>();
			}
			if ((Object)(object)val == (Object)null || val.ViewID == 0)
			{
				return;
			}
			int viewID = val.ViewID;
			if (_requested.Add(viewID))
			{
				if (PhotonNetwork.IsMasterClient)
				{
					BroadcastCashIn(viewID);
					return;
				}
				PhotonNetwork.RaiseEvent((byte)177, (object)viewID, new RaiseEventOptions
				{
					Receivers = (ReceiverGroup)2
				}, SendOptions.SendReliable);
			}
		}

		private void BroadcastCashIn(int viewId)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			PhotonNetwork.RaiseEvent((byte)178, (object)viewId, new RaiseEventOptions
			{
				Receivers = (ReceiverGroup)1
			}, SendOptions.SendReliable);
		}

		private void OnPhotonEvent(EventData photonEvent)
		{
			try
			{
				if (photonEvent.Code != 177 && photonEvent.Code != 178)
				{
					return;
				}
				int num = Convert.ToInt32(photonEvent.CustomData);
				if (photonEvent.Code == 177)
				{
					if (PhotonNetwork.IsMasterClient && _requested.Add(num))
					{
						BroadcastCashIn(num);
					}
					return;
				}
				PhotonView val = PhotonView.Find(num);
				if (!((Object)(object)val == (Object)null))
				{
					CosmeticWorldObject val2 = ((Component)val).GetComponent<CosmeticWorldObject>();
					if ((Object)(object)val2 == (Object)null)
					{
						val2 = ((Component)val).GetComponentInParent<CosmeticWorldObject>();
					}
					if (!((Object)(object)val2 == (Object)null) && _cashedIn.Add(num))
					{
						ApplyCashIn(val2, PhotonNetwork.IsMasterClient);
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("InstantCosmeticCrates: Fehler bei Event-Verarbeitung: " + ex.Message));
			}
		}

		private void ApplyCashIn(CosmeticWorldObject crate, bool removeCrate)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: 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_0031: Unknown result type (might be due to invalid IL or missing references)
			Rarity rarity = crate.rarity;
			if ((Object)(object)RoundDirector.instance != (Object)null)
			{
				RoundDirector.instance.CosmeticWorldObjectExtracted(rarity);
			}
			if ((Object)(object)CosmeticWorldObjectUI.instance != (Object)null)
			{
				CosmeticWorldObjectUI.instance.Add(rarity);
			}
			if (!removeCrate)
			{
				return;
			}
			Component component = ((Component)crate).GetComponent("PhysGrabObject");
			if ((Object)(object)component != (Object)null)
			{
				MethodInfo method = ((object)component).GetType().GetMethod("DestroyPhysGrabObject", BindingFlags.Instance | BindingFlags.Public);
				if (method != null)
				{
					method.Invoke(component, null);
				}
			}
		}
	}
}