Skip to content

Commit bad6414

Browse files
authored
Improvements for Call()
1 parent 58bb396 commit bad6414

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

PS4DBG.Proc.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ public ulong AllocateMemory(int pid, int length) {
6161
public ulong Call(int pid, ulong rpcstub, ulong address, params object[] args) {
6262
CheckConnected(); // Check if the connection is active
6363

64-
// Create a custom format packet
65-
CMDPacket packet = new CMDPacket {
66-
magic = CMD_PACKET_MAGIC,
67-
cmd = (uint)CMDS.CMD_PROC_CALL,
68-
datalen = (uint)CMD_PROC_CALL_PACKET_SIZE
69-
};
70-
SendData(GetBytesFromObject(packet), CMD_PACKET_SIZE); // Send the packet
71-
72-
MemoryStream rs = new MemoryStream(); // Create a memory stream to store the arguments
73-
rs.Write(BitConverter.GetBytes(pid), 0, sizeof(int)); // Write PID to the stream
64+
// Build a new CMD (Command) Packet that will be sent to the PS4 System
65+
CMDPacket packet = new CMDPacket();
66+
packet.magic = CMD_PACKET_MAGIC;
67+
packet.cmd = (uint)CMDS.CMD_PROC_CALL;
68+
packet.datalen = (uint)CMD_PROC_CALL_PACKET_SIZE;
69+
70+
// Send the packet to the PS4 System
71+
SendData(GetBytesFromObject(packet), CMD_PACKET_SIZE);
72+
73+
// Create a memory stream to store the arguments
74+
MemoryStream rs = new MemoryStream();
75+
rs.Write(BitConverter.GetBytes(pid), 0, sizeof(int)); // Write PID to the stream
7476
rs.Write(BitConverter.GetBytes(rpcstub), 0, sizeof(ulong)); // Write RPC stub to the stream
7577
rs.Write(BitConverter.GetBytes(address), 0, sizeof(ulong)); // Write address to the stream
7678

@@ -149,7 +151,12 @@ public ulong Call(int pid, ulong rpcstub, ulong address, params object[] args) {
149151
}
150152

151153
// Check the number of arguments
152-
if (num > 6) throw new Exception("libdebug: too many arguments");
154+
if (num > 6) {
155+
// cleanup memory stream
156+
rs.Dispose();
157+
// throw the exception
158+
throw new Exception($"libdebug: too many arguments! max 6, current is {num}");
159+
}
153160

154161
// If there are less than 6 arguments, pad the remaining slots with zeros
155162
if (num < 6) {
@@ -158,16 +165,17 @@ public ulong Call(int pid, ulong rpcstub, ulong address, params object[] args) {
158165
}
159166
}
160167

161-
// Send the data
168+
// Now send the memory stream to our PS4 System
162169
SendData(rs.ToArray(), CMD_PROC_CALL_PACKET_SIZE);
163-
rs.Dispose(); // Dispose of the memory stream
170+
171+
// Perform some cleanup
172+
rs.Dispose();
164173

165174
// Check the status
166175
CheckStatus();
167176

168177
// Receive and return the result
169-
byte[] data = ReceiveData(PROC_CALL_SIZE);
170-
return BitConverter.ToUInt64(data, 4);
178+
return BitConverter.ToUInt64(ReceiveData(PROC_CALL_SIZE), 4);
171179
}
172180

173181
/// <summary>
@@ -347,6 +355,7 @@ public T ReadMemory<T>(int pid, ulong address) {
347355

348356
while (true) {
349357
byte value = ReadMemory(pid, address + i, sizeof(byte))[0];
358+
350359
if (value == 0) {
351360
break;
352361
}

0 commit comments

Comments
 (0)