|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | +using DV.JObjectExtstensions; |
| 4 | +using HarmonyLib; |
| 5 | +using Multiplayer.Components.Networking; |
| 6 | +using Multiplayer.Components.Networking.Player; |
| 7 | +using Newtonsoft.Json.Linq; |
| 8 | +using UnityEngine; |
| 9 | + |
| 10 | +namespace Multiplayer.Patches.Mods; |
| 11 | + |
| 12 | +public static class RemoteDispatchPatch |
| 13 | +{ |
| 14 | + private const byte DECIMAL_PLACES = 8; |
| 15 | + private const float DEGREES_PER_METER = 360f / 40e6f; |
| 16 | + |
| 17 | + private static MethodInfo Sessions_AddTag; |
| 18 | + |
| 19 | + public static void Patch(Harmony harmony, Assembly assembly) |
| 20 | + { |
| 21 | + foreach (Type type in assembly.ExportedTypes) |
| 22 | + { |
| 23 | + if (type.Namespace != "DvMod.RemoteDispatch") |
| 24 | + continue; |
| 25 | + switch (type.Name) |
| 26 | + { |
| 27 | + case "PlayerData": |
| 28 | + MethodInfo getPlayerData = AccessTools.DeclaredMethod(type, "GetPlayerData"); |
| 29 | + MethodInfo getPlayerDataPostfix = AccessTools.Method(typeof(RemoteDispatchPatch), nameof(GetPlayerData_Postfix)); |
| 30 | + harmony.Patch(getPlayerData, postfix: new HarmonyMethod(getPlayerDataPostfix)); |
| 31 | + |
| 32 | + MethodInfo checkTransform = AccessTools.DeclaredMethod(type, "CheckTransform"); |
| 33 | + MethodInfo CheckTransformPostfix = AccessTools.Method(typeof(RemoteDispatchPatch), nameof(CheckTransform_Postfix)); |
| 34 | + harmony.Patch(checkTransform, postfix: new HarmonyMethod(CheckTransformPostfix)); |
| 35 | + break; |
| 36 | + case "Sessions": |
| 37 | + Sessions_AddTag = AccessTools.DeclaredMethod(type, "AddTag", new[] { typeof(string) }); |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static void GetPlayerData_Postfix(ref JObject __result) |
| 44 | + { |
| 45 | + if (!NetworkLifecycle.Instance.IsClientRunning) |
| 46 | + return; |
| 47 | + |
| 48 | + foreach (NetworkedPlayer player in NetworkLifecycle.Instance.Client.PlayerManager.Players) |
| 49 | + { |
| 50 | + JObject data = new(); |
| 51 | + |
| 52 | + Transform playerTransform = player.transform; |
| 53 | + Vector3 position = playerTransform.position - WorldMover.currentMove; |
| 54 | + float rotation = playerTransform.eulerAngles.y; |
| 55 | + |
| 56 | + JArray latLon = new( |
| 57 | + Math.Round(DEGREES_PER_METER * position.z, DECIMAL_PLACES), |
| 58 | + Math.Round(DEGREES_PER_METER * position.x, DECIMAL_PLACES) |
| 59 | + ); |
| 60 | + |
| 61 | + data.SetString("color", "aqua"); |
| 62 | + data.Add("position", latLon); |
| 63 | + data.SetFloat("rotation", rotation); |
| 64 | + __result.SetJObject(player.Username, data); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static void CheckTransform_Postfix() |
| 69 | + { |
| 70 | + Sessions_AddTag?.Invoke(null, new object[] { "player" }); |
| 71 | + } |
| 72 | +} |
0 commit comments