-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch.py
193 lines (161 loc) · 7.33 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
from pathlib import Path
import io
from typing import TypeVar
from binary import BinaryStream
import argparse
import struct
from upkreader import readerGet
from texture2d import Texture2D
def patch(filepath, ph, addDir = None, silent=False, end=False):
sp = False
if addDir is not None:
sp = True
rr = readerGet(filepath, silent=silent, split=sp)
reader = rr["reader"]
imports = rr["imports"]
exports = rr["exports"]
data = rr["data"]
outDir = rr["dir"]
headerSize = rr["headerSize"]
rrnames = rr["dNames"]
dataOff = 0
dataSize = 0
if addDir is not None:
files = Path(f"_DYpatched{os.sep}{addDir}").glob('*.*_patched')
else:
files = Path("_DYpatched").glob('*.*_patched')
patchedFiles = []
for pfile in files:
oid = int(str(pfile).split(".")[1])
ot = str(pfile).split(".")[2].split("_")[:-1]
otype = '_'.join(ot)
oname = str(pfile.stem.split(".")[0])
patchedFiles.append(f"{oname}.{oid}.{otype}")
a = True
with open(str(filepath) + "_patched", "wb") as pf:
pr = BinaryStream(pf)
pr.seek(0)
if ph:
with open(f"{outDir}/_header", "rb") as head:
pr.writeBytes(head.read())
else:
reader.seek(0)
pr.writeBytes(reader.readBytes(headerSize))
offsetDiff = 0
sizeDiff = 0
prevSize = 0
with open(f"{outDir}/_objects.txt", "r") as listfile:
for line in listfile:
odata = line.replace(' ', '').replace('\n','').split(";")
name = odata[0]
sizeOff = int(odata[1])
size = int(odata[2])
headerOff = int(odata[3])
offset = int(odata[4])
b = False
if name in patchedFiles:
tmpPath = f"_DYpatched/"
if addDir is not None:
tmpPath += f"{addDir}/"
tmpPath += f"{name}_patched"
psize = os.stat(tmpPath).st_size
with open(tmpPath, "rb") as f:
writeData = f.read()
sizeDiff = psize - size
offsetDiff = offsetDiff + sizeDiff
b = True
else:
sizeDiff = 0
reader.seek(offset)
writeData = reader.readBytes(size)
if a and b:
a = False
if not silent:
print("Patched Objects:")
oDiff = offsetDiff
if (not end) and b:
oDiff = offsetDiff - sizeDiff
offe = offset + oDiff
if end and b:
q = reader.offset()
reader.seek(0, os.SEEK_END)
offe = reader.offset() + prevSize
reader.seek(q)
offsetDiff = 0
oDiff = offsetDiff
if (not silent) and b:
print(f"- {name}\n original size: {size}\n patched size: {psize}\n size diff: {sizeDiff}\n offset: {offe}")
## Write new offsets into elements
if not end:
if name.split(".")[-1] == "Texture2D":
texBytes = io.BytesIO(writeData)
tex2d = Texture2D(texBytes, rrnames)
r = tex2d.reader
mm = tex2d.mipmaps
try:
r.seek(tex2d.firstAddress[0])
except:
print(name)
raise
r.writeUInt32(offe + r.offset()+4)
for m in mm:
r.seek(m["offset"]-4)
r.writeUInt32(offe + r.offset() + 4)
r.seek(0)
writeData = r.readBytes(tex2d.datasize)
if name.split(".")[-1] == "TextureCube":
writeData = writeData[:len(writeData)-4]
writeData += struct.pack("I", offe + 4 + len(writeData))
## Write data to header
newSize = size + sizeDiff
if b:
newSize = psize
pr.seek(offe)
pr.writeBytes(writeData)
pr.seek(sizeOff)
pr.writeInt32(newSize)
if end and b:
prevSize = newSize + prevSize
pr.seek(headerOff)
pr.writeInt32(offe)
# bt.seek(0, os.SEEK_END)
# bts = bt.tell()
# bt.seek(0)
# ff = False
# while not ff:
# a = struct.unpack("I", bt.read(4))[0]
# if a <= len(rrnames):
# n = rrnames[a].decode("ISO-8859-1")
# if n == "None\x00":
# ff = True
#
# if n == "BoolProperty\x00":
# bt.read(1)
#
# if bt.tell() > 1024:
# raise TypeError("*censored* -- OUT OF RANGE")
#
# bt.read(16)
# pr.seek(bt.tell() + offe)
# pr.writeInt32(pr.offset() + 4)
# first = struct.unpack("I", bt.read(4))[0]
# limit = bts + offe
# while bt.tell() < bts:
# k = struct.unpack("I", bt.read(4))[0]
# if (k < limit) and (k > first):
# bt.seek(bt.tell() - 4)
# break
# pr.seek(bt.tell() + offe)
# pr.writeInt32(pr.offset() + 4)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = 'UPK Patcher (Dishonored), uses files from folder "_DYpatched"', epilog = 'Work in progress')
parser.add_argument("filename", help = "File to patch (saves as <filename>_patched in folder with file)")
parser.add_argument("-p", "--patch-header", default=False, help = "Insert a header file from _DYpatched", action = argparse.BooleanOptionalAction)
parser.add_argument("-s", "--split", default=False, help = "Get patched files from _DYpatched/<upk name>", action = argparse.BooleanOptionalAction)
parser.add_argument("-e", "--end", default=False, help="Put patched files to end of upk", action=argparse.BooleanOptionalAction)
args = parser.parse_args()
ad = None
if args.split:
ad = '.'.join(os.path.basename(args.filename).split(".")[::-1][-1::])
patch(Path(args.filename), args.patch_header, ad, args.end)