Skip to content

Commit

Permalink
Allow first_diff frontend to accept an optional bytes converter callback
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Jul 28, 2023
1 parent 9e413ac commit 7b0c5ff
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[project]
name = "mapfile_parser"
version = "1.1.5"
version = "1.2.0.dev0"
description = "Map file parser library focusing decompilation projects"
readme = "README.md"
requires-python = ">=3.7"
Expand Down
4 changes: 2 additions & 2 deletions src/mapfile_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from __future__ import annotations

__version_info__ = (1, 1, 5)
__version__ = ".".join(map(str, __version_info__))
__version_info__ = (1, 2, 0)
__version__ = ".".join(map(str, __version_info__)) + ".dev0"
__author__ = "Decompollaborate"

from . import utils
Expand Down
21 changes: 18 additions & 3 deletions src/mapfile_parser/frontends/first_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import argparse
from pathlib import Path
from typing import Callable

from .. import mapfile
from .. import utils


def doFirstDiff(mapPath: Path, expectedMapPath: Path, romPath: Path, expectedRomPath: Path, diffCount: int=5, mismatchSize: bool=False) -> int:
def doFirstDiff(mapPath: Path, expectedMapPath: Path, romPath: Path, expectedRomPath: Path, diffCount: int=5, mismatchSize: bool=False, bytesConverterCallback:Callable[[bytes, mapfile.MapFile],str|None]|None=None) -> int:
if not mapPath.exists():
print(f"{mapPath} must exist")
return 1
Expand Down Expand Up @@ -61,7 +62,14 @@ def doFirstDiff(mapPath: Path, expectedMapPath: Path, romPath: Path, expectedRom
if vromInfo is not None:
extraMessage = f", {vromInfo.getAsStrPlusOffset()}"
print(f"First difference at ROM addr 0x{i:X}{extraMessage}")
print(f"Bytes: {utils.hexbytes(builtRom[i : i + 4])} vs {utils.hexbytes(expectedRom[i : i + 4])}")
builtBytes = builtRom[i : i + 4]
expectedBytes = expectedRom[i : i + 4]
print(f"Bytes: {utils.hexbytes(builtBytes)} vs {utils.hexbytes(expectedBytes)}")
if bytesConverterCallback is not None:
builtConverted = bytesConverterCallback(builtBytes, builtMapFile)
expectedConverted = bytesConverterCallback(expectedBytes, expectedMapFile)
if builtConverted is not None and expectedConverted is not None:
print(f"{builtConverted} vs {expectedConverted}")
diffs += 1

if (
Expand All @@ -78,7 +86,14 @@ def doFirstDiff(mapPath: Path, expectedMapPath: Path, romPath: Path, expectedRom
if vromInfo is not None:
extraMessage = f", {vromInfo.getAsStrPlusOffset()}"
print(f"Instruction difference at ROM addr 0x{i:X}{extraMessage}")
print(f"Bytes: {utils.hexbytes(builtRom[i : i + 4])} vs {utils.hexbytes(expectedRom[i : i + 4])}")
builtBytes = builtRom[i : i + 4]
expectedBytes = expectedRom[i : i + 4]
print(f"Bytes: {utils.hexbytes(builtBytes)} vs {utils.hexbytes(expectedBytes)}")
if bytesConverterCallback is not None:
builtConverted = bytesConverterCallback(builtBytes, builtMapFile)
expectedConverted = bytesConverterCallback(expectedBytes, expectedMapFile)
if builtConverted is not None and expectedConverted is not None:
print(f"{builtConverted} vs {expectedConverted}")

if len(map_search_diff) >= diffCount and diffs > shift_cap:
break
Expand Down
2 changes: 1 addition & 1 deletion src/mapfile_parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def readFileAsBytearray(filepath: Path) -> bytearray:
with filepath.open(mode="rb") as f:
return bytearray(f.read())

def hexbytes(bs):
def hexbytes(bs: bytes) -> str:
return ":".join("{:02X}".format(c) for c in bs)

def getGitCommitTimestamp() -> int:
Expand Down

0 comments on commit 7b0c5ff

Please sign in to comment.