using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
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: AssemblyTitle("QuickTeleport")]
[assembly: AssemblyDescription("https://valheim.thunderstore.io/package/OdinPlus/QuickTeleport/")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("elg")]
[assembly: AssemblyProduct("QuickTeleport")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace QuickTeleport
{
[BepInPlugin("elg.QuickTeleport", "QuickTeleport", "1.1.0")]
public class QuickTeleportPlugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(Player), "UpdateTeleport")]
private static class UpdateTeleportPatch
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> il = instructions.ToList();
for (int i = 0; i < il.Count; i++)
{
if (il[i].opcode == OpCodes.Ldc_R4 && CodeInstructionExtensions.OperandIs(il[i], (object)2))
{
il[i].opcode = OpCodes.Call;
il[i].operand = AccessTools.DeclaredMethod(typeof(UpdateTeleportPatch), "Set2SecDelayToZero", (Type[])null, (Type[])null);
}
yield return il[i];
if (i > 3 && il[i - 3].opcode == OpCodes.Ldc_R4 && CodeInstructionExtensions.OperandIs(il[i - 3], (object)8))
{
yield return new CodeInstruction(OpCodes.Call, (object)AccessTools.DeclaredMethod(typeof(QuickTeleportPlugin), "IsQuickTeleportEnabled", (Type[])null, (Type[])null));
yield return new CodeInstruction(OpCodes.Not, (object)null);
yield return new CodeInstruction(OpCodes.And, (object)null);
}
}
}
private static float Set2SecDelayToZero()
{
if (!IsQuickTeleportEnabled())
{
return 2f;
}
return 0f;
}
}
[HarmonyPatch(typeof(ZNetScene), "IsAreaReady")]
public static class IsAreaReadyPatch
{
private static bool Prefix(ref bool __result, Vector3 point, ref ZNetScene __instance)
{
//IL_0060: Unknown result type (might be due to invalid IL or missing references)
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_0066: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_008a: Unknown result type (might be due to invalid IL or missing references)
if (!IsQuickTeleportEnabled())
{
return true;
}
if (Game.instance.m_requestRespawn && Game.instance.m_firstSpawn)
{
return true;
}
ConfigEntry<bool>? skipLoadingArea = SkipLoadingArea;
if (skipLoadingArea != null && skipLoadingArea.Value)
{
QuickTeleportLogger.LogInfo((object)"Skipping area ready check");
__result = true;
return false;
}
ConfigEntry<bool>? skipLoadingObjects = SkipLoadingObjects;
if (skipLoadingObjects != null && skipLoadingObjects.Value)
{
Vector2i zone = ZoneSystem.GetZone(point);
if (!ZoneSystem.instance.IsZoneLoaded(zone))
{
__result = false;
return false;
}
__instance.m_tempCurrentObjects.Clear();
ZDOMan.instance.FindSectorObjects(zone, 1, 0, __instance.m_tempCurrentObjects, (List<ZDO>)null);
foreach (ZDO tempCurrentObject in __instance.m_tempCurrentObjects)
{
int prefab = tempCurrentObject.GetPrefab();
if (prefab != 0)
{
__instance.GetPrefab(prefab);
}
if (__instance.IsPrefabZDOValid(tempCurrentObject) && !Object.op_Implicit((Object)(object)__instance.FindInstance(tempCurrentObject)))
{
__result = false;
return false;
}
}
QuickTeleportLogger.LogInfo((object)"Skipping loading objects check");
__result = true;
return false;
}
return true;
}
}
internal const string ModName = "QuickTeleport";
internal const string ModVersion = "1.1.0";
internal const string Author = "elg";
private const string ModGUID = "elg.QuickTeleport";
private static readonly string ConfigFileName = "elg.QuickTeleport.cfg";
private static readonly string ConfigFileFullPath;
internal static ConfigEntry<bool>? EnableMod;
internal static ConfigEntry<bool>? SkipLoadingObjects;
internal static ConfigEntry<bool>? SkipLoadingArea;
private readonly Harmony _harmony = new Harmony("elg.QuickTeleport");
public static readonly ManualLogSource QuickTeleportLogger;
public void Awake()
{
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Expected O, but got Unknown
//IL_0047: Unknown result type (might be due to invalid IL or missing references)
//IL_0051: Expected O, but got Unknown
//IL_0072: Unknown result type (might be due to invalid IL or missing references)
//IL_007c: Expected O, but got Unknown
EnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enable Mod", true, new ConfigDescription("Default config, with just this one enabled, the teleports will be faster than vanilla.", (AcceptableValueBase)null, Array.Empty<object>()));
SkipLoadingObjects = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Loading", "Skip loading objects", false, new ConfigDescription("When enabled, ignores waiting for loading objects, but waits for the area and terrain to load. Warning: Can make you teleport to lower floors on buildings.", (AcceptableValueBase)null, Array.Empty<object>()));
SkipLoadingArea = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Loading", "Skip loading area", false, new ConfigDescription("Instant teleport. Warning: When enabled, ignores all loading on teleport, but your player can arrive before the world and objects load.", (AcceptableValueBase)null, Array.Empty<object>()));
Assembly executingAssembly = Assembly.GetExecutingAssembly();
_harmony.PatchAll(executingAssembly);
SetupWatcher();
}
private void OnDestroy()
{
((BaseUnityPlugin)this).Config.Save();
}
public static bool IsQuickTeleportEnabled()
{
return EnableMod?.Value ?? false;
}
private void SetupWatcher()
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
fileSystemWatcher.Changed += ReadConfigValues;
fileSystemWatcher.Created += ReadConfigValues;
fileSystemWatcher.Renamed += ReadConfigValues;
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
fileSystemWatcher.EnableRaisingEvents = true;
}
private void ReadConfigValues(object sender, FileSystemEventArgs e)
{
if (!File.Exists(ConfigFileFullPath))
{
return;
}
try
{
QuickTeleportLogger.LogDebug((object)"ReadConfigValues called");
((BaseUnityPlugin)this).Config.Reload();
}
catch
{
QuickTeleportLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
QuickTeleportLogger.LogError((object)"Please check your config entries for spelling and format!");
}
}
static QuickTeleportPlugin()
{
string configPath = Paths.ConfigPath;
char directorySeparatorChar = Path.DirectorySeparatorChar;
ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
QuickTeleportLogger = Logger.CreateLogSource("QuickTeleport");
}
}
}