This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
/
Program.cs
413 lines (345 loc) · 20.7 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Diagnostics;
using Newtonsoft.Json;
using LibGetDownloader;
using SDSetupCommon;
using Octokit;
using ICSharpCode.SharpZipLib.Zip;
/*
* Forewarning:
* This tool is designed specifically for SDSetup. While you could feasibly
* use the frontend and backend for any sort of package management where files
* need to be joined into a single ZIP, this updater tool is specially designed
* to update Nintendo Switch homebrew packages on SDSetup and nothing else.
*
* Also the code sucks and is not dynamic in any way.
*
*/
namespace SDSetupUpdater {
class Program {
static GitHubClient ghClient;
static List<string> FinalOutput = new List<string>();
static Config config;
static List<string> OutdatedPackagesLibGet = new List<string>();
static Dictionary<string, string> OutdatedPackagesKosmos = new Dictionary<string, string>();
static bool KosmosOutdated = false;
static Dictionary<string, SDSetupCommon.Package> SDPackages = new Dictionary<string, SDSetupCommon.Package>();
static string nPackagesetDirectory = null;
static bool newPackagesetCreated = false;
static void Main(string[] args) {
try {
//start arg and config validation
Options o = new Options();
Parser.Default.ParseArguments<Options>(args).WithParsed<Options>(opts => o = opts);
if (!File.Exists("config.json")) {
Log("File config.json not found. Make sure it exists!");
ExitProcedure(1);
}
if (!Directory.Exists(o.Directory)) {
Log($"Directory {new DirectoryInfo(o.Directory).FullName} doesn't exist!");
ExitProcedure(1);
}
string oPackagesetDirectory = new DirectoryInfo(Path.Join(o.Directory, o.Packageset)).FullName;
if (!Directory.Exists(oPackagesetDirectory)) {
Log($"Packageset directory {new DirectoryInfo(oPackagesetDirectory).FullName} doesn't exist!");
ExitProcedure(1);
}
//end arg and config validation
DateTime startTime = DateTime.UtcNow;
config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
Log("SDSetup Updater Started - " + startTime.ToLongTimeString() + " " + startTime.ToLongDateString());
if (o.DryRun) Log("NOTE: Dry run is enabled!");
if (config.OldPackagesets?.Count > 0) {
Log("============================================================");
Log("");
if (Directory.EnumerateDirectories(o.Directory).Count() <= 1) {
Log("Not cleaning old packagesets, there is only one in the files directory!");
} else {
Log("Cleaning old packagesets");
foreach (string k in config.OldPackagesets) {
if (k == o.Packageset) {
Log($"Skipping removal of packageset {k} as it is the one in use!");
}
Log($"Removing packageset {k}");
Directory.Delete(new DirectoryInfo(Path.Join(o.Directory, k)).FullName, true);
}
}
config.OldPackagesets = new List<string>();
Log("");
}
Log("============================================================");
Log("");
Log("Downloading latest Kosmos update script...");
string kosmosScriptPath = new FileInfo(config.KosmosUpdaterScriptPath).Directory.FullName;
Directory.Delete(kosmosScriptPath, true);
Directory.CreateDirectory(kosmosScriptPath);
using (var client = new HttpClient()) {
File.WriteAllBytes(Path.Join(kosmosScriptPath, "master.zip"), client.GetByteArrayAsync(config.KosmosMasterUrl).Result);
FastZip zip = new FastZip();
zip.ExtractZip(Path.Join(kosmosScriptPath, "master.zip"), kosmosScriptPath, null);
File.Delete(Path.Join(kosmosScriptPath, "master.zip"));
}
string scriptMasterFolder = Directory.EnumerateDirectories(kosmosScriptPath).First();
foreach (string k in Directory.EnumerateFiles(scriptMasterFolder)) {
FileInfo f = new FileInfo(k);
if (f.Name.StartsWith('.')) continue;
File.Move(f.FullName, Path.Join(f.Directory.Parent.FullName, f.Name));
}
foreach (string k in Directory.EnumerateDirectories(scriptMasterFolder)) {
DirectoryInfo f = new DirectoryInfo(k);
if (f.Name.StartsWith('.')) continue;
U.DirectoryCopy(f.FullName, Path.Join(f.Parent.Parent.FullName, f.Name), true);
}
Directory.Delete(scriptMasterFolder, true);
("chmod -R 775 " + kosmosScriptPath).ExecuteAsBash();
Log("Done!");
Log("");
Log("============================================================");
Log($"Using libget repo '{config.LibGetRepo}'");
Log($"Using SDSetup packageset '{o.Packageset}'");
try {
ghClient = new GitHubClient(new Octokit.ProductHeaderValue("sdsetup"));
Credentials credentials = new Credentials(config.GithubUsername, config.GithubAuthToken);
} catch (Exception e) {
Log($"An exception occurred: {e.GetType().ToString()}\n{e.Message}\nStack Trace:\n{e.StackTrace}");
ExitProcedure(1);
return;
}
Log($"Github authenticated successfully ({ghClient.Miscellaneous.GetRateLimits().Result.Resources.Core.Remaining})");
Log("============================================================");
Repo repo;
try {
repo = Repo.GetRepo(config.LibGetRepo);
} catch (Exception e) {
Log($"An exception occurred: {e.GetType().ToString()}\n{e.Message}\nStack Trace:\n{e.StackTrace}");
ExitProcedure(1);
return;
}
Log("");
Log("------------------------- LibGet -------------------------");
foreach (string _k in Directory.EnumerateDirectories(oPackagesetDirectory)) {
string k = new DirectoryInfo(_k).Name;
string packageDirectory = new DirectoryInfo(Path.Join(oPackagesetDirectory, k)).FullName;
SDPackages[k] = JsonConvert.DeserializeObject<SDSetupCommon.Package>(File.ReadAllText(Path.Join(packageDirectory, "info.json")));
}
foreach (SDSetupCommon.Package sdPackage in SDPackages.Values) {
if (sdPackage.AutoUpdateType == AutoUpdateType.LibGet) {
if (!repo.PackageExists(sdPackage.AutoUpdateHint)) {
Log($"**[Warning]** libget package named '{sdPackage.AutoUpdateHint}' for SDSetup package {sdPackage.ID} doesn't exist. Skipping...");
continue;
}
LibGetDownloader.Package lgPackage = repo.GetPackage(sdPackage.AutoUpdateHint);
if (sdPackage.Versions["latest"] != lgPackage.Version && sdPackage.Versions["latest"] != "v" + lgPackage.Version) {
Log($"**[Update Detected]** '{sdPackage.ID}' {sdPackage.Versions["latest"]} -> {lgPackage.Version}");
OutdatedPackagesLibGet.Add(sdPackage.ID);
}
}
}
if (OutdatedPackagesLibGet.Count == 0) {
Log("LibGet packages are already up-to-date!");
}
Log("");
Log("------------------------- Kosmos -------------------------");
string latestKosmos = ghClient.Repository.Release.GetLatest(config.KosmosRepositoryId).Result.TagName;
if (latestKosmos == SDPackages["atmos_musthave"].Versions["latest"]) {
Log($"Kosmos is already up-to-date ({latestKosmos})!");
} else {
KosmosOutdated = true;
Log($"Kosmos is outdated! Running auto script...");
Dictionary<string, string> kosmos = RunKosmosAutoScript();
foreach (SDSetupCommon.Package sdPackage in SDPackages.Values) {
if (sdPackage.AutoUpdateType == AutoUpdateType.Kosmos) {
//musthave needs special handling because im too lazy to fix my setup for how Atmosphere is packaged by SDSetup.
if (sdPackage.ID == "atmos_musthave") {
Log($"**[Update Detected]** '{sdPackage.ID}' {sdPackage.Versions["latest"]} -> {latestKosmos}");
OutdatedPackagesKosmos[sdPackage.ID] = kosmos[sdPackage.AutoUpdateHint];
continue;
}
if (!kosmos.ContainsKey(sdPackage.AutoUpdateHint)) {
Log($"**[Warning]** Kosmos auto package output named '{sdPackage.AutoUpdateHint}' for SDSetup package {sdPackage.ID} doesn't exist. Skipping...");
continue;
}
if (kosmos[sdPackage.AutoUpdateHint] != sdPackage.Versions["latest"]) {
Log($"**[Update Detected]** '{sdPackage.ID}' {sdPackage.Versions["latest"]} -> {kosmos[sdPackage.AutoUpdateHint]}");
OutdatedPackagesKosmos[sdPackage.ID] = kosmos[sdPackage.AutoUpdateHint];
}
}
}
}
Log("");
Log("============================================================");
Log("");
if (o.DryRun) {
Log("Dry run completed.");
ExitProcedure(0);
return;
}
string newPackageset = "auto" + (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
nPackagesetDirectory = new DirectoryInfo(Path.Join(o.Directory, newPackageset)).FullName;
if (!(OutdatedPackagesLibGet.Count > 0 || KosmosOutdated)) {
Log("No outdated packages detected. Process complete.");
ExitProcedure(0);
return;
}
Log($"Detected outdated packages. Cloning packageset '{o.Packageset}' to '{newPackageset}'");
newPackagesetCreated = true;
U.DirectoryCopy(oPackagesetDirectory, nPackagesetDirectory, true);
if (OutdatedPackagesLibGet.Count > 0) {
Log("");
Log("============================================================");
Log("");
Log($"Detected {OutdatedPackagesLibGet.Count} outdated libget packages. Updating...");
foreach (string k in OutdatedPackagesLibGet) {
string packageFilesDirectory = Path.Join(nPackagesetDirectory, k, "latest", "sd");
Directory.Delete(packageFilesDirectory, true);
Directory.CreateDirectory(packageFilesDirectory);
repo.DownloadPackageToDisk(SDPackages[k].AutoUpdateHint, packageFilesDirectory, true);
if (File.Exists(Path.Join(packageFilesDirectory, "info.json"))) File.Delete(Path.Join(packageFilesDirectory, "info.json"));
if (File.Exists(Path.Join(packageFilesDirectory, "manifest.install"))) File.Delete(Path.Join(packageFilesDirectory, "manifest.install"));
string oVersion = SDPackages[k].Versions["latest"];
string nVersion;
string lgVersion = repo.GetPackage(SDPackages[k].AutoUpdateHint).Version;
if (Char.IsNumber(lgVersion[0])) nVersion = "v" + lgVersion;
else nVersion = lgVersion;
SDPackages[k].Versions["latest"] = nVersion;
File.WriteAllText(Path.Join(nPackagesetDirectory, k, "info.json"), JsonConvert.SerializeObject(SDPackages[k], Formatting.Indented));
Log($"**[Package Updated]** '{k}' {oVersion} -> {nVersion}");
}
}
if (KosmosOutdated) {
Log("");
Log("============================================================");
Log("");
Log($"Detected {OutdatedPackagesKosmos.Count} outdated Kosmos packages. Updating...");
foreach (string k in OutdatedPackagesKosmos.Keys) {
string kosmosAutoPackageDirectory = Path.Join(new FileInfo(config.KosmosUpdaterScriptPath).Directory.FullName, "out", SDPackages[k].AutoUpdateHint);
string packageFilesDirectory = Path.Join(nPackagesetDirectory, k, "latest", "sd");
Directory.Delete(packageFilesDirectory, true);
U.DirectoryCopy(kosmosAutoPackageDirectory, packageFilesDirectory, true);
string oVersion = SDPackages[k].Versions["latest"];
string nVersion;
if (SDPackages[k].ID == "atmos_musthave") {
nVersion = latestKosmos;
} else {
nVersion = OutdatedPackagesKosmos[k];
}
SDPackages[k].Versions["latest"] = nVersion;
File.WriteAllText(Path.Join(nPackagesetDirectory, k, "info.json"), JsonConvert.SerializeObject(SDPackages[k], Formatting.Indented));
Log($"**[Package Updated]** '{k}' {oVersion} -> {nVersion}");
}
//update public facing "atmosphere" package with new atmos/kosmos version.
SDPackages["atmosphere"].Versions["latest"] = $"{OutdatedPackagesKosmos["atmos_musthave"]}/{latestKosmos}";
File.WriteAllText(Path.Join(nPackagesetDirectory, "atmosphere", "info.json"), JsonConvert.SerializeObject(SDPackages["atmosphere"], Formatting.Indented));
}
if (!String.IsNullOrWhiteSpace(o.PrivilegedUUID)) {
Log("");
Log("============================================================");
Log("");
Log($"Reloading backend {config.BackendHostname}");
UpdateBackend(o.PrivilegedUUID, newPackageset);
config.OldPackagesets?.Add(o.Packageset);
File.WriteAllText("config.json", JsonConvert.SerializeObject(config, Formatting.Indented));
Log($"Flagged packageset {o.Packageset} for deletion on next run.");
}
Log("");
Log("============================================================");
Log("");
Log($"Done! in {DateTime.UtcNow.Subtract(startTime).TotalSeconds}s");
ExitProcedure(0);
return;
} catch (Exception e) {
Log($"An exception occurred: {e.GetType().ToString()}\n{e.Message}\nStack Trace:\n{e.StackTrace}");
try {
if (newPackagesetCreated && !String.IsNullOrWhiteSpace(nPackagesetDirectory) && Directory.Exists(nPackagesetDirectory)) {
Directory.Delete(nPackagesetDirectory, true);
}
} catch (Exception e2) {
Log($"When cleaning up the new packageset directory, an exception occurred: {e2.GetType().ToString()}\n{e2.Message}\nStack Trace:\n{e2.StackTrace}");
}
ExitProcedure(1);
return;
}
}
static void Log(string message) {
Console.WriteLine(message);
FinalOutput.Add(message);
}
static void ExitProcedure(int code) {
if (!String.IsNullOrEmpty(config.WebhookEndpoint)) {
using (HttpClient client = new HttpClient()) {
for (int i = 0; i < FinalOutput.Count; i+= 40) {
List<string> finalOutputRange = new List<string>();
foreach(string k in FinalOutput.GetRange(i, Math.Min(40, FinalOutput.Count - i))) {
finalOutputRange.Add(k.Replace("\\", "/"));
}
string contentString = String.Join("\\n", finalOutputRange);
HttpContent content = new StringContent("{\"username\": \"SDSetup Updater\", \"content\": \"" + contentString +"\"}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
client.PostAsync(config.WebhookEndpoint, content).Wait();
}
}
}
Environment.Exit(code);
}
static bool UpdateBackend(string uuid, string packageSet) {
using (HttpClient client = new HttpClient()) {
HttpResponseMessage resp = client.GetAsync($"{config.BackendHostname}/api/v1/admin/reloadall/{uuid}").Result;
if (!resp.IsSuccessStatusCode) {
Log($"**[Error]** Failed to reload backend manifests! ({resp.StatusCode})");
Log($"{resp.Content.ReadAsStringAsync().Result}");
return false;
}
resp = client.GetAsync($"{config.BackendHostname}/api/v1/set/latestpackageset/{uuid}/{packageSet}").Result;
if (!resp.IsSuccessStatusCode) {
Log($"**[Error]** Failed to set latest package set! ({resp.StatusCode})");
Log($"{resp.Content.ReadAsStringAsync().Result}");
return false;
}
return true;
}
}
static Dictionary<string, string> RunKosmosAutoScript() {
string scriptDirectory = new FileInfo(config.KosmosUpdaterScriptPath).Directory.FullName;
string scriptPath = new FileInfo(config.KosmosUpdaterScriptPath).FullName;
if (Directory.Exists(Path.Join(scriptDirectory, "out"))) {
Directory.Delete(Path.Join(scriptDirectory, "out"), true);
}
Process process = new Process {
StartInfo = new ProcessStartInfo {
FileName = scriptPath,
Arguments = $"{Path.Join(scriptDirectory, "out")} {config.GithubUsername} {config.GithubAuthToken} auto",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = scriptDirectory
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Dictionary<string, string> versions = new Dictionary<string, string>();
foreach (string k in result.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) {
string version = k.Split(':')[1];
if (Char.IsNumber(version[0])) version = "v" + version;
versions[k.Split(':')[0]] = version;
}
return versions;
}
}
class Options {
[Option('d', "directory", Required = true, HelpText = "SDSetup \"files\" directory containing packagesets.")]
public string Directory { get; set; }
[Option('p', "packageset", Required = true, HelpText = "Packageset that needs updating.")]
public string Packageset { get; set; }
[Option('u', "uuid", HelpText = "Privileged UUID used to reload the backend. If not specified, backend is not reloaded.")]
public string PrivilegedUUID { get; set; }
[Option("dryrun", HelpText = "Perform update procedures without changing any files. For testing purposes.", Default = false)]
public bool DryRun { get; set; }
}
}