Skip to content

Commit

Permalink
🎨 float Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Oct 28, 2024
1 parent fedc2cc commit bd0e5f6
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 275 deletions.
32 changes: 16 additions & 16 deletions SevenZip/ArchiveExtractCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,19 @@ private void IntEventArgsHandler(object sender, IntEventArgs e)
return;
}

var pold = (int)(_bytesWrittenOld * 100 / _bytesCount);
var pold = _bytesWrittenOld * 100f / _bytesCount;
_bytesWritten += e.Value;
var pnow = (int)(_bytesWritten * 100 / _bytesCount);
var pnow = _bytesWritten * 100f / _bytesCount;

if (pnow > pold)
{
if (pnow > 100)
if (pnow > 100f)
{
pold = pnow = 0;
}

_bytesWrittenOld = _bytesWritten;
Extracting?.Invoke(this, new ProgressEventArgs((byte)pnow, (byte)(pnow - pold)));
Extracting?.Invoke(this, new ProgressEventArgs(pnow, pnow - pold));
}
}

Expand Down Expand Up @@ -254,7 +254,7 @@ public int GetStream(uint index, out ISequentialOutStream outStream, AskMode ask
try
{
fileName = Path.Combine(RemoveIllegalCharacters(_directory, true), RemoveIllegalCharacters(_directoryStructure ? entryName : Path.GetFileName(entryName)));

if (string.IsNullOrEmpty(fileName))
{
throw new SevenZipArchiveException("Some archive name is null or empty.");
Expand All @@ -274,13 +274,13 @@ public int GetStream(uint index, out ISequentialOutStream outStream, AskMode ask
{
_archive.GetProperty(index, ItemPropId.LastWriteTime, ref data);
var time = NativeMethods.SafeCast(data, DateTime.MinValue);

if (File.Exists(fileName))
{
var fnea = new FileOverwriteEventArgs(fileName);

FileExists?.Invoke(this, fnea);

if (fnea.Cancel)
{
Canceled = true;
Expand All @@ -298,10 +298,10 @@ public int GetStream(uint index, out ISequentialOutStream outStream, AskMode ask
}

_doneRate += 1.0f / _filesCount;
var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[(int) index], PercentDoneEventArgs.ProducePercentDone(_doneRate));
var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[(int)index], _doneRate);

FileExtractionStarted?.Invoke(this, iea);

if (iea.Cancel)
{
Canceled = true;
Expand Down Expand Up @@ -332,14 +332,14 @@ public int GetStream(uint index, out ISequentialOutStream outStream, AskMode ask
}

_fileStream.BytesWritten += IntEventArgsHandler;
outStream = _fileStream;
outStream = _fileStream;
}
else
{
_doneRate += 1.0f / _filesCount;
var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[(int)index], PercentDoneEventArgs.ProducePercentDone(_doneRate));
var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[(int)index], _doneRate);
FileExtractionStarted?.Invoke(this, iea);

if (iea.Cancel)
{
Canceled = true;
Expand Down Expand Up @@ -378,7 +378,7 @@ public int GetStream(uint index, out ISequentialOutStream outStream, AskMode ask

if (index == _fileIndex)
{
outStream = _fileStream;
outStream = _fileStream;
_fileIndex = null;
}
else
Expand Down Expand Up @@ -416,7 +416,7 @@ public void SetOperationResult(OperationResult operationResult)
case OperationResult.UnexpectedEnd:
AddException(new ExtractionFailedException("Unexpected end of file."));
break;
case OperationResult.DataAfterEnd:
case OperationResult.DataAfterEnd:
AddException(new ExtractionFailedException("Data after end of archive."));
break;
case OperationResult.IsNotArc:
Expand Down Expand Up @@ -446,9 +446,9 @@ public void SetOperationResult(OperationResult operationResult)
_fileStream = null;
}

var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[_currentIndex], PercentDoneEventArgs.ProducePercentDone(_doneRate));
var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[_currentIndex], _doneRate);
FileExtractionFinished?.Invoke(this, iea);

if (iea.Cancel)
{
Canceled = true;
Expand Down
68 changes: 34 additions & 34 deletions SevenZip/ArchiveUpdateCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal sealed class ArchiveUpdateCallback : CallbackBase, IArchiveUpdateCallba
/// <summary>
/// Gets or sets the value indicating whether to compress as fast as possible, without calling events.
/// </summary>
public bool FastCompression { private get; set; }
public bool FastCompression { private get; set; }

private int _memoryPressure;

Expand Down Expand Up @@ -183,7 +183,7 @@ private void CommonInit(SevenZipCompressor compressor, UpdateData updateData, bo
}
_updateData = updateData;
_directoryStructure = directoryStructure;
DefaultItemName = "default";
DefaultItemName = "default";
}

private void Init(
Expand Down Expand Up @@ -282,10 +282,10 @@ private bool EventsForGetStream(uint index)
_fileStream.BytesRead += IntEventArgsHandler;
}
_doneRate += 1.0f / _actualFilesCount;
var fiea = new FileNameEventArgs(_files != null? _files[index].Name : _entries[index],
PercentDoneEventArgs.ProducePercentDone(_doneRate));
var fiea = new FileNameEventArgs(_files != null ? _files[index].Name : _entries[index],
_doneRate);
OnFileCompression(fiea);

if (fiea.Cancel)
{
Canceled = true;
Expand Down Expand Up @@ -332,9 +332,9 @@ private void OnFileCompressionFinished(EventArgs e)

#region IArchiveUpdateCallback Members

public void SetTotal(ulong total) {}
public void SetTotal(ulong total) { }

public void SetCompleted(ref ulong completeValue) {}
public void SetCompleted(ref ulong completeValue) { }

public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties, ref uint indexInArchive)
{
Expand Down Expand Up @@ -375,7 +375,7 @@ public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties,
do
{
indexInArchive--;
} while (indexInArchive > 0
} while (indexInArchive > 0
&& _updateData.FileNamesToModify.ContainsKey((int)indexInArchive)
&& _updateData.FileNamesToModify[(int)indexInArchive] == null);
}
Expand Down Expand Up @@ -438,7 +438,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
val = _updateData.FileNamesToModify[(int) index];
val = _updateData.FileNamesToModify[(int)index];
}
value.Value = Marshal.StringToBSTR(val);
#endregion
Expand All @@ -465,7 +465,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.UInt64Value = Convert.ToUInt64(_updateData.ArchiveFileData[(int) index].IsDirectory);
value.UInt64Value = Convert.ToUInt64(_updateData.ArchiveFileData[(int)index].IsDirectory);
}
break;
case ItemPropId.Size:
Expand All @@ -479,23 +479,23 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
{
if (_streams == null)
{
size = _bytesCount > 0 ? (ulong) _bytesCount : 0;
size = _bytesCount > 0 ? (ulong)_bytesCount : 0;
}
else
{
size = (ulong) (_streams[index] == null? 0 : _streams[index].Length);
size = (ulong)(_streams[index] == null ? 0 : _streams[index].Length);
}
}
else
{
size = (_files[index].Attributes & FileAttributes.Directory) == 0
? (ulong) _files[index].Length
? (ulong)_files[index].Length
: 0;
}
}
else
{
size = _updateData.ArchiveFileData[(int) index].Size;
size = _updateData.ArchiveFileData[(int)index].Size;
}
value.UInt64Value = size;

