-
Notifications
You must be signed in to change notification settings - Fork 5
/
patch.py
48 lines (37 loc) · 1.25 KB
/
patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import sys
import os.path
import hashlib
class Patchset():
def __init__(self, patch_steam):
self.sha256 = patch_steam.readline().strip("\r\n")
self.patches = {}
for x in patch_steam.readlines():
data = x.split(':')
if len(data) < 2:
continue
offset = int(data[0], 16)
byte = bytes([int(data[1], 16)])
self.patches[offset] = byte
def patch(self, stream):
for offset, byte in self.patches.items():
print(f'offset = {offset}, byte = {byte}')
stream.seek(offset)
stream.write(byte)
def sha256_ok(self, stream):
return hashlib.sha256(stream.read()).hexdigest() == self.sha256
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f'Usage: {sys.argv[0]} file patch')
exit(1)
for x in range(3):
if not os.path.isfile(sys.argv[x]):
raise FileNotFoundError(sys.argv[x])
target = sys.argv[1]
patch = sys.argv[2]
with open(patch, 'r') as pf:
ps = Patchset(pf)
with open(target, 'r+b') as tf:
if not ps.sha256_ok(tf):
raise Exception('Provided file is not valid for the specified patch')
ps.patch(tf)