Skip to content

Commit c7b1b41

Browse files
committed
Add support for Remote Dispatch
Closes #16
1 parent e2b475d commit c7b1b41

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

Diff for: Multiplayer/Multiplayer.cs

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using JetBrains.Annotations;
55
using Multiplayer.Components.Networking;
66
using Multiplayer.Editor;
7+
using Multiplayer.Patches.Mods;
78
using Multiplayer.Patches.World;
89
using UnityChan;
910
using UnityEngine;
@@ -40,6 +41,13 @@ private static bool Load(UnityModManager.ModEntry modEntry)
4041
harmony.PatchAll();
4142
SimComponent_Tick_Patch.Patch(harmony);
4243

44+
UnityModManager.ModEntry remoteDispatch = UnityModManager.FindMod("RemoteDispatch");
45+
if (remoteDispatch?.Enabled == true)
46+
{
47+
Log("Found RemoteDispatch, patching...");
48+
RemoteDispatchPatch.Patch(harmony, remoteDispatch.Assembly);
49+
}
50+
4351
if (!LoadAssets())
4452
return false;
4553

Diff for: Multiplayer/Patches/Mods/RemoteDispatchPatch.cs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

Diff for: info.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"DisplayName": "Multiplayer",
55
"Author": "Insprill",
66
"EntryMethod": "Multiplayer.Multiplayer.Load",
7-
"ManagerVersion": "0.27.3"
7+
"ManagerVersion": "0.27.3",
8+
"LoadAfter": [ "RemoteDispatch" ]
89
}

0 commit comments

Comments
 (0)