Updater.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using Gtk;
  2. using ICSharpCode.SharpZipLib.GZip;
  3. using ICSharpCode.SharpZipLib.Tar;
  4. using ICSharpCode.SharpZipLib.Zip;
  5. using Newtonsoft.Json.Linq;
  6. using Ryujinx.Common.Logging;
  7. using Ryujinx.Ui;
  8. using System;
  9. using System.IO;
  10. using System.Net;
  11. using System.Net.NetworkInformation;
  12. using System.Runtime.InteropServices;
  13. using System.Threading.Tasks;
  14. namespace Ryujinx
  15. {
  16. public static class Updater
  17. {
  18. internal static bool Running;
  19. private static readonly string HomeDir = AppDomain.CurrentDomain.BaseDirectory;
  20. private static readonly string UpdateDir = Path.Combine(Path.GetTempPath(), "Ryujinx", "update");
  21. private static readonly string UpdatePublishDir = Path.Combine(UpdateDir, "publish");
  22. private static string _jobId;
  23. private static string _buildVer;
  24. private static string _platformExt;
  25. private static string _buildUrl;
  26. private const string AppveyorApiUrl = "https://ci.appveyor.com/api";
  27. public static async Task BeginParse(MainWindow mainWindow, bool showVersionUpToDate)
  28. {
  29. if (Running) return;
  30. Running = true;
  31. mainWindow.UpdateMenuItem.Sensitive = false;
  32. // Detect current platform
  33. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  34. {
  35. _platformExt = "osx_x64.zip";
  36. }
  37. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  38. {
  39. _platformExt = "win_x64.zip";
  40. }
  41. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  42. {
  43. _platformExt = "linux_x64.tar.gz";
  44. }
  45. Version newVersion;
  46. Version currentVersion;
  47. try
  48. {
  49. currentVersion = Version.Parse(Program.Version);
  50. }
  51. catch
  52. {
  53. GtkDialog.CreateWarningDialog("Failed to convert the current Ryujinx version.", "Cancelling Update!");
  54. Logger.Error?.Print(LogClass.Application, "Failed to convert the current Ryujinx version!");
  55. return;
  56. }
  57. // Get latest version number from Appveyor
  58. try
  59. {
  60. using (WebClient jsonClient = new WebClient())
  61. {
  62. string fetchedJson = await jsonClient.DownloadStringTaskAsync($"{AppveyorApiUrl}/projects/gdkchan/ryujinx/branch/master");
  63. JObject jsonRoot = JObject.Parse(fetchedJson);
  64. JToken buildToken = jsonRoot["build"];
  65. _jobId = (string)buildToken["jobs"][0]["jobId"];
  66. _buildVer = (string)buildToken["version"];
  67. _buildUrl = $"{AppveyorApiUrl}/buildjobs/{_jobId}/artifacts/ryujinx-{_buildVer}-{_platformExt}";
  68. }
  69. }
  70. catch (Exception exception)
  71. {
  72. Logger.Error?.Print(LogClass.Application, exception.Message);
  73. GtkDialog.CreateErrorDialog("An error has occurred when trying to get release information from AppVeyor.");
  74. return;
  75. }
  76. try
  77. {
  78. newVersion = Version.Parse(_buildVer);
  79. }
  80. catch
  81. {
  82. GtkDialog.CreateWarningDialog("Failed to convert the received Ryujinx version from AppVeyor.", "Cancelling Update!");
  83. Logger.Error?.Print(LogClass.Application, "Failed to convert the received Ryujinx version from AppVeyor!");
  84. return;
  85. }
  86. if (newVersion <= currentVersion)
  87. {
  88. if (showVersionUpToDate)
  89. {
  90. GtkDialog.CreateInfoDialog("Ryujinx - Updater", "You are already using the most updated version of Ryujinx!", "");
  91. }
  92. Running = false;
  93. mainWindow.UpdateMenuItem.Sensitive = true;
  94. return;
  95. }
  96. // Show a message asking the user if they want to update
  97. UpdateDialog updateDialog = new UpdateDialog(mainWindow, newVersion, _buildUrl);
  98. updateDialog.Show();
  99. }
  100. public static async Task UpdateRyujinx(UpdateDialog updateDialog, string downloadUrl)
  101. {
  102. // Empty update dir, although it shouldn't ever have anything inside it
  103. if (Directory.Exists(UpdateDir))
  104. {
  105. Directory.Delete(UpdateDir, true);
  106. }
  107. Directory.CreateDirectory(UpdateDir);
  108. string updateFile = Path.Combine(UpdateDir, "update.bin");
  109. // Download the update .zip
  110. updateDialog.MainText.Text = "Downloading Update...";
  111. updateDialog.ProgressBar.Value = 0;
  112. updateDialog.ProgressBar.MaxValue = 100;
  113. using (WebClient client = new WebClient())
  114. {
  115. client.DownloadProgressChanged += (_, args) =>
  116. {
  117. updateDialog.ProgressBar.Value = args.ProgressPercentage;
  118. };
  119. await client.DownloadFileTaskAsync(downloadUrl, updateFile);
  120. }
  121. // Extract Update
  122. updateDialog.MainText.Text = "Extracting Update...";
  123. updateDialog.ProgressBar.Value = 0;
  124. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  125. {
  126. using (Stream inStream = File.OpenRead(updateFile))
  127. using (Stream gzipStream = new GZipInputStream(inStream))
  128. using (TarInputStream tarStream = new TarInputStream(gzipStream))
  129. {
  130. updateDialog.ProgressBar.MaxValue = inStream.Length;
  131. await Task.Run(() =>
  132. {
  133. TarEntry tarEntry;
  134. while ((tarEntry = tarStream.GetNextEntry()) != null)
  135. {
  136. if (tarEntry.IsDirectory) continue;
  137. string outPath = Path.Combine(UpdateDir, tarEntry.Name);
  138. Directory.CreateDirectory(Path.GetDirectoryName(outPath));
  139. using (FileStream outStream = File.OpenWrite(outPath))
  140. {
  141. tarStream.CopyEntryContents(outStream);
  142. }
  143. File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
  144. TarEntry entry = tarEntry;
  145. Application.Invoke(delegate
  146. {
  147. updateDialog.ProgressBar.Value += entry.Size;
  148. });
  149. }
  150. });
  151. updateDialog.ProgressBar.Value = inStream.Length;
  152. }
  153. }
  154. else
  155. {
  156. using (Stream inStream = File.OpenRead(updateFile))
  157. using (ZipFile zipFile = new ZipFile(inStream))
  158. {
  159. updateDialog.ProgressBar.MaxValue = zipFile.Count;
  160. await Task.Run(() =>
  161. {
  162. foreach (ZipEntry zipEntry in zipFile)
  163. {
  164. if (zipEntry.IsDirectory) continue;
  165. string outPath = Path.Combine(UpdateDir, zipEntry.Name);
  166. Directory.CreateDirectory(Path.GetDirectoryName(outPath));
  167. using (Stream zipStream = zipFile.GetInputStream(zipEntry))
  168. using (FileStream outStream = File.OpenWrite(outPath))
  169. {
  170. zipStream.CopyTo(outStream);
  171. }
  172. File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));
  173. Application.Invoke(delegate
  174. {
  175. updateDialog.ProgressBar.Value++;
  176. });
  177. }
  178. });
  179. }
  180. }
  181. // Delete downloaded zip
  182. File.Delete(updateFile);
  183. string[] allFiles = Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories);
  184. updateDialog.MainText.Text = "Renaming Old Files...";
  185. updateDialog.ProgressBar.Value = 0;
  186. updateDialog.ProgressBar.MaxValue = allFiles.Length;
  187. // Replace old files
  188. await Task.Run(() =>
  189. {
  190. foreach (string file in allFiles)
  191. {
  192. if (!Path.GetExtension(file).Equals(".log"))
  193. {
  194. try
  195. {
  196. File.Move(file, file + ".ryuold");
  197. Application.Invoke(delegate
  198. {
  199. updateDialog.ProgressBar.Value++;
  200. });
  201. }
  202. catch
  203. {
  204. Logger.Warning?.Print(LogClass.Application, "Updater wasn't able to rename file: " + file);
  205. }
  206. }
  207. }
  208. Application.Invoke(delegate
  209. {
  210. updateDialog.MainText.Text = "Adding New Files...";
  211. updateDialog.ProgressBar.Value = 0;
  212. updateDialog.ProgressBar.MaxValue = Directory.GetFiles(UpdatePublishDir, "*", SearchOption.AllDirectories).Length;
  213. });
  214. MoveAllFilesOver(UpdatePublishDir, HomeDir, updateDialog);
  215. });
  216. Directory.Delete(UpdateDir, true);
  217. updateDialog.MainText.Text = "Update Complete!";
  218. updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?";
  219. updateDialog.Modal = true;
  220. updateDialog.ProgressBar.Hide();
  221. updateDialog.YesButton.Show();
  222. updateDialog.NoButton.Show();
  223. }
  224. public static bool CanUpdate(bool showWarnings)
  225. {
  226. if (RuntimeInformation.OSArchitecture != Architecture.X64)
  227. {
  228. if (showWarnings)
  229. {
  230. GtkDialog.CreateWarningDialog("You are not running a supported system architecture!", "(Only x64 systems are supported!)");
  231. }
  232. return false;
  233. }
  234. if (!NetworkInterface.GetIsNetworkAvailable())
  235. {
  236. if (showWarnings)
  237. {
  238. GtkDialog.CreateWarningDialog("You are not connected to the Internet!", "Please verify that you have a working Internet connection!");
  239. }
  240. return false;
  241. }
  242. if (Program.Version.Contains("dirty"))
  243. {
  244. if (showWarnings)
  245. {
  246. GtkDialog.CreateWarningDialog("You Cannot update a Dirty build of Ryujinx!", "Please download Ryujinx at https://ryujinx.org/ if you are looking for a supported version.");
  247. }
  248. return false;
  249. }
  250. return true;
  251. }
  252. private static void MoveAllFilesOver(string root, string dest, UpdateDialog dialog)
  253. {
  254. foreach (string directory in Directory.GetDirectories(root))
  255. {
  256. string dirName = Path.GetFileName(directory);
  257. if (!Directory.Exists(Path.Combine(dest, dirName)))
  258. {
  259. Directory.CreateDirectory(Path.Combine(dest, dirName));
  260. }
  261. MoveAllFilesOver(directory, Path.Combine(dest, dirName), dialog);
  262. }
  263. foreach (string file in Directory.GetFiles(root))
  264. {
  265. File.Move(file, Path.Combine(dest, Path.GetFileName(file)), true);
  266. Application.Invoke(delegate
  267. {
  268. dialog.ProgressBar.Value++;
  269. });
  270. }
  271. }
  272. public static void CleanupUpdate()
  273. {
  274. foreach (string file in Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories))
  275. {
  276. if (Path.GetExtension(file).EndsWith(".ryuold"))
  277. {
  278. File.Delete(file);
  279. }
  280. }
  281. }
  282. }
  283. }