Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of ProximityPickup v1.0.1
ProximityPickup.dll
Decompiled 2 days agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using BepInEx; using BepInEx.Configuration; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyVersion("0.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 ProximityPickup { [BepInPlugin("BioHazardN7.ProximityPickup", "ProximityPickup", "1.0.1")] public class ProximityPickupPlugin : BaseUnityPlugin { private sealed class Tracked { public ZNetView View; public int Id; public float NextTry; } public const string PluginGuid = "BioHazardN7.ProximityPickup"; public const string PluginName = "ProximityPickup"; public const string PluginVersion = "1.0.1"; private const string DefaultPrefabs = "Pickable_DolmenTreasure, Pickable_ForestCryptRandom, Pickable_Item, Pickable_MorkHallaTreasure, Pickable_MountainCaveRandom, Pickable_SunkenCryptRandom"; internal static ProximityPickupPlugin Instance; private ConfigEntry<bool> _enabled; private ConfigEntry<float> _radius; private ConfigEntry<float> _maxBelow; private ConfigEntry<int> _maxPerFrame; private ConfigEntry<float> _retryDelay; private ConfigEntry<string> _prefabList; private ConfigEntry<bool> _debugLog; private readonly HashSet<int> _prefabHashes = new HashSet<int>(); private int _prefabCount; private readonly List<Tracked> _tracked = new List<Tracked>(); private readonly HashSet<int> _trackedIds = new HashSet<int>(); private Harmony _harmony; private void Awake() { //IL_0056: Unknown result type (might be due to invalid IL or missing references) //IL_0060: Expected O, but got Unknown //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_009e: Expected O, but got Unknown //IL_00c7: Unknown result type (might be due to invalid IL or missing references) //IL_00d1: Expected O, but got Unknown //IL_0105: Unknown result type (might be due to invalid IL or missing references) //IL_010f: Expected O, but got Unknown //IL_017d: Unknown result type (might be due to invalid IL or missing references) //IL_0187: Expected O, but got Unknown Instance = this; _enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable or disable the mod."); _radius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Radius", 5f, new ConfigDescription("Horizontal radius (in meters) around the player in which listed objects are picked up automatically.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 50f), Array.Empty<object>())); _maxBelow = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MaxDistanceBelow", 0.5f, new ConfigDescription("Maximum distance (in meters) BELOW the player at which objects are still picked up. Objects deeper than this (for example buried treasure) are ignored. There is no limit above the player.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 50f), Array.Empty<object>())); _maxPerFrame = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxPerFrame", 3, new ConfigDescription("Maximum number of objects interacted with per frame. Prevents spikes when many objects enter the radius at once.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>())); _retryDelay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RetryDelay", 3f, new ConfigDescription("Seconds to wait before interacting with the same object again (protects against network spam).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 30f), Array.Empty<object>())); _prefabList = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Prefabs", "Pickable_DolmenTreasure, Pickable_ForestCryptRandom, Pickable_Item, Pickable_MorkHallaTreasure, Pickable_MountainCaveRandom, Pickable_SunkenCryptRandom", "Comma-separated list of prefab names to pick up automatically. Names are written without \"(Clone)\"; case is ignored."); _debugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DebugLog", false, "Log every registered object (prefab name, attached components) and the result of each interaction. Useful for diagnosing objects that do not react."); _prefabList.SettingChanged += delegate { OnPrefabListChanged(); }; ParsePrefabs(); _harmony = new Harmony("BioHazardN7.ProximityPickup"); _harmony.PatchAll(typeof(ProximityPickupPlugin).Assembly); ((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1} loaded. Tracking {2} prefab(s).", "ProximityPickup", "1.0.1", _prefabCount)); } private void OnDestroy() { Harmony harmony = _harmony; if (harmony != null) { harmony.UnpatchSelf(); } if ((Object)(object)Instance == (Object)(object)this) { Instance = null; } } internal void Register(ZNetView nview) { if ((Object)(object)nview == (Object)null || _prefabHashes.Count == 0 || Application.isBatchMode) { return; } ZDO zDO = nview.GetZDO(); if (zDO == null || !_prefabHashes.Contains(zDO.GetPrefab())) { return; } int instanceID = ((Object)nview).GetInstanceID(); if (_trackedIds.Add(instanceID)) { _tracked.Add(new Tracked { View = nview, Id = instanceID }); if (_debugLog.Value) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Registered '" + GetPrefabName(((Component)nview).gameObject) + "'; components: " + DescribeComponents(nview))); } } } private void OnPrefabListChanged() { ParsePrefabs(); _tracked.Clear(); _trackedIds.Clear(); ZNetView[] array = Object.FindObjectsByType<ZNetView>((FindObjectsSortMode)0); foreach (ZNetView nview in array) { Register(nview); } } private void ParsePrefabs() { _prefabHashes.Clear(); _prefabCount = 0; string[] array = (_prefabList.Value ?? string.Empty).Split(new char[3] { ',', ';', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < array.Length; i++) { string text = array[i].Trim(); if (text.Length != 0 && _prefabHashes.Add(StableHash(text))) { _prefabCount++; } } } private void Update() { //IL_0078: 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_0107: Unknown result type (might be due to invalid IL or missing references) //IL_010c: Unknown result type (might be due to invalid IL or missing references) //IL_010d: Unknown result type (might be due to invalid IL or missing references) //IL_0112: Unknown result type (might be due to invalid IL or missing references) //IL_0114: Unknown result type (might be due to invalid IL or missing references) //IL_0122: Unknown result type (might be due to invalid IL or missing references) //IL_0129: Unknown result type (might be due to invalid IL or missing references) //IL_0131: Unknown result type (might be due to invalid IL or missing references) //IL_0138: Unknown result type (might be due to invalid IL or missing references) if (!_enabled.Value || _tracked.Count == 0) { return; } Player localPlayer = Player.m_localPlayer; if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead() || ((Character)localPlayer).InCutscene() || ((Character)localPlayer).IsTeleporting() || InventoryGui.IsVisible() || Menu.IsVisible() || ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus()) || Console.IsVisible()) { return; } Vector3 position = ((Component)localPlayer).transform.position; float value = _radius.Value; float num = value * value; float value2 = _maxBelow.Value; float time = Time.time; int num2 = 0; int value3 = _maxPerFrame.Value; for (int num3 = _tracked.Count - 1; num3 >= 0; num3--) { Tracked tracked = _tracked[num3]; ZNetView view = tracked.View; if ((Object)(object)view == (Object)null) { RemoveAt(num3); } else if (!(tracked.NextTry > time)) { Vector3 val = ((Component)view).transform.position - position; if (!(val.y < 0f - value2) && !(val.x * val.x + val.z * val.z > num) && view.IsValid()) { string text = (_debugLog.Value ? GetPrefabName(((Component)view).gameObject) : null); bool flag = TryInteract(view, localPlayer, text); if (_debugLog.Value) { ((BaseUnityPlugin)this).Logger.LogInfo((object)$"Interact('{text}') returned {flag}"); } tracked.NextTry = time + _retryDelay.Value; if (++num2 >= value3) { break; } } } } } private void RemoveAt(int index) { int index2 = _tracked.Count - 1; _trackedIds.Remove(_tracked[index].Id); _tracked[index] = _tracked[index2]; _tracked.RemoveAt(index2); } private bool TryInteract(ZNetView nview, Player player, string prefabName) { Pickable componentInChildren = ((Component)nview).GetComponentInChildren<Pickable>(); if ((Object)(object)componentInChildren != (Object)null) { return componentInChildren.Interact((Humanoid)(object)player, false, false); } Interactable componentInChildren2 = ((Component)nview).GetComponentInChildren<Interactable>(); if (componentInChildren2 != null) { return componentInChildren2.Interact((Humanoid)(object)player, false, false); } if (_debugLog.Value) { ((BaseUnityPlugin)this).Logger.LogWarning((object)("'" + prefabName + "' has no Pickable or Interactable component.")); } return false; } private static string DescribeComponents(ZNetView nview) { HashSet<string> hashSet = new HashSet<string>(); StringBuilder stringBuilder = new StringBuilder(); Component[] componentsInChildren = ((Component)nview).GetComponentsInChildren<Component>(true); foreach (Component val in componentsInChildren) { if ((Object)(object)val == (Object)null) { continue; } string name = ((object)val).GetType().Name; if (hashSet.Add(name)) { if (stringBuilder.Length > 0) { stringBuilder.Append(", "); } stringBuilder.Append(name); } } return stringBuilder.ToString(); } private static int StableHash(string str) { int num = 5381; int num2 = num; for (int i = 0; i < str.Length && str[i] != 0; i += 2) { num = ((num << 5) + num) ^ str[i]; if (i == str.Length - 1 || str[i + 1] == '\0') { break; } num2 = ((num2 << 5) + num2) ^ str[i + 1]; } return num + num2 * 1566083941; } private static string GetPrefabName(GameObject go) { string name = ((Object)go).name; int num = name.IndexOf("(Clone)", StringComparison.Ordinal); if (num < 0) { return name; } return name.Substring(0, num).Trim(); } } [HarmonyPatch(typeof(ZNetView), "Awake")] internal static class ZNetViewAwakePatch { private static void Postfix(ZNetView __instance) { ProximityPickupPlugin.Instance?.Register(__instance); } } }