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 strLength = fixedLength > -1 ? Math.Min(value.Length, fixedLength) : value.Length;
var chars = value.AsSpan(0, strLength); var chars = value.AsSpan(0, strLength);
;
encoding.GetBytes(chars, buffer.Slice(offset)); encoding.GetBytes(chars, buffer.Slice(offset));
var reader = new CircularBufferReader(buffer.SliceToLength(firstSize), buffer.Slice(firstSize)); 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:*-:1234", "::1024:8A13:1234", false, false)]
[InlineData("::1024:?1: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("::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) public void TestIPMatch(string val, string addr, bool shouldMatch, bool shouldBeValid)
{ {
var address = IPAddress.Parse(addr); var address = IPAddress.Parse(addr);

View file

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

View file

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

View file

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

View file

@ -72,7 +72,7 @@ namespace Server.Commands
} }
#nullable enable #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}"; category = string.IsNullOrWhiteSpace(category) ? ce.Title : $"{category}{ce.Title}";

View file

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