Skip to content

Commit 7da8498

Browse files
committed
Feature: Dereference to Pointer
1 parent c17ced2 commit 7da8498

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

Source/Reloaded.Memory.Tests/Memory/Pointers/RefPointer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ public unsafe void TryDereferenceAlternateSyntax()
2727
}
2828
}
2929

30+
[Fact]
31+
public unsafe void TryDereferenceRawPointer()
32+
{
33+
int number = 1;
34+
int* numberPtr = &number;
35+
int** numberPtrPtr = &numberPtr;
36+
37+
var refPointer = new Reloaded.Memory.Pointers.RefPointer<int>((int*)numberPtrPtr, 2);
38+
for (int x = 0; x < 100; x++)
39+
{
40+
bool success = refPointer.TryDereference(out int* value);
41+
*value = x;
42+
Assert.True(success);
43+
Assert.Equal(number, *value);
44+
}
45+
}
46+
3047
[Fact]
3148
public unsafe void ReadMultiLevelPointer()
3249
{

Source/Reloaded.Memory/Memory/Pointers/RefPointer.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ public bool TryDereference(ref TStruct value)
5252
return true;
5353
}
5454

55+
/// <summary>
56+
/// Attempts to dereference the pointer, returning the innermost pointer as a pointer.
57+
/// If at any point along the way, the pointed to address is null, the method fails.
58+
/// </summary>
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
public bool TryDereference(out TStruct* value)
61+
{
62+
TStruct* currentAddress = Address;
63+
value = (TStruct*) 0;
64+
65+
if (currentAddress == (TStruct*)0)
66+
return false;
67+
68+
for (int x = 0; x < DepthLevel - 1; x++)
69+
{
70+
currentAddress = *(TStruct**)(currentAddress);
71+
if (currentAddress == (TStruct*)0)
72+
return false;
73+
}
74+
75+
value = currentAddress;
76+
return true;
77+
}
78+
5579
/// <summary>
5680
/// Attempts to dereference the pointer, returning the innermost pointer level as a ref type.
5781
/// If at any point along the way, the pointed to address is null, the method fails.

Source/Reloaded.Memory/Reloaded.Memory.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<RepositoryUrl></RepositoryUrl>
1717
<RepositoryType></RepositoryType>
1818
<Description>A fully featured C# memory editing library supporting reading/writing primitive or generic struct types in either current or an external process.</Description>
19-
<Version>2.2.0</Version>
19+
<Version>2.3.0</Version>
2020
<Authors>Sewer56</Authors>
2121
<Company />
2222
<Product>Project Reloaded</Product>

0 commit comments

Comments
 (0)