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.

192 lines
8.2KB

  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 HashSet<string> GetInstalledMods()
  19. {
  20. bool disabled = false;
  21. if (!Directory.Exists(GamePath("\\Plugins")))
  22. if (Directory.Exists(GamePath("\\Plugins_Disabled")))
  23. disabled = true;
  24. else return new HashSet<string>();
  25. var installed = new HashSet<string>();
  26. foreach (var modPath in Directory.GetFiles(GamePath(disabled ? @"\Plugins_Disabled" : @"\Plugins"), "*.dll"))
  27. {
  28. try
  29. {
  30. var an = AssemblyName.GetAssemblyName(modPath);
  31. if (an.Name == "0Harmony") continue;
  32. //Use filename to avoid differences between repository & assembly name casing
  33. var mod = new ModInfo { Name = Path.GetFileNameWithoutExtension(modPath), Version = an.Version, LastUpdated = File.GetLastWriteTime(modPath) };
  34. AddUpdateModInList(mod);
  35. installed.Add(mod.Name);
  36. }
  37. catch (BadImageFormatException)
  38. { //Not a .NET assembly
  39. }
  40. }
  41. try
  42. {
  43. string ipath = GamePath("\\IPA.exe"); //Heh
  44. if (File.Exists(ipath))
  45. {
  46. var an = AssemblyName.GetAssemblyName(ipath);
  47. gcipa.Version = an.Version;
  48. gcipa.LastUpdated = File.GetLastWriteTime(ipath);
  49. }
  50. }
  51. catch (BadImageFormatException)
  52. { //Not a .NET assembly
  53. }
  54. return installed;
  55. }
  56. public async Task GetAvailableMods()
  57. {
  58. bool preview = GetExe()?.Contains("Preview") ?? false;
  59. using (var client = GetClient())
  60. {
  61. string str = await client.DownloadStringTaskAsync("https://exmods.org/mods/modlist.tsv");
  62. foreach (string line in str.Trim().Split('\n'))
  63. {
  64. var sp = line.Split('\t');
  65. if (sp.Length < 2) continue;
  66. var mod = new ModInfo
  67. {
  68. Author = sp[0].Trim(),
  69. Name = sp[1].Trim()
  70. };
  71. if (await FetchModInfo(mod, preview, true)) //If it's actually a mod
  72. AddUpdateModInList(mod);
  73. }
  74. }
  75. await FetchModInfo(gcipa, preview, false);
  76. }
  77. public async Task<bool> FetchModInfo(ModInfo mod, bool preview, bool desc)
  78. {
  79. string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
  80. using (var client = GetClient())
  81. {
  82. var arr = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
  83. var release = arr.FirstOrDefault(rel =>
  84. {
  85. if ((bool) rel["prerelease"] || (bool) rel["draft"])
  86. return false;
  87. var vs = rel["tag_name"].ToString();
  88. int ind = vs.IndexOf('-');
  89. if (ind != -1)
  90. {
  91. if (vs.Substring(ind + 1).Equals("preview", StringComparison.InvariantCultureIgnoreCase)
  92. && !preview)
  93. return false;
  94. }
  95. return true;
  96. });
  97. if (release == null)
  98. return false;
  99. var verstr = release["tag_name"].ToString().Replace("v", "");
  100. int index = verstr.IndexOf('-');
  101. if (index != -1)
  102. verstr = verstr.Substring(0, index);
  103. JToken asset;
  104. if (release["assets"].Count() == 1)
  105. asset = release["assets"].First;
  106. else
  107. asset = release["assets"].FirstOrDefault(token =>
  108. {
  109. string name = token["name"].ToString();
  110. return name == mod.Name + ".dll" || name == mod.Name + ".zip";
  111. });
  112. mod.DownloadURL = asset?["browser_download_url"]?.ToString();
  113. mod.LastUpdated = (DateTime)release["published_at"];
  114. var ver = verstr.Split('.').Select(str => int.Parse(str)).ToArray();
  115. 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
  116. mod.LatestVersion = new Version(getver(0), getver(1), getver(2), getver(3));
  117. mod.UpdateDetails = release["name"] + "\n\n" + release["body"].ToString();
  118. if (desc)
  119. {
  120. try
  121. {
  122. var obj = JObject.Parse(await client.DownloadStringTaskAsync("/api/v1/repos/" + mod.Author + "/" + mod.Name + "/contents/README.md"));
  123. mod.Description = Encoding.UTF8.GetString(Convert.FromBase64String(obj["content"].ToString()));
  124. }
  125. catch (WebException)
  126. { //It returns a HTTP 500 if it doesn't exist...
  127. }
  128. }
  129. return true;
  130. }
  131. }
  132. public void AddUpdateModInList(ModInfo mod)
  133. {
  134. if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name)) //The ListView's keys aren't case sensitive
  135. throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
  136. ListViewItem item;
  137. if (modlist.Items.ContainsKey(mod.Name))
  138. {
  139. var omod = mods[mod.Name];
  140. item = modlist.Items[mod.Name];
  141. var items = item.SubItems;
  142. omod.Author = mod.Author ?? omod.Author;
  143. omod.Version = mod.Version ?? omod.Version; //If the object comes from the dictionary then it's directly modified (uninstall)
  144. omod.LatestVersion = mod.LatestVersion ?? omod.LatestVersion;
  145. omod.LastUpdated = mod.LastUpdated == default ? omod.LastUpdated : mod.LastUpdated;
  146. omod.Description = mod.Description ?? omod.Description;
  147. omod.DownloadURL = mod.DownloadURL ?? omod.DownloadURL;
  148. omod.UpdateDetails = mod.UpdateDetails ?? omod.UpdateDetails;
  149. items[1].Text = omod.Author ?? "";
  150. items[2].Text = (omod.Version ?? omod.LatestVersion)?.ToString();
  151. items[3].Text = omod.LastUpdated.ToString();
  152. item.Group = omod.Installed ? modlist.Groups["installed"] : modlist.Groups["available"];
  153. mod = omod;
  154. }
  155. else
  156. {
  157. mods.Add(mod.Name, mod);
  158. item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", (mod.Version ?? mod.LatestVersion)?.ToString() ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
  159. item.Name = mod.Name;
  160. modlist.Items.Add(item);
  161. }
  162. if (mod.LatestVersion != null && mod.Version != null && mod.Version < mod.LatestVersion)
  163. item.ForeColor = Color.Blue;
  164. else
  165. item.ForeColor = modlist.ForeColor;
  166. }
  167. public void CheckUninstalledMods(HashSet<string> installed)
  168. {
  169. List<string> delete = new List<string>();
  170. foreach (string name in mods.Keys.Except(installed))
  171. {
  172. var mod = mods[name];
  173. mod.Version = null;
  174. if (mod.Author != null)
  175. AddUpdateModInList(mod);
  176. else
  177. delete.Add(name);
  178. }
  179. delete.ForEach(name => { mods.Remove(name); modlist.Items[name].Remove(); });
  180. }
  181. }
  182. }