Subversion Repositories spk

Rev

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