-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg.py
297 lines (276 loc) · 10.4 KB
/
msg.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import io
import os
import sys
import struct
from wand.image import Image
import ast
def extract(msgIn, charsFile = None, out = None):
print(f"\n---- Extracting {msgIn}")
dir_path = os.path.dirname(os.path.realpath(__file__))
if charsFile == None:
charsFile = f"{dir_path}/charindex.txt"
with open(charsFile, "r", encoding="utf-8") as chi:
charTableEngText = chi.read()
charTableEng = ast.literal_eval(charTableEngText)
if out == None:
out = f"{msgIn}.txt"
print(f"---- Output {out}")
print(f"---- Charset {os.path.basename(charsFile)}")
stringData = ''
with open(msgIn, 'rb') as mf:
mf.seek(0, os.SEEK_END)
fs = mf.tell()
mf.seek(0)
headerCount = struct.unpack("i", mf.read(4))[0]
mfoff = 0
pointer = 0
# ig = 0
cmdByte = "7e"
for i in range(headerCount):
mf.seek(4 + 8*i)
off = struct.unpack('i', mf.read(4))[0]
if i == 0:
mfoff = off
size = struct.unpack('i', mf.read(4))[0]
pointer = mf.tell()
mf.seek(off)
data = mf.read(size)
string = ''
d = io.BytesIO(data)
while d.tell() < size:
dat = d.read(1)
if dat == b'\x7E' or dat == b'\x77':
cmdByte = dat.hex()
q = d.read(1)
if q == b'\x09' or q == b'\x0A' or q == b'\x0B':
a = d.read(1)
string += f"{{{q.hex()}:{a.hex()}}}"
elif q == b'\x0E':
string += f"\n{{ENDSTRING:{d.read(size - d.tell()).hex()}}}"
break
elif q == b'\x0d':
a = d.read(1).hex()
b = d.read(1).hex()
string += f"{{{q.hex()}:[{a},{b}]}}"
elif q == b'\x05':
string += " "
# with Image(width=16,height=32) as img:
# img.save(filename=f"test/{i}/{ig}_space.dds")
elif q == b'\x0c':
string += "\n"
else:
string += f"{{{q.hex()}}}"
continue
if dat == b'\x72':
itm = struct.unpack("B", d.read(1))[0]
else:
itm = struct.unpack("B", dat)[0]
try:
string += charTableEng[itm]
except:
string += f"{{?{itm}}}"
# x = int(itm * 16)
# yc = 0
# while x > (256-16):
# x = x - 256
# if x < 0:
# x = 0
# yc += 1
# y = yc * 32
# ii = 0
# if x > 256-16:
# ii = 1
# x = x - 256 - 16
# print(f"x: {x} :: y: {y}")
# if (x < 256-15) and (y < 512-32):
# with Image(filename=f"_msg_tex/f096e34f.{ii}.dds") as img:
# with img[x:x+16, y:y+32] as ci:
# ci.save(filename=f"test/{i}/{ig}.dds")
# ig += 1
stringData += ";;;STR\n" + string + "\n;;;ENDSTR\n\n"
stringData += f";;;CMDBYTE:{cmdByte}\n"
if pointer != 0 and mfoff != 0:
endDataFile = mf.read(fs - mf.tell()).hex()
mf.seek(pointer)
endDataHeader = mf.read(mfoff - mf.tell()).hex()
stringData += ";;;ENDHEADERBYTES:"+endDataHeader
if len(endDataFile) != 0:
stringData += "\n;;;ENDBYTES:"+endDataFile
stringData += "\n" #EOF
with open(out, "w", encoding='utf-8') as msgText:
msgText.write(";;;CHARTABLE\n")
msgText.write(charTableEngText)
msgText.write(";;;ENDCHARTABLE\n\n")
msgText.write(stringData)
print("\n---- Done")
def pack(txtIn, out = None):
print(f"\n---- Packing {txtIn}")
if out == None:
out = txtIn.replace('.txt', '')
print(f"---- Output {out}")
chartable = None
endFileData = None
endHeaderData = None
cmdByte = None
strings = []
with open(txtIn, "r", encoding='utf-8') as msg:
args = msg.read().split(';;;')
for i in range(len(args)):
if args[i] == "ENDCHARTABLE\n\n":
chartable = ast.literal_eval(args[i-1].rstrip().replace('CHARTABLE', ''))
if args[i] == "ENDSTR\n\n":
strings.append(args[i-1].replace("STR\n", ''))
if args[i].startswith("CMDBYTE"):
cmdByte = bytes.fromhex(args[i].rstrip().replace("CMDBYTE:", ''))
if args[i].startswith("ENDHEADERBYTES"):
endHeaderData = bytes.fromhex(args[i].rstrip().replace("ENDHEADERBYTES:", ''))
if args[i].startswith("ENDBYTES"):
endFileData = bytes.fromhex(args[i].rstrip().replace("ENDBYTES:",''))
if chartable == None:
dir_path = os.path.dirname(os.path.realpath(__file__))
charsFile = f"{dir_path}/charindex.txt"
with open(charsFile, "r", encoding='utf-8') as chi:
chartable = ast.literal_eval(chi.read())
if cmdByte == None or endHeaderData == None or strings == []:
raise ValueError("Something wrong")
invCharTable = {v: k for k, v in chartable.items()}
cc = len(strings)
msgBytes = struct.pack("I", cc)
msgBytes += bytes(cc * 8)
msgBytes += endHeaderData
strData = []
for i in range(len(strings)):
strEnd = b''
strBytes = b''
string = strings[i].replace('\n{ENDSTRING', '{ENDSTRING').rstrip()
dat = io.StringIO(string)
d = dat.read(1)
while d:
if d == "{":
p = dat.tell()
if dat.read(10) == "ENDSTRING:":
cmdEnd = string.find("}", dat.tell())
end = dat.read(cmdEnd-dat.tell())
strEnd = cmdByte + b'\x0e' + bytes.fromhex(end)
dat.seek(cmdEnd+1)
else:
dat.seek(p+2)
ff = dat.read(1)
if ff == ":":
arg = dat.read(1)
if arg == "[":
arrEnd = string.find("]", dat.tell())
arr = dat.read(arrEnd - dat.tell()).split(',')
dat.seek(p)
arg = ''.join(arr)
else:
arg += dat.read(1)
dat.seek(p)
cmd = dat.read(2)
strBytes += cmdByte + bytes.fromhex(cmd+arg)
cmdEnd = string.find("}", dat.tell())
dat.seek(cmdEnd+1)
elif ff == "}":
e = dat.tell()
dat.seek(p)
strBytes += cmdByte + bytes.fromhex(dat.read(2))
dat.seek(e)
else:
dat.seek(p)
if dat.read(1) == "?":
cmdEnd = string.find("}", dat.tell())
s = dat.read(cmdEnd-dat.tell())
strBytes += struct.pack("B", int(s))
dat.seek(cmdEnd+1)
else:
dat.seek(p)
elif d == ' ':
strBytes += cmdByte + b'\x05'
elif d == '\n':
strBytes += cmdByte + b'\x0c'
else:
try:
if invCharTable[d] >= 112:
strBytes += b'\x72'
strBytes += struct.pack("B", invCharTable[d])
except KeyError:
raise ValueError(f"Symbol '{d}'({ord(d)}) not found in CHARTABLE")
d = dat.read(1)
strBytes += strEnd
da = len(strBytes) & 1
while da != 0:
strBytes += b'\x00'
da = len(strBytes) & 1
strData.append(strBytes)
with io.BytesIO(msgBytes) as mb:
for a in range(len(strData)):
mb.seek(0, os.SEEK_END)
off = mb.tell()
offBytes = struct.pack("I", off)
size = len(strData[a])
sizeBytes = struct.pack("I", size)
mb.write(strData[a])
mb.seek(4 + (a*8))
mb.write(offBytes)
mb.write(sizeBytes)
mb.seek(0)
msgBytes = mb.read()
if endFileData != None:
msgBytes += endFileData
with open(out, "wb") as nm:
nm.write(msgBytes)
print("\n---- Done")
def printHelp():
print(f"\n---- Usage: python3 {sys.argv[0]} [options] <filename>\n")
print(f"---- Options:")
print(f" -e Extract msg to txt (default option)")
print(f" -p Pack txt to msg")
print(f" -c Charset file (default: charindex.txt)")
exit()
if __name__ == "__main__":
print("---- MSG Repacker (DMC 1) ----")
print("---- by deadYokai ----")
isExtract = None
charset = None
out = None
name = None
indx = []
args = sys.argv[1::]
for i in range(len(args)):
arg = args[i]
if arg.startswith("-"):
if arg == "-p":
if isExtract != None:
printHelp()
isExtract = False
elif arg == "-e":
if isExtract != None:
printHelp()
isExtract = True
elif arg == "-c":
if i+1 <= len(args)-2 and charset == None:
charset = args[i+1]
indx.append(i+1)
else:
printHelp()
elif arg == "-o":
if i+1 <= len(args)-2 and out == None:
out = args[i+1]
indx.append(i+1)
else:
printHelp()
else:
printHelp()
else:
if not i in indx and name == None:
name = arg
elif name != None:
printHelp()
if isExtract == None:
isExtract = True
if name == None:
printHelp()
if isExtract:
extract(name, charset, out)
else:
pack(name, out)