Subversion Repositories spk

Rev

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

Rev Author Line No. Line
94 cycrow 1
 
2
#include "GameDirectories.h"
3
#include "GameExe.h"
4
#include "Packages.h"
95 cycrow 5
#include "GameExe.h"
94 cycrow 6
 
7
namespace SPK {
8
 
95 cycrow 9
	CGameDirectories::CGameDirectories(const Utils::String &mydoc) : _pSelected(NULL),
94 cycrow 10
		_pCurrentItr(NULL),
11
		_iLanguage(0)
12
	{
13
		_pDirs = new std::vector<SGameDir *>();
95 cycrow 14
 
15
		_updateControlledDirs(mydoc);
94 cycrow 16
	}
17
 
18
	CGameDirectories::~CGameDirectories(void)
19
	{
20
		clear();
21
		delete _pDirs;
95 cycrow 22
		_lControlledDirs.MemoryClear();
94 cycrow 23
	}
24
 
25
	bool CGameDirectories::add(const Utils::String &dir, const Utils::String &name, int iGame, const Utils::String &addon, bool bLoad)
26
	{
27
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
28
			if ( dir.Compare((*itr)->dir) ) return false;
29
		}
30
 
31
		_add(dir, name, iGame, addon, bLoad);
32
		return true;
33
	}
34
 
35
	bool CGameDirectories::remove(const Utils::String &dir)
36
	{
37
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
38
			if ( dir.Compare((*itr)->dir) ) {
39
				delete (*itr);
40
				_pDirs->erase(itr);
41
				return true;
42
			}
43
		}
44
 
45
		return false;
46
	}
47
 
48
	bool CGameDirectories::findDir(const Utils::String &dir)
49
	{
50
		SGameDir *d = _findGameDir(dir);
51
		if ( d ) {
52
			_pCurrentItr = d;
53
			return true;
54
		}
55
 
56
		return false;
57
	}
58
 
59
	bool CGameDirectories::parse(const Utils::String &data, CPackages *pPackages)
60
	{
61
		int iGame = data.token(";", 2).tokens(" ", 1).toLong();
62
		SGameExe *exe = pPackages->GetGameExe()->GetGame(iGame);
63
		_add(data.token(";", 1), data.token(";", 2).tokens(" ", 2), iGame, (exe) ? exe->sAddon : Utils::String::Null(), data.token(";", 3).toBool());
64
 
65
		return true;
66
	}
67
 
68
	bool CGameDirectories::writeData(CyStringList *lines)
69
	{
70
		bool writeAny = false;
71
		for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
72
			SGameDir *gd = *itr;
73
			lines->PushBack(CyString("GameDir:" + gd->dir + ";" + Utils::String::Number(gd->iGame) + " " + gd->name + ((gd->bLoad) ? ";1" : ";0")));
74
			writeAny = true;
75
		}
76
 
77
		return writeAny;
78
	}
79
 
101 cycrow 80
	void CGameDirectories::updateCurrentVFS(bool bReload)
94 cycrow 81
	{
82
		if ( !_pCurrentItr ) return;
83
 
101 cycrow 84
		// dont load text if its already been loaded, unless we need to reload them
85
		if ( !bReload && _pCurrentItr->pVfs->isTextUpdated() ) return;
86
 
87
		// update the texts for all pages
94 cycrow 88
		_pCurrentItr->pVfs->updateTexts(0);
89
	}
90
 
91
	void CGameDirectories::clear()
92
	{
93
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
94
			delete *itr;
95
		}
96
		_pDirs->clear();
97
	}
98
 
99
	bool CGameDirectories::setSelectedGameDirectory(int iGame, bool temp)
100
	{
101
		_pTemporary = NULL;
102
		if ( temp ) _pTemporary = _pSelected;
103
		_pSelected = _findGameDir(iGame);
104
		return hasSelected();
105
	}
106
 
107
	bool CGameDirectories::setSelectedDirectory(const Utils::String &dir, bool temp)
108
	{
109
		_pTemporary = NULL;
110
		if ( temp ) _pTemporary = _pSelected;
111
		_pSelected = _findGameDir(dir);
112
		return hasSelected();
113
	}
114
 
115
	void CGameDirectories::reselectTemporaryDirectory()
116
	{
117
		if ( _pTemporary ) _pSelected = _pTemporary;
118
		_pTemporary = NULL;
119
	}
120
 
121
	void CGameDirectories::setLanguage(int iLang)
122
	{
123
		_iLanguage = iLang;
124
	}
125
 
126
	void CGameDirectories::setLoad(bool bLoad)
127
	{
128
		if ( _pCurrentItr ) _pCurrentItr->bLoad = bLoad;
129
	}
130
 
101 cycrow 131
	bool CGameDirectories::isAllTextLoaded() const
132
	{
133
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
134
			if ( !(*itr)->pVfs->isTextUpdated() ) return false;
135
		}
136
 
137
		return true;
138
	}
