From 5e3f864fab5214b82019881b5ffb15b3f4aeea1c Mon Sep 17 00:00:00 2001 From: matias Date: Mon, 25 Sep 2023 01:43:45 -0300 Subject: [PATCH] DataPacher: add patched file callback --- src/Parsec/Shaiya/Data/DataPatcher.cs | 12 +++++++++--- tests/Parsec.Tests/Shaiya/Data/DataTests.cs | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Parsec/Shaiya/Data/DataPatcher.cs b/src/Parsec/Shaiya/Data/DataPatcher.cs index 70600003..2868f4c3 100644 --- a/src/Parsec/Shaiya/Data/DataPatcher.cs +++ b/src/Parsec/Shaiya/Data/DataPatcher.cs @@ -18,7 +18,8 @@ public void Dispose() /// /// Data instance where the patch should be applied /// Data instance containing the patch files - public void Patch(Data targetData, Data patchData) + /// Action which gets invoked when a file gets patched + public void Patch(Data targetData, Data patchData, Action filePatchedCallback = null) { // Create binary writer instance for the target saf file _targetBinaryWriter = new BinaryWriter(File.OpenWrite(targetData.Saf.Path)); @@ -27,7 +28,7 @@ public void Patch(Data targetData, Data patchData) _patchBinaryReader = new BinaryReader(File.OpenRead(patchData.Saf.Path)); // Patch files - PatchFiles(targetData, patchData); + PatchFiles(targetData, patchData, filePatchedCallback); // Remove previous sah and save the new one FileHelper.DeleteFile(targetData.Sah.Path); @@ -41,9 +42,11 @@ public void Patch(Data targetData, Data patchData) /// /// Data where to save the files /// Data where to take the files from - private void PatchFiles(Data targetData, Data patchData) + /// Action which gets invoked when a file gets patched + private void PatchFiles(Data targetData, Data patchData, Action filePatchedCallback = null) { foreach (var patchFile in patchData.FileIndex.Values) + { // File was already present in the data - it needs to be replaced and doesn't need to be added to the FileIndex if (targetData.FileIndex.TryGetValue(patchFile.RelativePath, out var targetFile)) { @@ -87,6 +90,9 @@ private void PatchFiles(Data targetData, Data patchData) // Add file to file index targetData.FileIndex.Add(patchFile.RelativePath, patchFile); } + + filePatchedCallback?.Invoke(); + } } /// diff --git a/tests/Parsec.Tests/Shaiya/Data/DataTests.cs b/tests/Parsec.Tests/Shaiya/Data/DataTests.cs index 34cdc032..e98e8f08 100644 --- a/tests/Parsec.Tests/Shaiya/Data/DataTests.cs +++ b/tests/Parsec.Tests/Shaiya/Data/DataTests.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using System.IO; using System.Linq; using Parsec.Shaiya.Data; @@ -103,10 +104,15 @@ public void DataPatchingTest() var patchFiles = patch.FileIndex.Keys.Concat(patch2.FileIndex.Keys).ToList(); + int patchedFileCount = 0; + var callback = new Action(() => { patchedFileCount++; }); + // Apply patches using var dataPatcher = new DataPatcher(); - dataPatcher.Patch(data, patch); - dataPatcher.Patch(data, patch2); + dataPatcher.Patch(data, patch, callback); + dataPatcher.Patch(data, patch2, callback); + + Assert.Equal(patchFiles.Count, patchedFileCount); // Get files that were added to the data and weren't present before var newFiles = patchFiles.Except(initialFiles).ToList();