Subversion Repositories spk

Rev

Rev 211 | Rev 274 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
 
2
#include "VirtualFileSystem.h"
3
#include "File_IO.h"
4
#include "CatFile.h"
35 cycrow 5
#include "XspFile.h"
94 cycrow 6
#include "TextDB.h"
1 cycrow 7
 
79 cycrow 8
#include "Packages.h"
9
 
112 cycrow 10
#define DELETELIST(list) if ( list ) { list->clear(); delete list; }
94 cycrow 11
 
35 cycrow 12
namespace SPK {
13
 
94 cycrow 14
CVirtualFileSystem::CVirtualFileSystem() : _lShields(NULL),
15
	_lLasers(NULL),
16
	_lMissiles(NULL),
17
	_lCockpits(NULL),
18
	_lComponentSections(NULL),
19
	_lDummySections(NULL),
101 cycrow 20
	_lBodiesSections(NULL),
21
	_lShips(NULL)
1 cycrow 22
{
23
	m_bLoaded = false;
35 cycrow 24
	m_pMap = new FileList;
25
	m_pModMap = new FileList;
26
 
27
	m_pTexts = new CTextDB();
28
	m_pModTexts = new CTextDB();
29
 
197 cycrow 30
	m_sAddon = L"";
35 cycrow 31
 
32
	m_iLang = 0;
1 cycrow 33
}
34
 
35
CVirtualFileSystem::~CVirtualFileSystem(void)
36
{
101 cycrow 37
	_clear();
35 cycrow 38
 
39
	if ( m_pTexts )		delete m_pTexts;
40
	if ( m_pModTexts )  delete m_pModTexts;
101 cycrow 41
}
94 cycrow 42
 
197 cycrow 43
void CVirtualFileSystem::setAddon(const Utils::WString &addon)
101 cycrow 44
{ 
45
	m_sAddon = addon; 
1 cycrow 46
}
35 cycrow 47
void CVirtualFileSystem::setLanguage(int iLang)
1 cycrow 48
{
35 cycrow 49
	m_iLang = iLang;
50
}
51
 
197 cycrow 52
Utils::WString CVirtualFileSystem::firstShield()
94 cycrow 53
{
54
	if ( !_lShields || _lShields->empty() ) _updateShields();
112 cycrow 55
	return _returnText(_lShields->first());
94 cycrow 56
}
57
 
197 cycrow 58
Utils::WString CVirtualFileSystem::nextShield()
94 cycrow 59
{
112 cycrow 60
	return _returnText(_lShields->next());
94 cycrow 61
}
62
 
197 cycrow 63
Utils::WString CVirtualFileSystem::firstComponentSection()
94 cycrow 64
{
65
	if ( !_lComponentSections || _lComponentSections->empty() ) _updateComponentSections();
112 cycrow 66
	return _returnLine(_lComponentSections->first());
94 cycrow 67
}
68
 
197 cycrow 69
Utils::WString CVirtualFileSystem::nextComponentSection()
94 cycrow 70
{
112 cycrow 71
	return _returnLine(_lComponentSections->next());
94 cycrow 72
}
73
 
197 cycrow 74
Utils::WString CVirtualFileSystem::firstDummySection()
94 cycrow 75
{
76
	if ( !_lDummySections || _lDummySections->empty() ) _updateDummySections();
112 cycrow 77
	return _returnLine(_lDummySections->first());
94 cycrow 78
}
79
 
197 cycrow 80
Utils::WString CVirtualFileSystem::nextDummySection()
94 cycrow 81
{
112 cycrow 82
	return _returnLine(_lDummySections->next());
94 cycrow 83
}
84
 
197 cycrow 85
Utils::WString CVirtualFileSystem::firstBodiesSection()
94 cycrow 86
{
87
	if ( !_lBodiesSections || _lBodiesSections->empty() ) _updateBodiesSections();
112 cycrow 88
	return _returnLine(_lBodiesSections->first());
94 cycrow 89
}
90
 
197 cycrow 91
Utils::WString CVirtualFileSystem::nextBodiesSection()
94 cycrow 92
{
112 cycrow 93
	return _returnLine(_lBodiesSections->next());
94 cycrow 94
}
95
 
197 cycrow 96
std::pair<Utils::WString, Utils::WString> CVirtualFileSystem::nextLaser()
94 cycrow 97
{
112 cycrow 98
	return _returnPair(_lLasers->next());
94 cycrow 99
}
100
 
197 cycrow 101
std::pair<Utils::WString, Utils::WString>  CVirtualFileSystem::firstLaser()
94 cycrow 102
{
103
	if ( !_lLasers || _lLasers->empty() ) _updateLasers();
112 cycrow 104
	return _returnPair(_lLasers->first());
94 cycrow 105
}
106
 
197 cycrow 107
std::pair<Utils::WString, Utils::WString>  CVirtualFileSystem::firstMissile()
94 cycrow 108
{
109
	if ( !_lMissiles || _lMissiles->empty() ) _updateMissiles();
112 cycrow 110
	return _returnPair(_lMissiles->first());
94 cycrow 111
}
112
 
197 cycrow 113
std::pair<Utils::WString, Utils::WString> CVirtualFileSystem::nextMissile()
94 cycrow 114
{
112 cycrow 115
	return _returnPair(_lMissiles->next());
94 cycrow 116
}
117
 
197 cycrow 118
Utils::WString CVirtualFileSystem::firstCockpit()
94 cycrow 119
{
120
	if ( !_lCockpits || _lCockpits->empty() ) _updateCockpits();
112 cycrow 121
	return _returnID(_lCockpits->first());
94 cycrow 122
}
123
 
197 cycrow 124
Utils::WString CVirtualFileSystem::nextCockpit()
94 cycrow 125
{
112 cycrow 126
	return _returnID(_lCockpits->next());
94 cycrow 127
}
128
 
197 cycrow 129
std::pair<Utils::WString, Utils::WString> CVirtualFileSystem::_returnPair(Utils::WStringNode *s)
94 cycrow 130
{
197 cycrow 131
	if ( s && !s->data.empty() && !s->data.left(8).Compare(L"ReadText") ) return std::make_pair<Utils::WString, Utils::WString>(std::move(s->str), std::move(s->data));
132
	if ( s ) return std::make_pair<Utils::WString, Utils::WString>(std::move(s->str), s->str.token(L";", -2));
133
	return std::make_pair<Utils::WString, Utils::WString>(Utils::WString::Null(), Utils::WString::Null());
94 cycrow 134
}
135
 
197 cycrow 136
Utils::WString CVirtualFileSystem::_returnText(Utils::WStringNode *s)
94 cycrow 137
{
197 cycrow 138
	if ( s && !s->data.empty() && !s->data.left(8).Compare(L"ReadText") ) return s->data;
139
	if ( s ) return s->str.token(L";", -2);
140
	return Utils::WString::Null();
94 cycrow 141
}
142
 
197 cycrow 143
Utils::WString CVirtualFileSystem::_returnID(Utils::WStringNode *s)
94 cycrow 144
{
197 cycrow 145
	if ( s ) return s->str.token(L";", -2);
146
	return Utils::WString::Null();
94 cycrow 147
}
148
 
197 cycrow 149
Utils::WString CVirtualFileSystem::_returnLine(Utils::WStringNode *s)
94 cycrow 150
{
151
	if ( s ) return s->str;
197 cycrow 152
	return Utils::WString::Null();
94 cycrow 153
}
154
 
155
 
197 cycrow 156
bool CVirtualFileSystem::LoadFilesystem(const Utils::WString &dir, int maxPatch)
35 cycrow 157
{
197 cycrow 158
	return this->LoadFilesystem(dir, L"", maxPatch);
35 cycrow 159
}
160
 
197 cycrow 161
bool CVirtualFileSystem::LoadFilesystem(const Utils::WString &dir, const Utils::WString &mod, int maxPatch)
35 cycrow 162
{
197 cycrow 163
	m_sDir = dir.findReplace(L"\\", L"/");
1 cycrow 164
 
35 cycrow 165
	this->_clear();
1 cycrow 166
 
114 cycrow 167
	// if we are in the addon directory, then also read the previous directory
197 cycrow 168
	Utils::WString s = dir.token(L"/", -1);
169
	if ( !this->m_sAddon.empty() && dir.token(L"/", -1).Compare(this->m_sAddon) ) {
114 cycrow 170
		//LoadFilesystem(dir.tokens("/", 1, -2), "", 0);
171
	}
172
 
1 cycrow 173
	int number = 1;
197 cycrow 174
	while ( CFileIO::Exists(dir + L"/" + Utils::WString::PadNumber(number, 2) + L".cat") )
1 cycrow 175
	{
58 cycrow 176
		if ( maxPatch && maxPatch < number ) break;
197 cycrow 177
		Utils::WString file = dir + L"/" + Utils::WString::PadNumber(number, 2);
178
		if ( !CFileIO::Exists(file + L".dat") )
1 cycrow 179
			break;
180
 
181
		CCatFile cat;
197 cycrow 182
		if ( cat.open(file + L".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE)
1 cycrow 183
		{
124 cycrow 184
			for(auto itr = cat.GetFiles()->cbegin(); itr != cat.GetFiles()->cend(); itr++)
185
			{
197 cycrow 186
				this->_addModFile(CFileIO((*itr)->sFile).fullFilename(), file + L".cat", m_pMap);
1 cycrow 187
				m_bLoaded = true;
188
			}
189
		}
190
		++number;
191
	}
192
 
43 cycrow 193
	// add all the files from the mod
35 cycrow 194
	if ( !mod.empty() )
195
		this->addMod(mod);
196
 
43 cycrow 197
	// now add all the extracted data
197 cycrow 198
	this->_addDir(m_sDir, L"");
43 cycrow 199
 
1 cycrow 200
	return m_bLoaded;
201
}
202
 
43 cycrow 203
 
197 cycrow 204
void CVirtualFileSystem::_addDir(const Utils::WString &sStart, const Utils::WString &sDir)
43 cycrow 205
{
197 cycrow 206
	CDirIO Dir(sStart + L"/" + sDir);
196 cycrow 207
	Utils::WStringList list;
124 cycrow 208
	if (Dir.dirList(list))
209
	{
210
		for (auto itr = list.begin(); itr != list.end(); itr++)
211
		{
197 cycrow 212
			if (CDirIO(Dir.dir((*itr)->str)).isDir()) _addDir(sStart, sDir + L"/" + (*itr)->str);
196 cycrow 213
			else if (CFileIO((*itr)->str).isFileExtension(L"cat")) continue;
214
			else if (CFileIO((*itr)->str).isFileExtension(L"dat")) continue;
197 cycrow 215
			else this->_addFile(sDir + L"/" + (*itr)->str, Dir.file((*itr)->str), m_pMap);
43 cycrow 216
		}
217
	}
218
}
219
 
114 cycrow 220
 
197 cycrow 221
void CVirtualFileSystem::_removeSameFile(const Utils::WString &sFile, const Utils::WString &sDest, const Utils::WString &ext, FileList *pList)
114 cycrow 222
{
223
	CFileIO F(sFile);
224
 
197 cycrow 225
	Utils::WString newFile = F.changeFileExtension(ext);
114 cycrow 226
	newFile = newFile.lower();
227
 
228
	FileListItr itr = pList->find(newFile);
229
	if ( itr != pList->end() ) {
230
		if ( !CFileIO((*pList)[newFile]).dir().Compare(CFileIO(sDest).dir()) ) {
231
			pList->erase(newFile);
232
		}
233
	}
234
}
235
 
197 cycrow 236
void CVirtualFileSystem::_addFile(const Utils::WString &sFile, const Utils::WString &sDest, FileList *pList)
43 cycrow 237
{
197 cycrow 238
	Utils::WString file = sFile.asFilename().removeIf(0, '/');
114 cycrow 239
 
240
	// check file extension, remove any patching files in previous files
241
	CFileIO F(file);
196 cycrow 242
	Utils::WString ext = F.extension().toLower();
243
	if (ext.Compare(L"bob", true) || ext.Compare(L"pbd", true) || ext.Compare(L"bod", true) ) {
202 cycrow 244
		_removeSameFile(file, sDest, L"pbb", pList);
245
		_removeSameFile(file, sDest, L"bob", pList);
246
		_removeSameFile(file, sDest, L"pbd", pList);
114 cycrow 247
	}
196 cycrow 248
	else if (ext.Compare(L"xml", true) || ext.Compare(L"txt", true) )
202 cycrow 249
		_removeSameFile(file, sDest, L"pck", pList);
114 cycrow 250
 
202 cycrow 251
	(*pList)[file.lower()] = sDest.findReplace(L"\\", L"/").findReplace(L"//", L"/");
43 cycrow 252
}
197 cycrow 253
void CVirtualFileSystem::_addModFile(const Utils::WString &sFile, const Utils::WString &sMod, FileList *pList)
43 cycrow 254
{
197 cycrow 255
	this->_addFile(sFile, sMod + L"::" + sFile, pList);
43 cycrow 256
}
257
 
197 cycrow 258
bool CVirtualFileSystem::loadMod(const Utils::WString &mod)
1 cycrow 259
{
260
	bool loaded = false;
35 cycrow 261
 
262
	if ( !m_pModMap ) m_pModMap = new FileList;
263
 
1 cycrow 264
	CCatFile cat;
197 cycrow 265
	if ( CCatFile::Opened(cat.open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false))
1 cycrow 266
	{
124 cycrow 267
		for (auto itr = cat.GetFiles()->cbegin(); itr != cat.GetFiles()->cend(); itr++) 
268
		{
197 cycrow 269
			this->_addModFile(CFileIO((*itr)->sFile).fullFilename(), mod, m_pModMap);
35 cycrow 270
			loaded = true;
271
		}
272
	}
273
 
274
	return loaded;
275
}
276
 
197 cycrow 277
bool CVirtualFileSystem::addMod(const Utils::WString &mod)
35 cycrow 278
{
279
	bool loaded = false;
280
 
281
	if ( !m_pMap ) m_pMap = new FileList;
282
 
283
	CCatFile cat;
197 cycrow 284
	if ( CCatFile::Opened(cat.open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false))
35 cycrow 285
	{
124 cycrow 286
		for (auto itr = cat.GetFiles()->cbegin(); itr != cat.GetFiles()->cend(); itr++)
35 cycrow 287
		{
197 cycrow 288
			this->_addModFile(CFileIO((*itr)->sFile).fullFilename(), mod, m_pMap);
1 cycrow 289
			loaded = true;
290
		}
291
	}
292
 
293
	return loaded;
294
}
295
 
35 cycrow 296
bool CVirtualFileSystem::textExists(int iLang, int iPage, int iID) const
1 cycrow 297
{
94 cycrow 298
	if ( iLang <= 0 ) iLang = m_iLang;
299
 
35 cycrow 300
	if ( m_pTexts ) {
301
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->exists(iLang, iPage, iID);
302
	}
303
	if ( m_pModTexts ) {
304
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->exists(iLang, iPage, iID);
305
	}
306
 
307
	return false;
1 cycrow 308
}
309
 
197 cycrow 310
Utils::WString CVirtualFileSystem::findText(int iLang, int iPage, int iID) const
1 cycrow 311
{
94 cycrow 312
	if ( iLang <= 0 ) iLang = m_iLang;
313
 
35 cycrow 314
	if ( m_pTexts ) {
315
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->get(iLang, iPage, iID);
316
	}
317
	if ( m_pModTexts ) {
318
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->get(iLang, iPage, iID);
319
	}
1 cycrow 320
 
197 cycrow 321
	return Utils::WString(L"ReadText") + (long)iPage + L"-" + (long)iID;
35 cycrow 322
}
43 cycrow 323
 
197 cycrow 324
const Utils::WString &CVirtualFileSystem::directory() const
101 cycrow 325
{
326
	return m_sDir;
327
}
328
 
197 cycrow 329
bool CVirtualFileSystem::isFileAvailable(const Utils::WString &file) const
35 cycrow 330
{
43 cycrow 331
	return !this->_findFile(file).empty();
332
}
333
 
101 cycrow 334
bool CVirtualFileSystem::isTextUpdated() const
335
{
336
	if ( m_pTexts ) return m_pTexts->anyTextLoaded();
337
	return false;
338
}
339
 
197 cycrow 340
Utils::WString CVirtualFileSystem::_findFile(const Utils::WString &file) const
43 cycrow 341
{
197 cycrow 342
	Utils::WString toFile = file.findReplace(L"\\", L"/").lower();
43 cycrow 343
 
35 cycrow 344
	if ( m_pModMap && !m_pModMap->empty() ) {
345
		if ( !m_sAddon.empty() ) {
197 cycrow 346
			FileListItr aitr = m_pModMap->find(CFileIO(m_sAddon + L"/" + toFile).fullFilename().lower().c_str());
347
			if ( aitr == m_pModMap->end() ) aitr = m_pModMap->find(CFileIO(_convertExtension(m_sAddon + L"/" + toFile)).fullFilename().lower().c_str());
35 cycrow 348
			if ( aitr != m_pModMap->end() ) return aitr->second;
349
		}
197 cycrow 350
		FileListItr itr = m_pModMap->find(CFileIO(toFile).fullFilename().lower().c_str());
351
		if ( itr == m_pModMap->end() ) itr = m_pModMap->find(CFileIO(_convertExtension(toFile)).fullFilename().lower().c_str());
35 cycrow 352
		if ( itr != m_pModMap->end() ) return itr->second;
353
	}
114 cycrow 354
 
355
	if ( m_pMap && !m_pMap->empty() ) {
35 cycrow 356
		if ( !m_sAddon.empty() ) {
197 cycrow 357
			FileListItr aitr = m_pMap->find(CFileIO(m_sAddon + L"/" + toFile).fullFilename().lower().c_str());
358
			if ( aitr == m_pMap->end() ) aitr = m_pMap->find(CFileIO(_convertExtension(m_sAddon + L"/" + file)).fullFilename().lower().c_str());
35 cycrow 359
			if ( aitr != m_pMap->end() ) return aitr->second;
360
		}
197 cycrow 361
		FileListItr itr = m_pMap->find(CFileIO(file).fullFilename().lower().c_str());
362
		if ( itr == m_pMap->end() ) itr = m_pMap->find(CFileIO(_convertExtension(file)).fullFilename().lower().c_str());
35 cycrow 363
		if ( itr != m_pMap->end() ) return itr->second;
364
	}
197 cycrow 365
	return L"";
35 cycrow 366
}
367
 
197 cycrow 368
Utils::WString CVirtualFileSystem::_extractFromCat(const Utils::WString &sCat, const Utils::WString &sFile, const Utils::WString &sTo)
35 cycrow 369
{
58 cycrow 370
	CCatFile catFile;
197 cycrow 371
	if ( catFile.open(sCat, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE)
58 cycrow 372
	{
373
		// check for the file
197 cycrow 374
		if ( catFile.extractFile(sFile, sTo)) return sTo;
124 cycrow 375
		if ( catFile.error() == CATERR_INVALIDDEST || catFile.error() == CATERR_CANTCREATEDIR ) {
181 cycrow 376
			if ( catFile.extractFile(sFile) ) return sFile;
58 cycrow 377
		}
378
	}
379
 
202 cycrow 380
	return L"";
58 cycrow 381
}
382
 
197 cycrow 383
Utils::WString CVirtualFileSystem::_extract(const Utils::WString &sFile, const Utils::WString &sTo)
58 cycrow 384
{
385
	// check if we need to unpack the file
386
	if ( CCatFile::CheckPackedExtension(sFile) ) {
387
		CFileIO File(sFile);
197 cycrow 388
		C_File f(File.fullFilename());
389
		if ( !f.readFromFile(File) ) return L"";
390
		if ( !f.UnPCKFile() ) return L"";
391
		if ( !f.writeToFile(sTo) ) return L"";
58 cycrow 392
		return sTo;
393
	}
394
	return sFile;
395
}
396
 
197 cycrow 397
Utils::WString CVirtualFileSystem::extractGameFile(const Utils::WString &file, const Utils::WString &to)
58 cycrow 398
{
197 cycrow 399
	Utils::WString sIn = _findFile(file);
400
	Utils::WString sFile = file;
43 cycrow 401
 
197 cycrow 402
	if (sIn.empty()) return L"";
35 cycrow 403
 
197 cycrow 404
	if (sIn.contains(L"::"))
405
	{
406
		sFile = sIn.token(L"::", 2);
407
		sIn = sIn.token(L"::", 1);
58 cycrow 408
		return _extractFromCat(sIn, sFile, to);
43 cycrow 409
	}
58 cycrow 410
 
411
	return _extract(sIn, to);
1 cycrow 412
}
413
 
197 cycrow 414
Utils::WString CVirtualFileSystem::_convertExtension(const Utils::WString &sFile) const
35 cycrow 415
{
416
	CFileIO File(sFile);
196 cycrow 417
	if ( File.isFileExtension(L"pck") ) {
197 cycrow 418
		return File.changeFileExtension(L"xml");
35 cycrow 419
	}
196 cycrow 420
	else if ( File.isFileExtension(L"xml") ) {
197 cycrow 421
		return File.changeFileExtension(L"pck");
35 cycrow 422
	}
196 cycrow 423
	else if ( File.isFileExtension(L"pbb") ) {
197 cycrow 424
		return File.changeFileExtension(L"bob");
35 cycrow 425
	}
196 cycrow 426
	else if ( File.isFileExtension(L"bob") ) {
197 cycrow 427
		return File.changeFileExtension(L"pbb");
35 cycrow 428
	}
196 cycrow 429
	else if ( File.isFileExtension(L"pbd") ) {
197 cycrow 430
		return File.changeFileExtension(L"bod");
35 cycrow 431
	}
196 cycrow 432
	else if ( File.isFileExtension(L"bod") ) {
197 cycrow 433
		return File.changeFileExtension(L"pbd");
35 cycrow 434
	}
435
 
436
	return sFile;
437
}
438
 
197 cycrow 439
Utils::WStringList *CVirtualFileSystem::getTShipsEntries()
35 cycrow 440
{
101 cycrow 441
	if ( !_lShips || _lShips->empty() ) _updateShips();
442
	return _lShips;
35 cycrow 443
}
444
 
197 cycrow 445
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::WString &sFile, FileType iFileType)
35 cycrow 446
{
447
	return this->extractGameFileToPackage(pPackage, sFile, iFileType, sFile);
448
}
449
 
197 cycrow 450
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::WString &sFile, FileType iFileType, const Utils::WString &sTo)
35 cycrow 451
{
264 cycrow 452
	Utils::WString to = this->extractGameFile(sFile, CPackages::tempDirectory() + L"/tmp.dat");
58 cycrow 453
	if ( !to.empty() ) {
454
		CFileIO File(to);
35 cycrow 455
 
197 cycrow 456
		C_File *f = pPackage->addFile(CFileIO(sTo).filename(), CFileIO(sTo).dir(), iFileType);
35 cycrow 457
		if ( f ) {
264 cycrow 458
			if ( f->readFromFile(CPackages::tempDirectory() + L"/tmp.dat") ) {
52 cycrow 459
				File.remove();
35 cycrow 460
				return f;
461
			}
462
		}
463
 
52 cycrow 464
		File.remove();
35 cycrow 465
	}
466
 
467
	return NULL;
468
}
469
 
197 cycrow 470
Utils::WString CVirtualFileSystem::getTShipsEntry(const Utils::WString &sId)
35 cycrow 471
{
101 cycrow 472
	if ( !_lShips || _lShips->empty() ) _updateShips();
473
	return _lShips->findData(sId, true);
35 cycrow 474
}
475
 
476
void CVirtualFileSystem::extractTexts(CXspFile *pPackage, int textId)
477
{
478
	//TODO: check for better finding of files in map
479
	for ( FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); ++itr ) {
197 cycrow 480
		Utils::WString str = itr->first;
35 cycrow 481
 
482
		if ( !m_sAddon.empty() ) {
197 cycrow 483
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + L"/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + L"\\t\\") )
35 cycrow 484
				continue;
485
		}
486
		else {
197 cycrow 487
			if ( !str.left(2).Compare(L"t\\") && !str.left(2).Compare(L"t/") && !str.left(8).Compare(L"addon\t\\") && !str.left(8).Compare(L"addon/t/") )
35 cycrow 488
				continue;
489
		}
490
 
264 cycrow 491
		Utils::WString sTo = this->extractGameFile(str, CPackages::tempDirectory() + L"/tmp.dat");
58 cycrow 492
		if ( !sTo.empty() ) {
197 cycrow 493
			pPackage->addTextFromFile(sTo, textId);
58 cycrow 494
			CFileIO::Remove(sTo);
35 cycrow 495
		}
496
	}
497
}
498
 
499
void CVirtualFileSystem::updateTexts(int iFromPage, int iToPage)
500
{
501
	this->_updateTexts(iFromPage, iToPage, m_pMap, m_pTexts);
502
}
503
void CVirtualFileSystem::updateTexts(int iPage)
504
{
505
	this->_updateTexts(iPage, iPage, m_pMap, m_pTexts);
506
}
507
void CVirtualFileSystem::updateModTexts(int iFromPage, int iToPage)
508
{
509
	this->_updateTexts(iFromPage, iToPage, m_pModMap, m_pModTexts);
510
}
511
void CVirtualFileSystem::updateModTexts(int iPage)
512
{
513
	this->_updateTexts(iPage, iPage, m_pModMap, m_pModTexts);
514
}
515
 
516
void CVirtualFileSystem::_clear()
517
{
518
	m_bLoaded = false;
519
 
520
	if ( m_pMap ) delete m_pMap;
521
	m_pMap = new FileList;
522
	if ( m_pModMap ) delete m_pModMap;
523
	m_pModMap = new FileList;
101 cycrow 524
 
525
	DELETELIST(_lShields);
526
	DELETELIST(_lLasers);
527
	DELETELIST(_lMissiles);
528
	DELETELIST(_lCockpits);
529
	DELETELIST(_lComponentSections);
530
	DELETELIST(_lDummySections);
531
	DELETELIST(_lBodiesSections);
532
	DELETELIST(_lShips);
35 cycrow 533
}
534
 
535
void CVirtualFileSystem::_updateTexts(int iFromPage, int iToPage, FileList *pFileList, CTextDB *pTextList)
536
{
537
	// read all text files
538
	for ( FileListItr itr = pFileList->begin(); itr != pFileList->end(); ++itr ) {
197 cycrow 539
		Utils::WString str = itr->first;
35 cycrow 540
 
541
		if ( !m_sAddon.empty() ) {
197 cycrow 542
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + L"/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + L"\\t\\") )
35 cycrow 543
				continue;
544
		}
545
		else {
197 cycrow 546
			if ( !str.left(2).Compare(L"t\\") && !str.left(2).Compare(L"t/") )
35 cycrow 547
				continue;
548
		}
549
 
264 cycrow 550
		Utils::WString sTo = this->extractGameFile(str, CPackages::tempDirectory() + L"/tmp.dat");
58 cycrow 551
		if ( !sTo.empty() ) {
35 cycrow 552
			// add all texts into the map, id=(pageid, tid) data=text
196 cycrow 553
			Utils::WString baseFile = CFileIO(str).baseName();
554
			int lang = (baseFile.contains(L"-L")) ? baseFile.right(3) : baseFile.truncate(-4);
35 cycrow 555
 
556
			if ( m_iLang && lang != m_iLang ) continue;
557
			// open the text file to add
58 cycrow 558
			pTextList->parseTextFile(iFromPage, iToPage, sTo, lang);
35 cycrow 559
		}
560
	}
561
}
562
 
78 cycrow 563
void CVirtualFileSystem::clearMods(bool bIncludeStandard)
35 cycrow 564
{
78 cycrow 565
	if ( bIncludeStandard ) {
566
		if ( m_pTexts ) delete m_pTexts;
567
		m_pTexts = new CTextDB();
568
	}
569
 
35 cycrow 570
	if ( m_pModTexts ) delete m_pModTexts;
571
	m_pModTexts = new CTextDB();
572
}
573
 
197 cycrow 574
Utils::WStringList *CVirtualFileSystem::_updateList(const Utils::WString &typesFile, int iTextPos)
94 cycrow 575
{
197 cycrow 576
	Utils::WStringList *list = new Utils::WStringList();
35 cycrow 577
 
197 cycrow 578
	Utils::WString file = this->extractGameFile(L"types\\" + typesFile + L".pck", CPackages::tempDirectory() + L"/" + typesFile + L".txt");
94 cycrow 579
 
580
	CFileIO f(file);
581
 
582
	if ( f.exists() && f.startRead() ) {
583
		int itemsLeft = -1;
584
 
585
		while(!f.atEnd()) {
586
			// read the next line
197 cycrow 587
			Utils::WString line = f.readEndOfLineStr();
94 cycrow 588
 
589
			// remove the unneeded characters
590
			line.removeChar('\r');
591
			line.removeChar(9);
592
			line.removeFirstSpace();
593
 
594
			// blank line ? skip it
595
			if ( line.empty() ) continue;
596
			// skip anything starting with / as its a comment
597
			if ( line[0] == '/' ) continue;
598
 
599
			// first line is the count
600
			if ( itemsLeft == -1 )
197 cycrow 601
				itemsLeft = line.token(L";", 2);
94 cycrow 602
			else {
603
				if ( iTextPos == 0 )
604
					list->pushBack(line, line);
605
				else if ( iTextPos == -1 ) 
197 cycrow 606
					list->pushBack(line, line.token(L";", -2));
94 cycrow 607
				else
197 cycrow 608
					list->pushBack(line, this->findText(m_iLang, TEXTPAGE_OBJECTS, line.token(L";", iTextPos).toLong()));
94 cycrow 609
				--itemsLeft;
610
			}
611
		}
612
 
613
		f.close();
614
		f.remove();
615
	}
616
 
617
	return list;
618
}
619
 
620
void CVirtualFileSystem::_updateShields()
621
{
622
	DELETELIST(_lShields);
197 cycrow 623
	_lShields = _updateList(L"TShields", 7);
94 cycrow 624
}
625
 
626
void CVirtualFileSystem::_updateLasers()
627
{
628
	DELETELIST(_lLasers);
197 cycrow 629
	_lLasers = _updateList(L"TLaser", 7);
94 cycrow 630
}
631
 
632
void CVirtualFileSystem::_updateMissiles()
633
{
634
	DELETELIST(_lMissiles);
197 cycrow 635
	_lMissiles = _updateList(L"TMissiles", 7);
94 cycrow 636
}
637
 
638
void CVirtualFileSystem::_updateCockpits()
639
{
640
	DELETELIST(_lCockpits);
197 cycrow 641
	_lCockpits = _updateList(L"TCockpits", -1);
94 cycrow 642
}
643
 
101 cycrow 644
void CVirtualFileSystem::_updateShips()
645
{
646
	DELETELIST(_lShips);
197 cycrow 647
	_lShips = _updateList(L"TShips", -1);
101 cycrow 648
}
649
 
94 cycrow 650
void CVirtualFileSystem::_updateComponentSections()
651
{
652
	DELETELIST(_lComponentSections);
653
 
197 cycrow 654
	_lComponentSections = new Utils::WStringList();
94 cycrow 655
 
197 cycrow 656
	Utils::WString file = this->extractGameFile(L"types\\Components.pck", CPackages::tempDirectory() + L"/Components.txt");
94 cycrow 657
 
658
	CFileIO f(file);
659
 
660
	if ( f.exists() && f.startRead() ) {
661
		int sectionRemaining = 0;
662
		int itemsRemaining = 0;
663
 
664
		while(!f.atEnd()) {
665
			// read the next line
197 cycrow 666
			Utils::WString line = f.readEndOfLineStr();
94 cycrow 667
 
668
			// remove the unneeded characters
669
			line.removeChar('\r');
670
			line.removeChar(9);
671
			line.removeFirstSpace();
672
 
673
			// blank line ? skip it
674
			if ( line.empty() ) continue;
675
			// skip anything starting with / as its a comment
197 cycrow 676
			if ( line[0] == L'/' ) continue;
94 cycrow 677
 
678
			if ( itemsRemaining )
679
				--itemsRemaining;
680
			else if ( !sectionRemaining )
681
			{
197 cycrow 682
				sectionRemaining = line.token(L";", 2).toLong();
683
				Utils::WString section = line.token(L";", 1);
94 cycrow 684
				if ( !section.empty() )
685
					_lComponentSections->pushBack(section, section);
686
			}
687
			else if ( !itemsRemaining )
688
			{
197 cycrow 689
				itemsRemaining = line.token(L";", 2).toLong();
94 cycrow 690
				--sectionRemaining;
691
			}
692
		}
693
 
694
		f.close();
695
		f.remove();
696
	}
697
 
698
	if ( _lComponentSections->empty() )
699
	{
197 cycrow 700
		_lComponentSections->pushBack(L"SCTYPE_LASER", L"SCTYPE_LASER");
701
		_lComponentSections->pushBack(L"SCTYPE_COCKPIT", L"SCTYPE_COCKPIT");
702
		_lComponentSections->pushBack(L"SCTYPE_DOCKING", L"SCTYPE_DOCKING");
94 cycrow 703
	}
704
}
705
 
197 cycrow 706
Utils::WStringList *CVirtualFileSystem::_updateSectionList(const Utils::WString &sFile, bool singleEntry)
94 cycrow 707
{
197 cycrow 708
	Utils::WStringList *list = new Utils::WStringList();
94 cycrow 709
 
197 cycrow 710
	Utils::WString file = this->extractGameFile(L"types\\" + sFile + L".pck", CPackages::tempDirectory() + L"/" + sFile + L".txt");
94 cycrow 711
 
712
	CFileIO f(file);
713
 
714
	if ( f.exists() && f.startRead() ) {
715
		int sectionRemaining = 0;
716
 
717
		while(!f.atEnd()) {
718
			// read the next line
197 cycrow 719
			Utils::WString line = f.readEndOfLineStr();
94 cycrow 720
 
721
			// remove the unneeded characters
722
			line.removeChar('\r');
723
			line.removeChar(9);
724
			line.removeFirstSpace();
725
 
726
			// blank line ? skip it
727
			if ( line.empty() ) continue;
728
			// skip anything starting with / as its a comment
197 cycrow 729
			if ( line[0] == L'/' ) continue;
94 cycrow 730
 
731
			if ( !sectionRemaining )
732
			{
197 cycrow 733
				sectionRemaining = line.token(L";", 2).toLong();
734
				Utils::WString section = line.token(L";", 1);
94 cycrow 735
				if ( !section.empty() )
736
					list->pushBack(section, section);
737
			}
738
			else {
739
				if ( singleEntry )
197 cycrow 740
					sectionRemaining -= (line.countToken(L";") - 1);
94 cycrow 741
				else 
742
					--sectionRemaining;
743
			}
744
		}
745
 
746
		f.close();
747
		f.remove();
748
	}
749
 
750
	return list;
751
}
752
 
753
void CVirtualFileSystem::_updateDummySections()
754
{
755
	DELETELIST(_lDummySections);
756
 
197 cycrow 757
	_lDummySections = _updateSectionList(L"Dummies", false);
94 cycrow 758
 
759
	if ( _lDummySections->empty() )
760
	{
197 cycrow 761
		_lDummySections->pushBack(L"SDTYPE_ANIMATED", L"SDTYPE_ANIMATED");
762
		_lDummySections->pushBack(L"SDTYPE_DOCK", L"SDTYPE_DOCK");
763
		_lDummySections->pushBack(L"SDTYPE_DOORWAY", L"SDTYPE_DOORWAY");
764
		_lDummySections->pushBack(L"SDTYPE_GUN", L"SDTYPE_GUN");
765
		_lDummySections->pushBack(L"SDTYPE_CONNECTION", L"SDTYPE_CONNECTION");
94 cycrow 766
	}
767
}
768
 
769
void CVirtualFileSystem::_updateBodiesSections()
770
{
771
	DELETELIST(_lBodiesSections);
772
 
197 cycrow 773
	_lBodiesSections = _updateSectionList(L"Bodies", true);
94 cycrow 774
 
775
	if ( _lBodiesSections->empty() )
776
	{
197 cycrow 777
		_lBodiesSections->pushBack(L"SBTYPE_2D", L"SBTYPE_2D");
778
		_lBodiesSections->pushBack(L"SBTYPE_FACECAMERA", L"SBTYPE_FACECAMERA");
779
		_lBodiesSections->pushBack(L"SBTYPE_2D2", L"SBTYPE_2D2");
780
		_lBodiesSections->pushBack(L"SBTYPE_2DY", L"SBTYPE_2DY");
781
		_lBodiesSections->pushBack(L"SBTYPE_LOGO", L"SBTYPE_LOGO");
782
		_lBodiesSections->pushBack(L"SBTYPE_FC", L"SBTYPE_FC");
783
		_lBodiesSections->pushBack(L"SBTYPE_TURRET", L"SBTYPE_TURRET");
784
		_lBodiesSections->pushBack(L"SBTYPE_JET", L"SBTYPE_JET");
785
		_lBodiesSections->pushBack(L"SBTYPE_RADAR", L"SBTYPE_RADAR");
786
		_lBodiesSections->pushBack(L"SBTYPE_CONTAINER", L"SBTYPE_CONTAINER");
787
		_lBodiesSections->pushBack(L"SBTYPE_DISPLAY", L"SBTYPE_DISPLAY");
788
		_lBodiesSections->pushBack(L"SBTYPE_DOCKPOINT", L"SBTYPE_DOCKPOINT");
789
		_lBodiesSections->pushBack(L"SBTYPE_SMALLJET", L"SBTYPE_SMALLJET");
94 cycrow 790
	}
791
}
792
 
114 cycrow 793
 
794
/////////////////////////////////////////////////////////////////////////////////////////
795
//	DEBUG Functions
796
 
797
 
197 cycrow 798
void CVirtualFileSystem::DEBUG_LogContents(const Utils::WString &sFile)
114 cycrow 799
{
800
	CFileIO File(sFile);
801
 
802
	if ( File.startWrite() ) {
803
		if ( m_pMap ) {
804
			for(FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); itr++) {
197 cycrow 805
				Utils::WString line = itr->first + L" => " + itr->second + L"\r\n";
211 cycrow 806
#pragma warning(disable:4244)
197 cycrow 807
				std::string str = std::string(line.begin(), line.end());
211 cycrow 808
#pragma warning(default:4244)
197 cycrow 809
				File.write(str.c_str(), str.length());
114 cycrow 810
			}
811
		}
812
 
813
		File.close();
814
	}
815
}
816
 
35 cycrow 817
} //NAMESPACE