Decompiled source of McPortalMetal v1.0.0

McPortalMetal.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("McPortalMetal")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("McPortalMetal")]
[assembly: AssemblyTitle("McPortalMetal")]
[assembly: AssemblyVersion("1.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 McPortalMetal
{
	[BepInPlugin("joaorodrigues.valheim.mcportalmetal", "McPortalMetal", "1.0.0")]
	[BepInProcess("valheim.exe")]
	public class McPortalMetalPlugin : BaseUnityPlugin
	{
		public const string Guid = "joaorodrigues.valheim.mcportalmetal";

		public const string Name = "McPortalMetal";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> ModEnabled;

		internal static ConfigEntry<string> BlockedItems;

		private void Awake()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Carry ore, bars and everything else through portals. Turning this off puts the restriction back straight away, without restarting or uninstalling.");
			BlockedItems = ((BaseUnityPlugin)this).Config.Bind<string>("1 - General", "BlockedItems", "", "Items that should still be refused, as a comma-separated list of prefab names, for example: FlametalOre, BlackMetalScrap. Leave empty to carry everything. These are prefab names as the console uses them, matched case-insensitively.");
			ModEnabled.SettingChanged += delegate
			{
				Teleportable.Refresh();
			};
			BlockedItems.SettingChanged += delegate
			{
				Teleportable.Refresh();
			};
			try
			{
				new Harmony("joaorodrigues.valheim.mcportalmetal").PatchAll();
			}
			catch (Exception ex)
			{
				Log.LogError((object)("McPortalMetal: could not patch the game, so the mod is inactive. " + ex.Message));
				return;
			}
			Log.LogInfo((object)"McPortalMetal 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			Teleportable.Restore();
		}
	}
	internal static class Teleportable
	{
		private const int NeverTeleportToolTier = 1000;

		private static readonly List<SharedData> Flipped = new List<SharedData>();

		private static readonly HashSet<string> Blocked = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private static bool _failed;

		private static void ParseBlocked()
		{
			Blocked.Clear();
			string[] array = (McPortalMetalPlugin.BlockedItems.Value ?? string.Empty).Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length > 0)
				{
					Blocked.Add(text);
				}
			}
		}

		internal static void Refresh()
		{
			if (_failed)
			{
				return;
			}
			try
			{
				Restore();
				if (!McPortalMetalPlugin.ModEnabled.Value)
				{
					return;
				}
				ObjectDB instance = ObjectDB.instance;
				if ((Object)(object)instance == (Object)null)
				{
					return;
				}
				ParseBlocked();
				int num = 0;
				int num2 = 0;
				int num3 = 0;
				foreach (GameObject item in instance.m_items)
				{
					if ((Object)(object)item == (Object)null)
					{
						continue;
					}
					SharedData val = item.GetComponent<ItemDrop>()?.m_itemData?.m_shared;
					if (val != null && !val.m_teleportable)
					{
						if (val.m_toolTier >= 1000)
						{
							num3++;
							continue;
						}
						if (Blocked.Contains(((Object)item).name))
						{
							num2++;
							continue;
						}
						val.m_teleportable = true;
						Flipped.Add(val);
						num++;
					}
				}
				McPortalMetalPlugin.Log.LogInfo((object)($"McPortalMetal: {num} item type(s) may now pass a portal" + ((num2 > 0) ? $", {num2} kept blocked by config" : "") + ((num3 > 0) ? $", {num3} left alone (the game never allows those)" : "") + "."));
			}
			catch (Exception ex)
			{
				_failed = true;
				McPortalMetalPlugin.Log.LogError((object)("McPortalMetal: failed to apply; stock behaviour stands for this session. " + ex));
			}
		}

		internal static void Restore()
		{
			foreach (SharedData item in Flipped)
			{
				if (item != null)
				{
					item.m_teleportable = false;
				}
			}
			Flipped.Clear();
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	internal static class Patch_ObjectDB_Awake
	{
		private static void Postfix()
		{
			Teleportable.Refresh();
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	internal static class Patch_ObjectDB_Copy
	{
		private static void Postfix()
		{
			Teleportable.Refresh();
		}
	}
}