-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersionHandshake.cs
116 lines (107 loc) · 4.44 KB
/
VersionHandshake.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using HarmonyLib;
namespace Vert2
{
[HarmonyPatch(typeof(ZNet), nameof(ZNet.OnNewConnection))]
public static class RegisterAndCheckVersion
{
private static void Prefix(ZNetPeer peer, ref ZNet __instance)
{
// Register version check call
Vert2Plugin.Vert2Logger.LogDebug("Registering version RPC handler");
peer.m_rpc.Register($"{Vert2Plugin.ModName}_VersionCheck",
new Action<ZRpc, ZPackage>(RpcHandlers.RPC_Vert2_Version));
// Make calls to check versions
Vert2Plugin.Vert2Logger.LogDebug("Invoking version check");
ZPackage zpackage = new();
zpackage.Write(Vert2Plugin.ModVersion);
peer.m_rpc.Invoke($"{Vert2Plugin.ModName}_VersionCheck", zpackage);
}
}
[HarmonyPatch(typeof(ZNet), nameof(ZNet.RPC_PeerInfo))]
public static class VerifyClient
{
private static bool Prefix(ZRpc rpc, ZPackage pkg, ref ZNet __instance)
{
if (!__instance.IsServer() || RpcHandlers.ValidatedPeers.Contains(rpc)) return true;
// Disconnect peer if they didn't send mod version at all
Vert2Plugin.Vert2Logger.LogWarning(
$"Peer ({rpc.m_socket.GetHostName()}) never sent version or couldn't due to previous disconnect, disconnecting");
rpc.Invoke("Error", 3);
return false; // Prevent calling underlying method
}
private static void Postfix(ZNet __instance)
{
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.instance.GetServerPeerID(),
$"{Vert2Plugin.ModName}RequestAdminSync",
new ZPackage());
}
}
[HarmonyPatch(typeof(FejdStartup), nameof(FejdStartup.ShowConnectError))]
public class ShowConnectionError
{
private static void Postfix(FejdStartup __instance)
{
if (__instance.m_connectionFailedPanel.activeSelf)
{
__instance.m_connectionFailedError.fontSizeMax = 25;
__instance.m_connectionFailedError.fontSizeMin = 15;
__instance.m_connectionFailedError.text += "\n" + Vert2Plugin.ConnectionError;
}
}
}
[HarmonyPatch(typeof(ZNet), nameof(ZNet.Disconnect))]
public static class RemoveDisconnectedPeerFromVerified
{
private static void Prefix(ZNetPeer peer, ref ZNet __instance)
{
if (!__instance.IsServer()) return;
// Remove peer from validated list
Vert2Plugin.Vert2Logger.LogInfo(
$"Peer ({peer.m_rpc.m_socket.GetHostName()}) disconnected, removing from validated list");
_ = RpcHandlers.ValidatedPeers.Remove(peer.m_rpc);
}
}
public static class RpcHandlers
{
public static readonly List<ZRpc> ValidatedPeers = new();
public static void RPC_Vert2_Version(ZRpc rpc, ZPackage pkg)
{
string? version = pkg.ReadString();
Vert2Plugin.Vert2Logger.LogInfo("Version check, local: " +
Vert2Plugin.ModVersion +
", remote: " + version);
if (version != Vert2Plugin.ModVersion)
{
Vert2Plugin.ConnectionError =
$"{Vert2Plugin.ModName} Installed: {Vert2Plugin.ModVersion}\n Needed: {version}";
if (!ZNet.instance.IsServer()) return;
// Different versions - force disconnect client from server
Vert2Plugin.Vert2Logger.LogWarning(
$"Peer ({rpc.m_socket.GetHostName()}) has incompatible version, disconnecting...");
rpc.Invoke("Error", 3);
}
else
{
if (!ZNet.instance.IsServer())
{
// Enable mod on client if versions match
Vert2Plugin.Vert2Logger.LogInfo(
"Received same version from server!");
}
else
{
// Add client to validated list
Vert2Plugin.Vert2Logger.LogInfo(
$"Adding peer ({rpc.m_socket.GetHostName()}) to validated list");
ValidatedPeers.Add(rpc);
}
}
}
}
}