DIBS.dll

Decompiled 11 months 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 BepInEx;
using HG;
using Microsoft.CodeAnalysis;
using On.RoR2;
using R2API;
using R2API.Utils;
using RoR2;
using RoR2.Audio;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("DIBS")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DIBS")]
[assembly: AssemblyTitle("DIBS")]
[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 DIBS
{
	public static class AssetsHelper
	{
		internal static NetworkSoundEventDef CreateNetworkSoundEventDef(string eventName)
		{
			NetworkSoundEventDef obj = ScriptableObject.CreateInstance<NetworkSoundEventDef>();
			obj.akId = AkSoundEngine.GetIDFromString(eventName);
			obj.eventName = eventName;
			ContentAddition.AddNetworkSoundEventDef(obj);
			return obj;
		}
	}
	internal class ClaimManager
	{
		private Dictionary<NetworkInstanceId, NetworkInstanceId> _claims = new Dictionary<NetworkInstanceId, NetworkInstanceId>();

		private LockManager _lockManager;

		public ClaimManager(LockManager lockManager)
		{
			_lockManager = lockManager;
		}

		public NetworkInstanceId? GetClaimer(NetworkInstanceId targetId)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			if (!_claims.ContainsValue(targetId))
			{
				return null;
			}
			return _claims.First((KeyValuePair<NetworkInstanceId, NetworkInstanceId> x) => x.Value == targetId).Key;
		}

		public bool TryGetClaim(NetworkInstanceId playerId, out NetworkInstanceId targetId)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			return _claims.TryGetValue(playerId, out targetId);
		}

		public void SetClaim(NetworkInstanceId playerId, NetworkInstanceId targetId)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			_claims[playerId] = targetId;
			_lockManager.InstantiateLock(targetId);
		}

		public void RemoveClaim(NetworkInstanceId playerId)
		{
			//IL_0006: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			if (_claims.TryGetValue(playerId, out var value))
			{
				_claims.Remove(playerId);
				_lockManager.DestroyLock(value);
			}
		}

		public void ClearDibs()
		{
			EnumerableExtensions.ForEachTry<NetworkInstanceId>((IEnumerable<NetworkInstanceId>)_claims.Keys, (Action<NetworkInstanceId>)RemoveClaim, (IDictionary<NetworkInstanceId, Exception>)null);
		}

		public static bool IsValidObject(GameObject target)
		{
			if ((Object.op_Implicit((Object)(object)target.GetComponent<ChestBehavior>()) && !((Object)target).name.Contains("Lockbox")) || (Object.op_Implicit((Object)(object)target.GetComponent<ShopTerminalBehavior>()) && !((Object)target).name.Contains("Duplicator") && !((Object)target).name.Contains("Cauldron")) || Object.op_Implicit((Object)(object)target.GetComponent<ShrineChanceBehavior>()))
			{
				return target.GetComponent<PurchaseInteraction>().available;
			}
			return false;
		}
	}
	[BepInPlugin("joeyvanlierop.DIBS", "DIBS", "1.0.6")]
	public class DIBS : BaseUnityPlugin
	{
		public const string PluginGuid = "joeyvanlierop.DIBS";

		public const string PluginAuthor = "joeyvanlierop";

		public const string PluginName = "DIBS";

		public const string PluginVersion = "1.0.6";

		private ClaimManager _claimManager;

		private const string NewClaimSound = "Play_gravekeeper_attack2_shoot_singleChain";

		private const string RedeemClaimSound = "Play_gravekeeper_attack2_shoot_singleChain";

		private const string FailClaimSound = "Play_UI_insufficient_funds";

		private readonly NetworkSoundEventDef NewClaimSoundNet = AssetsHelper.CreateNetworkSoundEventDef("Play_gravekeeper_attack2_shoot_singleChain");

		private readonly NetworkSoundEventDef RedeemClaimSoundNet = AssetsHelper.CreateNetworkSoundEventDef("Play_gravekeeper_attack2_shoot_singleChain");

		private readonly NetworkSoundEventDef FailClaimSoundNet = AssetsHelper.CreateNetworkSoundEventDef("Play_UI_insufficient_funds");

		public void OnEnable()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			LockManager lockManager = new LockManager();
			_claimManager = new ClaimManager(lockManager);
			Stage.Start += new hook_Start(Stage_Start);
			PingerController.SetCurrentPing += new hook_SetCurrentPing(PingerController_SetCurrentPing);
			Interactor.PerformInteraction += new hook_PerformInteraction(Interactor_PerformInteraction);
			UserChatMessage.ConstructChatString += new hook_ConstructChatString(Chat_UserChatMessage_ConstructChatString);
			GlobalEventManager.OnPlayerCharacterDeath += new hook_OnPlayerCharacterDeath(GlobalEventManager_OnPlayerCharacterDeath);
		}

		private void GlobalEventManager_OnPlayerCharacterDeath(orig_OnPlayerCharacterDeath orig, GlobalEventManager self, DamageReport damageReport, NetworkUser victim)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			_claimManager.RemoveClaim(((NetworkBehaviour)victim).netId);
			orig.Invoke(self, damageReport, victim);
		}

		private void Stage_Start(orig_Start orig, Stage stage)
		{
			_claimManager.ClearDibs();
			orig.Invoke(stage);
		}

		private void PingerController_SetCurrentPing(orig_SetCurrentPing orig, PingerController controller, PingInfo pingInfo)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			NetworkUser user = UsersHelper.GetUser(controller);
			GameObject targetGameObject = ((PingInfo)(ref pingInfo)).targetGameObject;
			if ((Object)(object)targetGameObject == (Object)null || !ClaimManager.IsValidObject(targetGameObject))
			{
				orig.Invoke(controller, pingInfo);
				return;
			}
			if (_claimManager.TryGetClaim(((NetworkBehaviour)user).netId, out var _))
			{
				Util.PlaySound("Play_UI_insufficient_funds", targetGameObject);
				orig.Invoke(controller, pingInfo);
				return;
			}
			if (_claimManager.GetClaimer(pingInfo.targetNetworkIdentity.netId).HasValue)
			{
				Util.PlaySound("Play_UI_insufficient_funds", targetGameObject);
				orig.Invoke(controller, pingInfo);
				return;
			}
			NetworkIdentity targetNetworkIdentity = pingInfo.targetNetworkIdentity;
			_claimManager.SetClaim(((NetworkBehaviour)user).netId, targetNetworkIdentity.netId);
			Util.PlayAttackSpeedSound("Play_gravekeeper_attack2_shoot_singleChain", targetGameObject, 1f);
			orig.Invoke(controller, pingInfo);
		}

		private void Interactor_PerformInteraction(orig_PerformInteraction orig, Interactor interactor, GameObject target)
		{
			//IL_002e: 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_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: 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_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			if (!ClaimManager.IsValidObject(target))
			{
				orig.Invoke(interactor, target);
				return;
			}
			if (!target.GetComponent<PurchaseInteraction>().CanBeAffordedByInteractor(interactor))
			{
				orig.Invoke(interactor, target);
				return;
			}
			NetworkInstanceId netId = ((NetworkBehaviour)UsersHelper.GetUser(interactor)).netId;
			NetworkInstanceId netId2 = target.GetComponent<NetworkIdentity>().netId;
			NetworkInstanceId? claimer = _claimManager.GetClaimer(netId2);
			NetworkInstanceId? val;
			NetworkInstanceId val2;
			if (claimer.HasValue)
			{
				val = claimer;
				val2 = netId;
				if (!val.HasValue || (val.HasValue && val.GetValueOrDefault() != val2))
				{
					EntitySoundManager.EmitSoundServer(AkEventIdArg.op_Implicit(FailClaimSoundNet.akId), target);
					return;
				}
			}
			if (_claimManager.TryGetClaim(netId, out var targetId) && targetId != netId2)
			{
				EntitySoundManager.EmitSoundServer(AkEventIdArg.op_Implicit(FailClaimSoundNet.akId), target);
				return;
			}
			val = claimer;
			val2 = netId;
			if (val.HasValue && (!val.HasValue || val.GetValueOrDefault() == val2))
			{
				EntitySoundManager.EmitSoundServer(AkEventIdArg.op_Implicit(RedeemClaimSoundNet.akId), target);
				_claimManager.RemoveClaim(netId);
			}
			orig.Invoke(interactor, target);
		}

		private string Chat_UserChatMessage_ConstructChatString(orig_ConstructChatString orig, UserChatMessage message)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			if (message.text.Contains("undibs"))
			{
				NetworkUser component = message.sender.GetComponent<NetworkUser>();
				_claimManager.RemoveClaim(((NetworkBehaviour)component).netId);
			}
			return orig.Invoke(message);
		}
	}
	internal class LockManager
	{
		private readonly GameObject _purchaseLockPrefab = Addressables.LoadAssetAsync<GameObject>((object)"RoR2/Base/Teleporters/PurchaseLock.prefab").WaitForCompletion();

		private readonly Dictionary<NetworkInstanceId, List<GameObject>> _idToLocks = new Dictionary<NetworkInstanceId, List<GameObject>>();

		public void InstantiateLock(NetworkInstanceId targetId)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: 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_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkServer.active)
			{
				return;
			}
			GameObject val = ClientScene.FindLocalObject(targetId);
			List<GameObject> list = new List<GameObject>();
			if (Object.op_Implicit((Object)(object)val.GetComponent<ShopTerminalBehavior>()))
			{
				ReadOnlyArray<GameObject> terminalGameObjects = val.GetComponent<ShopTerminalBehavior>().serverMultiShopController.terminalGameObjects;
				Enumerator<GameObject> enumerator = terminalGameObjects.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						GameObject current = enumerator.Current;
						GameObject val2 = Object.Instantiate<GameObject>(_purchaseLockPrefab, current.transform.position, Quaternion.identity);
						NetworkServer.Spawn(val2);
						list.Add(val2);
					}
				}
				finally
				{
					((IDisposable)enumerator).Dispose();
				}
			}
			else
			{
				GameObject val3 = Object.Instantiate<GameObject>(_purchaseLockPrefab, val.transform.position, Quaternion.identity);
				NetworkServer.Spawn(val3);
				list.Add(val3);
			}
			_idToLocks[targetId] = list;
		}

		public void DestroyLock(NetworkInstanceId targetId)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			if (!_idToLocks.TryGetValue(targetId, out var value))
			{
				return;
			}
			foreach (GameObject item in value)
			{
				NetworkServer.Destroy(item);
			}
			_idToLocks.Remove(targetId);
		}
	}
	public static class UsersHelper
	{
		public static CharacterMaster GetUserMaster(CharacterBody body)
		{
			foreach (CharacterMaster item in PlayerCharacterMasterController.instances.Select((PlayerCharacterMasterController p) => p.master))
			{
				if ((Object)(object)item.GetBody() == (Object)(object)body)
				{
					return item;
				}
			}
			return null;
		}

		public static Interactor GetUserInteractor(NetworkUser user)
		{
			if (!Object.op_Implicit((Object)(object)user) || !Object.op_Implicit((Object)(object)user.master))
			{
				return null;
			}
			GameObject bodyObject = user.master.GetBodyObject();
			if (!Object.op_Implicit((Object)(object)bodyObject))
			{
				return null;
			}
			return bodyObject.GetComponent<InteractionDriver>().interactor;
		}

		public static IEnumerable<NetworkUser> GetAllUsers()
		{
			return NetworkUser.readOnlyInstancesList;
		}

		public static NetworkUser GetUser(CharacterMaster userMaster)
		{
			return ((IEnumerable<NetworkUser>)NetworkUser.readOnlyInstancesList).FirstOrDefault((Func<NetworkUser, bool>)((NetworkUser u) => (Object)(object)u.master == (Object)(object)userMaster));
		}

		public static NetworkUser GetUser(CharacterBody userBody)
		{
			return GetUser(GetUserMaster(userBody));
		}

		public static NetworkUser GetUser(Interactor userInteractor)
		{
			return GetUser((userInteractor != null) ? ((Component)userInteractor).GetComponent<CharacterBody>() : null);
		}

		public static NetworkUser GetUser(PingerController userPinger)
		{
			return GetUser((userPinger != null) ? ((Component)userPinger).GetComponent<CharacterMaster>() : null);
		}
	}
}