Allow patches to ignore filesize so a partial file can be patched.

This commit is contained in:
Jennifer Taylor 2021-10-08 15:16:45 +00:00
parent b81f25a38f
commit 0aa4d6403d
3 changed files with 12 additions and 7 deletions

View File

@ -91,6 +91,8 @@ instead patch the reverse of each patch. A binary that was patched using `Binary
can be reverted back to the original format by calling `BinaryDiff.patch` again with can be reverted back to the original format by calling `BinaryDiff.patch` again with
the "reverse" argument set to True. Note that the only restriction to this is if any of the "reverse" argument set to True. Note that the only restriction to this is if any of
the patches include wildcards then the resulting patched binary cannot be reversed. the patches include wildcards then the resulting patched binary cannot be reversed.
If you pass in the optional boolean keyword argument "ignore_size_differences" then the
"File Size" comment will be ignored.
### Patch Format ### Patch Format

View File

@ -166,14 +166,17 @@ class BinaryDiff:
patchlines: List[str], patchlines: List[str],
*, *,
reverse: bool = False, reverse: bool = False,
ignore_size_differences: bool = False,
) -> bytes: ) -> bytes:
# First, grab the differences # First, grab the differences
file_size = BinaryDiff.size(patchlines) if not ignore_size_differences:
if file_size is not None and file_size != len(binary): file_size = BinaryDiff.size(patchlines)
raise BinaryDiffException( if file_size is not None and file_size != len(binary):
f"Patch is for binary of size {file_size} but binary is {len(binary)} " raise BinaryDiffException(
f"bytes long!" f"Patch is for binary of size {file_size} but binary is {len(binary)} "
) f"bytes long!"
)
differences: List[Tuple[int, Optional[bytes], bytes]] = sorted( differences: List[Tuple[int, Optional[bytes], bytes]] = sorted(
BinaryDiff._gather_differences(patchlines, reverse), BinaryDiff._gather_differences(patchlines, reverse),
key=lambda diff: diff[0], key=lambda diff: diff[0],

View File

@ -8,7 +8,7 @@ with open(os.path.join("arcadeutils", "README.md"), "r", encoding="utf-8") as fh
setup( setup(
name='arcadeutils', name='arcadeutils',
version='0.1.2', version='0.1.3',
description='Collection of utilities written in Python for working with various arcade binaries.', description='Collection of utilities written in Python for working with various arcade binaries.',
long_description=long_description, long_description=long_description,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",