Expand All @@ -518,12 +518,12 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.UInt32Value = (uint) _files[index].Attributes;
value.UInt32Value = (uint)_files[index].Attributes;
}
}
else
{
value.UInt32Value = _updateData.ArchiveFileData[(int) index].Attributes;
value.UInt32Value = _updateData.ArchiveFileData[(int)index].Attributes;
}
break;
#region Times
Expand All @@ -537,7 +537,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.Int64Value = _updateData.ArchiveFileData[(int) index].CreationTime.ToFileTime();
value.Int64Value = _updateData.ArchiveFileData[(int)index].CreationTime.ToFileTime();
}
break;
case ItemPropId.LastAccessTime:
Expand All @@ -550,7 +550,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.Int64Value = _updateData.ArchiveFileData[(int) index].LastAccessTime.ToFileTime();
value.Int64Value = _updateData.ArchiveFileData[(int)index].LastAccessTime.ToFileTime();
}
break;
case ItemPropId.LastWriteTime:
Expand All @@ -563,7 +563,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.Int64Value = _updateData.ArchiveFileData[(int) index].LastWriteTime.ToFileTime();
value.Int64Value = _updateData.ArchiveFileData[(int)index].LastWriteTime.ToFileTime();
}
break;
#endregion
Expand All @@ -589,7 +589,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
val = Path.GetExtension(_updateData.ArchiveFileData[(int) index].FileName);
val = Path.GetExtension(_updateData.ArchiveFileData[(int)index].FileName);
value.Value = Marshal.StringToBSTR(val);
}

