fix(core): Fixes IPMatch and some cleanup (#443)

- [X] Fixes a bug where IPMatch was returning false on some IP ranges
- [X] Cleans up shrink table load.
This commit is contained in:
Kamron Batman 2021-02-03 14:31:02 -08:00 committed by GitHub
parent cb22e662b4
commit 24ebdd0f90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 30 deletions

View file

@ -62,7 +62,7 @@ namespace Server.Tests.Network
var strLength = fixedLength > -1 ? Math.Min(value.Length, fixedLength) : value.Length;
var chars = value.AsSpan(0, strLength);
;
encoding.GetBytes(chars, buffer.Slice(offset));
var reader = new CircularBufferReader(buffer.SliceToLength(firstSize), buffer.Slice(firstSize));

View file

@ -53,6 +53,7 @@ namespace Server.Tests
[InlineData("::1024:*-:1234", "::1024:8A13:1234", false, false)]
[InlineData("::1024:?1:1234", "::1024:8A13:1234", false, false)]
[InlineData("::1024:1_2:1234", "::1024:8A13:1234", false, false)]
[InlineData("172.16-31.*", "172.16.17.2", true, true)]
public void TestIPMatch(string val, string addr, bool shouldMatch, bool shouldBeValid)
{
var address = IPAddress.Parse(addr);

View file

@ -21,7 +21,7 @@ namespace Server.Network
{
public sealed class ServerInfo
{
private IPEndPoint m_Address;
private readonly IPEndPoint m_Address;
public ServerInfo(string name, int fullPercent, TimeZoneInfo tz, IPEndPoint address)
{
@ -40,7 +40,7 @@ namespace Server.Network
public IPEndPoint Address
{
get => m_Address;
set
init
{
m_Address = value;
Span<byte> integer = stackalloc byte[4];

View file

@ -317,7 +317,7 @@ namespace Server
}
isRange = true;
match = match && num > number;
match = match && num >= number;
number = 0;
break;
}

View file

@ -536,9 +536,7 @@ namespace Server
private static void ProcessDecay()
{
Item item;
while (_decayQueue.TryDequeue(out item))
while (_decayQueue.TryDequeue(out var item))
{
if (item.OnDecay())
{
@ -555,7 +553,7 @@ namespace Server
return;
}
WaitForWriteCompletion(); // Blocks Save until current disk flush is done.s
WaitForWriteCompletion(); // Blocks Save until current disk flush is done.
++_Saves;

View file

@ -72,7 +72,7 @@ namespace Server.Commands
}
#nullable enable
public static void RecurseExport(List<CAGJson> list, CategoryEntry ce, string category)
public static void RecurseExport(List<CAGJson> list, CategoryEntry ce, string? category)
{
category = string.IsNullOrWhiteSpace(category) ? ce.Title : $"{category}{ce.Title}";

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace Server
@ -17,37 +18,27 @@ namespace Server
public static int Lookup(int body, int defaultValue)
{
if (m_Table == null)
m_Table ??= Load();
var index = body < m_Table.Length ? m_Table[body] : -1;
if (index < 0)
{
Load();
return defaultValue;
}
var val = 0;
if (body >= 0 && body < m_Table!.Length)
{
val = m_Table[body];
}
if (val == 0)
{
val = defaultValue;
}
return val;
var val = m_Table[body];
return val == 0 ? defaultValue : val;
}
private static void Load()
private static int[] Load()
{
var path = Path.Combine(Core.BaseDirectory, "Data/shrink.cfg");
if (!File.Exists(path))
{
m_Table = Array.Empty<int>();
return;
return Array.Empty<int>();
}
m_Table = new int[1000];
var table = new List<int>();
using var ip = new StreamReader(path);
string line;
@ -70,9 +61,9 @@ namespace Server
var body = Utility.ToInt32(split[0]);
var item = Utility.ToInt32(split[1]);
if (body >= 0 && body < m_Table.Length)
if (body >= 0)
{
m_Table[body] = item;
table[body] = item;
}
}
}
@ -81,6 +72,8 @@ namespace Server
// ignored
}
}
return table.ToArray();
}
}
}