Skip to content

Commit

Permalink
DataPacher: add patched file callback
Browse files Browse the repository at this point in the history
  • Loading branch information
matigramirez committed Sep 25, 2023
1 parent f674e5e commit 5e3f864
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/Parsec/Shaiya/Data/DataPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void Dispose()
/// </summary>
/// <param name="targetData">Data instance where the patch should be applied</param>
/// <param name="patchData">Data instance containing the patch files</param>
public void Patch(Data targetData, Data patchData)
/// <param name="filePatchedCallback">Action which gets invoked when a file gets patched</param>
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));
Expand All @@ -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);
Expand All @@ -41,9 +42,11 @@ public void Patch(Data targetData, Data patchData)
/// </summary>
/// <param name="targetData">Data where to save the files</param>
/// <param name="patchData">Data where to take the files from</param>
private void PatchFiles(Data targetData, Data patchData)
/// <param name="filePatchedCallback">Action which gets invoked when a file gets patched</param>
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))
{
Expand Down Expand Up @@ -87,6 +90,9 @@ private void PatchFiles(Data targetData, Data patchData)
// Add file to file index
targetData.FileIndex.Add(patchFile.RelativePath, patchFile);
}

filePatchedCallback?.Invoke();
}
}

/// <summary>
Expand Down
12 changes: 9 additions & 3 deletions tests/Parsec.Tests/Shaiya/Data/DataTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Parsec.Shaiya.Data;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5e3f864

Please sign in to comment.