From aa5bfb3b4022e9af60cd0baac94c5b367d9f4f9b Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Wed, 15 Nov 2023 19:44:29 -0500 Subject: [PATCH 1/3] fix process buffer read, fix wrong path in access check --- .../.idea/.idea.Patcher/.idea/avalonia.xml | 11 ++ .../Helpers/XdeltaProcessHelper.cs | 151 ++++++++++++++++++ Patcher/PatcherUtils/PatchHelper.cs | 70 +------- 3 files changed, 168 insertions(+), 64 deletions(-) create mode 100644 Patcher/.idea/.idea.Patcher/.idea/avalonia.xml create mode 100644 Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs diff --git a/Patcher/.idea/.idea.Patcher/.idea/avalonia.xml b/Patcher/.idea/.idea.Patcher/.idea/avalonia.xml new file mode 100644 index 0000000..49f50b2 --- /dev/null +++ b/Patcher/.idea/.idea.Patcher/.idea/avalonia.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs b/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs new file mode 100644 index 0000000..cd4af72 --- /dev/null +++ b/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs @@ -0,0 +1,151 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading; +using PatcherUtils.Model; + +namespace PatcherUtils.Helpers; + +public class XdeltaProcessHelper +{ + private readonly int _timeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds; + private string _args; + private string _sourcePath; + private string _deltaPath; + private string _decodedPath; + private bool _isDebug; + + public XdeltaProcessHelper(string args, string sourcePath, string deltaPath, string decodedPath, bool isDebug) + { + _args = args; + _sourcePath = sourcePath; + _deltaPath = deltaPath; + _decodedPath = decodedPath; + _isDebug = isDebug; + } + + public bool Run() => _isDebug ? RunDebug() : RunNormal(); + + private bool RunNormal() + { + using var proc = new Process(); + + proc.StartInfo = new ProcessStartInfo + { + FileName = LazyOperations.XDelta3Path, + Arguments = $"{_args} \"{_sourcePath}\" \"{_deltaPath}\" \"{_decodedPath}\"", + CreateNoWindow = true + }; + + if (proc.WaitForExit(_timeout)) + { + PatchLogger.LogError("xdelta3 process timed out"); + PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); + return false; + } + + PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); + return true; + } + + private bool DebugPathsCheck() + { + try + { + var stream = File.Open(_sourcePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + stream.Close(); + stream.Dispose(); + PatchLogger.LogDebug($"File is openable: {_sourcePath}"); + } + catch (Exception ex) + { + PatchLogger.LogException(ex); + return false; + } + + try + { + var stream = File.Open(_deltaPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + stream.Close(); + stream.Dispose(); + PatchLogger.LogDebug($"File is openable: {_deltaPath}"); + } + catch (Exception ex) + { + PatchLogger.LogException(ex); + return false; + } + + return true; + } + + private bool RunDebug() + { + if (!DebugPathsCheck()) + { + return false; + } + + using var proc = new Process(); + + proc.StartInfo = new ProcessStartInfo + { + FileName = LazyOperations.XDelta3Path, + Arguments = $"{_args} \"{_sourcePath}\" \"{_deltaPath}\" \"{_decodedPath}\"", + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + var outputBuilder = new StringBuilder(); + var errorBuilder = new StringBuilder(); + + using AutoResetEvent outputWaitHandle = new AutoResetEvent(false); + using AutoResetEvent errorWaitHandle = new AutoResetEvent(false); + + proc.OutputDataReceived += (s, e) => + { + if (e.Data == null) + { + outputWaitHandle.Set(); + } + else + { + outputBuilder.AppendLine(e.Data); + } + }; + + proc.ErrorDataReceived += (s, e) => + { + if (e.Data == null) + { + errorWaitHandle.Set(); + } + else + { + errorBuilder.AppendLine(e.Data); + } + }; + + proc.Start(); + + proc.BeginOutputReadLine(); + proc.BeginErrorReadLine(); + + if (!proc.WaitForExit(_timeout) || !outputWaitHandle.WaitOne(_timeout) || !errorWaitHandle.WaitOne(_timeout)) + { + PatchLogger.LogError("xdelta3 process timed out"); + PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); + return false; + } + + PatchLogger.LogDebug("__xdelta stdout__"); + PatchLogger.LogDebug(outputBuilder.ToString()); + PatchLogger.LogDebug("__xdelta stderr__"); + PatchLogger.LogDebug(errorBuilder.ToString()); + PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); + + return true; + } +} \ No newline at end of file diff --git a/Patcher/PatcherUtils/PatchHelper.cs b/Patcher/PatcherUtils/PatchHelper.cs index 76682ac..6710e21 100644 --- a/Patcher/PatcherUtils/PatchHelper.cs +++ b/Patcher/PatcherUtils/PatchHelper.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Security.Cryptography; +using PatcherUtils.Helpers; namespace PatcherUtils { @@ -104,71 +105,12 @@ namespace PatcherUtils var xdeltaArgs = $"-d {(debugOutput ? "-v -v" : "")} -f -s"; - if (debugOutput) + var xdeltaHelper = + new XdeltaProcessHelper(xdeltaArgs, SourceFilePath, DeltaFilePath, decodedPath, debugOutput); + + if (!xdeltaHelper.Run()) { - try - { - var stream = File.Open(SourceFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); - stream.Close(); - stream.Dispose(); - PatchLogger.LogDebug($"File is openable: {SourceFilePath}"); - } - catch (Exception ex) - { - PatchLogger.LogException(ex); - } - - try - { - var stream = File.Open(SourceFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); - stream.Close(); - stream.Dispose(); - PatchLogger.LogDebug($"File is openable: {DeltaFilePath}"); - } - catch (Exception ex) - { - PatchLogger.LogException(ex); - } - } - - var proc = Process.Start(new ProcessStartInfo - { - FileName = LazyOperations.XDelta3Path, - Arguments = $"{xdeltaArgs} \"{SourceFilePath}\" \"{DeltaFilePath}\" \"{decodedPath}\"", - RedirectStandardError = true, - RedirectStandardOutput = true, - CreateNoWindow = true - }); - - if (proc == null) - { - PatchLogger.LogError($"xdelta3 process failed to start: {nameof(proc)} is null"); - return (false, "xdelta3 process failed to start"); - } - - proc.WaitForExit(); - - if (debugOutput) - { - try - { - PatchLogger.LogDebug($"xdelta exit code :: {proc.ExitCode}"); - PatchLogger.LogDebug("___Dumping xdelta stdout___"); - while (!proc.StandardOutput.EndOfStream) - { - PatchLogger.LogDebug(proc.StandardOutput.ReadLine()); - } - - PatchLogger.LogDebug("___Dumping xdelta stderr___"); - while (!proc.StandardError.EndOfStream) - { - PatchLogger.LogDebug(proc.StandardError.ReadLine()); - } - } - catch (Exception ex) - { - PatchLogger.LogException(ex); - } + return (false, "something went wrong during the xdelta3 process"); } if (File.Exists(decodedPath)) From a3d9bf079917a9f6ab5173ff0804c27a08287154 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Wed, 15 Nov 2023 22:03:05 -0500 Subject: [PATCH 2/3] slight fixes --- Patcher/PatchClient/PatchClient.csproj | 4 +-- Patcher/PatchGenerator/PatchGenerator.csproj | 4 +-- .../PatchGenerator/Resources/PatchClient.exe | 4 +-- .../Helpers/XdeltaProcessHelper.cs | 36 ++++++++++++------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Patcher/PatchClient/PatchClient.csproj b/Patcher/PatchClient/PatchClient.csproj index b802792..27c7599 100644 --- a/Patcher/PatchClient/PatchClient.csproj +++ b/Patcher/PatchClient/PatchClient.csproj @@ -4,8 +4,8 @@ net6.0 true enable - 2.10.0 - 2.10.0 + 2.10.1 + 2.10.1 diff --git a/Patcher/PatchGenerator/PatchGenerator.csproj b/Patcher/PatchGenerator/PatchGenerator.csproj index 3a62d2c..3116ee1 100644 --- a/Patcher/PatchGenerator/PatchGenerator.csproj +++ b/Patcher/PatchGenerator/PatchGenerator.csproj @@ -4,8 +4,8 @@ net6.0 true enable - 2.10.0 - 2.10.0 + 2.10.1 + 2.10.1 diff --git a/Patcher/PatchGenerator/Resources/PatchClient.exe b/Patcher/PatchGenerator/Resources/PatchClient.exe index 924cd0c..717dfc1 100644 --- a/Patcher/PatchGenerator/Resources/PatchClient.exe +++ b/Patcher/PatchGenerator/Resources/PatchClient.exe @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:633df532bcdd3b1c6a1fa8a0179a4ec1d6e2a7f2d9fbed9648bdbb61e29c5aef -size 102612425 +oid sha256:a2f2315e664995795da5fc406062b3987309ec7664784cac290e5a46865d029c +size 102628809 diff --git a/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs b/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs index cd4af72..836145e 100644 --- a/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs +++ b/Patcher/PatcherUtils/Helpers/XdeltaProcessHelper.cs @@ -29,24 +29,34 @@ public class XdeltaProcessHelper private bool RunNormal() { - using var proc = new Process(); - - proc.StartInfo = new ProcessStartInfo + try { - FileName = LazyOperations.XDelta3Path, - Arguments = $"{_args} \"{_sourcePath}\" \"{_deltaPath}\" \"{_decodedPath}\"", - CreateNoWindow = true - }; + using var proc = new Process(); + + proc.StartInfo = new ProcessStartInfo + { + FileName = LazyOperations.XDelta3Path, + Arguments = $"{_args} \"{_sourcePath}\" \"{_deltaPath}\" \"{_decodedPath}\"", + CreateNoWindow = true + }; + + proc.Start(); + + if (!proc.WaitForExit(_timeout)) + { + PatchLogger.LogError("xdelta3 process timed out"); + PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); + return false; + } - if (proc.WaitForExit(_timeout)) - { - PatchLogger.LogError("xdelta3 process timed out"); PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); + return true; + } + catch (Exception ex) + { + PatchLogger.LogException(ex); return false; } - - PatchLogger.LogDebug($"xdelta exit code: {proc.ExitCode}"); - return true; } private bool DebugPathsCheck() From 0e83ee04d7c79e5896a2ffaf9e5655a4b0963f68 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Fri, 17 Nov 2023 11:33:19 -0500 Subject: [PATCH 3/3] add version output --- Patcher/PatchClient/App.axaml.cs | 5 +++++ Patcher/PatchClient/PatchClient.csproj | 4 ++-- Patcher/PatchGenerator/App.axaml.cs | 5 +++++ Patcher/PatchGenerator/PatchGenerator.csproj | 4 ++-- Patcher/PatchGenerator/Resources/PatchClient.exe | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Patcher/PatchClient/App.axaml.cs b/Patcher/PatchClient/App.axaml.cs index 651ae39..4b394b5 100644 --- a/Patcher/PatchClient/App.axaml.cs +++ b/Patcher/PatchClient/App.axaml.cs @@ -7,6 +7,7 @@ using ReactiveUI; using System.Reactive; using System; using System.Linq; +using System.Reflection; using PatcherUtils.Model; namespace PatchClient @@ -46,6 +47,10 @@ namespace PatchClient PatchLogger.LogInfo("Running with autoclose"); } + var version = Assembly.GetExecutingAssembly().GetName().Version; + + PatchLogger.LogInfo($"Patch Client v{version?.ToString() ?? "N/A"}"); + desktop.MainWindow = new MainWindow { DataContext = new MainWindowViewModel(autoClose, debugOutput), diff --git a/Patcher/PatchClient/PatchClient.csproj b/Patcher/PatchClient/PatchClient.csproj index 27c7599..90d23d4 100644 --- a/Patcher/PatchClient/PatchClient.csproj +++ b/Patcher/PatchClient/PatchClient.csproj @@ -4,8 +4,8 @@ net6.0 true enable - 2.10.1 - 2.10.1 + 2.10.2 + 2.10.2 diff --git a/Patcher/PatchGenerator/App.axaml.cs b/Patcher/PatchGenerator/App.axaml.cs index 0e0c1c6..eda4c00 100644 --- a/Patcher/PatchGenerator/App.axaml.cs +++ b/Patcher/PatchGenerator/App.axaml.cs @@ -7,6 +7,7 @@ using PatchGenerator.Views; using ReactiveUI; using System.Reactive; using System; +using System.Reflection; using PatcherUtils.Model; namespace PatchGenerator @@ -26,6 +27,10 @@ namespace PatchGenerator public override void OnFrameworkInitializationCompleted() { + var version = Assembly.GetExecutingAssembly().GetName().Version; + + PatchLogger.LogInfo($"Patch Generator v{version?.ToString() ?? "N/A"}"); + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { desktop.Startup += Desktop_Startup; diff --git a/Patcher/PatchGenerator/PatchGenerator.csproj b/Patcher/PatchGenerator/PatchGenerator.csproj index 3116ee1..ac6becd 100644 --- a/Patcher/PatchGenerator/PatchGenerator.csproj +++ b/Patcher/PatchGenerator/PatchGenerator.csproj @@ -4,8 +4,8 @@ net6.0 true enable - 2.10.1 - 2.10.1 + 2.10.2 + 2.10.2 diff --git a/Patcher/PatchGenerator/Resources/PatchClient.exe b/Patcher/PatchGenerator/Resources/PatchClient.exe index 717dfc1..aa98b0f 100644 --- a/Patcher/PatchGenerator/Resources/PatchClient.exe +++ b/Patcher/PatchGenerator/Resources/PatchClient.exe @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a2f2315e664995795da5fc406062b3987309ec7664784cac290e5a46865d029c +oid sha256:232ada1ba713252985713bc85f12c721f9497ec367524f07c33a4ebf4d0281a4 size 102628809