-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement update
SBLauncherUpdater
application
- Loading branch information
Showing
8 changed files
with
74 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using ImeSense.Launchers.Belarus.Core.Manager; | ||
using ImeSense.Launchers.Belarus.Core.Storage; | ||
|
||
using System.Net.Http.Headers; | ||
|
||
namespace ImeSense.Launchers.Belarus.Core.Services; | ||
|
||
// TODO: Decompose and deploy IoC | ||
public class UpdaterService { | ||
public static async Task UpdaterAsync(Uri uri, string fileSavePath) { | ||
var appName = Path.GetFileNameWithoutExtension(fileSavePath); | ||
var fullAppName = Path.GetFileName(fileSavePath); | ||
|
||
using var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = uri; | ||
httpClient.DefaultRequestHeaders.Accept.Clear(); | ||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); | ||
httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter"); | ||
|
||
var gitHubService = new GitHubApiService(null, httpClient, null); | ||
var lastRelease = await gitHubService.GetLastReleaseAsync() | ||
?? throw new NullReferenceException("Latest release is null!"); | ||
var sblauncher = lastRelease.Assets?.FirstOrDefault(x => x.Name.Equals(fullAppName)) | ||
?? throw new NullReferenceException($"{appName} asset is null!"); | ||
|
||
if (sblauncher.BrowserDownloadUrl == null) { | ||
throw new NullReferenceException("Browser download url is null!"); | ||
} | ||
var progress = new Progress<int>(percentage => { | ||
Console.WriteLine($"{appName} is {percentage}% downloaded"); | ||
}); | ||
|
||
var pathDownloadFolder = Path.Combine(FileLocations.BaseDirectory, "temp"); | ||
var fileDownloadPath = Path.Combine(pathDownloadFolder, fullAppName); | ||
|
||
if (!Directory.Exists(pathDownloadFolder)) { | ||
Directory.CreateDirectory(pathDownloadFolder); | ||
} | ||
|
||
var download = new FileDownloadManager(null, httpClient); | ||
await download.DownloadAsync(sblauncher.BrowserDownloadUrl, fileDownloadPath, progress); | ||
|
||
if (File.Exists(fileSavePath)) { | ||
File.Delete(fileSavePath); | ||
} | ||
File.Move(fileDownloadPath, fileSavePath); | ||
|
||
if (Directory.Exists(pathDownloadFolder)) { | ||
Directory.Delete(pathDownloadFolder); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ImeSense.Launchers.Belarus.Core.Storage; | ||
|
||
public static class UriStorage { | ||
public static Uri LauncherUri => new("https://api.github.com/repos/imesense/belarus-launcher/"); | ||
public static Uri BelarusUri => new("https://api.github.com/repos/Belarus-Mod/Mod-Data/"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters