-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tbl merge bug and generic file merging
- Fixed TBL Merging bug related to expanded TBL files - Added generic file merging for a bunch of files for P3P, P4G and P5R - Add testing units for new generic merging
- Loading branch information
1 parent
2027491
commit bb2ec52
Showing
20 changed files
with
489 additions
and
4 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Persona.Merger.Common/Patching/Tbl/FieldResolvers/Generic/ByteResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Sewer56.StructuredDiff.Interfaces; | ||
|
||
namespace Persona.Merger.Patching.Tbl.FieldResolvers; | ||
|
||
public struct ByteResolver : IEncoderFieldResolver | ||
{ | ||
public bool Resolve(nuint offset, out int moveBy, out int length) | ||
{ | ||
moveBy = 0; | ||
length = 1; | ||
return false; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
Persona.Merger.Common/Patching/Tbl/FieldResolvers/Generic/GenericPatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using Persona.Merger.Utilities; | ||
using Reloaded.Memory.Streams; | ||
using static Persona.Merger.Patching.Tbl.FieldResolvers.TblPatcherCommon; | ||
|
||
namespace Persona.Merger.Patching.Tbl.FieldResolvers.Generic; | ||
|
||
/// <summary> | ||
/// Utility class for patching P5R TBL files. | ||
/// </summary> | ||
public struct GenericPatcher | ||
{ | ||
/// <summary> | ||
/// The table to patch. | ||
/// </summary> | ||
public byte[] TblData { get; set; } | ||
|
||
public GenericPatcher(byte[] tblData) | ||
{ | ||
TblData = tblData; | ||
} | ||
|
||
/// <summary> | ||
/// Generates a table patch. | ||
/// </summary> | ||
/// <param name="otherTbl">Data of the new table.</param> | ||
public unsafe TblPatch GeneratePatchGeneric(byte[] otherTbl, int ResolverSize) | ||
{ | ||
fixed (byte* otherTblData = &otherTbl[0]) | ||
fixed (byte* tblData = &TblData[0]) | ||
{ | ||
var patch = new TblPatch(); | ||
|
||
var originalSegments = stackalloc PointerLengthTuple[1]; // using pointer to elide bounds checks below | ||
var newSegments = stackalloc PointerLengthTuple[1]; | ||
PopulateGeneric(tblData, TblData.Length, originalSegments); | ||
PopulateGeneric(otherTblData, otherTbl.Length, newSegments); | ||
|
||
if (ResolverSize == 2) DiffSegment(patch, newSegments[0], originalSegments[0], new ShortResolver()); | ||
else if (ResolverSize == 4) DiffSegment(patch, newSegments[0], originalSegments[0], new IntResolver()); | ||
else DiffSegment(patch, newSegments[0], originalSegments[0], new ByteResolver()); | ||
|
||
return patch; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Applies a list of table patches. | ||
/// </summary> | ||
/// <param name="patches">List of patches to apply.</param> | ||
public unsafe byte[] ApplyGeneric(List<TblPatch> patches) | ||
{ | ||
fixed (byte* tblData = &TblData[0]) | ||
{ | ||
// Get original segments. | ||
var segmentCount = 1; | ||
var originalSegments = stackalloc PointerLengthTuple[segmentCount]; // using pointer to elide bounds checks below | ||
PopulateGeneric(tblData, segmentCount, originalSegments); | ||
|
||
// Convert original segments into Memory<T>. | ||
var segments = ConvertSegmentsToMemoryGeneric(segmentCount, originalSegments, tblData, TblData); | ||
|
||
// Apply Patch(es). | ||
for (int x = 0; x < segmentCount; x++) | ||
ApplyPatch(patches, x, segments); | ||
|
||
// Produce new file. | ||
var fileSize = 0; | ||
foreach (var segment in segments) | ||
fileSize += segment.Length; | ||
|
||
var result = GC.AllocateUninitializedArray<byte>(fileSize); | ||
using var memoryStream = new ExtendedMemoryStream(result, true); | ||
foreach (var segment in segments) | ||
{ | ||
// memoryStream.WriteBigEndianPrimitive(segment.Length); | ||
memoryStream.Write(segment.Span); | ||
// memoryStream.AddPadding(P5RTblSegmentFinder.TblSegmentAlignment); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
public unsafe static void PopulateGeneric(byte* tblPointer, int length, PointerLengthTuple* segments) | ||
{ | ||
ref var currentSegment = ref segments[0]; | ||
currentSegment.Pointer = tblPointer; | ||
currentSegment.Length = length; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Persona.Merger.Common/Patching/Tbl/FieldResolvers/Generic/IntResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Sewer56.StructuredDiff.Interfaces; | ||
|
||
namespace Persona.Merger.Patching.Tbl.FieldResolvers; | ||
|
||
public struct IntResolver : IEncoderFieldResolver | ||
{ | ||
public bool Resolve(nuint offset, out int moveBy, out int length) | ||
{ | ||
// All data are u32s. | ||
var fourByteAligned = offset / 4 * 4; | ||
moveBy = (int)(offset - fourByteAligned); | ||
length = 4; | ||
return true; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Persona.Merger.Common/Patching/Tbl/FieldResolvers/Generic/ShortResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Sewer56.StructuredDiff.Interfaces; | ||
|
||
namespace Persona.Merger.Patching.Tbl.FieldResolvers; | ||
|
||
public struct ShortResolver : IEncoderFieldResolver | ||
{ | ||
public bool Resolve(nuint offset, out int moveBy, out int length) | ||
{ | ||
var twoByteAligned = offset / 2 * 2; | ||
moveBy = (int)(offset - twoByteAligned); | ||
length = 2; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.