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.

MainModder.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace GCMM
  13. {
  14. partial class MainForm
  15. {
  16. public void GetInstalledMods()
  17. {
  18. foreach (var modPath in Directory.GetFiles(Settings.Default.GamePath + @"\Plugins", "*.dll"))
  19. {
  20. try
  21. {
  22. var an = AssemblyName.GetAssemblyName(modPath);
  23. if (an.Name == "0Harmony") continue;
  24. AddUpdateModInList(new ModInfo { Name = an.Name, Version = an.Version.ToString(), LastUpdated = File.GetLastWriteTime(modPath) });
  25. }
  26. catch (BadImageFormatException)
  27. { //Not a .NET assembly
  28. }
  29. }
  30. }
  31. public async void GetAvailableMods()
  32. {
  33. string reposURL = "/api/v1/repos/search?sort=updated&order=desc";
  34. using (var client = GetClient())
  35. {
  36. var obj = JObject.Parse(await client.DownloadStringTaskAsync(reposURL));
  37. if (!(bool)obj["ok"]) return;
  38. foreach (var repo in obj["data"])
  39. {
  40. var mod = new ModInfo
  41. {
  42. Name = repo["name"].ToString(),
  43. Author = repo["owner"]["username"].ToString(),
  44. LastUpdated = (DateTime)repo["updated_at"]
  45. };
  46. if (await FetchModInfo(mod)) //If it's actually a mod
  47. AddUpdateModInList(mod);
  48. }
  49. }
  50. }
  51. public async Task<bool> FetchModInfo(ModInfo mod)
  52. {
  53. string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
  54. using (var client = GetClient())
  55. {
  56. var obj = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
  57. var release = obj.FirstOrDefault(rel => !(bool)rel["prerelease"] && !(bool)rel["draft"]);
  58. if (release == null)
  59. return false;
  60. else
  61. {
  62. mod.DownloadURL = release["assets"].First["browser_download_url"].ToString();
  63. mod.LastUpdated = (DateTime)release["published_at"];
  64. mod.LatestVersion = release["tag_name"].ToString().Replace("v", "");
  65. return true;
  66. }
  67. }
  68. }
  69. public void AddUpdateModInList(ModInfo mod)
  70. {
  71. if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name))
  72. throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
  73. if (modlist.Items.ContainsKey(mod.Name))
  74. {
  75. var omod = mods[mod.Name];
  76. var item = modlist.Items[mod.Name];
  77. var items = item.SubItems;
  78. omod.Author = mod.Author ?? omod.Author;
  79. omod.Version = mod.Version ?? omod.Version;
  80. omod.LatestVersion = mod.LatestVersion ?? omod.LatestVersion;
  81. omod.LastUpdated = mod.LastUpdated == default ? omod.LastUpdated : mod.LastUpdated;
  82. items[1].Text = omod.Author ?? "";
  83. items[2].Text = omod.Version ?? omod.LatestVersion;
  84. items[3].Text = omod.LastUpdated.ToString();
  85. item.Group = omod.Installed ? modlist.Groups["installed"] : modlist.Groups["available"];
  86. if (mod.Version != mod.LatestVersion)
  87. items[2].ForeColor = Color.Red;
  88. }
  89. else
  90. {
  91. mods.Add(mod.Name, mod);
  92. var item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", mod.Version ?? mod.LatestVersion ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
  93. item.Name = mod.Name;
  94. modlist.Items.Add(item);
  95. }
  96. }
  97. public void RemoveModFromList(ModInfo mod)
  98. {
  99. if (mods.Remove(mod.Name))
  100. modlist.Items.RemoveByKey(mod.Name);
  101. }
  102. }
  103. }