From b5a8c8ba1cead8fcbdebe93c4b4d7a3314f53f2f Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Thu, 9 Nov 2023 20:49:40 -0500 Subject: [PATCH] added a debug parameter and a bunch of logging when it's used --- Patcher/.idea/.idea.Patcher/.idea/.gitignore | 13 ++++ .../.idea/.idea.Patcher/.idea/encodings.xml | 4 + .../.idea/.idea.Patcher/.idea/indexLayout.xml | 8 ++ Patcher/.idea/.idea.Patcher/.idea/vcs.xml | 6 ++ Patcher/PatchClient/App.axaml.cs | 21 +++++- .../ViewModels/MainWindowViewModel.cs | 4 +- .../ViewModels/PatcherViewModel.cs | 6 +- Patcher/PatcherUtils/Model/PatchLogger.cs | 1 + Patcher/PatcherUtils/PatchHelper.cs | 74 +++++++++++++++++-- 9 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 Patcher/.idea/.idea.Patcher/.idea/.gitignore create mode 100644 Patcher/.idea/.idea.Patcher/.idea/encodings.xml create mode 100644 Patcher/.idea/.idea.Patcher/.idea/indexLayout.xml create mode 100644 Patcher/.idea/.idea.Patcher/.idea/vcs.xml diff --git a/Patcher/.idea/.idea.Patcher/.idea/.gitignore b/Patcher/.idea/.idea.Patcher/.idea/.gitignore new file mode 100644 index 0000000..6571a2a --- /dev/null +++ b/Patcher/.idea/.idea.Patcher/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.Patcher.iml +/modules.xml +/projectSettingsUpdater.xml +/contentModel.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Patcher/.idea/.idea.Patcher/.idea/encodings.xml b/Patcher/.idea/.idea.Patcher/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/Patcher/.idea/.idea.Patcher/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Patcher/.idea/.idea.Patcher/.idea/indexLayout.xml b/Patcher/.idea/.idea.Patcher/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/Patcher/.idea/.idea.Patcher/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Patcher/.idea/.idea.Patcher/.idea/vcs.xml b/Patcher/.idea/.idea.Patcher/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Patcher/.idea/.idea.Patcher/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Patcher/PatchClient/App.axaml.cs b/Patcher/PatchClient/App.axaml.cs index 94d8c2d..651ae39 100644 --- a/Patcher/PatchClient/App.axaml.cs +++ b/Patcher/PatchClient/App.axaml.cs @@ -6,6 +6,7 @@ using PatchClient.Views; using ReactiveUI; using System.Reactive; using System; +using System.Linq; using PatcherUtils.Model; namespace PatchClient @@ -27,13 +28,27 @@ namespace PatchClient if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { bool autoClose = false; + bool debugOutput = false; - if(desktop.Args != null && desktop.Args.Length >= 1 && desktop.Args[0]?.ToLower() == "autoclose") - autoClose = true; + if (desktop.Args != null && desktop.Args.Length >= 1) + { + autoClose = desktop.Args.Any(x => x.ToLower() == "autoclose"); + debugOutput = desktop.Args.Any(x => x.ToLower() == "debug"); + } + + if (debugOutput) + { + PatchLogger.LogInfo("Running in debug mode"); + } + + if (autoClose) + { + PatchLogger.LogInfo("Running with autoclose"); + } desktop.MainWindow = new MainWindow { - DataContext = new MainWindowViewModel(autoClose), + DataContext = new MainWindowViewModel(autoClose, debugOutput), }; } diff --git a/Patcher/PatchClient/ViewModels/MainWindowViewModel.cs b/Patcher/PatchClient/ViewModels/MainWindowViewModel.cs index 27bd0fb..2f59f91 100644 --- a/Patcher/PatchClient/ViewModels/MainWindowViewModel.cs +++ b/Patcher/PatchClient/ViewModels/MainWindowViewModel.cs @@ -18,11 +18,11 @@ namespace PatchClient.ViewModels } }); - public MainWindowViewModel(bool autoClose) + public MainWindowViewModel(bool autoClose, bool debugOutput) { this.WhenActivated((CompositeDisposable disposable) => { - Router.Navigate.Execute(new PatcherViewModel(this, autoClose)); + Router.Navigate.Execute(new PatcherViewModel(this, autoClose, debugOutput)); }); } } diff --git a/Patcher/PatchClient/ViewModels/PatcherViewModel.cs b/Patcher/PatchClient/ViewModels/PatcherViewModel.cs index 63a1cc3..2330913 100644 --- a/Patcher/PatchClient/ViewModels/PatcherViewModel.cs +++ b/Patcher/PatchClient/ViewModels/PatcherViewModel.cs @@ -18,6 +18,7 @@ namespace PatchClient.ViewModels { private bool _initLineItemProgress = true; private bool _autoClose = false; + private bool _debugOutput = false; private Stopwatch _patchStopwatch; private Timer _udpatePatchElapsedTimer = new Timer(1000); @@ -52,9 +53,10 @@ namespace PatchClient.ViewModels } - public PatcherViewModel(IScreen Host, bool autoClose) : base(Host) + public PatcherViewModel(IScreen Host, bool autoClose, bool debugOutput) : base(Host) { _autoClose = autoClose; + _debugOutput = debugOutput; ElapsedPatchTimeDetails = "Starting ..."; _udpatePatchElapsedTimer.Elapsed += _udpatePatchElapsedTimer_Elapsed; @@ -104,7 +106,7 @@ namespace PatchClient.ViewModels { LazyOperations.ExtractResourcesToTempDir(Assembly.GetExecutingAssembly()); - PatchHelper patcher = new PatchHelper(Environment.CurrentDirectory, null, LazyOperations.PatchFolder); + PatchHelper patcher = new PatchHelper(Environment.CurrentDirectory, null, LazyOperations.PatchFolder, _debugOutput); patcher.ProgressChanged += patcher_ProgressChanged; diff --git a/Patcher/PatcherUtils/Model/PatchLogger.cs b/Patcher/PatcherUtils/Model/PatchLogger.cs index 3669be6..58077da 100644 --- a/Patcher/PatcherUtils/Model/PatchLogger.cs +++ b/Patcher/PatcherUtils/Model/PatchLogger.cs @@ -42,6 +42,7 @@ namespace PatcherUtils.Model LogToFile($"{GetTimestamp()}[OS]: {RuntimeInformation.OSDescription}"); } + public static void LogDebug(string message) => LogToFile($"{GetTimestamp()}[DEBUG]: {message}"); public static void LogInfo(string message) => LogToFile($"{GetTimestamp()}[INFO]: {message}"); public static void LogError(string message) => LogToFile($"{GetTimestamp()}[ERROR]: {message}"); public static void LogException(Exception ex) => LogToFile($"{GetTimestamp()}[EXCEPTION]: {ex.Message}\n\nStackTrace:\n{ex.StackTrace}"); diff --git a/Patcher/PatcherUtils/PatchHelper.cs b/Patcher/PatcherUtils/PatchHelper.cs index b810ee9..76682ac 100644 --- a/Patcher/PatcherUtils/PatchHelper.cs +++ b/Patcher/PatcherUtils/PatchHelper.cs @@ -23,6 +23,8 @@ namespace PatcherUtils private int delCount; private int existCount; + private bool debugOutput; + private List AdditionalInfo = new List(); /// @@ -45,11 +47,12 @@ namespace PatcherUtils /// The directory to compare against during patch creation. /// The directory where the patches are/will be located. /// can be null if you only plan to apply patches. - public PatchHelper(string SourceFolder, string TargetFolder, string DeltaFolder) + public PatchHelper(string SourceFolder, string TargetFolder, string DeltaFolder, bool debugOutput = false) { this.SourceFolder = SourceFolder; this.TargetFolder = TargetFolder; this.DeltaFolder = DeltaFolder; + this.debugOutput = debugOutput; } /// @@ -99,13 +102,74 @@ namespace PatcherUtils { string decodedPath = SourceFilePath + ".decoded"; - Process.Start(new ProcessStartInfo + var xdeltaArgs = $"-d {(debugOutput ? "-v -v" : "")} -f -s"; + + if (debugOutput) + { + 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 = $"-d -f -s \"{SourceFilePath}\" \"{DeltaFilePath}\" \"{decodedPath}\"", + Arguments = $"{xdeltaArgs} \"{SourceFilePath}\" \"{DeltaFilePath}\" \"{decodedPath}\"", + RedirectStandardError = true, + RedirectStandardOutput = true, CreateNoWindow = true - }) - .WaitForExit(); + }); + + 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); + } + } if (File.Exists(decodedPath)) {