Techblox Mod Manager / Launcher
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

134 lines
5.7KB

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