Techblox Mod Manager / Launcher
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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