-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteAvatarLoader.cs
More file actions
448 lines (367 loc) · 17.1 KB
/
RemoteAvatarLoader.cs
File metadata and controls
448 lines (367 loc) · 17.1 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using System.Collections;
using System.Globalization;
using Il2CppRUMBLE.Managers;
using Il2CppSystem.Text;
using Il2CppTMPro;
using MelonLoader;
using MelonLoader.Utils;
using RumbleModdingAPI;
using UnityEngine;
using UnityEngine.Networking;
namespace CustomAvatars;
public class RemoteAvatarLoader
{
// ====== CONFIG ======
const string GH_REPO = "xLoadingx/custom-avatars";
const string BRANCH = "main";
private const int MAX_UPLOAD_BYTES = 25 * 1024 * 1024; // 25 MB
private const string PART_A_B64 = "PTMuMi84BSo7LgVraxsMHREAEANqH2spLzwdFihrKS5rBTwrAi49NBYdPBQVKw==";
private const string PART_B_B64 = "Dh0iEzwJPjsjPm4xIBwYbCw7Cz0gCBgYEjcwa2NuA25sAw4SAxUZLQIiaDc/FD4=";
private const byte XOR_KEY = 0x5A;
private static readonly HashSet<string> _downloadingPlayers = new();
public static bool isUploading = false;
public static object uploadCoroutine;
// Helper for GitHub API authentication
static string GetToken()
{
byte[] a = Convert.FromBase64String(PART_A_B64);
byte[] b = Convert.FromBase64String(PART_B_B64);
for (int i = 0; i < a.Length; i++) a[i] ^= XOR_KEY;
for (int i = 0; i < b.Length; i++) b[i] ^= XOR_KEY;
var merged = new byte[a.Length + b.Length];
Buffer.BlockCopy(a, 0, merged, 0, a.Length);
Buffer.BlockCopy(b, 0, merged, a.Length, b.Length);
return Encoding.UTF8.GetString(merged);
}
// Simple encryption
// Doesn't do much, but its good for preventing casual copying
public static byte[] XorCrypt(byte[] data, byte key = 0x5A)
{
for (int i = 0; i < data.Length; i++)
data[i] ^= key;
return data;
}
// Adds required GitHub headers to the request.
// Mostly not to get rate-limited.
static void SetGhHeaders(UnityWebRequest req, bool wantRaw)
{
req.SetRequestHeader("User-Agent", "CustomAvatars/1.0");
req.SetRequestHeader("Authorization", "Bearer " + GetToken());
req.SetRequestHeader("Accept", wantRaw
? "application/vnd.github.raw"
: "application/vnd.github+json");
}
// Builds GitHub API URL for fetching an avatar.
static string GhUrl(string masterId)
{
var fname = Uri.EscapeDataString(masterId);
return $"https://api.github.com/repos/{GH_REPO}/contents/avatars/{fname}?ref={BRANCH}";
}
// Builds GitHub API URL for uploading files
static string UploadUrlForPath(string pathRelativeToRepoRoot)
{
var fname = Uri.EscapeDataString(pathRelativeToRepoRoot);
return $"https://api.github.com/repos/{GH_REPO}/contents/{fname}";
}
// Sends a small JSON log to GitHub (tattletale system)
static IEnumerator SendAudit(string tag, string jsonPayload)
{
var url = UploadUrlForPath($"logs/{DateTime.UtcNow:yyyy-MM-dd}/{Guid.NewGuid():N}.json");
var body = $"{{\"message\":\"log:{tag}\",\"content\":\"{Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonPayload))}\",\"branch\":\"{BRANCH}\"}}";
var req = new UnityWebRequest(url, "PUT");
req.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(body));
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
SetGhHeaders(req, wantRaw: false);
yield return req.SendWebRequest();
req.Dispose();
}
// Starts coroutine to upload a bundle to GitHub
// Mostly because I hate typing MelonCoroutines.Start()
public static void UploadBundle(string masterId, string path, Action onStartUpload, Action<bool, bool> done, Action<float> onProgress = null, TextMeshPro serverStatusText = null) =>
uploadCoroutine ??= MelonCoroutines.Start(UploadBundleCoroutine(masterId, path, onStartUpload, done, onProgress, serverStatusText));
// Okay here's the actual coroutine
// Full upload workflow: validate, check SHA, skip if identical, else let GitHub eat it.
// Sure, the WaitForSeconds does slow down the process a bit, but visually it's a lot better
public static IEnumerator UploadBundleCoroutine(
string masterId,
string path,
Action onStartUpload,
Action<bool, bool> done,
Action<float> onProgress = null, /* 0-1 progress callback */
TextMeshPro serverStatusText = null
)
{
var data = PlayerManager.instance.LocalPlayer.Data.GeneralData;
if (masterId != data.PlayFabMasterId)
{
Main.instance.LoggerInstance.Error($"Player tried to upload avtar for masterId that isn't theirs.");
MelonCoroutines.Start(
SendAudit(
"masterId_mismatch",
$"[{DateTime.UtcNow:O}] Player {data.PublicUsername.TrimString()} ({data.PlayFabMasterId}) tried to write avatar for MasterId {masterId}"
)
);
uploadCoroutine = null;
if (serverStatusText != null)
{
serverStatusText.color = Color.red;
serverStatusText.text = "MasterId Mismatch";
}
yield return new WaitForSeconds(2f);
done?.Invoke(false, false);
yield break;
}
if (!File.Exists(path))
{
Main.instance.LoggerInstance.Error($"AssetBundle at path '{path}' does not exist.");
if (serverStatusText != null)
{
serverStatusText.color = Color.red;
serverStatusText.text = "Avatar doesn't exist";
}
yield return new WaitForSeconds(2f);
done?.Invoke(false, false);
uploadCoroutine = null;
yield break;
}
byte[] bytes;
try { bytes = File.ReadAllBytes(path); }
catch (Exception e)
{
Main.instance.LoggerInstance.Error($"ReadAllBytes failed: {e.Message}");
done?.Invoke(false, false);
uploadCoroutine = null;
yield break;
}
if (bytes.Length > MAX_UPLOAD_BYTES)
{
Main.instance.LoggerInstance.Error($"Upload failed: Bundle size {bytes.Length / (1024 * 1024)} MB exceeds {MAX_UPLOAD_BYTES / (1024 * 1024)} MB Limit.");
if (serverStatusText != null)
{
serverStatusText.color = Color.red;
serverStatusText.text = $"Bundle size is bigger than {MAX_UPLOAD_BYTES / (1024 * 1024)} MB limit";
}
yield return new WaitForSeconds(2f);
done?.Invoke(false, false);
uploadCoroutine = null;
yield break;
}
if (string.IsNullOrWhiteSpace(masterId) || bytes.Length == 0)
{ done?.Invoke(false, false); yield break; }
string sha = null;
Main.instance.LoggerInstance.Msg("Fetching Remote SHA...");
if (serverStatusText != null)
serverStatusText.text = "Fetching Remote SHA...";
yield return GetSha(masterId, s => sha = s);
Main.instance.LoggerInstance.Msg(sha != null
? $"Remote SHA: {sha.Substring(0, 8)}"
: "No remote file found - will create new file.");
if (serverStatusText != null)
serverStatusText.text = sha != null
? $"Remote File Exists"
: "No remote file found - will create new file.";
yield return new WaitForSeconds(1f);
if (sha != null && !string.IsNullOrEmpty(sha) && ShaMatchesLocal(sha, path))
{
Main.instance.LoggerInstance.Msg("Upload Skipped: Local file is identical to the server version.");
if (serverStatusText != null)
serverStatusText.text = "Local file is identical to the server version.";
yield return new WaitForSeconds(2f);
done?.Invoke(true, true);
uploadCoroutine = null;
yield break;
}
Main.instance.LoggerInstance.Msg($"File size: {bytes.Length / 1024f / 1024f:F2} MB");
Main.instance.LoggerInstance.Msg("Uploading to GitHub...");
if (serverStatusText != null)
serverStatusText.text = $"Uploading {bytes.Length / 1024f / 1024f:F2} MB to GitHub...";
yield return new WaitForSeconds(2f);
var encryped = XorCrypt(bytes);
var body = $"{{\"message\":\"Upload bundle for {masterId}. Uploaded by {data.PublicUsername.TrimString()}\",\"content\":\"{Convert.ToBase64String(encryped)}\",\"branch\":\"{BRANCH}\"" +
(sha != null ? $",\"sha\":\"{sha}\"" : "") + "}";
var url = UploadUrlForPath($"avatars/{masterId}");
var req = new UnityWebRequest(url, "PUT");
req.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(body));
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
SetGhHeaders(req, wantRaw: false);
var op = req.SendWebRequest();
onStartUpload?.Invoke();
while (!op.isDone)
{
onProgress?.Invoke(req.uploadProgress);
if (serverStatusText != null)
serverStatusText.text = $"{req.uploadProgress * 100:F2}%";
yield return null;
}
onProgress?.Invoke(1f);
bool ok = req.result == UnityWebRequest.Result.Success &&
req.responseCode is >= 200 and < 300;
if (!ok)
{
var errBytes = req.downloadHandler?.data;
var errTxt = errBytes != null ? System.Text.Encoding.UTF8.GetString(errBytes) : "";
Main.instance.LoggerInstance.Error(
$"Upload failed {masterId}: {req.responseCode} {req.error}\n{errTxt}");
MelonCoroutines.Start(
SendAudit(
"upload_fail",
$"[{DateTime.UtcNow:O}] Upload failed for MasterId {masterId} " +
$"Code={req.responseCode} Error = {req.error} " +
$"Body={(string.IsNullOrWhiteSpace(errTxt) ? "<empty>" : errTxt)}"
)
);
if (serverStatusText != null)
{
serverStatusText.color = Color.red;
serverStatusText.text = $"Upload failed. Check console for more info.";
}
}
else
{
if (serverStatusText != null)
{
serverStatusText.color = Color.green;
serverStatusText.text = "Uploaded succesfully!";
}
}
yield return new WaitForSeconds(2f);
req.Dispose();
uploadCoroutine = null;
done?.Invoke(ok, false);
}
// Compares local file SHA with remote (a SHA is just a hash to check differences)
public static bool ShaMatchesLocal(string remoteSha, string filePath, bool log = true)
{
var bytes = File.ReadAllBytes(filePath);
var header = System.Text.Encoding.ASCII.GetBytes($"blob {bytes.Length}\0");
// Crpytography is a strange thing
using var sha1 = System.Security.Cryptography.SHA1.Create();
sha1.TransformBlock(header, 0, header.Length, header, 0);
sha1.TransformFinalBlock(bytes, 0, bytes.Length);
var hex = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant();
if (log)
Main.instance.LoggerInstance.Msg($"Local SHA: {hex.Substring(0, 8)}");
return hex == remoteSha;
}
// Asks GitHub if a player has an avatar file uploaded
// Also works as a barrier for player load times messing it up XD
public static IEnumerator PlayerHasAvatar(string masterId, Action<(bool hasAvatar, string returnedSha)> callback)
{
yield return MelonCoroutines.Start(GetSha(masterId, sha => callback((!string.IsNullOrEmpty(sha), sha))));
}
// Retrieves the SHA hash of a player's avatar from GitHub.
public static IEnumerator GetSha(string masterId, Action<string> cb, bool log = true)
{
if (log)
Main.instance.LoggerInstance.Msg($"Fetching remote SHA for masterId {masterId}...");
var url = $"https://api.github.com/repos/{GH_REPO}/contents/avatars/{Uri.EscapeDataString(masterId)}?ref={BRANCH}";
var req = UnityWebRequest.Get(url);
SetGhHeaders(req, wantRaw:false);
yield return req.SendWebRequest();
if (req.responseCode == 404) { req.Dispose(); cb(null); yield break; }
if (log)
Main.instance.LoggerInstance.Msg($"GitHub responded {req.responseCode}: {req.result}");
if (req.result != UnityWebRequest.Result.Success)
{
Main.instance.LoggerInstance.Error($"Web request completed unsuccessfully | ERROR {req.responseCode} | {req.error}");
req.Dispose(); cb(null); yield break;
}
var data = req.downloadHandler?.data;
req.Dispose();
if (data == null || data.Length == 0) { cb(null); yield break; }
var txt = System.Text.Encoding.UTF8.GetString(data);
// I was on something when I made this
// Basically just returns the actual sha instead of the rest of the response.
int i = txt.IndexOf("\"sha\":\"", StringComparison.Ordinal);
if (i < 0) { cb(null); yield break; }
i += 7; int j = txt.IndexOf('\"', i);
cb(j > i ? txt.Substring(i, j - i) : null);
}
// Same as UploadAvatar
// Just starts the coroutine, for testing purposes
public static void StartDownloadToFile(string masterId, string savePath) =>
MelonCoroutines.Start(DownloadToFile(masterId, savePath));
// Downloads avatar bundle, size-checks it with the metadata, and saves to disk.
public static IEnumerator DownloadToFile(string masterId, string savePath)
{
if (!_downloadingPlayers.Add(masterId))
{
Main.instance.LoggerInstance.Warning($"Player {masterId} is already being downloaded.");
yield break;
}
// Contained inside of the file uploaded itself
var metaUrl = $"https://api.github.com/repos/{GH_REPO}/contents/avatars/{Uri.EscapeDataString(masterId.Split('_')[0])}?ref={BRANCH}";
var metaReq = UnityWebRequest.Get(metaUrl);
SetGhHeaders(metaReq, wantRaw: false);
yield return metaReq.SendWebRequest();
if (metaReq.result != UnityWebRequest.Result.Success)
{
Main.instance.LoggerInstance.Error($"Metadata fetch failed for {masterId}: {metaReq.error}");
metaReq.Dispose();
_downloadingPlayers.Remove(masterId);
yield break;
}
try
{
var bytes = metaReq.downloadHandler?.data;
if (bytes == null || bytes.Length == 0)
{
metaReq.Dispose();
_downloadingPlayers.Remove(masterId);
yield break;
}
var json = System.Text.Encoding.UTF8.GetString(bytes);
var sizeIndex = json.IndexOf("\"size\":", StringComparison.Ordinal);
if (sizeIndex >= 0)
{
sizeIndex += 7;
int endIndex = json.IndexOfAny(new[] { ',', '}' }, sizeIndex);
var sizeStr = json.Substring(sizeIndex, endIndex - sizeIndex).Trim();
if (int.TryParse(sizeStr, out int fileSizeBytes))
{
int maxDownloadBytes = (int)Main.instance.downloadLimitMB.SavedValue * 1024 * 1024;
if (fileSizeBytes > maxDownloadBytes)
{
Main.instance.LoggerInstance.Warning(
$"Download skipped: {fileSizeBytes / (1024 * 1024)} MB exceeds limit of {maxDownloadBytes / (1024 * 1024)} MB.");
metaReq.Dispose();
_downloadingPlayers.Remove(masterId);
yield break;
}
}
}
}
catch (Exception e)
{
Main.instance.LoggerInstance.Error($"Error parsing metadata for {masterId}: {e.Message}");
metaReq.Dispose();
_downloadingPlayers.Remove(masterId);
yield break;
}
metaReq.Dispose();
var req = UnityWebRequest.Get(GhUrl(masterId.Split('_')[0]));
SetGhHeaders(req, true);
yield return req.SendWebRequest();
if (req.result != UnityWebRequest.Result.Success)
{
Main.instance.LoggerInstance.Error($"Download failed for {masterId}: {req.error}");
}
else
{
var data = req.downloadHandler.data;
if (data == null || data.Length < 16)
{
Main.instance.LoggerInstance.Error($"Blocked: {masterId} file too small to be a valid AssetBundle.");
_downloadingPlayers.Remove(masterId);
req.Dispose();
yield break;
}
var encrypted = XorCrypt(data);
File.WriteAllBytes(savePath, encrypted);
}
_downloadingPlayers.Remove(masterId);
req.Dispose();
}
}