93 |
cycrow |
1 |
|
|
|
2 |
#include "OriginalFiles.h"
|
|
|
3 |
#include "File.h"
|
|
|
4 |
#include "Logging/Log.h"
|
|
|
5 |
#include "BaseFile.h"
|
|
|
6 |
#include "spk.h"
|
|
|
7 |
//#include "ProgressInfo.h"
|
|
|
8 |
|
|
|
9 |
namespace SPK {
|
|
|
10 |
|
|
|
11 |
void AddLogEntry(int type, CyString args, CyStringList *errors)
|
|
|
12 |
{
|
|
|
13 |
if ( errors ) errors->PushBack(args, ERRORLOG(type));
|
|
|
14 |
}
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
COriginalFiles::COriginalFiles(const Utils::String &dir) : _sDir(dir)
|
|
|
18 |
{
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
COriginalFiles::~COriginalFiles(void)
|
|
|
22 |
{
|
|
|
23 |
reset();
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
int COriginalFiles::count() const
|
|
|
27 |
{
|
|
|
28 |
return _lFiles.size();
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Check for original file
|
|
|
33 |
*
|
|
|
34 |
* Checks if the file is an original file or not
|
|
|
35 |
*/
|
|
|
36 |
bool COriginalFiles::isOriginal(C_File *f) const
|
|
|
37 |
{
|
|
|
38 |
if ( _getFile(f) )
|
|
|
39 |
return true;
|
|
|
40 |
return false;
|
|
|
41 |
}
|
|
|
42 |
|
105 |
cycrow |
43 |
void COriginalFiles::installed(CBaseFile *package)
|
|
|
44 |
{
|
|
|
45 |
for ( CListNode<C_File> *oNode = _lFiles.Front(); oNode; oNode = oNode->next() )
|
|
|
46 |
{
|
|
|
47 |
C_File *of = oNode->Data();
|
|
|
48 |
for ( CListNode<C_File> *checkNode = package->GetFileList()->Front(); checkNode; checkNode = checkNode->next() )
|
|
|
49 |
{
|
|
|
50 |
C_File *f = checkNode->Data();
|
|
|
51 |
|
|
|
52 |
// match the same filetype
|
|
|
53 |
if ( of->GetFileType() != f->GetFileType() )
|
|
|
54 |
continue;
|
|
|
55 |
|
|
|
56 |
// same file
|
|
|
57 |
if ( of->GetFilename().Compare(f->GetFilename()) )
|
|
|
58 |
_storeOverride(f);
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
93 |
cycrow |
63 |
void COriginalFiles::backup(CBaseFile *package, CyStringList *errors)
|
|
|
64 |
{
|
|
|
65 |
// backup any original files before installing
|
|
|
66 |
CLog::log(CLog::Log_Install, 3, "Checking for any original files to backup");
|
|
|
67 |
CDirIO oDir(_sDir + "/PluginManager/Original");
|
|
|
68 |
for ( CListNode<C_File> *oNode = _lFiles.Front(); oNode; oNode = oNode->next() )
|
|
|
69 |
{
|
|
|
70 |
C_File *of = oNode->Data();
|
|
|
71 |
for ( CListNode<C_File> *checkNode = package->GetFileList()->Front(); checkNode; checkNode = checkNode->next() )
|
|
|
72 |
{
|
|
|
73 |
C_File *f = checkNode->Data();
|
|
|
74 |
|
|
|
75 |
// match the same filetype
|
|
|
76 |
if ( of->GetFileType() != f->GetFileType() )
|
|
|
77 |
continue;
|
|
|
78 |
|
|
|
79 |
// same file
|
|
|
80 |
if ( of->GetFilename().Compare(f->GetFilename()) )
|
|
|
81 |
{
|
|
|
82 |
// check if original file already exists (assume already backed up)
|
|
|
83 |
if ( !backupFile(of, errors) )
|
|
|
84 |
continue;
|
|
|
85 |
break;
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
bool COriginalFiles::backupFile(C_File *f, CyStringList *errors)
|
|
|
92 |
{
|
121 |
cycrow |
93 |
Utils::String newDir = "PluginManager/Original/" + f->getDirectory(NULL);
|
93 |
cycrow |
94 |
CDirIO oDir(_sDir);
|
125 |
cycrow |
95 |
if ( oDir.exists(newDir + "/" + f->GetFilename().ToString()) )
|
93 |
cycrow |
96 |
return true;
|
|
|
97 |
|
|
|
98 |
// make sure the directory exissts
|
121 |
cycrow |
99 |
if ( !oDir.exists(newDir) )
|
93 |
cycrow |
100 |
{
|
|
|
101 |
if ( !oDir.Create(newDir) )
|
|
|
102 |
{
|
|
|
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, errors);
|
|
|
105 |
return false;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
CLog::logf(CLog::Log_Install, 2, "Created new directory, %s", newDir.c_str());
|
|
|
109 |
AddLogEntry(SPKINSTALL_CREATEDIRECTORY, newDir, errors);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// now lets copy the file
|
130 |
cycrow |
113 |
CFileIO CopyFile(f->filePointer());
|
93 |
cycrow |
114 |
if ( CopyFile.copy(oDir.File(newDir + "/" + f->GetFilename().ToString()).ToString()) )
|
|
|
115 |
{
|
|
|
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);
|
|
|
118 |
return true;
|
|
|
119 |
}
|
|
|
120 |
else
|
|
|
121 |
{
|
|
|
122 |
CLog::logf(CLog::Log_Install, 2, "Failed to backup the original file: %s", f->GetNameDirectory(NULL).c_str());
|
|
|
123 |
AddLogEntry(SPKINSTALL_ORIGINAL_BACKUP_FAIL, f->GetNameDirectory(NULL), errors);
|
|
|
124 |
return false;
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
bool COriginalFiles::doBackup(C_File *f, CyStringList *errors)
|
|
|
129 |
{
|
|
|
130 |
bool backed = false;
|
|
|
131 |
|
|
|
132 |
C_File *of = this->_getFile(f);
|
|
|
133 |
if ( of ) {
|
|
|
134 |
// check if the orignal file is already backed up
|
|
|
135 |
if ( !CFileIO::Exists(_sDir + "/PluginManager/Original/" + f->GetNameDirectory(NULL).ToString()) )
|
|
|
136 |
{
|
|
|
137 |
// lets back up the file now
|
|
|
138 |
backed = this->backupFile(of, errors);
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return backed;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
int COriginalFiles::restoreAll(CProgressInfo *info, int files, int max)
|
|
|
147 |
{
|
|
|
148 |
for ( CListNode<C_File> *oNode = _lFiles.Front(); oNode; oNode = oNode->next() )
|
|
|
149 |
{
|
|
|
150 |
// update the progress
|
|
|
151 |
if ( info ) info->UpdateProgress(files, max);
|
|
|
152 |
++files;
|
|
|
153 |
|
|
|
154 |
C_File *f = oNode->Data();
|
|
|
155 |
CFileIO of(_sDir + "/PluginManager/Original/" + f->GetNameDirectory(NULL).ToString());
|
|
|
156 |
|
|
|
157 |
if ( of.exists() )
|
|
|
158 |
of.Rename(_sDir + "/" + f->GetNameDirectory(NULL).ToString());
|
|
|
159 |
|
|
|
160 |
delete oNode->Data();
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
_lFiles.clear();
|
|
|
164 |
|
|
|
165 |
return files;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
bool COriginalFiles::restoreFile(C_File *f, CyStringList *errors)
|
|
|
169 |
{
|
|
|
170 |
bool original = false;
|
|
|
171 |
|
|
|
172 |
// check if its an original file and restore
|
|
|
173 |
if ( this->isOriginal(f) )
|
|
|
174 |
{
|
|
|
175 |
CFileIO of(_sDir + "/PluginManager/Original/" + f->GetNameDirectory(NULL).ToString());
|
|
|
176 |
if ( of.exists() )
|
|
|
177 |
{
|
|
|
178 |
original = true;
|
|
|
179 |
if ( of.Rename(_sDir + "/" + f->GetNameDirectory(NULL).ToString()) )
|
|
|
180 |
AddLogEntry(SPKINSTALL_ORIGINAL_RESTORE, f->GetNameDirectory(NULL), errors);
|
|
|
181 |
else
|
|
|
182 |
AddLogEntry(SPKINSTALL_ORIGINAL_RESTORE_FAIL, f->GetNameDirectory(NULL), errors);
|
105 |
cycrow |
183 |
|
|
|
184 |
CFileIO backupFile(_sDir + "/PluginManager/Original/Replacements/" + f->GetNameDirectory(NULL).ToString());
|
|
|
185 |
if ( backupFile.exists() ) backupFile.remove();
|
|
|
186 |
|
93 |
cycrow |
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
return original;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
void COriginalFiles::parse(const Utils::String &data)
|
|
|
195 |
{
|
|
|
196 |
C_File *uf = new C_File();
|
|
|
197 |
|
127 |
cycrow |
198 |
uf->setFileType((FileType)data.token(" ", 1).toLong());
|
93 |
cycrow |
199 |
|
|
|
200 |
// Extra files also contain a directory, so make sure we split that up
|
127 |
cycrow |
201 |
if (uf->fileType() == FILETYPE_EXTRA )
|
93 |
cycrow |
202 |
{
|
|
|
203 |
uf->SetDir(data.tokens(" ", 2).tokens(":", 2));
|
|
|
204 |
uf->SetFilename(data.tokens(" ", 2).token(":", 1));
|
|
|
205 |
}
|
|
|
206 |
else
|
|
|
207 |
uf->SetFilename(data.tokens(" ", 2));
|
|
|
208 |
|
|
|
209 |
// set the complete filename, GetNameDirectory returns the directory based on the file type
|
|
|
210 |
uf->SetFilename(_sDir + "/" + uf->GetNameDirectory(NULL).ToString());
|
|
|
211 |
|
|
|
212 |
_lFiles.push_back(uf);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
void COriginalFiles::update(bool bForce, const CLinkList<C_File> *pFiles)
|
|
|
216 |
{
|
|
|
217 |
if ( _lFiles.empty() || bForce )
|
|
|
218 |
{
|
|
|
219 |
_storeFiles(FILETYPE_SCRIPT, "scripts", pFiles);
|
|
|
220 |
_storeFiles(FILETYPE_TEXT, "t", pFiles);
|
|
|
221 |
_storeFiles(FILETYPE_SOUND, "soundtrack", pFiles);
|
|
|
222 |
_storeFiles(FILETYPE_EXTRA, "mov", pFiles);
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
bool COriginalFiles::writeData(CyStringList *lines)
|
|
|
227 |
{
|
|
|
228 |
bool addAny = false;
|
|
|
229 |
|
|
|
230 |
if ( !_lFiles.empty() )
|
|
|
231 |
{
|
|
|
232 |
for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
|
|
|
233 |
{
|
|
|
234 |
C_File *uf = node->Data();
|
|
|
235 |
Utils::String uString = "Original: ";
|
|
|
236 |
uString += Utils::String::Number((long)uf->GetFileType()) + " ";
|
|
|
237 |
uString += uf->GetFilename().ToString();
|
|
|
238 |
|
|
|
239 |
// add the directory for Extra files
|
|
|
240 |
if ( uf->GetFileType() == FILETYPE_EXTRA && !uf->GetDir().Empty())
|
|
|
241 |
{
|
|
|
242 |
uString += ":";
|
|
|
243 |
uString += uf->GetDir().ToString();
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
lines->PushBack(CyString(uString));
|
|
|
247 |
addAny = true;
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
return addAny;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
void COriginalFiles::reset()
|
|
|
256 |
{
|
|
|
257 |
_lFiles.MemoryClear();
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Update original files list
|
|
|
263 |
*
|
|
|
264 |
* Scan the current directory for any fiels that are already there, ie ones that have not been installed
|
|
|
265 |
* Original files should be all the scripts/sounds, etc that are in an unmodified game directory
|
|
|
266 |
*
|
|
|
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
|
|
|
269 |
*/
|
127 |
cycrow |
270 |
void COriginalFiles::_storeFiles(FileType filetype, const Utils::String &searchPath, const CLinkList<C_File> *pFiles)
|
93 |
cycrow |
271 |
{
|
|
|
272 |
Utils::String ext = "pck";
|
|
|
273 |
switch ( filetype )
|
|
|
274 |
{
|
|
|
275 |
case FILETYPE_SOUND:
|
|
|
276 |
ext = "mp3";
|
|
|
277 |
break;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
CDirIO Dir(_sDir + "/" + searchPath);
|
|
|
281 |
|
|
|
282 |
CyStringList *files = Dir.DirList();
|
|
|
283 |
if ( !files )
|
|
|
284 |
return;
|
|
|
285 |
|
|
|
286 |
for ( SStringList *node = files->Head(); node; node = node->next )
|
|
|
287 |
{
|
|
|
288 |
CFileIO File(Dir.File(node->str));
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
if ( File.extension().Compare(ext) )
|
|
|
292 |
_add(filetype, File.filename(), searchPath, pFiles);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
delete files;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Get original file
|
|
|
300 |
*
|
|
|
301 |
* Finds a matching original file entry and returns it
|
|
|
302 |
*/
|
|
|
303 |
C_File *COriginalFiles::_getFile(C_File *file) const
|
|
|
304 |
{
|
|
|
305 |
if ( !file ) return NULL;
|
|
|
306 |
|
|
|
307 |
for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
|
|
|
308 |
{
|
|
|
309 |
C_File *of = node->Data();
|
|
|
310 |
|
|
|
311 |
// not of the same file type
|
|
|
312 |
if ( of->GetFileType() != file->GetFileType() )
|
|
|
313 |
continue;
|
|
|
314 |
|
|
|
315 |
// same file name, must be the same file
|
|
|
316 |
if ( of->GetFilename().Compare(file->GetFilename()) )
|
|
|
317 |
return of;
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
return NULL;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* Adds a file onto the original files list
|
|
|
326 |
*
|
|
|
327 |
* Checks if it already exists so we dont create multiples
|
|
|
328 |
*/
|
127 |
cycrow |
329 |
void COriginalFiles::_add(FileType filetype, const Utils::String &filename, const Utils::String &searchPath, const CLinkList<C_File> *pFiles)
|
93 |
cycrow |
330 |
{
|
|
|
331 |
// dont add plugin manager as original files
|
|
|
332 |
if ( filetype == FILETYPE_SCRIPT && filename.isin("!init.pmanager") )
|
|
|
333 |
return;
|
|
|
334 |
|
|
|
335 |
// first check if a matching one exists
|
|
|
336 |
for ( CListNode<C_File> *node = _lFiles.Front(); node; node = node->next() )
|
|
|
337 |
{
|
|
|
338 |
C_File *file = node->Data();
|
|
|
339 |
if ( file->GetFileType() != filetype )
|
|
|
340 |
continue;
|
|
|
341 |
|
|
|
342 |
if ( file->GetFilename().Compare(CyString(filename)) )
|
|
|
343 |
return;
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
// check that no packages are currently using them either, not original if being used already
|
|
|
347 |
for ( CListNode<C_File> *node = pFiles->Front(); node; node = node->next() )
|
|
|
348 |
{
|
|
|
349 |
C_File *file = node->Data();
|
|
|
350 |
if ( file->GetFileType() != filetype )
|
|
|
351 |
continue;
|
|
|
352 |
|
|
|
353 |
if ( file->GetFilename().Compare(CyString(filename)) )
|
|
|
354 |
return;
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
// add the file entry to the list
|
127 |
cycrow |
358 |
C_File *of = new C_File(filename);
|
|
|
359 |
of->setFileType(filetype);
|
|
|
360 |
if (filetype == FILETYPE_EXTRA)
|
93 |
cycrow |
361 |
of->SetDir(searchPath);
|
|
|
362 |
of->SetFilename(_sDir + "/" + of->GetNameDirectory(NULL).ToString());
|
|
|
363 |
|
|
|
364 |
_lFiles.push_back(of);
|
|
|
365 |
}
|
|
|
366 |
|
105 |
cycrow |
367 |
void COriginalFiles::_storeOverride(C_File *f)
|
|
|
368 |
{
|
|
|
369 |
CDirIO oDir(_sDir + "/PluginManager/Original/Replacements");
|
121 |
cycrow |
370 |
if ( !oDir.exists() ) oDir.Create();
|
|
|
371 |
CFileIO file(oDir.file(f->GetNameDirectory(NULL).ToString()));
|
|
|
372 |
if ( !file.GetDirIO().exists() ) file.GetDirIO().Create();
|
105 |
cycrow |
373 |
CFileIO fromFile(f->GetFilePointer());
|
|
|
374 |
fromFile.copy(file.fullFilename(), true);
|
93 |
cycrow |
375 |
}
|
105 |
cycrow |
376 |
|
|
|
377 |
}
|