Skip to content

Commit

Permalink
feature: gracefully fail pattern scanning and print helpful error mes…
Browse files Browse the repository at this point in the history
…sages

refactor: use regex pattern scanning everywhere
  • Loading branch information
gurrgur committed Mar 28, 2022
1 parent 7ae9c59 commit f49096c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A tool aimed at enhancing the experience when playing the game on linux through

## Dependencies

- Python >= 3.8.xx (lowest tested)
- Python >= 3.8

## Usage

Expand Down
92 changes: 57 additions & 35 deletions er-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,80 @@ if __name__ == "__main__":
exe_hex = f.read().hex()

if patch.rate != 60 and patch.rate > 0:
exe_hex = exe_hex.replace(
"c743208988883ceb43897318ebca897318",
"c743208988883ceb43897318ebca897318".replace(
"8988883c", struct.pack('<f', 1 / patch.rate).hex()
)
)
r_pattern = "c7 43 20 89 88 88 3c eb 43 89 73 18 eb ca 89 73 18".replace(" ", "")
if (res := re.search(r_pattern, exe_hex)) is not None:
r_addr = res.span()[0] + 6
r_patch = struct.pack('<f', 1 / patch.rate).hex()
exe_hex = exe_hex[:r_addr] + r_patch + exe_hex[r_addr + len(r_patch):]
else:
print("er-patcher: rate pattern scan failed")

if patch.fix_camera:
cf_pattern = '0f 29 a6 .. .. .. .. 41 0f 28 cf'.replace(" ", "")
cf_addr = re.search(cf_pattern, exe_hex).span()[0]
cf_offset = 0
cf_patch = "90 90 90 90 90 90 90".replace(" ", "")
exe_hex = exe_hex[:cf_addr + cf_offset] + cf_patch + exe_hex[cf_addr + cf_offset + len(cf_patch):]
if (res := re.search(cf_pattern, exe_hex)) is not None:
cf_addr = res.span()[0]
cf_patch = "90 90 90 90 90 90 90".replace(" ", "")
exe_hex = exe_hex[:cf_addr] + cf_patch + exe_hex[cf_addr + len(cf_patch):]
else:
print("er-patcher: fix_camera pattern scan failed")

if patch.ultrawide or patch.all:
exe_hex = exe_hex.replace(
"8b0185c07442448b5904",
"8b0185c0eb42448b5904"
)
uw_pattern = "8b 01 85 c0 74 42 44 8b 59 04".replace(" ", "")
if (res := re.search(uw_pattern, exe_hex)) is not None:
uw_addr = res.span()[0] + 8
uw_patch = "eb"
exe_hex = exe_hex[:uw_addr] + uw_patch + exe_hex[uw_addr + len(uw_patch):]
else:
print("er-patcher: ultrawide pattern scan failed")


if patch.disable_vigniette or patch.all:
v_pattern = 'f3 0f 10 .. .. f3 0f 59 .. .. .. .. .. e8 .. .. .. .. f3 41 0f .. .. f3 45 0f .. .. 4c 8d .. .. .. .. .. .. 48'.replace(" ", "")
v_addr = re.search(v_pattern, exe_hex).span()[0]
v_offset = 46
v_patch = "f3 0f 5c c0 90".replace(" ", "") # SUBSS XMM0,XMM0; NOP; all NOP does work too
exe_hex = exe_hex[:v_addr + v_offset] + v_patch + exe_hex[v_addr + v_offset + len(v_patch):]
if (res := re.search(v_pattern, exe_hex)) is not None:
v_addr = res.span()[0] + 46
v_patch = "f3 0f 5c c0 90".replace(" ", "") # SUBSS XMM0,XMM0; NOP; all NOP does work too
exe_hex = exe_hex[:v_addr] + v_patch + exe_hex[v_addr + len(v_patch):]
else:
print("er-patcher: disable_vigniette pattern scan failed")

