From 83eacc3ed2b1c97becc8ee5dacafa3ec3fa43d01 Mon Sep 17 00:00:00 2001 From: Akbar Yahya <11765082+akbaryahya@users.noreply.github.com> Date: Thu, 22 Sep 2022 07:07:12 +0800 Subject: [PATCH] * Better downloader system * Better check update Akebi --- API.cs | 52 +++++++++++++++++++++++++-- Download.Designer.cs | 4 +-- Download.cs | 79 ++++++++++++++++++++++++++++++----------- Main.cs | 40 ++++++++++++++++++--- Tool.cs | 24 ++++++++----- YuukiPS Launcher.csproj | 7 ++-- 6 files changed, 166 insertions(+), 40 deletions(-) diff --git a/API.cs b/API.cs index 4f97f7a..5bd9b56 100644 --- a/API.cs +++ b/API.cs @@ -11,7 +11,8 @@ public class API public static string API_DL_CF = "https://file.yuuki.me/"; public static string API_DL_OW = "https://drive.yuuki.me/"; public static string API_DL_WB = "https://ps.yuuki.me/api/"; - public static string API_GITHUB = "https://api.github.com/repos/akbaryahya/YuukiPS-Launcher/"; + public static string API_GITHUB_YuukiPS = "https://api.github.com/repos/akbaryahya/YuukiPS-Launcher/"; + public static string API_GITHUB_Akebi = "https://api.github.com/repos/Akebi-Group/Akebi-GC/"; public static GS GS_DL(string dl = "os") { @@ -150,7 +151,7 @@ public static GS GS_DL(string dl = "os") public static Update? GetUpdate() { - var client = new RestClient(API_GITHUB); + var client = new RestClient(API_GITHUB_YuukiPS); var request = new RestRequest("releases"); var response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK) @@ -178,5 +179,52 @@ public static GS GS_DL(string dl = "os") } return null; } + + public static string? GetAkebi(int ch = 1) + { + var client = new RestClient(API_GITHUB_Akebi); + var request = new RestRequest("actions/artifacts"); + var response = client.Execute(request); + + var whos = "master"; + if (ch == 2) + { + whos = "chinese"; + } + + if (response.StatusCode == HttpStatusCode.OK) + { + if (response.Content != null) + { + try + { + var tes = JsonConvert.DeserializeObject(response.Content); + if (tes != null) + { + foreach (var file in tes.artifacts) + { + if (file.workflow_run != null) + { + if (file.workflow_run.head_branch == whos) + { + return file.workflow_run.head_sha + "|https://nightly.link/Akebi-Group/Akebi-GC/actions/runs/" + file.workflow_run.id + "/" + file.name + ".zip"; + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine("Error GetAkebi: ", ex); + } + + } + } + else + { + Console.WriteLine("Error GetUpdate2: " + response.StatusCode); + } + return ""; + } } } diff --git a/Download.Designer.cs b/Download.Designer.cs index edc21b0..a835dcd 100644 --- a/Download.Designer.cs +++ b/Download.Designer.cs @@ -65,7 +65,7 @@ private void InitializeComponent() // this.GetNumDownload.AutoSize = true; this.GetNumDownload.Font = new System.Drawing.Font("Segoe UI", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.GetNumDownload.Location = new System.Drawing.Point(174, 60); + this.GetNumDownload.Location = new System.Drawing.Point(12, 87); this.GetNumDownload.Name = "GetNumDownload"; this.GetNumDownload.Size = new System.Drawing.Size(16, 20); this.GetNumDownload.TabIndex = 3; @@ -95,7 +95,7 @@ private void InitializeComponent() // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(556, 92); + this.ClientSize = new System.Drawing.Size(556, 111); this.Controls.Add(this.btCancel); this.Controls.Add(this.btDownload); this.Controls.Add(this.GetNumDownload); diff --git a/Download.cs b/Download.cs index 43d1254..0bd53db 100644 --- a/Download.cs +++ b/Download.cs @@ -1,6 +1,5 @@ -using System.ComponentModel; -using System.Globalization; -using System.Net; +using Downloader; +using System.ComponentModel; namespace YuukiPS_Launcher { @@ -8,7 +7,7 @@ public partial class Download : Form { private string set_download; private string set_folder; - private WebClient dl; + private DownloadService dl; public Download(string url_download, string folder_download) { @@ -39,13 +38,20 @@ private void btDownload_Click(object sender, EventArgs e) if (dl == null) { - dl = new WebClient(); - dl.DownloadFileCompleted += DLDone; - dl.DownloadProgressChanged += DLProgress; + var downloadOpt = new DownloadConfiguration() + { + ChunkCount = 8, // file parts to download, default value is 1 + OnTheFlyDownload = true, // caching in-memory or not? default values is true + ParallelDownload = true // download parts of file as parallel or not. Default value is false + }; + dl = new DownloadService(downloadOpt); + dl.DownloadStarted += Dl_DownloadStarted; + dl.DownloadFileCompleted += Dl_DownloadFileCompleted; + dl.DownloadProgressChanged += Dl_DownloadProgressChanged; + dl.ChunkDownloadProgressChanged += Dl_ChunkDownloadProgressChanged; try { - Console.WriteLine("Start Download: " + set_download); - dl.DownloadFileAsync(new Uri(set_download), set_folder); + dl.DownloadFileTaskAsync(set_download, set_folder); } catch (Exception ek) { @@ -59,22 +65,55 @@ private void btDownload_Click(object sender, EventArgs e) } - private void DLProgress(object sender, DownloadProgressChangedEventArgs e) + private void Dl_DownloadFileCompleted(object? sender, AsyncCompletedEventArgs e) { - var bytesIn = double.Parse(e.BytesReceived.ToString()); - var totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); - var percentage = bytesIn / totalBytes * 100; - DLBar.Value = int.Parse(Math.Truncate(percentage).ToString(CultureInfo.InvariantCulture)); + btDownload.Invoke((Action)delegate + { + btDownload.Enabled = true; + }); + GetNumDownload.Invoke((Action)delegate + { + GetNumDownload.Text = "Done"; + }); + DialogResult = DialogResult.OK; + } - GetNumDownload.Text = $@"Update {Tool.SizeSuffix(e.BytesReceived)} of {Tool.SizeSuffix(e.TotalBytesToReceive)}"; + private void Dl_ChunkDownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e) + { + //Console.WriteLine($@"Update {e.ReceivedBytes} of {e.TotalBytesToReceive}"); } - private void DLDone(object? sender, AsyncCompletedEventArgs e) + private void Dl_DownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e) { - btDownload.Enabled = true; - GetNumDownload.Text = "Done"; - // TODO: check vaild file - DialogResult = DialogResult.OK; + double nonZeroSpeed = e.BytesPerSecondSpeed + 0.0001; + int estimateTime = (int)((e.TotalBytesToReceive - e.ReceivedBytesSize) / nonZeroSpeed); + bool isMinutes = estimateTime >= 60; + string timeLeftUnit = "seconds"; + + if (isMinutes) + { + timeLeftUnit = "minutes"; + estimateTime /= 60; + } + + if (estimateTime < 0) + { + estimateTime = 0; + timeLeftUnit = "unknown"; + } + + string bytesReceived = Tool.CalcMemoryMensurableUnit(e.ReceivedBytesSize); + string totalBytesToReceive = Tool.CalcMemoryMensurableUnit(e.TotalBytesToReceive); + + GetNumDownload.Invoke((Action)delegate + { + GetNumDownload.Text = $"{bytesReceived} of {totalBytesToReceive} | {estimateTime} {timeLeftUnit} left | Speed: {Tool.CalcMemoryMensurableUnit(e.BytesPerSecondSpeed)}/s"; + }); + } + + private void Dl_DownloadStarted(object? sender, DownloadStartedEventArgs e) + { + Console.WriteLine("Start Download: " + e.FileName); } private void btCancel_Click(object sender, EventArgs e) diff --git a/Main.cs b/Main.cs index 0999ef4..2643486 100644 --- a/Main.cs +++ b/Main.cs @@ -1131,13 +1131,40 @@ private void btStart_Click(object sender, EventArgs e) Directory.CreateDirectory(set_AkebiGC); string get_AkebiGC = Path.Combine(set_AkebiGC, "injector.exe"); string get_AkebiGC_zip = Path.Combine(set_AkebiGC, "update.zip"); + string get_AkebiGC_md5 = Path.Combine(set_AkebiGC, "md5.txt"); - if (!File.Exists(get_AkebiGC)) + var Update_AkebiGC = false; + + var cekAkebi = API.GetAkebi(GameChannel); + if (string.IsNullOrEmpty(cekAkebi)) + { + MessageBox.Show("Can't check latest Akebi"); + return; + } + string[] SplitAkebiGC = cekAkebi.Split("|"); + + // Check file update, jika tidak ada + if (!File.Exists(get_AkebiGC_md5)) { - var DL2 = new Download("https://github.com/Akebi-Group/Akebi-GC/releases/download/v0.95/akebi-gc-v0.95-g3.0-binaries-global.zip", get_AkebiGC_zip); + Console.WriteLine("Md5 no found, update!!!"); + Update_AkebiGC = true; + } + else + { + string readText = File.ReadAllText(get_AkebiGC_md5); + if (!readText.Contains(SplitAkebiGC[0])) + { + Console.WriteLine("Found a new version, time to download"); + Update_AkebiGC = true; + } + } + + if (Update_AkebiGC)//!File.Exists(get_AkebiGC) + { + var DL2 = new Download(SplitAkebiGC[1], get_AkebiGC_zip); if (DL2.ShowDialog() != DialogResult.OK) { - MessageBox.Show("No Akebi"); + MessageBox.Show("Download Akebi failed"); return; } else @@ -1173,16 +1200,19 @@ private void btStart_Click(object sender, EventArgs e) ProcessStartInfo pInfo = new ProcessStartInfo(); pInfo.FileName = file_update_AkebiGC; Process p = Process.Start(pInfo); - //p.WaitForInputIdle(); + p.WaitForExit(); + // Update MD5 + File.WriteAllText(get_AkebiGC_md5, SplitAkebiGC[0]); + } catch (Exception ex) { MessageBox.Show(ex.Message); } - } + } cst_gamefile = get_AkebiGC; //WatchFile = "injector"; diff --git a/Tool.cs b/Tool.cs index 8fc16db..709090f 100644 --- a/Tool.cs +++ b/Tool.cs @@ -4,16 +4,24 @@ namespace YuukiPS_Launcher { - public class Tool + public static class Tool { - private static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; - public static string SizeSuffix(long value) + public static string CalcMemoryMensurableUnit(double bytes) { - if (value < 0) { return "-" + SizeSuffix(-value); } - if (value == 0) { return "0.0 bytes"; } - var mag = (int)Math.Log(value, 1024); - var adjustedSize = (decimal)value / (1L << (mag * 10)); - return $"{adjustedSize:n1} {SizeSuffixes[mag]}"; + double kb = bytes / 1024; // · 1024 Bytes = 1 Kilobyte + double mb = kb / 1024; // · 1024 Kilobytes = 1 Megabyte + double gb = mb / 1024; // · 1024 Megabytes = 1 Gigabyte + double tb = gb / 1024; // · 1024 Gigabytes = 1 Terabyte + + string result = + tb > 1 ? $"{tb:0.##}TB" : + gb > 1 ? $"{gb:0.##}GB" : + mb > 1 ? $"{mb:0.##}MB" : + kb > 1 ? $"{kb:0.##}KB" : + $"{bytes:0.##}B"; + + result = result.Replace("/", "."); + return result; } public const string UNIX_PID_REGX = @"\w+\s+(\d+).*"; diff --git a/YuukiPS Launcher.csproj b/YuukiPS Launcher.csproj index 5d59881..e86d0cd 100644 --- a/YuukiPS Launcher.csproj +++ b/YuukiPS Launcher.csproj @@ -12,8 +12,8 @@ Properties\app.manifest - 2022.9.21.1954 - 2022.9.21.1954 + 2022.9.21.2303 + 2022.9.21.2303 true YuukiPS @@ -45,9 +45,10 @@ + - +