Decompiled source of Kaezia2026fixPersonalTeleportsDeadMoroz v1.0.1

PersonalTeleports.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
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 HarmonyLib;
using Jotunn.Configs;
using Jotunn.Managers;
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("PersonalTeleports")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("DeadMoroz")]
[assembly: AssemblyProduct("PersonalTeleports")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("555e2d06-795f-45b7-9973-467d66baf87e")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace PersonalTeleports;

[BepInPlugin("DeadMoroz.PersonalTeleports", "PersonalTeleports", "1.0.0")]
public class PersonalTeleportsPlugin : BaseUnityPlugin
{
	internal const string ModName = "PersonalTeleports";

	internal const string ModVersion = "1.0.0";

	internal const string ModAuthor = "DeadMoroz";

	private const string ModGUID = "DeadMoroz.PersonalTeleports";

	public static PersonalTeleportsPlugin instance;

	private Vector3 lastShipPos;

	private Vector3[] savedPos = (Vector3[])(object)new Vector3[2];

	private int savedCount = 0;

	private bool haveShip;

	private ConfigEntry<KeyboardShortcut> homeKeyConfig;

	private ConfigEntry<KeyboardShortcut> backKeyConfig;

	private ConfigEntry<KeyboardShortcut> deathKeyConfig;

	private ConfigEntry<KeyboardShortcut> shipKeyConfig;

	private ConfigEntry<float> safeDistance;

	private ButtonConfig homeButton;

	private ButtonConfig backButton;

	private ButtonConfig deathButton;

	private ButtonConfig shipButton;

	private void Awake()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Expected O, but got Unknown
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Expected O, but got Unknown
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Expected O, but got Unknown
		instance = this;
		homeKeyConfig = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "Return Home key", new KeyboardShortcut((KeyCode)278, Array.Empty<KeyCode>()), "Key to teleport to player spawn point");
		homeButton = new ButtonConfig
		{
			Name = "home",
			ShortcutConfig = homeKeyConfig,
			HintToken = "$homeButton"
		};
		InputManager.Instance.AddButton("DeadMoroz.PersonalTeleports", homeButton);
		backKeyConfig = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "Back key", new KeyboardShortcut((KeyCode)277, Array.Empty<KeyCode>()), "Key to teleport to last saved position");
		backButton = new ButtonConfig
		{
			Name = "back",
			ShortcutConfig = backKeyConfig,
			HintToken = "$backButton"
		};
		InputManager.Instance.AddButton("DeadMoroz.PersonalTeleports", backButton);
		deathKeyConfig = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "Death point key", new KeyboardShortcut((KeyCode)279, Array.Empty<KeyCode>()), "Key to teleport to player last death point");
		deathButton = new ButtonConfig
		{
			Name = "death",
			ShortcutConfig = deathKeyConfig,
			HintToken = "$deathButton"
		};
		InputManager.Instance.AddButton("DeadMoroz.PersonalTeleports", deathButton);
		shipKeyConfig = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "Return to last ship key", new KeyboardShortcut((KeyCode)280, Array.Empty<KeyCode>()), "Key to teleport to player last ship");
		shipButton = new ButtonConfig
		{
			Name = "ship",
			ShortcutConfig = shipKeyConfig,
			HintToken = "$shipButton"
		};
		InputManager.Instance.AddButton("DeadMoroz.PersonalTeleports", shipButton);
		safeDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Safe distance", 10f, "Minimal distance from spawn point, after which teleportation to home will be enabled. Used to protect of accidentaly overwriting last return point.");
		Harmony val = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	public void LoadFromPackage(ZPackage zpackage)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		haveShip = zpackage.ReadBool();
		lastShipPos = zpackage.ReadVector3();
		savedCount = zpackage.ReadInt();
		savedPos[0] = zpackage.ReadVector3();
		savedPos[1] = zpackage.ReadVector3();
		Debug.Log((object)$"Read from zpackage: have ship pos: {haveShip}, ship pos {lastShipPos}, saved pos count: {savedCount}, pos1: {savedPos[0]}, pos2 {savedPos[1]}");
	}

	public void SaveToPackage(ZPackage zpackage)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		zpackage.Write(haveShip);
		zpackage.Write(lastShipPos);
		zpackage.Write(savedCount);
		zpackage.Write(savedPos[0]);
		zpackage.Write(savedPos[1]);
	}

	public void SaveShipPos(Vector3 shipPos)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		lastShipPos = shipPos;
		haveShip = true;
	}

	private void SavePos()
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		savedPos[1] = savedPos[0];
		savedPos[0] = ((Component)Player.m_localPlayer).transform.position;
		if (savedCount < 2)
		{
			savedCount++;
		}
		Debug.Log((object)$"Saved pos = {savedPos[1]}");
	}

	private void Update()
	{
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		if (ZInput.instance == null || !((Object)(object)Game.instance != (Object)null))
		{
			return;
		}
		PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
		if (ZInput.GetButtonDown(homeButton.Name))
		{
			Vector3 val = (playerProfile.HaveCustomSpawnPoint() ? playerProfile.GetCustomSpawnPoint() : playerProfile.GetHomePoint());
			val.y += 0.5f;
			if (!(Vector3.Distance(((Component)Player.m_localPlayer).transform.position, val) < safeDistance.Value))
			{
				if ((Object)(object)((Character)Player.m_localPlayer).GetStandingOnShip() == (Object)null)
				{
					SavePos();
				}
				((Character)Player.m_localPlayer).TeleportTo(val, ((Component)Player.m_localPlayer).transform.rotation, true);
			}
		}
		else if (ZInput.GetButtonDown(backButton.Name) && savedCount > 0)
		{
			Vector3 val2 = savedPos[0];
			if (savedCount > 1)
			{
				savedPos[0] = savedPos[1];
				savedPos[1] = val2;
			}
			((Character)Player.m_localPlayer).TeleportTo(val2, ((Component)Player.m_localPlayer).transform.rotation, true);
		}
		else if (ZInput.GetButtonDown(deathButton.Name) && playerProfile.HaveDeathPoint())
		{
			((Character)Player.m_localPlayer).TeleportTo(playerProfile.GetDeathPoint(), ((Component)Player.m_localPlayer).transform.rotation, true);
		}
		else if (ZInput.GetButtonDown(shipButton.Name) && haveShip)
		{
			Debug.Log((object)$"Teleporting to ship at {lastShipPos}");
			((Character)Player.m_localPlayer).TeleportTo(lastShipPos, ((Component)Player.m_localPlayer).transform.rotation, true);
		}
	}
}
[HarmonyPatch]
internal class PlayerPatcher
{
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	[HarmonyPostfix]
	public static void Player_OnSpawned(Player __instance)
	{
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Expected O, but got Unknown
		string text = Path.Combine(Paths.ConfigPath, $"PersonalTeleports_{__instance.GetPlayerID()}_{ZNet.instance.GetWorldUID()}.dat");
		if (!File.Exists(text))
		{
			return;
		}
		byte[] array;
		try
		{
			using FileStream input = new FileStream(text, FileMode.Open, FileAccess.Read, FileShare.Read);
			using BinaryReader binaryReader = new BinaryReader(input);
			int count = binaryReader.ReadInt32();
			array = binaryReader.ReadBytes(count);
		}
		catch (Exception ex)
		{
			Debug.Log((object)$"Error loading saved positions data. Path: {text}, Error: {ex.Message}");
			return;
		}
		ZPackage zpackage = new ZPackage(array);
		if ((Object)(object)PersonalTeleportsPlugin.instance != (Object)null)
		{
			PersonalTeleportsPlugin.instance.LoadFromPackage(zpackage);
		}
	}

