DownloadManager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // FILE: DownloadManager.cpp //////////////////////////////////////////////////////
  19. // Generals download manager code
  20. // Author: Matthew D. Campbell, July 2002
  21. #include "debug.h"
  22. #include "chatapi.h"
  23. #include "DownloadManager.h"
  24. #include "resource.h"
  25. DownloadManager *TheDownloadManager = NULL;
  26. DownloadManager::DownloadManager()
  27. {
  28. m_download = new CDownload(this);
  29. m_wasError = m_sawEnd = false;
  30. m_statusString = Fetch_String(FTP_StatusIdle);
  31. // ----- Initialize Winsock -----
  32. m_winsockInit = true;
  33. WORD verReq = MAKEWORD(2, 2);
  34. WSADATA wsadata;
  35. int err = WSAStartup(verReq, &wsadata);
  36. if (err != 0)
  37. {
  38. m_winsockInit = false;
  39. }
  40. else
  41. {
  42. if ((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) !=2))
  43. {
  44. WSACleanup();
  45. m_winsockInit = false;
  46. }
  47. }
  48. }
  49. DownloadManager::~DownloadManager()
  50. {
  51. delete m_download;
  52. if (m_winsockInit)
  53. {
  54. WSACleanup();
  55. m_winsockInit = false;
  56. }
  57. }
  58. void DownloadManager::init( void )
  59. {
  60. }
  61. void DownloadManager::reset( void )
  62. {
  63. }
  64. HRESULT DownloadManager::update( void )
  65. {
  66. return m_download->PumpMessages();
  67. }
  68. HRESULT DownloadManager::downloadFile( std::string server, std::string username, std::string password, std::string file, std::string localfile, std::string regkey, bool tryResume )
  69. {
  70. return m_download->DownloadFile( server.c_str(), username.c_str(), password.c_str(), file.c_str(), localfile.c_str(), regkey.c_str(), tryResume );
  71. }
  72. void DownloadManager::queueFileForDownload( std::string server, std::string username, std::string password, std::string file, std::string localfile, std::string regkey, bool tryResume )
  73. {
  74. QueuedDownload q;
  75. q.file = file;
  76. q.localFile = localfile;
  77. q.password = password;
  78. q.regKey = regkey;
  79. q.server = server;
  80. q.tryResume = tryResume;
  81. q.userName = username;
  82. m_queuedDownloads.push_back(q);
  83. }
  84. HRESULT DownloadManager::downloadNextQueuedFile( void )
  85. {
  86. QueuedDownload q;
  87. std::list<QueuedDownload>::iterator it = m_queuedDownloads.begin();
  88. if (it != m_queuedDownloads.end())
  89. {
  90. q = *it;
  91. m_queuedDownloads.pop_front();
  92. m_wasError = m_sawEnd = false;
  93. return downloadFile( q.server, q.userName, q.password, q.file, q.localFile, q.regKey, q.tryResume );
  94. }
  95. else
  96. {
  97. DEBUG_CRASH(("Starting non-existent download!"));
  98. return S_OK;
  99. }
  100. }
  101. std::string DownloadManager::getLastLocalFile( void )
  102. {
  103. char buf[256] = "";
  104. m_download->GetLastLocalFile(buf, 256);
  105. return buf;
  106. }
  107. HRESULT DownloadManager::OnError( int error )
  108. {
  109. m_wasError = true;
  110. std::string s = Fetch_String(FTP_UnknownError);
  111. switch (error)
  112. {
  113. case DOWNLOADEVENT_NOSUCHSERVER:
  114. s = Fetch_String(FTP_NoSuchServer);
  115. break;
  116. case DOWNLOADEVENT_COULDNOTCONNECT:
  117. s = Fetch_String(FTP_CouldNotConnect);
  118. break;
  119. case DOWNLOADEVENT_LOGINFAILED:
  120. s = Fetch_String(FTP_LoginFailed);
  121. break;
  122. case DOWNLOADEVENT_NOSUCHFILE:
  123. s = Fetch_String(FTP_NoSuchFile);
  124. break;
  125. case DOWNLOADEVENT_LOCALFILEOPENFAILED:
  126. s = Fetch_String(FTP_LocalFileOpenFailed);
  127. break;
  128. case DOWNLOADEVENT_TCPERROR:
  129. s = Fetch_String(FTP_TCPError);
  130. break;
  131. case DOWNLOADEVENT_DISCONNECTERROR:
  132. s = Fetch_String(FTP_DisconnectError);
  133. break;
  134. }
  135. m_errorString = s;
  136. DEBUG_LOG(("DownloadManager::OnError(): %s(%d)\n", s.c_str(), error));
  137. return S_OK;
  138. }
  139. HRESULT DownloadManager::OnEnd()
  140. {
  141. m_sawEnd = true;
  142. DEBUG_LOG(("DownloadManager::OnEnd()\n"));
  143. return S_OK;
  144. }
  145. HRESULT DownloadManager::OnQueryResume()
  146. {
  147. DEBUG_LOG(("DownloadManager::OnQueryResume()\n"));
  148. //return DOWNLOADEVENT_DONOTRESUME;
  149. return DOWNLOADEVENT_RESUME;
  150. }
  151. HRESULT DownloadManager::OnProgressUpdate( int bytesread, int totalsize, int timetaken, int timeleft )
  152. {
  153. DEBUG_LOG(("DownloadManager::OnProgressUpdate(): %d/%d %d/%d\n", bytesread, totalsize, timetaken, timeleft));
  154. return S_OK;
  155. }
  156. HRESULT DownloadManager::OnStatusUpdate( int status )
  157. {
  158. std::string s = Fetch_String(FTP_StatusNone);
  159. switch (status)
  160. {
  161. case DOWNLOADSTATUS_CONNECTING:
  162. s = Fetch_String(FTP_StatusConnecting);
  163. break;
  164. case DOWNLOADSTATUS_LOGGINGIN:
  165. s = Fetch_String(FTP_StatusLoggingIn);
  166. break;
  167. case DOWNLOADSTATUS_FINDINGFILE:
  168. s = Fetch_String(FTP_StatusFindingFile);
  169. break;
  170. case DOWNLOADSTATUS_QUERYINGRESUME:
  171. s = Fetch_String(FTP_StatusQueryingResume);
  172. break;
  173. case DOWNLOADSTATUS_DOWNLOADING:
  174. s = Fetch_String(FTP_StatusDownloading);
  175. break;
  176. case DOWNLOADSTATUS_DISCONNECTING:
  177. s = Fetch_String(FTP_StatusDisconnecting);
  178. break;
  179. case DOWNLOADSTATUS_FINISHING:
  180. s = Fetch_String(FTP_StatusFinishing);
  181. break;
  182. case DOWNLOADSTATUS_DONE:
  183. s = Fetch_String(FTP_StatusDone);
  184. break;
  185. }
  186. m_statusString = s;
  187. DEBUG_LOG(("DownloadManager::OnStatusUpdate(): %s(%d)\n", s.c_str(), status));
  188. return S_OK;
  189. }