Decompiled source of ResourceScout Scan v0.1.18

ResourceScout_Scan.dll

Decompiled 17 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ResourceScout_Scan")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ResourceScout_Scan")]
[assembly: AssemblyTitle("ResourceScout_Scan")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 ResourceScout_Scan
{
	[BepInPlugin("pupsi.repo.resourcescout_scan", "ResourceScout_Scan", "0.1.18")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "pupsi.repo.resourcescout_scan";

		public const string PluginName = "ResourceScout_Scan";

		public const string PluginVersion = "0.1.18";

		private Harmony harmony;

		internal static Plugin Instance { get; private set; }

		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("pupsi.repo.resourcescout_scan");
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[ResourceScout_Scan] Plugin geladen");
		}
	}
	internal static class ResourceScoutDefinitions
	{
		public const string CarrierUpgradeItemKey = "Item Upgrade Player Health";

		public const string ScannerItemKey = "Item Upgrade Resource Scanner";

		public const string ScannerDisplayName = "Resourcen Scanner";

		public const string ScannerUpgradeKey = "playerUpgradeResourceScanner";

		public const string ScouterItemKey = "Item Upgrade Resource Scouter";

		public const string ScouterDisplayName = "Resourcen Scouter";

		public const string ScouterUpgradeKey = "playerUpgradeResourceScouter";

		public const float ScannerValueMin = 50f;

		public const float ScannerValueMax = 60f;

		public const float ScouterValueMin = 25f;

		public const float ScouterValueMax = 35f;
	}
	internal sealed class ResourceScoutUpgradeMarker : MonoBehaviour
	{
		public string ItemKey;
	}
	[HarmonyPatch(typeof(StatsManager), "Start")]
	internal static class StatsManagerStartPatch
	{
		private static void Postfix(StatsManager __instance)
		{
			ResourceScoutStats.EnsureRegistered(__instance);
		}
	}
	[HarmonyPatch(typeof(StatsManager), "LoadItemsFromFolder")]
	internal static class StatsManagerLoadItemsFromFolderPatch
	{
		private static void Postfix(StatsManager __instance)
		{
			ResourceScoutItems.RegisterItems(__instance);
		}
	}
	[HarmonyPatch(typeof(ShopManager), "GetAllItemsFromStatsManager")]
	internal static class ShopManagerGetAllItemsFromStatsManagerPatch
	{
		private static void Prefix()
		{
			ResourceScoutItems.EnsureShopItemsRegistered();
		}

		private static void Postfix(ShopManager __instance)
		{
			ResourceScoutItems.LimitCustomShopUpgradeCopies(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemManager), "GetPurchasedItems")]
	internal static class ItemManagerGetPurchasedItemsPatch
	{
		private static void Prefix()
		{
			ResourceScoutItems.EnsureShopItemsRegistered();
		}
	}
	[HarmonyPatch(typeof(ItemAttributes), "Awake")]
	internal static class ItemAttributesAwakePatch
	{
		private static void Postfix(ItemAttributes __instance)
		{
			ResourceScoutItems.ApplyCustomItemFromPhotonData(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemAttributes), "Start")]
	internal static class ItemAttributesStartPatch
	{
		private static void Prefix(ItemAttributes __instance)
		{
			ResourceScoutItems.ApplyCustomItemFromPhotonData(__instance);
		}
	}
	[HarmonyPatch(typeof(ItemAttributes), "GetValue")]
	internal static class ItemAttributesGetValuePatch
	{
		private static bool Prefix(ItemAttributes __instance)
		{
			return !ResourceScoutItems.TrySetFixedShopValue(__instance);
		}
	}
	[HarmonyPatch(typeof(PunManager), "SpawnShopItem")]
	internal static class PunManagerSpawnShopItemPatch
	{
		private static bool Prefix(ItemVolume itemVolume, List<Item> itemList, ref int spawnCount, bool isSecret, ref bool __result)
		{
			//IL_002a: 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)
			if ((Object)(object)itemVolume == (Object)null || itemList == null)
			{
				return true;
			}
			for (int num = itemList.Count - 1; num >= 0; num--)
			{
				Item val = itemList[num];
				if (ResourceScoutItems.IsCustomItem(val) && val.itemVolume == itemVolume.itemVolume)
				{
					if (!ResourceScoutItems.SpawnCustomShopItem(val, itemVolume))
					{
						return true;
					}
					itemList.RemoveAt(num);
					if (!isSecret)
					{
						spawnCount++;
					}
					__result = true;
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(PunManager), "SpawnItem")]
	internal static class PunManagerSpawnItemPatch
	{
		private static bool Prefix(Item item, ItemVolume volume)
		{
			return !ResourceScoutItems.SpawnCustomTruckItem(item, volume);
		}
	}
	[HarmonyPatch(typeof(ItemUpgrade), "PlayerUpgrade")]
	internal static class ItemUpgradePlayerUpgradePatch
	{
		private static bool Prefix(ItemUpgrade __instance)
		{
			return !ResourceScoutUpgrades.TryApplyCustomUpgrade(__instance);
		}
	}
	[HarmonyPatch(typeof(Map), "Start")]
	internal static class MapStartPatch
	{
		private static void Postfix()
		{
			ResourceScoutRadar.ResetScannerMarkers();
		}
	}
	[HarmonyPatch(typeof(Map), "ActiveSet")]
	internal static class MapActiveSetPatch
	{
		private static void Postfix(bool active)
		{
			if (active)
			{
				ResourceScoutOverlay.RequestImmediateRefresh();
				ResourceScoutRadar.OnMapOpened();
			}
		}
	}
	[HarmonyPatch(typeof(LevelUI), "Update")]
	internal static class LevelUiUpdatePatch
	{
		private static void Postfix(LevelUI __instance)
		{
			ResourceScoutOverlay.Update(__instance);
		}
	}
	[HarmonyPatch(typeof(StatsManager), "DictionaryUpdateValue")]
	internal static class StatsManagerDictionaryUpdateValuePatch
	{
		private static void Postfix(object __0, object __1, object __2)
		{
			ResourceScoutStats.OnDictionaryUpdateValue(__0, __1, __2);
		}
	}
	internal static class ResourceScoutStats
	{
		private static readonly FieldInfo DictionaryOfDictionariesField = AccessTools.Field(typeof(StatsManager), "dictionaryOfDictionaries");

		private static readonly Dictionary<string, int> ScannerUpgrades = new Dictionary<string, int>();

		private static readonly Dictionary<string, int> ScouterUpgrades = new Dictionary<string, int>();

		public static void EnsureRegistered(StatsManager stats)
		{
			if (!((Object)(object)stats == (Object)null))
			{
				SortedDictionary<string, Dictionary<string, int>> dictionaries = GetDictionaries(stats);
				if (dictionaries != null)
				{
					EnsureDictionary(dictionaries, "playerUpgradeResourceScanner", ScannerUpgrades);
					EnsureDictionary(dictionaries, "playerUpgradeResourceScouter", ScouterUpgrades);
				}
				EnsureUpgradeInfo(stats, "playerUpgradeResourceScanner", "Resource Scanner");
				EnsureUpgradeInfo(stats, "playerUpgradeResourceScouter", "Resource Scouter");
			}
		}

		public static bool HasScanner()
		{
			return GetLocalUpgradeValue("playerUpgradeResourceScanner") > 0;
		}

		public static bool HasScouter()
		{
			return GetLocalUpgradeValue("playerUpgradeResourceScouter") > 0;
		}

		public static void AddUpgrade(string upgradeKey, string steamId)
		{
			if (!string.IsNullOrEmpty(upgradeKey) && !string.IsNullOrEmpty(steamId))
			{
				StatsManager instance = StatsManager.instance;
				EnsureRegistered(instance);
				Dictionary<string, int> upgradeDictionary = GetUpgradeDictionary(upgradeKey);
				int num = 1;
				if (upgradeDictionary != null)
				{
					upgradeDictionary.TryGetValue(steamId, out var value);
					num = (upgradeDictionary[steamId] = value + 1);
				}
				if ((Object)(object)PunManager.instance != (Object)null)
				{
					PunManager.instance.UpdateStat(upgradeKey, steamId, num);
				}
				else if ((Object)(object)instance != (Object)null)
				{
					instance.DictionaryUpdateValue(upgradeKey, steamId, num);
				}
				ResourceScoutOverlay.RequestImmediateRefresh();
				if (upgradeKey == "playerUpgradeResourceScanner" && (Object)(object)Map.Instance != (Object)null && Map.Instance.Active)
				{
					ResourceScoutRadar.OnMapOpened();
				}
			}
		}

		public static void OnDictionaryUpdateValue(object dictionaryKey, object steamIdKey, object statValue)
		{
			string text = dictionaryKey as string;
			if (text != "playerUpgradeResourceScanner" && text != "playerUpgradeResourceScouter")
			{
				return;
			}
			string text2 = steamIdKey as string;
			if (!string.IsNullOrEmpty(text2))
			{
				int value;
				try
				{
					value = Convert.ToInt32(statValue);
				}
				catch
				{
					return;
				}
				EnsureRegistered(StatsManager.instance);
				Dictionary<string, int> upgradeDictionary = GetUpgradeDictionary(text);
				if (upgradeDictionary != null)
				{
					upgradeDictionary[text2] = value;
				}
				ResourceScoutOverlay.RequestImmediateRefresh();
				if (text == "playerUpgradeResourceScanner" && (Object)(object)Map.Instance != (Object)null && Map.Instance.Active)
				{
					ResourceScoutRadar.OnMapOpened();
				}
			}
		}

		private static int GetLocalUpgradeValue(string upgradeKey)
		{
			PlayerAvatar val = SemiFunc.PlayerAvatarLocal();
			if ((Object)(object)val == (Object)null)
			{
				return 0;
			}
			string text = SemiFunc.PlayerGetSteamID(val);
			if (string.IsNullOrEmpty(text))
			{
				return 0;
			}
			Dictionary<string, int> upgradeDictionary = GetUpgradeDictionary(upgradeKey);
			if (upgradeDictionary == null || !upgradeDictionary.TryGetValue(text, out var value))
			{
				return 0;
			}
			return value;
		}

		private static SortedDictionary<string, Dictionary<string, int>> GetDictionaries(StatsManager stats)
		{
			return DictionaryOfDictionariesField?.GetValue(stats) as SortedDictionary<string, Dictionary<string, int>>;
		}

		private static Dictionary<string, int> GetUpgradeDictionary(string upgradeKey)
		{
			StatsManager instance = StatsManager.instance;
			SortedDictionary<string, Dictionary<string, int>> sortedDictionary = (((Object)(object)instance != (Object)null) ? GetDictionaries(instance) : null);
			if (sortedDictionary != null && sortedDictionary.TryGetValue(upgradeKey, out var value))
			{
				return value;
			}
			if (!(upgradeKey == "playerUpgradeResourceScanner"))
			{
				return ScouterUpgrades;
			}
			return ScannerUpgrades;
		}

		private static void EnsureDictionary(SortedDictionary<string, Dictionary<string, int>> dictionaries, string key, Dictionary<string, int> fallback)
		{
			if (!dictionaries.TryGetValue(key, out var value))
			{
				dictionaries.Add(key, fallback);
			}
			else
			{
				if (value == fallback)
				{
					return;
				}
				fallback.Clear();
				foreach (KeyValuePair<string, int> item in value)
				{
					fallback[item.Key] = item.Value;
				}
			}
		}

		private static void EnsureUpgradeInfo(StatsManager stats, string key, string displayName)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			if (stats.upgradesInfo != null && !stats.upgradesInfo.ContainsKey(key))
			{
				stats.upgradesInfo.Add(key, new UpgradeInfo
				{
					displayName = displayName,
					displayNameLocalized = null
				});
			}
		}
	}
	internal static class ResourceScoutItems
	{
		private static Item scannerItem;

		private static Item scouterItem;

		private static readonly FieldInfo ItemNameField = AccessTools.Field(typeof(ItemAttributes), "itemName");

		private static readonly FieldInfo ItemTypeField = AccessTools.Field(typeof(ItemAttributes), "itemType");

		private static readonly FieldInfo ItemAssetNameField = AccessTools.Field(typeof(ItemAttributes), "itemAssetName");

		private static readonly FieldInfo EmojiIconField = AccessTools.Field(typeof(ItemAttributes), "emojiIcon");

		private static readonly FieldInfo ColorPresetField = AccessTools.Field(typeof(ItemAttributes), "colorPreset");

		private static readonly FieldInfo ItemValueMinField = AccessTools.Field(typeof(ItemAttributes), "itemValueMin");

		private static readonly FieldInfo ItemValueMaxField = AccessTools.Field(typeof(ItemAttributes), "itemValueMax");

		private static readonly FieldInfo ValueField = AccessTools.Field(typeof(ItemAttributes), "value");

		private static readonly FieldInfo UpgradeEventField = AccessTools.Field(typeof(ItemUpgrade), "upgradeEvent");

		private static readonly Type ItemUpgradeBaseType = typeof(ItemUpgrade);

		public static void RegisterItems(StatsManager stats)
		{
			if (!((Object)(object)stats == (Object)null) && stats.itemDictionary != null)
			{
				ResourceScoutStats.EnsureRegistered(stats);
				Item val = FindCarrierUpgrade(stats);
				if ((Object)(object)val == (Object)null)
				{
					Debug.LogWarning((object)"[ResourceScout_Scan] Carrier upgrade item missing: Item Upgrade Player Health");
					return;
				}
				scannerItem = EnsureItem(stats, val, "Item Upgrade Resource Scanner", "Resourcen Scanner", 50f, 60f);
				scouterItem = EnsureItem(stats, val, "Item Upgrade Resource Scouter", "Resourcen Scouter", 25f, 35f);
			}
		}

		public static bool IsCustomItem(Item item)
		{
			if ((Object)(object)item != (Object)null)
			{
				return IsCustomItemKey(((Object)item).name);
			}
			return false;
		}

		public static bool IsCustomItemKey(string itemKey)
		{
			if (!(itemKey == "Item Upgrade Resource Scanner"))
			{
				return itemKey == "Item Upgrade Resource Scouter";
			}
			return true;
		}

		public static bool TryGetCustomItem(string itemKey, out Item item)
		{
			if (itemKey == "Item Upgrade Resource Scanner")
			{
				item = scannerItem;
				return (Object)(object)item != (Object)null;
			}
			if (itemKey == "Item Upgrade Resource Scouter")
			{
				item = scouterItem;
				return (Object)(object)item != (Object)null;
			}
			item = null;
			return false;
		}

		public static bool TrySetFixedShopValue(ItemAttributes attributes)
		{
			if ((Object)(object)attributes == (Object)null || !IsCustomItem(attributes.item))
			{
				return false;
			}
			if (SemiFunc.IsMultiplayer() && !PhotonNetwork.IsMasterClient)
			{
				return true;
			}
			int num = Mathf.CeilToInt(Random.Range(attributes.item.value.valueMin, attributes.item.value.valueMax));
			ValueField?.SetValue(attributes, num);
			PhotonView component = ((Component)attributes).GetComponent<PhotonView>();
			if (SemiFunc.IsMultiplayer() && (Object)(object)component != (Object)null && component.ViewID != 0)
			{
				component.RPC("GetValueRPC", (RpcTarget)1, new object[1] { num });
			}
			return true;
		}

		public static void EnsureShopItemsRegistered()
		{
			EnsureRuntimeItems();
		}

		public static void LimitCustomShopUpgradeCopies(ShopManager shop)
		{
			if (!((Object)(object)shop == (Object)null) && shop.potentialItemUpgrades != null)
			{
				LimitItemCopies(shop.potentialItemUpgrades, "Item Upgrade Resource Scanner");
				LimitItemCopies(shop.potentialItemUpgrades, "Item Upgrade Resource Scouter");
			}
		}

		public static bool SpawnCustomShopItem(Item item, ItemVolume itemVolume)
		{
			//IL_004d: 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)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: 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_008d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)item == (Object)null || (Object)(object)itemVolume == (Object)null || item.prefab == null || (Object)(object)ShopManager.instance == (Object)null)
			{
				return false;
			}
			Transform itemRotateHelper = ShopManager.instance.itemRotateHelper;
			if ((Object)(object)itemRotateHelper == (Object)null)
			{
				return false;
			}
			itemRotateHelper.SetParent(((Component)itemVolume).transform);
			itemRotateHelper.localRotation = item.spawnRotationOffset;
			Quaternion rotation = itemRotateHelper.rotation;
			itemRotateHelper.SetParent(((Component)ShopManager.instance).transform);
			Vector3 position = ((Component)itemVolume).transform.position;
			GameObject val = ((!SemiFunc.IsMultiplayer()) ? Object.Instantiate<GameObject>(item.prefab.Prefab, position, rotation) : PhotonNetwork.InstantiateRoomObject(item.prefab.ResourcePath, position, rotation, (byte)0, new object[1] { ((Object)item).name }));
			ApplyCustomItem(val, ((Object)item).name);
			PrepareCustomUpgradeObject(val, ((Object)item).name);
			return (Object)(object)val != (Object)null;
		}

		public static bool SpawnCustomTruckItem(Item item, ItemVolume itemVolume)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: 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_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (!IsCustomItem(item) || (Object)(object)itemVolume == (Object)null || item.prefab == null || (Object)(object)ShopManager.instance == (Object)null)
			{
				return false;
			}
			Transform itemRotateHelper = ShopManager.instance.itemRotateHelper;
			if ((Object)(object)itemRotateHelper == (Object)null)
			{
				return true;
			}
			itemRotateHelper.SetParent(((Component)itemVolume).transform);
			itemRotateHelper.localRotation = item.spawnRotationOffset;
			Quaternion rotation = itemRotateHelper.rotation;
			itemRotateHelper.SetParent(((Component)ShopManager.instance).transform);
			Vector3 position = ((Component)itemVolume).transform.position;
			GameObject spawned = null;
			if (SemiFunc.IsMultiplayer())
			{
				if (SemiFunc.IsMasterClient())
				{
					spawned = PhotonNetwork.InstantiateRoomObject(item.prefab.ResourcePath, position, rotation, (byte)0, new object[1] { ((Object)item).name });
				}
			}
			else
			{
				spawned = Object.Instantiate<GameObject>(item.prefab.Prefab, position, rotation);
			}
			ApplyCustomItem(spawned, ((Object)item).name);
			return true;
		}

		public static void ApplyCustomItemFromPhotonData(ItemAttributes attributes)
		{
			if (!((Object)(object)attributes == (Object)null))
			{
				PhotonView component = ((Component)attributes).GetComponent<PhotonView>();
				object[] array = (((Object)(object)component != (Object)null) ? component.InstantiationData : null);
				if (array != null && array.Length != 0 && array[0] is string itemKey && TryGetCustomItem(itemKey, out var item))
				{
					ApplyCustomItem(attributes, item);
					PrepareCustomUpgradeObject(((Component)attributes).gameObject, itemKey);
				}
			}
		}

		private static void ApplyCustomItem(GameObject spawned, string itemKey)
		{
			if (!((Object)(object)spawned == (Object)null) && TryGetCustomItem(itemKey, out var item))
			{
				ItemAttributes component = spawned.GetComponent<ItemAttributes>();
				if ((Object)(object)component != (Object)null)
				{
					ApplyCustomItem(component, item);
				}
				PrepareCustomUpgradeObject(spawned, itemKey);
			}
		}

		private static void ApplyCustomItem(ItemAttributes attributes, Item item)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			attributes.item = item;
			ItemNameField?.SetValue(attributes, item.itemName);
			ItemTypeField?.SetValue(attributes, item.itemType);
			ItemAssetNameField?.SetValue(attributes, ((Object)item).name);
			EmojiIconField?.SetValue(attributes, item.emojiIcon);
			ColorPresetField?.SetValue(attributes, item.colorPreset);
			ItemValueMinField?.SetValue(attributes, item.value.valueMin);
			ItemValueMaxField?.SetValue(attributes, item.value.valueMax);
		}

		private static void PrepareCustomUpgradeObject(GameObject obj, string itemKey)
		{
			if (!((Object)(object)obj == (Object)null) && IsCustomItemKey(itemKey))
			{
				ResourceScoutUpgradeMarker resourceScoutUpgradeMarker = obj.GetComponent<ResourceScoutUpgradeMarker>();
				if ((Object)(object)resourceScoutUpgradeMarker == (Object)null)
				{
					resourceScoutUpgradeMarker = obj.AddComponent<ResourceScoutUpgradeMarker>();
				}
				resourceScoutUpgradeMarker.ItemKey = itemKey;
				StripForeignUpgradeHandlers(obj);
				ResourceScoutSkin.Apply(obj, itemKey);
				ItemUpgrade component = obj.GetComponent<ItemUpgrade>();
				object obj2 = (object)(((Object)(object)component != (Object)null) ? /*isinst with value type is only supported in some contexts*/: null);
				if (obj2 != null)
				{
					((UnityEventBase)obj2).RemoveAllListeners();
				}
			}
		}

		private static void StripForeignUpgradeHandlers(GameObject obj)
		{
			Component[] componentsInChildren = obj.GetComponentsInChildren<Component>(true);
			foreach (Component val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null))
				{
					Type type = ((object)val).GetType();
					if (!(type == ItemUpgradeBaseType) && type.Name.StartsWith("ItemUpgrade", StringComparison.Ordinal))
					{
						Object.Destroy((Object)(object)val);
					}
				}
			}
		}

		private static void EnsureRuntimeItems()
		{
			if (((Object)(object)scannerItem == (Object)null || (Object)(object)scouterItem == (Object)null) && (Object)(object)StatsManager.instance != (Object)null)
			{
				RegisterItems(StatsManager.instance);
			}
		}

		private static Item EnsureItem(StatsManager stats, Item carrierItem, string key, string displayName, float minValue, float maxValue)
		{
			if (stats.itemDictionary.TryGetValue(key, out var value))
			{
				ConfigureItem(value, key, displayName, minValue, maxValue);
				return value;
			}
			Item val = Object.Instantiate<Item>(carrierItem);
			ConfigureItem(val, key, displayName, minValue, maxValue);
			stats.itemDictionary.Add(key, val);
			return val;
		}

		private static void ConfigureItem(Item item, string key, string displayName, float minValue, float maxValue)
		{
			//IL_0028: 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)item == (Object)null))
			{
				((Object)item).name = key;
				item.itemName = displayName;
				item.description = displayName;
				item.itemNameLocalized = null;
				item.itemType = (itemType)3;
				item.itemVolume = (itemVolume)6;
				item.maxPurchase = false;
				item.maxPurchaseAmount = 1;
				item.maxAmountInShop = 1;
				if ((Object)(object)item.value == (Object)null || ((Object)item.value).name != key + " Value")
				{
					item.value = ScriptableObject.CreateInstance<Value>();
					((Object)item.value).name = key + " Value";
				}
				item.value.valueMin = minValue;
				item.value.valueMax = maxValue;
			}
		}

		private static void LimitItemCopies(List<Item> items, string itemKey)
		{
			bool flag = false;
			for (int num = items.Count - 1; num >= 0; num--)
			{
				Item val = items[num];
				if (!((Object)(object)val == (Object)null) && !(((Object)val).name != itemKey))
				{
					if (!flag)
					{
						flag = true;
					}
					else
					{
						items.RemoveAt(num);
					}
				}
			}
		}

		private static Item FindCarrierUpgrade(StatsManager stats)
		{
			if (stats.itemDictionary.TryGetValue("Item Upgrade Player Health", out var value) && IsUsableCarrier(value))
			{
				return value;
			}
			return null;
		}

		private static bool IsUsableCarrier(Item item)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Invalid comparison between Unknown and I4
			if ((Object)(object)item != (Object)null && !item.disabled && (int)item.itemType == 3 && item.prefab != null && item.prefab.IsValid())
			{
				return !IsCustomItem(item);
			}
			return false;
		}
	}
	internal static class ResourceScoutSkin
	{
		private static readonly Dictionary<string, Texture2D> Textures = new Dictionary<string, Texture2D>();

		private static bool loaded;

		public static void Apply(GameObject obj, string itemKey)
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)obj == (Object)null)
			{
				return;
			}
			Texture2D val = Load(itemKey);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Renderer[] componentsInChildren = obj.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val2 in componentsInChildren)
			{
				if (IsParticleRenderer(val2))
				{
					continue;
				}
				Material[] materials = val2.materials;
				foreach (Material val3 in materials)
				{
					if (!((Object)(object)val3 == (Object)null))
					{
						if (val3.HasProperty("_BaseMap"))
						{
							val3.SetTexture("_BaseMap", (Texture)(object)val);
						}
						if (val3.HasProperty("_MainTex"))
						{
							val3.SetTexture("_MainTex", (Texture)(object)val);
						}
						if (val3.HasProperty("_Color"))
						{
							val3.SetColor("_Color", Color.white);
						}
						if (val3.HasProperty("_EmissionColor"))
						{
							val3.SetColor("_EmissionColor", new Color(0.6f, 1f, 0.25f, 1f));
						}
					}
				}
			}
		}

		private static bool IsParticleRenderer(Renderer renderer)
		{
			if ((Object)(object)renderer == (Object)null || ((object)renderer).GetType().Name == "ParticleSystemRenderer")
			{
				return true;
			}
			Transform val = ((Component)renderer).transform;
			while ((Object)(object)val != (Object)null)
			{
				if (string.Equals(((Object)val).name, "Particle Effects", StringComparison.OrdinalIgnoreCase))
				{
					return true;
				}
				val = val.parent;
			}
			return false;
		}

		private static Texture2D Load(string itemKey)
		{
			EnsureLoaded();
			if (string.IsNullOrEmpty(itemKey) || !Textures.TryGetValue(itemKey, out var value))
			{
				return null;
			}
			return value;
		}

		private static void EnsureLoaded()
		{
			if (loaded)
			{
				return;
			}
			loaded = true;
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			if (string.IsNullOrEmpty(directoryName))
			{
				return;
			}
			List<string> list = FindSkinFiles(directoryName);
			if (list.Count == 0)
			{
				Debug.LogWarning((object)("[ResourceScout_Scan] No PNG skin files found in: " + Path.Combine(directoryName, "Assets")));
				return;
			}
			string text = PickByName(list, "scan");
			string text2 = PickByName(list, "scout");
			List<string> list2 = new List<string>();
			foreach (string item in list)
			{
				if (item != text && item != text2)
				{
					list2.Add(item);
				}
			}
			if (text == null && list2.Count > 0)
			{
				text = list2[0];
				list2.RemoveAt(0);
			}
			if (text2 == null && list2.Count > 0)
			{
				text2 = list2[0];
			}
			if (text2 == null)
			{
				text2 = text;
			}
			AddTexture("Item Upgrade Resource Scanner", text);
			AddTexture("Item Upgrade Resource Scouter", text2);
		}

		private static List<string> FindSkinFiles(string directory)
		{
			string path = Path.Combine(directory, "Assets");
			string[] array = (Directory.Exists(path) ? Directory.GetFiles(path, "*.png") : Array.Empty<string>());
			if (array.Length == 0 && Directory.Exists(directory))
			{
				array = Directory.GetFiles(directory, "*.png");
			}
			List<string> list = new List<string>(array);
			list.Sort(StringComparer.OrdinalIgnoreCase);
			return list;
		}

		private static string PickByName(List<string> paths, string namePart)
		{
			foreach (string path in paths)
			{
				if (Path.GetFileNameWithoutExtension(path).IndexOf(namePart, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return path;
				}
			}
			return null;
		}

		private static void AddTexture(string itemKey, string path)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			if (!string.IsNullOrEmpty(itemKey) && !string.IsNullOrEmpty(path) && File.Exists(path))
			{
				byte[] array = File.ReadAllBytes(path);
				Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
				if (!ImageConversion.LoadImage(val, array))
				{
					Object.Destroy((Object)(object)val);
					return;
				}
				((Object)val).name = Path.GetFileNameWithoutExtension(path);
				((Texture)val).wrapMode = (TextureWrapMode)1;
				((Texture)val).filterMode = (FilterMode)1;
				Textures[itemKey] = val;
			}
		}
	}
	internal static class ResourceScoutUpgrades
	{
		private static readonly FieldInfo PlayerTogglePhotonIdField = AccessTools.Field(typeof(ItemToggle), "playerTogglePhotonID");

		private static readonly FieldInfo ToggleStateField = AccessTools.Field(typeof(ItemToggle), "toggleState");

		private static readonly FieldInfo UpgradeDoneField = AccessTools.Field(typeof(ItemUpgrade), "upgradeDone");

		private static readonly FieldInfo IsPlayerUpgradeField = AccessTools.Field(typeof(ItemUpgrade), "isPlayerUpgrade");

		private static readonly FieldInfo ItemToggleField = AccessTools.Field(typeof(ItemUpgrade), "itemToggle");

		private static readonly FieldInfo ImpactDetectorField = AccessTools.Field(typeof(ItemUpgrade), "impactDetector");

		private static readonly FieldInfo ParticleEffectsField = AccessTools.Field(typeof(ItemUpgrade), "particleEffects");

		private static readonly MethodInfo DestroyObjectMethod = AccessTools.Method(typeof(PhysGrabObjectImpactDetector), "DestroyObject", new Type[1] { typeof(bool) }, (Type[])null);

		public static bool IsCustomUpgradeObject(GameObject obj)
		{
			if ((Object)(object)obj != (Object)null)
			{
				return (Object)(object)obj.GetComponent<ResourceScoutUpgradeMarker>() != (Object)null;
			}
			return false;
		}

		public static bool TryApplyCustomUpgrade(ItemUpgrade itemUpgrade)
		{
			if ((Object)(object)itemUpgrade == (Object)null)
			{
				return false;
			}
			ResourceScoutUpgradeMarker component = ((Component)itemUpgrade).GetComponent<ResourceScoutUpgradeMarker>();
			if ((Object)(object)component == (Object)null)
			{
				return false;
			}
			if (!ResourceScoutItems.IsCustomItemKey(component.ItemKey) || IsUpgradeDone(itemUpgrade))
			{
				return true;
			}
			if (!IsReadyForUse(itemUpgrade))
			{
				return false;
			}
			PlayerAvatar targetPlayer = GetTargetPlayer(itemUpgrade);
			string text = (((Object)(object)targetPlayer != (Object)null) ? SemiFunc.PlayerGetSteamID(targetPlayer) : null);
			if (!string.IsNullOrEmpty(text))
			{
				ResourceScoutStats.AddUpgrade((component.ItemKey == "Item Upgrade Resource Scanner") ? "playerUpgradeResourceScanner" : "playerUpgradeResourceScouter", text);
			}
			PlayUpgradeFeedback(itemUpgrade, targetPlayer);
			FinishUpgradeObject(itemUpgrade, component.ItemKey);
			return true;
		}

		private static bool IsUpgradeDone(ItemUpgrade itemUpgrade)
		{
			if ((Object)(object)itemUpgrade != (Object)null && UpgradeDoneField != null)
			{
				return (bool)UpgradeDoneField.GetValue(itemUpgrade);
			}
			return false;
		}

		private static bool IsReadyForUse(ItemUpgrade itemUpgrade)
		{
			if ((Object)(object)itemUpgrade == (Object)null)
			{
				return false;
			}
			if (IsPlayerUpgradeField != null && !(bool)IsPlayerUpgradeField.GetValue(itemUpgrade))
			{
				return false;
			}
			ItemToggle itemToggle = GetItemToggle(itemUpgrade);
			if ((Object)(object)itemToggle != (Object)null && ToggleStateField != null)
			{
				return (bool)ToggleStateField.GetValue(itemToggle);
			}
			return false;
		}

		private static PlayerAvatar GetTargetPlayer(ItemUpgrade itemUpgrade)
		{
			ItemToggle itemToggle = GetItemToggle(itemUpgrade);
			int num = 0;
			if ((Object)(object)itemToggle != (Object)null && PlayerTogglePhotonIdField != null && PlayerTogglePhotonIdField.GetValue(itemToggle) is int num2)
			{
				num = num2;
			}
			if (num == 0)
			{
				return SemiFunc.PlayerAvatarLocal();
			}
			return SemiFunc.PlayerAvatarGetFromPhotonID(num);
		}

		private static ItemToggle GetItemToggle(ItemUpgrade itemUpgrade)
		{
			object? obj = ItemToggleField?.GetValue(itemUpgrade);
			ItemToggle val = (ItemToggle)((obj is ItemToggle) ? obj : null);
			if (!((Object)(object)val != (Object)null))
			{
				return ((Component)itemUpgrade).GetComponent<ItemToggle>();
			}
			return val;
		}

		private static void PlayUpgradeFeedback(ItemUpgrade itemUpgrade, PlayerAvatar targetPlayer)
		{
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)targetPlayer != (Object)null && (Object)(object)targetPlayer == (Object)(object)SemiFunc.PlayerAvatarLocal())
			{
				if ((Object)(object)StatsUI.instance != (Object)null)
				{
					StatsUI.instance.Fetch();
					StatsUI.instance.ShowStats();
				}
				if ((Object)(object)CameraGlitch.Instance != (Object)null)
				{
					CameraGlitch.Instance.PlayUpgrade();
				}
			}
			else if ((Object)(object)itemUpgrade != (Object)null && (Object)(object)GameDirector.instance != (Object)null && (Object)(object)GameDirector.instance.CameraImpact != (Object)null)
			{
				GameDirector.instance.CameraImpact.ShakeDistance(5f, 1f, 6f, ((Component)itemUpgrade).transform.position, 0.2f);
			}
		}

		private static void FinishUpgradeObject(ItemUpgrade itemUpgrade, string itemKey)
		{
			UpgradeDoneField?.SetValue(itemUpgrade, true);
			DecrementPurchasedCounter(itemKey);
			Transform val = ((Component)itemUpgrade).transform.Find("Mesh");
			if ((Object)(object)val != (Object)null)
			{
				((Component)val).gameObject.SetActive(false);
			}
			object? obj = ParticleEffectsField?.GetValue(itemUpgrade);
			Transform val2 = (Transform)((obj is Transform) ? obj : null);
			if ((Object)(object)val2 != (Object)null)
			{
				val2.parent = null;
				((Component)val2).gameObject.SetActive(true);
			}
			object? obj2 = ImpactDetectorField?.GetValue(itemUpgrade);
			PhysGrabObjectImpactDetector val3 = (PhysGrabObjectImpactDetector)((obj2 is PhysGrabObjectImpactDetector) ? obj2 : null);
			if ((Object)(object)val3 == (Object)null)
			{
				val3 = ((Component)itemUpgrade).GetComponent<PhysGrabObjectImpactDetector>();
			}
			if ((Object)(object)val3 != (Object)null && DestroyObjectMethod != null)
			{
				DestroyObjectMethod.Invoke(val3, new object[1] { false });
			}
			else
			{
				Object.Destroy((Object)(object)((Component)itemUpgrade).gameObject);
			}
		}

		private static void DecrementPurchasedCounter(string itemKey)
		{
			if (!((Object)(object)StatsManager.instance == (Object)null) && StatsManager.instance.itemsPurchased != null && !string.IsNullOrEmpty(itemKey) && StatsManager.instance.itemsPurchased.TryGetValue(itemKey, out var value))
			{
				if (value <= 1)
				{
					StatsManager.instance.itemsPurchased.Remove(itemKey);
				}
				else
				{
					StatsManager.instance.itemsPurchased[itemKey] = value - 1;
				}
			}
		}
	}
	internal static class ResourceScoutRadar
	{
		private static readonly FieldInfo DiscoveredField = AccessTools.Field(typeof(ValuableObject), "discovered");

		private static readonly FieldInfo DollarValueCurrentField = AccessTools.Field(typeof(ValuableObject), "dollarValueCurrent");

		private static readonly MethodInfo GetCurrentCartMethod = AccessTools.Method(typeof(ValuableObject), "GetCurrentCart", (Type[])null, (Type[])null);

		private static readonly HashSet<int> ScannerMarkerIds = new HashSet<int>();

		public static void ResetScannerMarkers()
		{
			ScannerMarkerIds.Clear();
		}

		public static void OnMapOpened()
		{
			if (!ResourceScoutStats.HasScanner() || (Object)(object)Map.Instance == (Object)null)
			{
				return;
			}
			ValuableObject[] array = Object.FindObjectsOfType<ValuableObject>();
			foreach (ValuableObject val in array)
			{
				if (ShouldCountValuable(val))
				{
					int instanceID = ((Object)val).GetInstanceID();
					if (!IsDiscovered(val) && !ScannerMarkerIds.Contains(instanceID))
					{
						Map.Instance.AddValuable(val);
						ScannerMarkerIds.Add(instanceID);
					}
				}
			}
		}

		public static ResourceSummary GetLooseResourceSummary()
		{
			int num = 0;
			int num2 = 0;
			ValuableObject[] array = Object.FindObjectsOfType<ValuableObject>();
			foreach (ValuableObject valuable in array)
			{
				if (ShouldCountValuable(valuable))
				{
					float dollarValueCurrent = GetDollarValueCurrent(valuable);
					if (!(dollarValueCurrent <= 0f))
					{
						num++;
						num2 += Mathf.RoundToInt(dollarValueCurrent);
					}
				}
			}
			return new ResourceSummary(num, num2);
		}

		private static bool ShouldCountValuable(ValuableObject valuable)
		{
			if ((Object)(object)valuable == (Object)null || (Object)(object)((Component)valuable).gameObject == (Object)null || !((Component)valuable).gameObject.activeInHierarchy)
			{
				return false;
			}
			if (IsInCart(valuable) || IsInExtractionPoint(valuable))
			{
				return false;
			}
			return GetDollarValueCurrent(valuable) > 0f;
		}

		private static bool IsDiscovered(ValuableObject valuable)
		{
			if ((Object)(object)valuable != (Object)null && DiscoveredField != null)
			{
				return (bool)DiscoveredField.GetValue(valuable);
			}
			return false;
		}

		private static float GetDollarValueCurrent(ValuableObject valuable)
		{
			if (!((Object)(object)valuable != (Object)null) || !(DollarValueCurrentField != null))
			{
				return 0f;
			}
			return (float)DollarValueCurrentField.GetValue(valuable);
		}

		private static bool IsInCart(ValuableObject valuable)
		{
			if ((Object)(object)valuable != (Object)null && GetCurrentCartMethod != null)
			{
				return GetCurrentCartMethod.Invoke(valuable, null) != null;
			}
			return false;
		}

		private static bool IsInExtractionPoint(ValuableObject valuable)
		{
			if ((Object)(object)valuable != (Object)null && (Object)(object)RoundDirector.instance != (Object)null && RoundDirector.instance.dollarHaulList != null)
			{
				return RoundDirector.instance.dollarHaulList.Contains(((Component)valuable).gameObject);
			}
			return false;
		}
	}
	internal readonly struct ResourceSummary
	{
		public readonly int Count;

		public readonly int Value;

		public ResourceSummary(int count, int value)
		{
			Count = count;
			Value = value;
		}
	}
	internal static class ResourceScoutOverlay
	{
		private static TextMeshProUGUI text;

		private static float nextUpdateTime;

		private const float RefreshInterval = 0.1f;

		public static void RequestImmediateRefresh()
		{
			nextUpdateTime = 0f;
		}

		public static void Update(LevelUI levelUi)
		{
			if (!((Object)(object)levelUi != (Object)null) || !((Object)(object)Map.Instance != (Object)null) || !Map.Instance.Active || !ResourceScoutStats.HasScouter())
			{
				if ((Object)(object)text != (Object)null)
				{
					((Component)text).gameObject.SetActive(false);
				}
				return;
			}
			EnsureText(levelUi);
			if (!((Object)(object)text == (Object)null))
			{
				((Component)text).gameObject.SetActive(true);
				if (!(Time.realtimeSinceStartup < nextUpdateTime))
				{
					nextUpdateTime = Time.realtimeSinceStartup + 0.1f;
					ResourceSummary looseResourceSummary = ResourceScoutRadar.GetLooseResourceSummary();
					TextMeshProUGUI obj = text;
					int count = looseResourceSummary.Count;
					((TMP_Text)obj).text = "Beute: " + count + "\nWert: " + SemiFunc.DollarGetString(looseResourceSummary.Value);
				}
			}
		}

		private static void EnsureText(LevelUI levelUi)
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_007d: 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_0095: 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_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)text != (Object)null) && !((Object)(object)levelUi.textNumber == (Object)null))
			{
				Transform val = (((Object)(object)((TMP_Text)levelUi.textNumber).transform.parent != (Object)null) ? ((TMP_Text)levelUi.textNumber).transform.parent : ((TMP_Text)levelUi.textNumber).transform);
				GameObject val2 = new GameObject("ResourceScout_Scan Overlay");
				val2.transform.SetParent(val, false);
				RectTransform obj = val2.AddComponent<RectTransform>();
				RectTransform rectTransform = ((TMP_Text)levelUi.textNumber).rectTransform;
				obj.anchorMin = rectTransform.anchorMin;
				obj.anchorMax = rectTransform.anchorMax;
				obj.pivot = rectTransform.pivot;
				obj.anchoredPosition = rectTransform.anchoredPosition + new Vector2(0f, -210f);
				obj.sizeDelta = new Vector2(360f, 110f);
				text = val2.AddComponent<TextMeshProUGUI>();
				((TMP_Text)text).font = ((TMP_Text)levelUi.textNumber).font;
				((TMP_Text)text).fontMaterial = ((TMP_Text)levelUi.textNumber).fontMaterial;
				((TMP_Text)text).alignment = (TextAlignmentOptions)514;
				((TMP_Text)text).fontSize = Mathf.Max(22f, ((TMP_Text)levelUi.textNumber).fontSize * 0.38f);
				((Graphic)text).color = new Color(0.95f, 1f, 0.28f, 1f);
				((TMP_Text)text).enableWordWrapping = false;
				((Graphic)text).raycastTarget = false;
			}
		}
	}
}