Compare commits

..

15 Commits
2.12.0 ... main

Author SHA1 Message Date
fc6b574f47 Merge pull request 'update logging' (#22) from impl/better-logging into main
Reviewed-on: #22
2024-07-09 22:27:01 +00:00
067410e3a2 update logging 2024-07-09 18:25:23 -04:00
291ff54101 update patchclient resource 2024-06-25 18:42:33 -04:00
e2c1e9831d Merge pull request 'Switch patch apply to use XDeltaSharp' (#21) from DrakiaXYZ/Patcher:feat-xdeltasharp into main
Reviewed-on: #21
2024-06-25 12:54:58 +00:00
e5f6c681de Merge pull request 'Improve patch creation performance' (#20) from DrakiaXYZ/Patcher:perf-nocompression into main
Reviewed-on: #20

Slightly larger space should be fine, since the installer adds about 10Gb to the space check for misc files (patcher, release, metadata)

LGTM!
2024-06-25 12:54:20 +00:00
DrakiaXYZ
a22fccd42d Switch patch apply to use XDeltaSharp
- This resolves the persistent `-1073741819` error some users receive during patching
- XDeltaSharp doesn't support creating deltas, so patch creation still depends on xdelta3.exe
2024-06-24 21:36:06 -07:00
DrakiaXYZ
38f2425aeb Improve patch creation performance
- Skip MD5 hash generation if the file size differs, as it's guaranteed to be different
- Disable xdelta compression, LZMA does a better job and quicker
2024-06-24 21:09:40 -07:00
c49c076ea7 Merge pull request 'update patch files dir name' (#19) from slugma into main
Reviewed-on: #19
2024-05-24 00:20:36 +00:00
a8e8b1d221 update patch files dir name 2024-05-23 20:07:47 -04:00
21f4a58cc7 raise patching timeout to 10mins 2024-05-20 09:23:05 -04:00
66f1832c21 Merge pull request 'try-catch parallel operations' (#18) from fix/change-ui-on-fail into main
Reviewed-on: #18
2024-05-18 16:49:38 +00:00
32d910473b try-catch parallel operations 2024-05-18 12:46:44 -04:00
da36ea3438 Merge pull request 'impr/parallel-patching' (#17) from impr/parallel-patching into main
Reviewed-on: #17
2024-05-04 14:57:27 +00:00
24fe16177a finish adding parallel processing 2024-05-04 10:56:49 -04:00
5e8293fcbc stuff blah 2024-05-03 14:57:08 -04:00
10 changed files with 345 additions and 240 deletions

View File

@ -4,8 +4,8 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AssemblyVersion>2.12.0</AssemblyVersion> <AssemblyVersion>2.15.4</AssemblyVersion>
<FileVersion>2.12.0</FileVersion> <FileVersion>2.15.4</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<AvaloniaResource Include="Assets\**" /> <AvaloniaResource Include="Assets\**" />

View File

@ -4,8 +4,8 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AssemblyVersion>2.12.0</AssemblyVersion> <AssemblyVersion>2.15.4</AssemblyVersion>
<FileVersion>2.12.0</FileVersion> <FileVersion>2.15.4</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -23,7 +23,6 @@ namespace PatchGenerator.ViewModels
{ {
this.WhenActivated((CompositeDisposable disposables) => this.WhenActivated((CompositeDisposable disposables) =>
{ {
if (genArgs != null && genArgs.ReadyToRun) if (genArgs != null && genArgs.ReadyToRun)
{ {
PatchGenInfo genInfo = new PatchGenInfo(); PatchGenInfo genInfo = new PatchGenInfo();
@ -31,7 +30,8 @@ namespace PatchGenerator.ViewModels
genInfo.TargetFolderPath = genArgs.TargetFolderPath; genInfo.TargetFolderPath = genArgs.TargetFolderPath;
genInfo.SourceFolderPath = genArgs.SourceFolderPath; genInfo.SourceFolderPath = genArgs.SourceFolderPath;
genInfo.PatchName = genArgs.OutputFolderName; genInfo.PatchName = genArgs.OutputFolderName;
genInfo.AutoZip = genArgs.AutoZip; // issues with auto zip, but it's not really used anymore so just disabling for now
genInfo.AutoZip = false;
genInfo.AutoClose = genArgs.AutoClose; genInfo.AutoClose = genArgs.AutoClose;
Router.Navigate.Execute(new PatchGenerationViewModel(this, genInfo)); Router.Navigate.Execute(new PatchGenerationViewModel(this, genInfo));

View File

@ -14,9 +14,11 @@ using System.IO;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
using PatcherUtils.Model; using PatcherUtils.Model;
using Timer = System.Timers.Timer;
namespace PatchGenerator.ViewModels namespace PatchGenerator.ViewModels
{ {
@ -113,14 +115,16 @@ namespace PatchGenerator.ViewModels
var message = patcher.GeneratePatches(); var message = patcher.GeneratePatches();
if(message.ExitCode != PatcherExitCode.Success && generationInfo.AutoClose)
{
Environment.Exit((int)message.ExitCode);
}
patchGenStopwatch.Stop(); patchGenStopwatch.Stop();
updateElapsedTimeTimer.Stop(); updateElapsedTimeTimer.Stop();
if(message.ExitCode != PatcherExitCode.Success && generationInfo.AutoClose)
{
PatchLogger.LogInfo("Exiting: Auto close on failure");
Environment.Exit((int)message.ExitCode);
}
PatchLogger.LogInfo("Printing summary info ...");
PrintSummary(); PrintSummary();
StringBuilder sb = new StringBuilder() StringBuilder sb = new StringBuilder()
@ -133,29 +137,32 @@ namespace PatchGenerator.ViewModels
File.Copy(LazyOperations.PatcherClientPath, $"{generationInfo.PatchName.FromCwd()}\\patcher.exe", true); File.Copy(LazyOperations.PatcherClientPath, $"{generationInfo.PatchName.FromCwd()}\\patcher.exe", true);
if (generationInfo.AutoZip) PatchLogger.LogInfo("Copied patcher.exe to output folder");
{
IndeterminateProgress = true;
PatchItemCollection.Add(new PatchItem("Allowing Time for files to unlock ...")); // if (generationInfo.AutoZip)
// {
System.Threading.Thread.Sleep(2000); // PatchLogger.LogInfo("AutoZipping");
// IndeterminateProgress = true;
PatchItemCollection.Add(new PatchItem("Zipping patcher ...")); //
// PatchItemCollection.Add(new PatchItem("Allowing Time for files to unlock ..."));
ProgressMessage = "Zipping patcher"; //
// Thread.Sleep(2000);
IndeterminateProgress = false; //
// PatchItemCollection.Add(new PatchItem("Zipping patcher ..."));
var progress = new Progress<int>(p => //
{ // ProgressMessage = "Zipping patcher";
PatchPercent = p; //
}); // IndeterminateProgress = false;
//
LazyOperations.CompressDirectory(generationInfo.PatchName.FromCwd(), $"{generationInfo.PatchName}.7z".FromCwd(), progress); // var progress = new Progress<int>(p =>
// {
PatchItemCollection.Add(new PatchItem("Done")); // PatchPercent = p;
} // });
//
// LazyOperations.CompressDirectory(generationInfo.PatchName.FromCwd(), $"{generationInfo.PatchName}.7z".FromCwd(), progress);
//
// PatchItemCollection.Add(new PatchItem("Done"));
// }
if (generationInfo.AutoClose) if (generationInfo.AutoClose)
{ {

View File

@ -16,8 +16,9 @@
Watermark="Output Folder Name" Watermark="Output Folder Name"
/> />
<CheckBox Content="Zip Generated Files" Grid.Row="4" <!-- UNRESOLVED ISSUES: disabling for now -->
IsChecked="{Binding GenerationInfo.AutoZip}"/> <!-- <CheckBox Content="Zip Generated Files" Grid.Row="4" -->
<!-- IsChecked="{Binding GenerationInfo.AutoZip}"/> -->
<Button Content="Generate Patches" Grid.ColumnSpan="3" Grid.Row="4" <Button Content="Generate Patches" Grid.ColumnSpan="3" Grid.Row="4"
HorizontalAlignment="Right" HorizontalAlignment="Right"

View File

@ -9,7 +9,7 @@ namespace PatcherUtils.Helpers;
public class XdeltaProcessHelper public class XdeltaProcessHelper
{ {
private readonly int _timeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds; private readonly int _timeout = (int)TimeSpan.FromMinutes(10).TotalMilliseconds;
private string _args; private string _args;
private string _sourcePath; private string _sourcePath;
private string _deltaPath; private string _deltaPath;

View File

@ -2,7 +2,6 @@
using PatcherUtils.Model; using PatcherUtils.Model;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using Aki.Common.Utils;
using SevenZip; using SevenZip;
namespace PatcherUtils namespace PatcherUtils
@ -18,7 +17,7 @@ namespace PatcherUtils
/// <summary> /// <summary>
/// The folder that the patches will be stored in /// The folder that the patches will be stored in
/// </summary> /// </summary>
public static string PatchFolder = "Aki_Patches"; public static string PatchFolder = "SPT_Patches";
private static string SevenZDll = "7z.dll"; private static string SevenZDll = "7z.dll";
@ -102,31 +101,44 @@ namespace PatcherUtils
public static void CompressDirectory(string SourceDirectoryPath, string DestinationFilePath, IProgress<int> progress) public static void CompressDirectory(string SourceDirectoryPath, string DestinationFilePath, IProgress<int> progress)
{ {
var outputFile = new FileInfo(DestinationFilePath); try
SevenZipBase.SetLibraryPath(SevenZDllPath);
var compressor = new SevenZipCompressor()
{ {
ArchiveFormat = OutArchiveFormat.SevenZip, PatchLogger.LogInfo($"Compressing: {SourceDirectoryPath}");
CompressionMethod = CompressionMethod.Lzma2, PatchLogger.LogInfo($"Output file: {DestinationFilePath}");
CompressionLevel = CompressionLevel.Normal var outputFile = new FileInfo(DestinationFilePath);
}; SevenZipBase.SetLibraryPath(SevenZDllPath);
compressor.Compressing += (_, args) => PatchLogger.LogInfo($"7z.dll set: {SevenZDllPath}");
var compressor = new SevenZipCompressor()
{
ArchiveFormat = OutArchiveFormat.SevenZip,
CompressionMethod = CompressionMethod.Lzma2,
CompressionLevel = CompressionLevel.Normal,
PreserveDirectoryRoot = true
};
compressor.Compressing += (_, args) => { progress.Report(args.PercentDone); };
using var outputStream = outputFile.OpenWrite();
PatchLogger.LogInfo("Starting compression");
compressor.CompressDirectory(SourceDirectoryPath, outputStream);
PatchLogger.LogInfo("Compression complete");
outputFile.Refresh();
// failed to compress data
if (!outputFile.Exists || outputFile.Length == 0)
{
PatchLogger.LogError("Failed to compress patcher");
}
}
catch (Exception ex)
{ {
progress.Report(args.PercentDone); PatchLogger.LogException(ex);
};
using var outputStream = outputFile.OpenWrite();
compressor.CompressDirectory(SourceDirectoryPath, outputStream);
outputFile.Refresh();
// failed to compress data
if (!outputFile.Exists || outputFile.Length == 0)
{
Logger.LogError("Failed to compress patcher");
} }
} }
@ -140,7 +152,7 @@ namespace PatcherUtils
if (dir.Exists) if (dir.Exists)
{ {
dir.Delete(true); dir.Delete(true);
PatchLogger.LogInfo("Temp directory delted"); PatchLogger.LogInfo("Temp directory deleted");
} }
} }
} }

View File

@ -1,12 +1,15 @@
using PatchClient.Models; using PatchClient.Models;
using PatcherUtils.Model; using PatcherUtils.Model;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using PatcherUtils.Helpers; using System.Threading;
using System.Threading.Tasks;
using PleOps.XdeltaSharp.Decoder;
namespace PatcherUtils namespace PatcherUtils
{ {
@ -34,7 +37,8 @@ namespace PatcherUtils
/// <remarks>Includes an array of <see cref="LineItem"/> with details for each type of patch</remarks> /// <remarks>Includes an array of <see cref="LineItem"/> with details for each type of patch</remarks>
public event ProgressChangedHandler ProgressChanged; public event ProgressChangedHandler ProgressChanged;
protected virtual void RaiseProgressChanged(int progress, int total, string Message = "", params LineItem[] AdditionalLineItems) protected virtual void RaiseProgressChanged(int progress, int total, string Message = "",
params LineItem[] AdditionalLineItems)
{ {
int percent = (int)Math.Floor((double)progress / total * 100); int percent = (int)Math.Floor((double)progress / total * 100);
@ -79,6 +83,12 @@ namespace PatcherUtils
var sourceInfo = new FileInfo(SourceFilePath); var sourceInfo = new FileInfo(SourceFilePath);
var targetInfo = new FileInfo(TargetFilePath); var targetInfo = new FileInfo(TargetFilePath);
// Return false if file size differs
if (sourceInfo.Length != targetInfo.Length)
{
return false;
}
using (MD5 md5Service = MD5.Create()) using (MD5 md5Service = MD5.Create())
using (var sourceStream = File.OpenRead(SourceFilePath)) using (var sourceStream = File.OpenRead(SourceFilePath))
using (var targetStream = File.OpenRead(TargetFilePath)) using (var targetStream = File.OpenRead(TargetFilePath))
@ -88,7 +98,8 @@ namespace PatcherUtils
bool matched = Enumerable.SequenceEqual(sourceHash, targetHash); bool matched = Enumerable.SequenceEqual(sourceHash, targetHash);
PatchLogger.LogInfo($"Hash Check: S({sourceInfo.Name}|{Convert.ToBase64String(sourceHash)}) - T({targetInfo.Name}|{Convert.ToBase64String(targetHash)}) - Match:{matched}"); PatchLogger.LogInfo(
$"Hash Check: S({sourceInfo.Name}|{Convert.ToBase64String(sourceHash)}) - T({targetInfo.Name}|{Convert.ToBase64String(targetHash)}) - Match:{matched}");
return matched; return matched;
} }
@ -103,37 +114,30 @@ namespace PatcherUtils
{ {
string decodedPath = SourceFilePath + ".decoded"; string decodedPath = SourceFilePath + ".decoded";
var xdeltaArgs = $"-d {(debugOutput ? "-v -v" : "")} -f -s"; try
var xdeltaHelper =
new XdeltaProcessHelper(xdeltaArgs, SourceFilePath, DeltaFilePath, decodedPath, debugOutput);
if (!xdeltaHelper.Run())
{ {
return (false, "something went wrong during the xdelta3 process"); using var inputFile = new FileStream(SourceFilePath, FileMode.Open);
using var patchFile = new FileStream(DeltaFilePath, FileMode.Open);
using var decodedFile = new FileStream(decodedPath, FileMode.Create);
using var decoder = new Decoder(inputFile, patchFile, decodedFile);
decoder.Run();
}
catch (Exception ex)
{
PatchLogger.LogException(ex);
return (false, ex.Message);
} }
if (File.Exists(decodedPath)) try
{ {
PatchLogger.LogInfo($"File delta decoded: {SourceFilePath}"); File.Move(decodedPath, SourceFilePath, true);
PatchLogger.LogInfo($"Delta applied: {DeltaFilePath}");
try return (true, "");
{
File.Move(decodedPath, SourceFilePath, true);
PatchLogger.LogInfo($"Delta applied: {DeltaFilePath}");
return (true, "");
}
catch (Exception ex)
{
PatchLogger.LogException(ex);
return (false, ex.Message);
}
} }
else catch (Exception ex)
{ {
string error = $"Failed to decode file delta: {SourceFilePath}"; PatchLogger.LogException(ex);
PatchLogger.LogError(error); return (false, ex.Message);
return (false, error);
} }
} }
@ -153,18 +157,18 @@ namespace PatcherUtils
{ {
Directory.CreateDirectory(deltaPath.Replace(sourceFileInfo.Name + ".delta", "")); Directory.CreateDirectory(deltaPath.Replace(sourceFileInfo.Name + ".delta", ""));
} }
catch(Exception ex) catch (Exception ex)
{ {
PatchLogger.LogException(ex); PatchLogger.LogException(ex);
} }
Process.Start(new ProcessStartInfo Process.Start(new ProcessStartInfo
{ {
FileName = LazyOperations.XDelta3Path, FileName = LazyOperations.XDelta3Path,
Arguments = $"-0 -e -f -s \"{SourceFilePath}\" \"{TargetFilePath}\" \"{deltaPath}\"", Arguments = $"-0 -e -f -S none -s \"{SourceFilePath}\" \"{TargetFilePath}\" \"{deltaPath}\"",
CreateNoWindow = true CreateNoWindow = true
}) })
.WaitForExit(); .WaitForExit();
if (File.Exists(deltaPath)) if (File.Exists(deltaPath))
{ {
@ -191,7 +195,7 @@ namespace PatcherUtils
{ {
Directory.CreateDirectory(deltaPath.Replace(sourceFileInfo.Name + ".del", "")); Directory.CreateDirectory(deltaPath.Replace(sourceFileInfo.Name + ".del", ""));
} }
catch(Exception ex) catch (Exception ex)
{ {
PatchLogger.LogException(ex); PatchLogger.LogException(ex);
} }
@ -201,7 +205,7 @@ namespace PatcherUtils
File.Create(deltaPath); File.Create(deltaPath);
PatchLogger.LogInfo($"File Created [DEL]: {deltaPath}"); PatchLogger.LogInfo($"File Created [DEL]: {deltaPath}");
} }
catch(Exception ex) catch (Exception ex)
{ {
PatchLogger.LogException(ex); PatchLogger.LogException(ex);
} }
@ -222,7 +226,7 @@ namespace PatcherUtils
{ {
Directory.CreateDirectory(deltaPath.Replace(targetSourceInfo.Name + ".new", "")); Directory.CreateDirectory(deltaPath.Replace(targetSourceInfo.Name + ".new", ""));
} }
catch(Exception ex) catch (Exception ex)
{ {
PatchLogger.LogException(ex); PatchLogger.LogException(ex);
} }
@ -232,7 +236,7 @@ namespace PatcherUtils
targetSourceInfo.CopyTo(deltaPath, true); targetSourceInfo.CopyTo(deltaPath, true);
PatchLogger.LogInfo($"File Created [NEW]: {deltaPath}"); PatchLogger.LogInfo($"File Created [NEW]: {deltaPath}");
} }
catch(Exception ex) catch (Exception ex)
{ {
PatchLogger.LogException(ex); PatchLogger.LogException(ex);
} }
@ -275,9 +279,11 @@ namespace PatcherUtils
LazyOperations.ExtractResourcesToTempDir(); LazyOperations.ExtractResourcesToTempDir();
List<FileInfo> SourceFiles = sourceDir.GetFiles("*", SearchOption.AllDirectories).ToList(); List<FileInfo> sourceFiles = sourceDir.GetFiles("*", SearchOption.AllDirectories).ToList();
var targetFiles = targetDir.GetFiles("*", SearchOption.AllDirectories).ToList();
ConcurrentQueue<FileInfo> foundFilesQueue = new ConcurrentQueue<FileInfo>();
fileCountTotal = SourceFiles.Count; fileCountTotal = targetFiles.Count;
PatchLogger.LogInfo($"Total source files: {fileCountTotal}"); PatchLogger.LogInfo($"Total source files: {fileCountTotal}");
@ -291,74 +297,101 @@ namespace PatcherUtils
RaiseProgressChanged(0, fileCountTotal, "Generating deltas..."); RaiseProgressChanged(0, fileCountTotal, "Generating deltas...");
foreach (FileInfo targetFile in targetDir.GetFiles("*", SearchOption.AllDirectories)) try
{ {
//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 =>
{
//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)
{
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++;
}
filesProcessed++;
AdditionalInfo[0].ItemValue = deltaCount;
AdditionalInfo[1].ItemValue = newCount;
AdditionalInfo[3].ItemValue = existCount;
RaiseProgressChanged(filesProcessed, fileCountTotal,
$"{targetFile.FullName.Replace(TargetFolder, "...")}{extension}", AdditionalInfo.ToArray());
});
}
catch (Exception ex)
{
PatchLogger.LogException(ex);
}
// remove all queued files that were found in the source files list
PatchLogger.LogInfo(":: Updating Source List ::");
try
{
int processedQueueCount = 0;
int queueTotal = foundFilesQueue.Count;
foreach (var queuedFile in foundFilesQueue)
{ {
PatchLogger.LogInfo("::: Creating .new file :::"); RaiseProgressChanged(processedQueueCount, queueTotal, $"Queued file removed: {queuedFile.Name}",
CreateNewFile(targetFile.FullName); AdditionalInfo.ToArray());
sourceFiles.Remove(queuedFile);
newCount++;
filesProcessed++;
RaiseProgressChanged(filesProcessed, fileCountTotal, $"{targetFile.FullName.Replace(TargetFolder, "...")}.new", AdditionalInfo.ToArray());
continue;
} }
}
string extension = ""; catch (Exception ex)
{
//if a matching source file was found, check the file hashes and get the delta. PatchLogger.LogException(ex);
if (!CompareFileHashes(sourceFile.FullName, targetFile.FullName))
{
PatchLogger.LogInfo("::: Creating .delta file :::");
CreateDelta(sourceFile.FullName, targetFile.FullName);
extension = ".delta";
deltaCount++;
}
else
{
PatchLogger.LogInfo("::: File Exists :::");
existCount++;
}
try
{
SourceFiles.Remove(sourceFile);
}
catch(Exception ex)
{
PatchLogger.LogException(ex);
}
filesProcessed++;
AdditionalInfo[0].ItemValue = deltaCount;
AdditionalInfo[1].ItemValue = newCount;
AdditionalInfo[3].ItemValue = existCount;
RaiseProgressChanged(filesProcessed, fileCountTotal, $"{targetFile.FullName.Replace(TargetFolder, "...")}{extension}", AdditionalInfo.ToArray());
} }
//Any remaining source files do not exist in the target folder and can be removed. //Any remaining source files do not exist in the target folder and can be removed.
//reset progress info //reset progress info
if (SourceFiles.Count == 0) if (sourceFiles.Count == 0)
{ {
PatchLogger.LogInfo("::: Patch Generation Complete :::"); PatchLogger.LogInfo("::: Patch Generation Complete :::");
return new PatchMessage("Generation Done", PatcherExitCode.Success); return new PatchMessage("Generation Done", PatcherExitCode.Success);
} }
RaiseProgressChanged(0, SourceFiles.Count, "Processing .del files..."); RaiseProgressChanged(0, sourceFiles.Count, "Processing .del files...");
filesProcessed = 0; filesProcessed = 0;
fileCountTotal = SourceFiles.Count; fileCountTotal = sourceFiles.Count;
foreach (FileInfo delFile in SourceFiles) foreach (FileInfo delFile in sourceFiles)
{ {
PatchLogger.LogInfo("::: Creating .del file :::"); PatchLogger.LogInfo("::: Creating .del file :::");
CreateDelFile(delFile.FullName); CreateDelFile(delFile.FullName);
@ -368,7 +401,8 @@ namespace PatcherUtils
AdditionalInfo[2].ItemValue = delCount; AdditionalInfo[2].ItemValue = delCount;
filesProcessed++; filesProcessed++;
RaiseProgressChanged(filesProcessed, fileCountTotal, $"{delFile.FullName.Replace(SourceFolder, "...")}.del", AdditionalInfo.ToArray()); RaiseProgressChanged(filesProcessed, fileCountTotal,
$"{delFile.FullName.Replace(SourceFolder, "...")}.del", AdditionalInfo.ToArray());
} }
PatchLogger.LogInfo("::: Patch Generation Complete :::"); PatchLogger.LogInfo("::: Patch Generation Complete :::");
@ -398,7 +432,7 @@ namespace PatcherUtils
return new PatchMessage(message, PatcherExitCode.MissingDir); return new PatchMessage(message, PatcherExitCode.MissingDir);
} }
if(!deltaDir.Exists) if (!deltaDir.Exists)
{ {
string message = $"Could not find delta directory: {deltaDir.FullName}"; string message = $"Could not find delta directory: {deltaDir.FullName}";
PatchLogger.LogError(message); PatchLogger.LogError(message);
@ -424,93 +458,143 @@ namespace PatcherUtils
new LineItem("Files to Delete", delCount) new LineItem("Files to Delete", delCount)
}; };
ConcurrentQueue<PatchMessage> errorsQueue = new ConcurrentQueue<PatchMessage>();
filesProcessed = 0; filesProcessed = 0;
fileCountTotal = deltaFiles.Count; fileCountTotal = deltaFiles.Count;
var patchingTokenSource = new CancellationTokenSource();
foreach (FileInfo deltaFile in deltaDir.GetFiles("*", SearchOption.AllDirectories)) try
{ {
switch (deltaFile.Extension)
Parallel.ForEach(deltaDir.GetFiles("*", SearchOption.AllDirectories).ToList(),
new ParallelOptions() { MaxDegreeOfParallelism = 5, CancellationToken = patchingTokenSource.Token },
deltaFile =>
{
switch (deltaFile.Extension)
{
case ".delta":
{
//apply delta
FileInfo sourceFile = SourceFiles.Find(f =>
f.FullName.Replace(sourceDir.FullName, "") == deltaFile.FullName
.Replace(deltaDir.FullName, "").Replace(".delta", ""));
if (sourceFile == null)
{
errorsQueue.Enqueue(new PatchMessage(
$"Failed to find matching source file for '{deltaFile.FullName}'",
PatcherExitCode.MissingFile));
patchingTokenSource.Cancel();
return;
}
PatchLogger.LogInfo("::: Applying Delta :::");
var result = ApplyDelta(sourceFile.FullName, deltaFile.FullName);
if (!result.Item1)
{
errorsQueue.Enqueue(new PatchMessage(result.Item2, PatcherExitCode.PatchFailed));
patchingTokenSource.Cancel();
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)
{
PatchLogger.LogException(ex);
errorsQueue.Enqueue(new PatchMessage(ex.Message, PatcherExitCode.PatchFailed));
patchingTokenSource.Cancel();
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)
{
PatchLogger.LogException(ex);
errorsQueue.Enqueue(new PatchMessage(ex.Message, PatcherExitCode.PatchFailed));
patchingTokenSource.Cancel();
return;
}
delCount--;
break;
}
}
AdditionalInfo[0].ItemValue = deltaCount;
AdditionalInfo[1].ItemValue = newCount;
AdditionalInfo[2].ItemValue = delCount;
++filesProcessed;
RaiseProgressChanged(filesProcessed, fileCountTotal, deltaFile.Name, AdditionalInfo.ToArray());
});
}
catch (Exception ex)
{
PatchLogger.LogException(ex);
}
if (errorsQueue.Count > 0)
{
PatchLogger.LogError($"Error queue entry count: {errorsQueue.Count}");
PatchLogger.LogError("Dequeuing errors");
PatchMessage error = null;
for (int i = 0; i < errorsQueue.Count; i++)
{ {
case ".delta": if (!errorsQueue.TryDequeue(out error))
{ {
//apply delta return new PatchMessage(
FileInfo sourceFile = SourceFiles.Find(f => f.FullName.Replace(sourceDir.FullName, "") == deltaFile.FullName.Replace(deltaDir.FullName, "").Replace(".delta", "")); "Errors occurred during patching, but we couldn't dequeue them :(\n\nThere may be more information in the log",
PatcherExitCode.PatchFailed);
}
if (sourceFile == null) PatchLogger.LogError(error.Message);
{
return new PatchMessage($"Failed to find matching source file for '{deltaFile.FullName}'", PatcherExitCode.MissingFile);
}
PatchLogger.LogInfo("::: Applying Delta :::");
var result = ApplyDelta(sourceFile.FullName, deltaFile.FullName);
if(!result.Item1)
{
return new PatchMessage(result.Item2, PatcherExitCode.PatchFailed);
}
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)
{
PatchLogger.LogException(ex);
return new PatchMessage(ex.Message, PatcherExitCode.PatchFailed);
}
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)
{
PatchLogger.LogException(ex);
return new PatchMessage(ex.Message, PatcherExitCode.PatchFailed);
}
delCount--;
break;
}
} }
AdditionalInfo[0].ItemValue = deltaCount; return error ?? new PatchMessage("Something went wrong :(", PatcherExitCode.PatchFailed);
AdditionalInfo[1].ItemValue = newCount;
AdditionalInfo[2].ItemValue = delCount;
++filesProcessed;
RaiseProgressChanged(filesProcessed, fileCountTotal, deltaFile.Name, AdditionalInfo.ToArray());
} }
PatchLogger.LogInfo("::: Patching Complete :::"); PatchLogger.LogInfo("::: Patching Complete :::");
return new PatchMessage($"Patching Complete. You can delete the patcher.exe file.", PatcherExitCode.Success); return new PatchMessage($"Patching Complete. You can delete the patcher.exe file.",
PatcherExitCode.Success);
} }
} }
} }

View File

@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<AssemblyVersion>2.11.0</AssemblyVersion> <AssemblyVersion>2.15.3</AssemblyVersion>
<FileVersion>2.11.0</FileVersion> <FileVersion>2.15.3</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -22,7 +22,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.6.1.23" /> <PackageReference Include="PleOps.XdeltaSharp" Version="1.3.0" />
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.6.2.24" />
</ItemGroup> </ItemGroup>
</Project> </Project>