11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics . Contracts ;
4+ using System . Linq ;
45using System . Runtime . InteropServices ;
56using ReClassNET . Core ;
67using ReClassNET . Extensions ;
7- using ReClassNET . Util ;
88
99namespace ReClassNET . Memory
1010{
@@ -143,12 +143,16 @@ public IList<DisassembledInstruction> DisassembleFunction(byte[] data, IntPtr vi
143143 /// <returns>The prior instruction.</returns>
144144 public DisassembledInstruction RemoteGetPreviousInstruction ( RemoteProcess process , IntPtr address )
145145 {
146- var buffer = process . ReadRemoteMemory ( address - 6 * MaximumInstructionLength , 7 * MaximumInstructionLength ) ;
146+ const int TotalBufferSize = 7 * MaximumInstructionLength ;
147+ const int BufferShiftSize = 6 * MaximumInstructionLength ;
148+
149+ var buffer = process . ReadRemoteMemory ( address - BufferShiftSize , TotalBufferSize ) ;
147150
148151 var handle = GCHandle . Alloc ( buffer , GCHandleType . Pinned ) ;
149152 try
150153 {
151154 var bufferAddress = handle . AddrOfPinnedObject ( ) ;
155+ var targetBufferAddress = bufferAddress + BufferShiftSize ;
152156
153157 var instruction = default ( InstructionData ) ;
154158
@@ -157,16 +161,16 @@ public DisassembledInstruction RemoteGetPreviousInstruction(RemoteProcess proces
157161 6 * MaximumInstructionLength ,
158162 4 * MaximumInstructionLength ,
159163 2 * MaximumInstructionLength ,
160- MaximumInstructionLength ,
164+ 1 * MaximumInstructionLength ,
161165 14 , 13 , 12 , 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1
162166 } )
163167 {
164- var currentAddress = bufferAddress - offset ;
168+ var currentAddress = targetBufferAddress - offset ;
165169
166170 coreFunctions . DisassembleCode ( currentAddress , offset + 1 , address - offset , false , ( ref InstructionData data ) =>
167171 {
168172 var nextAddress = currentAddress + data . Length ;
169- if ( nextAddress . CompareTo ( address ) > - 1 )
173+ if ( nextAddress . CompareTo ( targetBufferAddress ) > 0 )
170174 {
171175 return false ;
172176 }
@@ -178,7 +182,7 @@ public DisassembledInstruction RemoteGetPreviousInstruction(RemoteProcess proces
178182 return true ;
179183 } ) ;
180184
181- if ( currentAddress == address )
185+ if ( currentAddress == targetBufferAddress )
182186 {
183187 return new DisassembledInstruction ( ref instruction ) ;
184188 }
@@ -227,19 +231,9 @@ public IntPtr RemoteGetFunctionStartAddress(RemoteProcess process, IntPtr addres
227231 if ( prevInstruction . Length == 1 && prevInstruction . Data [ 0 ] == 0xCC )
228232 {
229233 // Disassemble the code from the start and check if the instructions sum up to address.
230- var length = 0 ;
231- var res = coreFunctions . DisassembleCode ( start , address . Sub ( start ) . ToInt32 ( ) , IntPtr . Zero , false , ( ref InstructionData data ) =>
232- {
233- length += data . Length ;
234-
235- return true ;
236- } ) ;
237- if ( ! res )
238- {
239- continue ;
240- }
241-
242- if ( start + length == address )
234+ var totalInstructionLength = RemoteDisassembleCode ( process , start , address . Sub ( start ) . ToInt32 ( ) )
235+ . Sum ( ins => ins . Length ) ;
236+ if ( start + totalInstructionLength == address )
243237 {
244238 return start ;
245239 }
0 commit comments