Skip to content

Commit

Permalink
hash progress display
Browse files Browse the repository at this point in the history
  • Loading branch information
zdimension committed Dec 25, 2019
1 parent bb1533f commit e1d7311
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 48 deletions.
27 changes: 5 additions & 22 deletions SharpBoot/Forms/GenIsoFrm.cs
Expand Up @@ -209,26 +209,13 @@ public override void DoWork()

private void CopyFile(string source, string dest)
{
var tok = new CancellationTokenSource();

void handler(object sender, EventArgs e)
{
tok.Cancel();
}

WorkCancelled += handler;
var started = DateTime.Now;
XCopy.Copy(source, dest, true,
true,
(o, pce) =>
{
var rem = TimeSpan.FromSeconds((DateTime.Now - started).TotalSeconds / pce.ProgressPercentage *
(100 - pce.ProgressPercentage));
ChangeProgressBar(pce.ProgressPercentage, 100);
ChangeAdditional(string.Format(Strings.RemainingTime, rem));
}, tok.Token);
WorkCancelled -= handler;
ChangeProgressBarEstimate(pce.ProgressPercentage, 100, started);
}, CancellationTokenSource.Token);
}

private void Cleanup(string f)
Expand Down Expand Up @@ -430,14 +417,10 @@ private void GenerateISO(string archs, SevenZipExtractor ext, string isodir)
if (double.TryParse(pp, NumberStyles.Float, CultureInfo.InvariantCulture, out var d))
if (d >= 0 && d <= 100)
{
var rem = TimeSpan.FromSeconds((DateTime.Now - started).TotalSeconds / d *
(100 - d));
ChangeProgress(
Convert.ToInt32(Math.Round(d, 0, MidpointRounding.AwayFromZero)),
100, Strings.CreatingISO + " " + (d / 100).ToString("P"));
ChangeStatus(Strings.CreatingISO + " " + (d / 100).ToString("P"));
ChangeAdditional(string.Format(Strings.RemainingTime, rem));
ChangeProgressBarEstimate(
(int)Math.Round(d, 0, MidpointRounding.AwayFromZero), 100, started);
}
}
catch
Expand Down
4 changes: 2 additions & 2 deletions SharpBoot/Forms/MainWindow.Designer.cs

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

23 changes: 15 additions & 8 deletions SharpBoot/Forms/MainWindow.cs
Expand Up @@ -9,6 +9,7 @@
using System.Net;
using System.Reflection;
using System.Resources;
using System.Security.Cryptography;
using System.Threading;
using System.Windows.Forms;
using System.Xml.Linq;
Expand Down Expand Up @@ -515,7 +516,8 @@ private void btnChecksum_Click(object sender, EventArgs e)
btnChecksum.ShowContextMenuStrip();
}

