| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- using Gtk;
- using ICSharpCode.SharpZipLib.GZip;
- using ICSharpCode.SharpZipLib.Tar;
- using ICSharpCode.SharpZipLib.Zip;
- using Newtonsoft.Json.Linq;
- using Ryujinx.Common.Logging;
- using Ryujinx.Ui;
- using Ryujinx.Ui.Widgets;
- using System;
- using System.IO;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ryujinx.Modules
- {
- public static class Updater
- {
- internal static bool Running;
- private static readonly string HomeDir = AppDomain.CurrentDomain.BaseDirectory;
- private static readonly string UpdateDir = Path.Combine(Path.GetTempPath(), "Ryujinx", "update");
- private static readonly string UpdatePublishDir = Path.Combine(UpdateDir, "publish");
- private static string _jobId;
- private static string _buildVer;
- private static string _platformExt;
- private static string _buildUrl;
-
- private const string AppveyorApiUrl = "https://ci.appveyor.com/api";
- public static async Task BeginParse(MainWindow mainWindow, bool showVersionUpToDate)
- {
- if (Running) return;
- Running = true;
- mainWindow.UpdateMenuItem.Sensitive = false;
- // Detect current platform
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- _platformExt = "osx_x64.zip";
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- _platformExt = "win_x64.zip";
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- {
- _platformExt = "linux_x64.tar.gz";
- }
- Version newVersion;
- Version currentVersion;
- try
- {
- currentVersion = Version.Parse(Program.Version);
- }
- catch
- {
- GtkDialog.CreateWarningDialog("Failed to convert the current Ryujinx version.", "Cancelling Update!");
- Logger.Error?.Print(LogClass.Application, "Failed to convert the current Ryujinx version!");
- return;
- }
- // Get latest version number from Appveyor
- try
- {
- using (WebClient jsonClient = new WebClient())
- {
- string fetchedJson = await jsonClient.DownloadStringTaskAsync($"{AppveyorApiUrl}/projects/gdkchan/ryujinx/branch/master");
- JObject jsonRoot = JObject.Parse(fetchedJson);
- JToken buildToken = jsonRoot["build"];
- _jobId = (string)buildToken["jobs"][0]["jobId"];
- _buildVer = (string)buildToken["version"];
- _buildUrl = $"{AppveyorApiUrl}/buildjobs/{_jobId}/artifacts/ryujinx-{_buildVer}-{_platformExt}";
- // If build not done, assume no new update are availaible.
- if ((string)buildToken["jobs"][0]["status"] != "success")
- {
- if (showVersionUpToDate)
- {
- GtkDialog.CreateUpdaterInfoDialog("You are already using the most updated version of Ryujinx!", "");
- }
- return;
- }
- }
- }
- catch (Exception exception)
- {
- Logger.Error?.Print(LogClass.Application, exception.Message);
- GtkDialog.CreateErrorDialog("An error has occurred when trying to get release information from AppVeyor.");
- return;
- }
- try
- {
- newVersion = Version.Parse(_buildVer);
- }
- catch
- {
- GtkDialog.CreateWarningDialog("Failed to convert the received Ryujinx version from AppVeyor.", "Cancelling Update!");
- Logger.Error?.Print(LogClass.Application, "Failed to convert the received Ryujinx version from AppVeyor!");
- return;
- }
- if (newVersion <= currentVersion)
- {
- if (showVersionUpToDate)
- {
- GtkDialog.CreateUpdaterInfoDialog("You are already using the most updated version of Ryujinx!", "");
- }
- Running = false;
- mainWindow.UpdateMenuItem.Sensitive = true;
- return;
- }
- // Show a message asking the user if they want to update
- UpdateDialog updateDialog = new UpdateDialog(mainWindow, newVersion, _buildUrl);
- updateDialog.Show();
- }
- public static async Task UpdateRyujinx(UpdateDialog updateDialog, string downloadUrl)
- {
- // Empty update dir, although it shouldn't ever have anything inside it
- if (Directory.Exists(UpdateDir))
- {
- Directory.Delete(UpdateDir, true);
- }
- Directory.CreateDirectory(UpdateDir);
- string updateFile = Path.Combine(UpdateDir, "update.bin");
- // Download the update .zip
- updateDialog.MainText.Text = "Downloading Update...";
- updateDialog.ProgressBar.Value = 0;
- updateDialog.ProgressBar.MaxValue = 100;
- using (WebClient client = new WebClient())
- {
- client.DownloadProgressChanged += (_, args) =>
- {
- updateDialog.ProgressBar.Value = args.ProgressPercentage;
- };
- await client.DownloadFileTaskAsync(downloadUrl, updateFile);
- }
- // Extract Update
- updateDialog.MainText.Text = "Extracting Update...";
- updateDialog.ProgressBar.Value = 0;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- {
- using (Stream inStream = File.OpenRead(updateFile))
- using (Stream gzipStream = new GZipInputStream(inStream))
- using (TarInputStream tarStream = new TarInputStream(gzipStream, Encoding.ASCII))
- {
- updateDialog.ProgressBar.MaxValue = inStream.Length;
- await Task.Run(() =>
- {
- TarEntry tarEntry;
- while ((tarEntry = tarStream.GetNextEntry()) != null)
- {
- if (tarEntry.IsDirectory) continue;
- string outPath = Path.Combine(UpdateDir, tarEntry.Name);
- Directory.CreateDirectory(Path.GetDirectoryName(outPath));
- using (FileStream outStream = File.OpenWrite(outPath))
- {
- tarStream.CopyEntryContents(outStream);
- }
- File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
- TarEntry entry = tarEntry;
- Application.Invoke(delegate
- {
- updateDialog.ProgressBar.Value += entry.Size;
- });
- }
- });
- updateDialog.ProgressBar.Value = inStream.Length;
- }
- }
- else
- {
- using (Stream inStream = File.OpenRead(updateFile))
- using (ZipFile zipFile = new ZipFile(inStream))
- {
- updateDialog.ProgressBar.MaxValue = zipFile.Count;
- await Task.Run(() =>
- {
- foreach (ZipEntry zipEntry in zipFile)
- {
- if (zipEntry.IsDirectory) continue;
- string outPath = Path.Combine(UpdateDir, zipEntry.Name);
- Directory.CreateDirectory(Path.GetDirectoryName(outPath));
- using (Stream zipStream = zipFile.GetInputStream(zipEntry))
- using (FileStream outStream = File.OpenWrite(outPath))
- {
- zipStream.CopyTo(outStream);
- }
- File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));
- Application.Invoke(delegate
- {
- updateDialog.ProgressBar.Value++;
- });
- }
- });
- }
- }
- // Delete downloaded zip
- File.Delete(updateFile);
- string[] allFiles = Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories);
- updateDialog.MainText.Text = "Renaming Old Files...";
- updateDialog.ProgressBar.Value = 0;
- updateDialog.ProgressBar.MaxValue = allFiles.Length;
- // Replace old files
- await Task.Run(() =>
- {
- foreach (string file in allFiles)
- {
- if (!Path.GetExtension(file).Equals(".log"))
- {
- try
- {
- File.Move(file, file + ".ryuold");
- Application.Invoke(delegate
- {
- updateDialog.ProgressBar.Value++;
- });
- }
- catch
- {
- Logger.Warning?.Print(LogClass.Application, "Updater wasn't able to rename file: " + file);
- }
- }
- }
- Application.Invoke(delegate
- {
- updateDialog.MainText.Text = "Adding New Files...";
- updateDialog.ProgressBar.Value = 0;
- updateDialog.ProgressBar.MaxValue = Directory.GetFiles(UpdatePublishDir, "*", SearchOption.AllDirectories).Length;
- });
- MoveAllFilesOver(UpdatePublishDir, HomeDir, updateDialog);
- });
- Directory.Delete(UpdateDir, true);
- updateDialog.MainText.Text = "Update Complete!";
- updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?";
- updateDialog.Modal = true;
- updateDialog.ProgressBar.Hide();
- updateDialog.YesButton.Show();
- updateDialog.NoButton.Show();
- }
- public static bool CanUpdate(bool showWarnings)
- {
- if (RuntimeInformation.OSArchitecture != Architecture.X64)
- {
- if (showWarnings)
- {
- GtkDialog.CreateWarningDialog("You are not running a supported system architecture!", "(Only x64 systems are supported!)");
- }
- return false;
- }
- if (!NetworkInterface.GetIsNetworkAvailable())
- {
- if (showWarnings)
- {
- GtkDialog.CreateWarningDialog("You are not connected to the Internet!", "Please verify that you have a working Internet connection!");
- }
- return false;
- }
- if (Program.Version.Contains("dirty"))
- {
- if (showWarnings)
- {
- 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.");
- }
- return false;
- }
- return true;
- }
- private static void MoveAllFilesOver(string root, string dest, UpdateDialog dialog)
- {
- foreach (string directory in Directory.GetDirectories(root))
- {
- string dirName = Path.GetFileName(directory);
- if (!Directory.Exists(Path.Combine(dest, dirName)))
- {
- Directory.CreateDirectory(Path.Combine(dest, dirName));
- }
- MoveAllFilesOver(directory, Path.Combine(dest, dirName), dialog);
- }
- foreach (string file in Directory.GetFiles(root))
- {
- File.Move(file, Path.Combine(dest, Path.GetFileName(file)), true);
- Application.Invoke(delegate
- {
- dialog.ProgressBar.Value++;
- });
- }
- }
- public static void CleanupUpdate()
- {
- foreach (string file in Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories))
- {
- if (Path.GetExtension(file).EndsWith(".ryuold"))
- {
- File.Delete(file);
- }
- }
- }
- }
- }
|