Updater.cs 13 KB

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