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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(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. bool preview = GetExe()?.Contains("Preview") ?? false;
  37. using (var client = GetClient())
  38. {
  39. string str = await client.DownloadStringTaskAsync("https://exmods.org/mods/modlist.tsv");
  40. foreach (string line in str.Trim().Split('\n'))
  41. {
  42. var sp = line.Split('\t');
  43. if (sp.Length < 2) continue;
  44. var mod = new ModInfo
  45. {
  46. Author = sp[0].Trim(),
  47. Name = sp[1].Trim()
  48. };
  49. if (await FetchModInfo(mod, preview)) //If it's actually a mod
  50. AddUpdateModInList(mod);
  51. }
  52. }
  53. }
  54. public async Task<bool> FetchModInfo(ModInfo mod, bool preview)
  55. {
  56. string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
  57. using (var client = GetClient())
  58. {
  59. var arr = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
  60. var release = arr.FirstOrDefault(rel =>
  61. {
  62. if ((bool) rel["prerelease"] || (bool) rel["draft"])
  63. return false;
  64. var vs = rel["tag_name"].ToString();
  65. int ind = vs.IndexOf('-');
  66. if (ind != -1)
  67. {
  68. if (vs.Substring(ind + 1).Equals("preview", StringComparison.InvariantCultureIgnoreCase)
  69. && !preview)
  70. return false;
  71. }
  72. return true;
  73. });
  74. if (release == null)
  75. return false;
  76. var verstr = release["tag_name"].ToString().Replace("v", "");
  77. int index = verstr.IndexOf('-');
  78. if (index != -1)
  79. verstr = verstr.Substring(0, index);
  80. JToken asset;
  81. if (release["assets"].Count() == 1)
  82. asset = release["assets"].First;
  83. else
  84. asset = release["assets"].FirstOrDefault(token =>
  85. {
  86. string name = token["name"].ToString();
  87. return name == mod.Name + ".dll" || name == mod.Name + ".zip";
  88. });
  89. mod.DownloadURL = asset?["browser_download_url"]?.ToString();
  90. mod.LastUpdated = (DateTime)release["published_at"];
  91. var ver = verstr.Split('.').Select(str => int.Parse(str)).ToArray();
  92. 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
  93. mod.LatestVersion = new Version(getver(0), getver(1), getver(2), getver(3));
  94. mod.UpdateDetails = release["name"] + "\n\n" + release["body"].ToString();
  95. try
  96. {
  97. var obj = JObject.Parse(await client.DownloadStringTaskAsync("/api/v1/repos/" + mod.Author + "/" + mod.Name + "/contents/README.md"));
  98. mod.Description = Encoding.UTF8.GetString(Convert.FromBase64String(obj["content"].ToString()));
  99. }
  100. catch(WebException)
  101. { //It returns a HTTP 500 if it doesn't exist...
  102. }
  103. return true;
  104. }
  105. }
  106. public void AddUpdateModInList(ModInfo mod)
  107. {
  108. if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name)) //The ListView's keys aren't case sensitive
  109. throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
  110. ListViewItem item;
  111. if (modlist.Items.ContainsKey(mod.Name))
  112. {
  113. var omod = mods[mod.Name];
  114. item = modlist.Items[mod.Name];
  115. var items = item.SubItems;
  116. omod.Author = mod.Author ?? omod.Author;
  117. omod.Version = mod.Version ?? omod.Version;
  118. omod.LatestVersion = mod.LatestVersion ?? omod.LatestVersion;
  119. omod.LastUpdated = mod.LastUpdated == default ? omod.LastUpdated : mod.LastUpdated;
  120. omod.Description = mod.Description ?? omod.Description;
  121. omod.DownloadURL = mod.DownloadURL ?? omod.DownloadURL;
  122. omod.UpdateDetails = mod.UpdateDetails ?? omod.UpdateDetails;
  123. items[1].Text = omod.Author ?? "";
  124. items[2].Text = (omod.Version ?? omod.LatestVersion)?.ToString();
  125. items[3].Text = omod.LastUpdated.ToString();
  126. item.Group = omod.Installed ? modlist.Groups["installed"] : modlist.Groups["available"];
  127. mod = omod;
  128. }
  129. else
  130. {
  131. mods.Add(mod.Name, mod);
  132. item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", (mod.Version ?? mod.LatestVersion)?.ToString() ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
  133. item.Name = mod.Name;
  134. modlist.Items.Add(item);
  135. }
  136. if (mod.LatestVersion != null && mod.Version != null && mod.Version < mod.LatestVersion)
  137. item.ForeColor = Color.Blue;
  138. else
  139. item.ForeColor = modlist.ForeColor;
  140. }
  141. public void RemoveModFromList(ModInfo mod)
  142. {
  143. if (mods.Remove(mod.Name))
  144. modlist.Items.RemoveByKey(mod.Name);
  145. }
  146. }
  147. }