Techblox Mod Manager / Launcher
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

162 rindas
6.9KB

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