Decompiled source of ImmortalPortals v0.1.0

ImmortalPortals.dll

Decompiled a week ago
using System;
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.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("ImmortalPortals")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+63529edc52d9d6c1a41b33be6bacf9604c82a3ea")]
[assembly: AssemblyProduct("ImmortalPortals")]
[assembly: AssemblyTitle("ImmortalPortals")]
[assembly: AssemblyVersion("0.1.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 ImmortalPortals
{
	internal static class IplLog
	{
		internal static void Info(string message)
		{
			ManualLogSource log = ImmortalPortalsPlugin.Log;
			if (log != null)
			{
				log.LogInfo((object)message);
			}
		}

		internal static void Warn(string message)
		{
			ManualLogSource log = ImmortalPortalsPlugin.Log;
			if (log != null)
			{
				log.LogWarning((object)message);
			}
		}

		internal static void Error(string message, Exception exception = null)
		{
			if (exception == null)
			{
				ManualLogSource log = ImmortalPortalsPlugin.Log;
				if (log != null)
				{
					log.LogError((object)message);
				}
			}
			else
			{
				ManualLogSource log2 = ImmortalPortalsPlugin.Log;
				if (log2 != null)
				{
					log2.LogError((object)$"{message} {exception}");
				}
			}
		}

		internal static void Debug(string message)
		{
			if (ImmortalPortalsPlugin.DebugLog != null && ImmortalPortalsPlugin.DebugLog.Value)
			{
				ManualLogSource log = ImmortalPortalsPlugin.Log;
				if (log != null)
				{
					log.LogInfo((object)("[debug] " + message));
				}
			}
		}
	}
	internal static class Patches
	{
		internal static void Install(Harmony harmony)
		{
			MethodInfo target = AccessTools.Method(typeof(WearNTear), "ApplyDamage", new Type[2]
			{
				typeof(float),
				typeof(HitData)
			}, (Type[])null);
			TryPatch(harmony, target, HarmonyMethodFor("ApplyDamage_Prefix"), "WearNTear.ApplyDamage");
		}

		private static bool ApplyDamage_Prefix(WearNTear __instance, HitData hitData)
		{
			if (!PortalProtection.IsProtected(__instance))
			{
				return true;
			}
			if (hitData != null)
			{
				IplLog.Debug("已拦截对 " + ((Object)((Component)__instance).gameObject).name + " 的伤害 " + $"{hitData.GetTotalDamage():0.#}({DescribeAttacker(hitData)})");
			}
			return false;
		}

		private static string DescribeAttacker(HitData hitData)
		{
			Character attacker = hitData.GetAttacker();
			if (!((Object)(object)attacker != (Object)null))
			{
				return ((object)Unsafe.As<HitType, HitType>(ref hitData.m_hitType)/*cast due to .constrained prefix*/).ToString();
			}
			return attacker.m_name;
		}

		private static bool TryPatch(Harmony harmony, MethodBase target, HarmonyMethod prefix, string label)
		{
			if (target == null)
			{
				IplLog.Error("未找到目标方法,传送门保护不生效: " + label);
				return false;
			}
			try
			{
				harmony.Patch(target, prefix, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				IplLog.Info("已注入: " + label);
				return true;
			}
			catch (Exception exception)
			{
				IplLog.Error("注入失败 " + label + ": ", exception);
				return false;
			}
		}

		private static HarmonyMethod HarmonyMethodFor(string name)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method(typeof(Patches), name, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				throw new MissingMethodException("Patches." + name + " 不存在");
			}
			return new HarmonyMethod(methodInfo);
		}
	}
	[BepInPlugin("trigger.valheim.immortalportals", "Immortal Portals", "0.1.0")]
	public class ImmortalPortalsPlugin : BaseUnityPlugin
	{
		public const string Guid = "trigger.valheim.immortalportals";

		public const string PluginName = "Immortal Portals";

		public const string PluginVersion = "0.1.0";

		internal static ImmortalPortalsPlugin Instance;

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> ProtectWoodPortal;

		internal static ConfigEntry<bool> ProtectStonePortal;

		internal static ConfigEntry<bool> ProtectOtherPortals;

		internal static ConfigEntry<bool> DebugLog;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "启用传送门保护(总开关)。");
			ProtectWoodPortal = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ProtectWoodPortal", true, "保护木制传送门(portal_wood)。");
			ProtectStonePortal = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ProtectStonePortal", true, "保护石制传送门(portal_stone)。");
			ProtectOtherPortals = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ProtectOtherPortals", true, "保护其他带 TeleportWorld 组件的传送门(如模组新增的传送门变体)。");
			DebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugLog", false, "输出详细调试日志(排查用,默认关闭)。");
			IplLog.Info("Immortal Portals v0.1.0 初始化中...");
			_harmony = new Harmony("trigger.valheim.immortalportals");
			Patches.Install(_harmony);
			IplLog.Info("Immortal Portals v0.1.0 初始化完成。" + $"Enabled={Enabled.Value}, 木门={ProtectWoodPortal.Value}, " + $"石门={ProtectStonePortal.Value}, 其他={ProtectOtherPortals.Value}");
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}
	}
	internal static class PortalProtection
	{
		internal const string WoodPortalPrefab = "portal_wood";

		internal const string StonePortalPrefab = "portal_stone";

		internal static bool IsProtected(WearNTear wearNTear)
		{
			if ((Object)(object)wearNTear == (Object)null || !ImmortalPortalsPlugin.Enabled.Value)
			{
				return false;
			}
			if ((Object)(object)((Component)wearNTear).GetComponent<TeleportWorld>() == (Object)null)
			{
				return false;
			}
			string prefabName = Utils.GetPrefabName(((Object)((Component)wearNTear).gameObject).name);
			if (prefabName == "portal_wood")
			{
				return ImmortalPortalsPlugin.ProtectWoodPortal.Value;
			}
			if (prefabName == "portal_stone")
			{
				return ImmortalPortalsPlugin.ProtectStonePortal.Value;
			}
			return ImmortalPortalsPlugin.ProtectOtherPortals.Value;
		}
	}
}