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.

133 lines
5.6KB

  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. AddUpdateModInList(new ModInfo { Name = an.Name, Version = an.Version, LastUpdated = File.GetLastWriteTime(modPath) });
  27. }
  28. catch (BadImageFormatException)
  29. { //Not a .NET assembly
  30. }
  31. }
  32. }
  33. public async void GetAvailableMods()
  34. {
  35. /*string reposURL = "/api/v1/repos/search?sort=updated&order=desc";
  36. using (var client = GetClient())
  37. {
  38. var obj = JObject.Parse(await client.DownloadStringTaskAsync(reposURL));
  39. if (!(bool)obj["ok"]) return;
  40. foreach (var repo in obj["data"])
  41. {
  42. var mod = new ModInfo
  43. {
  44. Name = repo["name"].ToString(),
  45. Author = repo["owner"]["username"].ToString(),
  46. LastUpdated = (DateTime)repo["updated_at"]
  47. };
  48. if (await FetchModInfo(mod)) //If it's actually a mod
  49. AddUpdateModInList(mod);
  50. }
  51. }*/
  52. foreach (string line in File.ReadLines("modlist.txt"))
  53. {
  54. var sp = line.Split('\t');
  55. var mod = new ModInfo
  56. {
  57. Author = sp[0],
  58. Name = sp[1]
  59. };
  60. if (await FetchModInfo(mod)) //If it's actually a mod
  61. AddUpdateModInList(mod);
  62. }
  63. }
  64. public async Task<bool> FetchModInfo(ModInfo mod)
  65. {
  66. string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
  67. using (var client = GetClient())
  68. {
  69. var arr = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
  70. var release = arr.FirstOrDefault(rel => !(bool)rel["prerelease"] && !(bool)rel["draft"]);
  71. if (release == null)
  72. return false;
  73. if (release["assets"].Count() == 1)
  74. mod.DownloadURL = release["assets"].First["browser_download_url"].ToString();
  75. mod.LastUpdated = (DateTime)release["published_at"];
  76. var ver = release["tag_name"].ToString().Replace("v", "").Split('.').Select(str => int.Parse(str)).ToArray();
  77. 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
  78. mod.LatestVersion = new Version(getver(0), getver(1), getver(2), getver(3));
  79. try
  80. {
  81. var obj = JObject.Parse(await client.DownloadStringTaskAsync("/api/v1/repos/" + mod.Author + "/" + mod.Name + "/contents/README.md"));
  82. mod.Description = Encoding.UTF8.GetString(Convert.FromBase64String(obj["content"].ToString()));
  83. }
  84. catch(WebException)
  85. { //It returns a HTTP 500 if it doesn't exist...
  86. }
  87. return true;
  88. }
  89. }
  90. public void AddUpdateModInList(ModInfo mod)
  91. {
  92. if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name))
  93. throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
  94. if (modlist.Items.ContainsKey(mod.Name))
  95. {
  96. var omod = mods[mod.Name];
  97. var item = modlist.Items[mod.Name];
  98. var items = item.SubItems;
  99. omod.Author = mod.Author ?? omod.Author;
  100. omod.Version = mod.Version ?? omod.Version;
  101. omod.LatestVersion = mod.LatestVersion ?? omod.LatestVersion;
  102. omod.LastUpdated = mod.LastUpdated == default ? omod.LastUpdated : mod.LastUpdated;
  103. omod.Description = mod.Description ?? omod.Description;
  104. omod.DownloadURL = mod.DownloadURL ?? omod.DownloadURL;
  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. if (mod.Version != mod.LatestVersion)
  110. items[2].ForeColor = Color.Red;
  111. }
  112. else
  113. {
  114. mods.Add(mod.Name, mod);
  115. var item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", (mod.Version ?? mod.LatestVersion)?.ToString() ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
  116. item.Name = mod.Name;
  117. modlist.Items.Add(item);
  118. }
  119. }
  120. public void RemoveModFromList(ModInfo mod)
  121. {
  122. if (mods.Remove(mod.Name))
  123. modlist.Items.RemoveByKey(mod.Name);
  124. }
  125. }
  126. }