Updater.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. // If build not done, assume no new update are availaible.
  69. if ((string)buildToken["jobs"][0]["status"] != "success")
  70. {
  71. if (showVersionUpToDate)
  72. {
  73. GtkDialog.CreateInfoDialog("Ryujinx - Updater", "You are already using the most updated version of Ryujinx!", "");
  74. }
  75. return;
  76. }
  77. }
  78. }
  79. catch (Exception exception)
  80. {
  81. Logger.Error?.Print(LogClass.Application, exception.Message);
  82. GtkDialog.CreateErrorDialog("An error has occurred when trying to get release information from AppVeyor.");
  83. return;
  84. }
  85. try
  86. {
  87. newVersion = Version.Parse(_buildVer);
  88. }
  89. catch
  90. {
  91. GtkDialog.CreateWarningDialog("Failed to convert the received Ryujinx version from AppVeyor.", "Cancelling Update!");
  92. Logger.Error?.Print(LogClass.Application, "Failed to convert the received Ryujinx version from AppVeyor!");
  93. return;
  94. }
  95. if (newVersion <= currentVersion)
  96. {
  97. if (showVersionUpToDate)
  98. {
  99. GtkDialog.CreateInfoDialog("Ryujinx - Updater", "You are already using the most updated version of Ryujinx!", "");
  100. }
  101. Running = false;
  102. mainWindow.UpdateMenuItem.Sensitive = true;
  103. return;
  104. }
  105. // Show a message asking the user if they want to update
  106. UpdateDialog updateDialog = new UpdateDialog(mainWindow, newVersion, _buildUrl);
  107. updateDialog.Show();
  108. }
  109. public static async Task UpdateRyujinx(UpdateDialog updateDialog, string downloadUrl)
  110. {
  111. // Empty update dir, although it shouldn't ever have anything inside it
  112. if (Directory.Exists(UpdateDir))
  113. {
  114. Directory.Delete(UpdateDir, true);
  115. }
  116. Directory.CreateDirectory(UpdateDir);
  117. string updateFile = Path.Combine(UpdateDir, "update.bin");
  118. // Download the update .zip
  119. updateDialog.MainText.Text = "Downloading Update...";
  120. updateDialog.ProgressBar.Value = 0;
  121. updateDialog.ProgressBar.MaxValue = 100;
  122. using (WebClient client = new WebClient())
  123. {
  124. client.DownloadProgressChanged += (_, args) =>
  125. {
  126. updateDialog.ProgressBar.Value = args.ProgressPercentage;
  127. };
  128. await client.DownloadFileTaskAsync(downloadUrl, updateFile);
  129. }
  130. // Extract Update
  131. updateDialog.MainText.Text = "Extracting Update...";
  132. updateDialog.ProgressBar.Value = 0;
  133. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  134. {
  135. using (Stream inStream = File.OpenRead(updateFile))
  136. using (Stream gzipStream = new GZipInputStream(inStream))
  137. using (TarInputStream tarStream = new TarInputStream(gzipStream))
  138. {
  139. updateDialog.ProgressBar.MaxValue = inStream.Length;
  140. await Task.Run(() =>
  141. {
  142. TarEntry tarEntry;
  143. while ((tarEntry = tarStream.GetNextEntry()) != null)
  144. {
  145. if (tarEntry.IsDirectory) continue;
  146. string outPath = Path.Combine(UpdateDir, tarEntry.Name);
  147. Directory.CreateDirectory(Path.GetDirectoryName(outPath));
  148. using (FileStream outStream = File.OpenWrite(outPath))
  149. {
  150. tarStream.CopyEntryContents(outStream);
  151. }
  152. File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
  153. TarEntry entry = tarEntry;
  154. Application.Invoke(delegate
  155. {
  156. updateDialog.ProgressBar.Value += entry.Size;
  157. });
  158. }
  159. });
  160. updateDialog.ProgressBar.Value = inStream.Length;
  161. }
  162. }
  163. else
  164. {
  165. using (Stream inStream = File.OpenRead(updateFile))
  166. using (ZipFile zipFile = new ZipFile(inStream))
  167. {
  168. updateDialog.ProgressBar.MaxValue = zipFile.Count;
  169. await Task.Run(() =>
  170. {
  171. foreach (ZipEntry zipEntry in zipFile)
  172. {
  173. if (zipEntry.IsDirectory) continue;
  174. string outPath = Path.Combine(UpdateDir, zipEntry.Name);
  175. Directory.CreateDirectory(Path.GetDirectoryName(outPath));
  176. using (Stream zipStream = zipFile.GetInputStream(zipEntry))
  177. using (FileStream outStream = File.OpenWrite(outPath))
  178. {
  179. zipStream.CopyTo(outStream);
  180. }
  181. File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));
  182. Application.Invoke(delegate
  183. {
  184. updateDialog.ProgressBar.Value++;
  185. });
  186. }
  187. });
  188. }
  189. }
  190. // Delete downloaded zip
  191. File.Delete(updateFile);
  192. string[] allFiles = Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories);
  193. updateDialog.MainText.Text = "Renaming Old Files...";
  194. updateDialog.ProgressBar.Value = 0;
  195. updateDialog.ProgressBar.MaxValue = allFiles.Length;
  196. // Replace old files
  197. await Task.Run(() =>
  198. {
  199. foreach (string file in allFiles)
  200. {
  201. if (!Path.GetExtension(file).Equals(".log"))
  202. {
  203. try
  204. {
  205. File.Move(file, file + ".ryuold");
  206. Application.Invoke(delegate
  207. {
  208. updateDialog.ProgressBar.Value++;
  209. });
  210. }
  211. catch
  212. {
  213. Logger.Warning?.Print(LogClass.Application, "Updater wasn't able to rename file: " + file);
  214. }
  215. }
  216. }
  217. Application.Invoke(delegate
  218. {
  219. updateDialog.MainText.Text = "Adding New Files...";
  220. updateDialog.ProgressBar.Value = 0;
  221. updateDialog.ProgressBar.MaxValue = Directory.GetFiles(UpdatePublishDir, "*", SearchOption.AllDirectories).Length;
  222. });
  223. MoveAllFilesOver(UpdatePublishDir, HomeDir, updateDialog);
  224. });
  225. Directory.Delete(UpdateDir, true);
  226. updateDialog.MainText.Text = "Update Complete!";
  227. updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?";
  228. updateDialog.Modal = true;
  229. updateDialog.ProgressBar.Hide();
  230. updateDialog.YesButton.Show();
  231. updateDialog.NoButton.Show();
  232. }
  233. public static bool CanUpdate(bool showWarnings)
  234. {
  235. if (RuntimeInformation.OSArchitecture != Architecture.X64)
  236. {
  237. if (showWarnings)
  238. {
  239. GtkDialog.CreateWarningDialog("You are not running a supported system architecture!", "(Only x64 systems are supported!)");
  240. }
  241. return false;
  242. }
  243. if (!NetworkInterface.GetIsNetworkAvailable())
  244. {
  245. if (showWarnings)
  246. {
  247. GtkDialog.CreateWarningDialog("You are not connected to the Internet!", "Please verify that you have a working Internet connection!");
  248. }
  249. return false;
  250. }
  251. if (Program.Version.Contains("dirty"))
  252. {
  253. if (showWarnings)
  254. {
  255. 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.");
  256. }
  257. return false;
  258. }
  259. return true;
  260. }
  261. private static void MoveAllFilesOver(string root, string dest, UpdateDialog dialog)
  262. {
  263. foreach (string directory in Directory.GetDirectories(root))
  264. {
  265. string dirName = Path.GetFileName(directory);
  266. if (!Directory.Exists(Path.Combine(dest, dirName)))
  267. {
  268. Directory.CreateDirectory(Path.Combine(dest, dirName));
  269. }
  270. MoveAllFilesOver(directory, Path.Combine(dest, dirName), dialog);
  271. }
  272. foreach (string file in Directory.GetFiles(root))
  273. {
  274. File.Move(file, Path.Combine(dest, Path.GetFileName(file)), true);
  275. Application.Invoke(delegate
  276. {
  277. dialog.ProgressBar.Value++;
  278. });
  279. }
  280. }
  281. public static void CleanupUpdate()
  282. {
  283. foreach (string file in Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories))
  284. {
  285. if (Path.GetExtension(file).EndsWith(".ryuold"))
  286. {
  287. File.Delete(file);
  288. }
  289. }
  290. }
  291. }
  292. }