@@ -166,19 +166,7 @@ public byte ReadUInt8(ulong address)
166
166
}
167
167
168
168
public bool TryReadUInt8 ( ulong address , out byte value )
169
- => TryReadUInt8 ( address , _reader , out value ) ;
170
-
171
- private static bool TryReadUInt8 ( ulong address , Reader reader , out byte value )
172
- {
173
- value = 0 ;
174
- fixed ( byte * ptr = & value )
175
- {
176
- if ( reader . ReadFromTarget ( address , ptr , 1 ) < 0 )
177
- return false ;
178
- }
179
-
180
- return true ;
181
- }
169
+ => TryRead ( address , isUnsigned : true , out value ) ;
182
170
183
171
public uint ReadUInt32 ( ulong address )
184
172
{
@@ -189,21 +177,38 @@ public uint ReadUInt32(ulong address)
189
177
}
190
178
191
179
public bool TryReadUInt32 ( ulong address , out uint value )
192
- => TryReadUInt32 ( address , _config . IsLittleEndian , _reader , out value ) ;
180
+ => TryRead ( address , isUnsigned : true , out value ) ;
193
181
194
182
private static bool TryReadUInt32 ( ulong address , bool isLittleEndian , Reader reader , out uint value )
183
+ => TryRead ( address , isLittleEndian , isUnsigned : true , reader , out value ) ;
184
+
185
+ public ulong ReadUInt64 ( ulong address )
195
186
{
196
- value = 0 ;
187
+ if ( ! TryReadUInt64 ( address , out ulong value ) )
188
+ throw new InvalidOperationException ( $ "Failed to read uint32 at 0x{ address : x8} .") ;
197
189
198
- Span < byte > buffer = stackalloc byte [ sizeof ( uint ) ] ;
190
+ return value ;
191
+ }
192
+
193
+ public bool TryReadUInt64 ( ulong address , out ulong value )
194
+ => TryRead ( address , isUnsigned : true , out value ) ;
195
+
196
+ private static bool TryReadUInt64 ( ulong address , bool isLittleEndian , Reader reader , out ulong value )
197
+ => TryRead ( address , isLittleEndian , isUnsigned : true , reader , out value ) ;
198
+
199
+ private bool TryRead < T > ( ulong address , bool isUnsigned , out T value ) where T : unmanaged, IBinaryInteger < T >
200
+ => TryRead ( address , _config . IsLittleEndian , isUnsigned , _reader , out value ) ;
201
+
202
+ private static bool TryRead < T > ( ulong address , bool isLittleEndian , bool isUnsigned , Reader reader , out T value ) where T : unmanaged, IBinaryInteger < T >
203
+ {
204
+ value = default ;
205
+ Span < byte > buffer = stackalloc byte [ sizeof ( T ) ] ;
199
206
if ( reader . ReadFromTarget ( address , buffer ) < 0 )
200
207
return false ;
201
208
202
- value = isLittleEndian
203
- ? BinaryPrimitives . ReadUInt32LittleEndian ( buffer )
204
- : BinaryPrimitives . ReadUInt32BigEndian ( buffer ) ;
205
-
206
- return true ;
209
+ return isLittleEndian
210
+ ? T . TryReadLittleEndian ( buffer , isUnsigned , out value )
211
+ : T . TryReadBigEndian ( buffer , isUnsigned , out value ) ;
207
212
}
208
213
209
214
public TargetPointer ReadPointer ( ulong address )
@@ -225,22 +230,20 @@ private static bool TryReadPointer(ulong address, Configuration config, Reader r
225
230
if ( reader . ReadFromTarget ( address , buffer ) < 0 )
226
231
return false ;
227
232
228
- if ( config . PointerSize == sizeof ( uint ) )
233
+ if ( config . PointerSize == sizeof ( uint )
234
+ && TryReadUInt32 ( address , config . IsLittleEndian , reader , out uint value32 ) )
229
235
{
230
- pointer = new TargetPointer (
231
- config . IsLittleEndian
232
- ? BinaryPrimitives . ReadUInt32LittleEndian ( buffer )
233
- : BinaryPrimitives . ReadUInt32BigEndian ( buffer ) ) ;
236
+ pointer = new TargetPointer ( value32 ) ;
237
+ return true ;
234
238
}
235
- else if ( config . PointerSize == sizeof ( ulong ) )
239
+ else if ( config . PointerSize == sizeof ( ulong )
240
+ && TryReadUInt64 ( address , config . IsLittleEndian , reader , out ulong value64 ) )
236
241
{
237
- pointer = new TargetPointer (
238
- config . IsLittleEndian
239
- ? BinaryPrimitives . ReadUInt64LittleEndian ( buffer )
240
- : BinaryPrimitives . ReadUInt64BigEndian ( buffer ) ) ;
242
+ pointer = new TargetPointer ( value64 ) ;
243
+ return true ;
241
244
}
242
245
243
- return true ;
246
+ return false ;
244
247
}
245
248
246
249
public byte ReadGlobalUInt8 ( string name )
0 commit comments