-
Notifications
You must be signed in to change notification settings - Fork 4
/
TTMFile.cpp
467 lines (385 loc) · 12.7 KB
/
TTMFile.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "TTMFile.h"
SCRANTIC::TTMFile::TTMFile(const std::string &name, v8 &data)
: CompressedBaseFile(name) {
initMnemonics();
v8::iterator it = data.begin();
assertString(it, "VER:");
readUintLE(it, verSize);
version = readString(it, verSize-1);
assertString(it, "PAG:");
readUintLE(it, pagSize);
readUintLE(it, pag);
assertString(it, "TT3:");
if (!handleDecompression(data, it, rawScript)) {
return;
}
std::advance(it, compressedSize);
assertString(it, "TTI:");
readUintLE(it, fullTagSize);
readUintLE(it, magic);
assertString(it, "TAG:");
readUintLE(it, tagSize);
readUintLE(it, tagCount);
u16 id;
std::string desc;
for (u16 i = 0; i < tagCount; ++i) {
readUintLE(it, id);
desc = readString(it);
tagList.insert(std::pair<u16, std::string>(id, desc));
}
if (!rawScript.size()) {
return;
}
parseRawScript();
}
SCRANTIC::TTMFile::TTMFile(const std::string& filename)
: CompressedBaseFile(filename) {
initMnemonics();
std::ifstream in;
in.open(filename, std::ios::in);
if (!in.is_open()) {
std::cerr << "TTMFile: Could not open " << filename << std::endl;
return;
}
u16 i, j, k, l, m, n;
std::string line;
std::string tag;
std::string mnemonic;
u16 id, opcode;
int count;
std::string mode;
while (getline(in, line)) {
if (line.substr(0, 1) == "#" || line == "") {
continue;
}
if (line == "VERSION") {
getline(in, line);
version = line;
continue;
}
if (line == "TAGS") {
mode = line;
continue;
}
if (line == "SCRIPT") {
mode = line;
continue;
}
if (mode == "TAGS") {
std::istringstream iss(line);
if (!(iss >> std::hex >> id)) {
break;
}
tag = line.substr(line.find(" ")+1);
tagList.insert({id, tag});
} else if (mode == "SCRIPT") {
mnemonic = line.substr(0, line.find(" "));
opcode = getOpcodeFromMnemonic(mnemonic);
count = getParamCount(opcode);
opcode |= count;
writeUintLE(rawScript, opcode);
std::istringstream iss(line);
switch (count) {
case 1:
iss >> mnemonic >> std::hex >> i;
writeUintLE(rawScript, i);
break;
case 2:
iss >> mnemonic >> std::hex >> i >> j;
writeUintLE(rawScript, i);
writeUintLE(rawScript, j);
break;
case 4:
iss >> mnemonic >> std::hex >> i >> j >> k >> l;
writeUintLE(rawScript, i);
writeUintLE(rawScript, j);
writeUintLE(rawScript, k);
writeUintLE(rawScript, l);
break;
case 6:
iss >> mnemonic >> std::hex >> i >> j >> k >> l >> m >> n;
writeUintLE(rawScript, i);
writeUintLE(rawScript, j);
writeUintLE(rawScript, k);
writeUintLE(rawScript, l);
writeUintLE(rawScript, m);
writeUintLE(rawScript, n);
break;
case 0xF:
iss >> mnemonic >> tag;
std::copy(tag.begin(), tag.end(), std::back_inserter(rawScript));
rawScript.push_back(0);
if (tag.size() % 2 == 0) {
rawScript.push_back(0);
}
break;
default:
break;
}
}
}
in.close();
parseRawScript();
}
v8 SCRANTIC::TTMFile::repackIntoResource() {
std::string strings[5] = { "VER:", "PAG:", "TT3:", "TTI:", "TAG:" };
magic = 0x8000;
compressionFlag = 1;
v8 compressedData = RLECompress(rawScript);
uncompressedSize = rawScript.size();
compressedSize = compressedData.size() + 5;
verSize = version.size() + 1;
pagSize = 2;
pag = countUpdateInScript();
tagSize = 2;
for (auto it = tagList.begin(); it != tagList.end(); ++it) {
tagSize += 2 + it->second.size() + 1;
}
fullTagSize = tagSize + 8; // TAG to end
tagCount = tagList.size();
v8 rawData(strings[0].begin(), strings[0].end());
BaseFile::writeUintLE(rawData, verSize);
std::copy(version.begin(), version.end(), std::back_inserter(rawData));
rawData.push_back(0);
std::copy(strings[1].begin(), strings[1].end(), std::back_inserter(rawData));
BaseFile::writeUintLE(rawData, pagSize);
BaseFile::writeUintLE(rawData, pag);
std::copy(strings[2].begin(), strings[2].end(), std::back_inserter(rawData));
BaseFile::writeUintLE(rawData, compressedSize);
BaseFile::writeUintLE(rawData, compressionFlag);
BaseFile::writeUintLE(rawData, uncompressedSize);
std::copy(compressedData.begin(), compressedData.end(), std::back_inserter(rawData));
compressedSize -= 5;
std::copy(strings[3].begin(), strings[3].end(), std::back_inserter(rawData));
BaseFile::writeUintLE(rawData, fullTagSize);
BaseFile::writeUintLE(rawData, magic);
std::copy(strings[4].begin(), strings[4].end(), std::back_inserter(rawData));
BaseFile::writeUintLE(rawData, tagSize);
BaseFile::writeUintLE(rawData, tagCount);
for (auto it = tagList.begin(); it != tagList.end(); ++it) {
BaseFile::writeUintLE(rawData, it->first);
std::copy(it->second.begin(), it->second.end(), std::back_inserter(rawData));
rawData.push_back(0);
}
return rawData;
}
void SCRANTIC::TTMFile::parseRawScript() {
v8::iterator it = rawScript.begin();
u16 opcode;
u16 word, scene;
u8 length;
std::map<u16, std::string>::iterator tagIt;
scene = 0;
while (it != rawScript.end()) {
readUintLE(it, opcode);
length = (opcode & 0x000F);
Command command;
command.opcode = (opcode & 0xFFF0);
if ((command.opcode == CMD_SET_SCENE) || (command.opcode == CMD_SET_SCENE_LABEL)) { // && (length == 1)) // tag
readUintLE(it, word);
command.data.push_back(word);
tagIt = tagList.find(word);
if (tagIt != tagList.end()) {
command.name = tagIt->second;
}
} else if (length == 0xF) { //string
command.name = readString(it);
if ((command.name.length() % 2) == 0) { // align to word
readUintLE(it, length);
}
} else {
for (u8 i = 0; i < length; ++i) {
readUintLE(it, word);
command.data.push_back(word);
}
}
if (command.opcode == CMD_SET_SCENE_LABEL) {
Command c;
c.opcode = CMD_JMP_SCENE;
c.data.push_back(command.data.at(0));
c.data.push_back(0xFFFF);
script[scene].push_back(c);
}
if ((command.opcode == CMD_SET_SCENE) || (command.opcode == CMD_SET_SCENE_LABEL)) {
scene = command.data.at(0);
}
script[scene].push_back(command);
}
}
void SCRANTIC::TTMFile::saveFile(const std::string &path) {
std::stringstream output;
output << "# SCRANTIC TTM file" << std::endl;
output << "VERSION" << std::endl;
output << version << std::endl << std::endl;
output << "TAGS" << std::endl;
for (auto it = tagList.begin(); it != tagList.end(); ++it) {
output << hexToString(it->first, std::hex, 4) << " " << it->second << std::endl;
}
output << std::endl << "SCRIPT" << std::endl;
v8::iterator it = rawScript.begin();
u16 opcode;
u8 length;
u16 word;
while (it != rawScript.end()) {
readUintLE(it, opcode);
length = (opcode & 0x000F);
Command command;
command.opcode = (opcode & 0xFFF0);
if (command.opcode == CMD_SET_SCENE || command.opcode == CMD_SET_SCENE_LABEL) {
output << "# new scene" << std::endl;
}
if (length == 0xF) { //string
command.name = readString(it);
if ((command.name.length() % 2) == 0) { // align to word
readUintLE(it, length);
}
} else {
for (u8 i = 0; i < length; ++i) {
readUintLE(it, word);
command.data.push_back(word);
}
}
output << getMnemoic(command) << std::endl;
}
writeFile(output.str(), filename, path);
}
std::vector<SCRANTIC::Command> SCRANTIC::TTMFile::getFullScene(u16 num)
{
std::map<u16, std::vector<Command> >::iterator it = script.find(num);
if (it == script.end()) {
return std::vector<Command>();
}
return it->second;
}
std::string SCRANTIC::TTMFile::getTag(u16 num)
{
std::map<u16, std::string>::iterator it = tagList.find(num);
if (it == tagList.end()) {
return std::string();
}
return it->second;
}
bool SCRANTIC::TTMFile::hasInit()
{
std::map<u16, std::vector<Command> >::iterator it;
it = script.find(0);
if (it == script.end()) {
return false;
}
return true;
}
u16 SCRANTIC::TTMFile::countUpdateInScript() {
u16 count = 0;
for (auto it = script.begin(); it != script.end(); ++it) {
for (size_t i = 0; i < it->second.size(); ++i) {
if (it->second[i].opcode == CMD_UPDATE) {
++count;
}
}
}
return count;
}
void SCRANTIC::TTMFile::initMnemonics() {
//no params
mnemonics.insert({CMD_CLEAR_IMGSLOT, "CLRIMGSLOT"});
mnemonics.insert({CMD_PURGE, "PURGE"});
mnemonics.insert({CMD_UPDATE, "UPDATE"});
//1 param
mnemonics.insert({CMD_DELAY, "DELAY"});
mnemonics.insert({CMD_SEL_SLOT_IMG, "IMGSLOT"});
mnemonics.insert({CMD_SEL_SLOT_PAL, "PALSLOT"});
mnemonics.insert({CMD_SET_SCENE_LABEL, "LABEL"});
mnemonics.insert({CMD_SET_SCENE, "SCENE"});
mnemonics.insert({CMD_UNK_1120, "UNK1120"});
mnemonics.insert({CMD_JMP_SCENE, "JUMP"});
mnemonics.insert({CMD_CLEAR_RENDERER, "CLEAR"});
mnemonics.insert({CMD_PLAY_SOUND, "PLAYSND"});
//string param
mnemonics.insert({CMD_LOAD_SCREEN, "SCREEN"});
mnemonics.insert({CMD_LOAD_BITMAP, "BITMAP"});
mnemonics.insert({CMD_LOAD_PALETTE, "PALETTE"});
//2 params
mnemonics.insert({CMD_SET_COLOR, "SELCOLOR"});
mnemonics.insert({CMD_UNK_2010, "UNK2010"});
mnemonics.insert({CMD_TIMER, "TIMER"});
mnemonics.insert({CMD_DRAW_PIXEL, "PIXEL"});
//4 params
mnemonics.insert({CMD_CLIP_REGION, "SETCLIP"});
mnemonics.insert({CMD_SAVE_IMAGE, "SAVEIMG"});
mnemonics.insert({CMD_SAVE_IMAGE_NEW, "SAVENEWIMG"});
mnemonics.insert({CMD_UNK_A050, "UNKA050"});
mnemonics.insert({CMD_UNK_A060, "UNKA060"});
mnemonics.insert({CMD_DRAW_LINE, "LINE"});
mnemonics.insert({CMD_DRAW_RECTANGLE, "RECTANGLE"});
mnemonics.insert({CMD_DRAW_ELLIPSE, "ELLIPSE"});
mnemonics.insert({CMD_DRAW_SPRITE, "SPRITE"});
mnemonics.insert({CMD_DRAW_SPRITE_MIRROR, "SPRITEHINV"});
//6 params
mnemonics.insert({CMD_UNK_B600, "UNKB600"});
}
std::string SCRANTIC::TTMFile::getMnemoic(SCRANTIC::Command c) {
std::string result = mnemonics[c.opcode];
int count = getParamCount(c.opcode);
if (!count) {
return result;
}
if (count == 0xF) {
return result + " " + c.name;
}
for (int i = 0; i < count; ++i) {
std::string data = hexToString(c.data.at(i), std::hex, 4);
result += " " + data;
}
return result;
}
u16 SCRANTIC::TTMFile::getOpcodeFromMnemonic(std::string &mnemonic) {
for (auto it = mnemonics.begin(); it != mnemonics.end(); ++it) {
if (it->second == mnemonic) {
return it->first;
}
}
return 0;
}
int SCRANTIC::TTMFile::getParamCount(u16 opcode) {
switch (opcode) {
case CMD_CLEAR_IMGSLOT:
case CMD_PURGE:
case CMD_UPDATE:
return 0;
case CMD_DELAY:
case CMD_SEL_SLOT_IMG:
case CMD_SEL_SLOT_PAL:
case CMD_SET_SCENE_LABEL:
case CMD_SET_SCENE:
case CMD_UNK_1120:
case CMD_JMP_SCENE:
case CMD_CLEAR_RENDERER:
case CMD_PLAY_SOUND:
return 1;
case CMD_LOAD_SCREEN:
case CMD_LOAD_BITMAP:
case CMD_LOAD_PALETTE:
return 0xF;
case CMD_SET_COLOR:
case CMD_UNK_2010:
case CMD_TIMER:
case CMD_DRAW_PIXEL:
return 2;
case CMD_CLIP_REGION:
case CMD_SAVE_IMAGE:
case CMD_SAVE_IMAGE_NEW:
case CMD_UNK_A050:
case CMD_UNK_A060:
case CMD_DRAW_LINE:
case CMD_DRAW_RECTANGLE:
case CMD_DRAW_ELLIPSE:
case CMD_DRAW_SPRITE:
case CMD_DRAW_SPRITE_MIRROR:
return 4;
case CMD_UNK_B600:
return 6;
default:
std::cerr << "Unknown command found: " << opcode << std::endl;
return 0;
}
}