This repository was archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVRCModUpdaterCore.cs
571 lines (493 loc) · 28 KB
/
VRCModUpdaterCore.cs
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using MelonLoader;
using Mono.Cecil;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using System.Windows.Forms;
using VRCModUpdater.API;
using VRCModUpdater.Core.API;
using VRCModUpdater.Core.Externs;
using VRCModUpdater.Core.Utils;
using Winuser;
namespace VRCModUpdater.Core
{
public static class VRCModUpdaterCore
{
public const string VERSION = "1.0.6";
private static readonly Dictionary<string, string> oldToNewModNames = new Dictionary<string, string>()
{
// Used in case something is missing on Ruby's API
};
private static float postUpdateDisplayDuration = 3f;
private static bool isUpdatingMods = true;
private static Dictionary<string, ModDetail> remoteMods = new Dictionary<string, ModDetail>();
private static Dictionary<string, ModDetail> installedMods = new Dictionary<string, ModDetail>();
private static Dictionary<string, ModDetail> brokenMods = new Dictionary<string, ModDetail>();
public static string currentStatus = "", tmpCurrentStatus = "";
public static int progressTotal = 0, progressDownload = 0;
private static int tmpProgressTotal = 0, tmpProgressDownload = 0;
private static int toUpdateCount = 0;
private static string msg;
private static List<FailedUpdateInfo> failedUpdates = new List<FailedUpdateInfo>();
private static MelonPreferences_Entry<bool> toFromBroken, resolveDependencies, resolveOptionalDependencies, popUpMsg;
public static void Start()
{
var prefCategory = MelonPreferences.CreateCategory("VRCModUpdater");
var diplayTimeEntry = prefCategory.CreateEntry("displaytime", postUpdateDisplayDuration, "Display time (seconds)");
toFromBroken = prefCategory.CreateEntry("toFromBroken", true, "Attempt to move mods to and from Broken mods folder based on status in Remote API");
resolveDependencies = prefCategory.CreateEntry("resolveDependencies", true, "Attempt to download missing required dependencies");
resolveOptionalDependencies = prefCategory.CreateEntry("resolveOptionalDependencies", false, "Also attempt to download missing OPTIONAL dependencies");
popUpMsg = prefCategory.CreateEntry("popUpMsg", true, "Display pop up messsage box if mods are updated or moved to/from broken");
if (float.TryParse(diplayTimeEntry.GetValueAsString(), out float diplayTime))
postUpdateDisplayDuration = diplayTime;
UpdaterWindow.CreateWindow();
new Thread(() =>
{
try
{
UpdateMods();
}
catch (Exception e)
{
MelonLogger.Error("Failed to update mods:\n" + e);
}
isUpdatingMods = false;
})
{
Name = "VRCModUpdater",
IsBackground = true
}.Start();
while (isUpdatingMods || UpdaterWindow.IsOpen) // We need to do that event w/o the window, because the console need it
{
if (!isUpdatingMods && !UpdaterWindow.IsWindowClosing)
UpdaterWindow.DestroyWindow();
DispatchWindowEvents();
}
}
private static void DispatchWindowEvents()
{
if (!User32.PeekMessage(out Msg msg, IntPtr.Zero, 0, 0, 1))
{
if (currentStatus != tmpCurrentStatus || progressTotal != tmpProgressTotal || progressDownload != tmpProgressDownload)
{
tmpCurrentStatus = currentStatus;
tmpProgressTotal = progressTotal;
tmpProgressDownload = progressDownload;
UpdaterWindow.RedrawWindow();
}
Thread.Sleep(16); // ~60/s
}
else
{
User32.TranslateMessage(ref msg);
User32.DispatchMessage(ref msg);
}
}
private static void UpdateMods()
{
Thread.Sleep(500);
currentStatus = "Fetching remote mods...";
FetchRemoteMods();
currentStatus = "Listing installed mods...";
ScanModFolder();
currentStatus = "";
DownloadAndUpdateMods();
if (toUpdateCount == 0)
currentStatus = "All installed mods are already up to date !";
else if (failedUpdates.Count > 0)
currentStatus = $"{failedUpdates.Count} mods failed to update ({toUpdateCount - failedUpdates.Count}/{toUpdateCount} succeeded)";
else
currentStatus = "Sucessfully updated " + toUpdateCount + " mods !";
Thread.Sleep((int)(postUpdateDisplayDuration * 1000));
}
private static void FetchRemoteMods()
{
string apiResponse;
using (var client = new WebClient())
{
client.Headers.Add("User-Agent", APIConstants.USER_AGENT);
apiResponse = client.DownloadString(APIConstants.MODS_ENDPOINT);
}
APIMod[] apiMods = JsonConvert.DeserializeObject<APIMod[]>(apiResponse);
remoteMods.Clear();
int verifiedModsCount = 0;
foreach (APIMod mod in apiMods)
{
if (mod.versions.Length == 0)
continue;
APIModVersion versionDetails = mod.versions[0];
// Aliases
foreach (string alias in mod.aliases)
if (alias != versionDetails.name)
oldToNewModNames[alias] = versionDetails.name;
// Add to known mods
remoteMods.Add(versionDetails.name, new ModDetail(versionDetails.name, versionDetails.modVersion, versionDetails.downloadLink, versionDetails.hash, versionDetails.approvalStatus));
if (versionDetails.approvalStatus == 1)
verifiedModsCount++;
}
MelonLogger.Msg("API returned " + apiMods.Length + " mods, including " + verifiedModsCount + " verified mods");
}
private static void ScanModFolder()
{
var list = new[] { installedMods, brokenMods };
var runOnce = false;
var allDepList = new List<string>();
var allOptDepList = new List<string>();
foreach (var dict in list)
{ //This is a kinda dirty way to do this
dict.Clear();
string modsFolder = MelonHandler.ModsDirectory;
string desktopFolder = $"{modsFolder}/Desktop";
string vrFolder = $"{modsFolder}/VR";
string[] folders = new string[3]{ modsFolder,desktopFolder,vrFolder};
foreach (string folder in folders)
{
string basedirectory = !runOnce ? folder : folder + "/Broken";
if (!Directory.Exists(basedirectory))
continue;
string[] dlltbl = Directory.GetFiles(basedirectory, "*.dll");
if (dlltbl.Length > 0)
{
for (int i = 0; i < dlltbl.Length; i++)
{
string filename = dlltbl[i];
if (string.IsNullOrEmpty(filename))
continue;
if (filename.EndsWith(".dev.dll"))
continue;
try
{
string modName;
string modVersion;
string[] optDependencies = new string[0];
List<string> depList = new List<string>();
using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(filename, new ReaderParameters { ReadWrite = true }))
{
CustomAttribute melonInfoAttribute = assembly.CustomAttributes.FirstOrDefault(a =>
a.AttributeType.Name == "MelonModInfoAttribute" || a.AttributeType.Name == "MelonInfoAttribute");
if (melonInfoAttribute == null)
continue;
modName = melonInfoAttribute.ConstructorArguments[1].Value as string;
modVersion = melonInfoAttribute.ConstructorArguments[2].Value as string;
if (resolveDependencies.Value)
{
var optionals = new List<string>();
CustomAttribute melonOptDepend = assembly.CustomAttributes.FirstOrDefault(a =>
a.AttributeType.Name == "MelonOptionalDependenciesAttribute");
if (melonOptDepend != null)
{
foreach (var item in melonOptDepend.ConstructorArguments[0].Value as IEnumerable<CustomAttributeArgument>)
{
optionals.Add(item.Value.ToString());
}
}
var references = assembly.MainModule.AssemblyReferences;
foreach (var dependency in references)
{
var depName = GetNewModName(dependency.Name);
if (remoteMods.ContainsKey(depName))
{//If dep is in RemoteAPI
if (!optionals?.Contains(dependency.Name) ?? true) //Sum up all the deps
{
if (!allDepList.Contains(depName)) allDepList.Add(depName);
}
else
{
if (!allDepList.Contains(depName) && !allOptDepList.Contains(depName)) allOptDepList.Add(depName);
}
if (resolveOptionalDependencies.Value || (!optionals?.Contains(dependency.Name) ?? true))
{//If we want to add optional deps OR it's not in the optional dep list)
depList.Add(depName);
}
}
}
}
}
modName = GetNewModName(modName); // Backward mod compatibility
MelonLogger.Msg($"Found {(!runOnce ? "" : "broken ")}mod: " + modName);
if (dict.TryGetValue(modName, out ModDetail installedModDetail))
{
if (VersionUtils.CompareVersion(installedModDetail.version, modVersion) > 0)
{
File.Delete(filename); // Delete duplicated mods
MelonLogger.Msg("Deleted duplicated mod " + modName);
}
else
{
File.Delete(installedModDetail.filepath); // Delete duplicated mods
MelonLogger.Msg("Deleted duplicated mod " + modName);
dict[modName] = new ModDetail(modName, modVersion, filename, depList.ToArray(), optDependencies);
}
continue;
}
dict.Add(modName, new ModDetail(modName, modVersion, filename, depList.ToArray(), optDependencies));
}
catch (Exception ex)
{
MelonLogger.Msg("Failed to read assembly " + filename + "\n" + ex.ToString());
}
}
}
}
MelonLogger.Msg(ConsoleColor.DarkCyan, "Found " + dict.Count + " unique non-dev mods " + $"{(!runOnce ? "installed" : "in Broken folder")}");
runOnce = true;
}
if (resolveDependencies.Value)
{ //Just some stats
string depString, optDepString;
if (allDepList.Count > 0)
depString = string.Join(", ", allDepList);
else
depString = "N/A";
if (allOptDepList.Count > 0)
optDepString = string.Join(", ", allOptDepList);
else
optDepString = "N/A";
MelonLogger.Msg(ConsoleColor.DarkCyan, $"Found {allDepList.Count} required dependencies: '{depString}' " +
$"and {allOptDepList.Count} optional dependencies: '{optDepString}' used by the mods in your mod folders.\n" +
$"{(resolveOptionalDependencies.Value ? "Both Required and Optional dependencies" : "Only Required dependencies")} will be downloaded, you can change this in Mod Settings");
}
}
private static string GetNewModName(string currentName)
{
return oldToNewModNames.TryGetValue(currentName, out string newName) ? newName : currentName;
}
private static string ComputeSha256Hash(byte[] rawData)
{
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(rawData);
return Convert.ToBase64String(bytes);
}
}
private static void DownloadAndUpdateMods()
{
Dictionary<string, ModDetail> toMods = new Dictionary<string, ModDetail>();
Dictionary<string, ModDetail> toBroken = new Dictionary<string, ModDetail>();
Dictionary<string, ModDetail> toUpdate = new Dictionary<string, ModDetail>();
Dictionary<string, ModDetail> checkDeps = new Dictionary<string, ModDetail>();
List<string> updateList = new List<string>();
List<string> depList = new List<string>();
List<string> toBrokenList = new List<string>();
List<string> fromBrokenList = new List<string>();
// Check for broken mods with updates
foreach (KeyValuePair<string, ModDetail> brokenMod in brokenMods)
{
if (remoteMods.TryGetValue(brokenMod.Key, out ModDetail remoteMod))
{
if (remoteMod.approvalStatus != 1)
continue;
VersionUtils.VersionData brokenModVersion = VersionUtils.GetVersion(brokenMod.Value.version);
VersionUtils.VersionData remoteModVersion = VersionUtils.GetVersion(remoteMod.version);
int compareResult = VersionUtils.CompareVersion(remoteModVersion, brokenModVersion);
MelonLogger.Msg("(Broken Mod: " + remoteMod.name + ") version compare between [remote] " + remoteMod.version + " and [local] " + brokenMod.Value.version + ": " + compareResult);
if (compareResult > 0) // Don't move from broken unless new version > old
{
toMods.Add(brokenMod.Key, new ModDetail(brokenMod.Key, brokenMod.Value.version, brokenMod.Value.filepath, remoteMod.downloadUrl, remoteMod.hash));
checkDeps.Add(brokenMod.Key, brokenMod.Value);
fromBrokenList.Add(" " + brokenMod.Key);
}
continue;
}
}
// List all installed mods that can be updated or moved to broken
foreach (KeyValuePair<string, ModDetail> installedMod in installedMods)
{
if (remoteMods.TryGetValue(installedMod.Key, out ModDetail remoteMod))
{
VersionUtils.VersionData installedModVersion = VersionUtils.GetVersion(installedMod.Value.version);
VersionUtils.VersionData remoteModVersion = VersionUtils.GetVersion(remoteMod.version);
int compareResult = VersionUtils.CompareVersion(remoteModVersion, installedModVersion);
if (remoteMod.approvalStatus != 1)
{
MelonLogger.Msg($"(Mod: {installedMod.Key}) Remote Approval Status is: Broken/Other - {remoteMod.approvalStatus}");
if (compareResult >= 0) // Don't move to broken if local version is higher than remote
{
toBroken.Add(installedMod.Key, new ModDetail(installedMod.Key, installedMod.Value.version, installedMod.Value.filepath));
toBrokenList.Add(" " + installedMod.Key);
}
else
MelonLogger.Msg($"Ignoring as local version is higer than remote. Remote: {remoteMod.version}, Local: {installedMod.Value.version}");
continue;
}
MelonLogger.Msg("(Mod: " + remoteMod.name + ") version compare between [remote] " + remoteMod.version + " and [local] " + installedMod.Value.version + ": " + compareResult);
if (compareResult > 0)
{
toUpdate.Add(installedMod.Key, new ModDetail(installedMod.Key, installedMod.Value.version, installedMod.Value.filepath, remoteMod.downloadUrl, remoteMod.hash));
updateList.Add(" " + installedMod.Key);
}
if (!checkDeps.ContainsKey(installedMod.Key)) checkDeps.Add(installedMod.Key, installedMod.Value);
continue;
}
}
MelonLogger.Msg(ConsoleColor.DarkCyan, "Found " + toUpdate.Count + " outdated mods | " + toBroken.Count + " broken mods | " + toMods.Count + " fixed mods");
if (resolveDependencies.Value)
{
foreach (var modtoCheck in checkDeps)
{//Check all installed mods
try
{
foreach (var dep in modtoCheck.Value.dependencies)
{
if (!installedMods.ContainsKey(dep) && !toMods.ContainsKey(dep) && !toUpdate.ContainsKey(dep) && !brokenMods.ContainsKey(dep))
{//If dep is not in Installed Mods, & it is not being added to Mods from Broken, & it isn't already in toUpdate, & it isn't in the broken mods folder
//Dont want to install it twice... or more
if (remoteMods.TryGetValue(dep, out ModDetail remoteDep))
{
if (remoteDep.approvalStatus != 1) //Dont add broken deps
continue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteDep.downloadUrl); //Get file name, this is needed after api v1 switch to using redirects
request.AllowAutoRedirect = false;
request.UserAgent = APIConstants.USER_AGENT;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string redirUrl = response.Headers["Location"];
response.Close();
var newFileName = redirUrl.Split('/').Last();
var newPath = "Mods/" + newFileName;
MelonLogger.Warning($"Adding Dependency '{newPath}' for Mod '{modtoCheck.Value.name}'");
toUpdate.Add(remoteDep.name, new ModDetail(remoteDep.name, remoteDep.version, newPath, remoteDep.downloadUrl, remoteDep.hash));
depList.Add(" " + remoteDep.name);
}
}
}
}
catch (Exception ex)
{
MelonLogger.Error("Error adding dependancies for " + modtoCheck + "\n" + ex.ToString());
}
}
}
if (popUpMsg.Value && ((updateList.Count > 0) || (depList.Count > 0) || (fromBrokenList.Count > 0) || (toBrokenList.Count > 0)))
{
msg =
(updateList.Count > 0 ? "Downloading updates for:\n" + string.Join("\n", updateList) + "\n" : "") +
(depList.Count > 0 ? "Adding the following mod dependencies:\n" + string.Join("\n", depList) + "\n" : "") +
(fromBrokenList.Count > 0 ? "Moving from the broken folder:\n" + string.Join("\n", fromBrokenList) + "\n" : "") +
(toBrokenList.Count > 0 ? "Moving to the broken folder:\n" + string.Join("\n", toBrokenList) : "");
new Thread(new ThreadStart(SpawnMessage)).Start();
}
if (toFromBroken.Value)
{
foreach (KeyValuePair<string, ModDetail> mod in toMods)
{
MelonLogger.Warning(mod.Value.name + " - Moving to Mods from Broken folder");
try
{
if (File.Exists(mod.Value.filepath))
{
var newDir = Directory.GetParent(Path.GetDirectoryName(mod.Value.filepath)) + "/" + Path.GetFileName(mod.Value.filepath);
toUpdate.Add(mod.Key, new ModDetail(mod.Key, mod.Value.version, newDir, mod.Value.downloadUrl, mod.Value.hash));
File.Delete(mod.Value.filepath);
}
}
catch (Exception e)
{
MelonLogger.Error("Failed to updated fixed mod" + mod.Value.filepath + ":\n" + e);
}
}
foreach (KeyValuePair<string, ModDetail> mod in toBroken)
{
{
MelonLogger.Warning(mod.Value.name + " - Moving to Broken folder");
try
{
if (File.Exists(mod.Value.filepath))
{
var newDir = Path.GetDirectoryName(mod.Value.filepath) + "/Broken";
if (!Directory.Exists(newDir))
Directory.CreateDirectory(newDir);
var newFilePath = newDir + "/" + Path.GetFileName(mod.Value.filepath);
if (!File.Exists(newFilePath))
File.Move(mod.Value.filepath, newFilePath);
else
File.Delete(mod.Value.filepath);
}
}
catch (Exception e)
{
MelonLogger.Error("Failed to move mod to broken folder" + mod.Value.filepath + ":\n" + e);
}
}
}
toUpdateCount = toUpdate.Count;
int i = 0;
foreach (KeyValuePair<string, ModDetail> mod in toUpdate)
{
MelonLogger.Msg("Updating " + mod.Value.name);
progressTotal = (int)(i / (double)toUpdateCount * 100);
currentStatus = $"Updating {mod.Value.name} ({i + 1} / {toUpdateCount})...";
try
{
bool errored = false;
using (var client = new WebClient())
{
client.Headers.Add("User-Agent", APIConstants.USER_AGENT);
bool downloading = true;
byte[] downloadedFileData = null;
client.DownloadDataCompleted += (sender, e) =>
{
if (e.Error != null)
{
MelonLogger.Error("Failed to update " + mod.Value.name + ":\n" + e.Error);
errored = true;
failedUpdates.Add(new FailedUpdateInfo(mod.Value, FailedUpdateReason.DownloadError, e.ToString()));
}
else
downloadedFileData = e.Result;
progressDownload = 100;
downloading = false;
};
client.DownloadProgressChanged += (sender, e) =>
{
progressDownload = e.ProgressPercentage;
};
client.DownloadDataAsync(new Uri(mod.Value.downloadUrl));
while (downloading)
Thread.Sleep(50);
if (!errored)
{
string downloadedHash = ComputeSha256Hash(downloadedFileData);
MelonLogger.Msg("Downloaded file hash: " + downloadedHash);
if (downloadedHash == mod.Value.hash)
{
try
{
File.WriteAllBytes(mod.Value.filepath, downloadedFileData);
}
catch (Exception e)
{
MelonLogger.Error("Failed to save " + mod.Value.filepath + ":\n" + e);
failedUpdates.Add(new FailedUpdateInfo(mod.Value, FailedUpdateReason.SaveError, e.ToString()));
}
}
else
{
MelonLogger.Error("Downloaded file hash mismatches database hash!");
failedUpdates.Add(new FailedUpdateInfo(mod.Value, FailedUpdateReason.HashMismatch, $"Expected hash: {mod.Value.hash}, Downloaded file hash: {downloadedHash}"));
}
}
}
}
catch (Exception e)
{
MelonLogger.Error("Failed to update " + mod.Value.filepath + ":\n" + e);
failedUpdates.Add(new FailedUpdateInfo(mod.Value, FailedUpdateReason.Unknown, e.ToString()));
}
progressTotal = (int)((i + 1) / (double)toUpdateCount * 100);
MelonLogger.Msg($"Progress: {i + 1}/{toUpdateCount} -> {progressTotal}%");
i++;
}
}
}
private static void SpawnMessage()
{
if (msg != null)
MessageBox.Show(new Form { TopMost = true }, msg, "VRCModUpdater");
}
}
}