Skip to content

Commit

Permalink
Moved some functions around
Browse files Browse the repository at this point in the history
  • Loading branch information
GoobyCorp committed Feb 2, 2024
1 parent 161e6d1 commit 6df4fce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ static int Main(string[] args) {
}

internal static void FindDownloadExtractPackage(string creator, string mod) {
Task<PackageExperimental> t0 = ThunderstoreAPI.Package(creator, mod);
Task<PackageExperimental> t0 = ThunderstoreAPI.PackageExperimental(creator, mod);
t0.Wait();
var pe = t0.Result;
string path = Path.Combine(DOWNLOAD_DIR, pe.Latest.FullName + ".zip");

Console.WriteLine($"Downloading \"{pe.Namespace}/{pe.Name}\" version \"{pe.Latest.VersionNumber}\" to \"{path}\"...");

Task t1 = ThunderstoreAPI.Download(pe.Latest.DownloadUrl, path);
Task t1 = Utilities.DownloadFile(pe.Latest.DownloadUrl, path);
t1.Wait();
Task t2 = Task.Run(() => {
Utilities.ExtractZipEntriesWithPrefix(path, "BepInEx/plugins/", MODS_DIR);
Expand Down
14 changes: 4 additions & 10 deletions ThunderstoreAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@ namespace ThisCompanyIsGettingLethal
{
internal class ThunderstoreAPI
{
internal static async Task<PackageExperimental> Package(string creator, string mod) {
internal const string API_BASE = "https://thunderstore.io";

internal static async Task<PackageExperimental> PackageExperimental(string creator, string mod) {
using (var hc = new HttpClient()) {
var pe = await hc.GetFromJsonAsync<PackageExperimental>($"https://thunderstore.io/api/experimental/package/{creator}/{mod}/");
var pe = await hc.GetFromJsonAsync<PackageExperimental>($"{API_BASE}/api/experimental/package/{creator}/{mod}/");
if (pe == null)
return null;
if (String.IsNullOrEmpty(pe.Latest.DownloadUrl) || String.IsNullOrEmpty(pe.Latest.FullName))
return null;
return pe;
}
}

internal static async Task Download(string url, string path) {
using (var hc = new HttpClient()) {
Stream httpStream = await hc.GetStreamAsync(url);
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
httpStream.CopyTo(fs, 8192);
}
}
}
}
35 changes: 35 additions & 0 deletions ThunderstoreMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@

namespace ThisCompanyIsGettingLethal
{
#region V1
internal class PackageListing {
[JsonPropertyName("name"), ReadOnly(true)]
public string Name { get; set; }
[JsonPropertyName("full_name"), ReadOnly(true)]
public string FullName { get; set; }
[JsonPropertyName("owner"), ReadOnly(true)]
public string Owner { get; set; }
[JsonPropertyName("package_url"), ReadOnly(true)]
public string PackageUrl { get; set; }
[JsonPropertyName("donation_link"), ReadOnly(true)]
public string DonationLink { get; set; }
[JsonPropertyName("date_created"), ReadOnly(true)]
public string DateCreated { get; set; }
[JsonPropertyName("date_updated"), ReadOnly(true)]
public string DateUpdated { get; set; }
[JsonPropertyName("uuid4"), ReadOnly(true)]
public string UUID4 { get; set; }
[JsonPropertyName("rating_score"), ReadOnly(true)]
public string RatingScore { get; set; }
[JsonPropertyName("is_pinned"), ReadOnly(true)]
public string IsPinned { get; set; }
[JsonPropertyName("is_deprecated"), ReadOnly(true)]
public string IsDeprecated { get; set; }
[JsonPropertyName("has_nsfw_content")]
public bool HasNsfwContent { get; set; }
[JsonPropertyName("categories"), ReadOnly(true)]
public string Categories { get; set; }
[JsonPropertyName("versions"), ReadOnly(true)]
public string Versions { get; set; }
}
#endregion

#region Experimental
internal class PackageExperimental {
[JsonPropertyName("namespace"), ReadOnly(true)]
public string Namespace { get; set; }
Expand Down Expand Up @@ -97,4 +131,5 @@ internal class PackageListingExperimental {
[JsonPropertyName("review_status"), AllowedValues("unreviewed", "approved", "rejected")]
public string ReviewStatus { get; set; }

Check warning on line 132 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ReviewStatus' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
#endregion
}
8 changes: 8 additions & 0 deletions Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ internal static void ExtractZipEntriesWithPrefix(string zipPath, string prefix,
}
}
}

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

0 comments on commit 6df4fce

Please sign in to comment.