Techblox Mod Manager / Launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainModList.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using GCMM.Properties;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Reflection;
  10. using System.Runtime.CompilerServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace GCMM
  15. {
  16. partial class MainForm
  17. {
  18. public void GetInstalledMods()
  19. {
  20. foreach (var modPath in Directory.GetFiles(Settings.Default.GamePath + @"\Plugins", "*.dll"))
  21. {
  22. try
  23. {
  24. var an = AssemblyName.GetAssemblyName(modPath);
  25. if (an.Name == "0Harmony") continue;
  26. //Use filename to avoid differences between repository & assembly name casing
  27. AddUpdateModInList(new ModInfo { Name = Path.GetFileNameWithoutExtension(modPath), Version = an.Version, LastUpdated = File.GetLastWriteTime(modPath) });
  28. }
  29. catch (BadImageFormatException)
  30. { //Not a .NET assembly
  31. }
  32. }
  33. }
  34. public async void GetAvailableMods()
  35. {
  36. using (var client = GetClient())
  37. {
  38. string str = await client.DownloadStringTaskAsync("https://exmods.org/mods/modlist.tsv");
  39. foreach (string line in str.Trim().Split('\n'))
  40. {
  41. var sp = line.Split('\t');
  42. if (sp.Length < 2) continue;
  43. var mod = new ModInfo
  44. {
  45. Author = sp[0].Trim(),
  46. Name = sp[1].Trim()
  47. };
  48. if (await FetchModInfo(mod)) //If it's actually a mod
  49. AddUpdateModInList(mod);
  50. }
  51. }
  52. }
  53. public async Task<bool> FetchModInfo(ModInfo mod)
  54. {
  55. string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
  56. using (var client = GetClient())
  57. {
  58. var arr = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
  59. var release = arr.FirstOrDefault(rel => !(bool)rel["prerelease"] && !(bool)rel["draft"]);
  60. if (release == null)
  61. return false;
  62. JToken asset;
  63. if (release["assets"].Count() == 1)
  64. asset = release["assets"].First;
  65. else
  66. asset = release["assets"].FirstOrDefault(token =>
  67. {
  68. string name = token["name"].ToString();
  69. return name == mod.Name + ".dll" || name == mod.Name + ".zip";
  70. });
  71. mod.DownloadURL = asset?["browser_download_url"]?.ToString();
  72. mod.LastUpdated = (DateTime)release["published_at"];
  73. var ver = release["tag_name"].ToString().Replace("v", "").Split('.').Select(str => int.Parse(str)).ToArray();
  74. int getver(byte i) => ver.Length > i ? ver[i] : 0; //By default it sets values not present to -1, but we need them to be 0
  75. mod.LatestVersion = new Version(getver(0), getver(1), getver(2), getver(3));
  76. mod.UpdateDetails = release["name"] + "\n\n" + release["body"].ToString();
  77. try
  78. {
  79. var obj = JObject.Parse(await client.DownloadStringTaskAsync("/api/v1/repos/" + mod.Author + "/" + mod.Name + "/contents/README.md"));
  80. mod.Description = Encoding.UTF8.GetString(Convert.FromBase64String(obj["content"].ToString()));
  81. }
  82. catch(WebException)
  83. { //It returns a HTTP 500 if it doesn't exist...
  84. }
  85. return true;
  86. }
  87. }
  88. public void AddUpdateModInList(ModInfo mod)
  89. {
  90. if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name)) //The ListView's keys aren't case sensitive
  91. throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
  92. ListViewItem item;
  93. if (modlist.Items.ContainsKey(mod.Name))
  94. {
  95. var omod = mods[mod.Name];
  96. item = modlist.Items[mod.Name];
  97. var items = item.SubItems;
  98. omod.Author = mod.Author ?? omod.Author;
  99. omod.Version = mod.Version ?? omod.Version;
  100. omod.LatestVersion = mod.LatestVersion ?? omod.LatestVersion;
  101. omod.LastUpdated = mod.LastUpdated == default ? omod.LastUpdated : mod.LastUpdated;
  102. omod.Description = mod.Description ?? omod.Description;
  103. omod.DownloadURL = mod.DownloadURL ?? omod.DownloadURL;
  104. omod.UpdateDetails = mod.UpdateDetails ?? omod.UpdateDetails;
  105. items[1].Text = omod.Author ?? "";
  106. items[2].Text = (omod.Version ?? omod.LatestVersion)?.ToString();
  107. items[3].Text = omod.LastUpdated.ToString();
  108. item.Group = omod.Installed ? modlist.Groups["installed"] : modlist.Groups["available"];
  109. mod = omod;
  110. }
  111. else
  112. {
  113. mods.Add(mod.Name, mod);
  114. item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", (mod.Version ?? mod.LatestVersion)?.ToString() ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
  115. item.Name = mod.Name;
  116. modlist.Items.Add(item);
  117. }
  118. if (mod.LatestVersion != null && mod.Version != null && mod.Version < mod.LatestVersion)
  119. item.ForeColor = Color.Blue;
  120. }
  121. public void RemoveModFromList(ModInfo mod)
  122. {
  123. if (mods.Remove(mod.Name))
  124. modlist.Items.RemoveByKey(mod.Name);
  125. }
  126. }
  127. }