Expand Down Expand Up @@ -688,7 +688,7 @@ public void SetOperationResult(OperationResult operationResult)
case OperationResult.UnexpectedEnd:
AddException(new ExtractionFailedException("Unexpected end of file."));
break;
case OperationResult.DataAfterEnd:
case OperationResult.DataAfterEnd:
AddException(new ExtractionFailedException("Data after end of archive."));
break;
case OperationResult.IsNotArc:
Expand All @@ -714,18 +714,18 @@ public void SetOperationResult(OperationResult operationResult)
{
try
{
_fileStream.Dispose();
_fileStream.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}
else
{
_wrappersToDispose.Add(_fileStream);
}
}

_fileStream = null;
}

OnFileCompressionFinished(EventArgs.Empty);
}

Expand Down Expand Up @@ -755,7 +755,7 @@ public void Dispose()
{
_fileStream.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}

if (_wrappersToDispose == null)
Expand All @@ -769,35 +769,35 @@ public void Dispose()
{
wrapper.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}
}

#endregion

private void IntEventArgsHandler(object sender, IntEventArgs e)
{
var lockObject = ((object) _files ?? _streams) ?? _fileStream;
var lockObject = ((object)_files ?? _streams) ?? _fileStream;

lock (lockObject)
{
var pOld = (byte) (_bytesWrittenOld*100/_bytesCount);
var pOld = _bytesWrittenOld * 100f / _bytesCount;
_bytesWritten += e.Value;
byte pNow;
float pNow;

if (_bytesCount < _bytesWritten) //Holy shit, this check for ZIP is golden
{
pNow = 100;
pNow = 100f;
}
else
{
pNow = (byte)((_bytesWritten * 100) / _bytesCount);
pNow = (_bytesWritten * 100f) / _bytesCount;
}

if (pNow > pOld)
{
_bytesWrittenOld = _bytesWritten;
OnCompressing(new ProgressEventArgs(pNow, (byte) (pNow - pOld)));
OnCompressing(new ProgressEventArgs(pNow, pNow - pOld));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion SevenZip/EventArguments/FileInfoEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class FileInfoEventArgs : PercentDoneEventArgs, ICancellable
/// </summary>
/// <param name="fileInfo">The current ArchiveFileInfo.</param>
/// <param name="percentDone">The percent of finished work.</param>
public FileInfoEventArgs(ArchiveFileInfo fileInfo, byte percentDone)
public FileInfoEventArgs(ArchiveFileInfo fileInfo, float percentDone)
: base(percentDone)
{
FileInfo = fileInfo;
Expand Down
2 changes: 1 addition & 1 deletion SevenZip/EventArguments/FileNameEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class FileNameEventArgs : PercentDoneEventArgs, ICancellable
/// </summary>
/// <param name="fileName">The file name.</param>
/// <param name="percentDone">The percent of finished work</param>
public FileNameEventArgs(string fileName, byte percentDone) :
public FileNameEventArgs(string fileName, float percentDone) :
base(percentDone)
{
FileName = fileName;
Expand Down
Loading

0 comments on commit bd0e5f6

Please sign in to comment.