diff --git a/src/HackF5.UnitySpy/AssemblyImageFactory.cs b/src/HackF5.UnitySpy/AssemblyImageFactory.cs index 9736629..6be4dce 100644 --- a/src/HackF5.UnitySpy/AssemblyImageFactory.cs +++ b/src/HackF5.UnitySpy/AssemblyImageFactory.cs @@ -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); @@ -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); @@ -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."); } @@ -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); @@ -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."); } diff --git a/src/HackF5.UnitySpy/Detail/AssemblyImage.cs b/src/HackF5.UnitySpy/Detail/AssemblyImage.cs index d3b636d..62ae6e2 100644 --- a/src/HackF5.UnitySpy/Detail/AssemblyImage.cs +++ b/src/HackF5.UnitySpy/Detail/AssemblyImage.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Linq; using HackF5.UnitySpy.ProcessFacade; - using HackF5.UnitySpy.Util; using JetBrains.Annotations; /// @@ -68,7 +67,7 @@ public TypeDefinition GetTypeDefinition(string fullTypeName) => public TypeDefinition GetTypeDefinition(IntPtr address) { - if (address == Constants.NullPtr) + if (address == IntPtr.Zero) { return default; } @@ -81,7 +80,6 @@ public TypeDefinition GetTypeDefinition(IntPtr address) private ConcurrentDictionary CreateTypeDefinitions() { var definitions = new ConcurrentDictionary(); - int classCache = this.Process.MonoLibraryOffsets.ImageClassCache; var classCacheSize = this.ReadUInt32(classCache + this.Process.MonoLibraryOffsets.HashTableSize); var classCacheTableArray = this.ReadPtr(classCache + this.Process.MonoLibraryOffsets.HashTableTable); @@ -91,7 +89,7 @@ private ConcurrentDictionary 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)); diff --git a/src/HackF5.UnitySpy/Detail/FieldDefinition.cs b/src/HackF5.UnitySpy/Detail/FieldDefinition.cs index 53f4ea3..b91a1fc 100644 --- a/src/HackF5.UnitySpy/Detail/FieldDefinition.cs +++ b/src/HackF5.UnitySpy/Detail/FieldDefinition.cs @@ -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); @@ -80,24 +80,8 @@ public TValue GetValue(IntPtr address) public TValue GetValue(List 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); } } } \ No newline at end of file diff --git a/src/HackF5.UnitySpy/Detail/TypeDefinition.cs b/src/HackF5.UnitySpy/Detail/TypeDefinition.cs index 7e29e43..be230a0 100644 --- a/src/HackF5.UnitySpy/Detail/TypeDefinition.cs +++ b/src/HackF5.UnitySpy/Detail/TypeDefinition.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.Linq; using System.Text; - using HackF5.UnitySpy.Util; using JetBrains.Annotations; /// @@ -58,9 +57,9 @@ 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 @@ -68,7 +67,7 @@ public TypeDefinition([NotNull] AssemblyImage image, 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); @@ -165,9 +164,7 @@ public TValue GetStaticValue(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); } } @@ -190,7 +187,7 @@ private TypeDefinition GetClassDefinition(int address) => private IReadOnlyList GetFields() { var firstField = this.ReadPtr(this.Image.Process.MonoLibraryOffsets.TypeDefinitionFields); - if (firstField == Constants.NullPtr) + if (firstField == IntPtr.Zero) { return this.Parent?.Fields ?? new List(); } @@ -205,7 +202,7 @@ private IReadOnlyList 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; } @@ -222,7 +219,6 @@ private IReadOnlyList GetFields() private string GetFullName() { var builder = new StringBuilder(); - var hierarchy = this.NestedHierarchy().Reverse().ToArray(); if (!string.IsNullOrWhiteSpace(this.NamespaceName)) { diff --git a/src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs b/src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs index 17f84af..1676512 100644 --- a/src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs +++ b/src/HackF5.UnitySpy/Offsets/MonoLibraryOffsets.cs @@ -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; } diff --git a/src/HackF5.UnitySpy/ProcessFacade/ProcessFacade.cs b/src/HackF5.UnitySpy/ProcessFacade/ProcessFacade.cs index c021307..5b12784 100644 --- a/src/HackF5.UnitySpy/ProcessFacade/ProcessFacade.cs +++ b/src/HackF5.UnitySpy/ProcessFacade/ProcessFacade.cs @@ -192,7 +192,7 @@ private TValue ReadBufferValue(IntPtr address, int size, Func genericTypeArguments, IntPtr address) { var ptr = this.ReadPtr(address); - if (ptr == Constants.NullPtr) + if (ptr == IntPtr.Zero) { return default; } @@ -216,7 +216,7 @@ private object[] ReadManagedArray(TypeInfo type, List genericTypeArgum private ManagedClassInstance ReadManagedClassInstance(TypeInfo type, List genericTypeArguments, IntPtr address) { var ptr = this.ReadPtr(address); - return ptr == Constants.NullPtr + return ptr == IntPtr.Zero ? default : new ManagedClassInstance(type.Image, genericTypeArguments, ptr); } @@ -252,7 +252,7 @@ private object ReadManagedVar(TypeInfo type, List genericTypeArguments private string ReadManagedString(IntPtr address) { var ptr = this.ReadPtr(address); - if (ptr == Constants.NullPtr) + if (ptr == IntPtr.Zero) { return default; } diff --git a/src/HackF5.UnitySpy/Util/Constants.cs b/src/HackF5.UnitySpy/Util/Constants.cs deleted file mode 100644 index b61722c..0000000 --- a/src/HackF5.UnitySpy/Util/Constants.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace HackF5.UnitySpy.Util -{ - using System; - - public static class Constants - { - public static IntPtr NullPtr => IntPtr.Zero; - } -} \ No newline at end of file diff --git a/src/HackF5.UnitySpy/Util/MemoryReadingUtils.cs b/src/HackF5.UnitySpy/Util/MemoryReadingUtils.cs index fa7a828..868d183 100644 --- a/src/HackF5.UnitySpy/Util/MemoryReadingUtils.cs +++ b/src/HackF5.UnitySpy/Util/MemoryReadingUtils.cs @@ -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 { } } @@ -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)) { @@ -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 { }