forked from marcussacana/Playstation-4-Save-Mounter
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMounterClient.cs
More file actions
286 lines (258 loc) · 8.46 KB
/
Copy pathMounterClient.cs
File metadata and controls
286 lines (258 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net.Sockets;
using System.Text;
using Microsoft.Data.Sqlite;
namespace PS4Saves;
public class MounterClient : IDisposable
{
public const int DefaultPort = 9090;
private TcpClient _tcp;
private NetworkStream _stream;
private byte[] _saveDbCache;
private int _saveDbUserId;
public bool IsConnected => _tcp?.Connected == true;
public void Connect(string host, int port = DefaultPort, int retries = 10)
{
for (int i = 0; i < retries; i++)
{
try
{
_tcp = new TcpClient();
_tcp.Connect(host, port);
_tcp.ReceiveTimeout = 10000;
_tcp.SendTimeout = 10000;
_stream = _tcp.GetStream();
return;
}
catch
{
_tcp?.Dispose();
if (i == retries - 1) throw;
System.Threading.Thread.Sleep(500);
}
}
}
public void Disconnect()
{
try
{
if (_stream != null)
{
SendCommand("EXIT");
ReadLine();
}
}
catch { }
Dispose();
}
private void SendCommand(string cmd)
{
if (_stream == null) throw new IOException("Not connected");
byte[] data = Encoding.UTF8.GetBytes(cmd + "\n");
_stream.Write(data, 0, data.Length);
_stream.Flush();
}
private string ReadLine()
{
if (_stream == null) throw new IOException("Not connected");
var buf = new List<byte>();
while (true)
{
int b = _stream.ReadByte();
if (b < 0) return null;
if (b == '\n') break;
if (b == '\r') continue;
buf.Add((byte)b);
}
return Encoding.UTF8.GetString(buf.ToArray());
}
private byte[] ReadBytes(int count)
{
if (_stream == null) throw new IOException("Not connected");
byte[] buf = new byte[count];
int pos = 0;
while (pos < count)
{
int n = _stream.Read(buf, pos, count - pos);
if (n <= 0) throw new IOException("Connection closed during binary read");
pos += n;
}
return buf;
}
private string ExpectOk()
{
string line = ReadLine();
if (line == null) throw new IOException("Connection closed");
if (line.StartsWith("ERR ")) throw new Exception(line[4..]);
if (line == "OK") return "";
if (line.StartsWith("OK ")) return line[3..];
throw new Exception("Unexpected response: " + line);
}
public string GetFirmwareVersion()
{
SendCommand("GET_FW");
return ExpectOk();
}
public (int id, string name)[] GetUsers()
{
SendCommand("GET_USERS");
int count = int.Parse(ExpectOk());
var users = new List<(int, string)>();
for (int i = 0; i < count; i++)
{
string line = ReadLine() ?? throw new IOException("Connection closed");
int sp = line.IndexOf(' ');
if (sp < 0) continue;
int id = Convert.ToInt32(line[..sp], 16);
string name = line[(sp + 1)..];
users.Add((id, name));
}
return [.. users];
}
public string[] ListSaves(int userId)
{
SendCommand($"LIST_SAVES {userId:x}");
int count = int.Parse(ExpectOk());
var titles = new string[count];
for (int i = 0; i < count; i++)
titles[i] = ReadLine() ?? throw new IOException("Connection closed");
Array.Sort(titles);
return titles;
}
public SearchResult[] Search(int userId, string titleId)
{
SendCommand($"SEARCH {userId:x} {titleId}");
int count = int.Parse(ExpectOk());
var results = new SearchResult[count];
for (int i = 0; i < count; i++)
{
string line = ReadLine() ?? throw new IOException("Connection closed");
string[] parts = line.Split('\t');
results[i] = new SearchResult
{
DirName = parts.Length > 0 ? parts[0] : "",
Title = parts.Length > 1 ? parts[1] : "",
Subtitle = parts.Length > 2 ? parts[2] : "",
Detail = parts.Length > 3 ? parts[3] : "",
Time = parts.Length > 4 && long.TryParse(parts[4], out long mt) && mt > 0
? DateTimeOffset.FromUnixTimeSeconds(mt).LocalDateTime.ToString()
: "",
};
}
// Enrich entries missing subtitle/detail from savedata.db (mainly PS4 saves, PS5 uses SFO)
bool needsEnrich = false;
foreach (var r in results)
if (string.IsNullOrEmpty(r.Subtitle) && string.IsNullOrEmpty(r.Detail))
{ needsEnrich = true; break; }
if (needsEnrich)
EnrichFromSaveDb(userId, titleId, results);
return results;
}
private void EnrichFromSaveDb(int userId, string titleId, SearchResult[] results)
{
byte[] dbData;
try
{
if (_saveDbCache != null && _saveDbUserId == userId)
{
dbData = _saveDbCache;
}
else
{
string dbPath = $"/system_data/savedata/{userId:x}/db/user/savedata.db";
dbData = ReadFile(dbPath);
_saveDbCache = dbData;
_saveDbUserId = userId;
}
}
catch { return; }
string tmpPath = Path.GetTempFileName();
try
{
File.WriteAllBytes(tmpPath, dbData);
using var conn = new SqliteConnection($"Data Source={tmpPath};Mode=ReadOnly");
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT dir_name, main_title, sub_title, detail FROM savedata WHERE title_id = @tid";
cmd.Parameters.AddWithValue("@tid", titleId);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
string dirName = reader.GetString(0);
string mainTitle = reader.IsDBNull(1) ? "" : reader.GetString(1);
string subTitle = reader.IsDBNull(2) ? "" : reader.GetString(2);
string detail = reader.IsDBNull(3) ? "" : reader.GetString(3);
foreach (var r in results)
{
if (r.DirName != dirName) continue;
// Game title from app.db; fallback to savedata.db for uninstalled games
if (string.IsNullOrEmpty(r.Title) && !string.IsNullOrEmpty(mainTitle))
r.Title = mainTitle;
if (string.IsNullOrEmpty(r.Subtitle)) r.Subtitle = subTitle;
if (string.IsNullOrEmpty(r.Detail)) r.Detail = detail;
break;
}
}
}
catch { }
finally
{
try { File.Delete(tmpPath); } catch { }
}
}
public Image GetGameIcon(string titleId)
{
try
{
byte[] data = ReadFile($"/user/appmeta/{titleId}/icon0.png");
return Image.FromStream(new MemoryStream(data));
}
catch
{
return null;
}
}
public string Mount(int userId, string titleId, string dirName)
{
SendCommand($"MOUNT {userId:x} {titleId} {dirName}");
return ExpectOk();
}
public void Unmount()
{
SendCommand("UMOUNT");
ExpectOk();
}
public byte[] ReadFile(string path, int maxSize = 16 * 1024 * 1024)
{
SendCommand($"READ_FILE {path}");
int size = int.Parse(ExpectOk());
if (size > maxSize) throw new Exception($"File too large ({size} bytes, max {maxSize})");
return ReadBytes(size);
}
public string CreateSave(int userId, string titleId, string dirName, ulong blocks)
{
SendCommand($"CREATE {userId:x} {titleId} {dirName} {blocks}");
string result = ExpectOk();
_saveDbCache = null;
return result;
}
public void Dispose()
{
_stream?.Dispose();
_stream = null;
_tcp?.Dispose();
_tcp = null;
}
}
public class SearchResult
{
public string DirName;
public string Title;
public string Subtitle;
public string Detail;
public string Time;
public override string ToString() => DirName;
}