private void chksum(string n, Func<string> f)
private void chksum<T>(string n)
where T : HashAlgorithm, new()
{
Cursor = Cursors.WaitCursor;

Expand All @@ -524,9 +526,14 @@ private void chksum(string n, Func<string> f)
frm.WorkHandler = () =>
{
var d = DateTime.Now;
var sb = f();
var sb = Hash.FileHash<T>(
lvIsos.SelectedRows[0].Cells[4].Value.ToString(),
frm.CancellationTokenSource.Token,
(prog, size) =>
{
frm.ChangeProgressBarEstimate((int)(prog * 100 / size), 100, d);
});
var a = DateTime.Now;
var t = a - d;
Expand Down Expand Up @@ -694,22 +701,22 @@ private void btnInstBoot_Click(object sender, EventArgs e)

private void btnSha1_Click(object sender, EventArgs e)
{
chksum("SHA-1", () => Hash.FileSHA1(lvIsos.SelectedRows[0].Cells[4].Value.ToString()));
chksum<SHA1CryptoServiceProvider>("SHA-1");
}

private void btnSha256_Click(object sender, EventArgs e)
{
chksum("SHA-256", () => Hash.FileSHA256(lvIsos.SelectedRows[0].Cells[4].Value.ToString()));
chksum<SHA256CryptoServiceProvider>("SHA-256");
}

private void btnSha512_Click(object sender, EventArgs e)
{
chksum("SHA-512", () => Hash.FileSHA512(lvIsos.SelectedRows[0].Cells[4].Value.ToString()));
chksum<SHA512CryptoServiceProvider>("SHA-512");
}

private void btnSha384_Click(object sender, EventArgs e)
{
chksum("SHA-384", () => Hash.FileSHA384(lvIsos.SelectedRows[0].Cells[4].Value.ToString()));
chksum<SHA384CryptoServiceProvider>("SHA-384");
}

private void lvIsos_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
Expand Down Expand Up @@ -737,7 +744,7 @@ private void btnUSB_Click(object sender, EventArgs e)

private void mD5ToolStripMenuItem_Click(object sender, EventArgs e)
{
chksum("MD5", () => Hash.FileMD5(lvIsos.SelectedRows[0].Cells[4].Value.ToString()));
chksum<MD5CryptoServiceProvider>("MD5");
}

private void updateAvailableToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
27 changes: 15 additions & 12 deletions SharpBoot/Forms/MainWindow.resx
Expand Up @@ -329,7 +329,7 @@ the ISO or IMG file here</value>
<value>Bottom</value>
</data>
<data name="pnlBottom.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 571</value>
<value>0, 513</value>
</data>
<data name="pnlBottom.Size" type="System.Drawing.Size, System.Drawing">
<value>990, 128</value>
Expand Down Expand Up @@ -359,7 +359,7 @@ the ISO or IMG file here</value>
<value>NoControl</value>
</data>
<data name="btnCustomCode.Location" type="System.Drawing.Point, System.Drawing">
<value>5, 346</value>
<value>5, 288</value>
</data>
<data name="btnCustomCode.Size" type="System.Drawing.Size, System.Drawing">
<value>190, 46</value>
Expand Down Expand Up @@ -407,7 +407,7 @@ the ISO or IMG file here</value>
<value>cbxRes</value>
</data>
<data name="&gt;&gt;cbxRes.Type" xml:space="preserve">
<value>GroupedComboBox, DropDownControls, Version=1.0.4.0, Culture=neutral, PublicKeyToken=null</value>
<value>SharpBoot.Controls.GroupedComboBox.GroupedComboBox, SharpBoot, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null</value>
</data>
<data name="&gt;&gt;cbxRes.Parent" xml:space="preserve">
<value>groupBox3</value>
Expand All @@ -416,7 +416,7 @@ the ISO or IMG file here</value>
<value>0</value>
</data>
<data name="groupBox3.Location" type="System.Drawing.Point, System.Drawing">
<value>6, 289</value>
<value>6, 231</value>
</data>
<data name="groupBox3.Size" type="System.Drawing.Size, System.Drawing">
<value>188, 52</value>
Expand Down Expand Up @@ -446,7 +446,7 @@ the ISO or IMG file here</value>
<value>NoControl</value>
</data>
<data name="btnAbout.Location" type="System.Drawing.Point, System.Drawing">
<value>5, 496</value>
<value>5, 438</value>
</data>
<data name="btnAbout.Size" type="System.Drawing.Size, System.Drawing">
<value>190, 46</value>
Expand Down Expand Up @@ -527,7 +527,7 @@ the ISO or IMG file here</value>
<value>NoControl</value>
</data>
<data name="btnChecksum.Location" type="System.Drawing.Point, System.Drawing">
<value>5, 396</value>
<value>5, 338</value>
</data>
<data name="btnChecksum.Size" type="System.Drawing.Size, System.Drawing">
<value>190, 46</value>
Expand All @@ -548,7 +548,7 @@ the ISO or IMG file here</value>
<value>btnChecksum</value>
</data>
<data name="&gt;&gt;btnChecksum.Type" xml:space="preserve">
<value>wyDay.Controls.SplitButton, SharpBoot, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null</value>
<value>SharpBoot.Controls.SplitButton, SharpBoot, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null</value>
</data>
<data name="&gt;&gt;btnChecksum.Parent" xml:space="preserve">
<value>panel1</value>
Expand Down Expand Up @@ -737,7 +737,7 @@ the ISO or IMG file here</value>
<value>NoControl</value>
</data>
<data name="btnRemISO.Location" type="System.Drawing.Point, System.Drawing">
<value>5, 446</value>
<value>5, 388</value>
</data>
<data name="btnRemISO.Size" type="System.Drawing.Size, System.Drawing">
<value>190, 46</value>
Expand Down Expand Up @@ -824,7 +824,7 @@ the ISO or IMG file here</value>
<value>790, 24</value>
</data>
<data name="panel1.Size" type="System.Drawing.Size, System.Drawing">
<value>200, 547</value>
<value>200, 489</value>
</data>
<data name="panel1.TabIndex" type="System.Int32, mscorlib">
<value>2</value>
Expand Down Expand Up @@ -902,7 +902,7 @@ the ISO or IMG file here</value>
<value>0, 24</value>
</data>
<data name="lvIsos.Size" type="System.Drawing.Size, System.Drawing">
<value>790, 547</value>
<value>790, 489</value>
</data>
<data name="lvIsos.TabIndex" type="System.Int32, mscorlib">
<value>4</value>
Expand Down Expand Up @@ -1085,13 +1085,16 @@ the ISO or IMG file here</value>
<value>7, 15</value>
</data>
<data name="$this.ClientSize" type="System.Drawing.Size, System.Drawing">
<value>990, 699</value>
<value>990, 641</value>
</data>
<data name="$this.Font" type="System.Drawing.Font, System.Drawing">
<value>Segoe UI, 9pt</value>
</data>
<data name="$this.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="$this.MinimumSize" type="System.Drawing.Size, System.Drawing">
<value>1006, 738</value>
<value>1006, 680</value>
</data>
<data name="$this.Text" xml:space="preserve">
<value>SharpBoot</value>
Expand Down
12 changes: 12 additions & 0 deletions SharpBoot/Forms/WorkerFrm.cs
Expand Up @@ -33,6 +33,8 @@ protected WorkerFrm()
public event EventHandler WorkFinished;
public event EventHandler WorkCancelled;

public CancellationTokenSource CancellationTokenSource { get; } = new CancellationTokenSource();

protected virtual void OnFinished(EventArgs e)
{
if (!abort)
Expand All @@ -58,6 +60,15 @@ public void ChangeProgressBar(int val, int max)
});
}

