-
Notifications
You must be signed in to change notification settings - Fork 2
/
AtlasFile.cpp
596 lines (508 loc) · 13.3 KB
/
AtlasFile.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "stdafx.h"
#include <cstdio>
#include <string>
#include "AtlasFile.h"
#include "Table.h"
#include "AtlasLogger.h"
#include "AtlasStats.h"
#include "AtlasCore.h"
#include "AtlasExtension.h"
using namespace std;
AtlasFile::AtlasFile()
{
tfile = NULL;
pfile = NULL;
MaxScriptPos = -1;
ActiveTbl = NULL;
BytesInserted = 0;
TotalBytes = 0;
TotalBytesSkipped = 0;
StrType = STR_ENDTERM;
PascalLength = 1;
FixedPadValue = 0;
StringLength = 0;
FillBlocks = false;
FillValue = 0;
}
AtlasFile::~AtlasFile()
{
if (tfile != NULL)
fclose(tfile);
if (pfile != NULL)
fclose(pfile);
}
bool AtlasFile::OpenFileT(const char* Filename)
{
// Reset vars for new file
MaxScriptPos = -1;
// Try to open existing first
tfile = fopen(Filename, "rb+");
// If failed, then create new file
if (tfile == NULL)
{
tfile = fopen(Filename, "wb+");
}
return tfile != NULL;
}
bool AtlasFile::OpenFileP(const char* Filename)
{
// Try to open existing first
pfile = fopen(Filename, "rb+");
// If failed, then create new file
if (pfile == NULL)
pfile = fopen(Filename, "wb+");
return pfile != NULL;
}
void AtlasFile::CloseFileT()
{
if (tfile != NULL)
fclose(tfile);
}
void AtlasFile::CloseFileP()
{
if (pfile != NULL)
fclose(pfile);
}
void AtlasFile::WriteP(const void* Data, const unsigned int Size,
const unsigned int DataCount, const unsigned int Pos)
{
unsigned int OldPos = ftell(pfile);
int i = fseek(pfile, Pos, SEEK_SET);
int j = fwrite(Data, Size, DataCount, pfile);
fseek(pfile, OldPos, SEEK_SET);
}
void AtlasFile::WriteT(const void* Data, const unsigned int Size,
const unsigned int DataCount, const unsigned int Pos)
{
unsigned int OldPos = ftell(tfile);
int i = fseek(tfile, Pos, SEEK_SET);
fwrite(Data, Size, DataCount, tfile);
fseek(tfile, OldPos, SEEK_SET);
}
// Does not revert file offset
void AtlasFile::WriteT(const void* Data, const unsigned int Size, const unsigned int DataCount)
{
fwrite(Data, Size, DataCount, tfile);
}
unsigned int AtlasFile::GetPosT()
{
return ftell(tfile);
}
FILE* AtlasFile::GetFileT()
{
return tfile;
}
void AtlasFile::FillT(const unsigned char Data, const unsigned int DataCount)
{
unsigned char* DataArray = new unsigned char[DataCount];
memset(DataArray, Data, DataCount);
fwrite(DataArray, 1, DataCount, tfile);
delete[] DataArray;
}
int AtlasFile::InsertBinaryT(string Filename, const unsigned int StartOffset, const unsigned int Size)
{
unsigned int copysize;
unsigned int filesize;
FILE* f = fopen(Filename.c_str(), "rb");
if (f == NULL)
{
Logger.ReportError(__LINE__, "INSERTBINARY Could not open file %s for reading", Filename);
return 0;
}
fseek(f, 0, SEEK_END);
filesize = ftell(f);
if (Size == 0) // 0 param means to copy remainder of file
copysize = filesize - StartOffset;
else
{
copysize = Size;
if ((StartOffset + copysize) > filesize) // User requested to copy too many bytes, warn and copy to end of file
{
Logger.ReportWarning(CurrentLine, "INSERTBINARY User requested to copy more bytes than available, size truncated to end of file");
copysize = filesize - StartOffset;
}
}
fseek(f, StartOffset, SEEK_SET);
unsigned char* data = new unsigned char[copysize];
fread(data, 1, copysize, f);
fclose(f);
WriteT(data, 1, copysize);
delete[] data;
return copysize;
}
FILE* AtlasFile::GetFileP()
{
return pfile;
}
void AtlasFile::FillBlock()
{
if (MaxScriptPos == -1 || !FillBlocks)
return;
int CurPos = GetPosT();
int EndPos = MaxScriptPos;
int Size = EndPos - CurPos + 1; // ScriptBound is inclusive, so we need to write one more byte
FillT(FillValue, Size);
}
void AtlasFile::GetScriptBuf(std::list<TBL_STRING>& Strings)
{
Strings = ActiveTbl->StringTable;
}
void AtlasFile::SetScriptBuf(std::list<TBL_STRING>& Strings)
{
ActiveTbl->StringTable = Strings;
}
unsigned int AtlasFile::GetStringType()
{
return StrType;
}
void AtlasFile::MoveT(const unsigned int Pos, const unsigned int ScriptBound)
{
if (tfile)
{
FillBlock();
fseek(tfile, Pos, SEEK_SET);
MaxScriptPos = ScriptBound;
}
}
void AtlasFile::MoveT(const unsigned int Pos)
{
if (tfile)
{
FillBlock();
fseek(tfile, Pos, SEEK_SET);
MaxScriptPos = -1;
}
}
void AtlasFile::SetTable(Table* Tbl)
{
FlushText();
ActiveTbl = Tbl;
}
bool AtlasFile::SetStringType(std::string& Type)
{
for (int i = 0; i < StringTypeCount; i++)
{
if (Type == StringTypes[i])
{
StrType = i;
return true;
}
}
return false;
}
bool AtlasFile::SetPascalLength(unsigned int Length)
{
switch (Length)
{
case 1: case 2: case 3: case 4:
PascalLength = Length;
break;
default:
return false;
}
return true;
}
bool AtlasFile::SetFixedLength(unsigned int StrLength, unsigned int PadValue)
{
if (PadValue > 65536)
return false;
StringLength = StrLength;
FixedPadValue = (unsigned char)PadValue;
return true;
}
void AtlasFile::EnableFillBlock(unsigned char FillByte)
{
FillBlocks = true;
FillValue = FillByte;
}
bool AtlasFile::DisableWrite(string& EndTag, bool isPointerTable)
{
if (isPointerTable)
{
std::map<string, PointerTable*>::iterator it;
it = TblAutoWrite.find(EndTag);
if (it == TblAutoWrite.end())
{
Logger.ReportError(CurrentLine, "'%s' is not currently an enabled AUTOWRITE end token", EndTag);
return false;
}
TblAutoWrite.erase(it);
}
else
{
std::map<string, PointerList*>::iterator it;
it = ListAutoWrite.find(EndTag);
if (it == ListAutoWrite.end())
{
Logger.ReportError(CurrentLine, "'%s' is not currently an enabled AUTOWRITE end token", EndTag);
return false;
}
ListAutoWrite.erase(it);
}
return true;
}
bool AtlasFile::DisableAutoExtension(string& FuncName, string& EndTag)
{
std::map<string, ExtensionFunction>::iterator it;
it = ExtAutoWrite.find(EndTag);
if (it == ExtAutoWrite.end())
{
Logger.ReportError(CurrentLine, "'%s' has not been defined as an autoexec end token", EndTag);
return false;
}
ExtAutoWrite.erase(it);
return true;
}
bool AtlasFile::AutoWrite(PointerList* List, string& EndTag)
{
bool EndTokenFound = false;
if (ActiveTbl == NULL)
{
Logger.ReportError(CurrentLine, "You must enable a table with #ACTIVETBL before using #AUTOWRITE");
return false;
}
for (size_t i = 0; i < ActiveTbl->EndTokens.size(); i++)
{
if (EndTag == ActiveTbl->EndTokens[i])
EndTokenFound = true;
}
if (EndTokenFound)
ListAutoWrite.insert(map<string, PointerList*>::value_type(EndTag, List));
return EndTokenFound;
}
bool AtlasFile::AutoWrite(PointerTable* Tbl, string& EndTag)
{
bool EndTokenFound = false;
if (ActiveTbl == NULL)
{
Logger.ReportError(CurrentLine, "You must enable a table with #ACTIVETBL before using #AUTOWRITE");
return false;
}
for (size_t i = 0; i < ActiveTbl->EndTokens.size(); i++)
{
if (EndTag == ActiveTbl->EndTokens[i])
EndTokenFound = true;
}
if (EndTokenFound)
TblAutoWrite.insert(map<string, PointerTable*>::value_type(EndTag, Tbl));
return EndTokenFound;
}
bool AtlasFile::AutoWrite(AtlasExtension* Ext, string& FuncName, string& EndTag)
{
bool EndTokenFound = false;
ExtensionFunction Func;
if (ActiveTbl == NULL)
{
Logger.ReportError(CurrentLine, "You must enable a table with #ACTIVETBL before using #AUTOWRITE");
return false;
}
for (size_t i = 0; i < ActiveTbl->EndTokens.size(); i++)
{
if (EndTag == ActiveTbl->EndTokens[i])
EndTokenFound = true;
}
Func = Ext->GetFunction(FuncName);
if (!EndTokenFound)
{
Logger.ReportError(CurrentLine, "'%s' has not been defined as an end token in the active table", EndTag);
return false;
}
if (Func == NULL)
{
Logger.ReportError(CurrentLine, "Function 's' could not be found in the extension", FuncName);
return false;
}
ExtAutoWrite.insert(map<string, ExtensionFunction>::value_type(EndTag, Func));
return true;
}
bool AtlasFile::InsertText(string& Text, unsigned int Line)
{
if (ActiveTbl == NULL)
{
// Add error
printf("No active table loaded\n");
return false;
}
unsigned int BadCharPos = 0;
if (ActiveTbl->EncodeStream(Text, BadCharPos) == -1) // Failed insertion, char missing from tbl
{
// Add error
Logger.ReportError(Line, "Character '%c' missing from table. String = '%s'", Text[BadCharPos], Text.c_str());
return false;
}
return true;
}
bool AtlasFile::FlushText()
{
static unsigned int Size;
static unsigned int Address;
static unsigned int WritePos;
AtlasContext* Context = NULL;
if (ActiveTbl == NULL)
return false;
if (ActiveTbl->StringTable.empty())
return true;
// For every string, check autowrite/autoexec (list, table, and extension)
// Automatically write pointer if appropriate end string is found, then write the text string to ROM
ListTxtStringIt j = ActiveTbl->TxtStringTable.begin();
for (ListTblStringIt i = ActiveTbl->StringTable.begin();
i != ActiveTbl->StringTable.end(); i++, j++)
{
// #ALIGNSTRING, must do before autowrite writes a pointer
AlignString();
if (!i->EndToken.empty()) // If there's an end token, check for autowrite
{
ListIt = ListAutoWrite.find(i->EndToken);
if (ListIt != ListAutoWrite.end())
{
Address = ListIt->second->GetAddress(GetPosT(), Size, WritePos);
if (Address != -1)
{
if (bSwap)
Address = EndianSwap(Address, Size / 8);
WriteP(&Address, Size / 8, 1, WritePos);
Logger.Log("%6u AUTOWRITE Invoked ScriptPos $%X PointerPos $%X PointerValue $%08X\n", CurrentLine,
GetPosT(), WritePos, Address);
Stats.IncAutoPointerWrites();
}
else
Stats.IncFailedListWrites();
}
TblIt = TblAutoWrite.find(i->EndToken);
if (TblIt != TblAutoWrite.end())
{
Address = TblIt->second->GetAddress(GetPosT(), Size, WritePos);
if (bSwap)
Address = EndianSwap(Address, Size / 8);
WriteP(&Address, Size / 8, 1, WritePos);
Logger.Log("%6u AUTOWRITE Invoked ScriptPos $%X PointerPos $%X PointerValue $%08X\n", CurrentLine,
GetPosT(), WritePos, Address);
Stats.IncAutoPointerWrites();
}
ExtIt = ExtAutoWrite.find(i->EndToken);
if (ExtIt != ExtAutoWrite.end())
{
Atlas.CreateContext(&Context);
bool Success = Atlas.ExecuteExtensionFunction(ExtIt->second, &Context);
delete Context;
Context = NULL;
if (!Success)
{
Logger.ReportError(CurrentLine, "Autoexecuting extension with end token '%s' failed", i->EndToken);
return false;
}
else
Logger.Log("%6u AUTOEXEC Invoked ScriptPos $%X PointerPos $%X PointerValue $%08X\n", CurrentLine,
GetPosT(), WritePos, Address);
}
}
CurTextString = j->Text;
WriteString(i->Text);
Logger.Log("%s\n", CurTextString.c_str());
CurTextString.clear();
}
ActiveTbl->StringTable.clear();
return true;
}
inline bool AtlasFile::WriteString(string& text)
{
unsigned int StringSize = 0;
int PadBytes;
// Write string type
if (StrType == STR_ENDTERM)
StringSize = WriteNullString(text);
else if (StrType == STR_PASCAL)
StringSize = WritePascalString(text);
else
return false;
// #FIXEDLENGTH padding
if (StringLength != 0)
{
PadBytes = StringLength - StringSize;
if (PadBytes > 0)
{
for (int i = 0; i < PadBytes; i++)
fputc((int)FixedPadValue, tfile);
BytesInserted += PadBytes;
}
}
return true;
}
inline unsigned int AtlasFile::WriteNullString(string& text)
{
unsigned int size = (unsigned int)text.length();
unsigned int maxwrite = GetMaxWritableBytes();
Stats.AddScriptBytes(size);
// Truncate string if it overflows ROM bounds
if (maxwrite < size)
{
int overflowbytes = size - maxwrite;
TotalBytesSkipped += overflowbytes;
size = maxwrite;
}
// Truncate string if it's too long for a fixed length string
if (size > StringLength && StringLength != 0)
{
TotalBytesSkipped += (size - StringLength);
size = StringLength;
printf("Changed string length for %s to %d at %X\n", CurTextString.c_str(), StringLength, GetPosT());
}
fwrite(text.data(), 1, size, tfile);
BytesInserted += size;
return size;
}
inline unsigned int AtlasFile::WritePascalString(string& text)
{
unsigned int size = (unsigned int)text.length();
unsigned int maxwrite = GetMaxWritableBytes();
Stats.AddScriptBytes(size + PascalLength);
// Truncate string if it overflows ROM bounds
if (PascalLength > maxwrite) // PascalLength doesn't even fit
goto nowrite;
if (maxwrite < size + PascalLength) // PascalLength and maybe partial string fits
{
int overflowbytes = (size + PascalLength) - maxwrite;
TotalBytesSkipped += overflowbytes;
size = maxwrite - PascalLength;
}
// Truncate string if it's too long for a fixed length string
if (size > StringLength && StringLength != 0)
{
TotalBytesSkipped += (size - StringLength);
size = StringLength - PascalLength;
printf("Changed string length for %s to %d at %X\n", text.c_str(), StringLength, GetPosT());
}
int swaplen = size;
if (bSwap)
swaplen = EndianSwap(size, PascalLength);
fwrite(&swaplen, PascalLength, 1, tfile);
fwrite(text.c_str(), 1, size, tfile);
BytesInserted += size + PascalLength;
nowrite:
return size + PascalLength;
}
inline void AtlasFile::AlignString()
{
if (StringAlign != 0) // String align turned on
{
int curoffset = GetPosT() - Atlas.GetHeaderSize();
int PadBytes = StringAlign - (curoffset % StringAlign);
if (PadBytes == StringAlign)
PadBytes = 0;
if (PadBytes > 0)
{
for (int i = 0; i < PadBytes; i++)
fputc(0, tfile);
BytesInserted += PadBytes;
}
}
}
inline unsigned int AtlasFile::GetMaxWritableBytes()
{
if (MaxScriptPos == -1)
return -1;
unsigned int CurPos = ftell(tfile);
if (CurPos > MaxScriptPos)
return 0;
return MaxScriptPos - CurPos + 1;
}