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.

167 lines
6.2 KiB

  1. using SimpleJSON;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. using Logger = IllusionInjector.Logging.Logger;
  13. namespace IllusionInjector.Updating.ModsaberML
  14. {
  15. class Updater : MonoBehaviour
  16. {
  17. public Updater instance;
  18. public void Awake()
  19. {
  20. instance = this;
  21. CheckForUpdates();
  22. }
  23. public void CheckForUpdates()
  24. {
  25. StartCoroutine(CheckForUpdatesCoroutine());
  26. }
  27. private Regex commentRegex = new Regex(@"(?: \/\/.+)?$", RegexOptions.Compiled | RegexOptions.Multiline);
  28. private Dictionary<Uri, UpdateScript> cachedRequests = new Dictionary<Uri, UpdateScript>();
  29. IEnumerator CheckForUpdatesCoroutine()
  30. {
  31. Logger.log.Info("Checking for mod updates...");
  32. var toUpdate = new List<PluginManager.BSPluginMeta>();
  33. var modList = new List<ApiEndpoint.Mod>();
  34. using (var request = UnityWebRequest.Get(ApiEndpoint.ApiBase+ApiEndpoint.GetApprovedEndpoint))
  35. {
  36. yield return request.SendWebRequest();
  37. if (request.isNetworkError)
  38. {
  39. Logger.log.Error("Network error while trying to update mods");
  40. Logger.log.Error(request.error);
  41. yield break;
  42. }
  43. if (request.isHttpError)
  44. {
  45. Logger.log.Error($"Server returned an error code while trying to update mods");
  46. Logger.log.Error(request.error);
  47. }
  48. var json = request.downloadHandler.text;
  49. JSONObject obj = null;
  50. try
  51. {
  52. obj = JSON.Parse(json).AsObject;
  53. }
  54. catch (InvalidCastException)
  55. {
  56. Logger.log.Error($"Parse error while trying to update mods");
  57. Logger.log.Error($"Response doesn't seem to be a JSON object");
  58. yield break;
  59. }
  60. catch (Exception e)
  61. {
  62. Logger.log.Error($"Parse error while trying to update mods");
  63. Logger.log.Error(e);
  64. yield break;
  65. }
  66. foreach (var modObj in obj["mods"].AsArray.Children)
  67. {
  68. try
  69. {
  70. modList.Add(ApiEndpoint.Mod.DecodeJSON(modObj.AsObject));
  71. }
  72. catch (Exception e)
  73. {
  74. Logger.log.Error($"Parse error while trying to update mods");
  75. Logger.log.Error($"Response doesn't seem to be correctly formatted");
  76. Logger.log.Error(e);
  77. break;
  78. }
  79. }
  80. }
  81. var GameVersion = new Version(Application.version);
  82. foreach (var plugin in PluginManager.BSMetas)
  83. {
  84. var info = plugin.ModsaberInfo;
  85. var modRegistry = modList.FirstOrDefault(o => o.Name == info.InternalName);
  86. if (modRegistry != null)
  87. { // a.k.a we found it
  88. Logger.log.Debug($"Found Modsaber.ML registration for {plugin.Plugin.Name} ({info.InternalName})");
  89. Logger.log.Debug($"Installed version: {info.CurrentVersion}; Latest version: {modRegistry.Version}");
  90. if (modRegistry.Version > info.CurrentVersion)
  91. {
  92. Logger.log.Debug($"{plugin.Plugin.Name} needs an update!");
  93. if (modRegistry.GameVersion == GameVersion)
  94. {
  95. Logger.log.Debug($"Queueing update...");
  96. toUpdate.Add(plugin);
  97. }
  98. else
  99. {
  100. Logger.log.Warn($"Update avaliable for {plugin.Plugin.Name}, but for a different Beat Saber version!");
  101. }
  102. }
  103. }
  104. }
  105. Logger.log.Info($"{toUpdate.Count} mods need updating");
  106. if (toUpdate.Count == 0) yield break;
  107. string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + Path.GetRandomFileName());
  108. Directory.CreateDirectory(tempDirectory);
  109. Logger.log.Debug($"Created temp download dirtectory {tempDirectory}");
  110. foreach (var item in toUpdate)
  111. {
  112. StartCoroutine(DownloadPluginCoroutine(tempDirectory, item));
  113. }
  114. }
  115. IEnumerator DownloadPluginCoroutine(string tempdir, PluginManager.BSPluginMeta item)
  116. {
  117. yield return null;
  118. /*var file = Path.Combine(tempdir, item. + ".dll");
  119. using (var req = UnityWebRequest.Get(item.DownloadUri))
  120. {
  121. req.downloadHandler = new DownloadHandlerFile(file);
  122. yield return req.SendWebRequest();
  123. if (req.isNetworkError)
  124. {
  125. Logger.log.Error($"Network error while trying to download update for {item.Plugin.Plugin.Name}");
  126. Logger.log.Error(req.error);
  127. yield break;
  128. }
  129. if (req.isHttpError)
  130. {
  131. Logger.log.Error($"Server returned an error code while trying to download update for {item.Plugin.Plugin.Name}");
  132. Logger.log.Error(req.error);
  133. yield break;
  134. }
  135. }
  136. var pluginDir = Path.GetDirectoryName(item.Plugin.Filename);
  137. var newFile = Path.Combine(pluginDir, item.Name + ".dll");
  138. File.Delete(item.Plugin.Filename);
  139. if (File.Exists(newFile))
  140. File.Delete(newFile);
  141. File.Move(file, newFile);
  142. Logger.log.Info($"{item.Plugin.Plugin.Name} updated to {item.NewVersion}");*/
  143. }
  144. }
  145. }