Skip to content

Commit

Permalink
Lock _progress in installer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bagusnl committed Dec 24, 2024
1 parent bc7b320 commit 51ed030
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1791,30 +1791,33 @@ void StartWriteInner(byte[] bufferInner, FileStream outputStream, Stream entrySt
// Increment total size
_progressAllSizeCurrent += read;
_progressPerFileSizeCurrent += read;

// Calculate the speed
_progress.ProgressAllSpeed = CalculateSpeed(read);
lock (_progress) _progress.ProgressAllSpeed = CalculateSpeed(read);

if (!CheckIfNeedRefreshStopwatch())
{
continue;
}

// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Calculate percentage
_progress.ProgressAllPercentage =
Math.Round((double)_progressAllSizeCurrent / _progressAllSizeTotal * 100, 2);
_progress.ProgressPerFilePercentage =
Math.Round((double)_progressPerFileSizeCurrent / _progressPerFileSizeTotal * 100, 2);
// Calculate the timelapse
_progress.ProgressAllTimeLeft =
((_progressAllSizeTotal - _progressAllSizeCurrent) / _progress.ProgressAllSpeed.Unzeroed())
.ToTimeSpanNormalized();
lock (_progress)
{
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Calculate percentage
_progress.ProgressAllPercentage =
Math.Round((double)_progressAllSizeCurrent / _progressAllSizeTotal * 100, 2);
_progress.ProgressPerFilePercentage =
Math.Round((double)_progressPerFileSizeCurrent / _progressPerFileSizeTotal * 100, 2);
// Calculate the timelapse
_progress.ProgressAllTimeLeft =
((_progressAllSizeTotal - _progressAllSizeCurrent) / _progress.ProgressAllSpeed.Unzeroed())
.ToTimeSpanNormalized();
}

UpdateAll();
}
Expand Down Expand Up @@ -2318,11 +2321,14 @@ private async ValueTask FileHdiffPatcherInner(string patchPath, string sourceBas
public virtual async ValueTask ApplyHdiffListPatch()
{
List<PkgVersionProperties> hdiffEntry = TryGetHDiffList();

_progress.ProgressAllSizeTotal = hdiffEntry.Sum(x => x.fileSize);
_progress.ProgressAllSizeCurrent = 0;
_status.IsIncludePerFileIndicator = false;

lock (_progress)
{
_progress.ProgressAllSizeTotal = hdiffEntry.Sum(x => x.fileSize);
_progress.ProgressAllSizeCurrent = 0;
}

_progressAllCountTotal = 1;
_progressAllCountFound = hdiffEntry.Count;

Expand Down Expand Up @@ -2371,14 +2377,15 @@ public virtual async ValueTask ApplyHdiffListPatch()
lock (_progress)
{
_progress.ProgressAllSizeCurrent += entry.fileSize;
_progress.ProgressAllPercentage =
Math.Round(_progress.ProgressAllSizeCurrent / _progress.ProgressAllSizeTotal * 100, 2);
_progress.ProgressAllSpeed = CalculateSpeed(entry.fileSize);

_progress.ProgressAllTimeLeft =
((_progress.ProgressAllSizeTotal - _progress.ProgressAllSizeCurrent) /
_progress.ProgressAllSpeed.Unzeroed()).ToTimeSpanNormalized();
}
_progress.ProgressAllPercentage =
Math.Round(_progress.ProgressAllSizeCurrent / _progress.ProgressAllSizeTotal * 100, 2);
_progress.ProgressAllSpeed = CalculateSpeed(entry.fileSize);

_progress.ProgressAllTimeLeft =
((_progress.ProgressAllSizeTotal - _progress.ProgressAllSizeCurrent) /
_progress.ProgressAllSpeed.Unzeroed()).ToTimeSpanNormalized();
UpdateProgress();
}
finally
Expand Down Expand Up @@ -2433,13 +2440,16 @@ private void EventListener_PatchEvent(object sender, PatchEvent e)
}
if (CheckIfNeedRefreshStopwatch())
{
_progress.ProgressAllPercentage =
Math.Round(_progress.ProgressAllSizeCurrent / _progress.ProgressAllSizeTotal * 100, 2);
_progress.ProgressAllSpeed = CalculateSpeed(e.Read);
lock (_progress)
{
_progress.ProgressAllPercentage =
Math.Round(_progress.ProgressAllSizeCurrent / _progress.ProgressAllSizeTotal * 100, 2);
_progress.ProgressAllSpeed = CalculateSpeed(e.Read);

_progress.ProgressAllTimeLeft =
((_progress.ProgressAllSizeTotal - _progress.ProgressAllSizeCurrent) /
_progress.ProgressAllSpeed.Unzeroed()).ToTimeSpanNormalized();
_progress.ProgressAllTimeLeft =
((_progress.ProgressAllSizeTotal - _progress.ProgressAllSizeCurrent) /
_progress.ProgressAllSpeed.Unzeroed()).ToTimeSpanNormalized();
}
UpdateProgress();
}
}
Expand Down Expand Up @@ -4052,8 +4062,11 @@ public void UpdateCompletenessStatus(CompletenessStatus status)
InnerLauncherConfig.AppDiscordPresence?.SetActivity(ActivityType.Idle);
#endif
// HACK: Fix the progress not achieving 100% while completed
_progress.ProgressAllPercentage = 100f;
_progress.ProgressPerFilePercentage = 100f;
lock (_progress)
{
_progress.ProgressAllPercentage = 100f;
_progress.ProgressPerFilePercentage = 100f;
}
break;
case CompletenessStatus.Cancelled:
IsRunning = false;
Expand Down Expand Up @@ -4128,13 +4141,16 @@ protected void UpdateProgressBase()

