-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTES4Record.cpp
More file actions
357 lines (324 loc) · 10.5 KB
/
Copy pathTES4Record.cpp
File metadata and controls
357 lines (324 loc) · 10.5 KB
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is CBash code.
*
* The Initial Developer of the Original Code is
* Waruddar.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "Common.h"
#include "TES4Record.h"
#include "zlib/zlib.h"
TES4Record::TES4HEDR::TES4HEDR(FLOAT32 _version, UINT32 _numRecords, UINT32 _nextObject):
version(_version),
numRecords(_numRecords),
nextObject(_nextObject)
{
//
}
TES4Record::TES4HEDR::~TES4HEDR()
{
//
}
bool TES4Record::TES4HEDR::operator ==(const TES4HEDR &other) const
{
return (AlmostEqual(version,other.version,2) &&
numRecords == other.numRecords &&
nextObject == other.nextObject);
}
bool TES4Record::TES4HEDR::operator !=(const TES4HEDR &other) const
{
return !(*this == other);
}
TES4Record::TES4Record(unsigned char *_recData):
Record(_recData),
formVersion(0)
{
memset(&versionControl2[0], 0x00, 2);
}
TES4Record::TES4Record(TES4Record *srcRecord):
Record()
{
if(srcRecord == NULL)
return;
flags = srcRecord->flags;
formID = srcRecord->formID;
flagsUnk = srcRecord->flagsUnk;
formVersion = srcRecord->formVersion;
versionControl2[0] = srcRecord->versionControl2[0];
versionControl2[1] = srcRecord->versionControl2[1];
recData = srcRecord->recData;
if(!srcRecord->IsChanged())
return;
//EDID = srcRecord->EDID;
HEDR = srcRecord->HEDR;
OFST = srcRecord->OFST;
DELE = srcRecord->DELE;
CNAM = srcRecord->CNAM;
SNAM = srcRecord->SNAM;
MAST.resize(srcRecord->MAST.size());
for(UINT32 x = 0; x < srcRecord->MAST.size();++x)
{
UINT32 size = (UINT32)strlen(srcRecord->MAST[x]) + 1;
MAST[x] = new char[size];
memcpy(MAST[x], srcRecord->MAST[x], size);
}
ONAM = srcRecord->ONAM;
SCRN = srcRecord->SCRN;
INTV = srcRecord->INTV;
PURG = srcRecord->PURG;
return;
}
TES4Record::~TES4Record()
{
//
}
bool TES4Record::VisitFormIDs(FormIDOp &op)
{
if(!IsLoaded())
return false;
for(UINT32 x = 0; x < ONAM.value.size(); x++)
op.Accept(ONAM.value[x]);
return op.Stop();
}
bool TES4Record::IsESM()
{
if(!IsLoaded()) return false;
return (flags & fIsESM) != 0;
}
void TES4Record::IsESM(bool value)
{
if(!IsLoaded()) return;
flags = value ? (flags | fIsESM) : (flags & ~fIsESM);
}
UINT32 TES4Record::GetType()
{
return REV32(TES4);
}
STRING TES4Record::GetStrType()
{
return "TES4";
}
SINT32 TES4Record::ParseRecord(unsigned char *buffer, unsigned char *end_buffer, bool CompressedOnDisk)
{
UINT32 subType = 0;
UINT32 subSize = 0;
while(buffer < end_buffer){
subType = *(UINT32 *)buffer;
buffer += 4;
switch(subType)
{
case REV32(XXXX):
buffer += 2;
subSize = *(UINT32 *)buffer;
buffer += 4;
subType = *(UINT32 *)buffer;
buffer += 6;
break;
default:
subSize = *(UINT16 *)buffer;
buffer += 2;
break;
}
switch(subType)
{
case REV32(HEDR):
HEDR.Read(buffer, subSize);
break;
case REV32(CNAM):
CNAM.Read(buffer, subSize, CompressedOnDisk);
break;
case REV32(SNAM):
SNAM.Read(buffer, subSize, CompressedOnDisk);
break;
case REV32(MAST):
MAST.push_back(new char[subSize]);
memcpy(MAST.back(), buffer, subSize);
buffer += subSize;
break;
case REV32(DATA):
buffer += subSize;
break;
case REV32(OFST):
OFST.Read(buffer, subSize, CompressedOnDisk);
break;
case REV32(DELE):
DELE.Read(buffer, subSize, CompressedOnDisk);
break;
/* Fall-Out ------------------------------------- */
case REV32(ONAM):
ONAM.Read(buffer, subSize);
break;
case REV32(SCRN):
SCRN.Read(buffer, subSize, CompressedOnDisk);
break;
/* Skyrim --------------------------------------- */
case REV32(INTV): // 4 bytes
INTV.Read(buffer, subSize, CompressedOnDisk);
break;
default:
//printer("FileName = %s\n", FileName);
printer(" TES4: %08X - Unknown subType = %04x [%c%c%c%c]\n", formID, subType, (subType >> 0) & 0xFF, (subType >> 8) & 0xFF, (subType >> 16) & 0xFF, (subType >> 24) & 0xFF);
printer(" Size = %i\n", subSize);
printer(" CurPos = %04x\n\n", buffer - 6);
buffer = end_buffer;
break;
}
};
return 0;
}
SINT32 TES4Record::Unload()
{
//TES4 should never be unloaded, so do nothing
return 1;
}
SINT32 TES4Record::WriteRecord(FileWriter &writer)
{
UINT8 DATA[8] = {0};
switch(whichGame)
{
case eIsOblivion:
WRITE(HEDR);
WRITE(OFST);
WRITE(DELE);
WRITE(CNAM);
WRITE(SNAM);
for(UINT32 p = 0; p < MAST.size(); p++)
{
writer.record_write_subrecord(REV32(MAST), MAST[p], (UINT32)strlen(MAST[p]) + 1);
writer.record_write_subrecord(REV32(DATA), &DATA[0], sizeof(DATA));
}
break;
case eIsFallout3:
printer("TES4Record::WriteRecord: Error - Unable to write TES4 record. Fallout 3 support not yet implemented.\n");
return -1;
case eIsFalloutNewVegas:
WRITE(HEDR);
WRITE(OFST);
WRITE(DELE);
WRITE(CNAM);
WRITE(SNAM);
for(UINT32 p = 0; p < MAST.size(); p++)
{
writer.record_write_subrecord(REV32(MAST), MAST[p], (UINT32)strlen(MAST[p]) + 1);
writer.record_write_subrecord(REV32(DATA), &DATA[0], sizeof(DATA));
}
WRITE(ONAM);
WRITE(SCRN);
break;
case eIsSkyrim:
WRITE(HEDR);
WRITE(OFST);
WRITE(DELE);
WRITE(CNAM);
WRITE(SNAM);
for(UINT32 p = 0; p < MAST.size(); p++)
{
writer.record_write_subrecord(REV32(MAST), MAST[p], (UINT32)strlen(MAST[p]) + 1);
writer.record_write_subrecord(REV32(DATA), &DATA[0], sizeof(DATA));
}
WRITE(INTV);
WRITE(PURG);
break;
}
return -1;
}
UINT32 TES4Record::Write(FileWriter &writer, const bool &bMastersChanged, FormIDResolver &expander, FormIDResolver &collapser, std::vector<FormIDResolver *> &Expanders)
{
IsCompressed(false);
UINT32 recSize = 0;
UINT32 recType = GetType();
collapser.Accept(formID);
VisitFormIDs(collapser);
WriteRecord(writer);
recSize = writer.record_size();
writer.file_write(&recType, 4);
writer.file_write(&recSize, 4);
writer.file_write(&flags, 4);
writer.file_write(&formID, 4);
writer.file_write(&flagsUnk, 4);
if(whichGame == eIsFalloutNewVegas)
{
writer.file_write(&formVersion, 2);
writer.file_write(&versionControl2[0], 2);
}
else if(whichGame == eIsSkyrim)
{
writer.file_write(&formVersion, 2);
writer.file_write(&versionControl2[0], 2);
}
writer.record_flush();
expander.Accept(formID);
if(IsChanged())
VisitFormIDs(expander);
else
Unload();
if(whichGame == eIsFalloutNewVegas)
return recSize + 24;
else if(whichGame == eIsSkyrim)
return recSize + 24;
else
return recSize + 20;
}
bool TES4Record::operator ==(const TES4Record &other) const
{
if(OFST == other.OFST &&
DELE == other.DELE &&
CNAM.equals(other.CNAM) &&
SNAM.equals(other.SNAM) &&
formVersion == other.formVersion &&
versionControl2[0] == other.versionControl2[0] &&
versionControl2[1] == other.versionControl2[1] &&
SCRN == other.SCRN &&
MAST.size() == other.MAST.size() &&
ONAM == other.ONAM &&
INTV == other.INTV &&
PURG == other.PURG)
{
//Record order kinda sorta but doesn't really matter on masters, so equality testing is easy
//The order determines the mod index of all formIDs in the mod file
//If both records have the same masters in the same orders, the formIDs will have the same master indexing
//If both records have the same masters in different orders, the formIDs will have different indexing but be logically equivalent
//The ordering has no effect on load order in game or in the editor
for(UINT32 x = 0; x < MAST.size(); ++x)
if(icmps(MAST[x], other.MAST[x]) != 0)
return false;
return true;
}
return false;
}
bool TES4Record::operator !=(const TES4Record &other) const
{
return !(*this == other);
}
bool TES4Record::equals(Record *other)
{
return *this == *(TES4Record *)other;
}