Progress report when patching

This commit is contained in:
Westin Miller
2017-11-01 17:09:18 -07:00
parent c8377b318e
commit d5702d3065
3 changed files with 36 additions and 6 deletions

View File

@@ -27,11 +27,16 @@ namespace Torch.Managers.PatchManager
private byte[] _revertData = null; private byte[] _revertData = null;
private GCHandle? _pinnedPatch; private GCHandle? _pinnedPatch;
internal bool HasChanged()
{
return Prefixes.HasChanges() || Suffixes.HasChanges() || Transpilers.HasChanges();
}
internal void Commit() internal void Commit()
{ {
try try
{ {
if (!Prefixes.HasChanges() && !Suffixes.HasChanges() && !Transpilers.HasChanges()) if (!Prefixes.HasChanges(true) && !Suffixes.HasChanges(true) && !Transpilers.HasChanges(true))
return; return;
Revert(); Revert();

View File

@@ -36,9 +36,11 @@ namespace Torch.Managers.PatchManager
private int _hasChanges = 0; private int _hasChanges = 0;
internal bool HasChanges() internal bool HasChanges(bool reset = false)
{ {
if (reset)
return Interlocked.Exchange(ref _hasChanges, 0) != 0; return Interlocked.Exchange(ref _hasChanges, 0) != 0;
return _hasChanges != 0;
} }
/// <summary> /// <summary>

View File

@@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading;
using NLog; using NLog;
using Torch.API; using Torch.API;
using Torch.Managers.PatchManager.Transpile; using Torch.Managers.PatchManager.Transpile;
@@ -149,17 +151,38 @@ namespace Torch.Managers.PatchManager
} }
private static int _finishedPatchCount, _dirtyPatchCount;
private static void DoCommit(DecoratedMethod method)
{
if (!method.HasChanged())
return;
method.Commit();
int value = Interlocked.Increment(ref _finishedPatchCount);
var actualPercentage = (value * 100) / _dirtyPatchCount;
var currentPrintGroup = actualPercentage / 10;
var prevPrintGroup = (value - 1) * 10 / _dirtyPatchCount;
if (currentPrintGroup != prevPrintGroup && value >= 1)
{
_log.Info($"Patched {value}/{_dirtyPatchCount}. ({actualPercentage:D2}%)");
}
}
/// <inheritdoc cref="Commit"/> /// <inheritdoc cref="Commit"/>
internal static void CommitInternal() internal static void CommitInternal()
{ {
lock (_rewritePatterns) lock (_rewritePatterns)
{ {
_log.Info("Patching begins...");
_finishedPatchCount = 0;
_dirtyPatchCount = _rewritePatterns.Values.Sum(x => x.HasChanged() ? 1 : 0);
#if true #if true
ParallelTasks.Parallel.ForEach(_rewritePatterns.Values, x => x.Commit()); ParallelTasks.Parallel.ForEach(_rewritePatterns.Values, DoCommit);
#else #else
foreach (DecoratedMethod m in _rewritePatterns.Values) foreach (DecoratedMethod m in _rewritePatterns.Values)
m.Commit(); DoCommit(m);
#endif #endif
_log.Info("Patching done");
} }
} }