Skip to content

Commit af08e81

Browse files
committed
Refactor WriteAddressValue to simplify type handling
The code changes in the `WriteAddressValue` method within the `MemHackLib.PlatformImplementations` namespace in `MemHackLin.cs` remove the conversion of the `value` parameter to a byte array. Instead, the updated implementation directly assigns the `value` to the `data` variable based on its type. Specifically, it checks if `value` is of type `int`, `long`, `short`, or `byte` and assigns `data` as a new `nint` with the corresponding value. If `value` is of an unsupported type, it returns an error message "Unsupported value type." This refactor simplifies the process and eliminates the need for byte array conversion and pinning.
1 parent 77a09ea commit af08e81

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

MemHackLib/PlatformImplementations/MemHackLin.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,6 @@ public List<nint> MemorySearch(uint processId, long desiredValue)
398398
// Write value to process memory
399399
public string WriteAddressValue(uint processId, nint targetPointer, long value)
400400
{
401-
byte[] newValueBuffer = BitConverter.GetBytes((int)value);
402-
403401
// Open the process memory using ptrace
404402
nint handle = OpenProcess((int)processId);
405403

@@ -408,7 +406,19 @@ public string WriteAddressValue(uint processId, nint targetPointer, long value)
408406

409407
// Use ptrace PTRACE_POKEDATA to write the memory value
410408
nint addr = new(targetPointer);
411-
nint data = Marshal.UnsafeAddrOfPinnedArrayElement(newValueBuffer, 0);
409+
nint data;
410+
411+
if(ValueType == typeof(int))
412+
data = new((int)value);
413+
else if (ValueType == typeof(long))
414+
data = new((long)value);
415+
else if (ValueType == typeof(short))
416+
data = new((short)value);
417+
else if (ValueType == typeof(byte))
418+
data = new((byte)value);
419+
else
420+
return "Unsupported value type.";
421+
412422
int result = ptrace(PTRACE_POKEDATA, (int)processId, addr, data);
413423

414424
if (result == -1)

0 commit comments

Comments
 (0)