Updater.cs 13 KB

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