Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/HackF5.UnitySpy/AssemblyImageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static AssemblyImage GetAssemblyImage(UnityProcessFacade process, string
//// pointer to array of structs of type _MonoAssembly
var assemblyArrayAddress = process.ReadPtr(domain + process.MonoLibraryOffsets.ReferencedAssemblies);
for (var assemblyAddress = assemblyArrayAddress;
assemblyAddress != Constants.NullPtr;
assemblyAddress != IntPtr.Zero;
assemblyAddress = process.ReadPtr(assemblyAddress + process.SizeOfPtr))
{
var assembly = process.ReadPtr(assemblyAddress);
Expand All @@ -132,10 +132,9 @@ private static IntPtr GetRootDomainFunctionAddressPEFormat(byte[] moduleDump, Mo
var numberOfFunctions = moduleDump.ToInt32(exportDirectory + PEFormatOffsets.NumberOfFunctions);
var functionAddressArrayIndex = moduleDump.ToInt32(exportDirectory + PEFormatOffsets.FunctionAddressArrayIndex);
var functionNameArrayIndex = moduleDump.ToInt32(exportDirectory + PEFormatOffsets.FunctionNameArrayIndex);

var rootDomainFunctionAddress = Constants.NullPtr;
var rootDomainFunctionAddress = IntPtr.Zero;
for (var functionIndex = 0;
functionIndex < (numberOfFunctions * PEFormatOffsets.FunctionEntrySize);
functionIndex < numberOfFunctions * PEFormatOffsets.FunctionEntrySize;
functionIndex += PEFormatOffsets.FunctionEntrySize)
{
var functionNameIndex = moduleDump.ToInt32(functionNameArrayIndex + functionIndex);
Expand All @@ -149,7 +148,7 @@ private static IntPtr GetRootDomainFunctionAddressPEFormat(byte[] moduleDump, Mo
}
}

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

private static IntPtr GetRootDomainFunctionAddressMachOFormat(ModuleInfo monoModuleInfo)
{
var rootDomainFunctionAddress = Constants.NullPtr;
var rootDomainFunctionAddress = IntPtr.Zero;

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

Expand Down Expand Up @@ -196,7 +195,7 @@ private static IntPtr GetRootDomainFunctionAddressMachOFormat(ModuleInfo monoMod
}
}

if (rootDomainFunctionAddress == Constants.NullPtr)
if (rootDomainFunctionAddress == IntPtr.Zero)
{
throw new InvalidOperationException("Failed to find mono_get_root_domain function.");
}
Expand Down
6 changes: 2 additions & 4 deletions src/HackF5.UnitySpy/Detail/AssemblyImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Linq;
using HackF5.UnitySpy.ProcessFacade;
using HackF5.UnitySpy.Util;
using JetBrains.Annotations;

/// <summary>
Expand Down Expand Up @@ -68,7 +67,7 @@ public TypeDefinition GetTypeDefinition(string fullTypeName) =>

public TypeDefinition GetTypeDefinition(IntPtr address)
{
if (address == Constants.NullPtr)
if (address == IntPtr.Zero)
{
return default;
}
Expand All @@ -81,7 +80,6 @@ public TypeDefinition GetTypeDefinition(IntPtr address)
private ConcurrentDictionary<IntPtr, TypeDefinition> CreateTypeDefinitions()
{
var definitions = new ConcurrentDictionary<IntPtr, TypeDefinition>();

int classCache = this.Process.MonoLibraryOffsets.ImageClassCache;
var classCacheSize = this.ReadUInt32(classCache + this.Process.MonoLibraryOffsets.HashTableSize);
var classCacheTableArray = this.ReadPtr(classCache + this.Process.MonoLibraryOffsets.HashTableTable);
Expand All @@ -91,7 +89,7 @@ private ConcurrentDictionary<IntPtr, TypeDefinition> CreateTypeDefinitions()
tableItem += this.Process.SizeOfPtr)
{
for (var definition = this.Process.ReadPtr(classCacheTableArray + tableItem);
definition != Constants.NullPtr;
definition != IntPtr.Zero;
definition = this.Process.ReadPtr(definition + this.Process.MonoLibraryOffsets.TypeDefinitionNextClassCache))
{
definitions.GetOrAdd(definition, new TypeDefinition(this, definition));
Expand Down
22 changes: 3 additions & 19 deletions src/HackF5.UnitySpy/Detail/FieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public FieldDefinition([NotNull] TypeDefinition declaringType, IntPtr address)
{
var monoGenericClassAddress = this.TypeInfo.Data;
var monoClassAddress = this.Process.ReadPtr(monoGenericClassAddress);
TypeDefinition monoClass = this.Image.GetTypeDefinition(monoClassAddress);
this.Image.GetTypeDefinition(monoClassAddress);

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

public TValue GetValue<TValue>(List<TypeInfo> genericTypeArguments, IntPtr address)
{
int offset;
if (this.DeclaringType.IsValueType && !this.TypeInfo.IsStatic)
{
offset = this.Offset - (this.Process.SizeOfPtr * 2);
}
else
{
offset = this.Offset;
}

if (this.genericTypeArguments != null)
{
return (TValue)this.TypeInfo.GetValue(this.genericTypeArguments, address + offset);
}
else
{
return (TValue)this.TypeInfo.GetValue(genericTypeArguments, address + offset);
}
int offset = this.DeclaringType.IsValueType && !this.TypeInfo.IsStatic ? this.Offset - (this.Process.SizeOfPtr * 2) : this.Offset;
return (TValue)this.TypeInfo.GetValue(this.genericTypeArguments ?? genericTypeArguments, address + offset);
}
}
}
16 changes: 6 additions & 10 deletions src/HackF5.UnitySpy/Detail/TypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.Linq;
using System.Text;
using HackF5.UnitySpy.Util;
using JetBrains.Annotations;

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

// Get the generic type arguments
if (this.TypeInfo.TypeCode == TypeCode.GENERICINST)
{
var monoGenericClassAddress = this.TypeInfo.Data;
var monoClassAddress = this.Process.ReadPtr(monoGenericClassAddress);
TypeDefinition monoClass = this.Image.GetTypeDefinition(monoClassAddress);
this.Image.GetTypeDefinition(monoClassAddress);

var monoGenericContainerPtr = monoClassAddress + this.Process.MonoLibraryOffsets.TypeDefinitionGenericContainer;
var monoGenericContainerAddress = this.Process.ReadPtr(monoGenericContainerPtr);
Expand Down Expand Up @@ -165,9 +164,7 @@ public TValue GetStaticValue<TValue>(string fieldName)
}
catch (Exception e)
{
throw new Exception(
$"Exception received when trying to get static value for field '{fieldName}' in class '{this.FullName}': ${e.Message}.",
e);
throw new Exception($"Exception received when trying to get static value for field '{fieldName}' in class '{this.FullName}': ${e.Message}.", e);
}
}

Expand All @@ -190,7 +187,7 @@ private TypeDefinition GetClassDefinition(int address) =>
private IReadOnlyList<FieldDefinition> GetFields()
{
var firstField = this.ReadPtr(this.Image.Process.MonoLibraryOffsets.TypeDefinitionFields);
if (firstField == Constants.NullPtr)
if (firstField == IntPtr.Zero)
{
return this.Parent?.Fields ?? new List<FieldDefinition>();
}
Expand All @@ -205,7 +202,7 @@ private IReadOnlyList<FieldDefinition> GetFields()
for (var fieldIndex = 0; fieldIndex < this.fieldCount; fieldIndex++)
{
var field = firstField + (fieldIndex * this.Process.MonoLibraryOffsets.TypeDefinitionFieldSize);
if (this.Process.ReadPtr(field) == Constants.NullPtr)
if (this.Process.ReadPtr(field) == IntPtr.Zero)
{
break;
}
Expand All @@ -222,7 +219,6 @@ private IReadOnlyList<FieldDefinition> GetFields()
private string GetFullName()
{
var builder = new StringBuilder();

var hierarchy = this.NestedHierarchy().Reverse().ToArray();
if (!string.IsNullOrWhiteSpace(this.NamespaceName))
{
Expand Down
2 changes: 1 addition & 1 deletion src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private static MonoLibraryOffsets GetOffsets(UnityVersion unityVersion, bool is6
}

Console.WriteLine(unsupportedMsg);
Console.WriteLine($"Offsets of {bestCandidateUnityVersion.ToString()} selected instead.");
Console.WriteLine($"Offsets of {bestCandidateUnityVersion} selected instead.");

return bestCandidate;
}
Expand Down
6 changes: 3 additions & 3 deletions src/HackF5.UnitySpy/ProcessFacade/ProcessFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private TValue ReadBufferValue<TValue>(IntPtr address, int size, Func<byte[], TV
private object[] ReadManagedArray(TypeInfo type, List<TypeInfo> genericTypeArguments, IntPtr address)
{
var ptr = this.ReadPtr(address);
if (ptr == Constants.NullPtr)
if (ptr == IntPtr.Zero)
{
return default;
}
Expand All @@ -216,7 +216,7 @@ private object[] ReadManagedArray(TypeInfo type, List<TypeInfo> genericTypeArgum
private ManagedClassInstance ReadManagedClassInstance(TypeInfo type, List<TypeInfo> genericTypeArguments, IntPtr address)
{
var ptr = this.ReadPtr(address);
return ptr == Constants.NullPtr
return ptr == IntPtr.Zero
? default
: new ManagedClassInstance(type.Image, genericTypeArguments, ptr);
}
Expand Down Expand Up @@ -252,7 +252,7 @@ private object ReadManagedVar(TypeInfo type, List<TypeInfo> genericTypeArguments
private string ReadManagedString(IntPtr address)
{
var ptr = this.ReadPtr(address);
if (ptr == Constants.NullPtr)
if (ptr == IntPtr.Zero)
{
return default;
}
Expand Down
9 changes: 0 additions & 9 deletions src/HackF5.UnitySpy/Util/Constants.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/HackF5.UnitySpy/Util/MemoryReadingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ private void SingleReadMemoryRecursive(IntPtr address, int length, int stepSize,

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

var ptr = Constants.NullPtr;
if (address != Constants.NullPtr)
var ptr = IntPtr.Zero;
if (address != IntPtr.Zero)
{
try
{
ptr = this.process.ReadPtr(address);
}
catch (Exception)
catch
{
}
}
Expand All @@ -74,7 +74,7 @@ private void SingleReadMemoryRecursive(IntPtr address, int length, int stepSize,
return;
}

if (ptr != Constants.NullPtr)
if (ptr != IntPtr.Zero)
{
if (this.pointersShown.Contains(ptr))
{
Expand All @@ -86,7 +86,7 @@ private void SingleReadMemoryRecursive(IntPtr address, int length, int stepSize,
{
strBuilder.AppendLine("Value as char *: " + this.process.ReadAsciiString(ptr));
}
catch (Exception)
catch
{
}

Expand Down