Decompiled source of ContainerDiscoveries v1.0.1

ContainerDiscoveries.dll

Decompiled a week 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 BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using ValheimModShared;

[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 ValheimModShared
{
	internal static class ConfigWatcher
	{
		public static void Watch(ConfigFile config, ManualLogSource log)
		{
			if (config == null)
			{
				return;
			}
			string path = config.ConfigFilePath;
			string directoryName = Path.GetDirectoryName(path);
			string fileName = Path.GetFileName(path);
			if (string.IsNullOrEmpty(directoryName) || string.IsNullOrEmpty(fileName))
			{
				return;
			}
			FileSystemWatcher obj = new FileSystemWatcher(directoryName, fileName)
			{
				NotifyFilter = (NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite),
				IncludeSubdirectories = false,
				SynchronizingObject = null
			};
			FileSystemEventHandler reload = delegate
			{
				if (!File.Exists(path))
				{
					return;
				}
				try
				{
					config.Reload();
					ManualLogSource obj2 = log;
					if (obj2 != null)
					{
						obj2.LogInfo((object)"Config reloaded from disk");
					}
				}
				catch (Exception ex)
				{
					ManualLogSource obj3 = log;
					if (obj3 != null)
					{
						obj3.LogError((object)("Could not reload config; check its format. " + ex.Message));
					}
				}
			};
			obj.Changed += reload;
			obj.Created += reload;
			obj.Renamed += delegate(object s, RenamedEventArgs e)
			{
				reload(s, e);
			};
			obj.EnableRaisingEvents = true;
		}
	}
}
namespace ContainerDiscoveries
{
	[HarmonyPatch]
	internal static class DiscoveryPatches
	{
		private static readonly FieldRef<InventoryGui, Container> CurrentContainerRef = AccessTools.FieldRefAccess<InventoryGui, Container>("m_currentContainer");

		private static readonly List<ItemData> Pending = new List<ItemData>();

		private static bool s_suppressMessages;

		private static Settings S => Plugin.Settings;

		[HarmonyPatch(typeof(InventoryGui), "UpdateContainer")]
		[HarmonyPostfix]
		private static void UpdateContainer_Postfix(InventoryGui __instance, Player player)
		{
			if ((Object)(object)player == (Object)null || (Object)(object)player != (Object)(object)Player.m_localPlayer)
			{
				return;
			}
			Container val = CurrentContainerRef.Invoke(__instance);
			if ((Object)(object)val == (Object)null || !val.IsOwner())
			{
				return;
			}
			try
			{
				Discover(player, val.GetInventory());
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Discovery scan failed: {arg}");
			}
		}

		[HarmonyPatch(typeof(MessageHud), "QueueUnlockMsg")]
		[HarmonyPrefix]
		private static bool QueueUnlockMsg_Prefix()
		{
			return !s_suppressMessages;
		}

		private static void Discover(Player player, Inventory inventory)
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Invalid comparison between Unknown and I4
			if (inventory == null)
			{
				return;
			}
			bool value = S.DiscoverTrophies.Value;
			Pending.Clear();
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				SharedData val = allItem?.m_shared;
				if (val != null && !string.IsNullOrEmpty(val.m_name) && !player.IsKnownMaterial(val.m_name) && (value || (int)val.m_itemType != 13))
				{
					Pending.Add(allItem);
				}
			}
			if (Pending.Count == 0)
			{
				return;
			}
			s_suppressMessages = !S.ShowUnlockMessages.Value;
			try
			{
				foreach (ItemData item in Pending)
				{
					if (!player.IsKnownMaterial(item.m_shared.m_name))
					{
						player.AddKnownItem(item);
						Plugin.Log.LogDebug((object)("Discovered " + item.m_shared.m_name + " from container"));
					}
				}
			}
			finally
			{
				s_suppressMessages = false;
				Pending.Clear();
			}
		}
	}
	[BepInPlugin("dsoltyka.ContainerDiscoveries", "ContainerDiscoveries", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "dsoltyka.ContainerDiscoveries";

		private Harmony _harmony;

		internal static ManualLogSource Log { get; private set; }

		internal static Settings Settings { get; private set; }

		private void Awake()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Settings = new Settings(((BaseUnityPlugin)this).Config);
			ConfigWatcher.Watch(((BaseUnityPlugin)this).Config, Log);
			_harmony = new Harmony("dsoltyka.ContainerDiscoveries");
			_harmony.PatchAll(typeof(DiscoveryPatches));
			Log.LogInfo((object)"ContainerDiscoveries 1.0.1 loaded");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal sealed class Settings
	{
		public ConfigEntry<bool> ShowUnlockMessages { get; }

		public ConfigEntry<bool> DiscoverTrophies { get; }

		public Settings(ConfigFile config)
		{
			ShowUnlockMessages = config.Bind<bool>("1 - General", "ShowUnlockMessages", true, "Show the vanilla 'New material / New recipe' pop-ups for items discovered in containers. Turn off if opening a full storage room spams the screen.");
			DiscoverTrophies = config.Bind<bool>("1 - General", "DiscoverTrophies", true, "Also discover trophies found in containers. As in vanilla, discovering a trophy adds it to your trophy collection.");
		}
	}
	internal static class BuildInfo
	{
		public const string Name = "ContainerDiscoveries";

		public const string Version = "1.0.1";
	}
}