Subversion Repositories spk

Rev

Rev 196 | Rev 202 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 196 Rev 197
Line 6... Line 6...
6
#include "spk.h"
6
#include "spk.h"
7
//#include "ProgressInfo.h"
7
//#include "ProgressInfo.h"
8
 
8
 
9
namespace SPK {
9
namespace SPK {
10
 
10
 
11
	void addLogEntry(int type, const Utils::String &args, Utils::CStringList* errors)
11
	void addLogEntry(int type, const Utils::WString &args, Utils::WStringList* errors)
12
	{
12
	{
13
		if (errors) errors->pushBack(args, ERRORLOG(type));
13
		if (errors) errors->pushBack(args, ERRORLOG(type));
14
	}
14
	}
15
 
15
 
16
 
16
 
17
COriginalFiles::COriginalFiles(const Utils::String &dir) : _sDir(dir)
17
COriginalFiles::COriginalFiles(const Utils::WString &dir) : _sDir(dir)
18
{
18
{
19
}
19
}
20
 
20
 
21
COriginalFiles::~COriginalFiles(void)
21
COriginalFiles::~COriginalFiles(void)
22
{
22
{
Line 58... Line 58...
58
				_storeOverride(f);
58
				_storeOverride(f);
59
		}
59
		}
60
	}
60
	}
61
}
61
}
62
 
62
 
63
void COriginalFiles::backup(CBaseFile *package, Utils::CStringList *errors)
63
void COriginalFiles::backup(CBaseFile *package, Utils::WStringList *errors)
64
{
64
{
65
	// backup any original files before installing
65
	// backup any original files before installing
66
	CLog::log(CLog::Log_Install, 3, "Checking for any original files to backup");
66
	CLog::log(CLog::Log_Install, 3, "Checking for any original files to backup");
67
	CDirIO oDir(_sDir + "/PluginManager/Original");
67
	CDirIO oDir(_sDir + "/PluginManager/Original");
68
	for ( CListNode<C_File> *oNode = _lFiles.Front(); oNode; oNode = oNode->next() )
68
	for ( CListNode<C_File> *oNode = _lFiles.Front(); oNode; oNode = oNode->next() )
Line 86... Line 86...
86
			}
86
			}
87
		}
87
		}
88
	}
88
	}
89
}
89
}
90
 
90
 
