2020-09-29 22:05:25 +02:00
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 ;
2021-01-08 09:14:13 +01:00
using Ryujinx.Ui.Widgets ;
2020-09-29 22:05:25 +02:00
using System ;
2021-02-22 21:48:45 +01:00
using System.Collections.Generic ;
2020-09-29 22:05:25 +02:00
using System.IO ;
2021-02-23 16:19:02 +01:00
using System.Linq ;
2020-09-29 22:05:25 +02:00
using System.Net ;
2021-11-28 21:24:17 +01:00
using System.Net.Http ;
2020-09-29 22:05:25 +02:00
using System.Net.NetworkInformation ;
using System.Runtime.InteropServices ;
2020-11-27 18:57:20 +01:00
using System.Text ;
2021-02-22 21:48:45 +01:00
using System.Threading ;
2020-09-29 22:05:25 +02:00
using System.Threading.Tasks ;
2021-01-08 09:14:13 +01:00
namespace Ryujinx.Modules
2020-09-29 22:05:25 +02:00
{
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" ) ;
2021-02-22 21:48:45 +01:00
private static readonly int ConnectionCount = 4 ;
2020-09-29 22:05:25 +02:00
private static string _jobId ;
private static string _buildVer ;
private static string _platformExt ;
private static string _buildUrl ;
2021-02-22 21:48:45 +01:00
private static long _buildSize ;
2020-09-29 22:05:25 +02:00
private const string AppveyorApiUrl = "https://ci.appveyor.com/api" ;
2021-02-23 16:19:02 +01:00
// On Windows, GtkSharp.Dependencies adds these extra dirs that must be cleaned during updates.
private static readonly string [ ] WindowsDependencyDirs = new string [ ] { "bin" , "etc" , "lib" , "share" } ;
2020-09-29 22:05:25 +02:00
public static async Task BeginParse ( MainWindow mainWindow , bool showVersionUpToDate )
{
if ( Running ) return ;
Running = true ;
mainWindow . UpdateMenuItem . Sensitive = false ;
2021-02-22 21:48:45 +01:00
int artifactIndex = - 1 ;
2020-09-29 22:05:25 +02:00
// Detect current platform
2021-12-05 00:02:30 +01:00
if ( OperatingSystem . IsMacOS ( ) )
2020-09-29 22:05:25 +02:00
{
2021-02-22 21:48:45 +01:00
_platformExt = "osx_x64.zip" ;
artifactIndex = 1 ;
2020-09-29 22:05:25 +02:00
}
2021-12-05 00:02:30 +01:00
else if ( OperatingSystem . IsWindows ( ) )
2020-09-29 22:05:25 +02:00
{
2021-02-22 21:48:45 +01:00
_platformExt = "win_x64.zip" ;
artifactIndex = 2 ;
2020-09-29 22:05:25 +02:00
}
2021-12-05 00:02:30 +01:00
else if ( OperatingSystem . IsLinux ( ) )
2020-09-29 22:05:25 +02:00
{
2021-02-22 21:48:45 +01:00
_platformExt = "linux_x64.tar.gz" ;
artifactIndex = 0 ;
}
if ( artifactIndex = = - 1 )
{
GtkDialog . CreateErrorDialog ( "Your platform is not supported!" ) ;
return ;
2020-09-29 22:05:25 +02:00
}
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
{
2021-11-28 21:24:17 +01:00
using ( HttpClient jsonClient = new HttpClient ( ) )
2020-09-29 22:05:25 +02:00
{
2021-02-22 21:48:45 +01:00
// Fetch latest build information
2021-11-28 21:24:17 +01:00
string fetchedJson = await jsonClient . GetStringAsync ( $"{AppveyorApiUrl}/projects/gdkchan/ryujinx/branch/master" ) ;
2020-09-29 22:05:25 +02:00
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}" ;
2020-09-29 22:52:41 +02:00
// If build not done, assume no new update are availaible.
if ( ( string ) buildToken [ "jobs" ] [ 0 ] [ "status" ] ! = "success" )
{
if ( showVersionUpToDate )
{
2021-02-28 11:33:36 +01:00
GtkDialog . CreateUpdaterInfoDialog ( "You are already using the latest version of Ryujinx!" , "" ) ;
2020-09-29 22:52:41 +02:00
}
return ;
}
2020-09-29 22:05:25 +02:00
}
}
catch ( Exception exception )
{
Logger . Error ? . Print ( LogClass . Application , exception . Message ) ;
2021-08-20 23:48:00 +02:00
GtkDialog . CreateErrorDialog ( "An error occurred when trying to get release information from AppVeyor. This can be caused if a new release is being compiled by AppVeyor. Try again in a few minutes." ) ;
2020-09-29 22:05:25 +02:00
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 )
{
2021-02-28 11:33:36 +01:00
GtkDialog . CreateUpdaterInfoDialog ( "You are already using the latest version of Ryujinx!" , "" ) ;
2020-09-29 22:05:25 +02:00
}
Running = false ;
mainWindow . UpdateMenuItem . Sensitive = true ;
return ;
}
2021-02-22 21:48:45 +01:00
// Fetch build size information to learn chunk sizes.
2021-11-28 21:24:17 +01:00
using ( HttpClient buildSizeClient = new HttpClient ( ) )
{
2021-02-22 21:48:45 +01:00
try
{
2021-11-28 21:24:17 +01:00
buildSizeClient . DefaultRequestHeaders . Add ( "Range" , "bytes=0-0" ) ;
HttpResponseMessage message = await buildSizeClient . GetAsync ( new Uri ( _buildUrl ) , HttpCompletionOption . ResponseHeadersRead ) ;
2021-02-22 21:48:45 +01:00
2021-11-28 21:24:17 +01:00
_buildSize = message . Content . Headers . ContentRange . Length . Value ;
2021-02-22 21:48:45 +01:00
}
catch ( Exception ex )
{
Logger . Warning ? . Print ( LogClass . Application , ex . Message ) ;
2021-08-20 23:48:00 +02:00
Logger . Warning ? . Print ( LogClass . Application , "Couldn't determine build size for update, using single-threaded updater" ) ;
2021-02-22 21:48:45 +01:00
_buildSize = - 1 ;
}
}
2020-09-29 22:05:25 +02:00
// Show a message asking the user if they want to update
UpdateDialog updateDialog = new UpdateDialog ( mainWindow , newVersion , _buildUrl ) ;
updateDialog . Show ( ) ;
}
2021-02-22 21:48:45 +01:00
public static void UpdateRyujinx ( UpdateDialog updateDialog , string downloadUrl )
2020-09-29 22:05:25 +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" ) ;
// Download the update .zip
updateDialog . MainText . Text = "Downloading Update..." ;
updateDialog . ProgressBar . Value = 0 ;
updateDialog . ProgressBar . MaxValue = 100 ;
2021-02-22 21:48:45 +01:00
if ( _buildSize > = 0 )
{
DoUpdateWithMultipleThreads ( updateDialog , downloadUrl , updateFile ) ;
}
else
{
DoUpdateWithSingleThread ( updateDialog , downloadUrl , updateFile ) ;
}
}
private static void DoUpdateWithMultipleThreads ( UpdateDialog updateDialog , string downloadUrl , string updateFile )
{
// Multi-Threaded Updater
long chunkSize = _buildSize / ConnectionCount ;
long remainderChunk = _buildSize % ConnectionCount ;
int completedRequests = 0 ;
int totalProgressPercentage = 0 ;
int [ ] progressPercentage = new int [ ConnectionCount ] ;
List < byte [ ] > list = new List < byte [ ] > ( ConnectionCount ) ;
List < WebClient > webClients = new List < WebClient > ( ConnectionCount ) ;
for ( int i = 0 ; i < ConnectionCount ; i + + )
{
list . Add ( new byte [ 0 ] ) ;
}
for ( int i = 0 ; i < ConnectionCount ; i + + )
{
2021-11-28 21:24:17 +01:00
#pragma warning disable SYSLIB0014
// TODO: WebClient is obsolete and need to be replaced with a more complex logic using HttpClient.
2021-02-22 21:48:45 +01:00
using ( WebClient client = new WebClient ( ) )
2021-11-28 21:24:17 +01:00
#pragma warning restore SYSLIB0014
2021-02-22 21:48:45 +01:00
{
webClients . Add ( client ) ;
if ( i = = ConnectionCount - 1 )
{
client . Headers . Add ( "Range" , $"bytes={chunkSize * i}-{(chunkSize * (i + 1) - 1) + remainderChunk}" ) ;
}
else
{
client . Headers . Add ( "Range" , $"bytes={chunkSize * i}-{chunkSize * (i + 1) - 1}" ) ;
}
client . DownloadProgressChanged + = ( _ , args ) = >
{
int index = ( int ) args . UserState ;
Interlocked . Add ( ref totalProgressPercentage , - 1 * progressPercentage [ index ] ) ;
Interlocked . Exchange ( ref progressPercentage [ index ] , args . ProgressPercentage ) ;
Interlocked . Add ( ref totalProgressPercentage , args . ProgressPercentage ) ;
updateDialog . ProgressBar . Value = totalProgressPercentage / ConnectionCount ;
} ;
client . DownloadDataCompleted + = ( _ , args ) = >
{
int index = ( int ) args . UserState ;
if ( args . Cancelled )
{
webClients [ index ] . Dispose ( ) ;
return ;
}
list [ index ] = args . Result ;
Interlocked . Increment ( ref completedRequests ) ;
if ( Interlocked . Equals ( completedRequests , ConnectionCount ) )
{
byte [ ] mergedFileBytes = new byte [ _buildSize ] ;
for ( int connectionIndex = 0 , destinationOffset = 0 ; connectionIndex < ConnectionCount ; connectionIndex + + )
{
Array . Copy ( list [ connectionIndex ] , 0 , mergedFileBytes , destinationOffset , list [ connectionIndex ] . Length ) ;
destinationOffset + = list [ connectionIndex ] . Length ;
}
File . WriteAllBytes ( updateFile , mergedFileBytes ) ;
try
{
InstallUpdate ( updateDialog , 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." ) ;
DoUpdateWithSingleThread ( updateDialog , downloadUrl , updateFile ) ;
return ;
}
}
} ;
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." ) ;
for ( int j = 0 ; j < webClients . Count ; j + + )
{
webClients [ j ] . CancelAsync ( ) ;
}
DoUpdateWithSingleThread ( updateDialog , downloadUrl , updateFile ) ;
return ;
}
}
}
}
2021-11-28 21:24:17 +01:00
private static void DoUpdateWithSingleThreadWorker ( UpdateDialog updateDialog , string downloadUrl , string updateFile )
2021-02-22 21:48:45 +01:00
{
2021-11-28 21:24:17 +01:00
using ( HttpClient client = new HttpClient ( ) )
2020-09-29 22:05:25 +02:00
{
2021-11-28 21:24:17 +01:00
// We do not want to timeout while downloading
client . Timeout = TimeSpan . FromDays ( 1 ) ;
2020-09-29 22:05:25 +02:00
2021-11-28 21:24:17 +01:00
using ( HttpResponseMessage response = client . GetAsync ( downloadUrl , HttpCompletionOption . ResponseHeadersRead ) . Result )
using ( Stream remoteFileStream = response . Content . ReadAsStreamAsync ( ) . Result )
2021-02-22 21:48:45 +01:00
{
2021-11-28 21:24:17 +01:00
using ( Stream updateFileStream = File . Open ( updateFile , FileMode . Create ) )
{
long totalBytes = response . Content . Headers . ContentLength . Value ;
long byteWritten = 0 ;
byte [ ] buffer = new byte [ 32 * 1024 ] ;
while ( true )
{
int readSize = remoteFileStream . Read ( buffer ) ;
if ( readSize = = 0 )
{
break ;
}
2020-09-29 22:05:25 +02:00
2021-11-28 21:24:17 +01:00
byteWritten + = readSize ;
updateDialog . ProgressBar . Value = ( ( double ) byteWritten / totalBytes ) * 100 ;
updateFileStream . Write ( buffer , 0 , readSize ) ;
}
}
}
InstallUpdate ( updateDialog , updateFile ) ;
2021-02-22 21:48:45 +01:00
}
}
2021-11-28 21:24:17 +01:00
private static void DoUpdateWithSingleThread ( UpdateDialog updateDialog , string downloadUrl , string updateFile )
{
Thread worker = new Thread ( ( ) = > DoUpdateWithSingleThreadWorker ( updateDialog , downloadUrl , updateFile ) ) ;
worker . Name = "Updater.SingleThreadWorker" ;
worker . Start ( ) ;
}
2021-12-08 22:24:26 +01:00
[DllImport("libc", SetLastError = true)]
private static extern int chmod ( string path , uint mode ) ;
2021-02-25 00:35:48 +01:00
private static void SetUnixPermissions ( )
{
2021-11-28 21:24:17 +01:00
string ryuBin = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "Ryujinx" ) ;
2021-02-25 00:35:48 +01:00
2021-11-28 21:24:17 +01:00
if ( ! OperatingSystem . IsWindows ( ) )
2021-02-25 00:35:48 +01:00
{
2021-12-08 22:24:26 +01:00
chmod ( ryuBin , 0777 ) ;
2021-02-25 00:35:48 +01:00
}
}
2021-02-22 21:48:45 +01:00
private static async void InstallUpdate ( UpdateDialog updateDialog , string updateFile )
{
2020-09-29 22:05:25 +02:00
// Extract Update
updateDialog . MainText . Text = "Extracting Update..." ;
updateDialog . ProgressBar . Value = 0 ;
2021-12-05 00:02:30 +01:00
if ( OperatingSystem . IsLinux ( ) )
2020-09-29 22:05:25 +02:00
{
using ( Stream inStream = File . OpenRead ( updateFile ) )
using ( Stream gzipStream = new GZipInputStream ( inStream ) )
2020-11-27 18:57:20 +01:00
using ( TarInputStream tarStream = new TarInputStream ( gzipStream , Encoding . ASCII ) )
2020-09-29 22:05:25 +02:00
{
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 ) ;
2021-02-23 16:19:02 +01:00
List < string > allFiles = EnumerateFilesToDelete ( ) . ToList ( ) ;
2020-09-29 22:05:25 +02:00
updateDialog . MainText . Text = "Renaming Old Files..." ;
updateDialog . ProgressBar . Value = 0 ;
2021-02-23 16:19:02 +01:00
updateDialog . ProgressBar . MaxValue = allFiles . Count ;
2020-09-29 22:05:25 +02:00
// Replace old files
await Task . Run ( ( ) = >
{
foreach ( string file in allFiles )
{
2021-02-23 16:19:02 +01:00
try
2020-09-29 22:05:25 +02:00
{
2021-02-23 16:19:02 +01:00
File . Move ( file , file + ".ryuold" ) ;
2020-09-29 22:05:25 +02:00
2021-02-23 16:19:02 +01:00
Application . Invoke ( delegate
2020-09-29 22:05:25 +02:00
{
2021-02-23 16:19:02 +01:00
updateDialog . ProgressBar . Value + + ;
} ) ;
}
catch
{
2021-02-28 11:33:36 +01:00
Logger . Warning ? . Print ( LogClass . Application , "Updater was unable to rename file: " + file ) ;
2020-09-29 22:05:25 +02:00
}
}
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 ) ;
2021-02-25 00:35:48 +01:00
SetUnixPermissions ( ) ;
2020-09-29 22:05:25 +02:00
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 )
{
2021-02-28 11:56:15 +01:00
#if ! DISABLE_UPDATER
2020-09-29 22:05:25 +02:00
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 )
{
2021-02-28 11:56:15 +01:00
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." ) ;
2020-09-29 22:05:25 +02:00
}
return false ;
}
return true ;
2021-02-28 11:56:15 +01:00
#else
if ( showWarnings )
{
GtkDialog . CreateWarningDialog ( "Updater Disabled!" , "Please download Ryujinx at https://ryujinx.org/ if you are looking for a supported version." ) ;
}
return false ;
#endif
2020-09-29 22:05:25 +02:00
}
2021-02-23 16:19:02 +01:00
// NOTE: This method should always reflect the latest build layout.
private static IEnumerable < string > EnumerateFilesToDelete ( )
{
var files = Directory . EnumerateFiles ( HomeDir ) ; // All files directly in base dir.
2021-12-05 00:02:30 +01:00
if ( OperatingSystem . IsWindows ( ) )
2021-02-23 16:19:02 +01:00
{
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 ;
}
2020-09-29 22:05:25 +02:00
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 ( )
{
2021-02-23 16:19:02 +01:00
foreach ( string file in EnumerateFilesToDelete ( ) )
2020-09-29 22:05:25 +02:00
{
if ( Path . GetExtension ( file ) . EndsWith ( ".ryuold" ) )
{
File . Delete ( file ) ;
}
}
}
}
}