improved the client update service to avoid blowing out client unnecessarily, and avoided conflicts with 2009Scape installs
This commit is contained in:
		
							parent
							
								
									fa3d5ebee0
								
							
						
					
					
						commit
						dd7e229db3
					
				@ -16,8 +16,8 @@ namespace Saradomin.Infrastructure.Services
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        private float CurrentDownloadProgress { get; set; }
 | 
					        private float CurrentDownloadProgress { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public string ClientDownloadURL => "https://gitlab.com/2009scape/rt4-client/-/jobs/artifacts/master/raw/client/build/libs/rt4-client.jar?job=build";
 | 
					        public string ClientDownloadURL => "https://assets.emoscape.org/rt4-client.jar";
 | 
				
			||||||
        public string ClientHashURL => "https://gitlab.com/2009scape/rt4-client/-/jobs/artifacts/master/raw/client/build/libs/rt4-client.jar.sha256?job=build";
 | 
					        public string ClientHashURL => "https://assets.emoscape.org/rt4-client.jar.sha256";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public string PreferredTargetFilePath =>
 | 
					        public string PreferredTargetFilePath =>
 | 
				
			||||||
            CrossPlatform.Get2009scapeExecutable();
 | 
					            CrossPlatform.Get2009scapeExecutable();
 | 
				
			||||||
@ -38,45 +38,45 @@ namespace Saradomin.Infrastructure.Services
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public async Task FetchRemoteClientExecutableAsync(CancellationToken cancellationToken,
 | 
					        public async Task FetchRemoteClientExecutableAsync(CancellationToken cancellationToken, string targetPath = null)
 | 
				
			||||||
            string targetPath = null)
 | 
					 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            CurrentDownloadProgress = 0;
 | 
					            CurrentDownloadProgress = 0;
 | 
				
			||||||
 | 
					 | 
				
			||||||
            targetPath ??= PreferredTargetFilePath;
 | 
					            targetPath ??= PreferredTargetFilePath;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					            // Define a temporary file path to download to
 | 
				
			||||||
 | 
					            string tempFilePath = targetPath + ".tmp";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (File.Exists(targetPath))
 | 
					            // Download the client to the temporary file regardless of whether it already exists.
 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                File.Delete(targetPath);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            using (var httpClient = new HttpClient())
 | 
					            using (var httpClient = new HttpClient())
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var response = await httpClient.GetAsync(ClientDownloadURL, cancellationToken);
 | 
					                var response = await httpClient.GetAsync(ClientDownloadURL, cancellationToken);
 | 
				
			||||||
                var contentLength = response.Content.Headers.ContentLength ?? 12 * 1024 * 1024 * 1024f;
 | 
					                var contentLength = response.Content.Headers.ContentLength ?? 12 * 1024 * 1024 * 1024f;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
 | 
					                using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
 | 
				
			||||||
                using var outFileStream = File.OpenWrite(targetPath);
 | 
					                using (var outFileStream = File.Create(tempFilePath))
 | 
				
			||||||
 | 
					 | 
				
			||||||
                var data = new byte[1024];
 | 
					 | 
				
			||||||
                var totalRead = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                while (responseStream.Position < contentLength)
 | 
					 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    var dataRead = await responseStream.ReadAsync(data, 0, data.Length, cancellationToken);
 | 
					                    var buffer = new byte[1024];
 | 
				
			||||||
 | 
					                    var totalRead = 0;
 | 
				
			||||||
                    if (dataRead <= 0)
 | 
					                    int bytesRead;
 | 
				
			||||||
                        throw new IOException("Unexpected 0-byte read in network stream.");
 | 
					                    while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
                    await outFileStream.WriteAsync(data[0..dataRead], cancellationToken);
 | 
					                        await outFileStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);
 | 
				
			||||||
                    totalRead += dataRead;
 | 
					                        totalRead += bytesRead;
 | 
				
			||||||
 | 
					                        CurrentDownloadProgress = totalRead / contentLength;
 | 
				
			||||||
                    CurrentDownloadProgress = totalRead / contentLength;
 | 
					                        DownloadProgressChanged?.Invoke(this, CurrentDownloadProgress);
 | 
				
			||||||
                    DownloadProgressChanged?.Invoke(this, CurrentDownloadProgress);
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Replace the existing file only after a successful download.
 | 
				
			||||||
 | 
					            if (File.Exists(targetPath))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                File.Delete(targetPath);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            File.Move(tempFilePath, targetPath);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public async Task<string> ComputeLocalClientHashAsync(string filePath = null)
 | 
					        public async Task<string> ComputeLocalClientHashAsync(string filePath = null)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            filePath ??= PreferredTargetFilePath;
 | 
					            filePath ??= PreferredTargetFilePath;
 | 
				
			||||||
 | 
				
			|||||||
@ -188,18 +188,18 @@ namespace Saradomin.Utilities
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                return Path.Combine(
 | 
					                return Path.Combine(
 | 
				
			||||||
                    LocateUnixUserHome(),
 | 
					                    LocateUnixUserHome(),
 | 
				
			||||||
                    "2009scape"
 | 
					                    "EmoScape"
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var userProfile = Path.Combine (
 | 
					                var userProfile = Path.Combine (
 | 
				
			||||||
                    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
 | 
					                    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
 | 
				
			||||||
                    "2009scape"
 | 
					                    "EmoScape"
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
                var appData = Path.Combine(
 | 
					                var appData = Path.Combine(
 | 
				
			||||||
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
 | 
					                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
 | 
				
			||||||
                    "2009scape"
 | 
					                    "EmoScape"
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
                return Directory.Exists(userProfile) ? userProfile : appData;
 | 
					                return Directory.Exists(userProfile) ? userProfile : appData;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -213,14 +213,14 @@ namespace Saradomin.Utilities
 | 
				
			|||||||
                return Path.Combine(
 | 
					                return Path.Combine(
 | 
				
			||||||
                    // Get the XDG_DATA_HOME environment variable, or if it doesn't exist, use the default ~/.local/share
 | 
					                    // Get the XDG_DATA_HOME environment variable, or if it doesn't exist, use the default ~/.local/share
 | 
				
			||||||
                    LocateUnixUserHome(),
 | 
					                    LocateUnixUserHome(),
 | 
				
			||||||
                    "2009scape",
 | 
					                    "EmoScape",
 | 
				
			||||||
                    "saradomin"
 | 
					                    "saradomin"
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return Path.Combine(
 | 
					            return Path.Combine(
 | 
				
			||||||
                Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
 | 
					                Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
 | 
				
			||||||
                "2009scape",
 | 
					                "EmoScape",
 | 
				
			||||||
                "saradomin"
 | 
					                "saradomin"
 | 
				
			||||||
            );
 | 
					            );
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user