diff --git a/tools/isledecomp/isledecomp/dir.py b/tools/isledecomp/isledecomp/dir.py index 7904c1c0..2735cb62 100644 --- a/tools/isledecomp/isledecomp/dir.py +++ b/tools/isledecomp/isledecomp/dir.py @@ -3,7 +3,7 @@ def is_file_cpp(filename: str) -> bool: - (basefile, ext) = os.path.splitext(filename) + (_, ext) = os.path.splitext(filename) return ext.lower() in ('.h', '.cpp') @@ -12,7 +12,7 @@ def walk_source_dir(source: str, recursive: bool = True) -> Iterator[str]: any C++ files found.""" source = os.path.abspath(source) - for subdir, dirs, files in os.walk(source): + for subdir, _, files in os.walk(source): for file in files: if is_file_cpp(file): yield os.path.join(subdir, file) diff --git a/tools/isledecomp/isledecomp/utils.py b/tools/isledecomp/isledecomp/utils.py new file mode 100644 index 00000000..a017cdd6 --- /dev/null +++ b/tools/isledecomp/isledecomp/utils.py @@ -0,0 +1,22 @@ +import colorama + + +def print_diff(udiff, plain): + has_diff = False + for line in udiff: + has_diff = True + color = "" + if line.startswith("++") or line.startswith("@@") or line.startswith("--"): + # Skip unneeded parts of the diff for the brief view + continue + # Work out color if we are printing color + if not plain: + if line.startswith("+"): + color = colorama.Fore.GREEN + elif line.startswith("-"): + color = colorama.Fore.RED + print(color + line) + # Reset color if we're printing in color + if not plain: + print(colorama.Style.RESET_ALL, end="") + return has_diff \ No newline at end of file diff --git a/tools/reccmp/reccmp.py b/tools/reccmp/reccmp.py index 2cd2e43a..f64dad14 100755 --- a/tools/reccmp/reccmp.py +++ b/tools/reccmp/reccmp.py @@ -2,18 +2,21 @@ import argparse import base64 -from capstone import * import difflib -import struct -import subprocess +import json import logging import os -import sys -import colorama -import json import re +import struct +import subprocess +import sys + from isledecomp.dir import walk_source_dir from isledecomp.parser import find_code_blocks +from isledecomp.utils import print_diff + +from capstone import Cs, CS_ARCH_X86, CS_MODE_32 +import colorama from pystache import Renderer parser = argparse.ArgumentParser(allow_abbrev=False, @@ -503,24 +506,7 @@ def can_resolve_register_differences(original_asm, new_asm): else: print(f'{addrs}: {recinfo.name} Effective 100%% match. (Differs in register allocation only)\n\n{ok_text} (still differs in register allocation)\n\n') else: - for line in udiff: - if line.startswith('++') or line.startswith('@@') or line.startswith('--'): - # Skip unneeded parts of the diff for the brief view - pass - elif line.startswith('+'): - if plain: - print(line) - else: - print(colorama.Fore.GREEN + line) - elif line.startswith('-'): - if plain: - print(line) - else: - print(colorama.Fore.RED + line) - else: - print(line) - if not plain: - print(colorama.Style.RESET_ALL, end='') + print_diff(udiff, plain) print(f'\n{recinfo.name} is only {percenttext} similar to the original, diff above') diff --git a/tools/verexp/verexp.py b/tools/verexp/verexp.py index 3a469fb6..b99d0ba0 100755 --- a/tools/verexp/verexp.py +++ b/tools/verexp/verexp.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 import argparse -import colorama import difflib import subprocess import os import sys +from isledecomp.utils import print_diff + parser = argparse.ArgumentParser(allow_abbrev=False, description='Verify Exports: Compare the exports of two DLLs.') parser.add_argument('original', metavar='original-binary', help='The original binary') @@ -54,23 +55,6 @@ def get_exports(file): re_exp = get_exports(args.recompiled) udiff = difflib.unified_diff(og_exp, re_exp) -has_diff = False - -for line in udiff: - has_diff = True - color = '' - if line.startswith('++') or line.startswith('@@') or line.startswith('--'): - # Skip unneeded parts of the diff for the brief view - continue - # Work out color if we are printing color - if not args.no_color: - if line.startswith('+'): - color = colorama.Fore.GREEN - elif line.startswith('-'): - color = colorama.Fore.RED - print(color + line) - # Reset color if we're printing in color - if not args.no_color: - print(colorama.Style.RESET_ALL, end='') +has_diff = print_diff(udiff, args.no_color) sys.exit(1 if has_diff else 0)