Decompiled source of PvPEnabled v1.0.3

IronLabs.PvPEnabled.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using IronLabs.SharedLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("IronLabs.PvPEnabled")]
[assembly: AssemblyDescription("Keeps Valheim PvP permanently enabled.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("IronLabs.PvPEnabled")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("86006976-5150-4F0A-9A91-2104A9AC5C00")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.3.27463")]
namespace IronLabs.PvPEnabled
{
	[BepInPlugin("IronLabs.PvPEnabled", "IronLabs.PvPEnabled", "1.0.3")]
	public sealed class PvPEnabledPlugin : IronLabsPlugin
	{
		private const string PluginGuid = "IronLabs.PvPEnabled";

		private const string PluginName = "IronLabs.PvPEnabled";

		private const string PluginVersion = "1.0.3";

		private const float ActivationDelaySeconds = 5f;

		private Player _trackedPlayer;

		private float _activationTime;

		private static bool _waitingForRespawn;

		internal static bool EnforcementEnabled { get; private set; }

		internal static ModLog Log { get; private set; }

		private void Awake()
		{
			Log = InitializePlugin("IronLabs.PvPEnabled");
			Log.LogInfo("IronLabs.PvPEnabled 1.0.3 is loaded.");
		}

		private void Update()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				ResetPlayerTracking();
			}
			else if (_waitingForRespawn)
			{
				HandleRespawn(localPlayer);
			}
			else if ((Object)(object)_trackedPlayer != (Object)(object)localPlayer)
			{
				BeginActivationDelay(localPlayer);
			}
			else if (!EnforcementEnabled && Time.unscaledTime >= _activationTime)
			{
				EnforcementEnabled = true;
				localPlayer.SetPVP(true);
				Log.LogDebug("PvP was enabled after the five-second delay.");
			}
		}

		internal static void HandleLocalPlayerDeath(Player player)
		{
			EnforcementEnabled = false;
			_waitingForRespawn = true;
			player.SetPVP(false);
			Log.LogDebug("PvP was disabled when the local player died.");
		}

		private void HandleRespawn(Player player)
		{
			if (!((Character)player).IsDead())
			{
				_waitingForRespawn = false;
				BeginActivationDelay(player);
				Log.LogDebug("The post-respawn PvP activation delay started.");
			}
		}

		private void BeginActivationDelay(Player player)
		{
			_trackedPlayer = player;
			EnforcementEnabled = false;
			player.SetPVP(false);
			_activationTime = Time.unscaledTime + 5f;
			Log.LogDebug("Started the five-second PvP activation delay.");
		}

		private void ResetPlayerTracking()
		{
			_trackedPlayer = null;
			EnforcementEnabled = false;
		}

		private void OnDestroy()
		{
			ResetPlayerTracking();
			_waitingForRespawn = false;
			ShutdownPlugin();
			Log = null;
		}
	}
	[HarmonyPatch(typeof(Player), "SetPVP")]
	internal static class PreventPvpDisablePatch
	{
		private static void Prefix(Player __instance, ref bool enabled)
		{
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
			{
				enabled = PvPEnabledPlugin.EnforcementEnabled;
			}
		}
	}
	[HarmonyPatch(typeof(Player), "OnDeath")]
	internal static class DisablePvpOnDeathPatch
	{
		private static void Postfix(Player __instance)
		{
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
			{
				PvPEnabledPlugin.HandleLocalPlayerDeath(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Awake")]
	internal static class HidePvpTogglePatch
	{
		private static void Postfix(InventoryGui __instance)
		{
			if ((Object)(object)__instance.m_pvp != (Object)null)
			{
				((Component)__instance.m_pvp).gameObject.SetActive(false);
				PvPEnabledPlugin.Log.LogDebug("The vanilla PvP toggle was hidden.");
			}
		}
	}
}
namespace IronLabs.SharedLib
{
	public abstract class IronLabsPlugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		private bool _patchesApplied;

		protected ModLog InitializePlugin(string pluginGuid)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			ModLog modLog = new ModLog(((BaseUnityPlugin)this).Logger);
			Version version = ((object)this).GetType().Assembly.GetName().Version;
			modLog.LogInfo($"AssemblyVersion: {version}.");
			_harmony = new Harmony(pluginGuid);
			PatchOwnNamespace(modLog);
			return modLog;
		}

		protected void PatchOwnNamespace(ModLog log)
		{
			if (_patchesApplied)
			{
				log.LogDebug("Harmony patches are already active; skipping registration.");
				return;
			}
			string text = ((object)this).GetType().Namespace;
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			foreach (Type type in types)
			{
				if (type.Namespace == text)
				{
					_harmony.CreateClassProcessor(type).Patch();
				}
			}
			_patchesApplied = true;
			log.LogDebug("Harmony patches were applied for the plugin namespace.");
		}

		protected void ShutdownPlugin()
		{
			if (_patchesApplied)
			{
				Harmony harmony = _harmony;
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
				_patchesApplied = false;
			}
		}
	}
	public sealed class ModLog
	{
		private readonly ManualLogSource _logger;

		public ModLog(ManualLogSource logger)
		{
			_logger = logger;
		}

		public void LogFatal(object message)
		{
			Write((LogLevel)1, message);
		}

		public void LogError(object message)
		{
			Write((LogLevel)2, message);
		}

		public void LogWarning(object message)
		{
			Write((LogLevel)4, message);
		}

		public void LogMessage(object message)
		{
			Write((LogLevel)8, message);
		}

		public void LogInfo(object message)
		{
			Write((LogLevel)16, message);
		}

		public void LogDebug(object message)
		{
			Write((LogLevel)32, message);
		}

		public void Log(LogLevel level, object message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			Write(level, message);
		}

		private void Write(LogLevel level, object message)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			string arg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
			_logger.Log(level, (object)$"[{arg}] {message}");
		}
	}
}