-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVersionHandshake.cs
126 lines (115 loc) · 5.43 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
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using HarmonyLib;
namespace ItemManagerModTemplate
{
[HarmonyPatch(typeof(ZNet), nameof(ZNet.OnNewConnection))]
public static class RegisterAndCheckVersion
{
private static void Prefix(ZNetPeer peer, ref ZNet __instance)
{
// Register version check call
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.LogDebug("Registering version RPC handler");
peer.m_rpc.Register($"{ItemManagerModTemplatePlugin.ModName}_VersionCheck",
new Action<ZRpc, ZPackage>(RpcHandlers.RPC_ItemManagerModTemplate_Version));
// Make calls to check versions
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.LogInfo("Invoking version check");
ZPackage zpackage = new();
zpackage.Write(ItemManagerModTemplatePlugin.ModVersion);
peer.m_rpc.Invoke($"{ItemManagerModTemplatePlugin.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
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.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(), $"{ItemManagerModTemplatePlugin.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" + ItemManagerModTemplatePlugin.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
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.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_ItemManagerModTemplate_Version(ZRpc rpc, ZPackage pkg)
{
string? version = pkg.ReadString();
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.LogInfo("Version check, local: " +
ItemManagerModTemplatePlugin.ModVersion +
", remote: " + version);
if (version != ItemManagerModTemplatePlugin.ModVersion)
{
ItemManagerModTemplatePlugin.ConnectionError = $"{ItemManagerModTemplatePlugin.ModName} Installed: {ItemManagerModTemplatePlugin.ModVersion}\n Needed: {version}";
if (!ZNet.instance.IsServer()) return;
// Different versions - force disconnect client from server
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.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
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.LogInfo("Received same version from server!");
}
else
{
// Add client to validated list
ItemManagerModTemplatePlugin.ItemManagerModTemplateLogger.LogInfo(
$"Adding peer ({rpc.m_socket.GetHostName()}) to validated list");
ValidatedPeers.Add(rpc);
}
}
}
public static string ComputeHashForMod()
{
using SHA256 sha256Hash = SHA256.Create();
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(File.ReadAllBytes(Assembly.GetExecutingAssembly().Location));
// Convert byte array to a string
StringBuilder builder = new();
foreach (byte b in bytes)
{
builder.Append(b.ToString("X2"));
}
return builder.ToString();
}
}
}