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.

239 lines
8.2 KiB

  1. using IllusionInjector.Utilities;
  2. using Ionic.Zip;
  3. using SimpleJSON;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using UnityEngine;
  14. using UnityEngine.Networking;
  15. using Logger = IllusionInjector.Logging.Logger;
  16. namespace IllusionInjector.Updating.ModsaberML
  17. {
  18. class Updater : MonoBehaviour
  19. {
  20. public static Updater instance;
  21. public void Awake()
  22. {
  23. try
  24. {
  25. if (instance != null)
  26. Destroy(this);
  27. else
  28. {
  29. instance = this;
  30. CheckForUpdates();
  31. }
  32. }
  33. catch (Exception e)
  34. {
  35. Logger.log.Error(e);
  36. }
  37. }
  38. public void CheckForUpdates()
  39. {
  40. StartCoroutine(CheckForUpdatesCoroutine());
  41. }
  42. private struct UpdateStruct
  43. {
  44. public PluginManager.BSPluginMeta plugin;
  45. public ApiEndpoint.Mod externInfo;
  46. }
  47. IEnumerator CheckForUpdatesCoroutine()
  48. {
  49. Logger.log.Info("Checking for mod updates...");
  50. var toUpdate = new List<UpdateStruct>();
  51. var modList = new List<ApiEndpoint.Mod>();
  52. using (var request = UnityWebRequest.Get(ApiEndpoint.ApiBase+ApiEndpoint.GetApprovedEndpoint))
  53. {
  54. yield return request.SendWebRequest();
  55. if (request.isNetworkError)
  56. {
  57. Logger.log.Error("Network error while trying to update mods");
  58. Logger.log.Error(request.error);
  59. yield break;
  60. }
  61. if (request.isHttpError)
  62. {
  63. Logger.log.Error($"Server returned an error code while trying to update mods");
  64. Logger.log.Error(request.error);
  65. }
  66. var json = request.downloadHandler.text;
  67. JSONObject obj = null;
  68. try
  69. {
  70. obj = JSON.Parse(json).AsObject;
  71. }
  72. catch (InvalidCastException)
  73. {
  74. Logger.log.Error($"Parse error while trying to update mods");
  75. Logger.log.Error($"Response doesn't seem to be a JSON object");
  76. yield break;
  77. }
  78. catch (Exception e)
  79. {
  80. Logger.log.Error($"Parse error while trying to update mods");
  81. Logger.log.Error(e);
  82. yield break;
  83. }
  84. foreach (var modObj in obj["mods"].AsArray.Children)
  85. {
  86. try
  87. {
  88. modList.Add(ApiEndpoint.Mod.DecodeJSON(modObj.AsObject));
  89. }
  90. catch (Exception e)
  91. {
  92. Logger.log.Error($"Parse error while trying to update mods");
  93. Logger.log.Error($"Response doesn't seem to be correctly formatted");
  94. Logger.log.Error(e);
  95. break;
  96. }
  97. }
  98. }
  99. var GameVersion = new Version(Application.version);
  100. foreach (var plugin in PluginManager.BSMetas)
  101. {
  102. var info = plugin.ModsaberInfo;
  103. var modRegistry = modList.FirstOrDefault(o => o.Name == info.InternalName);
  104. if (modRegistry != null)
  105. { // a.k.a we found it
  106. Logger.log.Debug($"Found Modsaber.ML registration for {plugin.Plugin.Name} ({info.InternalName})");
  107. Logger.log.Debug($"Installed version: {info.CurrentVersion}; Latest version: {modRegistry.Version}");
  108. if (modRegistry.Version > info.CurrentVersion)
  109. {
  110. Logger.log.Debug($"{plugin.Plugin.Name} needs an update!");
  111. if (modRegistry.GameVersion == GameVersion)
  112. {
  113. Logger.log.Debug($"Queueing update...");
  114. toUpdate.Add(new UpdateStruct
  115. {
  116. plugin = plugin,
  117. externInfo = modRegistry
  118. });
  119. }
  120. else
  121. {
  122. Logger.log.Warn($"Update avaliable for {plugin.Plugin.Name}, but for a different Beat Saber version!");
  123. }
  124. }
  125. }
  126. }
  127. Logger.log.Info($"{toUpdate.Count} mods need updating");
  128. if (toUpdate.Count == 0) yield break;
  129. string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + Path.GetRandomFileName());
  130. Directory.CreateDirectory(tempDirectory);
  131. Logger.log.Debug($"Created temp download dirtectory {tempDirectory}");
  132. foreach (var item in toUpdate)
  133. {
  134. StartCoroutine(UpdateModCoroutine(tempDirectory, item));
  135. }
  136. }
  137. class StreamDownloadHandler : DownloadHandlerScript
  138. {
  139. public MemoryStream Stream { get; set; }
  140. protected void ReceiveContentLength(long contentLength)
  141. {
  142. Stream.Capacity = (int)contentLength;
  143. Logger.log.Debug($"Got content length: {contentLength}");
  144. }
  145. protected void OnContentComplete()
  146. {
  147. Logger.log.Debug("Download complete");
  148. }
  149. protected bool ReceiveData(byte[] data, long dataLength)
  150. {
  151. Stream.Write(data, 0, (int)dataLength);
  152. return true;
  153. }
  154. }
  155. IEnumerator UpdateModCoroutine(string tempdir, UpdateStruct item)
  156. {
  157. void DownloadPluginAsync(MemoryStream stream)
  158. { // embedded because i don't think unity likes it in the top level
  159. Logger.log.Debug($"Getting ZIP file for {item.plugin.Plugin.Name}");
  160. //var stream = await httpClient.GetStreamAsync(url);
  161. using (var zipFile = new ZipInputStream(new BlockingStream(stream)))
  162. {
  163. Logger.log.Debug("Streams opened");
  164. ZipEntry entry;
  165. while ((entry = zipFile.GetNextEntry()) != null)
  166. {
  167. Logger.log.Debug(entry?.FileName);
  168. }
  169. }
  170. Logger.log.Debug("Downloader exited");
  171. }
  172. string url;
  173. if (SteamCheck.IsAvailable || item.externInfo.OculusFile == null)
  174. url = item.externInfo.SteamFile;
  175. else
  176. url = item.externInfo.OculusFile;
  177. Logger.log.Debug($"URL = {url}");
  178. using (var req = UnityWebRequest.Get(url))
  179. using (var memStream = new MemoryStream())
  180. {
  181. req.downloadHandler = new StreamDownloadHandler
  182. {
  183. Stream = memStream
  184. };
  185. var downloadTask = Task.Run(() =>
  186. { // use slightly more multithreaded approach than coroutines
  187. DownloadPluginAsync(memStream);
  188. });
  189. Logger.log.Debug("Sending request");
  190. yield return req.SendWebRequest();
  191. if (req.isNetworkError)
  192. {
  193. Logger.log.Error("Network error while trying to update mod");
  194. Logger.log.Error(req.error);
  195. yield break;
  196. }
  197. if (req.isHttpError)
  198. {
  199. Logger.log.Error($"Server returned an error code while trying to update mod");
  200. Logger.log.Error(req.error);
  201. }
  202. downloadTask.Wait(); // wait for the damn thing to finish
  203. }
  204. yield return null;
  205. }
  206. }
  207. }