-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b97ff7c
commit 8946a01
Showing
4 changed files
with
149 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,81 @@ | ||
using System.IO; | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Security.Cryptography; | ||
using System.Threading.Tasks; | ||
using JiayiLauncher.Settings; | ||
using JiayiLauncher.Utils; | ||
|
||
namespace JiayiLauncher.Features.Versions; | ||
|
||
public static class VersionManager | ||
{ | ||
public static event EventHandler<EventArgs>? DownloadFinished; | ||
|
||
public static bool VersionInstalled(string ver) | ||
{ | ||
Directory.CreateDirectory(JiayiSettings.Instance!.VersionsPath); | ||
var folders = Directory.GetDirectories(JiayiSettings.Instance!.VersionsPath); | ||
return folders.Any(x => x.Contains(ver)); | ||
} | ||
|
||
// haven't finished this code yet pls no touch | ||
// public static async Task DownloadVersion(MinecraftVersion version) | ||
// { | ||
// var updateId = version.Archs.x64!.UpdateIds[0]; | ||
// var url = await RequestFactory.GetDownloadUrl(updateId); | ||
// var fileName = version.Archs.x64.FileName; | ||
// var filePath = Path.Combine(JiayiSettings.Instance!.VersionsPath, fileName); | ||
// | ||
// var hash = MD5.HashData(await File.ReadAllBytesAsync(filePath)); | ||
// var hashString = string.Join("", hash.Select(x => x.ToString("x2"))); | ||
// if (hashString == version.Archs.x64.Hashes.MD5) return; | ||
// | ||
// using var client = new HttpClient(); | ||
// using | ||
// } | ||
public static async Task DownloadVersion(MinecraftVersion version, IProgress<ProgressEventArgs> progress) | ||
{ | ||
var updateId = version.Archs.x64!.UpdateIds[0]; | ||
var url = await RequestFactory.GetDownloadUrl(updateId); | ||
var fileName = version.Archs.x64.FileName; | ||
var filePath = Path.Combine(JiayiSettings.Instance!.VersionsPath, fileName); | ||
|
||
if (File.Exists(filePath)) File.Delete(filePath); | ||
|
||
using var client = new HttpClient(); | ||
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead); | ||
|
||
if (!response.IsSuccessStatusCode) return; | ||
|
||
var contentLength = response.Content.Headers.ContentLength; | ||
var totalRead = 0L; | ||
var buffer = new byte[1048576]; // 1 MB buffer | ||
|
||
await using var stream = await response.Content.ReadAsStreamAsync(); | ||
await using var fileStream = new FileStream(filePath, FileMode.Create); | ||
|
||
progress.Report(new ProgressEventArgs(0)); | ||
|
||
while (true) | ||
{ | ||
var read = await stream.ReadAsync(buffer); | ||
if (read == 0) break; | ||
|
||
await fileStream.WriteAsync(buffer.AsMemory(0, read)); | ||
totalRead += read; | ||
progress.Report(new ProgressEventArgs((int)(totalRead * 100 / contentLength)!)); | ||
} | ||
|
||
progress.Report(new ProgressEventArgs(100)); | ||
|
||
fileStream.Close(); | ||
stream.Close(); | ||
|
||
var folder = Path.Combine(JiayiSettings.Instance.VersionsPath, version.Version); | ||
Directory.CreateDirectory(folder); | ||
ZipFile.ExtractToDirectory(filePath, folder); | ||
File.Delete(filePath); | ||
|
||
DownloadFinished?.Invoke(null, EventArgs.Empty); | ||
} | ||
|
||
public static async Task RemoveVersion(string ver) | ||
{ | ||
await Task.Run(() => | ||
{ | ||
var folders = Directory.GetDirectories(JiayiSettings.Instance!.VersionsPath); | ||
var folder = folders.FirstOrDefault(x => x.Contains(ver)); | ||
if (folder == null) return; | ||
Directory.Delete(folder, true); | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace JiayiLauncher.Utils; | ||
|
||
public class ProgressEventArgs : EventArgs | ||
{ | ||
public int Percentage { get; set; } | ||
|
||
public ProgressEventArgs(int percentage) | ||
{ | ||
Percentage = percentage; | ||
} | ||
} |