Skip to content

Commit 3de551c

Browse files
committed
Refactored extensions.
Added tests.
1 parent 8637de5 commit 3de551c

12 files changed

Lines changed: 195 additions & 102 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Diagnostics.Contracts;
4+
5+
namespace ReClassNET.Extensions
6+
{
7+
public static class ByteExtension
8+
{
9+
/// <summary>
10+
/// Sets every element in the array to zero.
11+
/// </summary>
12+
/// <param name="array"></param>
13+
[DebuggerStepThrough]
14+
public static void FillWithZero(this byte[] array)
15+
{
16+
Contract.Requires(array != null);
17+
18+
Array.Clear(array, 0, array.Length);
19+
}
20+
}
21+
}

ReClass.NET/Extensions/ColorExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
using System.Drawing;
1+
using System.Diagnostics;
2+
using System.Diagnostics.Contracts;
3+
using System.Drawing;
24

35
namespace ReClassNET.Extensions
46
{
57
public static class ExtensionColor
68
{
9+
[Pure]
10+
[DebuggerStepThrough]
11+
public static int ToRgb(this Color color)
12+
{
13+
return 0xFFFFFF & color.ToArgb();
14+
}
15+
16+
[Pure]
17+
[DebuggerStepThrough]
718
public static Color Invert(this Color color)
819
{
920
return Color.FromArgb(color.A, 255 - color.R, 255 - color.G, 255 - color.B);

ReClass.NET/Extensions/Extensions.cs

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Diagnostics.Contracts;
5+
6+
namespace ReClassNET.Extensions
7+
{
8+
public static class ListExtension
9+
{
10+
/// <summary>
11+
/// Searches a range of elements in the sorted list for an element using the specified comparer and returns the zero-based index of the element.
12+
/// </summary>
13+
/// <typeparam name="T"></typeparam>
14+
/// <param name="source"></param>
15+
/// <param name="comparer">The comparer to use</param>
16+
/// <returns>The zero-based index in the sorted list, if an item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger or, if there is no larger element, the bitwise complement of <see cref="IList{T}.Count"/>.</returns>
17+
[Pure]
18+
[DebuggerStepThrough]
19+
public static int BinarySearch<T>(this IList<T> source, Func<T, int> comparer)
20+
{
21+
Contract.Requires(source != null);
22+
Contract.Requires(comparer != null);
23+
24+
var lo = 0;
25+
var hi = source.Count - 1;
26+
27+
while (lo <= hi)
28+
{
29+
var i = lo + (hi - lo >> 1);
30+
31+
var order = comparer(source[i]);
32+
if (order == 0)
33+
{
34+
return i;
35+
}
36+
if (order > 0)
37+
{
38+
lo = i + 1;
39+
}
40+
else
41+
{
42+
hi = i - 1;
43+
}
44+
}
45+
46+
return ~lo;
47+
}
48+
}
49+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Diagnostics;
2+
using System.Diagnostics.Contracts;
3+
using System.Drawing;
4+
5+
namespace ReClassNET.Extensions
6+
{
7+
public static class PointExtension
8+
{
9+
/// <summary>
10+
/// Creates a new point which is relocated with the given offsets.
11+
/// </summary>
12+
/// <param name="point"></param>
13+
/// <param name="offsetX">The offset in x direction.</param>
14+
/// <param name="offsetY">The offset in y direction.</param>
15+
/// <returns>The relocated point.</returns>
16+
[Pure]
17+
[DebuggerStepThrough]
18+
public static Point Relocate(this Point point, int offsetX, int offsetY)
19+
{
20+
return new Point(point.X + offsetX, point.Y + offsetY);
21+
}
22+
}
23+
}

ReClass.NET/Memory/RemoteProcess.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,17 @@ public Section GetSectionToPointer(IntPtr address)
514514
{
515515
lock (sections)
516516
{
517-
return sections.BinaryFind(s => address.CompareToRange(s.Start, s.End));
517+
var index = sections.BinarySearch(s => address.CompareToRange(s.Start, s.End));
518+
return index < 0 ? null : sections[index];
518519
}
519520
}
520521

521522
public Module GetModuleToPointer(IntPtr address)
522523
{
523524
lock (modules)
524525
{
525-
return modules.BinaryFind(m => address.CompareToRange(m.Start, m.End));
526+
var index = modules.BinarySearch(m => address.CompareToRange(m.Start, m.End));
527+
return index < 0 ? null : modules[index];
526528
}
527529
}
528530

ReClass.NET/ReClass.NET.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
<Compile Include="Debugger\RemoteDebugger.Extensions.cs" />
172172
<Compile Include="Debugger\RemoteDebugger.Thread.cs" />
173173
<Compile Include="Debugger\SoftwareBreakpoint.cs" />
174+
<Compile Include="Extensions\ByteExtension.cs" />
175+
<Compile Include="Extensions\ListExtension.cs" />
176+
<Compile Include="Extensions\PointExtension.cs" />
174177
<Compile Include="Extensions\StringBuilderExtensions.cs" />
175178
<Compile Include="Extensions\XAttributeExtensions.cs" />
176179
<Compile Include="Forms\EnumEditorForm.cs">
@@ -408,7 +411,6 @@
408411
<DependentUpon>SettingsForm.cs</DependentUpon>
409412
</Compile>
410413
<Compile Include="UI\HotSpot.cs" />
411-
<Compile Include="Extensions\Extensions.cs" />
412414
<Compile Include="UI\FontEx.cs" />
413415
<Compile Include="UI\HotSpotTextBox.cs">
414416
<SubType>Component</SubType>

ReClass.NET/UI/MemoryViewControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ protected override void OnMouseHover(EventArgs e)
457457
if (selectedNodes.Count > 1)
458458
{
459459
var memorySize = selectedNodes.Sum(h => h.Node.MemorySize);
460-
nodeInfoToolTip.Show($"{selectedNodes.Count} Nodes selected, {memorySize} bytes", this, toolTipPosition.OffsetEx(16, 16));
460+
nodeInfoToolTip.Show($"{selectedNodes.Count} Nodes selected, {memorySize} bytes", this, toolTipPosition.Relocate(16, 16));
461461
}
462462
else
463463
{
@@ -469,14 +469,14 @@ protected override void OnMouseHover(EventArgs e)
469469
{
470470
memoryPreviewPopUp.InitializeMemory(spot.Memory.Process, previewAddress);
471471

472-
memoryPreviewPopUp.Show(this, toolTipPosition.OffsetEx(16, 16));
472+
memoryPreviewPopUp.Show(this, toolTipPosition.Relocate(16, 16));
473473
}
474474
else
475475
{
476476
var text = spot.Node.GetToolTipText(spot, spot.Memory);
477477
if (!string.IsNullOrEmpty(text))
478478
{
479-
nodeInfoToolTip.Show(text, this, toolTipPosition.OffsetEx(16, 16));
479+
nodeInfoToolTip.Show(text, this, toolTipPosition.Relocate(16, 16));
480480
}
481481
}
482482

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Linq;
2+
using NFluent;
3+
using ReClassNET.Extensions;
4+
using Xunit;
5+
6+
namespace ReClass.NET_Tests.Extensions
7+
{
8+
public class ByteExtensionTest
9+
{
10+
public static TheoryData<byte[]> GetTestFillWithZeroData() => new TheoryData<byte[]>
11+
{
12+
new byte[0],
13+
Enumerable.Repeat(1, 1).Select(i => (byte)i).ToArray(),
14+
Enumerable.Repeat(1, 10).Select(i => (byte)i).ToArray(),
15+
Enumerable.Repeat(1, 100).Select(i => (byte)i).ToArray(),
16+
Enumerable.Repeat(1, 1000).Select(i => (byte)i).ToArray()
17+
};
18+
19+
[Theory]
20+
[MemberData(nameof(GetTestFillWithZeroData))]
21+
public void TestFillWithZero(byte[] sut)
22+
{
23+
sut.FillWithZero();
24+
25+
Check.That(sut.All(b => b == 0)).IsTrue();
26+
}
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NFluent;
7+
using ReClassNET.Extensions;
8+
using Xunit;
9+
10+
namespace ReClass.NET_Tests.Extensions
11+
{
12+
public class ListExtensionTest
13+
{
14+
public static TheoryData<IList<int>, Func<int, int>, int> GetTestBinarySearchData() => new TheoryData<IList<int>, Func<int, int>, int>
15+
{
16+
17+
};
18+
19+
public void TestBinarySearch(IList<int> sut, Func<int, int> comparer, int expected)
20+
{
21+
Check.That(sut.BinarySearch(comparer)).IsEqualTo(expected);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)