- initial code for managing mod content (downloads and removal of user data)

This commit is contained in:
Mark Vejvoda
2011-03-25 10:11:16 +00:00
parent 90c12da33e
commit 9681e7f1bc
19 changed files with 1762 additions and 141 deletions

View File

@@ -51,7 +51,8 @@ public:
string currentFilename;
};
virtual void FTPClient_CallbackEvent(string itemName, FTP_Client_CallbackType type, FTP_Client_ResultType result, void *userdata) = 0;
virtual void FTPClient_CallbackEvent(string itemName,
FTP_Client_CallbackType type, pair<FTP_Client_ResultType,string> result, void *userdata) = 0;
};
class FTPClientThread : public BaseThread
@@ -65,22 +66,22 @@ protected:
std::pair<string,string> techtreesPath;
Mutex mutexMapFileList;
vector<string> mapFileList;
vector<pair<string,string> > mapFileList;
Mutex mutexTilesetList;
vector<string> tilesetList;
vector<pair<string,string> > tilesetList;
Mutex mutexTechtreeList;
vector<string> techtreeList;
vector<pair<string,string> > techtreeList;
void getMapFromServer(string mapFilename);
FTP_Client_ResultType getMapFromServer(string mapFileName, string ftpUser, string ftpUserPassword);
void getMapFromServer(pair<string,string> mapFilename);
pair<FTP_Client_ResultType,string> getMapFromServer(pair<string,string> mapFileName, string ftpUser, string ftpUserPassword);
void getTilesetFromServer(string tileSetName);
FTP_Client_ResultType getTilesetFromServer(string tileSetName, string tileSetNameSubfolder, string ftpUser, string ftpUserPassword, bool findArchive);
void getTilesetFromServer(pair<string,string> tileSetName);
pair<FTP_Client_ResultType,string> getTilesetFromServer(pair<string,string> tileSetName, string tileSetNameSubfolder, string ftpUser, string ftpUserPassword, bool findArchive);
void getTechtreeFromServer(string techtreeName);
FTP_Client_ResultType getTechtreeFromServer(string techtreeName, string ftpUser, string ftpUserPassword);
void getTechtreeFromServer(pair<string,string> techtreeName);
pair<FTP_Client_ResultType,string> getTechtreeFromServer(pair<string,string> techtreeName, string ftpUser, string ftpUserPassword);
Mutex mutexProgressMutex;
@@ -88,7 +89,7 @@ protected:
string fileArchiveExtractCommand;
string fileArchiveExtractCommandParameters;
FTP_Client_ResultType getFileFromServer(string fileNameTitle,
pair<FTP_Client_ResultType,string> getFileFromServer(pair<string,string> fileNameTitle,
string remotePath, string destFileSaveAs, string ftpUser,
string ftpUserPassword, vector <string> *wantDirListOnly=NULL);
@@ -106,9 +107,9 @@ public:
virtual void signalQuit();
virtual bool shutdownAndWait();
void addMapToRequests(string mapFilename);
void addTilesetToRequests(string tileSetName);
void addTechtreeToRequests(string techtreeName);
void addMapToRequests(string mapFilename,string URL="");
void addTilesetToRequests(string tileSetName,string URL="");
void addTechtreeToRequests(string techtreeName,string URL="");
FTPClientCallbackInterface * getCallBackObject();
void setCallBackObject(FTPClientCallbackInterface *value);

View File

@@ -194,6 +194,8 @@ int64 Chrono::getCurTicks() {
// =====================================
void Tokenize(const string& str,vector<string>& tokens,const string& delimiters) {
/*
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
@@ -207,6 +209,22 @@ void Tokenize(const string& str,vector<string>& tokens,const string& delimiters)
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
*/
// Assume textLine contains the line of text to parse.
string textLine = str;
size_t pos = 0;
while( true ) {
size_t nextPos = textLine.find( delimiters, pos );
if( nextPos == textLine.npos ) {
tokens.push_back(textLine.substr(pos, textLine.length( )));
break;
}
tokens.push_back( string( textLine.substr( pos, nextPos - pos ) ) );
pos = nextPos + 1;
}
}
void findDirs(string path, vector<string> &results, bool errorOnNotFound,bool keepDuplicates) {
@@ -372,7 +390,9 @@ bool isdir(const char *path)
void removeFolder(const string path) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
string deletePath = path + "*";
string deletePath = path;
endPathWithSlash(deletePath);
deletePath += "{,.}*";
vector<string> results = getFolderTreeContentsListRecursively(deletePath, "", true);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path [%s] results.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,path.c_str(),results.size());
@@ -381,6 +401,11 @@ void removeFolder(const string path) {
for(int i = results.size() -1; i >= 0; --i) {
string item = results[i];
if(item.find(".svn") != string::npos) {
printf("!!!!!!!!!!!!!!!!!! FOUND SPECIAL FOLDER [%s] in [%s]\n",item.c_str(),path.c_str());
}
if(isdir(item.c_str()) == false) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] file item [%s]\n",__FILE__,__FUNCTION__,__LINE__,item.c_str());
@@ -395,6 +420,9 @@ void removeFolder(const string path) {
string item = results[i];
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] item [%s] isdir(item.c_str()) = %d\n",__FILE__,__FUNCTION__,__LINE__,item.c_str(), isdir(item.c_str()));
if(isdir(item.c_str()) == true) {
printf("~~~~~ REMOVE FOLDER [%s] in [%s]\n",item.c_str(),path.c_str());
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] item [%s]\n",__FILE__,__FUNCTION__,__LINE__,item.c_str());
#ifdef WIN32
int result = _rmdir(item.c_str());
@@ -402,6 +430,11 @@ void removeFolder(const string path) {
int result = rmdir(item.c_str());
#endif
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] item [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,item.c_str(),result);
if(result != 0 && item != path) {
printf("CANNOT REMOVE FOLDER [%s] in [%s]\n",item.c_str(),path.c_str());
removeFolder(item);
}
}
}
@@ -861,7 +894,12 @@ vector<string> getFolderTreeContentsListRecursively(const string &path, const st
glob_t globbuf;
int res = glob(mypath.c_str(), 0, 0, &globbuf);
int globFlags = 0;
if(EndsWith(mypath,"{,.}*") == true) {
globFlags = GLOB_BRACE;
}
int res = glob(mypath.c_str(), globFlags, 0, &globbuf);
#if !defined(__APPLE__) && !defined(__FreeBSD__)
if(res < 0) {
std::stringstream msg;
@@ -872,25 +910,25 @@ vector<string> getFolderTreeContentsListRecursively(const string &path, const st
for(int i = 0; i < globbuf.gl_pathc; ++i) {
const char* p = globbuf.gl_pathv[i];
if(isdir(p) == false) {
bool addFile = true;
//if(EndsWith(p, ".") == true || EndsWith(p, "..") == true || EndsWith(p, ".svn") == true) {
// addFile = false;
//}
//else
if(filterFileExt != "") {
addFile = EndsWith(p, filterFileExt);
}
bool skipItem = (EndsWith(p, ".") == true || EndsWith(p, "..") == true);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("~~~~~~~~~~~ Glob file [%s] skipItem = %d\n",p,skipItem);
if(addFile) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] adding file [%s]\n",__FILE__,__FUNCTION__,p);
resultFiles.push_back(p);
}
}
else if(includeFolders == true) {
resultFiles.push_back(p);
}
if(skipItem == false) {
if(isdir(p) == false) {
bool addFile = true;
if(filterFileExt != "") {
addFile = EndsWith(p, filterFileExt);
}
if(addFile) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] adding file [%s]\n",__FILE__,__FUNCTION__,p);
resultFiles.push_back(p);
}
}
else if(includeFolders == true) {
resultFiles.push_back(p);
}
}
}
globfree(&globbuf);
@@ -899,7 +937,8 @@ vector<string> getFolderTreeContentsListRecursively(const string &path, const st
#if defined(__APPLE__) || defined(__FreeBSD__)
res = glob(mypath.c_str(), 0, 0, &globbuf);
#else //APPLE doesn't have the GLOB_ONLYDIR definition..
res = glob(mypath.c_str(), GLOB_ONLYDIR, 0, &globbuf);
globFlags |= GLOB_ONLYDIR;
res = glob(mypath.c_str(), globFlags, 0, &globbuf);
#endif
#if !defined(__APPLE__) && !defined(__FreeBSD__)
@@ -919,14 +958,26 @@ vector<string> getFolderTreeContentsListRecursively(const string &path, const st
continue;
#endif
const char* p = globbuf.gl_pathv[i];
if(includeFolders == true) {
resultFiles.push_back(p);
bool skipItem = (EndsWith(p, ".") == true || EndsWith(p, "..") == true);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("~~~~~~~~~~~ Glob folder [%s] skipItem = %d\n",p,skipItem);
if(skipItem == false) {
if(includeFolders == true) {
resultFiles.push_back(p);
}
string currentPath = p;
endPathWithSlash(currentPath);
if(EndsWith(mypath,"{,.}*") == true) {
currentPath += "{,.}*";
}
else {
currentPath += "*";
}
resultFiles = getFolderTreeContentsListRecursively(currentPath, filterFileExt, includeFolders,&resultFiles);
}
string currentPath = p;
endPathWithSlash(currentPath);
resultFiles = getFolderTreeContentsListRecursively(currentPath + "*", filterFileExt, includeFolders,&resultFiles);
}
globfree(&globbuf);

View File

@@ -193,7 +193,7 @@ int file_progress(struct FtpFile *out,double download_total, double download_now
stats.currentFilename = out->currentFilename;
MutexSafeWrapper safeMutex(out->ftpServer->getProgressMutex(),string(__FILE__) + "_" + intToStr(__LINE__));
out->ftpServer->getCallBackObject()->FTPClient_CallbackEvent(out->itemName, ftp_cct_DownloadProgress, ftp_crt_SUCCESS, &stats);
out->ftpServer->getCallBackObject()->FTPClient_CallbackEvent(out->itemName, ftp_cct_DownloadProgress, make_pair(ftp_crt_SUCCESS,""), &stats);
}
return 0;
@@ -236,18 +236,20 @@ bool FTPClientThread::shutdownAndWait() {
}
FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, string ftpUser, string ftpUserPassword) {
FTP_Client_ResultType result = ftp_crt_FAIL;
pair<FTP_Client_ResultType,string> FTPClientThread::getMapFromServer(pair<string,string> mapFileName, string ftpUser, string ftpUserPassword) {
pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
string destFileExt = "";
string destFile = this->mapsPath.second;
endPathWithSlash(destFile);
destFile += mapFileName;
destFile += mapFileName.first;
if(EndsWith(destFile,".mgm") == false && EndsWith(destFile,".gbm") == false) {
destFileExt = ".mgm";
destFile += destFileExt;
if(mapFileName.second == "") {
if(EndsWith(destFile,".mgm") == false && EndsWith(destFile,".gbm") == false) {
destFileExt = ".mgm";
destFile += destFileExt;
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("===> FTP Client thread about to try to RETR into [%s]\n",destFile.c_str());
@@ -270,7 +272,13 @@ FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, stri
ftpfile.stream = NULL;
char szBuf[1024]="";
sprintf(szBuf,"ftp://%s:%s@%s:%d/%s%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,mapFileName.c_str(),destFileExt.c_str());
if(mapFileName.second != "") {
sprintf(szBuf,"%s",mapFileName.second.c_str());
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
}
else {
sprintf(szBuf,"ftp://%s:%s@%s:%d/%s%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,mapFileName.first.c_str(),destFileExt.c_str());
}
curl_easy_setopt(curl, CURLOPT_URL,szBuf);
curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
@@ -280,6 +288,7 @@ FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, stri
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
// Max 10 minutes to transfer
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 600);
@@ -288,16 +297,17 @@ FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, stri
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
result.second = curl_easy_strerror(res);
// we failed
printf("curl FAILED with: %d [%s] szBuf [%s]\n", res,curl_easy_strerror(res),szBuf);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"curl FAILED with: %d [%s] szBuf [%s]\n", res,curl_easy_strerror(res),szBuf);
if(res == CURLE_PARTIAL_FILE) {
result = ftp_crt_PARTIALFAIL;
result.first = ftp_crt_PARTIALFAIL;
}
}
else {
result = ftp_crt_SUCCESS;
result.first = ftp_crt_SUCCESS;
}
SystemFlags::cleanupHTTP(&curl);
@@ -307,76 +317,100 @@ FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, stri
fclose(ftpfile.stream);
ftpfile.stream = NULL;
}
if(result != ftp_crt_SUCCESS) {
if(result.first != ftp_crt_SUCCESS) {
removeFile(destFile);
}
return result;
}
void FTPClientThread::getMapFromServer(string mapFileName) {
FTP_Client_ResultType result = getMapFromServer(mapFileName + ".mgm", FTP_MAPS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
if(result == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getMapFromServer(mapFileName + ".gbm", FTP_MAPS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
if(result == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getMapFromServer(mapFileName + ".mgm", FTP_MAPS_USERNAME, FTP_COMMON_PASSWORD);
if(result == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getMapFromServer(mapFileName + ".gbm", FTP_MAPS_USERNAME, FTP_COMMON_PASSWORD);
}
}
}
void FTPClientThread::getMapFromServer(pair<string,string> mapFileName) {
pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
if(mapFileName.second != "") {
result = getMapFromServer(mapFileName, "", "");
}
else {
pair<string,string> findMapFileName = mapFileName;
findMapFileName.first += + ".mgm";
result = getMapFromServer(findMapFileName, FTP_MAPS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
findMapFileName = mapFileName;
findMapFileName.first += + ".gbm";
result = getMapFromServer(findMapFileName, FTP_MAPS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
findMapFileName = mapFileName;
findMapFileName.first += + ".mgm";
result = getMapFromServer(findMapFileName, FTP_MAPS_USERNAME, FTP_COMMON_PASSWORD);
if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
findMapFileName = mapFileName;
findMapFileName.first += + ".gbm";
result = getMapFromServer(findMapFileName, FTP_MAPS_USERNAME, FTP_COMMON_PASSWORD);
}
}
}
}
MutexSafeWrapper safeMutex(this->getProgressMutex(),string(__FILE__) + "_" + intToStr(__LINE__));
if(this->pCBObject != NULL) {
this->pCBObject->FTPClient_CallbackEvent(mapFileName,ftp_cct_Map,result,NULL);
this->pCBObject->FTPClient_CallbackEvent(mapFileName.first,ftp_cct_Map,result,NULL);
}
}
void FTPClientThread::addMapToRequests(string mapFilename) {
void FTPClientThread::addMapToRequests(string mapFilename,string URL) {
std::pair<string,string> item = make_pair(mapFilename,URL);
MutexSafeWrapper safeMutex(&mutexMapFileList,string(__FILE__) + "_" + intToStr(__LINE__));
if(std::find(mapFileList.begin(),mapFileList.end(),mapFilename) == mapFileList.end()) {
mapFileList.push_back(mapFilename);
if(std::find(mapFileList.begin(),mapFileList.end(),item) == mapFileList.end()) {
mapFileList.push_back(item);
}
}
void FTPClientThread::addTilesetToRequests(string tileSetName) {
void FTPClientThread::addTilesetToRequests(string tileSetName,string URL) {
std::pair<string,string> item = make_pair(tileSetName,URL);
MutexSafeWrapper safeMutex(&mutexTilesetList,string(__FILE__) + "_" + intToStr(__LINE__));
if(std::find(tilesetList.begin(),tilesetList.end(),tileSetName) == tilesetList.end()) {
tilesetList.push_back(tileSetName);
if(std::find(tilesetList.begin(),tilesetList.end(),item) == tilesetList.end()) {
tilesetList.push_back(item);
}
}
void FTPClientThread::addTechtreeToRequests(string techtreeName) {
void FTPClientThread::addTechtreeToRequests(string techtreeName,string URL) {
std::pair<string,string> item = make_pair(techtreeName,URL);
MutexSafeWrapper safeMutex(&mutexTechtreeList,string(__FILE__) + "_" + intToStr(__LINE__));
if(std::find(techtreeList.begin(),techtreeList.end(),techtreeName) == techtreeList.end()) {
techtreeList.push_back(techtreeName);
if(std::find(techtreeList.begin(),techtreeList.end(),item) == techtreeList.end()) {
techtreeList.push_back(item);
}
}
void FTPClientThread::getTilesetFromServer(string tileSetName) {
void FTPClientThread::getTilesetFromServer(pair<string,string> tileSetName) {
bool findArchive = executeShellCommand(this->fileArchiveExtractCommand);
FTP_Client_ResultType result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, findArchive);
if(result == ftp_crt_FAIL && this->getQuitStatus() == false) {
if(findArchive == true) {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, false);
}
if(result == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, findArchive);
pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
if(tileSetName.second != "") {
result = getTilesetFromServer(tileSetName, "", "", "", findArchive);
}
else {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, findArchive);
if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
if(findArchive == true) {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, false);
}
if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, findArchive);
if(findArchive == true) {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, false);
}
}
}
if(findArchive == true) {
result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, false);
}
}
}
}
MutexSafeWrapper safeMutex(this->getProgressMutex(),string(__FILE__) + "_" + intToStr(__LINE__));
if(this->pCBObject != NULL) {
this->pCBObject->FTPClient_CallbackEvent(tileSetName,ftp_cct_Tileset,result,NULL);
this->pCBObject->FTPClient_CallbackEvent(tileSetName.first,ftp_cct_Tileset,result,NULL);
}
}
FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
pair<FTP_Client_ResultType,string> FTPClientThread::getTilesetFromServer(pair<string,string> tileSetName,
string tileSetNameSubfolder, string ftpUser, string ftpUserPassword,
bool findArchive) {
@@ -550,18 +584,23 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
if(findArchive == true) {
destFileSaveAs = this->tilesetsPath.second;
endPathWithSlash(destFileSaveAs);
destFileSaveAs += tileSetName + this->fileArchiveExtension;
destFileSaveAs += tileSetName.first + this->fileArchiveExtension;
remotePath = tileSetName + this->fileArchiveExtension;
if(tileSetName.second != "") {
remotePath = tileSetName.second;
}
else {
remotePath = tileSetName.first + this->fileArchiveExtension;
}
//sprintf(szBuf,"ftp://%s:%s@%s:%d/%s%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,
// tileSetName.c_str(),this->fileArchiveExtension.c_str());
}
else {
getFolderContents = true;
remotePath = tileSetName + "/";
remotePath = tileSetName.first + "/";
destFileSaveAs = this->tilesetsPath.second;
endPathWithSlash(destFileSaveAs);
destFileSaveAs += tileSetName;
destFileSaveAs += tileSetName.first;
destFileSaveAsNewFile = destFileSaveAs;
endPathWithSlash(destFileSaveAsNewFile);
destFileSaveAs += ".tmp";
@@ -571,10 +610,10 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
}
else {
getFolderContents = true;
remotePath = tileSetName + "/" + tileSetNameSubfolder + "/";
remotePath = tileSetName.first + "/" + tileSetNameSubfolder + "/";
destFileSaveAs = this->tilesetsPath.second;
endPathWithSlash(destFileSaveAs);
destFileSaveAs += tileSetName;
destFileSaveAs += tileSetName.first;
endPathWithSlash(destFileSaveAs);
destFileSaveAs += tileSetNameSubfolder;
@@ -591,23 +630,24 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
pWantDirListOnly = &wantDirListOnly;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("FTPClientThread::getTilesetFromServer [%s] remotePath [%s] destFileSaveAs [%s] getFolderContents = %d\n",tileSetName.c_str(),remotePath.c_str(),destFileSaveAs.c_str(),getFolderContents);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("FTPClientThread::getTilesetFromServer [%s] remotePath [%s] destFileSaveAs [%s] getFolderContents = %d\n",tileSetName.first.c_str(),remotePath.c_str(),destFileSaveAs.c_str(),getFolderContents);
FTP_Client_ResultType result = getFileFromServer(tileSetName,
pair<FTP_Client_ResultType,string> result = getFileFromServer(tileSetName,
remotePath, destFileSaveAs,ftpUser, ftpUserPassword, pWantDirListOnly);
// Extract the archive
if(result == ftp_crt_SUCCESS) {
if(result.first == ftp_crt_SUCCESS) {
if(findArchive == true) {
string destRootArchiveFolder = this->tilesetsPath.second;
endPathWithSlash(destRootArchiveFolder);
string extractCmd = getFullFileArchiveExtractCommand(this->fileArchiveExtractCommand,
this->fileArchiveExtractCommandParameters, destRootArchiveFolder,
destRootArchiveFolder + tileSetName + this->fileArchiveExtension);
destRootArchiveFolder + tileSetName.first + this->fileArchiveExtension);
if(executeShellCommand(extractCmd) == false) {
result = ftp_crt_FAIL;
result.first = ftp_crt_FAIL;
result.second = "failed to extract arhcive!";
}
return result;
@@ -627,7 +667,7 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
remotePath + fileFromList,
destFileSaveAsNewFile + fileFromList,
ftpUser, ftpUserPassword);
if(result != ftp_crt_SUCCESS) {
if(result.first != ftp_crt_SUCCESS) {
break;
}
}
@@ -635,7 +675,7 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
result = getTilesetFromServer(tileSetName,
fileFromList, ftpUser, ftpUserPassword,
findArchive);
if(result != ftp_crt_SUCCESS) {
if(result.first != ftp_crt_SUCCESS) {
break;
}
}
@@ -644,10 +684,10 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
}
}
if(result != ftp_crt_SUCCESS && findArchive == false) {
if(result.first != ftp_crt_SUCCESS && findArchive == false) {
string destRootFolder = this->tilesetsPath.second;
endPathWithSlash(destRootFolder);
destRootFolder += tileSetName;
destRootFolder += tileSetName.first;
endPathWithSlash(destRootFolder);
removeFolder(destRootFolder);
@@ -656,23 +696,28 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName,
return result;
}
void FTPClientThread::getTechtreeFromServer(string techtreeName) {
FTP_Client_ResultType result = ftp_crt_FAIL;
void FTPClientThread::getTechtreeFromServer(pair<string,string> techtreeName) {
pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
bool findArchive = executeShellCommand(this->fileArchiveExtractCommand);
if(findArchive == true) {
result = getTechtreeFromServer(techtreeName, FTP_TECHTREES_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
if(result == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getTechtreeFromServer(techtreeName, FTP_TECHTREES_USERNAME, FTP_COMMON_PASSWORD);
if(techtreeName.second != "") {
result = getTechtreeFromServer(techtreeName, "", "");
}
else {
result = getTechtreeFromServer(techtreeName, FTP_TECHTREES_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
result = getTechtreeFromServer(techtreeName, FTP_TECHTREES_USERNAME, FTP_COMMON_PASSWORD);
}
}
}
MutexSafeWrapper safeMutex(this->getProgressMutex(),string(__FILE__) + "_" + intToStr(__LINE__));
if(this->pCBObject != NULL) {
this->pCBObject->FTPClient_CallbackEvent(techtreeName,ftp_cct_Techtree,result,NULL);
this->pCBObject->FTPClient_CallbackEvent(techtreeName.first,ftp_cct_Techtree,result,NULL);
}
}
FTP_Client_ResultType FTPClientThread::getTechtreeFromServer(string techtreeName,
pair<FTP_Client_ResultType,string> FTPClientThread::getTechtreeFromServer(pair<string,string> techtreeName,
string ftpUser, string ftpUserPassword) {
/*
@@ -805,27 +850,32 @@ FTP_Client_ResultType FTPClientThread::getTechtreeFromServer(string techtreeName
string destRootFolder = this->techtreesPath.second;
endPathWithSlash(destRootFolder);
string destRootArchiveFolder = destRootFolder;
destRootFolder += techtreeName;
destRootFolder += techtreeName.first;
endPathWithSlash(destRootFolder);
string destFile = this->techtreesPath.second;
endPathWithSlash(destFile);
destFile += techtreeName;
destFile += techtreeName.first;
string destFileSaveAs = destFile + this->fileArchiveExtension;
endPathWithSlash(destFile);
string remotePath = techtreeName + this->fileArchiveExtension;
FTP_Client_ResultType result = getFileFromServer(techtreeName,
string remotePath = techtreeName.first + this->fileArchiveExtension;
if(techtreeName.second != "") {
remotePath = techtreeName.second;
}
pair<FTP_Client_ResultType,string> result = getFileFromServer(techtreeName,
remotePath, destFileSaveAs, ftpUser, ftpUserPassword);
// Extract the archive
if(result == ftp_crt_SUCCESS) {
if(result.first == ftp_crt_SUCCESS) {
string extractCmd = getFullFileArchiveExtractCommand(this->fileArchiveExtractCommand,
this->fileArchiveExtractCommandParameters, destRootArchiveFolder,
destRootArchiveFolder + techtreeName + this->fileArchiveExtension);
destRootArchiveFolder + techtreeName.first + this->fileArchiveExtension);
if(executeShellCommand(extractCmd) == false) {
result = ftp_crt_FAIL;
result.first = ftp_crt_FAIL;
result.second = "failed to extract archive!";
}
}
@@ -834,10 +884,10 @@ FTP_Client_ResultType FTPClientThread::getTechtreeFromServer(string techtreeName
}
FTP_Client_ResultType FTPClientThread::getFileFromServer(string fileNameTitle,
pair<FTP_Client_ResultType,string> FTPClientThread::getFileFromServer(pair<string,string> fileNameTitle,
string remotePath, string destFileSaveAs,
string ftpUser, string ftpUserPassword, vector <string> *wantDirListOnly) {
FTP_Client_ResultType result = ftp_crt_FAIL;
pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
if(wantDirListOnly) {
(*wantDirListOnly).clear();
}
@@ -854,7 +904,7 @@ FTP_Client_ResultType FTPClientThread::getFileFromServer(string fileNameTitle,
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread about to try to RETR into [%s] wantDirList = %d\n",destFileSaveAs.c_str(),wantDirList);
struct FtpFile ftpfile = {
fileNameTitle.c_str(),
fileNameTitle.first.c_str(),
destFileSaveAs.c_str(), // name to store the file as if successful
NULL,
NULL,
@@ -868,7 +918,13 @@ FTP_Client_ResultType FTPClientThread::getFileFromServer(string fileNameTitle,
ftpfile.stream = NULL;
char szBuf[1024]="";
sprintf(szBuf,"ftp://%s:%s@%s:%d/%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,remotePath.c_str());
if(fileNameTitle.second != "") {
sprintf(szBuf,"%s",fileNameTitle.second.c_str());
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
}
else {
sprintf(szBuf,"ftp://%s:%s@%s:%d/%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,remotePath.c_str());
}
curl_easy_setopt(curl, CURLOPT_URL,szBuf);
curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
@@ -905,12 +961,13 @@ FTP_Client_ResultType FTPClientThread::getFileFromServer(string fileNameTitle,
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
result.second = curl_easy_strerror(res);
// we failed
printf("curl FAILED with: %d [%s] attempting to remove folder contents [%s] szBuf [%s] ftpfile.isValidXfer = %d, pathCreated = %d\n", res,curl_easy_strerror(res),destRootFolder.c_str(),szBuf,ftpfile.isValidXfer,pathCreated);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"curl FAILED with: %d [%s] attempting to remove folder contents [%s] szBuf [%s] ftpfile.isValidXfer = %d, pathCreated = %d\n", res,curl_easy_strerror(res),destRootFolder.c_str(),szBuf,ftpfile.isValidXfer,pathCreated);
if(res == CURLE_PARTIAL_FILE || ftpfile.isValidXfer == true) {
result = ftp_crt_PARTIALFAIL;
result.first = ftp_crt_PARTIALFAIL;
}
if(destRootFolder != "") {
@@ -923,7 +980,7 @@ FTP_Client_ResultType FTPClientThread::getFileFromServer(string fileNameTitle,
}
}
else {
result = ftp_crt_SUCCESS;
result.first = ftp_crt_SUCCESS;
if(wantDirListOnly) {
if(ftpfile.stream) {
@@ -987,7 +1044,7 @@ void FTPClientThread::execute() {
while(this->getQuitStatus() == false) {
MutexSafeWrapper safeMutex(&mutexMapFileList,string(__FILE__) + "_" + intToStr(__LINE__));
if(mapFileList.size() > 0) {
string mapFilename = mapFileList[0];
pair<string,string> mapFilename = mapFileList[0];
mapFileList.erase(mapFileList.begin() + 0);
safeMutex.ReleaseLock();
@@ -1003,7 +1060,7 @@ void FTPClientThread::execute() {
MutexSafeWrapper safeMutex2(&mutexTilesetList,string(__FILE__) + "_" + intToStr(__LINE__));
if(tilesetList.size() > 0) {
string tileset = tilesetList[0];
pair<string,string> tileset = tilesetList[0];
tilesetList.erase(tilesetList.begin() + 0);
safeMutex2.ReleaseLock();
@@ -1015,7 +1072,7 @@ void FTPClientThread::execute() {
MutexSafeWrapper safeMutex3(&mutexTechtreeList,string(__FILE__) + "_" + intToStr(__LINE__));
if(techtreeList.size() > 0) {
string techtree = techtreeList[0];
pair<string,string> techtree = techtreeList[0];
techtreeList.erase(techtreeList.begin() + 0);
safeMutex3.ReleaseLock();