protected void DeltaPatchCheckProgress(object sender, PatchEvent e)
{
_progress.ProgressAllPercentage = e.ProgressPercentage;
lock (_progress)
{
_progress.ProgressAllPercentage = e.ProgressPercentage;

_progress.ProgressAllTimeLeft = e.TimeLeft;
_progress.ProgressAllSpeed = e.Speed;
_progress.ProgressAllTimeLeft = e.TimeLeft;
_progress.ProgressAllSpeed = e.Speed;

_progress.ProgressAllSizeTotal = e.TotalSizeToBePatched;
_progress.ProgressAllSizeCurrent = e.CurrentSizePatched;
_progress.ProgressAllSizeTotal = e.TotalSizeToBePatched;
_progress.ProgressAllSizeCurrent = e.CurrentSizePatched;
}

if (CheckIfNeedRefreshStopwatch())
{
Expand Down Expand Up @@ -4172,14 +4188,17 @@ protected void DeltaPatchCheckLogEvent(object sender, LoggerEvent e)

protected void DeltaPatchCheckProgress(object sender, TotalPerFileProgress e)
{
_progress.ProgressAllPercentage =
e.ProgressAllPercentage == 0 ? e.ProgressPerFilePercentage : e.ProgressAllPercentage;
lock (_progress)
{
_progress.ProgressAllPercentage =
e.ProgressAllPercentage == 0 ? e.ProgressPerFilePercentage : e.ProgressAllPercentage;

_progress.ProgressAllTimeLeft = e.ProgressAllTimeLeft;
_progress.ProgressAllSpeed = e.ProgressAllSpeed;
_progress.ProgressAllTimeLeft = e.ProgressAllTimeLeft;
_progress.ProgressAllSpeed = e.ProgressAllSpeed;

_progress.ProgressAllSizeTotal = e.ProgressAllSizeTotal;
_progress.ProgressAllSizeCurrent = e.ProgressAllSizeCurrent;
_progress.ProgressAllSizeTotal = e.ProgressAllSizeTotal;
_progress.ProgressAllSizeCurrent = e.ProgressAllSizeCurrent;
}

if (CheckIfNeedRefreshStopwatch())
{
Expand All @@ -4201,24 +4220,27 @@ private void ZipProgressAdapter(object sender, ExtractProgressProp e)
_progressPerFileSizeCurrent = (long)e.TotalRead;
_progressPerFileSizeTotal = (long)e.TotalSize;

// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;
lock (_progress)
{
// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Calculate the speed
_progress.ProgressAllSpeed = CalculateSpeed(lastSize);
// Calculate the speed
_progress.ProgressAllSpeed = CalculateSpeed(lastSize);

// Calculate percentage
_progress.ProgressAllPercentage =
Math.Round((double)_progressAllSizeCurrent / _progressAllSizeTotal * 100, 2);
_progress.ProgressPerFilePercentage =
Math.Round((double)_progressPerFileSizeCurrent / _progressPerFileSizeTotal * 100, 2);
// Calculate the timelapse
_progress.ProgressAllTimeLeft =
((_progressAllSizeTotal - _progressAllSizeCurrent) / _progress.ProgressAllSpeed.Unzeroed())
.ToTimeSpanNormalized();
// Calculate percentage
_progress.ProgressAllPercentage =
Math.Round((double)_progressAllSizeCurrent / _progressAllSizeTotal * 100, 2);
_progress.ProgressPerFilePercentage =
Math.Round((double)_progressPerFileSizeCurrent / _progressPerFileSizeTotal * 100, 2);
// Calculate the timelapse
_progress.ProgressAllTimeLeft =
((_progressAllSizeTotal - _progressAllSizeCurrent) / _progress.ProgressAllSpeed.Unzeroed())
.ToTimeSpanNormalized();
}

UpdateAll();
}
Expand Down Expand Up @@ -4250,31 +4272,35 @@ private void HttpClientDownloadProgressAdapter(int read, DownloadProgress downlo

if (CheckIfNeedRefreshStopwatch())
{
// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Assign speed with clamped value
double speedClamped = speedAll.ClampLimitedSpeedNumber();
_progress.ProgressAllSpeed = speedClamped;
lock (_progress)
{
// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Calculate percentage
_progress.ProgressPerFilePercentage =
Math.Round(_progressPerFileSizeCurrent / (double)_progressPerFileSizeTotal * 100, 2);
_progress.ProgressAllPercentage =
Math.Round(_progressAllSizeCurrent / (double)_progressAllSizeTotal * 100, 2);
// Assign speed with clamped value
double speedClamped = speedAll.ClampLimitedSpeedNumber();
_progress.ProgressAllSpeed = speedClamped;

// Calculate the timelapse
double progressTimeAvg = (_progressAllSizeTotal - _progressAllSizeCurrent) / speedClamped;
_progress.ProgressAllTimeLeft = progressTimeAvg.ToTimeSpanNormalized();
// Calculate percentage
_progress.ProgressPerFilePercentage =
Math.Round(_progressPerFileSizeCurrent / (double)_progressPerFileSizeTotal * 100, 2);
_progress.ProgressAllPercentage =
Math.Round(_progressAllSizeCurrent / (double)_progressAllSizeTotal * 100, 2);

// Update the status of per file size and current progress from Http client
_progressPerFileSizeCurrent = downloadProgress.BytesDownloaded;
_progressPerFileSizeTotal = downloadProgress.BytesTotal;
_progress.ProgressPerFilePercentage = ConverterTool.GetPercentageNumber(downloadProgress.BytesDownloaded, downloadProgress.BytesTotal);
// Calculate the timelapse
double progressTimeAvg = (_progressAllSizeTotal - _progressAllSizeCurrent) / speedClamped;
_progress.ProgressAllTimeLeft = progressTimeAvg.ToTimeSpanNormalized();

// Update the status of per file size and current progress from Http client
_progressPerFileSizeCurrent = downloadProgress.BytesDownloaded;
_progressPerFileSizeTotal = downloadProgress.BytesTotal;
_progress.ProgressPerFilePercentage =
ConverterTool.GetPercentageNumber(downloadProgress.BytesDownloaded,
downloadProgress.BytesTotal);
}
// Update the status
UpdateAll();
}
Expand All @@ -4299,25 +4325,28 @@ private void HttpClientDownloadProgressAdapter(object sender, DownloadEvent e)
{
if (e.State != DownloadState.Merging)
{
// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Calculate the speed
_progress.ProgressAllSpeed = speedAll;

// Calculate percentage
_progress.ProgressPerFilePercentage =
Math.Round(_progressPerFileSizeCurrent / (double)_progressPerFileSizeTotal * 100, 2);
_progress.ProgressAllPercentage =
Math.Round(_progressAllSizeCurrent / (double)_progressAllSizeTotal * 100, 2);

// Calculate the timelapse
_progress.ProgressAllTimeLeft =
((_progressAllSizeTotal - _progressAllSizeCurrent) / _progress.ProgressAllSpeed.Unzeroed())
.ToTimeSpanNormalized();
lock (_progress)
{
// Assign local sizes to progress
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;

// Calculate the speed
_progress.ProgressAllSpeed = speedAll;

// Calculate percentage
_progress.ProgressPerFilePercentage =
Math.Round(_progressPerFileSizeCurrent / (double)_progressPerFileSizeTotal * 100, 2);
_progress.ProgressAllPercentage =
Math.Round(_progressAllSizeCurrent / (double)_progressAllSizeTotal * 100, 2);

// Calculate the timelapse
_progress.ProgressAllTimeLeft =
((_progressAllSizeTotal - _progressAllSizeCurrent) / _progress.ProgressAllSpeed.Unzeroed())
.ToTimeSpanNormalized();
}
}
else
{
Expand All @@ -4327,22 +4356,25 @@ private void HttpClientDownloadProgressAdapter(object sender, DownloadEvent e)

// If status is merging, then use progress for speed and timelapse from Http client
// and set the rest from the base class
_progress.ProgressAllTimeLeft = e.TimeLeft;
lock (_progress)
{
_progress.ProgressAllTimeLeft = e.TimeLeft;

_progress.ProgressAllSpeed = speedAll;
_progress.ProgressAllSpeed = speedAll;

_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;
_progress.ProgressAllPercentage =
Math.Round(_progressAllSizeCurrent / (double)_progressAllSizeTotal * 100, 2);
_progress.ProgressPerFileSizeCurrent = _progressPerFileSizeCurrent;
_progress.ProgressPerFileSizeTotal = _progressPerFileSizeTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
_progress.ProgressAllSizeTotal = _progressAllSizeTotal;
_progress.ProgressAllPercentage =
Math.Round(_progressAllSizeCurrent / (double)_progressAllSizeTotal * 100, 2);
}
}

// Update the status of per file size and current progress from Http client
_progressPerFileSizeCurrent = e.SizeDownloaded;
_progressPerFileSizeTotal = e.SizeToBeDownloaded;
_progress.ProgressPerFilePercentage = e.ProgressPercentage;
lock (_progress) _progress.ProgressPerFilePercentage = e.ProgressPercentage;

// Update the status
UpdateAll();
Expand Down
Loading

0 comments on commit 51ed030

Please sign in to comment.