From 24ebdd0f905a5ad0daac6ae8659295e68e93abff Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Wed, 3 Feb 2021 14:31:02 -0800 Subject: [PATCH] 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. --- .../Buffers/CircularBufferReaderTests.cs | 2 +- .../Tests/Utility/IPAddressTests.cs | 1 + Projects/Server/Network/ServerInfo.cs | 4 +-- Projects/Server/Utilities/Utility.cs | 2 +- Projects/Server/World/World.cs | 6 ++-- .../Object Creation/Categorization.cs | 2 +- Projects/UOContent/Misc/ShrinkTable.cs | 35 ++++++++----------- 7 files changed, 22 insertions(+), 30 deletions(-) diff --git a/Projects/Server.Tests/Tests/Buffers/CircularBufferReaderTests.cs b/Projects/Server.Tests/Tests/Buffers/CircularBufferReaderTests.cs index 7eee1476d..8ca445382 100644 --- a/Projects/Server.Tests/Tests/Buffers/CircularBufferReaderTests.cs +++ b/Projects/Server.Tests/Tests/Buffers/CircularBufferReaderTests.cs @@ -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)); diff --git a/Projects/Server.Tests/Tests/Utility/IPAddressTests.cs b/Projects/Server.Tests/Tests/Utility/IPAddressTests.cs index 63f9bb1d8..5fa424b71 100644 --- a/Projects/Server.Tests/Tests/Utility/IPAddressTests.cs +++ b/Projects/Server.Tests/Tests/Utility/IPAddressTests.cs @@ -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); diff --git a/Projects/Server/Network/ServerInfo.cs b/Projects/Server/Network/ServerInfo.cs index 53f6e7a2f..d88233a41 100644 --- a/Projects/Server/Network/ServerInfo.cs +++ b/Projects/Server/Network/ServerInfo.cs @@ -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 integer = stackalloc byte[4]; diff --git a/Projects/Server/Utilities/Utility.cs b/Projects/Server/Utilities/Utility.cs index 962e4832b..bc24cc008 100644 --- a/Projects/Server/Utilities/Utility.cs +++ b/Projects/Server/Utilities/Utility.cs @@ -317,7 +317,7 @@ namespace Server } isRange = true; - match = match && num > number; + match = match && num >= number; number = 0; break; } diff --git a/Projects/Server/World/World.cs b/Projects/Server/World/World.cs index e30306bcc..271767964 100644 --- a/Projects/Server/World/World.cs +++ b/Projects/Server/World/World.cs @@ -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; diff --git a/Projects/UOContent/Commands/Object Creation/Categorization.cs b/Projects/UOContent/Commands/Object Creation/Categorization.cs index e916ee39a..abfe50bdb 100644 --- a/Projects/UOContent/Commands/Object Creation/Categorization.cs +++ b/Projects/UOContent/Commands/Object Creation/Categorization.cs @@ -72,7 +72,7 @@ namespace Server.Commands } #nullable enable - public static void RecurseExport(List list, CategoryEntry ce, string category) + public static void RecurseExport(List list, CategoryEntry ce, string? category) { category = string.IsNullOrWhiteSpace(category) ? ce.Title : $"{category}{ce.Title}"; diff --git a/Projects/UOContent/Misc/ShrinkTable.cs b/Projects/UOContent/Misc/ShrinkTable.cs index de397b191..9294b3d29 100644 --- a/Projects/UOContent/Misc/ShrinkTable.cs +++ b/Projects/UOContent/Misc/ShrinkTable.cs @@ -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(); - return; + return Array.Empty(); } - m_Table = new int[1000]; + var table = new List(); 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(); } } }