-
Notifications
You must be signed in to change notification settings - Fork 0
/
RE_MSG.bt
289 lines (238 loc) · 9.03 KB
/
RE_MSG.bt
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
//------------------------------------------------
//--- 010 Editor v12.0.1 Binary Template
//
// File: .msg.12, 0x2022033D(539099965), 14, 15, 17, 20, 0x20220626(539100710), 22
// Authors: dtlnor (Thanks to wwylele's mhrice and ponaromixxx's msg tool)
// Version: 1.1.0
// Purpose: To edit ReEngine's msg file
// Category:
// File Mask: *.msg.\d+
// ID Bytes:
// History:
//------------------------------------------------
// change value of this to view in other lang, ref to LanguagesEnum.md
// (while for debug msg file, sometimes it has japanese contents only, so default to 0)
local int defaultContentPreviewLangCode = 0;
// ~~Set to false to disable string search BEFORE decrypt (when your msg file is large),~~
// ~~otherwise it waste many time to read garbage from entire encrypted part again and again(cuz no null terminator).~~
// This will now be force to set by user input from MessageBox return.
local int isDecrypted = false;
local int i, j = 0;
// Utility method and struct
typedef struct (uint64 offset) {
if (isDecrypted) {
local int p = FTell();
FSeek(offset);
wstring WString<bgcolor=0x080080>;
FSeek(p);
} else {
local wstring WString = "";
}
} SeekString <read=ReadSeekString>;
wstring ReadSeekString (SeekString &r) { return r.WString; }
typedef struct {
uint64 offset<bgcolor=0xAABBCC>;
if (isDecrypted) {
local int p = FTell();
FSeek(offset);
wstring WString<bgcolor=0x080080>;
FSeek(p);
} else {
local wstring WString = "";
}
} ReadAttrString<read=ReadReadAttrString> ;
wstring ReadReadAttrString (ReadAttrString &r) { return r.WString; }
void CheckPosition(uint64 target, string hint){
if (target != FTell())
Printf(hint+" not match\n");
FSeek(target);
}
int IsVersionEncrypt(uint32 version){
return (version > 12) && (version != 0x2022033D);
}
int IsVersionEntryByHash(uint32 version){
return (version > 15) && (version != 0x2022033D);
}
// stolen GUID from RE_RSZ
typedef struct {
uchar uuid[16] <open=suppress>;
} rGUID <read=ReadrGUID, write=WriterGuid>;
string TranslateGUID (uchar uuid[]) {
local char s[37];
SPrintf(s,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuid[3], uuid[2], uuid[1], uuid[0], uuid[5], uuid[4], uuid[7], uuid[6],
uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
);
return s;
}
string ReadrGUID (rGUID &g) {
local string Guid = TranslateGUID(g.uuid);
return Guid;
}
void WriterGuid (rGUID &g, string s) {
local string out;
local byte ii, offset;
local uchar uuid[16];
for (ii=0; ii<16; ii++) {
if (ii==4 || ii== 6 || ii==8 || ii==10)
offset++;
SScanf(SubStr(s, ii*2 + offset, 2), "%x", uuid[ii]);
}
local uchar uuid_le[16] = {
uuid[3], uuid[2], uuid[1], uuid[0], uuid[5], uuid[4], uuid[7], uuid[6],
uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
};
WriteBytes(uuid_le, startof(g), 16);
}
// Header of GMSG
struct {
uint32 version<format=hex>;
char magic[4]; //GMSG
uint64 headerOffset; //0x10
uint32 entryCount;
uint32 attributeCount;
uint32 langCount;
uint32 padding<bgcolor=0x000000,hidden=true>; //align to 8
if (IsVersionEncrypt(version)){
uint64 dataOffset; // encrypted string pool
}
uint64 unknZeroOffset;
uint64 langOffset;
uint64 attributeOffset;
uint64 attributeNameOffset;
} Header <bgcolor=0xFFFFFF>;
if (IsVersionEncrypt(Header.version)){
if( MessageBox( idYes | idNo, "isDecrypted?", "Does your file been decrypted by RE_MSG_proc.1sc?") == idYes )
isDecrypted = true;
}else{
isDecrypted = true;
}
uint64 entryOffsets[Header.entryCount]<bgcolor=0x00FFFF>;
CheckPosition(Header.unknZeroOffset, "Header.unknZeroOffset");
uint64 unknZero<bgcolor=0x404040>;
if (unknZero != 0) Printf("unknZero Expected 0\n");
CheckPosition(Header.langOffset, "Header.langOffset");
uint32 languages[Header.langCount]<bgcolor=0xFF00FF>;
ubyte padding1[FTell() % 8]<bgcolor=0x000000,hidden=true>;
CheckPosition(Header.attributeOffset, "Header.attributeOffset");
int32 attributeValueTypes[Header.attributeCount]<bgcolor=0xFFFF00>;
ubyte padding2[FTell() % 8]<bgcolor=0x000000,hidden=true>;
CheckPosition(Header.attributeNameOffset, "Header.attributeNameOffset");
uint64 attributeNamesOffset[Header.attributeCount]<bgcolor=0xAA44FF>;
struct {
local uint attrIdx = 0;
for (attrIdx = 0; attrIdx < Header.attributeCount; attrIdx++){
SeekString attributeName(attributeNamesOffset[attrIdx]);
}
} attributeNames;
typedef struct{
rGUID uuid<bgcolor=0xFF0000>;
uint unkn<bgcolor=0xFF0000, format=hex, comment="CRC ???">;
if (IsVersionEntryByHash(Header.version)){
uint hash<bgcolor=0xFF0000, format=hex, comment="should be the murmurhash3 of the entry Name">;
}else{
uint index<bgcolor=0xFF0000, comment="entry index">;
}
uint64 entryNameOffset<bgcolor=0xFF4400>; //name
uint64 attributeOffset<bgcolor=0xFF0044>; //attributes
uint64 contentOffsetsByLangs[Header.langCount]<bgcolor=0xFF4444>; //content
} SubEntryHeader;
// entryOffsets
struct {
local uint entry = 0;
for (entry = 0; entry < Header.entryCount; entry++){
CheckPosition(entryOffsets[entry], "entryOffsets[entry]");
SubEntryHeader subEntryHead;
}
} EntryTbl;
typedef struct (uint entry){
local uint attrIdx = 0;
CheckPosition(EntryTbl.subEntryHead[entry].attributeOffset, "EntryTbl.subEntryHead[entry].attributeOffset");
for (attrIdx = 0; attrIdx < Header.attributeCount; attrIdx++) {
switch (attributeValueTypes[attrIdx]){
case -1: ReadAttrString attributeValue(); break;
case 0: int64 attributeValue<bgcolor=0xAABBCC>; break;
case 1: double attributeValue<bgcolor=0xAABBCC>; break;
case 2: ReadAttrString attributeValue(); break;
default: Printf("attribute Value DataType %d not implemented\n", attributeValueTypes[attrIdx]); break; //attributeOffsets
}
}
} Attribute;
typedef struct {
local uint langIndex = 0;
for (langIndex = 0; langIndex < Header.langCount; langIndex++) {
SeekString textContent(EntryTbl.subEntryHead[entry].contentOffsetsByLangs[langIndex]); //content
}
} ContentByLangs<read=ReadContentByLangs>;
wstring ReadContentByLangs (ContentByLangs &r) { return r.textContent[defaultContentPreviewLangCode].WString; }
// better readability
struct {
local uint entry = 0;
// struct {
// for (entry = 0; entry < Header.entryCount; entry++){
// Attribute attribute(entry);
// }
// } Attributes;
if (IsVersionEncrypt(Header.version)){
FSeek(Header.dataOffset);
// CheckPosition(Header.dataOffset, "dataOffset");
local uint64 dataSize = FileSize() - Header.dataOffset;
ubyte dataStream[dataSize]<hidden=true>; //data
}
// struct {
// for (entry = 0; entry < Header.entryCount; entry++){
// SeekString entryName(EntryTbl.subEntryHead[entry].entryNameOffset)<comment="should match the hash(utf16)">; //name
// }
// } EntryNames;
//struct {
for (entry = 0; entry < Header.entryCount; entry++){
SeekString entryName(EntryTbl.subEntryHead[entry].entryNameOffset)<comment="should match the hash(utf16)">; //name
ContentByLangs contentByLangs;
FSeek(EntryTbl.subEntryHead[entry].attributeOffset);
Attribute attribute(entry);
}
//} Contents;
}EntryContents;
// sequential read for debug use
/*
struct {
local uint entry = 0;
struct {
for (entry = 0; entry < Header.entryCount; entry++){
Attribute attribute(entry);
}
} Attributes;
if (IsVersionEncrypt(Header.version)){
// FSeek(Header.dataOffset);
CheckPosition(Header.dataOffset, "dataOffset");
local uint64 dataSize = FileSize() - Header.dataOffset;
ubyte dataStream[dataSize]<hidden=true>; //data
}
struct {
for (entry = 0; entry < Header.entryCount; entry++){
SeekString entryName(EntryTbl.subEntryHead[entry].entryNameOffset)<comment="should match the hash(utf16)">; //name
}
} EntryNames;
struct {
for (entry = 0; entry < Header.entryCount; entry++){
// SeekString entryName(EntryTbl.subEntryHead[entry].entryNameOffset)<comment="should match the hash(utf16)">; //name
ContentByLangs contentByLangs;
// FSeek(EntryTbl.subEntryHead[entry].attributeOffset);
// Attribute attribute(entry);
}
} Contents;
}EntryContents;
*/
// Show all string, debug use
/*
if (isDecrypted){
FSeek(Header.dataOffset);
struct{
while (!FEof()){
struct{
wstring WString;
}item<read=this.WString>;
};
}WStringPool;
}*/