Skip to content

Commit

Permalink
* Better downloader system
Browse files Browse the repository at this point in the history
* Better check update Akebi
  • Loading branch information
akbaryahya committed Sep 21, 2022
1 parent 0d1b88d commit 83eacc3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 40 deletions.
52 changes: 50 additions & 2 deletions API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<Nightly>(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 "";
}
}
}
4 changes: 2 additions & 2 deletions Download.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 59 additions & 20 deletions Download.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.ComponentModel;
using System.Globalization;
using System.Net;
using Downloader;
using System.ComponentModel;

namespace YuukiPS_Launcher
{
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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
40 changes: 35 additions & 5 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down
24 changes: 16 additions & 8 deletions Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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+).*";
Expand Down
7 changes: 4 additions & 3 deletions YuukiPS Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<ApplicationManifest>Properties\app.manifest</ApplicationManifest>

<AssemblyVersion>2022.9.21.1954</AssemblyVersion>
<FileVersion>2022.9.21.1954</FileVersion>
<AssemblyVersion>2022.9.21.2303</AssemblyVersion>
<FileVersion>2022.9.21.2303</FileVersion>

<EnableWindowsTargeting>true</EnableWindowsTargeting>
<AssemblyName>YuukiPS</AssemblyName>
Expand Down Expand Up @@ -45,9 +45,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Downloader" Version="2.4.1" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="108.0.1" />
<PackageReference Include="RestSharp" Version="108.0.2" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="Titanium.Web.Proxy" Version="3.1.1450" />
</ItemGroup>
Expand Down

0 comments on commit 83eacc3

Please sign in to comment.