-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNifFile.cpp
More file actions
458 lines (434 loc) · 15.2 KB
/
NifFile.cpp
File metadata and controls
458 lines (434 loc) · 15.2 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
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
#include "NifFile.h"
#include "BSfile.h"
map < UInt8, map < UInt32, NifFile* > > NifFile::RegList = map < UInt8, map < UInt32, NifFile* > >();
map <string, pair<UInt8, UInt32>* > NifFile::RegListByFilename = map <string, pair<UInt8, UInt32>* >();
vector<changeLog> NifFile::delta = vector<changeLog>();
NifFile::NifFile() : filePath(""), basePath(""), root(NULL), modID(255), nifID(-1) {
dPrintAndLog("NifFile c'tor","Default constructor");
}
NifFile::NifFile(const string& file, UInt8 modIndex, bool forEdit)
: filePath(file)
, basePath("")
, nifSEversion(g_pluginVersion)
, editable(forEdit)
, modID(modIndex)
, nifID(0)
{
dPrintAndLog("NifFile c'tor","NifFile created"+string(editable?" for editing.":" without editing."));
headerInfo = new Niflib::NifInfo();
loadNif();
if ( root ) {
dPrintAndLog("NifFile.reg","Registering \""+filePath+"\" on RegList.");
if ( RegList.find(modID) == RegList.end() )
RegList.insert( pair<UInt8,map<UInt32,NifFile*> >(modID,map<UInt32,NifFile*>()) );
UInt32 i = 0;
while ( !((RegList[modID].insert(pair<UInt32, NifFile*>(i, this))).second) )
++i;
nifID = i;
if ( editable ) {
basePath = filePath;
filePath = string(s_nifSEPath) + string((*g_dataHandler)->GetNthModName(modID)).substr(0,string((*g_dataHandler)->GetNthModName(modID)).length()-4) + string("_") + UIntToString(nifID) + ".nif";
RegListByFilename[filePath.substr(s_nifSEPathLen,filePath.length()-s_nifSEPathLen)] = new pair<UInt8, UInt32>(modID, nifID);
}
else
RegListByFilename[filePath] = new pair<UInt8, UInt32>(modID, nifID);
dPrintAndLog("NifFile.reg","Registered as #"+UIntToString(modID)+"-"+UIntToString(i)+".");
}
else
throw exception("Nif root not set; construction failed.");
}
NifFile::NifFile(const string& oriPath, const string& altPath) : filePath(oriPath), nifSEversion(0x0000001F), editable(true), modID(0xFF), nifID(0) {
dPrintAndLog("NifFile c'tor","NifFile created from deprecated function.");
headerInfo = new Niflib::NifInfo();
loadNif();
if ( root ) {
dPrintAndLog("NifFile.reg","Registering \""+altPath+"\" on RegList.");
if ( RegList.find(0xFF) == RegList.end() )
RegList.insert( pair<UInt8, map<UInt32, NifFile*> >(0xFF, map<UInt32, NifFile*>()) );
UInt32 i = 0;
while ( !((RegList[modID].insert(pair<UInt32, NifFile*>(i, this))).second) )
++i;
nifID = i;
basePath = oriPath;
filePath = altPath;
if ( !_stricmp(filePath.substr(0, s_nifSEPathLen).c_str(), s_nifSEPath) ) {
dPrintAndLog("NifFile.reg","Registered as #"+UIntToString(modID)+"-"+UIntToString(nifID)+" and \""+filePath.substr(s_nifSEPathLen)+"\".");
RegListByFilename[filePath.substr(s_nifSEPathLen)] = new pair<UInt8, UInt32>(modID, nifID);
}
else {
dPrintAndLog("NifFile.reg","Registered as #"+UIntToString(modID)+"-"+UIntToString(nifID)+" and \""+filePath+"\".");
RegListByFilename[filePath] = new pair<UInt8, UInt32>(modID, nifID);
filePath.insert(0,s_nifSEPath);
}
}
else
throw exception("Nif root not set; construction failed.");
}
NifFile::NifFile(const string& file, UInt8 modIndex, UInt32 nifIndex, bool forEdit, UInt32 nifSEv) : filePath(file), nifSEversion(nifSEv), editable(forEdit), modID(modIndex), nifID(nifIndex) {
dPrintAndLog("NifFile c'tor","Load constructor");
headerInfo = new Niflib::NifInfo();
loadNif();
if ( root ) {
dPrintAndLog("NifFile.reg","Registering \""+filePath+"\" on RegList as #"+UIntToString(modID)+"-"+UIntToString(nifID)+".");
if ( RegList.find(modID) == RegList.end() )
RegList.insert( pair<UInt8, map<UInt32, NifFile*> >(modID, map<UInt32, NifFile*>()) );
if ( (RegList[modID].insert(pair<UInt32, NifFile*>(nifIndex, this))).second ) {
nifID = nifIndex;
if ( editable ) {
basePath = filePath;
filePath = string(s_nifSEPath) + string((*g_dataHandler)->GetNthModName(modID)).substr(0,string((*g_dataHandler)->GetNthModName(modID)).length()-4) + string("_") + UIntToString(nifID) + ".nif";
RegListByFilename[filePath.substr(s_nifSEPathLen)] = new pair<UInt8, UInt32>(modID, nifID);
}
else
RegListByFilename[filePath] = new pair<UInt8, UInt32>(modID, nifID);
dPrintAndLog("NifFile.reg","Registered as #"+UIntToString(modID)+"-"+UIntToString(nifID)+".");
}
}
else
throw exception("Nif root not set; construction failed.");
}
NifFile::NifFile(const string& oriPath, const string& altPath, UInt32 nifIndex) : filePath(oriPath), nifSEversion(0x0000001F), editable(true), modID(0xFF), nifID(0) {
dPrintAndLog("NifFile c'tor","NifFile created from deprecated function.");
headerInfo = new Niflib::NifInfo();
loadNif();
if ( root ) {
dPrintAndLog("NifFile.reg","Registering \""+altPath+"\" on RegList.");
if ( RegList.find(0xFF) == RegList.end() )
RegList.insert( pair<UInt8, map<UInt32, NifFile*> >(0xFF, map<UInt32, NifFile*>()) );
nifID = nifIndex;
basePath = oriPath;
filePath = altPath;
if ( !_stricmp(filePath.substr(0, s_nifSEPathLen).c_str(), s_nifSEPath) ) {
dPrintAndLog("NifFile.reg","Registered as #"+UIntToString(modID)+"-"+UIntToString(nifID)+" and \""+filePath.substr(s_nifSEPathLen)+"\".");
RegListByFilename[filePath.substr(s_nifSEPathLen)] = new pair<UInt8, UInt32>(modID, nifID);
}
else {
dPrintAndLog("NifFile.reg","Registered as #"+UIntToString(modID)+"-"+UIntToString(nifID)+" and \""+filePath+"\".");
RegListByFilename[filePath] = new pair<UInt8, UInt32>(modID, nifID);
filePath.insert(0,s_nifSEPath);
}
}
else
throw exception("Nif root not set; construction failed.");
}
NifFile::~NifFile() {
dPrintAndLog("NifFile d'tor","Nif "+UIntToString(modID)+"-"+UIntToString(nifID)+" deleted.");
if ( RegList.find(modID) != RegList.end() ) {
if ( RegList[modID].find(nifID) != RegList[modID].end() ) {
RegListByFilename.erase(RegList[modID][nifID]->filePath);
RegList[modID][nifID] = NULL;
}
if ( RegList[modID].empty() )
RegList.erase(modID);
}
if ( headerInfo )
delete headerInfo;
}
// this calls readNif and then sets up the NifFile with the result
void NifFile::loadNif() {
dPrintAndLog("NifFile.loadNif","Loading \""+filePath+"\"!");
try {
readNif();
try {
dPrintAndLog("NifFile.loadNif", "Finding NIF root.");
findRoot();
dPrintAndLog("NifFile.loadNif", "Storing NIF header info.");
nifVersion = headerInfo->version;
dPrintAndLog("NifFile.loadNif","Nif is good; "+getVersion()+". Loaded "+UIntToString(nifSize)+" blocks.");
}
catch (exception e) {
dPrintAndLog("NifFile.loadNif","Could not find root, exception \""+string(e.what())+"\" thrown.");
loc = 0;
}
}
catch (exception& except) {
dPrintAndLog("NifFile.loadNif","Nif cannot be read. Exception \""+string(except.what())+"\" thrown.");
nifID = -1;
}
}
// this actually reads the nif in.
void NifFile::readNif() {
std::stringstream* stream = new std::stringstream(std::ios::binary|std::ios::in|std::ios::out);
BSfile* nifBSfile = loadBSfile(("Data\\Meshes\\"+filePath).c_str(), 0, 0x2800);
NifFile* nifPtr = NULL;
std::exception* e = NULL;
if ( nifBSfile ) {
const char* buffer = NULL;
try {
UInt32 size = nifBSfile->GetSize();
buffer = new const char[size];
UInt32 readCount = nifBSfile->Read((void*)buffer, size);
dPrintAndLog("NifFile.readNif", "Read "+UIntToString(readCount)+" of "+UIntToString(size)+" bytes.");
stream->write(buffer, size);
dPrintAndLog("NifFile.readNif", "Wrote stream.");
}
catch (std::exception except) {
e = new std::exception(except);
}
dPrintAndLog("NifFile.readNif", "Cleaning up BSA file.");
if ( nifBSfile )
nifBSfile->Destructor(true);
dPrintAndLog("NifFile.readNif", "Cleaning up buffer.");
if ( buffer )
delete [] buffer;
}
else if ( NifFile::getRegNif(filePath, nifPtr) ) {
if ( nifPtr->root ) {
try {
Niflib::WriteNifTree(*stream, nifPtr->root, *(nifPtr->headerInfo));
nifPtr->frzChange(this);
}
catch (std::exception except) {
e = new std::exception(except);
}
}
else {
e = new std::exception("Nif root bad.");
}
}
else {
e = new std::exception("Nif not found.");
}
if ( e ) {
std::exception except = *e;
delete e;
throw except;
}
try {
dPrintAndLog("NifFile.readNif", "Reading NIF list.");
nifList = Niflib::ReadNifList(*stream, headerInfo);
dPrintAndLog("NifFile.readNif", "Getting NIF size.");
nifSize = nifList.size();
}
catch (std::exception except) {
throw except;
}
}
// returns NifFile associated with given mod and index.
bool NifFile::getRegNif(UInt8 modID, UInt32 nifID, NifFile* &nifPtr) {
if ( RegList.find(modID) != RegList.end() ) {
if ( RegList[modID].find(nifID) != RegList[modID].end() ) {
// if ( RegList[modID][nifID]->root ) {
// dPrintAndLog("NifFile::getRegNif","Nif #"+UIntToString(modID)+"-"+UIntToString(nifID)+"'s root is good.");
if ( RegList[modID][nifID] ) {
nifPtr = RegList[modID][nifID];
return true;
}
else {
// dPrintAndLog("NifFile::getRegNif","Nif root is no good.");
dPrintAndLog("NifFile::getRegNif","Nif #"+UIntToString(modID)+"-"+UIntToString(nifID)+" has been deleted.");
return false;
}
}
else {
dPrintAndLog("NifFile::getRegNif","Nif #"+UIntToString(modID)+"-"+UIntToString(nifID)+" not found.");
return false;
}
}
else {
dPrintAndLog("NifFile::getRegNif","Mod #"+UIntToString(modID)+" not found.");
return false;
}
}
// returns the modID and index of nif with given filename.
bool NifFile::getRegNif(string filename, NifFile* &nifPtr) {
dPrintAndLog("NifFile::getRefNif","Finding nif with filename \""+filename+"\".");
if ( filename.substr(0, s_nifSEPathLen).compare(s_nifSEPath) == 0 )
filename = filename.substr(s_nifSEPathLen);
if ( RegListByFilename.find(filename) != RegListByFilename.end() ) {
pair<UInt8, UInt32> nifIDs = *(RegListByFilename[filename]);
return NifFile::getRegNif(nifIDs.first, nifIDs.second, nifPtr);
}
else {
dPrintAndLog("NifFile::getRegNif","Filename not registered.");
return false;
}
}
// from niflib.cpp
Niflib::NiNodeRef NifFile::findRoot() {
//--Look for a NiNode that has no parents--//
for (unsigned int i = 0; i < nifList.size(); ++i) {
root = Niflib::DynamicCast<Niflib::NiNode>(nifList[i]);
if ( root != NULL ) {
break;
}
}
//Make sure a node was found, if not return first node
if ( root == NULL )
throw std::exception("No nodes found.");
//Move up the chain to the root node
while ( root->GetParent() != NULL ) {
root = root->GetParent();
}
return root;
}
string NifFile::getAbsPath() const {
return GetOblivionDirectory()+"Data\\Meshes\\"+filePath;
}
string NifFile::getAbsPath(const string& path) const {
return GetOblivionDirectory()+"Data\\Meshes\\"+path;
}
string NifFile::getAbsBasePath() const {
return GetOblivionDirectory()+"Data\\Meshes\\"+basePath;
}
string NifFile::getVersion() const {
switch (nifVersion) {
case Niflib::VER_2_3:
return "v2.3";
break;
case Niflib::VER_3_0:
return "v3.0";
break;
case Niflib::VER_3_03:
return "v3.03";
break;
case Niflib::VER_3_1:
return "v3.1";
break;
case Niflib::VER_3_3_0_13:
return "v3.3.0.13";
break;
case Niflib::VER_4_0_0_0:
return "v4.0.0.0";
break;
case Niflib::VER_4_0_0_2:
return "v4.0.0.2";
break;
case Niflib::VER_4_1_0_12:
return "v4.1.0.12";
break;
case Niflib::VER_4_2_0_2:
return "v4.2.0.2";
break;
case Niflib::VER_4_2_1_0:
return "v4.2.1.0";
break;
case Niflib::VER_4_2_2_0:
return "v4.2.2.0";
break;
case Niflib::VER_10_0_1_0:
return "v10.0.1.0";
break;
case Niflib::VER_10_0_1_2:
return "v10.0.1.2";
break;
case Niflib::VER_10_0_1_3:
return "v10.0.1.3";
break;
case Niflib::VER_10_1_0_0:
return "v10.1.0.0";
break;
case Niflib::VER_10_1_0_101:
return "v10.1.0.101";
break;
case Niflib::VER_10_1_0_106:
return "v10.1.0.106";
break;
case Niflib::VER_10_2_0_0:
return "v10.2.0.0";
break;
case Niflib::VER_10_4_0_1:
return "v10.4.0.1";
break;
case Niflib::VER_20_0_0_4:
return "v20.0.0.4";
break;
case Niflib::VER_20_0_0_5:
return "v20.0.0.5";
break;
case Niflib::VER_20_1_0_3:
return "v20.1.0.3";
break;
case Niflib::VER_20_2_0_7:
return "v20.2.0.7";
break;
case Niflib::VER_20_2_0_8:
return "v20.2.0.8";
break;
case Niflib::VER_20_3_0_1:
return "v20.3.0.1";
break;
case Niflib::VER_20_3_0_2:
return "v20.3.0.2";
break;
case Niflib::VER_20_3_0_3:
return "v20.3.0.3";
break;
case Niflib::VER_20_3_0_6:
return "v20.3.0.6";
break;
case Niflib::VER_20_3_0_9:
return "v20.3.0.9";
break;
case Niflib::VER_UNSUPPORTED:
return "\"UNSUPPORTED VERSION\" ("+UIntToString(nifVersion)+")";
break;
case Niflib::VER_INVALID:
return "\"INVALID VERSION\" ("+UIntToString(nifVersion)+")";
break;
default:
return "\"UNKNOWN VERSION\" ("+UIntToString(nifVersion)+")";
break;
}
}
// writes to the new Nif file
void NifFile::write(string path) {
if ( path.compare("") == 0 )
path = filePath;
if ( root ) {
dPrintAndLog("NifFile.write","Writing to \""+path+"\".");
string::size_type i = 0;
UInt32 nifSEoff = (path.substr(0,s_nifSEPathLen).compare(s_nifSEPath)==0?s_nifSEPathLen:0);
while ( (i = path.substr(nifSEoff).find_first_of("\\", i+1)) != string::npos ) {
string folderPath = GetOblivionDirectory()+"Data\\Meshes\\"+path.substr(0,i+nifSEoff);
CreateDirectory(folderPath.c_str(), NULL);
}
try {
Niflib::WriteNifTree(getAbsPath(path),root,*headerInfo);
}
catch (...) {
throw;
}
}
else
throw std::exception("Nif root is bad.");
}
void NifFile::logChange(const UInt32 &block, const UInt32 &type, const UInt32 &action, const string &value, const bool &clrPrev) {
if ( clrPrev )
clrChange(block, type, action);
NifFile::delta.push_back(changeLog(modID, nifID, filePath, basePath, nifSEversion, editable, type, block, action, value));
}
void NifFile::clrChange(const UInt32 &block, const UInt32 &type, const UInt32 &action) {
for ( vector<changeLog>::iterator i = NifFile::delta.begin(); i != NifFile::delta.end(); ++i ) {
if ( i->mod == modID && i->nif == nifID && i->block == block && i->type == type && i->act == action && i->required.empty() ) {
NifFile::delta.erase(i);
break;
}
}
}
void NifFile::frzChange(NifFile* depends) {
for ( vector<changeLog>::iterator i = NifFile::delta.begin(); i!= NifFile::delta.end(); ++i )
if ( i->mod == modID && i->nif == nifID )
i->required.push_back(depends);
}
bool NifFile::delChange() {
changeLog* logPtr = NULL;
for ( vector<changeLog>::size_type i = NifFile::delta.size(); i > 0; --i ) {
logPtr = &(NifFile::delta[i-1]);
if ( logPtr->mod == modID && logPtr->nif == nifID ) {
if ( logPtr->required.empty() )
NifFile::delta.erase(NifFile::delta.begin()+i-1);
else
return false;
}
else if ( !logPtr->required.empty() ) {
for ( vector<NifFile*>::size_type j = 0; j < logPtr->required.size(); ++j ) {
if ( logPtr->required[j] == this ) {
logPtr->required.erase(logPtr->required.begin()+j);
--j;
}
}
}
}
return true;
}