Decompiled source of ProperPortals v1.2.5

ProperPortals.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ProperPortals")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("ProperPortals")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("21b0fa01-fc7d-40f1-a2a5-d5c0881c5e3d")]
[assembly: AssemblyFileVersion("1.2.5.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.2.5.0")]
namespace ProperPortals;

[BepInPlugin("dev.crystal.properportals", "Proper Portals", "1.2.5.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class ProperPortalsPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Inventory))]
	private static class Inventory_Patches
	{
		[HarmonyPatch("IsTeleportable")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> IsTeleportable_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			yield return new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
			yield return new CodeInstruction(OpCodes.Ret, (object)null);
		}
	}

	[HarmonyPatch(typeof(Hud))]
	private static class Hud_Patches
	{
		[HarmonyPatch("GetFadeDuration")]
		[HarmonyPrefix]
		private static bool GetFadeDuration_Prefix(Hud __instance, ref float __result, Player player)
		{
			if (player != null && ((Character)player).IsTeleporting())
			{
				__result = FadeTime.Value;
				return false;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Player))]
	private static class Player_Patches
	{
		[HarmonyPatch("UpdateTeleport")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> UpdateTeleport_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			foreach (CodeInstruction instruction in instructions)
			{
				if (instruction.opcode == OpCodes.Ldc_R4)
				{
					float num = (float)instruction.operand;
					if (num == 2f)
					{
						instruction.operand = FadeTime.Value;
					}
					else if (num == 8f)
					{
						instruction.operand = FadeTime.Value + MinPortalTime.Value;
					}
					else if (num == 15f)
					{
						instruction.operand = FadeTime.Value + MinPortalTime.Value + 0.5f;
					}
				}
				yield return instruction;
			}
		}
	}

	[HarmonyPatch(typeof(TeleportWorld))]
	private static class TeleportWorld_Patches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void Awake_Postfix(TeleportWorld __instance)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			if (((Behaviour)__instance).enabled)
			{
				UpdateActivationRange(__instance);
				sPortals[((Component)__instance).GetComponent<ZNetView>().GetZDO().m_uid] = __instance;
			}
		}
	}

	[HarmonyPatch(typeof(ZDOMan))]
	private static class ZDOMan_Patches
	{
		[HarmonyPatch("HandleDestroyedZDO")]
		[HarmonyPrefix]
		private static void HandleDestroyedZDO_Prefix(ZDOMan __instance, ZDOID uid)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			sPortals.Remove(uid);
		}
	}

	public const string ModId = "dev.crystal.properportals";

	public static ConfigEntry<bool> CarryAnything;

	public static ConfigEntry<float> FadeTime;

	public static ConfigEntry<float> MinPortalTime;

	public static ConfigEntry<float> ActivationRange;

	private static Harmony sInventoryHarmony;

	private static Harmony sHudHarmony;

	private static Harmony sPlayerHarmony;

	private static Harmony sTeleportWorldHarmony;

	private static Harmony sZDOManHarmony;

	private static Dictionary<ZDOID, TeleportWorld> sPortals;

	static ProperPortalsPlugin()
	{
		sPortals = new Dictionary<ZDOID, TeleportWorld>();
	}

	private void Awake()
	{
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Expected O, but got Unknown
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Expected O, but got Unknown
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Expected O, but got Unknown
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Expected O, but got Unknown
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Expected O, but got Unknown
		CarryAnything = ((BaseUnityPlugin)this).Config.Bind<bool>("Portal", "CarryAnything", true, "Whether to allow using portals while carrying portal restricted items such as metals. Game default is false.");
		CarryAnything.SettingChanged += CarryAnything_SettingChanged;
		FadeTime = ((BaseUnityPlugin)this).Config.Bind<float>("Portal", "FadeTime", 0.5f, "The time it takes to fade the screen before teleporting. Teleporting does not start until after the screen fade completes. Game default is 1.");
		FadeTime.SettingChanged += PortalTime_SettingChanged;
		MinPortalTime = ((BaseUnityPlugin)this).Config.Bind<float>("Portal", "MinPortalTime", 0f, "The minimum time to wait for a teleport to complete, in seconds. It can take longer if the target location needs to be loaded. Increase this if you have the issue of dropping in before loading completes. Game default is 8.");
		MinPortalTime.SettingChanged += PortalTime_SettingChanged;
		ActivationRange = ((BaseUnityPlugin)this).Config.Bind<float>("Portal", "ActivationRange", 2f, "The distance at which a portal will start glowing and making noise when a player approaches it. Maximum accepted value is 10. Setting to 0 prevents portals from glowing or making noise at all. Game default is 3.");
		ActivationRange.SettingChanged += ActivationRange_SettingChanged;
		ClampConfig();
		sInventoryHarmony = new Harmony("dev.crystal.properportals_Inventory");
		sHudHarmony = new Harmony("dev.crystal.properportals_Hud");
		sPlayerHarmony = new Harmony("dev.crystal.properportals_Player");
		sTeleportWorldHarmony = new Harmony("dev.crystal.properportals_TeleportWorld");
		sZDOManHarmony = new Harmony("dev.crystal.properportals_ZDOMan");
		if (CarryAnything.Value)
		{
			sInventoryHarmony.PatchAll(typeof(Inventory_Patches));
		}
		sHudHarmony.PatchAll(typeof(Hud_Patches));
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
		sTeleportWorldHarmony.PatchAll(typeof(TeleportWorld_Patches));
		sZDOManHarmony.PatchAll(typeof(ZDOMan_Patches));
	}

	private void OnDestroy()
	{
		sInventoryHarmony.UnpatchSelf();
		sHudHarmony.UnpatchSelf();
		sPlayerHarmony.UnpatchSelf();
		sTeleportWorldHarmony.UnpatchSelf();
		sZDOManHarmony.UnpatchSelf();
		sPortals.Clear();
	}

	private static void ClampConfig()
	{
		if (FadeTime.Value < 0f)
		{
			FadeTime.Value = 0f;
		}
		if ((double)FadeTime.Value > 10.0)
		{
			FadeTime.Value = 10f;
		}
		if (MinPortalTime.Value < 0f)
		{
			MinPortalTime.Value = 0f;
		}
		if ((double)MinPortalTime.Value > 60.0)
		{
			MinPortalTime.Value = 60f;
		}
		if (ActivationRange.Value < 0f)
		{
			ActivationRange.Value = 0f;
		}
		if ((double)ActivationRange.Value > 10.0)
		{
			ActivationRange.Value = 10f;
		}
	}

	private void CarryAnything_SettingChanged(object sender, EventArgs e)
	{
		if (CarryAnything.Value)
		{
			sInventoryHarmony.PatchAll(typeof(Inventory_Patches));
		}
		else
		{
			sInventoryHarmony.UnpatchSelf();
		}
	}

	private void PortalTime_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		sPlayerHarmony.UnpatchSelf();
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
	}

	private void ActivationRange_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (TeleportWorld value in sPortals.Values)
		{
			UpdateActivationRange(value);
		}
	}

	private static void UpdateActivationRange(TeleportWorld portal)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)portal))
		{
			portal.m_activationRange = ActivationRange.Value;
			Transform proximityRoot = portal.m_proximityRoot;
			proximityRoot.localPosition = new Vector3(proximityRoot.localPosition.x, proximityRoot.localPosition.y, ActivationRange.Value * 0.5f + 0.25f);
		}
	}
}