91
bool COriginalFiles::backupFile(C_File *f, Utils::CStringList *errors)
91
bool COriginalFiles::backupFile(C_File *f, Utils::WStringList *errors)
92
{
92
{
93
	Utils::WString newDir = L"PluginManager/Original/" + f->getDirectory(NULL).toWString();
93
	Utils::WString newDir = L"PluginManager/Original/" + f->getDirectory(NULL);
94
	CDirIO oDir(_sDir);
94
	CDirIO oDir(_sDir);
95
	if ( oDir.exists(newDir + L"/" + f->filename().toWString()) )
95
	if ( oDir.exists(newDir + L"/" + f->filename()) )
96
		return true;
96
		return true;
97
 
97
 
98
	// make sure the directory exissts
98
	// make sure the directory exissts
99
	if ( !oDir.exists(newDir) )
99
	if ( !oDir.exists(newDir) )
100
	{
100
	{
101
		if ( !oDir.create(newDir) )
101
		if ( !oDir.create(newDir) )
102
		{
102
		{
103
			CLog::logf(CLog::Log_Install, 2, "Unable to create directory to backup original files, %s", newDir.c_str());
103
			CLog::logf(CLog::Log_Install, 2, "Unable to create directory to backup original files, %s", newDir.c_str());
104
			addLogEntry(SPKINSTALL_CREATEDIRECTORY_FAIL, newDir.toString(), errors);
104
			addLogEntry(SPKINSTALL_CREATEDIRECTORY_FAIL, newDir, errors);
105
			return false;
105
			return false;
106
		}
106
		}
107
 
107
 
108
		CLog::logf(CLog::Log_Install, 2, "Created new directory, %s", newDir.c_str());
108
		CLog::logf(CLog::Log_Install, 2, "Created new directory, %s", newDir.c_str());
109
		addLogEntry(SPKINSTALL_CREATEDIRECTORY, newDir.toString(), errors);
109
		addLogEntry(SPKINSTALL_CREATEDIRECTORY, newDir, errors);
110
	}
110
	}
111
 
111
 
112
	// now lets copy the file
112
	// now lets copy the file
113
	CFileIO CopyFile(f->filePointer());
113
	CFileIO CopyFile(f->filePointer());
114
	if(CopyFile.copy(oDir.file(newDir + L"/" + f->filename().toWString())))
114
	if(CopyFile.copy(oDir.file(newDir + L"/" + f->filename())))
115
	{
115
	{
116
		CLog::logf(CLog::Log_Install, 2, "Original file: %s has been backed up", f->getNameDirectory(NULL).c_str());
116
		CLog::logf(CLog::Log_Install, 2, "Original file: %s has been backed up", f->getNameDirectory(NULL).c_str());
117
		addLogEntry(SPKINSTALL_ORIGINAL_BACKUP, f->getNameDirectory(NULL), errors);
117
		addLogEntry(SPKINSTALL_ORIGINAL_BACKUP, f->getNameDirectory(NULL), errors);
118
		return true;
118
		return true;
119
	}
119
	}
Line 123... Line 123...
123
		addLogEntry(SPKINSTALL_ORIGINAL_BACKUP_FAIL, f->getNameDirectory(NULL), errors);
123
		addLogEntry(SPKINSTALL_ORIGINAL_BACKUP_FAIL, f->getNameDirectory(NULL), errors);
124
		return false;
124
		return false;
125
	}
125
	}
126
}
126
}
127
 
127
 
128
bool COriginalFiles::doBackup(C_File *f, Utils::CStringList *errors)
128
bool COriginalFiles::doBackup(C_File *f, Utils::WStringList *errors)
129
{
129
{
130
	bool backed = false;
130
	bool backed = false;
131
 
131
 
132
	C_File *of = this->_getFile(f);
132
	C_File *of = this->_getFile(f);
133
	if ( of ) {
133
	if ( of ) {
134
		// check if the orignal file is already backed up
134
		// check if the orignal file is already backed up
135
		if ( !CFileIO::Exists(_sDir + "/PluginManager/Original/" + f->getNameDirectory(NULL)) )
135
		if ( !CFileIO::Exists(_sDir + L"/PluginManager/Original/" + f->getNameDirectory(NULL)) )
136
		{
136
		{
137
			// lets back up the file now
137
			// lets back up the file now
138
			backed = this->backupFile(of, errors);
138
			backed = this->backupFile(of, errors);
139
		}
139
		}
140
	}
140
	}
Line 150... Line 150...
150
		// update the progress
150
		// update the progress
151
		if ( info ) info->UpdateProgress(files, max);
151
		if ( info ) info->UpdateProgress(files, max);
152
		++files;
152
		++files;
153
 
153
 
154
		C_File *f = oNode->Data();
154
		C_File *f = oNode->Data();
155
		CFileIO of(_sDir + "/PluginManager/Original/" + f->getNameDirectory(NULL));
155
		CFileIO of(_sDir + L"/PluginManager/Original/" + f->getNameDirectory(NULL));
156
		
156
		
157
		if ( of.exists() )
157
		if ( of.exists() )
158
			of.Rename(_sDir + "/" + f->getNameDirectory(NULL));
158
			of.Rename(_sDir + L"/" + f->getNameDirectory(NULL));
159
 
159
 
160
		delete oNode->Data();
160
		delete oNode->Data();
161
	}
161
	}
162
 
162
 
163
	_lFiles.clear();
163
	_lFiles.clear();