public void ChangeProgressBarEstimate(int val, int max, DateTime started)
{
var rem = TimeSpan.FromSeconds(val == 0 ? 0 : ((DateTime.Now - started).TotalSeconds / val *
(max - val)));

ChangeProgressBar(val, max);
ChangeAdditional(string.Format(Strings.RemainingTime, rem));
}

public void ChangeStatus(string stat)
{
lblStatus.InvokeIfRequired(() => lblStatus.Text = stat);
Expand Down Expand Up @@ -109,6 +120,7 @@ public void CancelWork()
{
closeonclick = true;
abort = true;
CancellationTokenSource.Cancel();
bwkWorker.CancelAsync();
SetCancel();
WorkCancelled?.Invoke(this, EventArgs.Empty);
Expand Down
2 changes: 1 addition & 1 deletion SharpBoot/Strings.Designer.cs

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

2 changes: 1 addition & 1 deletion SharpBoot/Strings.resx
Expand Up @@ -265,6 +265,6 @@
<value>About SharpBoot</value>
</data>
<data name="RemainingTime" xml:space="preserve">
<value>Remaining time: {0:g}</value>
<value>Remaining time: {0:hh\:mm\:ss}</value>
</data>
</root>
39 changes: 37 additions & 2 deletions SharpBoot/Utilities/Hash.cs
Expand Up @@ -4,12 +4,47 @@
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SharpBoot.Utilities
{
public class Hash
{
public static string FileHash<T>(string fileName)
// https://stackoverflow.com/a/53966139/2196124
public static async Task<byte[]> ComputeHashAsync(
HashAlgorithm hashAlgorithm, Stream stream,
CancellationToken cancellationToken = default,
Action<long, long> progress = null,
int bufferSize = 1024 * 1024)
{
long totalBytesRead = 0;
var size = stream.Length;
var readAheadBuffer = new byte[bufferSize];
var readAheadBytesRead = await stream.ReadAsync(readAheadBuffer, 0,
readAheadBuffer.Length, cancellationToken);
totalBytesRead += readAheadBytesRead;
do
{
var bytesRead = readAheadBytesRead;
var buffer = readAheadBuffer;
readAheadBuffer = new byte[bufferSize];
readAheadBytesRead = await stream.ReadAsync(readAheadBuffer, 0,
readAheadBuffer.Length, cancellationToken);
totalBytesRead += readAheadBytesRead;

if (readAheadBytesRead == 0)
hashAlgorithm.TransformFinalBlock(buffer, 0, bytesRead);
else
hashAlgorithm.TransformBlock(buffer, 0, bytesRead, buffer, 0);
progress?.Invoke(totalBytesRead, size);
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();
} while (readAheadBytesRead != 0);
return hashAlgorithm.Hash;
}

public static string FileHash<T>(string fileName, CancellationToken cancellationToken = default, Action<long, long> progress = null)
where T : HashAlgorithm, new()
{
StringBuilder formatted;
Expand All @@ -18,7 +53,7 @@ public static string FileHash<T>(string fileName)
{
using (var ha = new T())
{
var hash = ha.ComputeHash(bs);
var hash = ComputeHashAsync(ha, bs, cancellationToken, progress).Result;
formatted = new StringBuilder(2 * hash.Length);
foreach (var b in hash) formatted.AppendFormat("{0:x2}", b);
}
Expand Down

0 comments on commit e1d7311

Please sign in to comment.