-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameClient+BattlEye.cs
149 lines (137 loc) · 4.69 KB
/
GameClient+BattlEye.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using BattlEye;
using ENet;
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
//A clipping from my old project!!!
public sealed partial class GameClient : ENetClient
{
IntPtr battlEyeClientHandle = IntPtr.Zero;
BEClient.BECL_GAME_DATA battlEyeClientInitData = null;
BEClient.BECL_BE_DATA battlEyeClientRunData = null;
private bool BattlEyeInitialized = false;
//somehow inform the client at the authorization stage that you need to initialize the anti-cheat
private bool RequireInitializeBattlEye(ByteStream stream)
{
string text = Path.Combine(Environment.CurrentDirectory, "BEClient_x64.dll");
if (!File.Exists(text))
{
text = Path.Combine(Environment.CurrentDirectory, "BEClient.dll");
}
if (!File.Exists(text))
{
Debug.LogError("Missing BattlEye client library! (" + text + ")");
return false;
}
Debug.Log("Try loading BattlEye server library from: " + text);
bool result = false;
try
{
battlEyeClientHandle = BEClient.LoadLibraryW(text);
if (battlEyeClientHandle == IntPtr.Zero)
{
Debug.LogError("Failed to load BattlEye client library!");
result = false;
}
BEClient.BEClientInitFn beclientInitFn = Marshal.GetDelegateForFunctionPointer(BEClient.GetProcAddress(battlEyeClientHandle, "Init"), typeof(BEClient.BEClientInitFn)) as BEClient.BEClientInitFn;
if (beclientInitFn == null)
{
BEClient.FreeLibrary(battlEyeClientHandle);
battlEyeClientHandle = IntPtr.Zero;
Debug.LogError("Failed to get BattlEye client init delegate!");
result = false;
}
uint ulAddress = stream.Read<uint>();
ushort usPort = stream.Read<ushort>();
battlEyeClientInitData = new BEClient.BECL_GAME_DATA();
battlEyeClientInitData.pstrGameVersion = Application.version;
battlEyeClientInitData.ulAddress = ulAddress;
battlEyeClientInitData.usPort = usPort;
battlEyeClientInitData.pfnPrintMessage = new BEClient.BECL_GAME_DATA.PrintMessageFn(battlEyeClientPrintMessage);
battlEyeClientInitData.pfnRequestRestart = new BEClient.BECL_GAME_DATA.RequestRestartFn(battlEyeClientRequestRestart);
battlEyeClientInitData.pfnSendPacket = new BEClient.BECL_GAME_DATA.SendPacketFn(battlEyeClientSendPacket);
battlEyeClientRunData = new BEClient.BECL_BE_DATA();
if (!beclientInitFn(2, battlEyeClientInitData, battlEyeClientRunData))
{
BEClient.FreeLibrary(battlEyeClientHandle);
battlEyeClientHandle = IntPtr.Zero;
Debug.LogError("Failed to call BattlEye client init!");
result = false;
}
}
catch { }
return result;
}
//need call every frame!!!
private void UpdateBattlEye()
{
if (BattlEyeInitialized && battlEyeClientRunData != null && battlEyeClientRunData.pfnRun != null)
{
battlEyeClientRunData.pfnRun();
}
}
//Always do it when the client disconnects!!
private void ShutdownBattlEye()
{
if (BattlEyeInitialized)
{
Debug.LogWarning("[GameClient] Shutting down BattlEye");
bool flag = battlEyeClientRunData.pfnExit();
if (flag) { Debug.Log("[GameClient] BattlEye shutdown complete!"); }
if (!flag) { Debug.Log("[GameClient] BattlEye shutdown failed!"); }
BEServer.FreeLibrary(battlEyeClientHandle);
BattlEyeInitialized = false;
}
}
private void battlEyeClientPrintMessage(string message)
{
SendLocalMessage(Color.red, "BattlEye", Color.yellow, message);
Debug.Log($"[GameClient] BattlEye: {message}");
}
private void battlEyeClientRequestRestart(int reason)
{
string reasonStr = string.Empty;
if (reason == 0)
{
reasonStr = "BattlEye broken!";
}
else if (reason == 1)
{
reasonStr = "BattlEye need update!";
}
else
{
reasonStr = "BattlEye unknown!";
}
Debug.Log($"BattlEye client requested restart with reason: {reason} [{reasonStr}]");
}
//sending data to BEServer using the game protocol
private void battlEyeClientSendPacket(IntPtr packetHandle, int length)
{
using(ByteStream bs = ByteStream.GetBitStream())
{
bs.Write(MessageID.BattlEye);
bs.Write(length);
bs.Write(packetHandle, length);
Send(bs.GetData(), bs.Length, PacketFlags.None, (byte)NetworkChannel.NetEvents);
}
}
//receiving data from BEServer using the game protocol
private unsafe void OnReceivedBattlEye(ByteStream stream, int length)
{
if (battlEyeClientHandle != IntPtr.Zero && battlEyeClientRunData != null && battlEyeClientRunData.pfnReceivedPacket != null)
{
int l = stream.Read<int>();
if (l > 0)
{
byte[] data = stream.ReadBytes(l);
fixed (byte* p = &data[0])
{
battlEyeClientRunData.pfnReceivedPacket(new IntPtr(p), l);
}
data = null;
}
}
}
}