164
 
164
 
165
	return files;
165
	return files;
166
}
166
}
167
 
167
 
168
bool COriginalFiles::restoreFile(C_File *f, Utils::CStringList *errors)
168
bool COriginalFiles::restoreFile(C_File *f, Utils::WStringList *errors)
169
{
169
{
170
	bool original = false;
170
	bool original = false;
171
 
171
 
172
	// check if its an original file and restore
172
	// check if its an original file and restore
173
	if ( this->isOriginal(f) )
173
	if ( this->isOriginal(f) )
174
	{
174
	{
175
		CFileIO of(_sDir + "/PluginManager/Original/" + f->getNameDirectory(NULL));
175
		CFileIO of(_sDir + L"/PluginManager/Original/" + f->getNameDirectory(NULL));
176
		if ( of.exists() )
176
		if ( of.exists() )
177
		{
177
		{
178
			original = true;
178
			original = true;
179
			if ( of.Rename(_sDir + "/" + f->getNameDirectory(NULL)) )
179
			if ( of.Rename(_sDir + L"/" + f->getNameDirectory(NULL)) )
180
				addLogEntry(SPKINSTALL_ORIGINAL_RESTORE, f->getNameDirectory(NULL), errors);
180
				addLogEntry(SPKINSTALL_ORIGINAL_RESTORE, f->getNameDirectory(NULL), errors);
181
			else
181
			else
182
				addLogEntry(SPKINSTALL_ORIGINAL_RESTORE_FAIL, f->getNameDirectory(NULL), errors);
182
				addLogEntry(SPKINSTALL_ORIGINAL_RESTORE_FAIL, f->getNameDirectory(NULL), errors);
183
 
183
 
184
			CFileIO backupFile(_sDir + "/PluginManager/Original/Replacements/" + f->getNameDirectory(NULL));
184
			CFileIO backupFile(_sDir + L"/PluginManager/Original/Replacements/" + f->getNameDirectory(NULL));
185
			if ( backupFile.exists() ) backupFile.remove();
185
			if ( backupFile.exists() ) backupFile.remove();
186
 
186
 
187
		}
187
		}
188
	}
188
	}
189
 
189
 
190
	return original;
190
	return original;
191
}
191
}
192
 
192
 
193
 
193
 
194
void COriginalFiles::parse(const Utils::String &data)
194
void COriginalFiles::parse(const Utils::WString &data)
195
{
195
{
196
	C_File *uf = new C_File();
196
	C_File *uf = new C_File();
197
 
197
 
198
	uf->setFileType((FileType)data.token(" ", 1).toLong());
198
	uf->setFileType((FileType)data.token(L" ", 1).toLong());
199
 
199
 
200
	// Extra files also contain a directory, so make sure we split that up
200
	// Extra files also contain a directory, so make sure we split that up
201
	if (uf->fileType() == FILETYPE_EXTRA )
201
	if (uf->fileType() == FILETYPE_EXTRA )
202
	{
202
	{
203
		uf->setDir(data.tokens(" ", 2).tokens(":", 2));
203
		uf->setDir(data.tokens(L" ", 2).tokens(L":", 2));
204
		uf->setFilename(data.tokens(" ", 2).token(":", 1));
204
		uf->setFilename(data.tokens(L" ", 2).token(L":", 1));
205
	}
205
	}
206
	else
206
	else
207
		uf->setFilename(data.tokens(" ", 2));
207
		uf->setFilename(data.tokens(L" ", 2));
208
 
208
 
209
	// set the complete filename, GetNameDirectory returns the directory based on the file type
209
	// set the complete filename, GetNameDirectory returns the directory based on the file type
210
	uf->setFilename(_sDir + "/" + uf->getNameDirectory(NULL));
210
	uf->setFilename(_sDir + L"/" + uf->getNameDirectory(NULL));
211
 
211
 
212
	_lFiles.push_back(uf);
212
	_lFiles.push_back(uf);
213
}
213
}
214
 
