Decompiled source of DiscoverAllValuables v1.0.1

DiscoverAllValuables.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Nixeld")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DiscoverAllValuables")]
[assembly: AssemblyTitle("DiscoverAllValuables")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 DiscoverAllValuables
{
	[BepInPlugin("Nixeld.DiscoverAllValuables", "DiscoverAllValuables", "1.0")]
	public class DiscoverAllValuables : BaseUnityPlugin
	{
		internal static DiscoverAllValuables Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			ValuableDiscovery.DiscoverAllEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DiscoverAllValuables", true, "When true, every valuable is marked discovered and shown on the map. When false, the game behaves as per usual.");
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded (DiscoverAllValuables={ValuableDiscovery.DiscoverAllEnabled.Value}).");
		}

		internal void Patch()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0025: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class ValuableDiscovery
	{
		private static readonly FieldInfo? DiscoveredField = AccessTools.Field(typeof(ValuableObject), "discovered");

		internal static ConfigEntry<bool>? DiscoverAllEnabled { get; set; }

		internal static void DiscoverIfEnabled(ValuableObject valuable)
		{
			ConfigEntry<bool> discoverAllEnabled = DiscoverAllEnabled;
			if (discoverAllEnabled != null && discoverAllEnabled.Value && IsAlive(valuable))
			{
				if (!IsDiscovered(valuable))
				{
					SetDiscovered(valuable, discovered: true);
				}
				EnsureMapMarker(valuable);
			}
		}

		internal static int DiscoverAllInScene()
		{
			int num = 0;
			ValuableObject[] array = Object.FindObjectsOfType<ValuableObject>();
			foreach (ValuableObject valuable in array)
			{
				if (IsAlive(valuable))
				{
					if (!IsDiscovered(valuable))
					{
						SetDiscovered(valuable, discovered: true);
						num++;
					}
					EnsureMapMarker(valuable);
				}
			}
			return num;
		}

		private static bool IsAlive(ValuableObject? valuable)
		{
			if ((Object)(object)valuable != (Object)null && ((Component)valuable).gameObject.activeInHierarchy)
			{
				return ((Behaviour)valuable).enabled;
			}
			return false;
		}

		private static bool IsDiscovered(ValuableObject valuable)
		{
			try
			{
				object obj = DiscoveredField?.GetValue(valuable);
				bool flag = default(bool);
				int num;
				if (obj is bool)
				{
					flag = (bool)obj;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
			}
			catch (Exception ex)
			{
				DiscoverAllValuables.Logger.LogDebug((object)("Could not read discovered state for " + ((Object)valuable).name + ": " + ex.Message));
				return false;
			}
		}

		private static void SetDiscovered(ValuableObject valuable, bool discovered)
		{
			try
			{
				DiscoveredField?.SetValue(valuable, discovered);
			}
			catch (Exception ex)
			{
				DiscoverAllValuables.Logger.LogWarning((object)("Could not set discovered state for " + ((Object)valuable).name + ": " + ex.Message));
			}
		}

		private static void EnsureMapMarker(ValuableObject valuable)
		{
			Map instance = Map.Instance;
			if ((Object)(object)instance == (Object)null || MapAlreadyHasMarker(valuable))
			{
				return;
			}
			try
			{
				instance.AddValuable(valuable);
			}
			catch (Exception ex)
			{
				DiscoverAllValuables.Logger.LogWarning((object)("Could not add map marker for " + ((Object)valuable).name + ": " + ex.Message));
			}
		}

		private static bool MapAlreadyHasMarker(ValuableObject valuable)
		{
			MapValuable[] array = Resources.FindObjectsOfTypeAll<MapValuable>();
			foreach (MapValuable val in array)
			{
				if ((Object)(object)val != (Object)null && (Object)(object)val.target == (Object)(object)valuable)
				{
					return true;
				}
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(LevelGenerator), "GenerateDone")]
	internal static class LevelGeneratorGenerateDonePatch
	{
		private static void Postfix()
		{
			ConfigEntry<bool> discoverAllEnabled = ValuableDiscovery.DiscoverAllEnabled;
			if (discoverAllEnabled != null && discoverAllEnabled.Value)
			{
				int num = ValuableDiscovery.DiscoverAllInScene();
				DiscoverAllValuables.Logger.LogInfo((object)$"DiscoverAllValuables: after level generation, updated {num} valuables (map markers ensured for all alive instances).");
			}
		}
	}
	[HarmonyPatch(typeof(ValuableObject), "Start")]
	internal static class ValuableObjectStartPatch
	{
		private static void Postfix(ValuableObject __instance)
		{
			ConfigEntry<bool> discoverAllEnabled = ValuableDiscovery.DiscoverAllEnabled;
			if (discoverAllEnabled != null && discoverAllEnabled.Value && SafeLevelGenDone())
			{
				ValuableDiscovery.DiscoverIfEnabled(__instance);
			}
		}

		private static bool SafeLevelGenDone()
		{
			try
			{
				return SemiFunc.LevelGenDone();
			}
			catch
			{
				return false;
			}
		}
	}
}