Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / client / src / login_patch.cpp
blob82665aac1d03f3ebd00173e3e51ef1ae5564bfdb
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2020 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Matthew LAGOE (Botanic) <cyberempires@gmail.com>
6 // Copyright (C) 2014-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 // Includes
25 #include "stdpch.h"
27 #include <sys/stat.h>
29 #ifndef NL_OS_WINDOWS
30 #include <unistd.h>
31 #endif
33 #ifdef NL_OS_MAC
34 #include "app_bundle_utils.h"
35 #endif
37 #include <memory>
38 #include <errno.h>
40 #define USE_CURL
42 #ifdef USE_CURL
43 #include <curl/curl.h>
44 #endif
46 #include <zlib.h>
48 #include "nel/misc/debug.h"
49 #include "nel/misc/path.h"
50 #include "nel/misc/thread.h"
51 #include "nel/misc/sha1.h"
52 #include "nel/misc/big_file.h"
53 #include "nel/misc/i18n.h"
54 #include "nel/misc/cmd_args.h"
55 #include "nel/misc/seven_zip.h"
56 #include "nel/web/curl_certificates.h"
58 #include "game_share/bg_downloader_msg.h"
60 #include "login_patch.h"
61 #include "login.h"
62 #include "user_agent.h"
65 #ifndef RY_BG_DOWNLOADER
66 #include "client_cfg.h"
67 #else
68 #include "../client_background_downloader/client_background_downloader.h"
69 #define __CLIENT_INSTALL_EXE__
70 #endif
72 #ifdef NL_OS_WINDOWS
73 #include <direct.h>
74 #endif
77 // Namespaces
81 using namespace std;
82 using namespace NLMISC;
85 extern string VersionName;
86 extern string R2ServerVersion;
88 #ifdef __CLIENT_INSTALL_EXE__
89 extern std::string TheTmpInstallDirectory;
90 extern std::string ClientLauncherUrl;
91 #else
92 std::string TheTmpInstallDirectory = "patch/client_install";
93 #endif
95 extern NLMISC::CCmdArgs Args;
97 // ****************************************************************************
98 // ****************************************************************************
99 // ****************************************************************************
100 // CPatchManager
101 // ****************************************************************************
102 // ****************************************************************************
103 // ****************************************************************************
105 struct EPatchDownloadException : public Exception
107 EPatchDownloadException() : Exception( "Download Error" ) {}
108 EPatchDownloadException( const std::string& str ) : Exception( str ) {}
109 virtual ~EPatchDownloadException() throw(){}
113 CPatchManager *CPatchManager::_Instance = NULL;
115 static std::string ClientRootPath;
117 // ****************************************************************************
118 CPatchManager::CPatchManager() : State("t_state"), DataScanState("t_data_scan_state")
120 DescFilename = "ryzom_xxxxx.idx";
122 #ifdef NL_OS_WINDOWS
123 UpdateBatchFilename = "updt_nl.bat";
124 UpgradeBatchFilename = "upgd_nl.bat";
125 #else
126 UpdateBatchFilename = "updt_nl.sh";
127 UpgradeBatchFilename = "upgd_nl.sh";
128 #endif
130 std::string rootPath;
132 if (ClientCfg.getDefaultConfigLocation(rootPath))
134 // use same directory as client_default.cfg
135 rootPath = CFile::getPath(rootPath);
137 else
139 // use current directory
140 rootPath = CPath::getCurrentPath();
143 setClientRootPath(rootPath);
145 VerboseLog = true;
147 PatchThread = NULL;
148 CheckThread = NULL;
149 InstallThread = NULL;
150 ScanDataThread = NULL;
151 DownloadThread = NULL;
152 Thread = NULL;
154 LogSeparator = "\n";
155 ValidDescFile = false;
157 MustLaunchBatFile = false;
159 DownloadInProgress = false;
160 _AsyncDownloader = NULL;
161 _StateListener = NULL;
162 _StartRyzomAtEnd = true;
165 // ****************************************************************************
166 void CPatchManager::setClientRootPath(const std::string& clientRootPath)
168 ClientRootPath = CPath::standardizePath(clientRootPath);
169 ClientPatchPath = CPath::standardizePath(ClientRootPath + "unpack");
171 // Delete the .sh file because it's not useful anymore
172 std::string fullUpdateBatchFilename = ClientRootPath + UpdateBatchFilename;
174 if (NLMISC::CFile::fileExists(fullUpdateBatchFilename))
175 NLMISC::CFile::deleteFile(fullUpdateBatchFilename);
177 WritableClientDataPath = CPath::standardizePath(ClientRootPath + "data");
179 #ifdef NL_OS_MAC
180 ReadableClientDataPath = CPath::standardizePath(getAppBundlePath() + "/Contents/Resources/data");
181 #elif defined(NL_OS_UNIX)
182 ReadableClientDataPath = CPath::standardizePath(getRyzomSharePrefix() + "/data");
183 if (CFile::isDirectory(ReadableClientDataPath)) ReadableClientDataPath.clear();
184 #else
185 ReadableClientDataPath.clear();
186 #endif
188 if (ReadableClientDataPath.empty()) ReadableClientDataPath = WritableClientDataPath;
191 // ****************************************************************************
192 void CPatchManager::setErrorMessage(const std::string &message)
194 _ErrorMessage = message;
197 // ****************************************************************************
198 void CPatchManager::forceStopCheckThread()
200 PatchThread->StopAsked = true;
203 // ****************************************************************************
204 void CPatchManager::forceStopPatchThread()
206 CheckThread->StopAsked = true;
209 // ****************************************************************************
210 void CPatchManager::init(const std::vector<std::string>& patchURIs, const std::string &sServerPath, const std::string &sServerVersion)
212 uint i;
213 PatchServers.clear();
215 for (i=0; i<patchURIs.size(); ++i)
217 PatchServers.push_back(CPatchServer(patchURIs[i]));
220 srand(NLMISC::CTime::getSecondsSince1970());
221 UsedServer = (sint)(((double)rand() / ((double)RAND_MAX+1.0)) * (double)PatchServers.size());
223 ServerPath = CPath::standardizePath (sServerPath);
224 ServerVersion = sServerVersion;
226 string::size_type pos = ServerPath.find ("@");
227 if (pos != string::npos)
228 DisplayedServerPath = "http://" + ServerPath.substr (pos+1);
229 else
230 DisplayedServerPath = ServerPath;
232 NLMISC::CFile::createDirectory(ClientPatchPath);
233 NLMISC::CFile::createDirectory(WritableClientDataPath);
236 // try to read the version file from the server (that will replace the version number)
239 CConfigFile *cf;
241 #ifdef RY_BG_DOWNLOADER
242 cf = &theApp.ConfigFile;
243 #else
244 cf = &ClientCfg.ConfigFile;
245 #endif
247 // App name matches Domain on the SQL server
248 std::string appName = cf->getVarPtr("Application")
249 ? cf->getVar("Application").asString(0)
250 : "default";
252 std::string versionFileName = appName + ".version";
253 getServerFile(versionFileName);
255 // ok, we have the file, extract version number (aka build number) and the
256 // version name if present
258 CIFile versionFile(ClientPatchPath + versionFileName);
259 char buffer[1024];
260 versionFile.getline(buffer, 1024);
261 CSString line(buffer);
263 #ifdef NL_DEBUG
264 CConfigFile::CVar *forceVersion = cf->getVarPtr("ForceVersion");
266 if (forceVersion != NULL)
268 line = forceVersion->asString();
270 #endif
272 // Use the version specified in this file, if the file does not contain an asterisk
273 if (line[0] != '*')
275 ServerVersion = line.firstWord(true);
276 VersionName = line.firstWord(true);
279 // force the R2ServerVersion
280 R2ServerVersion = ServerVersion;
282 #ifdef __CLIENT_INSTALL_EXE__
284 //The install program load a the url of the mini web site in the patch directory
286 std::string clientLauncherUrl = "client_launcher_url.txt";
287 bool ok = true;
290 uint32 nServerVersion;
291 fromString(ServerVersion, nServerVersion);
292 std::string url = toString("%05u/%s", nServerVersion, clientLauncherUrl.c_str());
293 // The client version is different from the server version : download new description file
294 getServerFile(url.c_str(), false); // For the moment description file is not zipped
296 catch (...)
298 // fallback to patch root directory
301 getServerFile(clientLauncherUrl.c_str(), false); // For the moment description file is not zipped
303 catch(...)
305 ok = false;
309 if (ok)
311 CIFile versionFile;
312 if (versionFile.open(ClientPatchPath+clientLauncherUrl) )
314 char buffer[1024];
315 versionFile.getline(buffer, 1024);
316 ClientLauncherUrl = std::string(buffer);
320 #endif
322 catch (...)
324 // no version file
328 // retrieve the current client version, according to .idx
329 readClientVersionAndDescFile();
332 // ***************************************************************************
333 void CPatchManager::readClientVersionAndDescFile()
337 ValidDescFile = false;
338 vector<string> vFiles;
339 CPath::getPathContent(ClientPatchPath, false, false, true, vFiles);
340 uint32 nVersion = 0xFFFFFFFF;
341 uint32 nNewVersion;
342 for (uint32 i = 0; i < vFiles.size(); ++i)
344 string sName = NLMISC::CFile::getFilename(vFiles[i]);
345 string sExt = NLMISC::CFile::getExtension(sName);
346 string sBase = sName.substr(0, sName.rfind('_'));
347 if ((sExt == "idx") && (sBase == "ryzom"))
349 string val = sName.substr(sName.rfind('_')+1, 5);
350 if (fromString(val, nNewVersion) && ((nNewVersion > nVersion) || (nVersion == 0xFFFFFFFF)))
351 nVersion = nNewVersion;
354 if (nVersion != 0xFFFFFFFF)
355 readDescFile(nVersion);
356 else
357 DescFilename = "unknown";
358 ValidDescFile = true;
360 catch(const Exception &)
362 nlwarning("EXCEPTION CATCH: readClientVersionAndDescFile() failed - not important");
363 // Not important that there is no desc file
367 // ****************************************************************************
368 void CPatchManager::startCheckThread(bool includeBackgroundPatch)
370 if (CheckThread != NULL)
372 nlwarning ("check thread is already running");
373 return;
375 if (Thread != NULL)
377 nlwarning ("a thread is already running");
378 return;
381 _ErrorMessage.clear();
383 CheckThread = new CCheckThread(includeBackgroundPatch);
384 nlassert (CheckThread != NULL);
386 Thread = IThread::create (CheckThread);
387 nlassert (Thread != NULL);
388 Thread->start ();
391 // ****************************************************************************
392 bool CPatchManager::isCheckThreadEnded(bool &ok)
394 if (CheckThread == NULL)
396 ok = false;
397 return true;
400 bool end = CheckThread->Ended;
401 if (end)
403 ok = CheckThread->CheckOk;
404 stopCheckThread();
407 return end;
410 // ****************************************************************************
411 void CPatchManager::stopCheckThread()
413 if(CheckThread && Thread)
415 Thread->wait();
416 delete Thread;
417 Thread = NULL;
418 delete CheckThread;
419 CheckThread = NULL;
423 // Return the position of the inserted/found category
424 sint32 updateCat(vector<CPatchManager::SPatchInfo::SCat> &rOutVec, CPatchManager::SPatchInfo::SCat &rInCat)
426 uint32 i;
427 for (i = 0; i < rOutVec.size(); ++i)
429 if (rOutVec[i].Name == rInCat.Name)
430 break;
433 if (i == rOutVec.size())
435 rOutVec.push_back(rInCat);
437 else
439 rOutVec[i].Size += rInCat.Size;
440 rOutVec[i].FinalFileSize += rInCat.FinalFileSize;
441 rOutVec[i].SZipSize += rInCat.SZipSize;
444 return i;
447 // ****************************************************************************
448 void CPatchManager::getInfoToDisp(SPatchInfo &piOut)
450 piOut.NonOptCat.clear();
451 piOut.OptCat.clear();
452 piOut.ReqCat.clear();
454 // Convert FilesToPatch vector that must be initialized into something human readable
455 uint32 i;
456 for (i = 0; i < FilesToPatch.size(); ++i)
458 SFileToPatch &rFTP = FilesToPatch[i];
459 // Find the category of the file
460 sint32 nCat = -1;
461 const CBNPCategorySet &rAllCats = DescFile.getCategories();
462 for (uint32 j = 0; j < rAllCats.categoryCount(); ++j)
464 const CBNPCategory &rCat = rAllCats.getCategory(j);
465 for (uint32 k = 0; k < rCat.fileCount(); ++k)
467 if (rCat.getFile(k) == rFTP.FileName)
469 nCat = j;
470 break;
474 if (nCat != -1)
475 break;
478 if (nCat != -1)
480 // Add the category found, if already there just update the size
481 const CBNPCategory &rCat = rAllCats.getCategory(nCat);
482 string sCatName = rCat.getName();
483 // Size of all patches
484 uint32 nTotalPatchesSize = 0;
485 for (uint32 j = 0; j < rFTP.PatcheSizes.size(); ++j)
486 nTotalPatchesSize += rFTP.PatcheSizes[j];
488 SPatchInfo::SCat c;
489 c.Name = sCatName;
490 c.Size = nTotalPatchesSize;
491 c.SZipSize = rFTP.SZFileSize;
492 c.FinalFileSize = rFTP.FinalFileSize;
493 if (!rCat.getCatRequired().empty())
495 // Ensure the required cat exists
496 SPatchInfo::SCat rc;
497 rc.Name = rCat.getCatRequired();
498 c.Req = updateCat(piOut.ReqCat, rc);
501 // In which category of category should we add it ?
502 if (rCat.isOptional())
504 if (rCat.isHidden())
505 updateCat(piOut.ReqCat, c);
506 else
507 updateCat(piOut.OptCat, c);
509 else
511 updateCat(piOut.NonOptCat, c);
516 for (i = 0; i < OptionalCat.size(); ++i)
518 const CBNPCategory *pCat = DescFile.getCategories().getCategory(OptionalCat[i]);
519 nlassert(pCat != NULL);
521 SPatchInfo::SCat c;
522 c.Name = pCat->getName();
524 if (!pCat->getCatRequired().empty())
526 // Ensure the required cat exists
527 SPatchInfo::SCat rc;
528 rc.Name = pCat->getCatRequired();
529 c.Req = updateCat(piOut.ReqCat, rc);
532 updateCat(piOut.OptCat, c);
536 // ****************************************************************************
537 // TODO : use selected categories to patch a list of files
538 void CPatchManager::startPatchThread(const vector<string> &CategoriesSelected, bool applyPatch)
540 if (PatchThread != NULL)
542 nlwarning ("check thread is already running");
543 return;
545 if (Thread != NULL)
547 nlwarning ("a thread is already running");
548 return;
551 _ErrorMessage.clear();
553 PatchThread = new CPatchThread(applyPatch);
554 nlassert (PatchThread != NULL);
556 // Select all the files we have to patch depending on non-optional categories and selected categories
557 uint32 i, j, k;
558 // Add non-optional categories
559 vector<string> CatsSelected = CategoriesSelected;
560 PatchThread->clear();
561 const CBNPCategorySet &rAllCats = DescFile.getCategories();
562 for (i = 0; i < rAllCats.categoryCount(); ++i)
564 const CBNPCategory &rCat = rAllCats.getCategory(i);
565 if (!rCat.isOptional())
566 CatsSelected.push_back(rCat.getName());
569 // Add all required categories
570 uint32 nSize = (uint32)CatsSelected.size();
573 nSize = (uint32)CatsSelected.size();
575 for (i = 0; i < CatsSelected.size(); ++i)
577 const CBNPCategory *pCat = rAllCats.getCategory(CatsSelected[i]);
578 if (pCat == NULL) continue;
579 if (pCat->getCatRequired().empty()) continue;
580 // Check if the category required is already present
581 for (j = 0; j < CatsSelected.size(); ++j)
583 const CBNPCategory *pCat2 = rAllCats.getCategory(CatsSelected[j]);
584 if (pCat2->getName() == pCat->getCatRequired())
585 break;
587 // Not present ?
588 if (j == CatsSelected.size())
590 CatsSelected.push_back(pCat->getCatRequired());
591 break;
595 while(nSize != CatsSelected.size());
597 // Select files
598 for (i = 0; i < CatsSelected.size(); ++i)
600 // Find the category from the name
601 const CBNPCategory *pCat = rAllCats.getCategory(CatsSelected[i]);
602 if (pCat != NULL)
604 for (j = 0; j < pCat->fileCount(); ++j)
606 const string &rFilename = pCat->getFile(j);
607 const CBNPFileSet &rFileSet = DescFile.getFiles();
608 const CBNPFile *pFile = rFileSet.getFileByName(rFilename);
609 if (pFile != NULL)
611 // Look if it's a file to patch
612 for (k = 0; k < FilesToPatch.size(); ++k)
613 if (FilesToPatch[k].FileName == pFile->getFileName())
614 break;
616 if (k < FilesToPatch.size())
618 FilesToPatch[k].Incremental = pCat->isIncremental();
619 if (!pCat->getUnpackTo().empty())
620 FilesToPatch[k].ExtractPath = CPath::standardizePath(pCat->getUnpackTo());
622 PatchThread->add(FilesToPatch[k]);
624 // Close opened big files
625 CBigFile::getInstance().remove(FilesToPatch[k].FileName);
632 // Launch the thread
633 Thread = IThread::create (PatchThread);
634 nlassert (Thread != NULL);
635 Thread->start ();
638 // ****************************************************************************
639 bool CPatchManager::isPatchThreadEnded (bool &ok)
641 if (PatchThread == NULL)
643 ok = false;
644 return true;
647 bool end = PatchThread->Ended;
648 if (end)
650 ok = PatchThread->PatchOk;
651 stopPatchThread();
654 return end;
657 // ****************************************************************************
658 // Called in main thread
659 bool CPatchManager::getThreadState (std::string &stateOut, vector<string> &stateLogOut)
661 if ((PatchThread == NULL) && (CheckThread == NULL) && (ScanDataThread==NULL))
662 return false;
664 // clear output
665 stateOut.clear();
666 stateLogOut.clear();
668 // Get access to the state
669 bool changed= false;
671 CSynchronized<CState>::CAccessor as(&State);
672 CState &rState= as.value();
673 if (rState.StateChanged)
675 // and retrieve info
676 changed= true;
677 stateOut = rState.State;
678 stateLogOut = rState.StateLog;
679 // clear state
680 rState.StateLog.clear();
681 rState.StateChanged= false;
685 // verbose log
686 if (isVerboseLog() && !stateLogOut.empty())
687 for (uint32 i = 0; i < stateLogOut.size(); ++i)
688 nlinfo("%s", stateLogOut[i].c_str());
690 return changed;
693 // ****************************************************************************
694 void CPatchManager::stopPatchThread()
696 if(PatchThread && Thread)
698 Thread->wait();
699 delete Thread;
700 Thread = NULL;
701 delete PatchThread;
702 PatchThread = NULL;
706 // ****************************************************************************
707 void CPatchManager::deleteBatchFile()
709 deleteFile(ClientRootPath + UpdateBatchFilename, false, false);
712 // ****************************************************************************
713 void CPatchManager::createBatchFile(CProductDescriptionForClient &descFile, bool wantRyzomRestart, bool useBatchFile)
715 uint nblab = 0;
717 std::string content;
719 // Unpack files with category ExtractPath non empty
720 const CBNPCategorySet &rDescCats = descFile.getCategories();
721 OptionalCat.clear();
723 for (uint32 i = 0; i < rDescCats.categoryCount(); ++i)
725 // For all optional categories check if there is a 'file to patch' in it
726 const CBNPCategory &rCat = rDescCats.getCategory(i);
728 nlinfo("Category = %s", rCat.getName().c_str());
730 if (!rCat.getUnpackTo().empty())
731 for (uint32 j = 0; j < rCat.fileCount(); ++j)
733 string rFilename = ClientPatchPath + rCat.getFile(j);
735 nlinfo("\tFileName = %s", rFilename.c_str());
737 // Extract to patch
738 vector<string> vFilenames;
740 bool result = false;
744 result = bnpUnpack(rFilename, ClientPatchPath, vFilenames);
746 catch(...)
748 throw;
751 if (!result)
753 // TODO: handle exception?
754 string err = toString("Error unpacking %s", rFilename.c_str());
756 throw Exception (err);
758 else
760 for (uint32 fff = 0; fff < vFilenames.size (); fff++)
762 // this file must be moved
763 string fullDstPath = CPath::standardizePath(rCat.getUnpackTo()); // to be sure there is a / at the end
764 NLMISC::CFile::createDirectoryTree(fullDstPath);
766 std::string FileName = vFilenames[fff];
768 bool succeeded = false;
770 if (!useBatchFile)
772 // don't check result, because it's possible the olk file doesn't exist
773 CFile::deleteFile(fullDstPath + FileName);
775 // try to move it, if fails move it later in a script
776 if (CFile::moveFile(fullDstPath + FileName, ClientPatchPath + FileName))
777 succeeded = true;
780 // if we didn't succeed to delete or move the file, create a batch file anyway
781 if (!succeeded)
783 string batchRelativeDstPath;
785 // should be always true
786 if (fullDstPath.compare(0, ClientRootPath.length(), ClientRootPath) == 0)
788 batchRelativeDstPath = fullDstPath.substr(ClientRootPath.length()) + FileName;
790 else
792 batchRelativeDstPath = fullDstPath + FileName;
795 #ifdef NL_OS_WINDOWS
796 // only fix backslashes for .bat
797 batchRelativeDstPath = CPath::standardizeDosPath(batchRelativeDstPath);
799 // use DSTPATH and SRCPATH variables and append filenames
800 string realDstPath = toString("\"%%ROOTPATH%%\\%s\"", batchRelativeDstPath.c_str());
801 string realSrcPath = toString("\"%%UNPACKPATH%%\\%s\"", FileName.c_str());
803 content += toString(":loop%u\n", nblab);
804 content += toString("attrib -r -a -s -h %s\n", realDstPath.c_str());
805 content += toString("del %s\n", realDstPath.c_str());
806 content += toString("if exist %s goto loop%u\n", realDstPath.c_str(), nblab);
807 content += toString("move %s %s\n", realSrcPath.c_str(), realDstPath.c_str());
808 #else
809 // use DSTPATH and SRCPATH variables and append filenames
810 string realDstPath = toString("\"$ROOTPATH/%s\"", batchRelativeDstPath.c_str());
811 string realSrcPath = toString("\"$UNPACKPATH/%s\"", FileName.c_str());
813 content += toString("rm -rf %s\n", realDstPath.c_str());
814 content += toString("mv %s %s\n", realSrcPath.c_str(), realDstPath.c_str());
815 #endif
817 content += "\n";
820 nblab++;
826 std::string patchDirectory = CPath::standardizePath(ClientRootPath + "patch");
828 // Finalize batch file
829 if (NLMISC::CFile::isExists(patchDirectory) && NLMISC::CFile::isDirectory(patchDirectory))
831 std::string patchContent;
833 vector<string> vFileList;
834 CPath::getPathContent (patchDirectory, false, false, true, vFileList, NULL, false);
836 for(uint32 i = 0; i < vFileList.size(); ++i)
838 bool succeeded = false;
840 if (!useBatchFile)
842 if (CFile::deleteFile(vFileList[i]))
843 succeeded = true;
846 // if we didn't succeed to delete, create a batch file anyway
847 if (!succeeded)
849 #ifdef NL_OS_WINDOWS
850 patchContent += toString("del \"%%ROOTPATH%%\\patch\\%s\"\n", vFileList[i].c_str());
851 #else
852 patchContent += toString("rm -f \"$ROOTPATH/patch/%s\"\n", vFileList[i].c_str());
853 #endif
857 if (!patchContent.empty())
859 #ifdef NL_OS_WINDOWS
860 content += ":looppatch\n";
862 content += patchContent;
864 content += "rd /Q /S \"%%ROOTPATH%%\\patch\"\n";
865 content += "if exist \"%%ROOTPATH%%\\patch\" goto looppatch\n";
866 #else
867 content += "rm -rf \"$ROOTPATH/patch\"\n";
868 #endif
870 else
872 CFile::deleteDirectory(patchDirectory);
876 if (!content.empty())
878 deleteBatchFile();
880 // batch full path
881 std::string batchFilename = ClientRootPath + UpdateBatchFilename;
883 // write windows .bat format else write sh format
884 FILE *fp = nlfopen (batchFilename, "wt");
886 if (fp == NULL)
888 string err = toString("Can't open file '%s' for writing: code=%d %s (error code 29)", batchFilename.c_str(), errno, strerror(errno));
889 throw Exception (err);
891 else
893 nlinfo("Creating %s...", batchFilename.c_str());
896 string contentPrefix;
898 //use bat if windows if not use sh
899 #ifdef NL_OS_WINDOWS
900 contentPrefix += "@echo off\n";
901 contentPrefix += "set RYZOM_CLIENT=%~1\n";
902 contentPrefix += "set UNPACKPATH=%~2\n";
903 contentPrefix += "set ROOTPATH=%~3\n";
904 contentPrefix += "set STARTUPPATH=%~4\n";
905 contentPrefix += toString("set UPGRADE_FILE=%%ROOTPATH%%\\%s\n", UpgradeBatchFilename.c_str());
906 contentPrefix += "\n";
907 contentPrefix += "set LOGIN=%~5\n";
908 contentPrefix += "set PASSWORD=%~6\n";
909 contentPrefix += "set SHARDID=%~7\n";
910 #else
911 contentPrefix += "#!/bin/sh\n";
912 contentPrefix += "export RYZOM_CLIENT=\"$1\"\n";
913 contentPrefix += "export UNPACKPATH=\"$2\"\n";
914 contentPrefix += "export ROOTPATH=\"$3\"\n";
915 contentPrefix += "export STARTUPPATH=\"$4\"\n";
916 contentPrefix += toString("export UPGRADE_FILE=$ROOTPATH/%s\n", UpgradeBatchFilename.c_str());
917 contentPrefix += "\n";
918 contentPrefix += "LOGIN=\"$5\"\n";
919 contentPrefix += "PASSWORD=\"$6\"\n";
920 contentPrefix += "SHARDID=\"$7\"\n";
921 #endif
923 contentPrefix += "\n";
925 string contentSuffix;
927 // if we need to restart Ryzom, we need to launch it in batch
928 std::string additionalParams;
930 if (Args.haveLongArg("profile"))
932 additionalParams = "--profile " + Args.getLongArg("profile").front();
935 #ifdef NL_OS_WINDOWS
936 // launch upgrade script if present (it'll execute additional steps like moving or deleting files)
937 contentSuffix += "if exist \"%UPGRADE_FILE%\" call \"%UPGRADE_FILE%\"\n";
939 if (wantRyzomRestart)
941 // client shouldn't be in memory anymore else it couldn't be overwritten
942 contentSuffix += toString("start \"\" /D \"%%STARTUPPATH%%\" \"%%RYZOM_CLIENT%%\" %s \"%%LOGIN%%\" \"%%PASSWORD%%\" \"%%SHARDID%%\"\n", additionalParams.c_str());
944 #else
945 if (wantRyzomRestart)
947 // wait until client not in memory anymore
948 contentSuffix += toString("until ! pgrep -x \"%s\" > /dev/null; do sleep 1; done\n", CFile::getFilename(RyzomFilename).c_str());
951 // launch upgrade script if present (it'll execute additional steps like moving or deleting files)
952 contentSuffix += "if [ -e \"$UPGRADE_FILE\" ]; then chmod +x \"$UPGRADE_FILE\" && \"$UPGRADE_FILE\"; fi\n\n";
954 // be sure file is executable
955 contentSuffix += "chmod +x \"$RYZOM_CLIENT\"\n\n";
957 if (wantRyzomRestart)
959 // change to previous client directory
960 contentSuffix += "cd \"$STARTUPPATH\"\n\n";
962 // launch new client
963 #ifdef NL_OS_MAC
964 // use exec command under OS X
965 contentSuffix += toString("exec \"$RYZOM_CLIENT\" %s \"$LOGIN\" \"$PASSWORD\" \"$SHARDID\"\n", additionalParams.c_str());
966 #else
967 contentSuffix += toString("\"$RYZOM_CLIENT\" %s \"$LOGIN\" \"$PASSWORD\" \"$SHARDID\" &\n", additionalParams.c_str());
968 #endif
970 #endif
972 // append content of script
973 fputs(contentPrefix.c_str(), fp);
974 fputs(content.c_str(), fp);
975 fputs(contentSuffix.c_str(), fp);
977 bool writeError = ferror(fp) != 0;
978 bool diskFull = ferror(fp) && errno == 28 /* ENOSPC */;
979 fclose(fp);
980 if (diskFull)
982 throw NLMISC::EDiskFullError(batchFilename.c_str());
984 if (writeError)
986 throw NLMISC::EWriteError(batchFilename.c_str());
991 // ****************************************************************************
992 void CPatchManager::executeBatchFile()
994 // normal quit
995 extern void quitCrashReport ();
996 quitCrashReport ();
998 bool r2Mode = false;
1000 #ifndef RY_BG_DOWNLOADER
1001 r2Mode = ClientCfg.R2Mode;
1002 #endif
1004 std::string batchFilename;
1006 std::vector<std::string> arguments;
1008 std::string startupPath = Args.getStartupPath();
1010 // 3 first parameters are Ryzom client full path, patch directory full path and client root directory full path
1011 #ifdef NL_OS_WINDOWS
1012 batchFilename = CPath::standardizeDosPath(ClientRootPath);
1014 arguments.push_back(CPath::standardizeDosPath(RyzomFilename));
1015 arguments.push_back(CPath::standardizeDosPath(ClientPatchPath));
1016 arguments.push_back(CPath::standardizeDosPath(ClientRootPath));
1017 arguments.push_back(CPath::standardizeDosPath(startupPath));
1018 #else
1019 batchFilename = ClientRootPath;
1021 arguments.push_back(RyzomFilename);
1022 arguments.push_back(ClientPatchPath);
1023 arguments.push_back(ClientRootPath);
1024 arguments.push_back(startupPath);
1025 #endif
1027 // log parameters passed to Ryzom client
1028 nlinfo("Restarting Ryzom...");
1029 nlinfo("RyzomFilename = %s", RyzomFilename.c_str());
1030 nlinfo("ClientPatchPath = %s", ClientPatchPath.c_str());
1031 nlinfo("ClientRootPath = %s", ClientRootPath.c_str());
1032 nlinfo("StartupPath = %s", startupPath.c_str());
1034 batchFilename += UpdateBatchFilename;
1036 // make script executable
1037 CFile::setRWAccess(batchFilename);
1038 CFile::setExecutable(batchFilename);
1040 // append login, password and shard
1041 if (!LoginLogin.empty())
1043 arguments.push_back(LoginLogin);
1045 if (!LoginPassword.empty())
1047 // encode password in hexadecimal to avoid invalid characters on command-line
1048 arguments.push_back("0x" + toHexa(LoginPassword));
1050 if (!r2Mode)
1052 arguments.push_back(toString(LoginShardId));
1057 // launchProgram with array of strings as argument will escape arguments with spaces
1058 if (!launchProgramArray(batchFilename, arguments, false))
1060 // error occurs during the launch
1061 string str = toString("Can't execute '%s': code=%d %s (error code 30)", batchFilename.c_str(), errno, strerror(errno));
1062 throw Exception (str);
1066 // ****************************************************************************
1067 void CPatchManager::reboot()
1069 onFileInstallFinished(); //In install program wait for the player to select in the gui of the install program if we want to Launch Ryzom or not
1070 createBatchFile(DescFile, _StartRyzomAtEnd);
1071 executeBatchFile();
1075 // ****************************************************************************
1076 int CPatchManager::getTotalFilesToGet()
1078 if (CheckThread != NULL)
1079 return CheckThread->TotalFileToCheck;
1081 if (PatchThread != NULL)
1082 return PatchThread->getNbFileToPatch();
1084 if (ScanDataThread != NULL)
1085 return ScanDataThread->TotalFileToScan;
1087 return 1;
1090 // ****************************************************************************
1091 int CPatchManager::getCurrentFilesToGet()
1093 if (CheckThread != NULL)
1094 return CheckThread->CurrentFileChecked;
1096 if (PatchThread != NULL)
1097 return PatchThread->getCurrentFilePatched();
1099 if (ScanDataThread != NULL)
1100 return ScanDataThread->CurrentFileScanned;
1102 return 1;
1105 // ****************************************************************************
1106 int CPatchManager::getPatchingSize()
1108 if (PatchThread != NULL)
1109 return PatchThread->getPatchingSize();
1110 return 0;
1113 // ****************************************************************************
1114 float CPatchManager::getCurrentFileProgress() const
1116 if (PatchThread != NULL)
1117 return PatchThread->getCurrentFileProgress();
1118 return 0.f;
1121 // ****************************************************************************
1122 void CPatchManager::setRWAccess (const string &filename, bool bThrowException)
1124 string s = CI18N::get("uiSetAttrib") + " " + CFile::getFilename(filename);
1125 setState(true, s);
1127 if (!NLMISC::CFile::setRWAccess(filename) && bThrowException)
1129 s = CI18N::get("uiAttribErr") + " " + CFile::getFilename(filename) + " (" + toString(errno) + "," + strerror(errno) + ")";
1130 setState(true, s);
1131 throw Exception (s);
1135 // ****************************************************************************
1136 string CPatchManager::deleteFile (const string &filename, bool bThrowException, bool bWarning)
1138 string s = CI18N::get("uiDelFile") + " " + CFile::getFilename(filename);
1139 setState(true, s);
1141 if (!NLMISC::CFile::fileExists(filename))
1143 s = CI18N::get("uiDelNoFile");
1144 setState(true, s);
1145 return s;
1148 if (!NLMISC::CFile::deleteFile(filename))
1150 s = CI18N::get("uiDelErr") + " " + CFile::getFilename(filename) + " (" + toString(errno) + "," + strerror(errno) + ")";
1151 if(bWarning)
1152 setState(true, s);
1153 if(bThrowException)
1154 throw Exception (s);
1155 return s;
1157 return "";
1160 // ****************************************************************************
1161 void CPatchManager::renameFile (const string &src, const string &dst)
1163 string s = CI18N::get("uiRenameFile") + " " + NLMISC::CFile::getFilename(src);
1164 setState(true, s);
1166 if (!NLMISC::CFile::moveFile(dst, src))
1168 s = CI18N::get("uiRenameErr") + " " + src + " -> " + dst + " (" + toString(errno) + "," + strerror(errno) + ")";
1169 setState(true, s);
1170 throw Exception (s);
1174 // ****************************************************************************
1175 // Take care this function is called by the thread
1176 void CPatchManager::setState (bool bOutputToLog, const string &ucsNewState)
1179 CSynchronized<CState>::CAccessor as(&State);
1180 CState &rState= as.value();
1181 rState.State= ucsNewState;
1182 if(bOutputToLog)
1183 rState.StateLog.push_back(ucsNewState);
1184 rState.StateChanged= true;
1186 if (_StateListener)
1188 _StateListener->setState(bOutputToLog, ucsNewState);
1192 // ****************************************************************************
1193 void CPatchManager::touchState ()
1196 CSynchronized<CState>::CAccessor as(&State);
1197 as.value().StateChanged= true;
1201 // ****************************************************************************
1202 string CPatchManager::getClientVersion()
1204 if (!ValidDescFile)
1205 return "";
1207 return toString(DescFile.getFiles().getVersionNumber());
1210 // ****************************************************************************
1211 void CPatchManager::readDescFile(sint32 nVersion)
1213 DescFilename = toString("ryzom_%05d.idx", nVersion);
1214 string srcName = ClientPatchPath + DescFilename;
1215 DescFile.clear();
1216 if (!DescFile.load(srcName))
1217 throw Exception ("Can't open file '%s'", srcName.c_str ());
1219 uint cat;
1221 if (ClientRootPath != "./")
1223 // fix relative paths
1224 for (cat = 0; cat < DescFile.getCategories().categoryCount(); ++cat)
1226 CBNPCategory &category = const_cast<CBNPCategory &>(DescFile.getCategories().getCategory(cat));
1228 std::string unpackTo = category.getUnpackTo();
1230 if (unpackTo.substr(0, 1) == ".")
1232 unpackTo = CPath::makePathAbsolute(unpackTo, ClientRootPath, true);
1233 category.setUnpackTo(unpackTo);
1238 // patch category for current platform
1239 std::string platformPatchCategory;
1241 #if defined(NL_OS_WIN64)
1242 platformPatchCategory = "main_exedll_win64";
1243 #elif defined(NL_OS_WINDOWS)
1244 platformPatchCategory = "main_exedll_win32";
1245 #elif defined(NL_OS_MAC)
1246 platformPatchCategory = "main_exedll_osx";
1247 #elif defined(NL_OS_UNIX) && defined(_LP64)
1248 platformPatchCategory = "main_exedll_linux64";
1249 #else
1250 platformPatchCategory = "main_exedll_linux32";
1251 #endif
1253 // check if we are using main_exedll or specific main_exedll_* for platform
1254 bool foundPlatformPatchCategory = false;
1256 for (cat = 0; cat < DescFile.getCategories().categoryCount(); ++cat)
1258 CBNPCategory &category = const_cast<CBNPCategory &>(DescFile.getCategories().getCategory(cat));
1260 if (category.getName() == platformPatchCategory)
1262 foundPlatformPatchCategory = true;
1263 break;
1267 if (foundPlatformPatchCategory)
1269 std::set<std::string> forceRemovePatchCategories;
1271 // only download binaries for current platform
1272 forceRemovePatchCategories.insert("main_exedll");
1273 forceRemovePatchCategories.insert("main_exedll_win32");
1274 forceRemovePatchCategories.insert("main_exedll_win64");
1275 forceRemovePatchCategories.insert("main_exedll_linux32");
1276 forceRemovePatchCategories.insert("main_exedll_linux64");
1277 forceRemovePatchCategories.insert("main_exedll_osx");
1279 // remove current platform category from remove list
1280 forceRemovePatchCategories.erase(platformPatchCategory);
1282 CBNPFileSet &bnpFS = const_cast<CBNPFileSet &>(DescFile.getFiles());
1284 // TODO: .ref files are expected to follow platform category naming (they are in 'main' category)
1285 std::set<std::string>::const_iterator it;
1286 for(it = forceRemovePatchCategories.begin(); it != forceRemovePatchCategories.end(); ++it)
1288 std::string name = *it;
1289 std::string::size_type pos = name.find("_");
1290 if (pos != std::string::npos)
1292 name = name.substr(pos+1) + "_.ref";
1293 bnpFS.removeFile(name);
1297 for (cat = 0; cat < DescFile.getCategories().categoryCount();)
1299 const CBNPCategory &bnpCat = DescFile.getCategories().getCategory(cat);
1301 if (std::find(forceRemovePatchCategories.begin(), forceRemovePatchCategories.end(),
1302 bnpCat.getName()) != forceRemovePatchCategories.end())
1304 for (uint file = 0; file < bnpCat.fileCount(); ++file)
1306 std::string fileName = bnpCat.getFile(file);
1307 bnpFS.removeFile(fileName);
1309 const_cast<CBNPCategorySet &>(DescFile.getCategories()).deleteCategory(cat);
1311 else
1313 ++cat;
1319 // ****************************************************************************
1320 void CPatchManager::getServerFile (const std::string &name, bool bZipped, const std::string& specifyDestName, NLMISC::IProgressCallback *progress)
1322 string srcName = name;
1323 if (bZipped) srcName += ".ngz";
1325 string dstName;
1326 if (specifyDestName.empty())
1328 dstName = ClientPatchPath + NLMISC::CFile::getFilename(name);
1330 else
1332 dstName = specifyDestName;
1334 if (bZipped) dstName += ".ngz";
1336 bool downloadSuccess = false;
1338 while (!downloadSuccess)
1340 std::string serverPath;
1341 std::string serverDisplayPath;
1343 if (UsedServer >= 0 && !PatchServers.empty())
1345 // first use main patch servers
1346 serverPath = PatchServers[UsedServer].ServerPath;
1347 serverDisplayPath = PatchServers[UsedServer].DisplayedServerPath;
1349 else
1351 // else use alternative emergency patch server
1352 serverPath = ServerPath;
1353 serverDisplayPath = DisplayedServerPath;
1354 UsedServer = -1;
1359 string s = CI18N::get("uiLoginGetFile") + " " + NLMISC::CFile::getFilename(srcName);
1360 setState(true, s);
1362 // get the new file
1363 downloadFile (serverPath+srcName, dstName, progress);
1365 downloadSuccess = true;
1367 catch (const EPatchDownloadException& e)
1369 //nlwarning("EXCEPTION CATCH: getServerFile() failed - try to find an alternative: %i: %s",UsedServer,PatchServers[UsedServer].DisplayedServerPath.c_str());
1371 nlwarning("EXCEPTION CATCH: getServerFile() failed - try to find an alternative : %s", (serverPath+srcName).c_str());
1372 nlwarning("%i", UsedServer);
1373 if (UsedServer >= 0 && UsedServer < (int) PatchServers.size())
1375 nlwarning("%s", PatchServers[UsedServer].DisplayedServerPath.c_str());
1378 // if emergency patch server, this is a real issue, rethrow exception
1379 if (UsedServer < 0)
1381 string s = CI18N::get("uiDLFailed");
1382 setState(true, s);
1384 throw Exception(e.what());
1387 string s = CI18N::get("uiDLURIFailed") + " " + serverDisplayPath;
1388 setState(true, s);
1390 // this server is unavailable
1391 PatchServers[UsedServer].Available = false;
1393 sint nextServer = (UsedServer+1) % PatchServers.size();
1395 while (nextServer != UsedServer && !PatchServers[nextServer].Available)
1396 nextServer = (nextServer+1) % PatchServers.size();
1398 // scanned all servers? use alternative
1399 if (nextServer == UsedServer)
1401 string s = CI18N::get("uiNoMoreURI");
1402 setState(true, s);
1403 UsedServer = -1;
1404 nlwarning("EXCEPTION CATCH: getServerFile() failed - no alternative found");
1406 else
1408 UsedServer = nextServer;
1409 nlwarning("EXCEPTION CATCH: getServerFile() failed - trying server: %i: %s",UsedServer,PatchServers[UsedServer].DisplayedServerPath.c_str());
1414 // decompress it
1415 if (bZipped)
1416 decompressFile (dstName);
1419 // ****************************************************************************
1420 void CPatchManager::downloadFileWithCurl (const string &source, const string &dest, NLMISC::IProgressCallback *progress)
1422 DownloadInProgress = true;
1425 #ifdef USE_CURL
1426 string s = CI18N::get("uiDLWithCurl") + " " + CFile::getFilename(dest);
1427 setState(true, s);
1429 // user agent = nel_launcher
1431 CURL *curl;
1432 CURLcode res;
1434 string sTranslate = CI18N::get("uiLoginGetFile") + " " + NLMISC::CFile::getFilename (source);
1435 setState(true, sTranslate);
1436 CurrentFile = NLMISC::CFile::getFilename (source);
1438 curl_global_init(CURL_GLOBAL_ALL);
1439 curl = curl_easy_init();
1440 if(curl == NULL)
1442 // file not found, delete local file
1443 throw Exception ("curl init failed");
1446 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
1447 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, downloadProgressFunc);
1448 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, (void *) progress);
1449 curl_easy_setopt(curl, CURLOPT_URL, source.c_str());
1450 if (source.length() > 8 && (source[4] == 's' || source[4] == 'S')) // 01234 https
1452 NLWEB::CCurlCertificates::addCertificateFile("cacert.pem");
1453 NLWEB::CCurlCertificates::useCertificates(curl);
1454 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1455 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
1458 // create the local file
1459 if (NLMISC::CFile::fileExists(dest))
1461 setRWAccess(dest, false);
1462 NLMISC::CFile::deleteFile(dest.c_str());
1465 FILE *fp = nlfopen (dest, "wb");
1467 if (fp == NULL)
1469 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, NULL);
1470 throw Exception ("Can't open file '%s' for writing: code=%d %s (error code 37)", dest.c_str (), errno, strerror(errno));
1473 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
1474 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
1476 //CurrentFilesToGet++;
1478 res = curl_easy_perform(curl);
1480 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, NULL);
1482 long r;
1483 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r);
1485 curl_easy_cleanup(curl);
1487 bool diskFull = ferror(fp) && errno == 28 /*ENOSPC*/;
1489 fclose(fp);
1490 curl_global_cleanup();
1492 CurrentFile.clear();
1494 if (diskFull)
1496 NLMISC::CFile::deleteFile(dest.c_str());
1497 throw NLMISC::EDiskFullError(dest);
1500 if(CURLE_WRITE_ERROR == res)
1502 // file not found, delete local file
1503 NLMISC::CFile::deleteFile(dest.c_str());
1504 throw NLMISC::EWriteError(dest);
1507 if(CURLE_FTP_COULDNT_RETR_FILE == res)
1509 // file not found, delete local file
1510 NLMISC::CFile::deleteFile(dest.c_str());
1511 throw EPatchDownloadException (NLMISC::toString("curl download failed: (ec %d %d)", res, r));
1514 if(CURLE_OK != res)
1516 NLMISC::CFile::deleteFile(dest.c_str());
1517 throw EPatchDownloadException (NLMISC::toString("curl download failed: (ec %d %d)", res, r));
1520 if(r == 404)
1522 // file not found, delete it
1523 NLMISC::CFile::deleteFile(dest.c_str());
1524 throw EPatchDownloadException (NLMISC::toString("curl download failed: (ec %d %d)", res, r));
1527 if(r < 200 || r >= 300)
1529 // file not found, delete it
1530 NLMISC::CFile::deleteFile(dest.c_str());
1531 throw EPatchDownloadException (NLMISC::toString("curl download failed: (ec %d %d)", res, r));
1534 #else
1535 throw Exception("USE_CURL is not defined, no curl method");
1536 #endif
1538 catch(...)
1540 DownloadInProgress = false;
1541 throw;
1543 if (progress)
1545 // set progress to 1
1546 validateProgress((void *) progress, 1, 1, 0, 0);
1548 DownloadInProgress = false;
1551 // ****************************************************************************
1552 void CPatchManager::downloadFile (const string &source, const string &dest, NLMISC::IProgressCallback *progress)
1554 // For the moment use only curl
1555 const string sourceLower = toLowerAscii(source.substr(0, 6));
1557 if (startsWith(sourceLower, "http:")
1558 || startsWith(sourceLower, "https:")
1559 || startsWith(sourceLower, "ftp:")
1560 || startsWith(sourceLower, "file:"))
1562 nldebug("Download patch file %s", source.c_str());
1563 downloadFileWithCurl(source, dest, progress);
1565 else
1567 if (!NLMISC::CFile::copyFile(dest, source, false, progress))
1569 if (errno == 28)
1571 throw NLMISC::EDiskFullError(dest);
1573 throw Exception ("cannot copy file %s to %s", source.c_str(), dest.c_str());
1578 // ****************************************************************************
1579 // TODO : Review this uncompress routine to uncompress in a temp file before overwriting destination file
1581 void CPatchManager::decompressFile (const string &filename)
1583 string sTranslate = CI18N::get("uiDecompressing") + " " + NLMISC::CFile::getFilename(filename);
1584 setState(true, sTranslate);
1586 //if(isVerboseLog()) nlinfo("Calling gzopen('%s','rb')", filename.c_str());
1587 gzFile gz = gzopen (filename.c_str (), "rb");
1588 if (gz == NULL)
1590 string err = toString("Can't open compressed file '%s' : ", filename.c_str());
1591 if(errno == 0)
1593 // gzerror
1594 int gzerrno;
1595 const char *gzerr = gzerror (gz, &gzerrno);
1596 err += toString("code=%d %s", gzerrno, gzerr);
1598 else
1600 err += toString("code=%d %s", errno, strerror (errno));
1602 err += " (error code 31)";
1603 deleteFile (filename);
1604 throw Exception (err);
1607 string dest = filename.substr(0, filename.size ()-4);
1608 setRWAccess(dest, false);
1609 //if(isVerboseLog()) nlinfo("Calling nlfopen('%s','wb')", dest.c_str());
1610 FILE *fp = nlfopen (dest, "wb");
1611 if (fp == NULL)
1613 string err = toString("Can't open file '%s' : code=%d %s, (error code 32)", dest.c_str(), errno, strerror(errno));
1615 gzclose(gz);
1616 deleteFile (filename);
1617 throw Exception (err);
1620 //if(isVerboseLog()) nlinfo("Entering the while loop decompression");
1622 uint32 currentSize = 0;
1623 uint8 buffer[10000];
1624 while (!gzeof(gz))
1626 //if(isVerboseLog()) nlinfo("Calling gzread");
1627 int res = gzread (gz, buffer, 10000);
1628 //if(isVerboseLog()) nlinfo("gzread returns %d", res);
1629 if (res == -1)
1631 int gzerrno;
1632 const char *gzerr = gzerror (gz, &gzerrno);
1633 gzclose(gz);
1634 fclose(fp);
1635 //deleteFile (filename);
1636 throw Exception ("Can't read compressed file '%s' (after %d bytes) : code=%d %s, (error code 33)", filename.c_str(), currentSize, gzerrno, gzerr);
1639 currentSize += res;
1641 //if(isVerboseLog()) nlinfo("Calling fwrite for %d bytes", res);
1642 int res2 = (int)fwrite (buffer, 1, res, fp);
1643 //if(isVerboseLog()) nlinfo("fwrite returns %d", res2);
1644 if (res2 != res)
1646 bool diskFull = (errno == 28 /* ENOSPC */);
1647 string err = toString("Can't write file '%s' : code=%d %s (error code 34)", dest.c_str(), errno, strerror(errno));
1649 gzclose(gz);
1650 fclose(fp);
1651 deleteFile (filename);
1652 if (diskFull)
1654 throw NLMISC::EDiskFullError(err);
1656 else
1658 throw Exception (err);
1663 //if(isVerboseLog()) nlinfo("Exiting the while loop decompression");
1665 //if(isVerboseLog()) nlinfo("Calling gzclose");
1666 gzclose(gz);
1667 //if(isVerboseLog()) nlinfo("Calling fclose");
1668 fclose(fp);
1669 deleteFile(filename);
1671 //if(isVerboseLog()) nlinfo("Exiting the decompressing file");
1674 // ****************************************************************************
1675 void CPatchManager::applyDate (const string &sFilename, uint32 nDate)
1677 // change the file time
1678 if(nDate != 0)
1680 setRWAccess(sFilename, false);
1681 string s = CI18N::get("uiChangeDate") + " " + NLMISC::CFile::getFilename(sFilename) + " " + timestampToHumanReadable(NLMISC::CFile::getFileModificationDate (sFilename)) +
1682 " -> " + timestampToHumanReadable(nDate);
1683 setState(true,s);
1685 if (!NLMISC::CFile::setFileModificationDate(sFilename, nDate))
1687 int err = NLMISC::getLastError();
1688 s = CI18N::get("uiChgDateErr") + " " + CFile::getFilename(sFilename) + " (" + toString(err) + ", " + formatErrorMessage(err) + ")";
1689 setState(true,s);
1691 s = CI18N::get("uiNowDate") + " " + CFile::getFilename(sFilename) + " " + timestampToHumanReadable(NLMISC::CFile::getFileModificationDate (sFilename));
1692 setState(true,s);
1696 // ****************************************************************************
1697 // Get all the patches that need to be applied to a file from the description of this file given by the server
1698 void CPatchManager::getPatchFromDesc(SFileToPatch &ftpOut, const CBNPFile &fIn, bool forceCheckSumTest)
1700 uint32 j;
1701 const CBNPFile rFile = fIn;
1702 const string &rFilename = rFile.getFileName();
1703 string sFilePath;
1704 bool inPatchDir = false;
1705 // first see if there's a version that was downloaded from the background downloader
1706 if (NLMISC::CFile::fileExists(ClientPatchPath + rFilename + ".tmp"))
1708 sFilePath = ClientPatchPath + rFilename + ".tmp";
1709 ftpOut.SrcFileName = sFilePath;
1710 inPatchDir = true;
1713 bool needUnpack = false;
1714 const CBNPCategory *cat = DescFile.getCategories().getCategoryFromFile(rFilename);
1715 if (cat)
1717 needUnpack = !cat->getUnpackTo().empty();
1719 else
1721 nlwarning("Can't find file category for %s:", rFilename.c_str());
1724 // Does the BNP exists ?
1725 // following lines added by Sadge to ensure that the correct file is patched
1726 // Only look in data path if the file should not be unpack (otherwise it should only remains in the "unpack" directory)
1727 if (!needUnpack)
1729 if (sFilePath.empty())
1731 if (NLMISC::CFile::fileExists(WritableClientDataPath + rFilename))
1733 // if file exists in writable directory, use it
1734 sFilePath = WritableClientDataPath + rFilename;
1736 else if (NLMISC::CFile::fileExists(ReadableClientDataPath + rFilename))
1738 // if file exists in readable directory, use it
1739 sFilePath = ReadableClientDataPath + rFilename;
1744 if (sFilePath.empty() && NLMISC::CFile::fileExists(ClientPatchPath + rFilename))
1746 sFilePath = ClientPatchPath + rFilename;
1749 // following lines removed by Sadge to ensure that the correct file is patched
1750 // string sFilePath = CPath::lookup(rFilename, false, false);
1751 // if (sFilePath.empty())
1752 // {
1753 // if (NLMISC::CFile::fileExists(ClientPatchPath + rFilename))
1754 // sFilePath = ClientPatchPath + rFilename;
1755 // }
1757 // if file not found anywhere
1758 if (sFilePath.empty())
1760 ftpOut.FileName = rFilename;
1761 ftpOut.LocalFileToDelete = false;
1762 ftpOut.LocalFileExists = false;
1763 // It happens some time (maybe a bug) that the versionCount is 0... =>
1764 // it happens if the BNP file is empty (8 bytes)
1765 ftpOut.FinalFileSize = EmptyBnpFileSize;
1766 // BNP does not exists : get all the patches version
1767 for (j = 0; j < rFile.versionCount(); ++j)
1769 ftpOut.Patches.push_back(rFile.getVersion(j).getVersionNumber());
1770 ftpOut.PatcheSizes.push_back(rFile.getVersion(j).getPatchSize());
1771 ftpOut.LastFileDate = rFile.getVersion(j).getTimeStamp();
1772 ftpOut.FinalFileSize = rFile.getVersion(j).getFileSize();
1773 ftpOut.SZFileSize = rFile.getVersion(j).get7ZFileSize();
1776 else
1778 // The local BNP file exists : find its version
1779 uint32 nLocalSize = NLMISC::CFile::getFileSize(sFilePath);
1780 uint32 nLocalTime = NLMISC::CFile::getFileModificationDate(sFilePath);
1781 // From the couple time, size look the version of the file
1782 uint32 nVersionFound = 0xFFFFFFFF;
1783 // If forceChecksum is wanted (slow), then don't do the test with filesize/date
1784 if(!forceCheckSumTest)
1786 for (j = 0; j < rFile.versionCount(); ++j)
1788 const CBNPFileVersion &rVersion = rFile.getVersion(j);
1789 uint32 nServerSize = rVersion.getFileSize();
1790 uint32 nServerTime = rVersion.getTimeStamp();
1791 // Does the time and size match a version ?
1792 if ((nServerSize == nLocalSize) && (abs((sint32)(nServerTime - nLocalTime)) <= 2) )
1794 nVersionFound = rVersion.getVersionNumber();
1795 // break; // ace -> get the last good version (if more than one version of the same file exists)
1800 // If the version cannot be found with size and time try with sha1
1801 if (nVersionFound == 0xFFFFFFFF)
1803 string sTranslate = CI18N::get("uiCheckInt") + " " + rFilename;
1804 setState(true, sTranslate);
1805 CHashKey hkLocalSHA1 = getSHA1(sFilePath);
1806 for (j = 0; j < rFile.versionCount(); ++j)
1808 const CBNPFileVersion &rVersion = rFile.getVersion(j);
1809 CHashKey hkServerSHA1 = rVersion.getHashKey();
1810 // Does the sha1 match a version ?
1811 if (hkServerSHA1 == hkLocalSHA1)
1813 nVersionFound = rVersion.getVersionNumber();
1814 applyDate(sFilePath, rVersion.getTimeStamp());
1815 // break; // ace -> same as above
1820 // No version available found
1821 if (nVersionFound == 0xFFFFFFFF)
1823 string sTranslate = CI18N::get("uiNoVersionFound");
1824 setState(true, sTranslate);
1825 // Get all patches from beginning (first patch is reference file)
1826 ftpOut.FileName = rFilename;
1827 ftpOut.LocalFileToDelete = true;
1828 ftpOut.LocalFileExists = !inPatchDir;
1829 // It happens some time (maybe a bug) that the versionCount is 0... =>
1830 // it happens if the BNP file is empty (8 bytes)
1831 ftpOut.FinalFileSize = EmptyBnpFileSize;
1832 // Get all the patches version
1833 for (j = 0; j < rFile.versionCount(); ++j)
1835 ftpOut.Patches.push_back(rFile.getVersion(j).getVersionNumber());
1836 ftpOut.PatcheSizes.push_back(rFile.getVersion(j).getPatchSize());
1837 ftpOut.LastFileDate = rFile.getVersion(j).getTimeStamp();
1838 ftpOut.FinalFileSize = rFile.getVersion(j).getFileSize();
1839 ftpOut.SZFileSize = rFile.getVersion(j).get7ZFileSize();
1842 else // A version of the file has been found
1844 string sTranslate = CI18N::get("uiVersionFound") + " " + toString(nVersionFound);
1845 setState(true, sTranslate);
1846 // Get All patches from this version !
1847 ftpOut.FileName = rFilename;
1848 ftpOut.LocalFileToDelete = false;
1849 ftpOut.LocalFileExists = !inPatchDir;
1850 // Go to the version
1851 for (j = 0; j < rFile.versionCount(); ++j)
1852 if (rFile.getVersion(j).getVersionNumber() == nVersionFound)
1853 break;
1855 nlassert(j != rFile.versionCount()); // Not normal if we cant find the version we found previously
1857 // Point on the next version
1858 j++;
1859 // If there are newer versions
1860 if (j != rFile.versionCount())
1862 // Add all version until the last one
1863 for (; j < rFile.versionCount(); ++j)
1865 ftpOut.Patches.push_back(rFile.getVersion(j).getVersionNumber());
1866 ftpOut.PatcheSizes.push_back(rFile.getVersion(j).getPatchSize());
1867 ftpOut.LastFileDate = rFile.getVersion(j).getTimeStamp();
1868 ftpOut.SZFileSize = rFile.getVersion(j).get7ZFileSize();
1871 // Else this file is up to date !
1873 // For info, get its final file size
1874 ftpOut.FinalFileSize= rFile.getVersion(rFile.versionCount()-1).getFileSize();
1876 } // end of else local BNP file exists
1879 // ****************************************************************************
1880 bool CPatchManager::bnpUnpack(const string &srcBigfile, const string &dstPath, vector<string> &vFilenames)
1882 nldebug("bnpUnpack: srcBigfile=%s", srcBigfile.c_str());
1883 string SourceName, DestPath;
1885 // following lines added by Sadge to ensure that the correct file gets patched
1886 // if (NLMISC::CFile::fileExists(ClientDataPath + srcBigfile)) SourceName = ClientDataPath + srcBigfile;
1887 // else if (NLMISC::CFile::fileExists(srcBigfile)) SourceName = ClientDataPath + srcBigfile;
1888 SourceName= srcBigfile;
1890 // following lines removed by Sadge to ensure that the correct file gets patched
1891 // SourceName = CPath::lookup(srcBigfile, false, false);
1892 // if (SourceName.empty())
1893 // SourceName = ClientPatchPath + srcBigfile;
1895 if (dstPath.empty())
1896 DestPath = ClientRootPath;
1897 else
1898 DestPath = CPath::standardizePath (dstPath);
1900 string s = CI18N::get("uiUnpack") + " " + NLMISC::CFile::getFilename(SourceName);
1901 setState(true,s);
1903 // Read Header of the BNP File
1904 CBigFile::BNP bnpFile;
1905 bnpFile.BigFileName = SourceName;
1907 if (!bnpFile.readHeader())
1909 string s = CI18N::get("uiUnpackErrHead") + " " + CFile::getFilename(SourceName);
1910 setState(true,s);
1911 return false;
1914 // Unpack
1915 if (!bnpFile.unpack(DestPath))
1916 return false;
1918 // Return names
1920 vFilenames.clear();
1921 for (uint32 i = 0; i < bnpFile.SFiles.size(); ++i)
1922 vFilenames.push_back(bnpFile.SFiles[i].Name);
1925 return true;
1929 // ****************************************************************************
1930 int CPatchManager::downloadProgressFunc(void *foo, double t, double d, double ultotal, double ulnow)
1932 if (d != t)
1934 // In the case of progress = 1, don't update because, this will be called in case of error to signal the end of the download, though
1935 // no download actually occurred. Instead, we set progress to 1.f at the end of downloadWithCurl if everything went fine
1936 return validateProgress(foo, t, d, ultotal, ulnow);
1938 return 0;
1941 // ****************************************************************************
1942 int CPatchManager::validateProgress(void *foo, double t, double d, double /* ultotal */, double /* ulnow */)
1944 static std::vector<std::string> units;
1946 if (units.empty())
1948 units.push_back(CI18N::get("uiByte"));
1949 units.push_back(CI18N::get("uiKb"));
1950 units.push_back(CI18N::get("uiMb"));
1953 CPatchManager *pPM = CPatchManager::getInstance();
1954 double pour1 = t!=0.0?d*100.0/t:0.0;
1955 string sTranslate = CI18N::get("uiLoginGetFile") + toString(" %s : %s / %s (%.02f %%)", NLMISC::CFile::getFilename(pPM->CurrentFile).c_str(),
1956 NLMISC::bytesToHumanReadableUnits((uint64)d, units).c_str(), NLMISC::bytesToHumanReadableUnits((uint64)t, units).c_str(), pour1);
1957 pPM->setState(false, sTranslate);
1958 if (foo)
1960 ((NLMISC::IProgressCallback *) foo)->progress((float) (t != 0 ? d / t : 0));
1962 return 0;
1965 // ****************************************************************************
1966 void CPatchManager::MyPatchingCB::progress(float f)
1968 CPatchManager *pPM = CPatchManager::getInstance();
1969 double p = 100.0*f;
1970 string sTranslate = CI18N::get("uiApplyingDelta") + toString(" %s (%.02f %%)", CFile::getFilename(patchFilename).c_str(), p);
1971 pPM->setState(false, sTranslate);
1974 // ***************************************************************************
1975 void CPatchManager::startScanDataThread()
1977 if (ScanDataThread != NULL)
1979 nlwarning ("scan data thread is already running");
1980 return;
1982 if (Thread != NULL)
1984 nlwarning ("a thread is already running");
1985 return;
1988 _ErrorMessage.clear();
1990 // Reset result
1991 clearDataScanLog();
1993 // Read now the client version and Desc File.
1994 readClientVersionAndDescFile();
1996 // start thread
1997 ScanDataThread = new CScanDataThread();
1998 nlassert (ScanDataThread != NULL);
2000 Thread = IThread::create (ScanDataThread);
2001 nlassert (Thread != NULL);
2002 Thread->start ();
2005 // ****************************************************************************
2006 bool CPatchManager::isScanDataThreadEnded(bool &ok)
2008 if (ScanDataThread == NULL)
2010 ok = false;
2011 return true;
2014 bool end = ScanDataThread->Ended;
2015 if (end)
2017 ok = ScanDataThread->CheckOk;
2018 stopScanDataThread();
2021 return end;
2024 // ****************************************************************************
2025 void CPatchManager::stopScanDataThread()
2027 if(ScanDataThread && Thread)
2029 Thread->wait();
2030 delete Thread;
2031 Thread = NULL;
2032 delete ScanDataThread;
2033 ScanDataThread = NULL;
2037 // ***************************************************************************
2038 void CPatchManager::askForStopScanDataThread()
2040 if(!ScanDataThread)
2041 return;
2043 ScanDataThread->AskForCancel= true;
2046 // ***************************************************************************
2047 uint CPatchManager::applyScanDataResult()
2049 // if still running, abort
2050 if(ScanDataThread)
2051 return 0;
2053 uint numError= 0;
2056 TSyncDataScanState::CAccessor ac(&DataScanState);
2057 CDataScanState &val= ac.value();
2058 numError= (uint)val.FilesWithScanDataError.size();
2060 // Touch the files with error (will be reloaded at next patch)
2061 for(uint i=0;i<numError;i++)
2063 SFileToPatch &ftp= val.FilesWithScanDataError[i];
2065 // if the file was not found (just loggued for information)
2066 if(!ftp.LocalFileExists)
2067 continue;
2069 // get file path
2070 // following lines added by Sadge to ensure that the correct file gets patched
2071 string sFilePath;
2072 if (NLMISC::CFile::fileExists(WritableClientDataPath + ftp.FileName)) sFilePath = WritableClientDataPath + ftp.FileName;
2073 if (sFilePath.empty() && NLMISC::CFile::fileExists(ReadableClientDataPath + ftp.FileName)) sFilePath = ReadableClientDataPath + ftp.FileName;
2074 if (sFilePath.empty() && NLMISC::CFile::fileExists(ClientPatchPath + ftp.FileName)) sFilePath = ClientPatchPath + ftp.FileName;
2076 // following lines removed by Sadge to ensure that the correct file gets patched
2077 // string sFilePath = CPath::lookup(ftp.FileName, false, false);
2078 // if (sFilePath.empty())
2079 // {
2080 // if (NLMISC::CFile::fileExists(ClientPatchPath + ftp.FileName))
2081 // sFilePath = ClientPatchPath + ftp.FileName;
2082 // }
2084 // Reset to a dummy date, so the patch will be fully/checked/patched next time
2085 if(!sFilePath.empty())
2086 applyDate(sFilePath, DefaultResetDate);
2087 else
2088 nlwarning("File '%s' Not Found. should exist...", ftp.FileName.c_str());
2092 return numError;
2095 // ***************************************************************************
2096 bool CPatchManager::getDataScanLog(string &text)
2098 text.clear();
2099 bool changed= false;
2101 TSyncDataScanState::CAccessor ac(&DataScanState);
2102 CDataScanState &val= ac.value();
2103 changed= val.Changed;
2104 // if changed, build the log
2105 if(changed)
2107 for(uint i=0;i<val.FilesWithScanDataError.size();i++)
2109 string str;
2110 getCorruptedFileInfo(val.FilesWithScanDataError[i], str);
2111 text+= str + "\n";
2114 // then reset
2115 val.Changed= false;
2118 return changed;
2121 // ***************************************************************************
2122 void CPatchManager::addDataScanLogCorruptedFile(const SFileToPatch &ftp)
2125 TSyncDataScanState::CAccessor ac(&DataScanState);
2126 CDataScanState &val= ac.value();
2127 val.FilesWithScanDataError.push_back(ftp);
2128 val.Changed= true;
2132 // ***************************************************************************
2133 void CPatchManager::clearDataScanLog()
2136 TSyncDataScanState::CAccessor ac(&DataScanState);
2137 CDataScanState &val= ac.value();
2138 val.FilesWithScanDataError.clear();
2139 val.Changed= true;
2143 // ***************************************************************************
2144 void CPatchManager::getCorruptedFileInfo(const SFileToPatch &ftp, string &sTranslate)
2146 sTranslate = CI18N::get("uiCorruptedFile") + " " + ftp.FileName + " (" +
2147 toString("%.1f ", (float)ftp.FinalFileSize/1000000.f) + CI18N::get("uiMb") + ")";
2151 // ****************************************************************************
2152 // ****************************************************************************
2153 // ****************************************************************************
2154 // CCheckThread
2155 // ****************************************************************************
2156 // ****************************************************************************
2157 // ****************************************************************************
2159 // ****************************************************************************
2160 CCheckThread::CCheckThread(bool includeBackgroundPatch)
2162 Ended = false;
2163 CheckOk = false;
2164 TotalFileToCheck = 0;
2165 CurrentFileChecked = 0;
2166 IncludeBackgroundPatch = includeBackgroundPatch;
2167 StopAsked = false;
2170 // ****************************************************************************
2171 void CCheckThread::run ()
2173 nlwarning("CCheckThread::start");
2174 StopAsked = false;
2175 CPatchManager *pPM = CPatchManager::getInstance();
2176 pPM->MustLaunchBatFile = false;
2179 nlwarning("CCheckThread : get version");
2180 uint32 i, j, k;
2181 // Check if the client version is the same as the server version
2182 string sClientVersion = pPM->getClientVersion();
2183 string sServerVersion = pPM->getServerVersion();
2184 string sTranslate = CI18N::get("uiClientVersion") + " (" + sClientVersion + ") ";
2185 sTranslate += CI18N::get("uiServerVersion") + " (" + sServerVersion + ")";
2186 pPM->setState(true, sTranslate);
2188 if (sServerVersion.empty())
2190 // No need to patch
2191 CheckOk = true;
2192 Ended = true;
2193 return;
2196 sint32 nServerVersion, nClientVersion;
2197 fromString(sServerVersion, nServerVersion);
2198 fromString(sClientVersion, nClientVersion);
2200 if (nClientVersion != nServerVersion)
2202 // first, try in the version subdirectory
2205 pPM->DescFilename = toString("%05d/ryzom_%05d.idx", nServerVersion, nServerVersion);
2206 // The client version is different from the server version : download new description file
2207 pPM->getServerFile(pPM->DescFilename, false); // For the moment description file is not zipped
2209 catch (...)
2211 // fallback to patch root directory
2212 pPM->DescFilename = toString("ryzom_%05d.idx", nServerVersion);
2213 // The client version is different from the server version : download new description file
2214 pPM->getServerFile(pPM->DescFilename, false); // For the moment description file is not zipped
2218 nlwarning("CCheckThread : read description files");
2220 // Read the description file
2221 pPM->readDescFile(nServerVersion);
2223 nlwarning("CCheckThread : check files");
2225 // For all bnp in the description file get all patches to apply
2226 // depending on the version of the client bnp files
2227 const CBNPFileSet &rDescFiles = pPM->DescFile.getFiles();
2228 pPM->FilesToPatch.clear();
2229 TotalFileToCheck = rDescFiles.fileCount();
2230 for (i = 0; i < rDescFiles.fileCount(); ++i)
2232 CPatchManager::SFileToPatch ftp;
2233 sTranslate = CI18N::get("uiCheckingFile") + " " + rDescFiles.getFile(i).getFileName();
2234 pPM->setState(true, sTranslate);
2235 // get list of patch to apply to this file. don't to a full checksum test if possible
2236 nlwarning(rDescFiles.getFile(i).getFileName().c_str());
2237 pPM->getPatchFromDesc(ftp, rDescFiles.getFile(i), false);
2238 // add the file if there are some patches to apply, or if an already patched version was found in the unpack directory
2239 if (!ftp.Patches.empty() || (IncludeBackgroundPatch && !ftp.SrcFileName.empty()))
2241 pPM->FilesToPatch.push_back(ftp);
2242 sTranslate = CI18N::get("uiNeededPatches") + " " + toString (ftp.Patches.size());
2243 pPM->setState(true, sTranslate);
2245 CurrentFileChecked = i;
2248 // Here we got all the files to patch in FilesToPatch and all the versions that must be obtained
2249 // Now we have to get the optional categories
2250 const CBNPCategorySet &rDescCats = pPM->DescFile.getCategories();
2251 pPM->OptionalCat.clear();
2252 for (i = 0; i < rDescCats.categoryCount(); ++i)
2254 // For all optional categories check if there is a 'file to patch' in it
2255 const CBNPCategory &rCat = rDescCats.getCategory(i);
2256 if (rCat.isOptional())
2257 for (j = 0; j < rCat.fileCount(); ++j)
2259 const string &rFilename = rCat.getFile(j);
2260 bool bAdded = false;
2261 for (k = 0; k < pPM->FilesToPatch.size(); ++k)
2263 if (stricmp(pPM->FilesToPatch[k].FileName.c_str(), rFilename.c_str()) == 0)
2265 pPM->OptionalCat.push_back(rCat.getName());
2266 bAdded = true;
2267 break;
2270 if (bAdded)
2271 break;
2275 // For all categories that required an optional category if the cat required is present the category that
2276 // reference it must be present
2277 for (i = 0; i < rDescCats.categoryCount(); ++i)
2279 // For all optional categories check if there is a 'file to patch' in it
2280 const CBNPCategory &rCat = rDescCats.getCategory(i);
2281 if (rCat.isOptional() && !rCat.getCatRequired().empty())
2283 // Does the rCat is already present ?
2284 bool bFound = false;
2285 for (j = 0; j < pPM->OptionalCat.size(); ++j)
2287 if (rCat.getName() == pPM->OptionalCat[j])
2289 bFound = true;
2290 break;
2294 if (!bFound)
2296 // rCat is not present but perhaps its required cat is present
2297 const string &sCatReq = rCat.getCatRequired();
2298 for (j = 0; j < pPM->OptionalCat.size(); ++j)
2300 if (sCatReq == pPM->OptionalCat[j])
2302 // Required Cat present -> Add the category rCat
2303 pPM->OptionalCat.push_back(rCat.getName());
2304 break;
2312 // Delete categories optional cat that are hidden
2313 for (i = 0; i < rDescCats.categoryCount(); ++i)
2315 // For all optional categories check if there is a 'file to patch' in it
2316 const CBNPCategory &rCat = rDescCats.getCategory(i);
2317 if (rCat.isOptional() && rCat.isHidden())
2319 // Does the rCat is present ?
2320 for (j = 0; j < pPM->OptionalCat.size(); ++j)
2322 if (rCat.getName() == pPM->OptionalCat[j])
2324 // remove it
2325 /*#ifdef RY_BG_DOWNLOADER
2326 // fixme : strange stlport link error for the background downloader when using erase ...
2327 std::copy(pPM->OptionalCat.begin() + j + 1, pPM->OptionalCat.end(), pPM->OptionalCat.begin()+j);
2328 pPM->OptionalCat.resize(pPM->OptionalCat.size() - 1);
2329 #else*/
2330 pPM->OptionalCat.erase(pPM->OptionalCat.begin()+j);
2331 //#endif
2332 break;
2338 // Get all extract to category and check files inside the bnp with real files
2339 for (i = 0; i < rDescCats.categoryCount(); ++i)
2341 // For all optional categories check if there is a 'file to patch' in it
2342 const CBNPCategory &rCat = rDescCats.getCategory(i);
2343 if (!rCat.getUnpackTo().empty())
2345 for (j = 0; j < rCat.fileCount(); ++j)
2347 string sBNPFilename = pPM->ClientPatchPath + rCat.getFile(j);
2349 sTranslate = CI18N::get("uiCheckInBNP") + " " + rCat.getFile(j);
2350 pPM->setState(true, sTranslate);
2351 CBigFile::BNP bnpFile;
2352 bnpFile.BigFileName = sBNPFilename;
2354 if (bnpFile.readHeader())
2356 // read the file inside the bnp and calculate the sha1
2357 FILE *bnp = nlfopen (sBNPFilename, "rb");
2358 if (bnp != NULL)
2360 for (uint32 k = 0; k < bnpFile.SFiles.size(); ++k)
2362 const CBigFile::SBNPFile &rBNPFile = bnpFile.SFiles[k];
2363 // Is the real file exists ?
2364 string sRealFilename = rCat.getUnpackTo() + rBNPFile.Name;
2365 if (NLMISC::CFile::fileExists(sRealFilename))
2367 // Yes compare the sha1 with the sha1 of the BNP File
2368 CHashKey sha1BNPFile;
2369 nlfseek64 (bnp, rBNPFile.Pos, SEEK_SET);
2370 uint8 *pPtr = new uint8[rBNPFile.Size];
2371 if (fread (pPtr, rBNPFile.Size, 1, bnp) != 1)
2373 delete [] pPtr;
2374 break;
2377 sha1BNPFile = getSHA1(pPtr, rBNPFile.Size);
2378 delete [] pPtr;
2379 CHashKey sha1RealFile = getSHA1(sRealFilename, true);
2380 if ( ! (sha1RealFile == sha1BNPFile))
2382 sTranslate = CI18N::get("uiSHA1Diff") + " " + rBNPFile.Name;
2383 pPM->setState(true, sTranslate);
2384 pPM->MustLaunchBatFile = true;
2387 else
2389 // File dest do not exist
2390 sTranslate = CI18N::get("uiSHA1Diff") + " " + rBNPFile.Name;
2391 pPM->setState(true, sTranslate);
2392 pPM->MustLaunchBatFile = true;
2396 fclose (bnp);
2398 if (StopAsked)
2400 StopAsked = false;
2401 return;
2408 sTranslate = CI18N::get("uiCheckEndNoErr");
2409 pPM->setState(true, sTranslate);
2410 CheckOk = true;
2411 Ended = true;
2413 catch (const NLMISC::EDiskFullError &)
2415 // more explicit message for this common error case
2416 nlwarning("EXCEPTION CATCH: disk full");
2417 pPM->setState(true, CI18N::get("uiCheckEndWithErr"));
2418 pPM->setErrorMessage(CI18N::get("uiPatchDiskFull"));
2419 CheckOk = false;
2420 Ended = true;
2422 catch (const Exception &e)
2424 nlwarning("EXCEPTION CATCH: CCheckThread::run() failed");
2425 string sTranslate = CI18N::get("uiCheckEndWithErr") + " " + e.what();
2426 pPM->setState(true, CI18N::get("uiCheckEndWithErr"));
2427 pPM->setErrorMessage(sTranslate);
2428 CheckOk = false;
2429 Ended = true;
2433 // ****************************************************************************
2434 // ****************************************************************************
2435 // ****************************************************************************
2436 // CPatchThread
2437 // ****************************************************************************
2438 // ****************************************************************************
2439 // ****************************************************************************
2441 // ****************************************************************************
2442 CPatchThread::CPatchThread(bool commitPatch)
2444 Ended = false;
2445 PatchOk = false;
2446 CurrentFilePatched = 0;
2447 PatchSizeProgress = 0;
2448 _CommitPatch = commitPatch;
2449 StopAsked = false;
2452 // ****************************************************************************
2453 void CPatchThread::clear()
2455 AllFilesToPatch.clear();
2458 // ****************************************************************************
2459 // trap : optimize this if needed
2460 void CPatchThread::add(const CPatchManager::SFileToPatch &ftp)
2462 for (uint32 i = 0; i < AllFilesToPatch.size(); ++i)
2464 if (AllFilesToPatch[i].FileName == ftp.FileName)
2465 return;
2467 AllFilesToPatch.push_back(ftp);
2471 // ****************************************************************************
2472 void CPatchThread::run()
2474 StopAsked = false;
2475 CPatchManager *pPM = CPatchManager::getInstance();
2476 bool bErr = false;
2477 uint32 i;
2479 string sServerVersion = pPM->getServerVersion();
2480 PatchSizeProgress = 0;
2482 if (sServerVersion.empty())
2484 // No need to patch
2485 PatchOk = true;
2486 Ended = true;
2487 return;
2490 // Patch all the files
2491 // If at least one file has been patched relaunch the client
2493 CurrentFilePatched = 0.f;
2495 string sTranslate;
2498 // First do all ref files
2499 // ----------------------
2501 for (i = 0; i < AllFilesToPatch.size(); ++i)
2503 CPatchManager::SFileToPatch &rFTP = AllFilesToPatch[i];
2504 string ext = NLMISC::CFile::getExtension(rFTP.FileName);
2505 if (ext == "ref")
2507 float oldCurrentFilePatched = CurrentFilePatched;
2508 processFile (rFTP);
2509 pPM->MustLaunchBatFile = true;
2510 CurrentFilePatched = oldCurrentFilePatched + 1.f;
2512 if (StopAsked)
2514 StopAsked = false;
2515 return;
2519 // Second do all bnp files
2520 // -----------------------
2522 for (i = 0; i < AllFilesToPatch.size(); ++i)
2524 CPatchManager::SFileToPatch &rFTP = AllFilesToPatch[i];
2526 string ext = NLMISC::CFile::getExtension(rFTP.FileName);
2527 if (ext == "bnp" || ext == "snp")
2529 float oldCurrentFilePatched = CurrentFilePatched;
2530 processFile (rFTP);
2531 pPM->MustLaunchBatFile = true;
2532 CurrentFilePatched = oldCurrentFilePatched + 1.f;
2534 if (StopAsked)
2536 StopAsked = false;
2537 return;
2542 catch (const NLMISC::EDiskFullError &)
2544 // more explicit message for this common error case
2545 nlwarning("EXCEPTION CATCH: CPatchThread::run() Disk Full");
2546 pPM->setState(true, CI18N::get("uiPatchEndWithErr"));
2547 sTranslate = CI18N::get("uiPatchDiskFull");
2548 bErr = true;
2550 catch(const Exception &e)
2552 nlwarning("EXCEPTION CATCH: CPatchThread::run() failed");
2553 pPM->setState(true, string(e.what()));
2554 sTranslate = CI18N::get("uiPatchEndWithErr");
2555 bErr = true;
2559 // Unpack all files with the UnpackTo flag
2560 // ---------------------------------------
2561 // To do that we create a batch file that will copy all files (unpacked to patch directory)
2562 // to the directory we want (the UnpackTo string)
2564 // Recreate batch file
2565 if (!pPM->MustLaunchBatFile && !pPM->getAsyncDownloader())
2567 pPM->deleteFile(pPM->UpdateBatchFilename, false, false);
2570 if (!bErr)
2572 sTranslate = CI18N::get("uiPatchEndNoErr");
2574 else
2576 // Set a more explicit error message
2577 pPM->setErrorMessage(sTranslate);
2580 PatchOk = !bErr;
2581 Ended = true;
2585 class CPatchThreadDownloadProgress : public NLMISC::IProgressCallback
2587 public:
2588 CPatchThread *PatchThread;
2589 float Scale;
2590 float Bias;
2591 uint CurrentFilePatched;
2592 CPatchThreadDownloadProgress() : PatchThread(NULL),
2593 Scale(1.f),
2594 Bias(0.f),
2595 CurrentFilePatched(0)
2598 virtual void progress (float progressValue)
2600 clamp(progressValue, 0.f, 1.f);
2601 nlassert(PatchThread);
2602 //PatchThread->CurrentFilePatched = std::max(PatchThread->CurrentFilePatched, (float) this->CurrentFilePatched + Bias + Scale * progressValue);
2603 PatchThread->CurrentFilePatched = this->CurrentFilePatched + Bias + Scale * progressValue;
2604 CPatchManager::getInstance()->touchState();
2608 void stopSoundMngr();
2610 // ****************************************************************************
2611 void CPatchThread::processFile (CPatchManager::SFileToPatch &rFTP)
2613 CPatchManager *pPM = CPatchManager::getInstance();
2615 // Source File Name (in writable or readable directory)
2616 string SourceName;
2618 // Destination File Name (in writable directory)
2619 string DestinationName;
2621 if (NLMISC::startsWith(rFTP.FileName, "sound"))
2623 // Stop sound playback
2624 stopSoundMngr();
2627 if (rFTP.ExtractPath.empty())
2629 DestinationName = pPM->WritableClientDataPath + rFTP.FileName;
2631 if (rFTP.LocalFileExists)
2633 // following lines added by Sadge to ensure that the correct file gets patched
2634 SourceName.clear();
2636 if (NLMISC::CFile::fileExists(pPM->WritableClientDataPath + rFTP.FileName))
2638 SourceName = pPM->WritableClientDataPath + rFTP.FileName;
2640 else if (NLMISC::CFile::fileExists(pPM->ReadableClientDataPath + rFTP.FileName))
2642 SourceName = pPM->ReadableClientDataPath + rFTP.FileName;
2645 // version from previous download
2646 if (SourceName.empty()) throw Exception (std::string("ERROR: Failed to find file: ")+rFTP.FileName);
2648 // following lines removed by Sadge to ensure that the correct file gets patched
2649 // SourceName = CPath::lookup(rFTP.FileName); // exception if file do not exists
2651 else
2653 // note : if file was background downloaded, we have :
2654 // rFTP.LocalFileExists = false
2655 // rFTP.SrcFileName = "unpack/filename.bnp.tmp"
2656 SourceName = DestinationName;
2659 else
2661 SourceName = pPM->ClientPatchPath + rFTP.FileName;
2662 DestinationName = SourceName;
2665 if (rFTP.LocalFileToDelete)
2667 // corrupted or invalid file ? ....
2668 NLMISC::CFile::deleteFile(SourceName);
2669 rFTP.LocalFileExists = false;
2672 string sTranslate;
2673 sTranslate = CI18N::get("uiProcessing") + " " + rFTP.FileName;
2674 pPM->setState(true, sTranslate);
2676 if (pPM->getAsyncDownloader())
2678 if (!rFTP.ExtractPath.empty())
2680 std::string info = rFTP.FileName;
2682 string PatchName = toString("%05d/%s%s", rFTP.Patches[rFTP.Patches.size() - 1], rFTP.FileName.c_str(), ".lzma");
2683 pPM->getAsyncDownloader()->addToDownloadList(PatchName, SourceName, rFTP.LastFileDate, rFTP.ExtractPath, rFTP.SZFileSize, rFTP.FinalFileSize );
2684 return;
2688 std::string tmpSourceName = rFTP.SrcFileName.empty() ? SourceName : rFTP.SrcFileName; // source is same than destination, or possibly
2689 // a patch from a previous background downloader session
2690 if (rFTP.LocalFileToDelete)
2692 // corrupted or invalid file ? ....
2693 rFTP.LocalFileExists = false;
2694 NLMISC::CFile::deleteFile(tmpSourceName);
2697 string OutFilename;
2698 bool usePatchFile = true;
2699 bool haveAllreadyTryiedDownloadingOfFile = false;
2701 // compute the total size of patch to download
2702 uint32 totalPatchSize = 0;
2703 if (rFTP.Incremental)
2705 for (uint i=0; i<rFTP.PatcheSizes.size(); ++i)
2706 totalPatchSize += rFTP.PatcheSizes[i];
2708 else if (!rFTP.PatcheSizes.empty())
2710 totalPatchSize = rFTP.PatcheSizes.back();
2714 CPatchThreadDownloadProgress progress;
2715 progress.PatchThread = this;
2716 progress.CurrentFilePatched = (uint) floorf(CurrentFilePatched);
2718 // look for the source file, if not present or invalid (not matching
2719 // a known version) or if total patch size greater than half the
2720 // uncompressed final size or if total patch size is greater
2721 // than lzma compressed file, then load the lzma file
2722 if ((rFTP.Incremental && !rFTP.LocalFileExists) // incremental and no base file, we need a complete file
2723 || totalPatchSize > rFTP.FinalFileSize / 2 // patch is too big regarding the final file, patch applying time will be slow
2724 || rFTP.SZFileSize < totalPatchSize) // lzma is smaller than patch !
2726 breakable
2728 usePatchFile = false;
2729 // compute the seven zip filename
2730 string lzmaFile = rFTP.FileName+".lzma";
2732 // download the 7zip file
2735 // first, try in the file version subfolfer
2738 progress.Scale = 1.f;
2739 progress.Bias = 0.f;
2740 if (!rFTP.Patches.empty())
2742 pPM->getServerFile(toString("%05u/", rFTP.Patches.back())+lzmaFile, false, "", &progress);
2744 // else -> file comes from a previous download (with .tmp extension, and is up to date)
2745 // the remaining code will just rename it with good name and exit
2747 catch (const NLMISC::EWriteError &)
2749 // this is a local error, rethrow ...
2750 throw;
2752 catch(...)
2754 // failed with version subfolder, try in the root patch directory
2755 pPM->getServerFile(lzmaFile, false, "", &progress);
2758 catch (const NLMISC::EWriteError &)
2760 // this is a local error, rethrow ...
2761 throw;
2763 catch (...)
2765 // can not load the 7zip file, use normal patching
2766 usePatchFile = true;
2767 haveAllreadyTryiedDownloadingOfFile = true;
2768 break;
2771 OutFilename = pPM->ClientPatchPath + NLMISC::CFile::getFilename(rFTP.FileName);
2772 // try to unpack the file
2775 if (!unpackLZMA(pPM->ClientPatchPath+lzmaFile, OutFilename+".tmp"))
2777 // fallback to standard patch method
2778 usePatchFile = true;
2779 haveAllreadyTryiedDownloadingOfFile = true;
2780 break;
2783 catch (const NLMISC::EWriteError&)
2785 throw;
2787 catch (...)
2789 nlwarning("Failed to unpack lzma file %s", (pPM->ClientPatchPath+lzmaFile).c_str());
2790 // fallback to standard patch method
2791 usePatchFile = true;
2792 haveAllreadyTryiedDownloadingOfFile = true;
2793 break;
2796 if (rFTP.LocalFileExists)
2797 pPM->deleteFile(SourceName);
2799 pPM->deleteFile(pPM->ClientPatchPath+lzmaFile); // delete the archive file
2800 pPM->deleteFile(SourceName, false, false); // File can exists if bad BNP loading
2801 if (_CommitPatch)
2803 pPM->renameFile(OutFilename+".tmp", DestinationName);
2807 if (usePatchFile)
2809 uint32 currentPatchedSize = 0;
2810 for (uint32 j = 0; j < rFTP.Patches.size(); ++j)
2812 // Get the patch
2813 // first, try in the patch subdirectory
2814 string PatchName;
2817 PatchName = toString("%05d/", rFTP.Patches[j]) + rFTP.FileName.substr(0, rFTP.FileName.size()-4) + toString("_%05d", rFTP.Patches[j]) + ".patch";
2818 sTranslate = CI18N::get("uiLoginGetFile") + " " + PatchName;
2819 pPM->setState(true, sTranslate);
2820 progress.Scale = 1.f;
2821 progress.Bias = totalPatchSize != 0 ? (float) currentPatchedSize / totalPatchSize : 0.f;
2822 progress.Scale = totalPatchSize != 0 ? (float) rFTP.PatcheSizes[j] / totalPatchSize : 1.f;
2823 pPM->getServerFile(PatchName, false, "", &progress);
2824 // remove the subfolder name
2825 PatchName = NLMISC::CFile::getFilename(PatchName);
2827 catch (const NLMISC::EWriteError &)
2829 throw;
2831 catch (...)
2833 // fallback to patch root directory
2834 PatchName = rFTP.FileName.substr(0, rFTP.FileName.size()-4);
2835 PatchName += toString("_%05d", rFTP.Patches[j]) + ".patch";
2836 sTranslate = CI18N::get("uiLoginGetFile") + " " + PatchName;
2837 pPM->setState(true, sTranslate);
2838 pPM->getServerFile(PatchName, false, "", &progress);
2841 // Apply the patch
2842 // If the patches are not to be applied on the last version
2843 string SourceNameXD = tmpSourceName;
2844 if (!rFTP.Incremental)
2846 // Some note about non incremental patches :
2847 // Usually, files such as '.exe' or '.dll' do not work well with incremental patch, so it is better
2848 // to apply a single patch from a refrence version to them. (so here we necessarily got
2849 // 'rFTP.Patches.size() == 1' !)
2850 // The reference version itself is incrementally patched, however, and may be rebuilt from time to time
2851 // when the delta to the reference has become too big (but is most of the time < to the sum of equivallent delta patchs)
2852 // The non-incremental final patch (from ref file to final bnp), is guaranteed to be done last
2853 // after the reference file has been updated
2855 // Find the reference file to apply the patch
2856 SourceNameXD = rFTP.FileName;
2857 SourceNameXD = SourceNameXD.substr(0, SourceNameXD.rfind('.'));
2858 SourceNameXD += "_.ref";
2860 if (!_CommitPatch)
2862 // works
2863 std::string tmpRefFile = SourceNameXD + ".tmp";
2864 if (!NLMISC::CFile::fileExists(pPM->ClientPatchPath + tmpRefFile))
2866 // Not found in the patch directory -> version in data directory should be good, or would have been
2867 // detected by the check thread else.
2869 else
2871 SourceNameXD = tmpRefFile; // good ref file download by bg downloader
2875 // following lines added by Sadge to ensure that the correct file gets patched
2876 // string SourceNameXDFull;
2877 // if (NLMISC::CFile::fileExists(pPM->ClientDataPath + SourceNameXD)) SourceNameXDFull = pPM->ClientDataPath + SourceNameXD;
2879 // following lines removed by Sadge to ensure that the correct file gets patched
2880 // string SourceNameXDFull = CPath::lookup(SourceNameXD, false, false);
2881 // if (SourceNameXDFull.empty())
2882 // SourceNameXDFull = pPM->ClientDataPath + SourceNameXD;
2883 // SourceNameXD = SourceNameXDFull;
2884 if (CFile::fileExists(pPM->WritableClientDataPath + SourceNameXD))
2886 SourceNameXD = pPM->WritableClientDataPath + SourceNameXD;
2888 else if (CFile::fileExists(pPM->ReadableClientDataPath + SourceNameXD))
2890 SourceNameXD = pPM->ReadableClientDataPath + SourceNameXD;
2894 PatchName = pPM->ClientPatchPath + PatchName;
2896 string OutFilename = pPM->ClientPatchPath + rFTP.FileName + ".tmp__" + toString(j);
2898 sTranslate = CI18N::get("uiApplyingDelta") + " " + CFile::getFilename(PatchName);
2899 pPM->setState(true, sTranslate);
2901 bool deltaPatchResult = xDeltaPatch(PatchName, SourceNameXD, OutFilename);
2903 if (!deltaPatchResult && !haveAllreadyTryiedDownloadingOfFile) // Patch failed, try to download and apply lzma
2905 breakable
2907 // compute the seven zip filename
2908 string lzmaFile = rFTP.FileName+".lzma";
2910 // download the 7zip file
2913 // first, try in the file version subfolfer
2916 progress.Scale = 1.f;
2917 progress.Bias = 0.f;
2918 if (!rFTP.Patches.empty())
2920 pPM->getServerFile(toString("%05u/", rFTP.Patches.back())+lzmaFile, false, "", &progress);
2922 // else -> file comes from a previous download (with .tmp extension, and is up to date)
2923 // the remaining code will just rename it with good name and exit
2925 catch (const NLMISC::EWriteError &)
2927 // this is a local error, rethrow ...
2928 throw;
2930 catch(...)
2932 // failed with version subfolder, try in the root patch directory
2933 pPM->getServerFile(lzmaFile, false, "", &progress);
2936 catch (const NLMISC::EWriteError &)
2938 // this is a local error, rethrow ...
2939 throw;
2941 catch (...)
2943 break;
2946 OutFilename = pPM->ClientPatchPath + NLMISC::CFile::getFilename(rFTP.FileName);
2947 // try to unpack the file
2950 if (!unpackLZMA(pPM->ClientPatchPath+lzmaFile, OutFilename+".tmp"))
2952 break;
2955 catch (const NLMISC::EWriteError&)
2957 throw;
2959 catch (...)
2961 nlwarning("Failed to unpack lzma file %s", (pPM->ClientPatchPath+lzmaFile).c_str());
2962 break;
2965 if (rFTP.LocalFileExists)
2966 pPM->deleteFile(SourceName);
2968 pPM->deleteFile(pPM->ClientPatchPath+lzmaFile); // delete the archive file
2969 pPM->deleteFile(SourceName, false, false); // File can exists if bad BNP loading
2970 if (_CommitPatch)
2972 pPM->renameFile(OutFilename+".tmp", DestinationName);
2976 else
2978 if (rFTP.LocalFileExists)
2979 pPM->deleteFile(SourceName);
2980 pPM->deleteFile(PatchName);
2982 if (j > 0)
2984 pPM->deleteFile(SourceNameXD, false, false); // File can exists if bad BNP loading
2986 tmpSourceName = OutFilename;
2987 PatchSizeProgress += rFTP.PatcheSizes[j];
2988 currentPatchedSize += rFTP.PatcheSizes[j];
2991 if (tmpSourceName != DestinationName)
2993 pPM->deleteFile(SourceName, false, false); // File can exists if bad BNP loading
2994 if (!_CommitPatch)
2996 // let the patch in the unpack directory
2997 pPM->renameFile(tmpSourceName, pPM->ClientPatchPath + rFTP.FileName + ".tmp");
2999 else
3001 pPM->renameFile(tmpSourceName, DestinationName);
3005 else
3007 PatchSizeProgress += totalPatchSize;
3010 // If all patches applied with success so file size should be ok
3011 // We just have to change file date to match the last patch applied
3012 pPM->applyDate(DestinationName, rFTP.LastFileDate);
3013 //progress.progress(1.f);
3016 // ****************************************************************************
3017 bool CPatchThread::xDeltaPatch(const string &patch, const string &src, const string &out)
3019 // Internal xdelta
3021 CPatchManager::MyPatchingCB patchingCB;
3023 patchingCB.patchFilename = patch;
3024 patchingCB.srcFilename = src;
3026 CPatchManager *pPM = CPatchManager::getInstance();
3027 pPM->deleteFile(out, false, false);
3029 std::string errorMsg;
3030 CXDeltaPatch::TApplyResult ar = CXDeltaPatch::apply(patch, src, out, errorMsg, &patchingCB);
3031 if (ar != CXDeltaPatch::ApplyResult_Ok)
3033 switch(ar)
3035 case CXDeltaPatch::ApplyResult_WriteError:
3036 throw NLMISC::EWriteError(out);
3037 break;
3038 case CXDeltaPatch::ApplyResult_DiskFull:
3039 throw NLMISC::EDiskFullError(out);
3040 break;
3041 default:
3043 nlinfo("Error applying %s to %s giving %s", patch.c_str(), src.c_str(), out.c_str());
3044 return false;
3046 break;
3048 } else
3049 return true;
3052 // Launching xdelta
3054 // Start the child process.
3055 string strCmdLine = "xdelta patch " + patch + " " + src + " " + out;
3060 // ****************************************************************************
3061 // ****************************************************************************
3062 // ****************************************************************************
3063 // CScanDataThread
3064 // ****************************************************************************
3065 // ****************************************************************************
3066 // ****************************************************************************
3068 // ****************************************************************************
3069 CScanDataThread::CScanDataThread()
3071 AskForCancel= false;
3072 Ended = false;
3073 CheckOk = false;
3074 TotalFileToScan = 1;
3075 CurrentFileScanned = 1;
3078 // ****************************************************************************
3079 void CScanDataThread::run ()
3081 CPatchManager *pPM = CPatchManager::getInstance();
3084 uint32 i;
3085 // Check if the client version is the same as the server version
3086 string sClientVersion = pPM->getClientVersion();
3087 string sTranslate = CI18N::get("uiClientVersion") + " (" + sClientVersion + ") ";
3088 pPM->setState(true, sTranslate);
3090 // For all bnp in the description file get all patches to apply
3091 // depending on the version of the client bnp files
3092 const CBNPFileSet &rDescFiles = pPM->DescFile.getFiles();
3093 TotalFileToScan = rDescFiles.fileCount();
3094 for (i = 0; i < rDescFiles.fileCount(); ++i)
3096 sTranslate = CI18N::get("uiCheckingFile") + " " + rDescFiles.getFile(i).getFileName();
3097 pPM->setState(true, sTranslate);
3099 // get list of file to apply to this patch, performing a full checksum test (slow...)
3100 CPatchManager::SFileToPatch ftp;
3101 pPM->getPatchFromDesc(ftp, rDescFiles.getFile(i), false);
3102 // if the file has been found but don't correspond to any local version (SHA1)
3103 // or if the file has not been found (or the .bnp is very very buggy so that addSearchFile() failed)
3104 if( (ftp.LocalFileExists && ftp.LocalFileToDelete) ||
3105 (!ftp.LocalFileExists && !ftp.LocalFileToDelete) )
3107 pPM->addDataScanLogCorruptedFile(ftp);
3108 CPatchManager::getCorruptedFileInfo(ftp, sTranslate);
3109 pPM->setState(true, sTranslate);
3111 CurrentFileScanned = i;
3113 // if the user ask to cancel the thread, stop now
3114 if(AskForCancel)
3115 break;
3118 sTranslate = CI18N::get("uiCheckEndNoErr");
3119 pPM->setState(true, sTranslate);
3120 CheckOk = true;
3121 Ended = true;
3123 catch (const Exception &e)
3125 nlwarning("EXCEPTION CATCH: CScanDataThread::run() failed");
3126 string sTranslate = CI18N::get("uiCheckEndWithErr") + " " + e.what();
3127 pPM->setState(true, sTranslate);
3128 CheckOk = false;
3129 Ended = true;
3134 // ****************************************************************************
3135 uint32 CPatchManager::SPatchInfo::getAvailablePatchsBitfield() const
3137 // About the test (until a patch enum is added, we use the 'optional' flag)
3138 // Non optional -> must patch it (will be for RoS)
3139 // Optional -> Will be for Mainland
3140 // Required : stands for 'bnp' required by the Optional bnps !! so ignore only RoS is wanted
3142 uint32 result = 0;
3143 if (!NonOptCat.empty())
3145 result |= (1 << BGDownloader::DownloadID_RoS);
3147 if (!OptCat.empty() || !ReqCat.empty())
3149 result |= (1 << BGDownloader::DownloadID_MainLand);
3151 return result;
3154 // ***************************************************************************
3155 void CPatchManager::setAsyncDownloader(IAsyncDownloader* asyncDownloader)
3157 _AsyncDownloader = asyncDownloader;
3159 // ***************************************************************************
3160 IAsyncDownloader* CPatchManager::getAsyncDownloader() const
3162 return _AsyncDownloader;
3164 // **************************************************************************
3165 // ****************************************************************************
3166 void CPatchManager::startInstallThread(const std::vector<CInstallThreadEntry>& entries)
3168 InstallThread = new CInstallThread(entries);
3169 Thread = IThread::create (InstallThread);
3170 nlassert (Thread != NULL);
3171 Thread->start ();
3174 void CPatchManager::startDownloadThread(const std::vector<CInstallThreadEntry>& entries)
3176 DownloadThread = new CDownloadThread(entries);
3177 Thread = IThread::create (DownloadThread);
3178 nlassert (Thread != NULL);
3179 Thread->start ();
3183 void CPatchManager::onFileInstallFinished()
3185 if (_AsyncDownloader)
3187 _AsyncDownloader->onFileInstallFinished();
3192 void CPatchManager::onFileDownloadFinished()
3194 if (_AsyncDownloader)
3196 _AsyncDownloader->onFileDownloadFinished();
3201 void CPatchManager::onFileDownloading(const std::string& sourceName, uint32 rate, uint32 fileIndex, uint32 fileCount, uint64 fileSize, uint64 fullSize)
3203 if (_AsyncDownloader)
3205 _AsyncDownloader->onFileDownloading(sourceName, rate, fileIndex, fileCount, fileSize, fullSize);
3209 void CPatchManager::onFileInstalling(const std::string& sourceName, uint32 rate, uint32 fileIndex, uint32 fileCount, uint64 fileSize, uint64 fullSize)
3211 if (_AsyncDownloader)
3213 _AsyncDownloader->onFileInstalling(sourceName, rate, fileIndex, fileCount, fileSize, fullSize);
3217 bool CPatchManager::download(const std::string& patchFullname, const std::string& sourceFullname,
3218 const std::string& tmpDirectory, uint32 timestamp)
3220 CPatchManager *pPM = CPatchManager::getInstance();
3222 static std::string zsStr = ".lzma";
3223 static std::string::size_type zsStrLength = zsStr.size();
3225 // we delete file if present and diferent timestamp
3226 // if the file is the same then indicates its not necessary to download it a second time
3227 if (NLMISC::CFile::fileExists(sourceFullname))
3229 uint32 t = NLMISC::CFile::getFileModificationDate(sourceFullname);
3230 if (t == timestamp)
3231 { //we didn't downl oad
3232 return true;
3234 pPM->deleteFile(sourceFullname);
3236 // file will be save to a .tmp file
3237 std::string extension;
3238 if (patchFullname.size() >= zsStrLength && patchFullname.substr(patchFullname.size() - zsStrLength) == zsStr)
3240 extension = zsStr;
3242 // remove tmp file if exist
3243 std::string patchName = tmpDirectory + NLMISC::CFile::getFilename(sourceFullname) + extension + std::string(".tmp");
3244 if (NLMISC::CFile::fileExists(patchName))
3246 pPM->deleteFile(patchName);
3248 // creates directory tree if necessary
3249 NLMISC::CFile::createDirectoryTree( NLMISC::CFile::getPath(patchName) );
3250 NLMISC::CFile::createDirectoryTree( NLMISC::CFile::getPath(sourceFullname) );
3252 // try to download
3255 pPM->getServerFile(patchFullname, false, patchName);
3257 catch ( const std::exception& e)
3259 nlwarning("%s", e.what());
3260 pPM->setState(true, string(e.what()) );
3261 return false;
3264 // install
3265 if (!NLMISC::CFile::fileExists(patchName))
3267 return false;
3269 // if file is compressed unpack then commit (rename)
3270 if (patchName.size() >= zsStrLength
3271 && patchName.substr(patchName.size() - zsStrLength) == zsStr)
3273 std::string outFilename = patchName.substr(0, patchName.size() - zsStrLength);
3274 unpack7Zip(patchName, outFilename);
3275 pPM->deleteFile(patchName);
3276 pPM->renameFile(outFilename, sourceFullname);
3278 else
3280 // file is not compressed so rename the .tmp file to final name
3281 pPM->renameFile(patchName, sourceFullname);
3283 // apply modification date
3284 pPM->applyDate(sourceFullname, timestamp);
3286 return true;
3290 bool CPatchManager::extract(const std::string& patchPath,
3291 const std::vector<std::string>& sourceFilename,
3292 const std::vector<std::string>& extractPath,
3293 const std::string& updateBatchFilename,
3294 const std::string& execName,
3295 void (*stopFun)() )
3297 nlassert(sourceFilename.size() == extractPath.size());
3298 CPatchManager *pPM = CPatchManager::getInstance();
3300 bool ok = false;
3301 for (uint32 j = 0; j < extractPath.size() && !ok; ++j)
3303 if (!extractPath[j].empty())
3305 ok = true;
3309 if (!ok)
3311 // nothing to extract
3312 return false;
3315 // extract
3316 uint nblab = 0;
3317 pPM->deleteFile(updateBatchFilename, false, false);
3319 FILE *fp = nlfopen (updateBatchFilename, "wt");
3321 if (fp == 0)
3323 string err = toString("Can't open file '%s' for writing: code=%d %s (error code 29)", updateBatchFilename.c_str(), errno, strerror(errno));
3324 throw Exception (err);
3327 #ifdef NL_OS_WINDOWS
3328 fprintf(fp, "@echo off\n");
3329 fprintf(fp, "ping 127.0.0.1 -n 7 -w 1000 > nul\n"); // wait
3330 #else
3331 fprintf(fp, "#!/bin/sh\n");
3332 fprintf(fp, "sleep 7\n"); // wait
3333 #endif
3335 // Unpack files with category ExtractPath non empty
3336 for (uint32 j = 0; j < sourceFilename.size(); ++j)
3338 if (!extractPath[j].empty())
3340 string rFilename = sourceFilename[j];
3341 // Extract to patch
3342 vector<string> vFilenames;
3343 if (!pPM->bnpUnpack(rFilename, patchPath, vFilenames))
3346 string err = toString("Error unpacking %s", rFilename.c_str());
3347 throw Exception (err);
3350 else
3352 std::string sourcePath = NLMISC::CFile::getPath(sourceFilename[j]);
3353 for (uint32 fff = 0; fff < vFilenames.size (); fff++)
3355 string SrcPath = CPath::standardizeDosPath(sourcePath);
3356 string SrcName = SrcPath + vFilenames[fff];
3357 string DstPath = CPath::standardizeDosPath(extractPath[j]);
3358 string DstName = DstPath + vFilenames[fff];
3359 NLMISC::CFile::createDirectoryTree(extractPath[j]);
3361 // this file must be moved
3362 #ifdef NL_OS_WINDOWS
3363 fprintf(fp, ":loop%u\n", nblab);
3364 fprintf(fp, "attrib -r -a -s -h %s\n", DstName.c_str());
3365 fprintf(fp, "del %s\n", DstName.c_str());
3366 fprintf(fp, "if exist %s goto loop%u\n", DstName.c_str(), nblab);
3367 fprintf(fp, "move %s %s\n", SrcName.c_str(), DstPath.c_str());
3368 #else
3369 // TODO: for Linux and OS X
3370 #endif
3372 nblab++;
3380 #ifdef NL_OS_WINDOWS
3381 fprintf(fp, "start %s %%1 %%2 %%3\n", execName.c_str());
3382 #else
3383 // TODO: for Linux and OS X
3384 #endif
3386 fclose(fp);
3388 if (stopFun)
3390 stopFun();
3393 if (!launchProgram(updateBatchFilename, "", false))
3395 // error occurs during the launch
3396 string str = toString("Can't execute '%s': code=%d %s (error code 30)", updateBatchFilename.c_str(), errno, strerror(errno));
3397 throw Exception (str);
3400 return true;
3404 void CPatchManager::setStateListener(IPatchManagerStateListener* stateListener)
3406 _StateListener = stateListener;
3410 void CPatchManager::fatalError(const std::string& errorId, const std::string& param1, const std::string& param2)
3412 if (_AsyncDownloader)
3414 _AsyncDownloader->fatalError(errorId, param1, param2);
3419 // ****************************************************************************
3420 void CDownloadThread::run()
3422 CPatchManager *pPM = CPatchManager::getInstance();
3424 std::string patchPath = CPath::standardizePath (ClientRootPath+TheTmpInstallDirectory)+"patch/";
3427 static bool _FirstTime = true;
3428 static uint64 _FullSize = 0;
3429 static uint64 _CurrentSize = 0;
3430 static uint32 _Start;
3432 // At first launch calculat the amount of data need to download
3433 if (_FirstTime)
3435 for (uint first = 0,last = (uint)_Entries.size() ; first != last; ++first)
3437 _FullSize += _Entries[first].SZipFileSize;
3439 _Start = NLMISC::CTime::getSecondsSince1970();
3440 _FirstTime = false;
3444 for (uint first = 0,last = (uint)_Entries.size() ; first != last; ++first)
3446 std::string patchName = CPath::standardizePath (_Entries[first].PatchName, false);
3447 std::string sourceName = CPath::standardizePath (_Entries[first].SourceName, false);
3449 _CurrentSize += _Entries[first].SZipFileSize;
3451 uint32 rate = 0;
3452 // Calculate the time since last update
3453 uint32 dt = NLMISC::CTime::getSecondsSince1970() - _Start;
3454 if (dt)
3456 rate = uint32(_CurrentSize / dt);
3458 // update Gui
3459 pPM->onFileDownloading(sourceName, rate, first+1, last, _CurrentSize, _FullSize);
3461 std::string finalFile =patchPath+ patchName;
3462 std::string tmpFile = finalFile + std::string(".part");
3465 bool toDownload = true;
3466 static volatile bool simulateDownload = false;
3467 if (simulateDownload)
3469 nlSleep(100);
3471 else
3473 // Do not download if file are identicaly (check size and modification date)
3474 if (NLMISC::CFile::fileExists(finalFile))
3476 uint64 fz = NLMISC::CFile::getFileSize(finalFile);
3477 uint32 timestamp = _Entries[first].Timestamp;
3478 uint32 timestamp2 = NLMISC::CFile::getFileModificationDate(finalFile);
3479 if ( fz == _Entries[first].SZipFileSize && timestamp == timestamp2)
3481 toDownload = false;
3483 else
3485 NLMISC::CFile::deleteFile(finalFile);
3489 // file need to be download
3490 if (toDownload)
3492 // delete tmp file if exist
3493 if (NLMISC::CFile::fileExists(tmpFile))
3495 NLMISC::CFile::deleteFile(tmpFile);
3497 std::string path = NLMISC::CFile::getPath(tmpFile);
3498 // create directory tree
3499 NLMISC::CFile::createDirectoryTree(path);
3500 // Try to download, rename, applyDate and send error msg to gui in case of error
3503 pPM->getServerFile(patchName, false, tmpFile);
3504 NLMISC::CFile::moveFile(finalFile, tmpFile);
3506 pPM->applyDate(finalFile, _Entries[first].Timestamp);
3508 catch ( const std::exception& e)
3510 nlwarning("%s", e.what());
3511 pPM->setState(true, string(e.what()) );
3512 pPM->fatalError("uiCanNotDownload", patchName.c_str(), "");
3514 catch (...)
3516 pPM->fatalError("uiCanNotDownload", patchName.c_str(), "");
3522 // message to gui to indicates that all file are downloaded
3523 pPM->onFileDownloadFinished();
3527 void CInstallThread::run()
3529 std::string patchPath = CPath::standardizePath (ClientRootPath+TheTmpInstallDirectory)+"patch/";
3530 CPatchManager *pPM = CPatchManager::getInstance();
3532 std::set<std::string> allowed;
3534 uint first, last;
3536 for (first = 0,last = (uint)_Entries.size() ; first != last; ++first)
3538 std::string correct = CPath::standardizePath (patchPath + _Entries[first].PatchName, false);
3539 allowed.insert(correct);
3541 // Delete file from tmp directory that are not "allowed" (torrent protocol can download partial file that are not asked)
3542 std::vector<std::string> vFiles;
3543 CPath::getPathContent(patchPath , true, false, true, vFiles);
3544 for (uint32 i = 0; i < vFiles.size(); ++i)
3546 std::string sName2 = CPath::standardizePath (vFiles[i], false);
3547 if (allowed.find(sName2) == allowed.end() )
3549 pPM->deleteFile(sName2 , false, false);
3553 static bool _FirstTime = true;
3554 static uint64 _FullSize = 0;
3555 static uint64 _CurrentSize = 0;
3556 static uint32 _Start;
3558 // calculate size of data to download in order to know the install speed
3559 if (_FirstTime)
3561 for (uint first = 0,last = (uint)_Entries.size() ; first != last; ++first)
3563 _FullSize += _Entries[first].Size;
3565 _Start = NLMISC::CTime::getSecondsSince1970();
3566 _FirstTime = false;
3570 for (first = 0,last = (uint)_Entries.size() ; first != last; ++first)
3572 std::string patchName = CPath::standardizePath (patchPath +_Entries[first].PatchName, false);
3573 std::string sourceName = CPath::standardizePath (_Entries[first].SourceName, false);
3574 uint32 lastFileDate = _Entries[first].Timestamp;
3577 _CurrentSize += _Entries[first].Size;
3578 uint32 rate = 0;
3579 // calcule the install speed
3580 uint32 dt = NLMISC::CTime::getSecondsSince1970() - _Start;
3581 if (dt)
3583 uint64 size = _CurrentSize / dt;
3584 rate = uint32 (size);
3587 // File can exists if bad BNP loading
3588 if (NLMISC::CFile::fileExists(sourceName))
3590 pPM->deleteFile(sourceName, false, false);
3593 if (NLMISC::CFile::fileExists(patchName))
3595 static std::string zsStr = ".lzma";
3596 static std::string::size_type zsStrLength = zsStr.size();
3599 // if file is compressed unpack the file (decompress, rename tempory file, apply modification date)
3600 if (patchName.size() >= zsStrLength
3601 && patchName.substr(patchName.size() - zsStrLength) == zsStr)
3603 std::string outFilename = patchName.substr(0, patchName.size() - zsStrLength);
3604 std::string localOutFilename = CPath::standardizeDosPath(outFilename);
3606 if ( unpackLZMA(patchName, localOutFilename) )
3608 pPM->deleteFile(patchName);
3609 pPM->renameFile(outFilename, sourceName);
3610 pPM->applyDate(sourceName, lastFileDate);
3612 else
3614 throw NLMISC::Exception("Can not unpack");
3618 else
3620 // if file is uncompressed rename tempory file and apply modification date)
3621 pPM->renameFile(patchName, sourceName);
3622 pPM->applyDate(sourceName, lastFileDate);
3625 catch ( const std::exception& e)
3627 nlwarning("%s", e.what());
3628 pPM->setState(true, string(e.what()) );
3629 pPM->fatalError("uiCanNotInstall", patchName.c_str(), "");
3630 return;
3633 catch (...)
3635 pPM->fatalError("uiCanNotInstall", patchName.c_str(), "");
3636 return;
3639 // update gui
3640 pPM->onFileInstalling(sourceName, rate, first+1, last, _CurrentSize, _FullSize);
3643 // extract bnp
3646 // remove date from tmp directory (because install is finished)
3647 std::string install = CPath::standardizePath (ClientRootPath+TheTmpInstallDirectory);
3649 std::vector<std::string> vFiles;
3650 // Delete all classic file from tmp directory
3651 CPath::getPathContent(install, true, false, true, vFiles);
3652 for (uint32 i = 0; i < vFiles.size(); ++i)
3654 NLMISC::CFile::deleteFile(vFiles[i]);
3656 // Delete all directory from tmp directory
3659 vFiles.clear();
3660 CPath::getPathContent(install, true, true, false, vFiles);
3661 for (uint32 i = 0; i < vFiles.size(); ++i)
3663 NLMISC::CFile::deleteDirectory(vFiles[i]);
3666 while ( !vFiles.empty() );
3667 // delete tmp directory
3668 NLMISC::CFile::deleteDirectory(install);
3669 // delete libtorrent_logs directory if exist (not activate)
3670 if (NLMISC::CFile::fileExists("libtorrent_logs"))
3674 vFiles.clear();
3675 CPath::getPathContent("libtorrent_logs", true, true, false, vFiles);
3676 for (uint32 i = 0; i < vFiles.size(); ++i)
3678 NLMISC::CFile::deleteDirectory(vFiles[i]);
3681 while ( !vFiles.empty() );
3682 NLMISC::CFile::deleteDirectory("libtorrent_logs");
3687 pPM->reboot(); // do not reboot just run the extract .bat