139
 
94 cycrow 140
	bool CGameDirectories::isDir(const Utils::String &dir) const
141
	{
142
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
143
			if ( dir.Compare((*itr)->dir) ) return true;
144
		}
145
 
146
		return false;
147
	}
148
 
149
	bool CGameDirectories::hasSelected() const
150
	{
151
		return (_pSelected) ? true : false;
152
	}
153
 
154
	bool CGameDirectories::isEmpty() const
155
	{
156
		return _pDirs->empty();
157
	}
158
 
159
	int CGameDirectories::highestGame() const
160
	{
161
		int highest = 1;
162
		for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
163
			if ( (*itr)->iGame > highest )
164
				highest = (*itr)->iGame;
165
		}
166
 
167
		return highest;
168
	}
169
 
170
	Utils::String CGameDirectories::currentName() const
171
	{
172
		if ( !_pCurrentItr ) return Utils::String::Null();
173
		return _pCurrentItr->name;
174
	}
175
 
176
	int CGameDirectories::currentGame() const
177
	{
178
		if ( !_pCurrentItr ) return -1;
179
		return _pCurrentItr->iGame;
180
	}
181
 
182
	bool CGameDirectories::currentLoad() const
183
	{
184
		if ( !_pCurrentItr ) return false;
185
		return _pCurrentItr->bLoad;
186
	}
187
 
188
	CVirtualFileSystem *CGameDirectories::selectedVFS() const
189
	{
190
		if ( _pSelected ) return _pSelected->pVfs;
191
		return NULL;
192
	}
193
 
95 cycrow 194
	void CGameDirectories::syncWithControlled(CGameExe *exe)
195
	{
196
		for(CListNode<Utils::SStringList> *node = _lControlledDirs.Front(); node; node = node->next()) {
197
			Utils::SStringList *str = node->Data();
198
			add(exe->GetProperDir(str->str), str->data, exe->GetGameType(str->str), exe->GetAddonDir(str->str), true);
199
		}
200
	}
201
 
94 cycrow 202
	Utils::String CGameDirectories::findText(int iLanguage, int iPage, int iID, Utils::String missing)
203
	{
204
		if ( iLanguage == 0 ) iLanguage = _iLanguage;
205
		if ( _pSelected ) {
206
			if ( _pSelected->pVfs->textExists(iLanguage, iPage, iID) ) {
207
				return _pSelected->pVfs->findText(iLanguage, iPage, iID);
208
			}
209
		}
210
 
211
		return missing;
212
	}
213
 
214
	Utils::String CGameDirectories::findText(int iGame, int iLanguage, int iPage, int iID)
215
	{
216
		if ( iLanguage == 0 ) iLanguage = _iLanguage;
217
		if ( iGame == -1 ) {
218
			for ( int game = (GAME_MAX - 1); game >= 0; game-- ) {
219
				SGameDir *gd = _findGameDir(game - 1);
220
				if ( !gd ) continue;
221
				if ( gd->pVfs->textExists(iLanguage, iPage, iID) ) {
222
					return gd->pVfs->findText(iLanguage, iPage, iID);
223
				}
224
			}
225
		}
226
		else {
227
			SGameDir *gd = _findGameDir(iGame);
228
			if ( gd ) {
229
				if ( gd->pVfs->textExists(iLanguage, iPage, iID) ) {
230
					return gd->pVfs->findText(iLanguage, iPage, iID);
231
				}
232
			}
233
		}
234
 
235
		return Utils::String::Null();
236
	}
237
 
238
	Utils::String CGameDirectories::first(int iGame)
239
	{
240
		if ( _pDirs->empty() ) return Utils::String::Null();
241
 
242
		_pCurrentItr = *_pDirs->begin();
243
		if ( iGame > -1 && _pCurrentItr->iGame != iGame ) return next(iGame);
244
		return (_pCurrentItr) ? _pCurrentItr->dir : Utils::String::Null();
245
	}
246
 
247
	Utils::String CGameDirectories::next(int iGame)
248
	{
249
		if ( !_pCurrentItr ) return Utils::String::Null();
250
 
251
		bool found = false;
252
		for(std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
253
			if ( (*itr) == _pCurrentItr ) found = true;
254
			else if ( found && ((*itr)->iGame == iGame || iGame == -1) ) {
255
				_pCurrentItr = *(itr);
256
				return (_pCurrentItr) ? _pCurrentItr->dir : Utils::String::Null();
257
			}
258
		}
259
 
260
		return Utils::String::Null();
261
	}
262
 
263
	Utils::String CGameDirectories::firstShield()
264
	{
265
		if ( !_pSelected ) return Utils::String::Null();
266
		return _pSelected->pVfs->firstShield();
267
	}
268
 
269
	Utils::String CGameDirectories::nextShield()
270
	{
271
		if ( !_pSelected ) return Utils::String::Null();
272
		return _pSelected->pVfs->nextShield();
273
	}
274
 
