Skip to content

Commit b907131

Browse files
Fixes SIMD comparing operations + GeoIP initialization bug fix.
Fixes SIMD comparing operations + GeoIP initialization bug fix.
1 parent 4230e7f commit b907131

File tree

5 files changed

+78
-140
lines changed

5 files changed

+78
-140
lines changed

BackendServices/CyberBackendLibrary/DataTypes/DataTypesUtils.cs

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ public static bool AreArraysIdentical(byte[] arr1, byte[] arr2)
4646
for (int i = 0; i < arr1.Length; i++)
4747
{
4848
#if NETCOREAPP3_0_OR_GREATER
49-
if (Avx2.IsSupported && Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, arr1[i]), Vector256<byte>.Zero.WithElement(0, arr2[i]))) == 0)
50-
return false;
51-
else if (Sse2.IsSupported && Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, arr1[i]), Vector128<byte>.Zero.WithElement(0, arr2[i]))) == 0)
52-
return false;
53-
else if (arr1[i] != arr2[i])
49+
if (!AreBytesIdentical(arr1[i], arr2[i]))
5450
return false;
5551
#else
5652
if (arr1[i] != arr2[i])
@@ -197,70 +193,14 @@ public static int FindBytePattern(byte[] buffer, byte[] searchPattern, int offse
197193
for (int i = offset; i <= buffer.Length - searchPattern.Length; i++)
198194
{
199195
#if NETCOREAPP3_0_OR_GREATER
200-
if (Avx2.IsSupported)
201-
{
202-
if (Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, buffer[i]), Vector256<byte>.Zero.WithElement(0, searchPattern[0]))) != 0)
203-
{
204-
if (buffer.Length > 1)
205-
{
206-
bool matched = true;
207-
for (int y = 1; y <= searchPattern.Length - 1; y++)
208-
{
209-
if (Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, buffer[i + y]), Vector256<byte>.Zero.WithElement(0, searchPattern[y]))) == 0)
210-
{
211-
matched = false;
212-
break;
213-
}
214-
}
215-
if (matched)
216-
{
217-
found = i;
218-
break;
219-
}
220-
}
221-
else
222-
{
223-
found = i;
224-
break;
225-
}
226-
}
227-
}
228-
else if (Sse2.IsSupported)
229-
{
230-
if (Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, buffer[i]), Vector128<byte>.Zero.WithElement(0, searchPattern[0]))) != 0)
231-
{
232-
if (buffer.Length > 1)
233-
{
234-
bool matched = true;
235-
for (int y = 1; y <= searchPattern.Length - 1; y++)
236-
{
237-
if (Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, buffer[i + y]), Vector128<byte>.Zero.WithElement(0, searchPattern[y]))) == 0)
238-
{
239-
matched = false;
240-
break;
241-
}
242-
}
243-
if (matched)
244-
{
245-
found = i;
246-
break;
247-
}
248-
}
249-
else
250-
{
251-
found = i;
252-
break;
253-
}
254-
}
255-
}
256-
else if (buffer[i] == searchPattern[0])
196+
if (AreBytesIdentical(buffer[i], searchPattern[0]))
257197
{
258198
if (buffer.Length > 1)
259199
{
260200
bool matched = true;
261201
for (int y = 1; y <= searchPattern.Length - 1; y++)
262202
{
263-
if (buffer[i + y] != searchPattern[y])
203+
if (!AreBytesIdentical(buffer[i + y], searchPattern[y]))
264204
{
265205
matched = false;
266206
break;
@@ -326,11 +266,7 @@ public static int FindBytePattern(ReadOnlySpan<byte> buffer, ReadOnlySpan<byte>
326266
for (int i = offset; i < buffer.Length - searchPattern.Length + 1; i++)
327267
{
328268
#if NETCOREAPP3_0_OR_GREATER
329-
if (Avx2.IsSupported && Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, buffer[i]), Vector256<byte>.Zero.WithElement(0, searchPattern[0]))) != 0 && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
330-
return i;
331-
else if (Sse2.IsSupported && Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, buffer[i]), Vector128<byte>.Zero.WithElement(0, searchPattern[0]))) != 0 && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
332-
return i;
333-
else if (buffer[i] == searchPattern[0] && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
269+
if (AreBytesIdentical(buffer[i], searchPattern[0]) && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
334270
return i;
335271
#else
336272
if (buffer[i] == searchPattern[0] && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
@@ -349,6 +285,18 @@ public static bool AreIntegersIdentical(int a, int b)
349285
return Avx2.CompareEqual(Vector256.Create(a), Vector256.Create(b)).Equals(Vector256<int>.AllBitsSet);
350286
else if (Sse2.IsSupported)
351287
return Sse2.CompareEqual(Vector128.Create(a), Vector128.Create(b)).Equals(Vector128<int>.AllBitsSet);
288+
#endif
289+
return a == b;
290+
}
291+
292+
public static bool AreBytesIdentical(byte a, byte b)
293+
{
294+
#if NETCOREAPP3_0_OR_GREATER
295+
// With SIMD, Check if the comparison results are all 1's (indicating equality)
296+
if (Avx2.IsSupported)
297+
return Avx2.CompareEqual(Vector256.Create(a), Vector256.Create(b)).Equals(Vector256<byte>.AllBitsSet);
298+
else if (Sse2.IsSupported)
299+
return Sse2.CompareEqual(Vector128.Create(a), Vector128.Create(b)).Equals(Vector128<byte>.AllBitsSet);
352300
#endif
353301
return a == b;
354302
}

BackendServices/CyberBackendLibrary/GeoLocalization/GeoIP.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,31 @@ public static void Initialize()
5757
{
5858
DatabaseReader? reader;
5959

60-
if (File.Exists($"{currentdir}/static/GeoIP2-Country.mmdb"))
60+
try
6161
{
62-
reader = new DatabaseReader($"{currentdir}/static/GeoIP2-Country.mmdb");
63-
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoIP2-Country.mmdb Database...");
62+
if (File.Exists($"{currentdir}/static/GeoIP2-Country.mmdb"))
63+
{
64+
reader = new DatabaseReader($"{currentdir}/static/GeoIP2-Country.mmdb");
65+
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoIP2-Country.mmdb Database...");
66+
}
67+
else if (File.Exists($"{currentdir}/static/GeoLite2-Country.mmdb"))
68+
{
69+
reader = new DatabaseReader($"{currentdir}/static/GeoLite2-Country.mmdb");
70+
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoLite2-Country.mmdb Database...");
71+
}
72+
else
73+
reader = null;
74+
75+
_instance = new GeoIP(reader);
6476
}
65-
else if (File.Exists($"{currentdir}/static/GeoLite2-Country.mmdb"))
77+
catch (IOException)
6678
{
67-
reader = new DatabaseReader($"{currentdir}/static/GeoLite2-Country.mmdb");
68-
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoLite2-Country.mmdb Database...");
79+
Initialize(); // Try again...
80+
}
81+
catch (Exception e)
82+
{
83+
CustomLogger.LoggerAccessor.LogError($"[GeoIP] - Initialize() - Failed to initialize GeoIP engine (exception: {e})");
6984
}
70-
else
71-
reader = null;
72-
73-
_instance = new GeoIP(reader);
7485
}
7586

7687
public static string? GetGeoCodeFromIP(IPAddress IPAddr)

SpecializedServers/Horizon/DME/Models/World.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ public void OnEndGameRequest(MediusServerEndGameRequest request)
293293

294294
public Task OnPlayerJoined(ClientObject player)
295295
{
296+
if (player.RemoteUdpEndpoint == null)
297+
{
298+
LoggerAccessor.LogError($"[World] - OnPlayerJoined - player {player.IP} on ApplicationId {player.ApplicationId} has no UdpEndpoint!");
299+
return Task.CompletedTask;
300+
}
301+
296302
lock (_Lock)
297303
{
298304
player.HasJoined = true;
@@ -314,28 +320,21 @@ public Task OnPlayerJoined(ClientObject player)
314320
{
315321
PlayerIndex = (short)player.DmeId,
316322
ScertId = (short)player.ScertId,
317-
IP = player.RemoteUdpEndpoint?.Address ?? MediusClass.SERVER_IP
323+
IP = player.RemoteUdpEndpoint.Address
318324
});
319325
}
320326

321327
_ = Task.Run(() => {
322328
foreach (ushort token in clientTokens.Keys)
323329
{
324-
try
330+
if (clientTokens.TryGetValue(token, out List<int>? value) && value.Count > 0)
325331
{
326-
if (clientTokens.TryGetValue(token, out List<int>? value) && value.Count > 0)
332+
player.EnqueueTcp(new RT_MSG_SERVER_TOKEN_MESSAGE() // We need to actualize client with every owned tokens.
327333
{
328-
player.EnqueueTcp(new RT_MSG_SERVER_TOKEN_MESSAGE() // We need to actualize client with every owned tokens.
329-
{
330-
tokenMsgType = RT_TOKEN_MESSAGE_TYPE.RT_TOKEN_SERVER_OWNED,
331-
TokenID = token,
332-
TokenHost = (ushort)value[0],
333-
});
334-
}
335-
}
336-
catch
337-
{
338-
334+
tokenMsgType = RT_TOKEN_MESSAGE_TYPE.RT_TOKEN_SERVER_OWNED,
335+
TokenID = token,
336+
TokenHost = (ushort)value[0],
337+
});
339338
}
340339
}
341340
});
@@ -354,6 +353,12 @@ public Task OnPlayerJoined(ClientObject player)
354353

355354
public async Task OnPlayerLeft(ClientObject player)
356355
{
356+
if (player.RemoteUdpEndpoint == null)
357+
{
358+
LoggerAccessor.LogError($"[World] - OnPlayerLeft - player {player.IP} on ApplicationId {player.ApplicationId} has no UdpEndpoint!");
359+
return;
360+
}
361+
357362
player.HasJoined = false;
358363

359364
// Plugin
@@ -383,7 +388,7 @@ public async Task OnPlayerLeft(ClientObject player)
383388
{
384389
PlayerIndex = (short)player.DmeId,
385390
ScertId = (short)player.ScertId,
386-
IP = player.RemoteUdpEndpoint?.Address ?? MediusClass.SERVER_IP
391+
IP = player.RemoteUdpEndpoint.Address
387392
});
388393
}
389394

SpecializedServers/Horizon/MEDIUS/Medius/MLS.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5505,7 +5505,7 @@ await HorizonServerConfiguration.Database.ClanTransferLeadership(data.ClientObje
55055505
{
55065506
gameList[gameList.Length - 1].EndOfList = true;
55075507

5508-
Thread.Sleep(new Random().Next(0, 3001)); // We simulate medius fetching delay between 0 and 3 seconds.
5508+
Thread.Sleep(new Random().Next(0, 6001)); // We simulate medius fetching delay between 0 and 6 seconds.
55095509
// Some games expect a delayed response and it's never the same for every clients.
55105510

55115511
// Add to responses

SpecializedServers/SSFWServer/FileHelper/FileHelper.cs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,15 @@ public class FileHelper
1818
if (src.Length > 4 && src[0] == 'T' && src[1] == 'L' && src[2] == 'Z' && src[3] == 'C')
1919
{
2020
byte[]? DecompressedData = EdgeLZMA.Decompress(src, false);
21-
if (!string.IsNullOrEmpty(key) && DecompressedData != null && DecompressedData.Length > 9 && DataTypesUtils.FindBytePattern(DecompressedData, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
22-
{
23-
byte[] dst = new byte[DecompressedData.Length - 9];
24-
Array.Copy(DecompressedData, 9, dst, 0, dst.Length);
25-
return FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key));
26-
}
21+
if (!string.IsNullOrEmpty(key) && DecompressedData != null)
22+
return DecryptStaticData(DecompressedData, key);
2723
else
2824
return DecompressedData;
2925
}
26+
else if (!string.IsNullOrEmpty(key))
27+
return DecryptStaticData(src, key);
3028
else
31-
{
32-
if (!string.IsNullOrEmpty(key) && src.Length > 9 && DataTypesUtils.FindBytePattern(src, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
33-
{
34-
byte[] dst = new byte[src.Length - 9];
35-
Array.Copy(src, 9, dst, 0, dst.Length);
36-
return FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key));
37-
}
38-
else
39-
return src;
40-
}
29+
return src;
4130
}
4231
catch (Exception ex)
4332
{
@@ -52,39 +41,24 @@ public class FileHelper
5241
if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath))
5342
return null;
5443

55-
try
56-
{
57-
byte[] src = File.ReadAllBytes(filepath);
58-
if (src.Length > 4 && src[0] == 'T' && src[1] == 'L' && src[2] == 'Z' && src[3] == 'C')
59-
{
60-
byte[]? DecompressedData = EdgeLZMA.Decompress(src, false);
61-
if (!string.IsNullOrEmpty(key) && DecompressedData != null && DecompressedData.Length > 9 && DataTypesUtils.FindBytePattern(DecompressedData, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
62-
{
63-
byte[] dst = new byte[DecompressedData.Length - 9];
64-
Array.Copy(DecompressedData, 9, dst, 0, dst.Length);
65-
return Encoding.UTF8.GetString(FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key)));
66-
}
67-
else if (DecompressedData != null)
68-
return Encoding.UTF8.GetString(DecompressedData);
69-
}
70-
else
71-
{
72-
if (!string.IsNullOrEmpty(key) && src.Length > 9 && DataTypesUtils.FindBytePattern(src, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
73-
{
74-
byte[] dst = new byte[src.Length - 9];
75-
Array.Copy(src, 9, dst, 0, dst.Length);
76-
return Encoding.UTF8.GetString(FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key)));
77-
}
78-
else
79-
return Encoding.UTF8.GetString(src);
80-
}
81-
}
82-
catch (Exception ex)
44+
byte[]? Data = ReadAllBytes(filepath, key);
45+
46+
if (Data == null)
47+
return null;
48+
else
49+
return Encoding.UTF8.GetString(Data);
50+
}
51+
52+
private static byte[] DecryptStaticData(byte[] src, string key)
53+
{
54+
if (src.Length > 9 && DataTypesUtils.FindBytePattern(src, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
8355
{
84-
LoggerAccessor.LogError($"[FileHelper] - ReadAllText errored out with this exception : {ex}");
56+
byte[] dst = new byte[src.Length - 9];
57+
Array.Copy(src, 9, dst, 0, dst.Length);
58+
return FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key));
8559
}
8660

87-
return null;
61+
return src;
8862
}
8963
}
9064
}

0 commit comments

Comments
 (0)