214
 
215
void COriginalFiles::update(bool bForce, const CLinkList<C_File> *pFiles)
215
void COriginalFiles::update(bool bForce, const CLinkList<C_File> *pFiles)
216
{
216
{
217
	if ( _lFiles.empty() || bForce )
217
	if ( _lFiles.empty() || bForce )
218
	{
218
	{
219
		_storeFiles(FILETYPE_SCRIPT, "scripts", pFiles);
219
		_storeFiles(FILETYPE_SCRIPT, L"scripts", pFiles);
220
		_storeFiles(FILETYPE_TEXT, "t", pFiles);
220
		_storeFiles(FILETYPE_TEXT, L"t", pFiles);
221
		_storeFiles(FILETYPE_SOUND, "soundtrack", pFiles);
221
		_storeFiles(FILETYPE_SOUND, L"soundtrack", pFiles);
222
		_storeFiles(FILETYPE_EXTRA, "mov", pFiles);
222
		_storeFiles(FILETYPE_EXTRA, L"mov", pFiles);
223
	}
223
	}
224
}
224
}
225
 
225
 
226
bool COriginalFiles::writeData(Utils::CStringList &lines)
226
bool COriginalFiles::writeData(Utils::WStringList &lines)
227
{
227
{
228
	bool addAny = false;
228
	bool addAny = false;
229
 
229
 
230
	if ( !_lFiles.empty() )
230
	if ( !_lFiles.empty() )
231
	{
231
	{
232
		for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
232
		for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
233
		{
233
		{
234
			C_File *uf = node->Data();
234
			C_File *uf = node->Data();
235
			Utils::String uString = "Original: ";
235
			Utils::WString uString = L"Original: ";
236
			uString += Utils::String::Number((long)uf->GetFileType()) + " ";
236
			uString += Utils::WString::Number((long)uf->GetFileType()) + L" ";
237
			uString += uf->filename();
237
			uString += uf->filename();
238
 
238
 
239
			// add the directory for Extra files
239
			// add the directory for Extra files
240
			if ( uf->GetFileType() == FILETYPE_EXTRA && !uf->dir().empty())
240
			if ( uf->GetFileType() == FILETYPE_EXTRA && !uf->dir().empty())
241
			{
241
			{
242
				uString += ":";
242
				uString += L":";
243
				uString += uf->dir();
243
				uString += uf->dir();
244
			}
244
			}
245
 
245
 
246
			lines.pushBack(uString);
246
			lines.pushBack(uString);
247
			addAny = true;
247
			addAny = true;
Line 265... Line 265...
265
 * Original files should be all the scripts/sounds, etc that are in an unmodified game directory
265
 * Original files should be all the scripts/sounds, etc that are in an unmodified game directory
266
 *
266
 *
267
 * Save list of files and check if any packages overright these files
267
 * Save list of files and check if any packages overright these files
268
 * Any file that gets overrighten from this list will automatically be backed up, and restored once the packages are removed
268
 * Any file that gets overrighten from this list will automatically be backed up, and restored once the packages are removed
269
 */
269
 */
270
void COriginalFiles::_storeFiles(FileType filetype, const Utils::String &searchPath, const CLinkList<C_File> *pFiles)
270
void COriginalFiles::_storeFiles(FileType filetype, const Utils::WString &searchPath, const CLinkList<C_File> *pFiles)
271
{
271
{
272
	Utils::String ext = "pck";
272
	Utils::WString ext = L"pck";
273
	switch ( filetype )
273
	switch ( filetype )
274
	{
274
	{
275
		case FILETYPE_SOUND:
275
		case FILETYPE_SOUND:
276
			ext = "mp3";
276
			ext = L"mp3";
277
			break;
277
			break;
278
	}
278
	}
279
 
279
 
280
	CDirIO Dir(_sDir + "/" + searchPath);
280
	CDirIO Dir(_sDir + L"/" + searchPath);
281
 
281
 
282
	Utils::CStringList files;
282
	Utils::WStringList files;
283
	Dir.dirList(files);
283
	Dir.dirList(files);
284
	if (files.empty())
284
	if (files.empty())
285
		return;
285
		return;
286
 
286
 
287
	for(auto itr = files.begin(); itr != files.end(); itr++)
287
	for(auto itr = files.begin(); itr != files.end(); itr++)
288
	{
288
	{
289
		CFileIO File(Dir.file((*itr)->str));
289
		CFileIO File(Dir.file((*itr)->str));
290
		
290
		
291
		if ( File.extension().Compare(ext) )
291
		if ( File.extension().Compare(ext) )
292
			_add(filetype, File.filenameStr(), searchPath, pFiles);
292
			_add(filetype, File.filename(), searchPath, pFiles);
293
	}
293
	}
294
}
294
}
295
 
295
 
296
/**
296
/**
297
 * Get original file
297
 * Get original file
Line 322... Line 322...
322
/**
322
/**
323
 * Adds a file onto the original files list
323
 * Adds a file onto the original files list
324
 *
324
 *
325
 * Checks if it already exists so we dont create multiples
325
 * Checks if it already exists so we dont create multiples
326
 */
326
 */
327
void COriginalFiles::_add(FileType filetype, const Utils::String &filename, const Utils::String &searchPath, const CLinkList<C_File> *pFiles)
327
void COriginalFiles::_add(FileType filetype, const Utils::WString &filename, const Utils::WString &searchPath, const CLinkList<C_File> *pFiles)
328
{
328
{
329
	// dont add plugin manager as original files
329
	// dont add plugin manager as original files
330
	if ( filetype == FILETYPE_SCRIPT && filename.isin("!init.pmanager") )
330
	if ( filetype == FILETYPE_SCRIPT && filename.contains(L"!init.pmanager") )
331
		return;
331
		return;
332
 
332
 
333
	// first check if a matching one exists
333
	// first check if a matching one exists
334
	for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
334
	for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
335
	{
335
	{
Line 355... Line 355...
355
	// add the file entry to the list
355
	// add the file entry to the list
356
	C_File *of = new C_File(filename);
356
	C_File *of = new C_File(filename);
357
	of->setFileType(filetype);
357
	of->setFileType(filetype);
358
	if (filetype == FILETYPE_EXTRA)
358
	if (filetype == FILETYPE_EXTRA)
359
		of->setDir(searchPath);
359
		of->setDir(searchPath);
360
	of->setFilename(_sDir + "/" + of->getNameDirectory(NULL));
360
	of->setFilename(_sDir + L"/" + of->getNameDirectory(NULL));
361
 
361
 
362
	_lFiles.push_back(of);
362
	_lFiles.push_back(of);
363
}
363
}
364
 
364
 
365
void COriginalFiles::_storeOverride(C_File *f)
365
void COriginalFiles::_storeOverride(C_File *f)
366
{
366
{
367
	CDirIO oDir(_sDir + "/PluginManager/Original/Replacements");
367
	CDirIO oDir(_sDir + L"/PluginManager/Original/Replacements");
368
	if ( !oDir.exists() ) oDir.create();
368
	if ( !oDir.exists() ) oDir.create();
369
	CFileIO file(oDir.file(f->getNameDirectory(NULL)));
369
	CFileIO file(oDir.file(f->getNameDirectory(NULL)));
370
	if ( !file.GetDirIO().exists() ) file.GetDirIO().create();
370
	if ( !file.GetDirIO().exists() ) file.GetDirIO().create();
371
	CFileIO fromFile(f->filePointer());
371
	CFileIO fromFile(f->filePointer());
372
	fromFile.copy(file.fullFilename(), true);
372
	fromFile.copy(file.fullFilename(), true);