-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkMessageHandler.cs
52 lines (45 loc) · 1.71 KB
/
NetworkMessageHandler.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
using BepInEx;
using UnityEngine;
using UnityEngine.Networking;
namespace MyLethalCompanyPlugin
{
public static class NetworkMessageHandler
{
private const short PluginCheckMessage = 1001;
public static void RegisterHandlers()
{
if (NetworkServer.active) // If this is the server (host)
{
NetworkServer.RegisterHandler(PluginCheckMessage, OnPluginCheckResponse);
SendPluginCheckMessage();
}
else if (NetworkClient.active) // If this is a client
{
NetworkManager.singleton.client.RegisterHandler(PluginCheckMessage, OnPluginCheckRequest);
}
}
private static void SendPluginCheckMessage()
{
NetworkServer.SendToAll(PluginCheckMessage, new PluginCheckMessageData { IsPluginInstalled = true });
}
private static void OnPluginCheckRequest(NetworkMessage netMsg)
{
var response = new PluginCheckMessageData { IsPluginInstalled = true };
netMsg.conn.Send(PluginCheckMessage, response);
}
private static void OnPluginCheckResponse(NetworkMessage netMsg)
{
var data = netMsg.ReadMessage<PluginCheckMessageData>();
if (!data.IsPluginInstalled)
{
// Handle the case where the client does not have the plugin installed
PreventCosmeticConfigPlugin.LoggerInstance.LogError("A client does not have the required plugin installed. Disconnecting...");
NetworkServer.DisconnectAll();
}
}
}
public class PluginCheckMessageData : MessageBase
{
public bool IsPluginInstalled;
}
}