2022-10-03 16:25:25 +02:00
|
|
|
using Avalonia.Controls;
|
2022-05-15 13:30:15 +02:00
|
|
|
using Avalonia.Threading;
|
2022-10-03 16:25:25 +02:00
|
|
|
using FluentAvalonia.UI.Controls;
|
2022-05-15 13:30:15 +02:00
|
|
|
using ICSharpCode.SharpZipLib.GZip;
|
|
|
|
using ICSharpCode.SharpZipLib.Tar;
|
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Ryujinx.Ava;
|
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2022-12-29 15:24:05 +01:00
|
|
|
using Ryujinx.Ava.UI.Helpers;
|
2022-05-15 13:30:15 +02:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Logging;
|
2022-12-12 15:17:22 +01:00
|
|
|
using Ryujinx.Ui.Common.Helper;
|
2022-05-15 13:30:15 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-10-03 16:25:25 +02:00
|
|
|
using System.Diagnostics;
|
2022-05-15 13:30:15 +02:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.NetworkInformation;
|
2022-10-03 16:25:25 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
2022-05-15 13:30:15 +02:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Ryujinx.Modules
|
|
|
|
{
|
2022-07-05 20:06:31 +02:00
|
|
|
internal static class Updater
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
private const string GitHubApiURL = "https://api.github.com";
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
private static readonly string HomeDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
private static readonly string UpdateDir = Path.Combine(Path.GetTempPath(), "Ryujinx", "update");
|
2022-05-15 13:30:15 +02:00
|
|
|
private static readonly string UpdatePublishDir = Path.Combine(UpdateDir, "publish");
|
2023-01-20 21:30:21 +01:00
|
|
|
private static readonly int ConnectionCount = 4;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
private static string _buildVer;
|
|
|
|
private static string _platformExt;
|
|
|
|
private static string _buildUrl;
|
2023-01-20 21:30:21 +01:00
|
|
|
private static long _buildSize;
|
|
|
|
private static bool _updateSuccessful;
|
|
|
|
private static bool _running;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
private static readonly string[] WindowsDependencyDirs = Array.Empty<string>();
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
public static async Task BeginParse(Window mainWindow, bool showVersionUpToDate)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
if (_running)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_running = true;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
// Detect current platform
|
|
|
|
if (OperatingSystem.IsMacOS())
|
|
|
|
{
|
|
|
|
_platformExt = "osx_x64.zip";
|
|
|
|
}
|
|
|
|
else if (OperatingSystem.IsWindows())
|
|
|
|
{
|
|
|
|
_platformExt = "win_x64.zip";
|
|
|
|
}
|
|
|
|
else if (OperatingSystem.IsLinux())
|
|
|
|
{
|
|
|
|
_platformExt = "linux_x64.tar.gz";
|
|
|
|
}
|
|
|
|
|
|
|
|
Version newVersion;
|
|
|
|
Version currentVersion;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
currentVersion = Version.Parse(Program.Version);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
Logger.Error?.Print(LogClass.Application, "Failed to convert the current Ryujinx version!");
|
2023-01-20 21:30:21 +01:00
|
|
|
|
2022-07-12 00:25:33 +02:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterConvertFailedMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterCancelUpdateMessage]);
|
2022-07-12 00:25:33 +02:00
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_running = false;
|
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get latest version number from GitHub API
|
|
|
|
try
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
using HttpClient jsonClient = ConstructHttpClient();
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
string buildInfoURL = $"{GitHubApiURL}/repos/{ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelRepo}/releases/latest";
|
|
|
|
string fetchedJson = await jsonClient.GetStringAsync(buildInfoURL);
|
|
|
|
JObject jsonRoot = JObject.Parse(fetchedJson);
|
|
|
|
JToken assets = jsonRoot["assets"];
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_buildVer = (string)jsonRoot["name"];
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
foreach (JToken asset in assets)
|
|
|
|
{
|
|
|
|
string assetName = (string)asset["name"];
|
|
|
|
string assetState = (string)asset["state"];
|
|
|
|
string downloadURL = (string)asset["browser_download_url"];
|
|
|
|
|
|
|
|
if (assetName.StartsWith("test-ava-ryujinx") && assetName.EndsWith(_platformExt))
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
_buildUrl = downloadURL;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
if (assetState != "uploaded")
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
if (showVersionUpToDate)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
await ContentDialogHelper.CreateUpdaterInfoDialog(LocaleManager.Instance[LocaleKeys.DialogUpdaterAlreadyOnLatestVersionMessage], "");
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_running = false;
|
|
|
|
|
|
|
|
return;
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
|
|
|
|
break;
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
// If build not done, assume no new update are available.
|
|
|
|
if (_buildUrl is null)
|
2023-01-20 21:30:21 +01:00
|
|
|
{
|
|
|
|
if (showVersionUpToDate)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
await ContentDialogHelper.CreateUpdaterInfoDialog(LocaleManager.Instance[LocaleKeys.DialogUpdaterAlreadyOnLatestVersionMessage], "");
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
|
|
|
|
_running = false;
|
|
|
|
|
|
|
|
return;
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
Logger.Error?.Print(LogClass.Application, exception.Message);
|
2023-01-20 21:30:21 +01:00
|
|
|
|
2022-07-12 00:25:33 +02:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdaterFailedToGetVersionMessage]);
|
2022-07-12 00:25:33 +02:00
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_running = false;
|
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
newVersion = Version.Parse(_buildVer);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
Logger.Error?.Print(LogClass.Application, "Failed to convert the received Ryujinx version from Github!");
|
2023-01-20 21:30:21 +01:00
|
|
|
|
2022-07-12 00:25:33 +02:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterConvertFailedGithubMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterCancelUpdateMessage]);
|
2022-07-12 00:25:33 +02:00
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_running = false;
|
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newVersion <= currentVersion)
|
|
|
|
{
|
|
|
|
if (showVersionUpToDate)
|
|
|
|
{
|
2022-07-12 00:25:33 +02:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
await ContentDialogHelper.CreateUpdaterInfoDialog(LocaleManager.Instance[LocaleKeys.DialogUpdaterAlreadyOnLatestVersionMessage], "");
|
2022-07-12 00:25:33 +02:00
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_running = false;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch build size information to learn chunk sizes.
|
|
|
|
using (HttpClient buildSizeClient = ConstructHttpClient())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
buildSizeClient.DefaultRequestHeaders.Add("Range", "bytes=0-0");
|
|
|
|
|
|
|
|
HttpResponseMessage message = await buildSizeClient.GetAsync(new Uri(_buildUrl), HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
|
|
|
|
_buildSize = message.Content.Headers.ContentRange.Length.Value;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Application, ex.Message);
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "Couldn't determine build size for update, using single-threaded updater");
|
|
|
|
|
|
|
|
_buildSize = -1;
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 16:25:25 +02:00
|
|
|
|
2022-07-12 00:25:33 +02:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
// Show a message asking the user if they want to update
|
2023-01-20 21:30:21 +01:00
|
|
|
var shouldUpdate = await ContentDialogHelper.CreateChoiceDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.RyujinxUpdater],
|
2023-01-03 19:45:08 +01:00
|
|
|
LocaleManager.Instance[LocaleKeys.RyujinxUpdaterMessage],
|
2022-10-03 16:25:25 +02:00
|
|
|
$"{Program.Version} -> {newVersion}");
|
|
|
|
|
|
|
|
if (shouldUpdate)
|
|
|
|
{
|
|
|
|
UpdateRyujinx(mainWindow, _buildUrl);
|
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_running = false;
|
|
|
|
}
|
2022-07-12 00:25:33 +02:00
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static HttpClient ConstructHttpClient()
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
HttpClient result = new();
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
// Required by GitHub to interact with APIs.
|
2022-05-15 13:30:15 +02:00
|
|
|
result.DefaultRequestHeaders.Add("User-Agent", "Ryujinx-Updater/1.0.0");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
private static async void UpdateRyujinx(Window parent, string downloadUrl)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
_updateSuccessful = false;
|
2022-10-03 16:25:25 +02:00
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
// 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");
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
TaskDialog taskDialog = new()
|
2022-10-03 16:25:25 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Header = LocaleManager.Instance[LocaleKeys.RyujinxUpdater],
|
|
|
|
SubHeader = LocaleManager.Instance[LocaleKeys.UpdaterDownloading],
|
|
|
|
IconSource = new SymbolIconSource { Symbol = Symbol.Download },
|
|
|
|
Buttons = { },
|
|
|
|
ShowProgressBar = true,
|
|
|
|
XamlRoot = parent
|
2022-10-03 16:25:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
taskDialog.Opened += (s, e) =>
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
if (_buildSize >= 0)
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
DoUpdateWithMultipleThreads(taskDialog, downloadUrl, updateFile);
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
DoUpdateWithSingleThread(taskDialog, downloadUrl, updateFile);
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
2022-10-03 16:25:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
await taskDialog.ShowAsync(true);
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
if (_updateSuccessful)
|
2022-10-03 16:25:25 +02:00
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
var shouldRestart = await ContentDialogHelper.CreateChoiceDialog(LocaleManager.Instance[LocaleKeys.RyujinxUpdater],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterCompleteMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterRestartMessage]);
|
2022-10-03 16:25:25 +02:00
|
|
|
|
|
|
|
if (shouldRestart)
|
|
|
|
{
|
|
|
|
string ryuName = Path.GetFileName(Environment.ProcessPath);
|
2023-01-20 21:30:21 +01:00
|
|
|
string ryuExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ryuName);
|
2022-10-03 16:25:25 +02:00
|
|
|
|
2022-12-12 15:17:22 +01:00
|
|
|
if (!Path.Exists(ryuExe))
|
2022-10-03 16:25:25 +02:00
|
|
|
{
|
2022-12-12 15:17:22 +01:00
|
|
|
ryuExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx");
|
2022-10-03 16:25:25 +02:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:17:22 +01:00
|
|
|
Process.Start(ryuExe, CommandLineState.Arguments);
|
2022-10-03 16:25:25 +02:00
|
|
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
private static void DoUpdateWithMultipleThreads(TaskDialog taskDialog, string downloadUrl, string updateFile)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
// Multi-Threaded Updater
|
2023-01-20 21:30:21 +01:00
|
|
|
long chunkSize = _buildSize / ConnectionCount;
|
2022-05-15 13:30:15 +02:00
|
|
|
long remainderChunk = _buildSize % ConnectionCount;
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
int completedRequests = 0;
|
|
|
|
int totalProgressPercentage = 0;
|
|
|
|
int[] progressPercentage = new int[ConnectionCount];
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
List<byte[]> list = new(ConnectionCount);
|
|
|
|
List<WebClient> webClients = new(ConnectionCount);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < ConnectionCount; i++)
|
|
|
|
{
|
|
|
|
list.Add(Array.Empty<byte>());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ConnectionCount; i++)
|
|
|
|
{
|
|
|
|
#pragma warning disable SYSLIB0014
|
|
|
|
// TODO: WebClient is obsolete and need to be replaced with a more complex logic using HttpClient.
|
2023-01-20 21:30:21 +01:00
|
|
|
using WebClient client = new();
|
2022-05-15 13:30:15 +02:00
|
|
|
#pragma warning restore SYSLIB0014
|
2023-01-20 21:30:21 +01:00
|
|
|
|
|
|
|
webClients.Add(client);
|
|
|
|
|
|
|
|
if (i == ConnectionCount - 1)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
client.Headers.Add("Range", $"bytes={chunkSize * i}-{(chunkSize * (i + 1) - 1) + remainderChunk}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
client.Headers.Add("Range", $"bytes={chunkSize * i}-{chunkSize * (i + 1) - 1}");
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
client.DownloadProgressChanged += (_, args) =>
|
|
|
|
{
|
|
|
|
int index = (int)args.UserState;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
Interlocked.Add(ref totalProgressPercentage, -1 * progressPercentage[index]);
|
|
|
|
Interlocked.Exchange(ref progressPercentage[index], args.ProgressPercentage);
|
|
|
|
Interlocked.Add(ref totalProgressPercentage, args.ProgressPercentage);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
taskDialog.SetProgressBarState(totalProgressPercentage / ConnectionCount, TaskDialogProgressState.Normal);
|
|
|
|
};
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
client.DownloadDataCompleted += (_, args) =>
|
|
|
|
{
|
|
|
|
int index = (int)args.UserState;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
if (args.Cancelled)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
webClients[index].Dispose();
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
taskDialog.Hide();
|
2022-10-03 16:25:25 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
list[index] = args.Result;
|
|
|
|
Interlocked.Increment(ref completedRequests);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
if (Equals(completedRequests, ConnectionCount))
|
|
|
|
{
|
|
|
|
byte[] mergedFileBytes = new byte[_buildSize];
|
|
|
|
for (int connectionIndex = 0, destinationOffset = 0; connectionIndex < ConnectionCount; connectionIndex++)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Array.Copy(list[connectionIndex], 0, mergedFileBytes, destinationOffset, list[connectionIndex].Length);
|
|
|
|
destinationOffset += list[connectionIndex].Length;
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
File.WriteAllBytes(updateFile, mergedFileBytes);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
InstallUpdate(taskDialog, updateFile);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Application, e.Message);
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
DoUpdateWithSingleThread(taskDialog, downloadUrl, updateFile);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
return;
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
}
|
|
|
|
};
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
client.DownloadDataAsync(new Uri(downloadUrl), i);
|
|
|
|
}
|
|
|
|
catch (WebException ex)
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.Application, ex.Message);
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
|
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
foreach (WebClient webClient in webClients)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-02-15 23:36:35 +01:00
|
|
|
webClient.CancelAsync();
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
DoUpdateWithSingleThread(taskDialog, downloadUrl, updateFile);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
return;
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
private static void DoUpdateWithSingleThreadWorker(TaskDialog taskDialog, string downloadUrl, string updateFile)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
using HttpClient client = new();
|
|
|
|
// We do not want to timeout while downloading
|
|
|
|
client.Timeout = TimeSpan.FromDays(1);
|
|
|
|
|
|
|
|
using (HttpResponseMessage response = client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead).Result)
|
|
|
|
using (Stream remoteFileStream = response.Content.ReadAsStreamAsync().Result)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
using Stream updateFileStream = File.Open(updateFile, FileMode.Create);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
long totalBytes = response.Content.Headers.ContentLength.Value;
|
|
|
|
long byteWritten = 0;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
byte[] buffer = new byte[32 * 1024];
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int readSize = remoteFileStream.Read(buffer);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
if (readSize == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
byteWritten += readSize;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
taskDialog.SetProgressBarState(GetPercentage(byteWritten, totalBytes), TaskDialogProgressState.Normal);
|
2022-10-03 16:25:25 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
updateFileStream.Write(buffer, 0, readSize);
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
|
|
|
|
InstallUpdate(taskDialog, updateFile);
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static double GetPercentage(double value, double max)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
return max == 0 ? 0 : value / max * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DoUpdateWithSingleThread(TaskDialog taskDialog, string downloadUrl, string updateFile)
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Thread worker = new(() => DoUpdateWithSingleThreadWorker(taskDialog, downloadUrl, updateFile))
|
|
|
|
{
|
|
|
|
Name = "Updater.SingleThreadWorker"
|
|
|
|
};
|
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
worker.Start();
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
private static async void InstallUpdate(TaskDialog taskDialog, string updateFile)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
// Extract Update
|
2023-01-03 19:45:08 +01:00
|
|
|
taskDialog.SubHeader = LocaleManager.Instance[LocaleKeys.UpdaterExtracting];
|
2022-10-03 16:25:25 +02:00
|
|
|
taskDialog.SetProgressBarState(0, TaskDialogProgressState.Normal);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
if (OperatingSystem.IsLinux())
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
using Stream inStream = File.OpenRead(updateFile);
|
|
|
|
using GZipInputStream gzipStream = new(inStream);
|
|
|
|
using TarInputStream tarStream = new(gzipStream, Encoding.ASCII);
|
|
|
|
|
|
|
|
await Task.Run(() =>
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
TarEntry tarEntry;
|
2023-02-15 23:36:35 +01:00
|
|
|
|
|
|
|
if (!OperatingSystem.IsWindows())
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-02-15 23:36:35 +01:00
|
|
|
while ((tarEntry = tarStream.GetNextEntry()) is not null)
|
|
|
|
{
|
|
|
|
if (tarEntry.IsDirectory) continue;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
string outPath = Path.Combine(UpdateDir, tarEntry.Name);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
using (FileStream outStream = File.OpenWrite(outPath))
|
|
|
|
{
|
|
|
|
tarStream.CopyEntryContents(outStream);
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
File.SetUnixFileMode(outPath, (UnixFileMode)tarEntry.TarHeader.Mode);
|
|
|
|
File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-02-15 23:36:35 +01:00
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
{
|
|
|
|
taskDialog.SetProgressBarState(GetPercentage(tarEntry.Size, inStream.Length), TaskDialogProgressState.Normal);
|
|
|
|
});
|
|
|
|
}
|
2023-01-20 21:30:21 +01:00
|
|
|
}
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
taskDialog.SetProgressBarState(100, TaskDialogProgressState.Normal);
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
using Stream inStream = File.OpenRead(updateFile);
|
|
|
|
using ZipFile zipFile = new(inStream);
|
|
|
|
|
|
|
|
await Task.Run(() =>
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
double count = 0;
|
|
|
|
foreach (ZipEntry zipEntry in zipFile)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
count++;
|
|
|
|
if (zipEntry.IsDirectory) continue;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
string outPath = Path.Combine(UpdateDir, zipEntry.Name);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
using (Stream zipStream = zipFile.GetInputStream(zipEntry))
|
|
|
|
using (FileStream outStream = File.OpenWrite(outPath))
|
|
|
|
{
|
|
|
|
zipStream.CopyTo(outStream);
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
{
|
|
|
|
taskDialog.SetProgressBarState(GetPercentage(count, zipFile.Count), TaskDialogProgressState.Normal);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete downloaded zip
|
|
|
|
File.Delete(updateFile);
|
|
|
|
|
|
|
|
List<string> allFiles = EnumerateFilesToDelete().ToList();
|
|
|
|
|
2023-01-03 19:45:08 +01:00
|
|
|
taskDialog.SubHeader = LocaleManager.Instance[LocaleKeys.UpdaterRenaming];
|
2022-10-03 16:25:25 +02:00
|
|
|
taskDialog.SetProgressBarState(0, TaskDialogProgressState.Normal);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
// Replace old files
|
|
|
|
await Task.Run(() =>
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
double count = 0;
|
2022-05-15 13:30:15 +02:00
|
|
|
foreach (string file in allFiles)
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
count++;
|
2022-05-15 13:30:15 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
File.Move(file, file + ".ryuold");
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
taskDialog.SetProgressBarState(GetPercentage(count, allFiles.Count), TaskDialogProgressState.Normal);
|
2022-05-15 13:30:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2023-01-21 02:06:19 +01:00
|
|
|
Logger.Warning?.Print(LogClass.Application, LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.UpdaterRenameFailed, file));
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(() =>
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
taskDialog.SubHeader = LocaleManager.Instance[LocaleKeys.UpdaterAddingFiles];
|
2022-10-03 16:25:25 +02:00
|
|
|
taskDialog.SetProgressBarState(0, TaskDialogProgressState.Normal);
|
2022-05-15 13:30:15 +02:00
|
|
|
});
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
MoveAllFilesOver(UpdatePublishDir, HomeDir, taskDialog);
|
2022-05-15 13:30:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Directory.Delete(UpdateDir, true);
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
_updateSuccessful = true;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
taskDialog.Hide();
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 21:30:21 +01:00
|
|
|
public static bool CanUpdate(bool showWarnings)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
#if !DISABLE_UPDATER
|
|
|
|
if (RuntimeInformation.OSArchitecture != Architecture.X64)
|
|
|
|
{
|
|
|
|
if (showWarnings)
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterArchNotSupportedMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterArchNotSupportedSubMessage]);
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
|
|
{
|
|
|
|
if (showWarnings)
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterNoInternetMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterNoInternetSubMessage]);
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-07 09:06:13 +01:00
|
|
|
if (Program.Version.Contains("dirty") || !ReleaseInformation.IsValid())
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
|
|
|
if (showWarnings)
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterDirtyBuildMessage],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterDirtyBuildSubMessage]);
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
if (showWarnings)
|
|
|
|
{
|
2023-01-07 09:06:13 +01:00
|
|
|
if (ReleaseInformation.IsFlatHubBuild())
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.UpdaterDisabledWarningTitle],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterFlatpakNotSupportedMessage]);
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
|
|
|
await ContentDialogHelper.CreateWarningDialog(
|
|
|
|
LocaleManager.Instance[LocaleKeys.UpdaterDisabledWarningTitle],
|
|
|
|
LocaleManager.Instance[LocaleKeys.DialogUpdaterDirtyBuildSubMessage]);
|
|
|
|
});
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This method should always reflect the latest build layout.s
|
|
|
|
private static IEnumerable<string> EnumerateFilesToDelete()
|
|
|
|
{
|
|
|
|
var files = Directory.EnumerateFiles(HomeDir); // All files directly in base dir.
|
|
|
|
|
|
|
|
if (OperatingSystem.IsWindows())
|
|
|
|
{
|
|
|
|
foreach (string dir in WindowsDependencyDirs)
|
|
|
|
{
|
|
|
|
string dirPath = Path.Combine(HomeDir, dir);
|
|
|
|
if (Directory.Exists(dirPath))
|
|
|
|
{
|
|
|
|
files = files.Concat(Directory.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
private static void MoveAllFilesOver(string root, string dest, TaskDialog taskDialog)
|
2022-05-15 13:30:15 +02:00
|
|
|
{
|
2023-01-20 21:30:21 +01:00
|
|
|
int total = Directory.GetFiles(root, "*", SearchOption.AllDirectories).Length;
|
2022-05-15 13:30:15 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
MoveAllFilesOver(directory, Path.Combine(dest, dirName), taskDialog);
|
2022-05-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:25:25 +02:00
|
|
|
double count = 0;
|
2022-05-15 13:30:15 +02:00
|
|
|
foreach (string file in Directory.GetFiles(root))
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
count++;
|
2023-01-20 21:30:21 +01:00
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
File.Move(file, Path.Combine(dest, Path.GetFileName(file)), true);
|
|
|
|
|
|
|
|
Dispatcher.UIThread.InvokeAsync(() =>
|
|
|
|
{
|
2022-10-03 16:25:25 +02:00
|
|
|
taskDialog.SetProgressBarState(GetPercentage(count, total), TaskDialogProgressState.Normal);
|
2022-05-15 13:30:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void CleanupUpdate()
|
|
|
|
{
|
|
|
|
foreach (string file in Directory.GetFiles(HomeDir, "*.ryuold", SearchOption.AllDirectories))
|
|
|
|
{
|
|
|
|
File.Delete(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-07 09:06:13 +01:00
|
|
|
}
|