Skip to content

Commit

Permalink
Implement update SBLauncherUpdater application
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Jan 11, 2024
1 parent 31f36b8 commit 9140aec
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private IServiceCollection ConfigureServices() {
}

private static void ConfigureClient(HttpClient httpClient) {
httpClient.BaseAddress = new Uri("https://api.github.com/repos/Belarus-Mod/Mod-Data/");
httpClient.BaseAddress = UriStorage.BelarusUri;
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using ImeSense.Launchers.Belarus.Core.Helpers;
using ImeSense.Launchers.Belarus.Core.Manager;
using ImeSense.Launchers.Belarus.Core.Storage;
using ImeSense.Launchers.Belarus.Core.Services;

namespace ImeSense.Launchers.Belarus.Avalonia.ViewModels;

Expand Down Expand Up @@ -55,9 +56,11 @@ public async Task InitializeAsync() {
try {
var isLauncherReleaseCurrent = await InitializerManager.IsLauncherReleaseCurrentAsync();
if (!isLauncherReleaseCurrent) {
var path = Path.Combine(FileLocations.BaseDirectory,
var pathLauncherUpdater = Path.Combine(FileLocations.BaseDirectory,
FileNamesStorage.SBLauncherUpdater);
var updater = Launcher.Launch(path);
await UpdaterService.UpdaterAsync(UriStorage.LauncherUri, pathLauncherUpdater);

var updater = Launcher.Launch(pathLauncherUpdater);
updater?.Start();

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task InitializeAsync() {

public static async Task<bool> IsLauncherReleaseCurrentAsync() {
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://api.github.com/repos/imesense/belarus-launcher/");
httpClient.BaseAddress = UriStorage.LauncherUri;
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
Expand Down
52 changes: 52 additions & 0 deletions src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public static class FileNamesStorage {
public const string HashResources = "HashGameResources.json";
public const string Log = "sblauncher_report.log";
public const string WebResources = "WebResources.json";
public const string SBLauncher = "SBLauncher.exe";
public const string SBLauncherUpdater = "SBLauncherUpdater.exe";
}
6 changes: 6 additions & 0 deletions src/ImeSense.Launchers.Belarus.Core/Storage/UriStorage.cs
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/");
}
10 changes: 4 additions & 6 deletions src/ImeSense.Launchers.Belarus.CryptoHasher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Encodings.Web;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

Expand All @@ -7,6 +7,9 @@
using ImeSense.Launchers.Belarus.Core.Storage;

using static ImeSense.Launchers.Belarus.Core.Storage.FileLocations;
IEnumerable<string> GetDirectories() {
return new[] { BinariesDirectory, ResourcesDirectory, PatchesDirectory };
}

var hashing = new Md5HashProvider(null);
var folders = GetDirectories();
Expand Down Expand Up @@ -38,8 +41,3 @@
using var reader = new StreamReader(fs);

Console.WriteLine(File.ReadAllText(FileNamesStorage.HashResources));
return;

IEnumerable<string> GetDirectories() {
return new[] { BinariesDirectory, ResourcesDirectory, PatchesDirectory };
}
52 changes: 4 additions & 48 deletions src/ImeSense.Launchers.Belarus.Updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,15 @@
Console.Title = "Belarus Launcher Updater";

try {
var fullAppName = "SBLauncher.exe";
var appName = Path.GetFileNameWithoutExtension(fullAppName);

foreach (var process in Process.GetProcessesByName(appName)) {
foreach (var process in Process.GetProcessesByName("SBLauncher")) {
process.Kill();
}

Console.WriteLine($"Start update");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://api.github.com/repos/imesense/belarus-launcher/");
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);

var filePath = Path.Combine(FileLocations.BaseDirectory, fullAppName);

if (File.Exists(filePath)) {
File.Delete(filePath);
}
File.Move(fileDownloadPath, filePath);

if (Directory.Exists(pathDownloadFolder)) {
Directory.Delete(pathDownloadFolder);
}

var fileSavePath = Path.Combine(FileLocations.BaseDirectory, FileNamesStorage.SBLauncher);
await UpdaterService.UpdaterAsync(UriStorage.LauncherUri, fileSavePath);
Console.WriteLine($"Finish!");

Launcher.Launch(filePath)?.Start();
Launcher.Launch(fileSavePath)?.Start();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Expand Down

0 comments on commit 9140aec

Please sign in to comment.