using System;
using System.Collections;
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.6.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.2.6.0")]
namespace ProperPortals;
[BepInPlugin("dev.crystal.properportals", "Proper Portals", "1.2.6.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class ProperPortalsPlugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(Inventory))]
private static class Inventory_Patches
{
[CompilerGenerated]
private sealed class <IsTeleportable_Transpiler>d__0 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
{
private int <>1__state;
private CodeInstruction <>2__current;
private int <>l__initialThreadId;
CodeInstruction IEnumerator<CodeInstruction>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <IsTeleportable_Transpiler>d__0(int <>1__state)
{
this.<>1__state = <>1__state;
<>l__initialThreadId = Environment.CurrentManagedThreadId;
}
[DebuggerHidden]
void IDisposable.Dispose()
{
<>1__state = -2;
}
private bool MoveNext()
{
//IL_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Expected O, but got Unknown
//IL_004a: Unknown result type (might be due to invalid IL or missing references)
//IL_0054: Expected O, but got Unknown
switch (<>1__state)
{
default:
return false;
case 0:
<>1__state = -1;
<>2__current = new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
<>1__state = 1;
return true;
case 1:
<>1__state = -1;
<>2__current = new CodeInstruction(OpCodes.Ret, (object)null);
<>1__state = 2;
return true;
case 2:
<>1__state = -1;
return false;
}
}
bool IEnumerator.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
return this.MoveNext();
}
[DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
[DebuggerHidden]
IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
{
if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
{
<>1__state = 0;
return this;
}
return new <IsTeleportable_Transpiler>d__0(0);
}
[DebuggerHidden]
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
}
}
[IteratorStateMachine(typeof(<IsTeleportable_Transpiler>d__0))]
[HarmonyPatch("IsTeleportable")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> IsTeleportable_Transpiler(IEnumerable<CodeInstruction> instructions)
{
//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
return new <IsTeleportable_Transpiler>d__0(-2);
}
}
[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
{
[CompilerGenerated]
private sealed class <UpdateTeleport_Transpiler>d__0 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
{
private int <>1__state;
private CodeInstruction <>2__current;
private int <>l__initialThreadId;
private IEnumerable<CodeInstruction> instructions;
public IEnumerable<CodeInstruction> <>3__instructions;
private IEnumerator<CodeInstruction> <>7__wrap1;
CodeInstruction IEnumerator<CodeInstruction>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <UpdateTeleport_Transpiler>d__0(int <>1__state)
{
this.<>1__state = <>1__state;
<>l__initialThreadId = Environment.CurrentManagedThreadId;
}
[DebuggerHidden]
void IDisposable.Dispose()
{
int num = <>1__state;
if (num == -3 || num == 1)
{
try
{
}
finally
{
<>m__Finally1();
}
}
<>7__wrap1 = null;
<>1__state = -2;
}
private bool MoveNext()
{
try
{
switch (<>1__state)
{
default:
return false;
case 0:
<>1__state = -1;
<>7__wrap1 = instructions.GetEnumerator();
<>1__state = -3;
break;
case 1:
<>1__state = -3;
break;
}
if (<>7__wrap1.MoveNext())
{
CodeInstruction current = <>7__wrap1.Current;
if (current.opcode == OpCodes.Ldc_R4)
{
float num = (float)current.operand;
if (num == 2f)
{
current.operand = FadeTime.Value;
}
else if (num == 8f)
{
current.operand = FadeTime.Value + MinPortalTime.Value;
}
else if (num == 15f)
{
current.operand = FadeTime.Value + MinPortalTime.Value + 0.5f;
}
}
<>2__current = current;
<>1__state = 1;
return true;
}
<>m__Finally1();
<>7__wrap1 = null;
return false;
}
catch
{
//try-fault
((IDisposable)this).Dispose();
throw;
}
}
bool IEnumerator.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
return this.MoveNext();
}
private void <>m__Finally1()
{
<>1__state = -1;
if (<>7__wrap1 != null)
{
<>7__wrap1.Dispose();
}
}
[DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
[DebuggerHidden]
IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
{
<UpdateTeleport_Transpiler>d__0 <UpdateTeleport_Transpiler>d__;
if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
{
<>1__state = 0;
<UpdateTeleport_Transpiler>d__ = this;
}
else
{
<UpdateTeleport_Transpiler>d__ = new <UpdateTeleport_Transpiler>d__0(0);
}
<UpdateTeleport_Transpiler>d__.instructions = <>3__instructions;
return <UpdateTeleport_Transpiler>d__;
}
[DebuggerHidden]
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
}
}
[IteratorStateMachine(typeof(<UpdateTeleport_Transpiler>d__0))]
[HarmonyPatch("UpdateTeleport")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> UpdateTeleport_Transpiler(IEnumerable<CodeInstruction> instructions)
{
//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
return new <UpdateTeleport_Transpiler>d__0(-2)
{
<>3__instructions = instructions
};
}
}
[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);
}
}
}