Skip to content

Commit

Permalink
Cleaned up async flow
Browse files Browse the repository at this point in the history
  • Loading branch information
GoobyCorp committed Feb 5, 2024
1 parent 89ae7c8 commit 94d9304
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
47 changes: 31 additions & 16 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ internal static class Program {
internal const string DOWNLOAD_DIR = "Downloads";
internal const string MODS_DIR = "Mods";
internal const string MODPACK_FILE = "modpack.zip";
internal const string PLUGIN_PREFIX = "BepInEx/plugins/";

static async Task<int> Main(string[] args) {
if(!File.Exists(CONFIG_FILE))
File.WriteAllText(CONFIG_FILE, "[]");

#if DEBUG
Console.WriteLine("DEBUG - Removing old directories...");

if (Directory.Exists(DOWNLOAD_DIR))
Directory.Delete(DOWNLOAD_DIR, true);

if (Directory.Exists(MODS_DIR))
Directory.Delete(MODS_DIR, true);

if (File.Exists(MODPACK_FILE))
File.Delete(MODPACK_FILE);
#endif

if (!Directory.Exists(DOWNLOAD_DIR))
Directory.CreateDirectory(DOWNLOAD_DIR);

Expand All @@ -27,13 +41,14 @@ static async Task<int> Main(string[] args) {
}

Console.WriteLine("Fetching download links and files...");
Task[] t = entries.ToList().Select((e) => FindDownloadExtractPackage(e.Creator, e.Mod)).ToArray();
await Task.WhenAll(t);
List<Task> tl = entries.ToList().Select((e) => FindDownloadExtractPackage(e.Creator, e.Mod)).ToList();
await Task.WhenAll(tl);

Console.WriteLine("Creating modpack...");
using (var fs = File.Open(MODPACK_FILE, FileMode.Create, FileAccess.Write, FileShare.None))
ZipFile.CreateFromDirectory(MODS_DIR, fs, CompressionLevel.Optimal, false);
await CreateModPackageAsync(MODPACK_FILE);

Console.WriteLine("Done!");

Finished:
#if DEBUG
Console.WriteLine("Press ENTER to exit...");
Expand All @@ -43,21 +58,21 @@ static async Task<int> Main(string[] args) {
}

internal static async Task FindDownloadExtractPackage(string creator, string mod) {
await ThunderstoreAPI.PackageExperimental(creator, mod).ContinueWith(async (e0) => {
var pe = e0.Result;
string path = Path.Combine(DOWNLOAD_DIR, pe.Latest.FullName + ".zip");
Console.WriteLine($"Downloading \"{pe.Namespace}/{pe.Name}\" version \"{pe.Latest.VersionNumber}\" to \"{path}\"...");
await Utilities.DownloadFileAsync(pe.Latest.DownloadUrl, path).ContinueWith(async (e1) => {
await ExtractPackageAsync(path).ContinueWith(async (e2) => {
return e2;
});
});
});
var pe = await ThunderstoreAPI.PackageExperimental(creator, mod);
string path = Path.Combine(DOWNLOAD_DIR, pe.Latest.FullName + ".zip");
Console.WriteLine($"Downloading \"{pe.Namespace}/{pe.Name}\" version \"{pe.Latest.VersionNumber}\" to \"{path}\"...");
await Utilities.DownloadFileAsync(pe.Latest.DownloadUrl, path);
await ExtractModPackageAsync(path);
}

internal static async Task ExtractModPackageAsync(string path) {
await Utilities.ExtractZipEntriesWithPrefix(path, PLUGIN_PREFIX, MODS_DIR);
}

internal static async Task ExtractPackageAsync(string path) {
internal static async Task CreateModPackageAsync(string path) {
await Task.Run(() => {
Utilities.ExtractZipEntriesWithPrefix(path, "BepInEx/plugins/", MODS_DIR);
using (var fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
ZipFile.CreateFromDirectory(MODS_DIR, fs, CompressionLevel.Optimal, false);
});
}
}
Expand Down
15 changes: 8 additions & 7 deletions Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ThisCompanyIsGettingLethal
{
internal class Utilities
{
internal static void ExtractZipEntriesWithPrefix(string zipPath, string prefix, string outPath) {
internal static async Task ExtractZipEntriesWithPrefix(string zipPath, string prefix, string outPath) {
using (ZipArchive archive = ZipFile.OpenRead(zipPath)) {
foreach (var entry in archive.Entries) {
if (String.IsNullOrEmpty(entry.Name))
Expand All @@ -21,17 +21,18 @@ internal static void ExtractZipEntriesWithPrefix(string zipPath, string prefix,
if (!Directory.Exists(finalDir))
Directory.CreateDirectory(finalDir);

entry.ExtractToFile(finalPath, true);
using (var fs = new FileStream(finalPath, FileMode.Create, FileAccess.Write, FileShare.None))
using (Stream zs = entry.Open())
await zs.CopyToAsync(fs);
}
}
}

internal static async Task DownloadFileAsync(string url, string path) {
using (var hc = new HttpClient()) {
var httpStream = await hc.GetStreamAsync(url);
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
await httpStream.CopyToAsync(fs, 8192);
}
using (var hc = new HttpClient())
using (var hs = await hc.GetStreamAsync(url))
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
await hs.CopyToAsync(fs, 8192);
}
}
}

0 comments on commit 94d9304

Please sign in to comment.