-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmdProcess.py
189 lines (146 loc) · 5.25 KB
/
gmdProcess.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
from struct import *
import os
import argparse
class BinaryStream:
def __init__(self, base_stream):
self.base_stream = base_stream
def clear(self):
self.base_stream.truncate()
def readByte(self):
return self.base_stream.read(1)
def readBytes(self, length):
return self.base_stream.read(length)
def readChar(self):
return self.unpack('b')
def readUChar(self):
return self.unpack('B')
def readBool(self):
return self.unpack('?')
def readInt16(self):
return self.unpack('h', 2)
def readUInt16(self):
return self.unpack('H', 2)
def readInt32(self):
return self.unpack('i', 4)
def readUInt32(self):
return self.unpack('I', 4)
def readInt64(self):
return self.unpack('q', 8)
def readUInt64(self):
return self.unpack('Q', 8)
def readFloat(self):
return self.unpack('f', 4)
def readDouble(self):
return self.unpack('d', 8)
def readString(self, length):
return self.unpack(str(length) + 's', length)
def writeBytes(self, value):
self.base_stream.write(value)
def writeChar(self, value):
self.pack('c', value)
def writeUChar(self, value):
self.pack('C', value)
def writeBool(self, value):
self.pack('?', value)
def writeInt16(self, value):
self.pack('h', value)
def writeUInt16(self, value):
self.pack('H', value)
def writeInt32(self, value):
self.pack('i', value)
def writeUInt32(self, value):
self.pack('I', value)
def writeInt64(self, value):
self.pack('q', value)
def writeUInt64(self, value):
self.pack('Q', value)
def writeFloat(self, value):
self.pack('f', value)
def writeDouble(self, value):
self.pack('d', value)
def writeString(self, value):
length = len(value)
self.writeUInt16(length)
self.pack(str(length) + 's', value)
def offset(self, asbytes = False):
if asbytes:
return int.to_bytes(self.base_stream.tell())
return self.base_stream.tell()
def seek(self, value, v = None):
if type(value) is bytes:
self.base_stream.seek(int.from_bytes(value))
else:
if v is not None:
self.base_stream.seek(value, v)
else:
self.base_stream.seek(value)
def pack(self, fmt, data):
return self.writeBytes(pack(fmt, data))
def unpack(self, fmt, length = 1):
return unpack(fmt, self.readBytes(length))[0]
import glob
import re
def text(path, pack):
f = os.path.dirname(os.path.abspath(path))
baseName = os.path.basename(path).split('.')[0]
if pack:
mode1 = "rb+"
mode2 = "r"
else:
mode1 = "rb"
mode2 = "w"
with open(fr"{f}{os.sep}{baseName}.gmd", mode1) as gfdFile:
gReader = BinaryStream(gfdFile)
gReader.seek(0, os.SEEK_END)
size = gReader.offset()
gReader.seek(32)
count = gReader.readUInt32()
diff = size - count
if diff > size or diff < 0:
diff = size
print(diff)
gReader.seek(diff)
with open(fr"{f}{os.sep}{baseName}.txt", mode2, encoding="utf-8") as out:
idnum = 0
if not pack:
data = gReader.readBytes(count).replace(b'\x00', b'FFFFFFFF').decode("UTF-8")
text = data.split('FFFFFFFF')
for a in text:
out.write(f"---STARTSTRING[{idnum}]---\n")
out.write(a)
out.write("\n---ENDSTRING---\n\n")
idnum += 1
else:
gReader.clear()
pt = out.read()
# pattern = r"---STARTSTRING(?:\[\d+\]|)---\n(.+?|\n)(?:|\n)---ENDSTRING---"
# text = re.sub(r"---STARTSTRING\[.*\]---\n", '', pt).replace("---STARTSTRING---\n", '').split("\n---ENDSTRING---\n\n")
# text = re.findall(pattern, pt, re.DOTALL)
lines = pt.split('\n')
linearr = []
lineblock = []
inBlock = False
for line in lines:
if line.startswith("---STARTSTRING"):
inBlock = True
if line.startswith("---ENDSTRING---"):
lineblock.append('\n'.join(linearr))
linearr = []
inBlock = False
if(inBlock):
linearr.append(line)
print(len(lineblock))
c = 0
for a in lineblock:
data = a.replace('\n', '\r\n').encode("UTF-8") + b'\x00'
gReader.writeBytes(data)
c += len(data)
o = gReader.offset()
gReader.seek(32)
gReader.writeUInt32(c)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = 'GMD Extractor (DMC4SE)', epilog = 'Work in progress')
parser.add_argument("filename", help = "File")
parser.add_argument("-p", "--pack", help = "Pack file", default=False, action = argparse.BooleanOptionalAction)
args = parser.parse_args()
text(args.filename, args.pack)