275
	Utils::String CGameDirectories::firstComponentSection()
276
	{
277
		if ( !_pSelected ) return Utils::String::Null();
278
		return _pSelected->pVfs->firstComponentSection();
279
	}
280
 
281
	Utils::String CGameDirectories::nextComponentSection()
282
	{
283
		if ( !_pSelected ) return Utils::String::Null();
284
		return _pSelected->pVfs->nextComponentSection();
285
	}
286
 
287
	Utils::String CGameDirectories::firstDummySection()
288
	{
289
		if ( !_pSelected ) return Utils::String::Null();
290
		return _pSelected->pVfs->firstDummySection();
291
	}
292
 
293
	Utils::String CGameDirectories::nextDummySection()
294
	{
295
		if ( !_pSelected ) return Utils::String::Null();
296
		return _pSelected->pVfs->nextDummySection();
297
	}
298
 
299
	Utils::String CGameDirectories::firstBodiesSection()
300
	{
301
		if ( !_pSelected ) return Utils::String::Null();
302
		return _pSelected->pVfs->firstBodiesSection();
303
	}
304
 
305
	Utils::String CGameDirectories::nextBodiesSection()
306
	{
307
		if ( !_pSelected ) return Utils::String::Null();
308
		return _pSelected->pVfs->nextBodiesSection();
309
	}
310
 
311
	Utils::String CGameDirectories::firstCockpit()
312
	{
313
		if ( !_pSelected ) return Utils::String::Null();
314
		return _pSelected->pVfs->firstCockpit();
315
	}
316
 
317
	Utils::String CGameDirectories::nextCockpit()
318
	{
319
		if ( !_pSelected ) return Utils::String::Null();
320
		return _pSelected->pVfs->nextCockpit();
321
	}
322
 
323
	std::pair<Utils::String, Utils::String> CGameDirectories::firstLaser()
324
	{
325
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
326
		return _pSelected->pVfs->firstLaser();
327
	}
328
 
329
	std::pair<Utils::String, Utils::String> CGameDirectories::nextLaser()
330
	{
331
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
332
		return _pSelected->pVfs->nextLaser();
333
	}
334
 
335
	std::pair<Utils::String, Utils::String> CGameDirectories::firstMissile()
336
	{
337
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
338
		return _pSelected->pVfs->firstMissile();
339
	}
340
 
341
	std::pair<Utils::String, Utils::String> CGameDirectories::nextMissile()
342
	{
343
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
344
		return _pSelected->pVfs->nextMissile();
345
	}
346
 
347
	SGameDir *CGameDirectories::_findGameDir(int iGame) const
348
	{
349
		if ( iGame == -1 ) {
350
			for ( int game = (GAME_MAX - 1); game >= 0; game-- ) {
351
				SGameDir *gd = _findGameDir(game);
352
				if ( !gd ) continue;
353
				return gd;
354
			}
355
		}
356
		else {
357
			for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
358
				if ( (*itr)->iGame == iGame ) return *itr;
359
			}
360
		}
361
		return NULL;
362
	}
363
 
364
	SGameDir *CGameDirectories::_findGameDir(const Utils::String &dir) const
365
	{
366
		for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
367
			if ( (*itr)->dir.Compare(dir) )
368
				return *itr;
369
		}
370
 
371
		return NULL;
372
	}
373
 
374
	void CGameDirectories::_add(const Utils::String &dir, const Utils::String &name, int iGame, const Utils::String &addon, bool bLoad)
375
	{
376
		SGameDir *gd = new SGameDir;
377
 
378
		gd->dir = dir;
379
		gd->name = name;
380
		gd->iGame = iGame;
381
 
382
		gd->bLoad = bLoad;
383
		gd->pVfs = new CVirtualFileSystem();
384
		gd->pVfs->setLanguage(44);
385
		gd->pVfs->LoadFilesystem(gd->dir);
101 cycrow 386
		gd->pVfs->setAddon(addon);
94 cycrow 387
 
388
		_pDirs->push_back(gd);
389
	}
95 cycrow 390
 
391
	void CGameDirectories::_updateControlledDirs(const Utils::String &mydoc)
392
	{
393
		_lControlledDirs.MemoryClear();
394
 
395
		CFileIO configFile(mydoc + "/Egosoft/pluginmanager.dat");
396
		if ( configFile.startRead() ) {
397
			while(!configFile.atEnd()) {
398
				Utils::String line = configFile.readEndOfLine();
399
				if ( line.token(":", 1).Compare("DirExe") ) {
400
					Utils::String rest = line.tokens(":", 2).removeFirstSpace();
401
					if ( rest.countToken("|" ) > 2 )
402
						_lControlledDirs.pushBack(rest.token("|", 3), rest.token("|", 2));
403
					else
404
						_lControlledDirs.pushBack(rest.token("|", 2), rest.token("|", 1));
405
				}
406
			}
407
		}
408
	}
94 cycrow 409
}