	[HarmonyPatch(typeof(Player), "Save")]
	[HarmonyPrefix]
	public static void Player_Save(Player __instance)
	{
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Expected O, but got Unknown
		if ((Object)(object)Player.m_localPlayer != (Object)(object)__instance)
		{
			return;
		}
		string path = Path.Combine(Paths.ConfigPath, $"PersonalTeleports_{__instance.GetPlayerID()}_{ZNet.instance.GetWorldUID()}.dat");
		ZPackage val = new ZPackage();
		if ((Object)(object)PersonalTeleportsPlugin.instance != (Object)null)
		{
			PersonalTeleportsPlugin.instance.SaveToPackage(val);
		}
		try
		{
			using FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write);
			using BinaryWriter binaryWriter = new BinaryWriter(output);
			byte[] array = val.GetArray();
			binaryWriter.Write(array.Length);
			binaryWriter.Write(array);
		}
		catch (Exception ex)
		{
			Debug.Log((object)$"Failed to save personal teleports data: {ex.Message}");
		}
	}
}
[HarmonyPatch]
internal class ShipPatcher
{
	[HarmonyPatch(typeof(Ship), "OnTriggerExit")]
	[HarmonyPostfix]
	public static void Ship_OnTriggerExit(ref Ship __instance, Collider collider)
	{
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		Debug.Log((object)$"Exited from {__instance}, {__instance.IsOwner()}");
		if (__instance.IsOwner())
		{
			Player component = ((Component)collider).GetComponent<Player>();
			Debug.Log((object)$"Triggered by player {(Object)(object)component == (Object)(object)Player.m_localPlayer}");
			if ((Object)(object)component == (Object)(object)Player.m_localPlayer)
			{
				Vector3 val = default(Vector3);
				((Vector3)(ref val))..ctor(0.5f, 0.5f, 0f);
				Vector3 shipPos = ((Component)__instance).transform.position + ((Component)__instance).transform.rotation * val;
				PersonalTeleportsPlugin.instance.SaveShipPos(shipPos);
			}
		}
	}
}
[HarmonyPatch]
internal class PlayerProfilePatcher
{
	[HarmonyPatch(typeof(PlayerProfile), "SavePlayerToDisk")]
	[HarmonyPrefix]
	public static bool PlayerProfile_SavePlayerToDisk(PlayerProfile __instance)
	{
		return true;
	}
}