Skip to content

Commit

Permalink
Added modpack generator
Browse files Browse the repository at this point in the history
  • Loading branch information
GoobyCorp committed Jan 31, 2024
1 parent 9bc9f4f commit 161e6d1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
20 changes: 14 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.IO;
using System.Text.Json;
using System.Text.Json;
using System.IO.Compression;

namespace ThisCompanyIsGettingLethal
{
internal static class Program {
internal const string CONFIG_FILE = "config.json";
internal const string DOWNLOAD_DIR = "Downloads";
internal const string MODS_DIR = "Mods";
internal const string MODPACK_FILE = "modpack.zip";

static int Main(string[] args) {
if(!File.Exists(CONFIG_FILE))
Expand All @@ -18,17 +19,24 @@ static int Main(string[] args) {
if (!Directory.Exists(MODS_DIR))
Directory.CreateDirectory(MODS_DIR);

Console.WriteLine("Reading config...");
ConfigEntry[] entries = JsonSerializer.Deserialize<ConfigEntry[]>(File.ReadAllText(CONFIG_FILE));
if (entries == null || entries.Length == 0) {
Console.WriteLine("No config entries found, aborting...");
goto Finished;
}

Console.WriteLine("Fetching download links and files...");
List<Task> allTasks = new List<Task>();
entries.ToList().ForEach(e => allTasks.Add(Task.Run(() => { FindDownloadExtractPackage(e.Creator, e.Mod); })));
Task.WaitAll(allTasks.ToArray());

Finished:
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);
Console.WriteLine("Done!");

Finished:
#if DEBUG
Console.WriteLine("Press ENTER to exit...");
Console.ReadKey();
Expand All @@ -41,11 +49,11 @@ internal static void FindDownloadExtractPackage(string creator, string mod) {
Task<PackageExperimental> t0 = ThunderstoreAPI.Package(creator, mod);
t0.Wait();
var pe = t0.Result;
string path = Path.Combine(DOWNLOAD_DIR, pe.latest.full_name + ".zip");
string path = Path.Combine(DOWNLOAD_DIR, pe.Latest.FullName + ".zip");

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

Task t1 = ThunderstoreAPI.Download(pe.latest.download_url, path);
Task t1 = ThunderstoreAPI.Download(pe.Latest.DownloadUrl, path);
t1.Wait();
Task t2 = Task.Run(() => {
Utilities.ExtractZipEntriesWithPrefix(path, "BepInEx/plugins/", MODS_DIR);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This Company Is Getting Lethal

A program for updating Lethal Company BepInEx plugins from [Thunderstore](https://thunderstore.io/c/lethal-company/)
A program for creating Lethal Company modpacks from [Thunderstore](https://thunderstore.io/c/lethal-company/)

API reference [here](https://thunderstore.io/api/docs/)
2 changes: 1 addition & 1 deletion ThunderstoreAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static async Task<PackageExperimental> Package(string creator, string m
var pe = await hc.GetFromJsonAsync<PackageExperimental>($"https://thunderstore.io/api/experimental/package/{creator}/{mod}/");
if (pe == null)
return null;
if (String.IsNullOrEmpty(pe.latest.download_url) || String.IsNullOrEmpty(pe.latest.full_name))
if (String.IsNullOrEmpty(pe.Latest.DownloadUrl) || String.IsNullOrEmpty(pe.Latest.FullName))
return null;
return pe;
}
Expand Down
63 changes: 29 additions & 34 deletions ThunderstoreMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,97 @@

namespace ThisCompanyIsGettingLethal
{
//internal class JsonMessages
//{

//}

internal class PackageExperimental {
[JsonPropertyName("namespace"), ReadOnly(true)]
public string _namespace { get; set; }
public string Namespace { get; set; }

[JsonPropertyName("name"), MinLength(1), MaxLength(128), Required]
public string name { get; set; }
public string Name { get; set; }

[JsonPropertyName("full_name"), ReadOnly(true)]
public string full_name { get; set; }
public string FullName { get; set; }

[JsonPropertyName("owner"), ReadOnly(true)]
public string owner { get; set; }
public string Owner { get; set; }

[JsonPropertyName("package_url"), ReadOnly(true)]
public string package_url { get; set; }
public string PackageUrl { get; set; }

[JsonPropertyName("date_created"), ReadOnly(true)]
public string date_created { get; set; }
public string DateCreated { get; set; }

[JsonPropertyName("date_updated"), ReadOnly(true)]
public string date_updated { get; set; }
public string DateUpdated { get; set; }

[JsonPropertyName("rating_score"), Range(0, int.MaxValue)]
public int rating_score { get; set; }
public int RatingScore { get; set; }

[JsonPropertyName("is_pinned")]
public bool is_pinned { get; set; }
public bool IsPinned { get; set; }

[JsonPropertyName("is_deprecated")]
public bool is_deprecated { get; set; }
public bool IsDeprecated { get; set; }

[JsonPropertyName("total_downloads"), Range(0, int.MaxValue)]
public int total_downloads { get; set; }
public int TotalDownloads { get; set; }

[JsonPropertyName("latest"), Required]
public PackageVersionExperimental latest { get; set; }
public PackageVersionExperimental Latest { get; set; }

[JsonPropertyName("community_listings"), Required]
public PackageListingExperimental[] community_listings { get; set; }
public PackageListingExperimental[] CommunityListings { get; set; }
}

internal class PackageVersionExperimental {
// string namespace;
[JsonPropertyName("namespace"), ReadOnly(true)]
public string _namespace { get; set; }
public string Namespace { get; set; }

Check warning on line 51 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Namespace' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("name"), MinLength(1), MaxLength(128), Required]
public string name { get; set; }
public string Name { get; set; }

Check warning on line 54 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("version_number"), MinLength(1), MaxLength(16), Required]
public string version_number { get; set; }
public string VersionNumber { get; set; }

Check warning on line 57 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'VersionNumber' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("full_name"), ReadOnly(true)]
public string full_name { get; set; }
public string FullName { get; set; }

Check warning on line 60 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FullName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("description"), MinLength(1), MaxLength(128), Required]
public string description { get; set; }
public string Description { get; set; }

Check warning on line 63 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("icon"), ReadOnly(true)]
public string icon { get; set; }
public string Icon { get; set; }

Check warning on line 66 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Icon' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("dependencies"), ReadOnly(true)]
public string[] dependencies { get; set; }
public string[] Dependencies { get; set; }

[JsonPropertyName("download_url"), ReadOnly(true)]
public string download_url { get; set; }
public string DownloadUrl { get; set; }

[JsonPropertyName("downloads"), Range(0, int.MaxValue)]
public int downloads { get; set; }
public int Downloads { get; set; }

[JsonPropertyName("date_created"), ReadOnly(true)]
public string date_created { get; set; }
public string DateCreated { get; set; }

[JsonPropertyName("website_url"), ReadOnly(true), MinLength(1), MaxLength(1024), Required]
public string website_url { get; set; }
public string WebsiteUrl { get; set; }

[JsonPropertyName("is_active")]
public bool is_active { get; set; }
public bool IsActive { get; set; }
}

internal class PackageListingExperimental {
[JsonPropertyName("has_nsfw_content")]
public bool has_nsfw_content { get; set; }
public bool HasNsfwContent { get; set; }

[JsonPropertyName("categories"), ReadOnly(true)]
public string[] categories { get; set; }
public string[] Categories { get; set; }

Check warning on line 92 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Categories' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("community"), ReadOnly(true)]
public string community { get; set; }
public string Community { get; set; }

Check warning on line 95 in ThunderstoreMessages.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Community' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("review_status"), AllowedValues("unreviewed", "approved", "rejected")]
public string review_status { get; set; }
public string ReviewStatus { get; set; }

Check warning on line 98 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.
}
}

0 comments on commit 161e6d1

Please sign in to comment.