"""Append a hand fix to the patch file so it survives a re-decompile. Keeping every fix in one reviewed file, rather than editing generated source, is what makes prepare-game.ps1 reproducible: the tree is thrown away and rebuilt on every run, so a fix that is not recorded simply disappears. python add_patch.py The before/after files hold the exact text to replace and must match the decompiled output byte for byte. """ import io import os import sys def main(): patch_file, java_path, before_file, after_file = sys.argv[1:5] with io.open(before_file, encoding='utf-8') as handle: before = handle.read().rstrip('\n') with io.open(after_file, encoding='utf-8') as handle: after = handle.read().rstrip('\n') if not before or not after: print('ERROR: before/after must not be empty') return 1 if before == after: print('ERROR: before and after are identical') return 1 existing = '' if os.path.isfile(patch_file): with io.open(patch_file, encoding='utf-8') as handle: existing = handle.read() header = '=== FILE %s\n' % java_path if header + '--- BEFORE\n' + before + '\n--- AFTER\n' in existing: print('patch already present') return 0 with io.open(patch_file, 'a', encoding='utf-8') as handle: if existing and not existing.endswith('\n'): handle.write('\n') handle.write('\n' + header) handle.write('--- BEFORE\n' + before + '\n') handle.write('--- AFTER\n' + after + '\n') print('appended patch for %s' % java_path) return 0 if __name__ == '__main__': raise SystemExit(main())