Skip to content

Commit d941c40

Browse files
Merge pull request #21 from nalka0/temp
Constants.NullPtr => IntPtr.Zero and few extra cleanups
2 parents 22bb57b + 576646e commit d941c40

8 files changed

Lines changed: 26 additions & 58 deletions

File tree

src/HackF5.UnitySpy/AssemblyImageFactory.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static AssemblyImage GetAssemblyImage(UnityProcessFacade process, string
105105
//// pointer to array of structs of type _MonoAssembly
106106
var assemblyArrayAddress = process.ReadPtr(domain + process.MonoLibraryOffsets.ReferencedAssemblies);
107107
for (var assemblyAddress = assemblyArrayAddress;
108-
assemblyAddress != Constants.NullPtr;
108+
assemblyAddress != IntPtr.Zero;
109109
assemblyAddress = process.ReadPtr(assemblyAddress + process.SizeOfPtr))
110110
{
111111
var assembly = process.ReadPtr(assemblyAddress);
@@ -132,10 +132,9 @@ private static IntPtr GetRootDomainFunctionAddressPEFormat(byte[] moduleDump, Mo
132132
var numberOfFunctions = moduleDump.ToInt32(exportDirectory + PEFormatOffsets.NumberOfFunctions);
133133
var functionAddressArrayIndex = moduleDump.ToInt32(exportDirectory + PEFormatOffsets.FunctionAddressArrayIndex);
134134
var functionNameArrayIndex = moduleDump.ToInt32(exportDirectory + PEFormatOffsets.FunctionNameArrayIndex);
135-
136-
var rootDomainFunctionAddress = Constants.NullPtr;
135+
var rootDomainFunctionAddress = IntPtr.Zero;
137136
for (var functionIndex = 0;
138-
functionIndex < (numberOfFunctions * PEFormatOffsets.FunctionEntrySize);
137+
functionIndex < numberOfFunctions * PEFormatOffsets.FunctionEntrySize;
139138
functionIndex += PEFormatOffsets.FunctionEntrySize)
140139
{
141140
var functionNameIndex = moduleDump.ToInt32(functionNameArrayIndex + functionIndex);
@@ -149,7 +148,7 @@ private static IntPtr GetRootDomainFunctionAddressPEFormat(byte[] moduleDump, Mo
149148
}
150149
}
151150

152-
if (rootDomainFunctionAddress == Constants.NullPtr)
151+
if (rootDomainFunctionAddress == IntPtr.Zero)
153152
{
154153
throw new InvalidOperationException("Failed to find mono_get_root_domain function.");
155154
}
@@ -159,7 +158,7 @@ private static IntPtr GetRootDomainFunctionAddressPEFormat(byte[] moduleDump, Mo
159158

160159
private static IntPtr GetRootDomainFunctionAddressMachOFormat(ModuleInfo monoModuleInfo)
161160
{
162-
var rootDomainFunctionAddress = Constants.NullPtr;
161+
var rootDomainFunctionAddress = IntPtr.Zero;
163162

164163
byte[] moduleFromPath = File.ReadAllBytes(monoModuleInfo.Path);
165164

@@ -196,7 +195,7 @@ private static IntPtr GetRootDomainFunctionAddressMachOFormat(ModuleInfo monoMod
196195
}
197196
}
198197

199-
if (rootDomainFunctionAddress == Constants.NullPtr)
198+
if (rootDomainFunctionAddress == IntPtr.Zero)
200199
{
201200
throw new InvalidOperationException("Failed to find mono_get_root_domain function.");
202201
}

src/HackF5.UnitySpy/Detail/AssemblyImage.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using HackF5.UnitySpy.ProcessFacade;
8-
using HackF5.UnitySpy.Util;
98
using JetBrains.Annotations;
109

1110
/// <summary>
@@ -68,7 +67,7 @@ public TypeDefinition GetTypeDefinition(string fullTypeName) =>
6867

6968
public TypeDefinition GetTypeDefinition(IntPtr address)
7069
{
71-
if (address == Constants.NullPtr)
70+
if (address == IntPtr.Zero)
7271
{
7372
return default;
7473
}
@@ -81,7 +80,6 @@ public TypeDefinition GetTypeDefinition(IntPtr address)
8180
private ConcurrentDictionary<IntPtr, TypeDefinition> CreateTypeDefinitions()
8281
{
8382
var definitions = new ConcurrentDictionary<IntPtr, TypeDefinition>();
84-
8583
int classCache = this.Process.MonoLibraryOffsets.ImageClassCache;
8684
var classCacheSize = this.ReadUInt32(classCache + this.Process.MonoLibraryOffsets.HashTableSize);
8785
var classCacheTableArray = this.ReadPtr(classCache + this.Process.MonoLibraryOffsets.HashTableTable);
@@ -91,7 +89,7 @@ private ConcurrentDictionary<IntPtr, TypeDefinition> CreateTypeDefinitions()
9189
tableItem += this.Process.SizeOfPtr)
9290
{
9391
for (var definition = this.Process.ReadPtr(classCacheTableArray + tableItem);
94-
definition != Constants.NullPtr;
92+
definition != IntPtr.Zero;
9593
definition = this.Process.ReadPtr(definition + this.Process.MonoLibraryOffsets.TypeDefinitionNextClassCache))
9694
{
9795
definitions.GetOrAdd(definition, new TypeDefinition(this, definition));

src/HackF5.UnitySpy/Detail/FieldDefinition.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public FieldDefinition([NotNull] TypeDefinition declaringType, IntPtr address)
3737
{
3838
var monoGenericClassAddress = this.TypeInfo.Data;
3939
var monoClassAddress = this.Process.ReadPtr(monoGenericClassAddress);
40-
TypeDefinition monoClass = this.Image.GetTypeDefinition(monoClassAddress);
40+
this.Image.GetTypeDefinition(monoClassAddress);
4141

4242
var monoGenericContainerPtr = monoClassAddress + this.Process.MonoLibraryOffsets.TypeDefinitionGenericContainer;
4343
var monoGenericContainerAddress = this.Process.ReadPtr(monoGenericContainerPtr);
@@ -80,24 +80,8 @@ public TValue GetValue<TValue>(IntPtr address)
8080

8181
public TValue GetValue<TValue>(List<TypeInfo> genericTypeArguments, IntPtr address)
8282
{
83-
int offset;
84-
if (this.DeclaringType.IsValueType && !this.TypeInfo.IsStatic)
85-
{
86-
offset = this.Offset - (this.Process.SizeOfPtr * 2);
87-
}
88-
else
89-
{
90-
offset = this.Offset;
91-
}
92-
93-
if (this.genericTypeArguments != null)
94-
{
95-
return (TValue)this.TypeInfo.GetValue(this.genericTypeArguments, address + offset);
96-
}
97-
else
98-
{
99-
return (TValue)this.TypeInfo.GetValue(genericTypeArguments, address + offset);
100-
}
83+
int offset = this.DeclaringType.IsValueType && !this.TypeInfo.IsStatic ? this.Offset - (this.Process.SizeOfPtr * 2) : this.Offset;
84+
return (TValue)this.TypeInfo.GetValue(this.genericTypeArguments ?? genericTypeArguments, address + offset);
10185
}
10286
}
10387
}

src/HackF5.UnitySpy/Detail/TypeDefinition.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Diagnostics;
88
using System.Linq;
99
using System.Text;
10-
using HackF5.UnitySpy.Util;
1110
using JetBrains.Annotations;
1211

1312
/// <summary>
@@ -58,17 +57,17 @@ public TypeDefinition([NotNull] AssemblyImage image, IntPtr address)
5857
this.NamespaceName = this.ReadString(image.Process.MonoLibraryOffsets.TypeDefinitionNamespace);
5958
this.Size = this.ReadInt32(image.Process.MonoLibraryOffsets.TypeDefinitionSize);
6059
var vtablePtr = this.ReadPtr(image.Process.MonoLibraryOffsets.TypeDefinitionRuntimeInfo);
61-
this.VTable = vtablePtr == Constants.NullPtr ? Constants.NullPtr : image.Process.ReadPtr(vtablePtr + image.Process.MonoLibraryOffsets.TypeDefinitionRuntimeInfoDomainVTables);
60+
this.VTable = vtablePtr == IntPtr.Zero ? IntPtr.Zero : image.Process.ReadPtr(vtablePtr + image.Process.MonoLibraryOffsets.TypeDefinitionRuntimeInfoDomainVTables);
6261
this.TypeInfo = new TypeInfo(image, this.Address + image.Process.MonoLibraryOffsets.TypeDefinitionByValArg);
63-
this.VTableSize = vtablePtr == Constants.NullPtr ? 0 : this.ReadInt32(image.Process.MonoLibraryOffsets.TypeDefinitionVTableSize);
62+
this.VTableSize = vtablePtr == IntPtr.Zero ? 0 : this.ReadInt32(image.Process.MonoLibraryOffsets.TypeDefinitionVTableSize);
6463
this.ClassKind = (MonoClassKind)(this.ReadByte(image.Process.MonoLibraryOffsets.TypeDefinitionClassKind) & 0x7);
6564

6665
// Get the generic type arguments
6766
if (this.TypeInfo.TypeCode == TypeCode.GENERICINST)
6867
{
6968
var monoGenericClassAddress = this.TypeInfo.Data;
7069
var monoClassAddress = this.Process.ReadPtr(monoGenericClassAddress);
71-
TypeDefinition monoClass = this.Image.GetTypeDefinition(monoClassAddress);
70+
this.Image.GetTypeDefinition(monoClassAddress);
7271

7372
var monoGenericContainerPtr = monoClassAddress + this.Process.MonoLibraryOffsets.TypeDefinitionGenericContainer;
7473
var monoGenericContainerAddress = this.Process.ReadPtr(monoGenericContainerPtr);
@@ -165,9 +164,7 @@ public TValue GetStaticValue<TValue>(string fieldName)
165164
}
166165
catch (Exception e)
167166
{
168-
throw new Exception(
169-
$"Exception received when trying to get static value for field '{fieldName}' in class '{this.FullName}': ${e.Message}.",
170-
e);
167+
throw new Exception($"Exception received when trying to get static value for field '{fieldName}' in class '{this.FullName}': ${e.Message}.", e);
171168
}
172169
}
173170

@@ -190,7 +187,7 @@ private TypeDefinition GetClassDefinition(int address) =>
190187
private IReadOnlyList<FieldDefinition> GetFields()
191188
{
192189
var firstField = this.ReadPtr(this.Image.Process.MonoLibraryOffsets.TypeDefinitionFields);
193-
if (firstField == Constants.NullPtr)
190+
if (firstField == IntPtr.Zero)
194191
{
195192
return this.Parent?.Fields ?? new List<FieldDefinition>();
196193
}
@@ -205,7 +202,7 @@ private IReadOnlyList<FieldDefinition> GetFields()
205202
for (var fieldIndex = 0; fieldIndex < this.fieldCount; fieldIndex++)
206203
{
207204
var field = firstField + (fieldIndex * this.Process.MonoLibraryOffsets.TypeDefinitionFieldSize);
208-
if (this.Process.ReadPtr(field) == Constants.NullPtr)
205+
if (this.Process.ReadPtr(field) == IntPtr.Zero)
209206
{
210207
break;
211208
}
@@ -222,7 +219,6 @@ private IReadOnlyList<FieldDefinition> GetFields()
222219
private string GetFullName()
223220
{
224221
var builder = new StringBuilder();
225-
226222
var hierarchy = this.NestedHierarchy().Reverse().ToArray();
227223
if (!string.IsNullOrWhiteSpace(this.NamespaceName))
228224
{

src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ private static MonoLibraryOffsets GetOffsets(UnityVersion unityVersion, bool is6
333333
}
334334

335335
Console.WriteLine(unsupportedMsg);
336-
Console.WriteLine($"Offsets of {bestCandidateUnityVersion.ToString()} selected instead.");
336+
Console.WriteLine($"Offsets of {bestCandidateUnityVersion} selected instead.");
337337

338338
return bestCandidate;
339339
}

src/HackF5.UnitySpy/ProcessFacade/ProcessFacade.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private TValue ReadBufferValue<TValue>(IntPtr address, int size, Func<byte[], TV
192192
private object[] ReadManagedArray(TypeInfo type, List<TypeInfo> genericTypeArguments, IntPtr address)
193193
{
194194
var ptr = this.ReadPtr(address);
195-
if (ptr == Constants.NullPtr)
195+
if (ptr == IntPtr.Zero)
196196
{
197197
return default;
198198
}
@@ -216,7 +216,7 @@ private object[] ReadManagedArray(TypeInfo type, List<TypeInfo> genericTypeArgum
216216
private ManagedClassInstance ReadManagedClassInstance(TypeInfo type, List<TypeInfo> genericTypeArguments, IntPtr address)
217217
{
218218
var ptr = this.ReadPtr(address);
219-
return ptr == Constants.NullPtr
219+
return ptr == IntPtr.Zero
220220
? default
221221
: new ManagedClassInstance(type.Image, genericTypeArguments, ptr);
222222
}
@@ -252,7 +252,7 @@ private object ReadManagedVar(TypeInfo type, List<TypeInfo> genericTypeArguments
252252
private string ReadManagedString(IntPtr address)
253253
{
254254
var ptr = this.ReadPtr(address);
255-
if (ptr == Constants.NullPtr)
255+
if (ptr == IntPtr.Zero)
256256
{
257257
return default;
258258
}

src/HackF5.UnitySpy/Util/Constants.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/HackF5.UnitySpy/Util/MemoryReadingUtils.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ private void SingleReadMemoryRecursive(IntPtr address, int length, int stepSize,
4040

4141
strBuilder.AppendLine("========================================== Reading Memory at " + addressStr + " Depth = " + recursiveDepth + " ========================================== ");
4242

43-
var ptr = Constants.NullPtr;
44-
if (address != Constants.NullPtr)
43+
var ptr = IntPtr.Zero;
44+
if (address != IntPtr.Zero)
4545
{
4646
try
4747
{
4848
ptr = this.process.ReadPtr(address);
4949
}
50-
catch (Exception)
50+
catch
5151
{
5252
}
5353
}
@@ -74,7 +74,7 @@ private void SingleReadMemoryRecursive(IntPtr address, int length, int stepSize,
7474
return;
7575
}
7676

77-
if (ptr != Constants.NullPtr)
77+
if (ptr != IntPtr.Zero)
7878
{
7979
if (this.pointersShown.Contains(ptr))
8080
{
@@ -86,7 +86,7 @@ private void SingleReadMemoryRecursive(IntPtr address, int length, int stepSize,
8686
{
8787
strBuilder.AppendLine("Value as char *: " + this.process.ReadAsciiString(ptr));
8888
}
89-
catch (Exception)
89+
catch
9090
{
9191
}
9292

0 commit comments

Comments
 (0)