Merge pull request 'try-catch parallel operations' (#18) from fix/change-ui-on-fail into main
Reviewed-on: #18
This commit is contained in:
commit
66f1832c21
Binary file not shown.
@ -298,57 +298,66 @@ namespace PatcherUtils
|
|||||||
|
|
||||||
RaiseProgressChanged(0, fileCountTotal, "Generating deltas...");
|
RaiseProgressChanged(0, fileCountTotal, "Generating deltas...");
|
||||||
|
|
||||||
Parallel.ForEach(targetFiles,
|
try
|
||||||
new ParallelOptions() { MaxDegreeOfParallelism = 5 }, targetFile =>
|
{
|
||||||
{
|
|
||||||
//find a matching source file based on the relative path of the file
|
|
||||||
FileInfo sourceFile = sourceFiles.Find(f =>
|
|
||||||
f.FullName.Replace(sourceDir.FullName, "") ==
|
|
||||||
targetFile.FullName.Replace(targetDir.FullName, ""));
|
|
||||||
|
|
||||||
//if the target file doesn't exist in the source files, the target file needs to be added.
|
|
||||||
if (sourceFile == null)
|
Parallel.ForEach(targetFiles,
|
||||||
|
new ParallelOptions() { MaxDegreeOfParallelism = 5 }, targetFile =>
|
||||||
{
|
{
|
||||||
PatchLogger.LogInfo("::: Creating .new file :::");
|
//find a matching source file based on the relative path of the file
|
||||||
CreateNewFile(targetFile.FullName);
|
FileInfo sourceFile = sourceFiles.Find(f =>
|
||||||
|
f.FullName.Replace(sourceDir.FullName, "") ==
|
||||||
|
targetFile.FullName.Replace(targetDir.FullName, ""));
|
||||||
|
|
||||||
|
//if the target file doesn't exist in the source files, the target file needs to be added.
|
||||||
|
if (sourceFile == null)
|
||||||
|
{
|
||||||
|
PatchLogger.LogInfo("::: Creating .new file :::");
|
||||||
|
CreateNewFile(targetFile.FullName);
|
||||||
|
|
||||||
|
newCount++;
|
||||||
|
filesProcessed++;
|
||||||
|
|
||||||
|
RaiseProgressChanged(filesProcessed, fileCountTotal,
|
||||||
|
$"{targetFile.FullName.Replace(TargetFolder, "...")}.new", AdditionalInfo.ToArray());
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string extension = "";
|
||||||
|
|
||||||
|
// if a matching source file was found, check the file hashes and get the delta.
|
||||||
|
// add it to the bag for removal later
|
||||||
|
if (!CompareFileHashes(sourceFile.FullName, targetFile.FullName))
|
||||||
|
{
|
||||||
|
foundFilesQueue.Enqueue(sourceFile);
|
||||||
|
PatchLogger.LogInfo("::: Creating .delta file :::");
|
||||||
|
CreateDelta(sourceFile.FullName, targetFile.FullName);
|
||||||
|
extension = ".delta";
|
||||||
|
deltaCount++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foundFilesQueue.Enqueue(sourceFile);
|
||||||
|
PatchLogger.LogInfo("::: File Exists :::");
|
||||||
|
existCount++;
|
||||||
|
}
|
||||||
|
|
||||||
newCount++;
|
|
||||||
filesProcessed++;
|
filesProcessed++;
|
||||||
|
|
||||||
|
AdditionalInfo[0].ItemValue = deltaCount;
|
||||||
|
AdditionalInfo[1].ItemValue = newCount;
|
||||||
|
AdditionalInfo[3].ItemValue = existCount;
|
||||||
|
|
||||||
RaiseProgressChanged(filesProcessed, fileCountTotal,
|
RaiseProgressChanged(filesProcessed, fileCountTotal,
|
||||||
$"{targetFile.FullName.Replace(TargetFolder, "...")}.new", AdditionalInfo.ToArray());
|
$"{targetFile.FullName.Replace(TargetFolder, "...")}{extension}", AdditionalInfo.ToArray());
|
||||||
|
});
|
||||||
return;
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
|
{
|
||||||
string extension = "";
|
PatchLogger.LogException(ex);
|
||||||
|
}
|
||||||
// if a matching source file was found, check the file hashes and get the delta.
|
|
||||||
// add it to the bag for removal later
|
|
||||||
if (!CompareFileHashes(sourceFile.FullName, targetFile.FullName))
|
|
||||||
{
|
|
||||||
foundFilesQueue.Enqueue(sourceFile);
|
|
||||||
PatchLogger.LogInfo("::: Creating .delta file :::");
|
|
||||||
CreateDelta(sourceFile.FullName, targetFile.FullName);
|
|
||||||
extension = ".delta";
|
|
||||||
deltaCount++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foundFilesQueue.Enqueue(sourceFile);
|
|
||||||
PatchLogger.LogInfo("::: File Exists :::");
|
|
||||||
existCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
filesProcessed++;
|
|
||||||
|
|
||||||
AdditionalInfo[0].ItemValue = deltaCount;
|
|
||||||
AdditionalInfo[1].ItemValue = newCount;
|
|
||||||
AdditionalInfo[3].ItemValue = existCount;
|
|
||||||
|
|
||||||
RaiseProgressChanged(filesProcessed, fileCountTotal,
|
|
||||||
$"{targetFile.FullName.Replace(TargetFolder, "...")}{extension}", AdditionalInfo.ToArray());
|
|
||||||
});
|
|
||||||
|
|
||||||
// remove all queued files that were found in the source files list
|
// remove all queued files that were found in the source files list
|
||||||
PatchLogger.LogInfo(":: Updating Source List ::");
|
PatchLogger.LogInfo(":: Updating Source List ::");
|
||||||
@ -456,106 +465,115 @@ namespace PatcherUtils
|
|||||||
fileCountTotal = deltaFiles.Count;
|
fileCountTotal = deltaFiles.Count;
|
||||||
var patchingTokenSource = new CancellationTokenSource();
|
var patchingTokenSource = new CancellationTokenSource();
|
||||||
|
|
||||||
// foreach (FileInfo deltaFile in deltaDir.GetFiles("*", SearchOption.AllDirectories))
|
try
|
||||||
Parallel.ForEach(deltaDir.GetFiles("*", SearchOption.AllDirectories).ToList(),
|
|
||||||
new ParallelOptions() { MaxDegreeOfParallelism = 5, CancellationToken = patchingTokenSource.Token},
|
|
||||||
deltaFile =>
|
|
||||||
{
|
{
|
||||||
switch (deltaFile.Extension)
|
|
||||||
{
|
|
||||||
case ".delta":
|
Parallel.ForEach(deltaDir.GetFiles("*", SearchOption.AllDirectories).ToList(),
|
||||||
|
new ParallelOptions() { MaxDegreeOfParallelism = 5, CancellationToken = patchingTokenSource.Token },
|
||||||
|
deltaFile =>
|
||||||
{
|
{
|
||||||
//apply delta
|
switch (deltaFile.Extension)
|
||||||
FileInfo sourceFile = SourceFiles.Find(f =>
|
|
||||||
f.FullName.Replace(sourceDir.FullName, "") == deltaFile.FullName
|
|
||||||
.Replace(deltaDir.FullName, "").Replace(".delta", ""));
|
|
||||||
|
|
||||||
if (sourceFile == null)
|
|
||||||
{
|
{
|
||||||
patchingTokenSource.Cancel();
|
case ".delta":
|
||||||
errorsQueue.Enqueue(new PatchMessage(
|
{
|
||||||
$"Failed to find matching source file for '{deltaFile.FullName}'",
|
//apply delta
|
||||||
PatcherExitCode.MissingFile));
|
FileInfo sourceFile = SourceFiles.Find(f =>
|
||||||
return;
|
f.FullName.Replace(sourceDir.FullName, "") == deltaFile.FullName
|
||||||
|
.Replace(deltaDir.FullName, "").Replace(".delta", ""));
|
||||||
|
|
||||||
|
if (sourceFile == null)
|
||||||
|
{
|
||||||
|
patchingTokenSource.Cancel();
|
||||||
|
errorsQueue.Enqueue(new PatchMessage(
|
||||||
|
$"Failed to find matching source file for '{deltaFile.FullName}'",
|
||||||
|
PatcherExitCode.MissingFile));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PatchLogger.LogInfo("::: Applying Delta :::");
|
||||||
|
var result = ApplyDelta(sourceFile.FullName, deltaFile.FullName);
|
||||||
|
|
||||||
|
if (!result.Item1)
|
||||||
|
{
|
||||||
|
patchingTokenSource.Cancel();
|
||||||
|
errorsQueue.Enqueue(new PatchMessage(result.Item2, PatcherExitCode.PatchFailed));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
deltaCount--;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ".new":
|
||||||
|
{
|
||||||
|
//copy new file
|
||||||
|
string destination = Path.Join(sourceDir.FullName,
|
||||||
|
deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".new", ""));
|
||||||
|
|
||||||
|
PatchLogger.LogInfo("::: Adding New File :::");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(destination));
|
||||||
|
File.Copy(deltaFile.FullName, destination, true);
|
||||||
|
PatchLogger.LogInfo($"File added: {destination}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
patchingTokenSource.Cancel();
|
||||||
|
PatchLogger.LogException(ex);
|
||||||
|
errorsQueue.Enqueue(new PatchMessage(ex.Message, PatcherExitCode.PatchFailed));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
newCount--;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ".del":
|
||||||
|
{
|
||||||
|
//remove unneeded file
|
||||||
|
string delFilePath = Path.Join(sourceDir.FullName,
|
||||||
|
deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".del", ""));
|
||||||
|
|
||||||
|
PatchLogger.LogInfo("::: Removing Uneeded File :::");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.Delete(delFilePath);
|
||||||
|
PatchLogger.LogInfo($"File removed: {delFilePath}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
patchingTokenSource.Cancel();
|
||||||
|
PatchLogger.LogException(ex);
|
||||||
|
errorsQueue.Enqueue(new PatchMessage(ex.Message, PatcherExitCode.PatchFailed));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delCount--;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PatchLogger.LogInfo("::: Applying Delta :::");
|
AdditionalInfo[0].ItemValue = deltaCount;
|
||||||
var result = ApplyDelta(sourceFile.FullName, deltaFile.FullName);
|
AdditionalInfo[1].ItemValue = newCount;
|
||||||
|
AdditionalInfo[2].ItemValue = delCount;
|
||||||
|
|
||||||
if (!result.Item1)
|
++filesProcessed;
|
||||||
{
|
RaiseProgressChanged(filesProcessed, fileCountTotal, deltaFile.Name, AdditionalInfo.ToArray());
|
||||||
patchingTokenSource.Cancel();
|
});
|
||||||
errorsQueue.Enqueue(new PatchMessage(result.Item2, PatcherExitCode.PatchFailed));
|
}
|
||||||
return;
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
|
PatchLogger.LogException(ex);
|
||||||
deltaCount--;
|
}
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case ".new":
|
|
||||||
{
|
|
||||||
//copy new file
|
|
||||||
string destination = Path.Join(sourceDir.FullName,
|
|
||||||
deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".new", ""));
|
|
||||||
|
|
||||||
PatchLogger.LogInfo("::: Adding New File :::");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(destination));
|
|
||||||
File.Copy(deltaFile.FullName, destination, true);
|
|
||||||
PatchLogger.LogInfo($"File added: {destination}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
patchingTokenSource.Cancel();
|
|
||||||
PatchLogger.LogException(ex);
|
|
||||||
errorsQueue.Enqueue(new PatchMessage(ex.Message, PatcherExitCode.PatchFailed));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
newCount--;
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case ".del":
|
|
||||||
{
|
|
||||||
//remove unneeded file
|
|
||||||
string delFilePath = Path.Join(sourceDir.FullName,
|
|
||||||
deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".del", ""));
|
|
||||||
|
|
||||||
PatchLogger.LogInfo("::: Removing Uneeded File :::");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Delete(delFilePath);
|
|
||||||
PatchLogger.LogInfo($"File removed: {delFilePath}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
patchingTokenSource.Cancel();
|
|
||||||
PatchLogger.LogException(ex);
|
|
||||||
errorsQueue.Enqueue(new PatchMessage(ex.Message, PatcherExitCode.PatchFailed));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
delCount--;
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AdditionalInfo[0].ItemValue = deltaCount;
|
|
||||||
AdditionalInfo[1].ItemValue = newCount;
|
|
||||||
AdditionalInfo[2].ItemValue = delCount;
|
|
||||||
|
|
||||||
++filesProcessed;
|
|
||||||
RaiseProgressChanged(filesProcessed, fileCountTotal, deltaFile.Name, AdditionalInfo.ToArray());
|
|
||||||
});
|
|
||||||
|
|
||||||
if (errorsQueue.Count > 0)
|
if (errorsQueue.Count > 0)
|
||||||
{
|
{
|
||||||
|
PatchLogger.LogError($"Error queue entry count: {errorsQueue.Count}");
|
||||||
if (!errorsQueue.TryDequeue(out PatchMessage error))
|
if (!errorsQueue.TryDequeue(out PatchMessage error))
|
||||||
{
|
{
|
||||||
return new PatchMessage("Errors occurred during patching, but we couldn't dequeue them :(\n\nThere may be more information in the log", PatcherExitCode.PatchFailed);
|
return new PatchMessage("Errors occurred during patching, but we couldn't dequeue them :(\n\nThere may be more information in the log", PatcherExitCode.PatchFailed);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user