Skip to content

Commit

Permalink
Fixes SIMD comparing operations + GeoIP initialization bug fix.
Browse files Browse the repository at this point in the history
Fixes SIMD comparing operations + GeoIP initialization bug fix.
  • Loading branch information
GitHubProUser67 committed Jun 16, 2024
1 parent 4230e7f commit b907131
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 140 deletions.
84 changes: 16 additions & 68 deletions BackendServices/CyberBackendLibrary/DataTypes/DataTypesUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ public static bool AreArraysIdentical(byte[] arr1, byte[] arr2)
for (int i = 0; i < arr1.Length; i++)
{
#if NETCOREAPP3_0_OR_GREATER
if (Avx2.IsSupported && Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, arr1[i]), Vector256<byte>.Zero.WithElement(0, arr2[i]))) == 0)
return false;
else if (Sse2.IsSupported && Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, arr1[i]), Vector128<byte>.Zero.WithElement(0, arr2[i]))) == 0)
return false;
else if (arr1[i] != arr2[i])
if (!AreBytesIdentical(arr1[i], arr2[i]))
return false;
#else
if (arr1[i] != arr2[i])
Expand Down Expand Up @@ -197,70 +193,14 @@ public static int FindBytePattern(byte[] buffer, byte[] searchPattern, int offse
for (int i = offset; i <= buffer.Length - searchPattern.Length; i++)
{
#if NETCOREAPP3_0_OR_GREATER
if (Avx2.IsSupported)
{
if (Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, buffer[i]), Vector256<byte>.Zero.WithElement(0, searchPattern[0]))) != 0)
{
if (buffer.Length > 1)
{
bool matched = true;
for (int y = 1; y <= searchPattern.Length - 1; y++)
{
if (Avx2.MoveMask(Avx2.CompareEqual(Vector256<byte>.Zero.WithElement(0, buffer[i + y]), Vector256<byte>.Zero.WithElement(0, searchPattern[y]))) == 0)
{
matched = false;
break;
}
}
if (matched)
{
found = i;
break;
}
}
else
{
found = i;
break;
}
}
}
else if (Sse2.IsSupported)
{
if (Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, buffer[i]), Vector128<byte>.Zero.WithElement(0, searchPattern[0]))) != 0)
{
if (buffer.Length > 1)
{
bool matched = true;
for (int y = 1; y <= searchPattern.Length - 1; y++)
{
if (Sse2.MoveMask(Sse2.CompareEqual(Vector128<byte>.Zero.WithElement(0, buffer[i + y]), Vector128<byte>.Zero.WithElement(0, searchPattern[y]))) == 0)
{
matched = false;
break;
}
}
if (matched)
{
found = i;
break;
}
}
else
{
found = i;
break;
}
}
}
else if (buffer[i] == searchPattern[0])
if (AreBytesIdentical(buffer[i], searchPattern[0]))
{
if (buffer.Length > 1)
{
bool matched = true;
for (int y = 1; y <= searchPattern.Length - 1; y++)
{
if (buffer[i + y] != searchPattern[y])
if (!AreBytesIdentical(buffer[i + y], searchPattern[y]))
{
matched = false;
break;
Expand Down Expand Up @@ -326,11 +266,7 @@ public static int FindBytePattern(ReadOnlySpan<byte> buffer, ReadOnlySpan<byte>
for (int i = offset; i < buffer.Length - searchPattern.Length + 1; i++)
{
#if NETCOREAPP3_0_OR_GREATER
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))
return i;
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))
return i;
else if (buffer[i] == searchPattern[0] && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
if (AreBytesIdentical(buffer[i], searchPattern[0]) && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
return i;
#else
if (buffer[i] == searchPattern[0] && buffer.Slice(i, searchPattern.Length).SequenceEqual(searchPattern))
Expand All @@ -349,6 +285,18 @@ public static bool AreIntegersIdentical(int a, int b)
return Avx2.CompareEqual(Vector256.Create(a), Vector256.Create(b)).Equals(Vector256<int>.AllBitsSet);
else if (Sse2.IsSupported)
return Sse2.CompareEqual(Vector128.Create(a), Vector128.Create(b)).Equals(Vector128<int>.AllBitsSet);
#endif
return a == b;
}

public static bool AreBytesIdentical(byte a, byte b)
{
#if NETCOREAPP3_0_OR_GREATER
// With SIMD, Check if the comparison results are all 1's (indicating equality)
if (Avx2.IsSupported)
return Avx2.CompareEqual(Vector256.Create(a), Vector256.Create(b)).Equals(Vector256<byte>.AllBitsSet);
else if (Sse2.IsSupported)
return Sse2.CompareEqual(Vector128.Create(a), Vector128.Create(b)).Equals(Vector128<byte>.AllBitsSet);
#endif
return a == b;
}
Expand Down
31 changes: 21 additions & 10 deletions BackendServices/CyberBackendLibrary/GeoLocalization/GeoIP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,31 @@ public static void Initialize()
{
DatabaseReader? reader;

if (File.Exists($"{currentdir}/static/GeoIP2-Country.mmdb"))
try
{
reader = new DatabaseReader($"{currentdir}/static/GeoIP2-Country.mmdb");
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoIP2-Country.mmdb Database...");
if (File.Exists($"{currentdir}/static/GeoIP2-Country.mmdb"))
{
reader = new DatabaseReader($"{currentdir}/static/GeoIP2-Country.mmdb");
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoIP2-Country.mmdb Database...");
}
else if (File.Exists($"{currentdir}/static/GeoLite2-Country.mmdb"))
{
reader = new DatabaseReader($"{currentdir}/static/GeoLite2-Country.mmdb");
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoLite2-Country.mmdb Database...");
}
else
reader = null;

_instance = new GeoIP(reader);
}
else if (File.Exists($"{currentdir}/static/GeoLite2-Country.mmdb"))
catch (IOException)
{
reader = new DatabaseReader($"{currentdir}/static/GeoLite2-Country.mmdb");
System.Diagnostics.Debug.WriteLine("[GeoIPUtils] - Loaded GeoLite2-Country.mmdb Database...");
Initialize(); // Try again...
}
catch (Exception e)
{
CustomLogger.LoggerAccessor.LogError($"[GeoIP] - Initialize() - Failed to initialize GeoIP engine (exception: {e})");
}
else
reader = null;

_instance = new GeoIP(reader);
}

public static string? GetGeoCodeFromIP(IPAddress IPAddr)
Expand Down
35 changes: 20 additions & 15 deletions SpecializedServers/Horizon/DME/Models/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public void OnEndGameRequest(MediusServerEndGameRequest request)

public Task OnPlayerJoined(ClientObject player)
{
if (player.RemoteUdpEndpoint == null)
{
LoggerAccessor.LogError($"[World] - OnPlayerJoined - player {player.IP} on ApplicationId {player.ApplicationId} has no UdpEndpoint!");
return Task.CompletedTask;
}

lock (_Lock)
{
player.HasJoined = true;
Expand All @@ -314,28 +320,21 @@ public Task OnPlayerJoined(ClientObject player)
{
PlayerIndex = (short)player.DmeId,
ScertId = (short)player.ScertId,
IP = player.RemoteUdpEndpoint?.Address ?? MediusClass.SERVER_IP
IP = player.RemoteUdpEndpoint.Address
});
}

_ = Task.Run(() => {
foreach (ushort token in clientTokens.Keys)
{
try
if (clientTokens.TryGetValue(token, out List<int>? value) && value.Count > 0)
{
if (clientTokens.TryGetValue(token, out List<int>? value) && value.Count > 0)
player.EnqueueTcp(new RT_MSG_SERVER_TOKEN_MESSAGE() // We need to actualize client with every owned tokens.
{
player.EnqueueTcp(new RT_MSG_SERVER_TOKEN_MESSAGE() // We need to actualize client with every owned tokens.
{
tokenMsgType = RT_TOKEN_MESSAGE_TYPE.RT_TOKEN_SERVER_OWNED,
TokenID = token,
TokenHost = (ushort)value[0],
});
}
}
catch
{

tokenMsgType = RT_TOKEN_MESSAGE_TYPE.RT_TOKEN_SERVER_OWNED,
TokenID = token,
TokenHost = (ushort)value[0],
});
}
}
});
Expand All @@ -354,6 +353,12 @@ public Task OnPlayerJoined(ClientObject player)

public async Task OnPlayerLeft(ClientObject player)
{
if (player.RemoteUdpEndpoint == null)
{
LoggerAccessor.LogError($"[World] - OnPlayerLeft - player {player.IP} on ApplicationId {player.ApplicationId} has no UdpEndpoint!");
return;
}

player.HasJoined = false;

// Plugin
Expand Down Expand Up @@ -383,7 +388,7 @@ public async Task OnPlayerLeft(ClientObject player)
{
PlayerIndex = (short)player.DmeId,
ScertId = (short)player.ScertId,
IP = player.RemoteUdpEndpoint?.Address ?? MediusClass.SERVER_IP
IP = player.RemoteUdpEndpoint.Address
});
}

Expand Down
2 changes: 1 addition & 1 deletion SpecializedServers/Horizon/MEDIUS/Medius/MLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5505,7 +5505,7 @@ await HorizonServerConfiguration.Database.ClanTransferLeadership(data.ClientObje
{
gameList[gameList.Length - 1].EndOfList = true;

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

// Add to responses
Expand Down
66 changes: 20 additions & 46 deletions SpecializedServers/SSFWServer/FileHelper/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,15 @@ public class FileHelper
if (src.Length > 4 && src[0] == 'T' && src[1] == 'L' && src[2] == 'Z' && src[3] == 'C')
{
byte[]? DecompressedData = EdgeLZMA.Decompress(src, false);
if (!string.IsNullOrEmpty(key) && DecompressedData != null && DecompressedData.Length > 9 && DataTypesUtils.FindBytePattern(DecompressedData, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
{
byte[] dst = new byte[DecompressedData.Length - 9];
Array.Copy(DecompressedData, 9, dst, 0, dst.Length);
return FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key));
}
if (!string.IsNullOrEmpty(key) && DecompressedData != null)
return DecryptStaticData(DecompressedData, key);
else
return DecompressedData;
}
else if (!string.IsNullOrEmpty(key))
return DecryptStaticData(src, key);
else
{
if (!string.IsNullOrEmpty(key) && src.Length > 9 && DataTypesUtils.FindBytePattern(src, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
{
byte[] dst = new byte[src.Length - 9];
Array.Copy(src, 9, dst, 0, dst.Length);
return FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key));
}
else
return src;
}
return src;
}
catch (Exception ex)
{
Expand All @@ -52,39 +41,24 @@ public class FileHelper
if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath))
return null;

try
{
byte[] src = File.ReadAllBytes(filepath);
if (src.Length > 4 && src[0] == 'T' && src[1] == 'L' && src[2] == 'Z' && src[3] == 'C')
{
byte[]? DecompressedData = EdgeLZMA.Decompress(src, false);
if (!string.IsNullOrEmpty(key) && DecompressedData != null && DecompressedData.Length > 9 && DataTypesUtils.FindBytePattern(DecompressedData, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
{
byte[] dst = new byte[DecompressedData.Length - 9];
Array.Copy(DecompressedData, 9, dst, 0, dst.Length);
return Encoding.UTF8.GetString(FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key)));
}
else if (DecompressedData != null)
return Encoding.UTF8.GetString(DecompressedData);
}
else
{
if (!string.IsNullOrEmpty(key) && src.Length > 9 && DataTypesUtils.FindBytePattern(src, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
{
byte[] dst = new byte[src.Length - 9];
Array.Copy(src, 9, dst, 0, dst.Length);
return Encoding.UTF8.GetString(FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key)));
}
else
return Encoding.UTF8.GetString(src);
}
}
catch (Exception ex)
byte[]? Data = ReadAllBytes(filepath, key);

if (Data == null)
return null;
else
return Encoding.UTF8.GetString(Data);
}

private static byte[] DecryptStaticData(byte[] src, string key)
{
if (src.Length > 9 && DataTypesUtils.FindBytePattern(src, new byte[] { 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x64, 0x65, 0x73 }) != -1)
{
LoggerAccessor.LogError($"[FileHelper] - ReadAllText errored out with this exception : {ex}");
byte[] dst = new byte[src.Length - 9];
Array.Copy(src, 9, dst, 0, dst.Length);
return FileHelperCryptoClass.DecryptData(dst, FileHelperCryptoClass.GetEncryptionKey(key));
}

return null;
return src;
}
}
}

0 comments on commit b907131

Please sign in to comment.