Skip to content

Commit

Permalink
Merge pull request #16 from ElaXan/master
Browse files Browse the repository at this point in the history
UI Overhaul and Code Optimization
  • Loading branch information
akbaryahya authored Jul 30, 2024
2 parents 6a90469 + a1a6c11 commit e27e0e9
Show file tree
Hide file tree
Showing 26 changed files with 1,389 additions and 1,565 deletions.
53 changes: 34 additions & 19 deletions Download.Designer.cs

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

72 changes: 50 additions & 22 deletions Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,43 @@ namespace YuukiPS_Launcher
{
public partial class Download : Form
{
private string set_download = "";
private string set_folder = "";
private readonly string setDownload = "";
private readonly string setFolder = "";
private DownloadService? dl = null;

public Download(string url_download = "", string folder_download = "")
public Download(string urlDownload = "", string folderDownload = "")
{
set_download = url_download;
set_folder = folder_download;
setDownload = urlDownload;
setFolder = folderDownload;

InitializeComponent();
}

private void btDownload_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(set_download))
if (string.IsNullOrEmpty(setDownload))
{
MessageBox.Show("Download failed because no url was found");
Logger.Error("Download", "Download failed: No URL provided");
MessageBox.Show("Download failed because no URL was found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

if (Directory.Exists(set_folder))
if (!Directory.Exists(Path.GetDirectoryName(setFolder)))
{
MessageBox.Show("Can't save file because folder can't be found or can't be accessed");
Logger.Error("Download", $"Download failed: Folder not found or inaccessible - {setFolder}");
MessageBox.Show("Can't save file because the destination folder can't be found or accessed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

if (File.Exists(set_folder))
if (File.Exists(setFolder))
{
Logger.Info("Download", $"File old found {set_folder} remove for redownload?");
File.Delete(set_folder);
Logger.Info("Download", $"File old found {setFolder} remove for redownload?");
File.Delete(setFolder);
}

btDownload.Enabled = false;

GetNameDownload.Text = set_download;
GetNameDownload.Text = setDownload;

if (dl == null)
{
Expand All @@ -58,7 +60,7 @@ private void btDownload_Click(object sender, EventArgs e)
dl.ChunkDownloadProgressChanged += Dl_ChunkDownloadProgressChanged;
try
{
dl.DownloadFileTaskAsync(set_download, set_folder);
dl.DownloadFileTaskAsync(setDownload, setFolder);
}
catch (Exception ek)
{
Expand All @@ -74,11 +76,11 @@ private void btDownload_Click(object sender, EventArgs e)

private void Dl_DownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
{
btDownload.Invoke((Action)delegate
btDownload.Invoke(delegate
{
btDownload.Enabled = true;
});
GetNumDownload.Invoke((Action)delegate
GetNumDownload.Invoke(delegate
{
GetNumDownload.Text = "Done";
});
Expand All @@ -92,6 +94,7 @@ private void Dl_ChunkDownloadProgressChanged(object? sender, DownloadProgressCha

private void Dl_DownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e)
{
if (dl == null || dl.IsCancelled) return;
double nonZeroSpeed = e.BytesPerSecondSpeed + 0.0001;
int estimateTime = (int)((e.TotalBytesToReceive - e.ReceivedBytesSize) / nonZeroSpeed);
bool isMinutes = estimateTime >= 60;
Expand All @@ -110,7 +113,7 @@ private void Dl_DownloadProgressChanged(object? sender, DownloadProgressChangedE
}
else
{
DLBar.Invoke((Action)delegate
DLBar.Invoke(delegate
{
DLBar.Value = (int)e.ProgressPercentage;
});
Expand All @@ -119,23 +122,48 @@ private void Dl_DownloadProgressChanged(object? sender, DownloadProgressChangedE
string bytesReceived = Tool.CalcMemoryMensurableUnit(e.ReceivedBytesSize);
string totalBytesToReceive = Tool.CalcMemoryMensurableUnit(e.TotalBytesToReceive);

GetNumDownload.Invoke((Action)delegate
GetNumDownload.Invoke(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: " + set_download);
Logger.Info("Download", $"Starting download - URL: {setDownload}, Destination: {setFolder}");
}

private void btCancel_Click(object sender, EventArgs e)
private async void BTCancel_Click(object sender, EventArgs e)
{
if (dl != null)
{
dl.CancelAsync();
dl.Dispose();
// dl.CancelAsync();
// dl.Dispose();
try
{
btCancel.Enabled = false;
btDownload.Enabled = false;
GetNumDownload.Text = "Canceling...";

dl.CancelAsync();
await Task.Delay(1000); // give some time to cancel

dl.Dispose();
dl = null;

GetNumDownload.Text = "Canceled";
DLBar.Value = 0;
}
catch (Exception ex)
{
Logger.Error("Download", $"Error during canceling download: {ex.Message}");
MessageBox.Show($"Error during canceling download: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btCancel.Enabled = true;
btDownload.Enabled = true;
}
}
DialogResult = DialogResult.Cancel;
}
Expand Down
8 changes: 5 additions & 3 deletions Extra/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public void Ready(string appid = "1023479009335582830")
{
if (client == null)
{
client = new DiscordRpcClient(appid);
client.Logger = new ConsoleLogger() { Level = LogLevel.None };
client = new DiscordRpcClient(appid)
{
Logger = new ConsoleLogger() { Level = LogLevel.None }
};
client.RegisterUriScheme();

//Subscribe to events
Expand Down Expand Up @@ -71,7 +73,7 @@ public void UpdateStatus(string details, string state, string iconkey = "", int
{
Editor.Buttons = new Button[]
{
new Button() { Label = "Join", Url = API.WEB_LINK }
new() { Label = "Join", Url = API.WebLink }
};
}

Expand Down
90 changes: 50 additions & 40 deletions Game/Genshin/Patch/HexUtility.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,72 @@
public class HexUtility

namespace YuukiPS_Launcher.Game.Genshin.Patch
{
public static bool EqualsBytes(byte[] b1, params byte[] b2)
public class HexUtility
{
if (b1.Length != b2.Length)
return false;
for (int i = 0; i < b1.Length; i++)
public static bool EqualsBytes(byte[] b1, params byte[] b2)
{
if (b1[i] != b2[i])
if (b1.Length != b2.Length)
return false;
}
return true;
}

public static byte[] Replace(byte[] sourceByteArray, List<HexReplaceEntity> replaces)
{
byte[] newByteArray = new byte[sourceByteArray.Length];
Buffer.BlockCopy(sourceByteArray, 0, newByteArray, 0, sourceByteArray.Length);
int offset = 0;
foreach (HexReplaceEntity rep in replaces)
{
if (EqualsBytes(rep.oldValue, rep.newValue))
for (int i = 0; i < b1.Length; i++)
{
continue;
if (b1[i] != b2[i])
return false;
}
return true;
}

for (; offset < sourceByteArray.Length; offset++)
public static byte[] Replace(byte[] sourceByteArray, List<HexReplaceEntity> replaces)
{
byte[] newByteArray = new byte[sourceByteArray.Length];
Buffer.BlockCopy(sourceByteArray, 0, newByteArray, 0, sourceByteArray.Length);
int offset = 0;
foreach (HexReplaceEntity rep in replaces)
{
if (sourceByteArray[offset] == rep.oldValue[0])
if (EqualsBytes(rep.OldValue, rep.NewValue))
{
if (sourceByteArray.Length - offset < rep.oldValue.Length)
break;
continue;
}

bool find = true;
for (int i = 1; i < rep.oldValue.Length - 1; i++)
for (; offset < sourceByteArray.Length; offset++)
{
if (sourceByteArray[offset] == rep.OldValue[0])
{
if (sourceByteArray[offset + i] != rep.oldValue[i])
if (sourceByteArray.Length - offset < rep.OldValue.Length)
break;

bool find = true;
for (int i = 1; i < rep.OldValue.Length - 1; i++)
{
find = false;
if (sourceByteArray[offset + i] != rep.OldValue[i])
{
find = false;
break;
}
}
if (find)
{
Buffer.BlockCopy(rep.NewValue, 0, newByteArray, offset, rep.NewValue.Length);
offset += rep.NewValue.Length - 1;
break;
}
}
if (find)
{
Buffer.BlockCopy(rep.newValue, 0, newByteArray, offset, rep.newValue.Length);
offset += (rep.newValue.Length - 1);
break;
}
}
}
return newByteArray;
}
return newByteArray;
}
}

public class HexReplaceEntity
{
public byte[] oldValue { get; set; }
public class HexReplaceEntity
{
public byte[] OldValue { get; set; } = null!;
public byte[] NewValue { get; set; } = null!;

public byte[] newValue { get; set; }
public HexReplaceEntity() { }

}
public HexReplaceEntity(byte[] oldValue, byte[] newValue)
{
OldValue = oldValue;
NewValue = newValue;
}
}
}
Loading

0 comments on commit e27e0e9

Please sign in to comment.