if patch.disable_ca or patch.all:
ca_addr = 94 + exe_hex.index("0f114360488d8b800000000f1087a00000000f1141f0488d87b00000000f10080f1109")
if exe_hex[ca_addr:ca_addr + 8] == "0f114920":
exe_hex = exe_hex[:ca_addr] + "660fefc9" + exe_hex[ca_addr + 8:] # PXOR XMM1,XMM1
ca_pattern = "0f 11 43 60 48 8d 8b 80 00 00 00 0f 10 87 a0 00 00 00 0f 11 41 f0 48 8d 87 b0 00 00 00 0f 10 08 0f 11 09".replace(" ", "")
if (res := re.search(ca_pattern, exe_hex)) is not None:
ca_addr = res.span()[0] + 94
ca_orig = "0f 11 49 20".replace(" ", "")
ca_patch = "66 0f ef c9".replace(" ", "") # PXOR XMM1,XMM1
if exe_hex[ca_addr:ca_addr + len(ca_patch)] == ca_orig:
exe_hex = exe_hex[:ca_addr] + ca_patch + exe_hex[ca_addr + len(ca_patch):]
else:
print("er-patcher: disable_ca pattern scan failed")

if patch.increase_animation_distance or patch.all:
iad_pattern = "e8 .. .. .. .. 0f 28 .. 0f 28 .. e8 .. .. .. .. f3 0f .. .. 0f 28 .. f3 41 0f 5e".replace(" ", "")
iad_addr = re.search(iad_pattern, exe_hex).span()[0]
iad_offset = 46
iad_patch = "0f 57 c9 66 0f ef c9".replace(" ", "") # DIVSS XMM1,dword ptr [R12 + 0x54] -> XORPS XMM1,XMM1; PXOR XMM1,XMM1
if exe_hex[iad_addr + iad_offset:iad_addr + iad_offset + len(iad_patch)] == "f3 41 0f 5e 4c 24 54".replace(" ", ""):
iad_pattern = "e8 .. .. .. .. 0f 28 .. 0f 28 .. e8 .. .. .. .. f3 0f .. .. 0f 28 .. f3 41 0f 5e 4c 24 54".replace(" ", "")
if (res := re.search(iad_pattern, exe_hex)) is not None:
iad_addr = res.span()[0] + 46
iad_patch = "0f 57 c9 66 0f ef c9".replace(" ", "") # DIVSS XMM1,dword ptr [R12 + 0x54] -> XORPS XMM1,XMM1; PXOR XMM1,XMM1
exe_hex = exe_hex[:iad_addr] + iad_patch + exe_hex[iad_addr + len(iad_patch):]
else:
print("er-patcher: increase_animation_distance pattern scan failed")

if patch.skip_intro or patch.all:
exe_hex = exe_hex.replace(
"80 bf b8 00 00 00 00 74 53 48".replace(" ", ""),
"80 bf b8 00 00 00 00 90 90 48".replace(" ", "")
)
si_pattern = "80 bf b8 00 00 00 00 74 53 48".replace(" ", "")
if (res := re.search(si_pattern, exe_hex)) is not None:
si_addr = res.span()[0] + 14
si_patch = "90 90".replace(" ", "")
exe_hex = exe_hex[:si_addr] + si_patch + exe_hex[si_addr + len(si_patch):]
else:
print("er-patcher: skip_intro pattern scan failed")

if patch.remove_60hz_fullscreen or patch.all:
exe_hex = exe_hex.replace(
"c745ef3c000000",
"c745ef00000000"
)

fs_pattern = "c7 45 ef 3c 00 00 00".replace(" ", "")
if (res := re.search(fs_pattern, exe_hex)) is not None:
fs_addr = res.span()[0] + 6
fs_patch = "00"
exe_hex = exe_hex[:fs_addr] + fs_patch + exe_hex[fs_addr + len(fs_patch):]
else:
print("er-patcher: remove_60hz_fullscreen pattern scan failed")

game_dir_patched = Path("er-patcher-tmp")
if not game_dir_patched.is_dir():
game_dir_patched.mkdir()
Expand Down

0 comments on commit f49096c

Please sign in to comment.