Decompiled source of PlayerPinSnap v0.1.0

PlayerPinSnap.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PlayerPinSnap;

[BepInPlugin("j1ga.playerpinsnap", "PlayerPinSnap", "0.1.0")]
public class PlayerPinSnapPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Minimap), "UpdatePlayerPins")]
	private static class Minimap_UpdatePlayerPins_Patch
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo moveTowards = AccessTools.Method(typeof(Vector3), "MoveTowards", (Type[])null, (Type[])null);
			MethodInfo step = AccessTools.Method(typeof(PlayerPinSnapPlugin), "PinStep", (Type[])null, (Type[])null);
			int replaced = 0;
			foreach (CodeInstruction ci in instructions)
			{
				if (CodeInstructionExtensions.Calls(ci, moveTowards))
				{
					ci.opcode = OpCodes.Call;
					ci.operand = step;
					replaced++;
				}
				yield return ci;
			}
			if (replaced != 1)
			{
				_log.LogWarning((object)("Minimap.UpdatePlayerPins: expected one Vector3.MoveTowards, replaced " + replaced + "."));
			}
		}
	}

	public const string Guid = "j1ga.playerpinsnap";

	public const string Name = "PlayerPinSnap";

	public const string Version = "0.1.0";

	private const float VanillaGlideSpeed = 200f;

	private const float VanillaSendInterval = 2f;

	private static ConfigEntry<bool> _enabled;

	private static ConfigEntry<float> _snapDistance;

	private static ConfigEntry<float> _glideSpeed;

	private static ConfigEntry<float> _sendInterval;

	private static ManualLogSource _log;

	private static MethodInfo _sendPlayerList;

	private static MethodInfo _sendServerSyncData;

	private float _sendTimer;

	private bool _sendFailed;

	private Harmony _harmony;

	private void Awake()
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Expected O, but got Unknown
		//IL_015f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Expected O, but got Unknown
		_log = ((BaseUnityPlugin)this).Logger;
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("01 General", "Enabled", true, "Off: pins glide and positions are sent as in the game.");
		_snapDistance = ((BaseUnityPlugin)this).Config.Bind<float>("02 Map", "SnapDistance", 100f, new ConfigDescription("A player's pin further than this from their new position (meters) jumps there at once (a teleport, a portal). Closer, it glides. 0: pins always jump.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10000f), new object[0]));
		_glideSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("02 Map", "GlideSpeed", 200f, new ConfigDescription("Speed of a gliding pin, meters per second. In the game: 200.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 10000f), new object[0]));
		_sendInterval = ((BaseUnityPlugin)this).Config.Bind<float>("03 Network", "SendInterval", 0.5f, new ConfigDescription("How often player positions are sent, seconds: on the server the list of everyone's positions, on a client its own position. In the game: every 2 seconds; 2 here leaves it as in the game.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 2f), new object[0]));
		_sendPlayerList = AccessTools.Method(typeof(ZNet), "SendPlayerList", (Type[])null, (Type[])null);
		_sendServerSyncData = AccessTools.Method(typeof(ZNet), "SendServerSyncPlayerData", new Type[1] { typeof(ZNetPeer) }, (Type[])null);
		if (_sendPlayerList == null || _sendServerSyncData == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"ZNet.SendPlayerList or ZNet.SendServerSyncPlayerData not found: positions are sent as in the game.");
		}
		_harmony = new Harmony("j1ga.playerpinsnap");
		Type[] array = new Type[1] { typeof(Minimap_UpdatePlayerPins_Patch) };
		foreach (Type type in array)
		{
			try
			{
				_harmony.CreateClassProcessor(type).Patch();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Patch " + type.Name + " failed: " + ex));
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PlayerPinSnap 0.1.0 loaded.");
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}

	private void Update()
	{
		ZNet instance = ZNet.instance;
		float value = _sendInterval.Value;
		if ((Object)(object)instance == (Object)null || !_enabled.Value || _sendFailed || value >= 2f)
		{
			_sendTimer = 0f;
			return;
		}
		_sendTimer += Time.unscaledDeltaTime;
		if (_sendTimer < value)
		{
			return;
		}
		_sendTimer = 0f;
		try
		{
			if (instance.IsServer())
			{
				if (_sendPlayerList != null && instance.GetPeers().Count > 0)
				{
					_sendPlayerList.Invoke(instance, null);
				}
				return;
			}
			ZNetPeer serverPeer = instance.GetServerPeer();
			if (serverPeer != null && serverPeer.IsReady() && _sendServerSyncData != null)
			{
				_sendServerSyncData.Invoke(instance, new object[1] { serverPeer });
			}
		}
		catch (Exception ex)
		{
			_sendFailed = true;
			((BaseUnityPlugin)this).Logger.LogError((object)("Sending positions failed, positions are sent as in the game from now on: " + ex));
		}
	}

	public static Vector3 PinStep(Vector3 current, Vector3 target, float maxDelta)
	{
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: 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_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		if (_enabled.Value)
		{
			float value = _snapDistance.Value;
			Vector3 val = target - current;
			if (((Vector3)(ref val)).sqrMagnitude > value * value)
			{
				return target;
			}
			maxDelta *= _glideSpeed.Value / 200f;
		}
		return Vector3.MoveTowards(current, target, maxDelta);
	}
}