Advertisement
Guest User

Untitled

a guest
May 20th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. lists_mutex.lock();
  2. QNetworkReply *repl = qobject_cast<QNetworkReply*>(sender());
  3. if(repl->error() == QNetworkReply::NoError)
  4. {
  5. if(sess)
  6. {
  7. #ifdef DEBUG
  8. try{
  9. #endif
  10. remove_torrents(sess);
  11. #ifdef DEBUG
  12. }
  13. catch(...)
  14. {
  15. cfg->log()<<"libtorrent exception: ";
  16. }
  17. #endif
  18. }
  19. QByteArray data = repl->readAll();
  20. data = lzUncompress(&data);
  21. if(data.size())
  22. {
  23. #ifdef DEBUG
  24. cfg->log()<<QString("torrent data recieved, loading");
  25. #endif
  26. try{
  27. libtorrent::torrent_info *ti = new libtorrent::torrent_info(data.data(), data.size());
  28. std::vector<int> fp(ti->files().num_files(), 0);
  29. {
  30. int ni =0;
  31. for(libtorrent::torrent_info::file_iterator i = ti->begin_files(), end = ti->end_files(); i != end; ++i, ++ni)
  32. {
  33. {
  34. QString file = QString::fromStdString(ti->files().at(i).path);
  35. file.remove(ti->name().c_str());
  36. file.replace("\\", "/");
  37. file = file.toLower();
  38. QString filepath = file;
  39. if(file.indexOf("/l10n/") != -1)
  40. {
  41. std::string hz = file.toStdString();
  42. std::string::size_type p1 = 0, p2 = 0;
  43. p1 = hz.find("/l10n/");
  44. p1+= strlen("/l10n/");
  45. p2 = hz.find("/", p1+1);
  46. if(QString(hz.substr(p1, p2-p1).c_str()) != cfg->current_lang())
  47. {
  48. std::string rand = get_random(10);
  49. ti->rename_file(ni, rand);
  50. #ifdef DEBUG
  51. cfg->log()<<QString("file ")+ ti->files().at(i).path.c_str() + " skipped, not our langpack";
  52. #endif
  53. continue;
  54. }
  55. filepath.remove(p1, p2-p1);
  56. }
  57. std::list<s_list_torrent_entry>::iterator ipt = std::find(updated_g_s_list_torrent.begin(), updated_g_s_list_torrent.end(), file.toUtf8().data());
  58. std::list<s_list_entry>::iterator iph = std::find(updated_g_s_list_http.begin(), updated_g_s_list_http.end(), file.toUtf8().data());
  59. std::list<f_list_entry>::iterator ipf = std::find(updated_g_f_list.begin(), updated_g_f_list.end(), s_i(file.toUtf8().data(), 4));
  60. std::list<s_list_cmp_entry>::iterator ipc = std::find(torrent_cmp_list.begin(), torrent_cmp_list.end(), file.toUtf8().data());
  61. std::list<s_list_entry>::iterator ipm = std::find(modyfied_list.begin(), modyfied_list.end(), file.toUtf8().toLower().data());
  62. if(QFile(cfg->game_path() + filepath).exists() && (ipt == updated_g_s_list_torrent.end()) && (iph == updated_g_s_list_http.end()) && (ipf == updated_g_f_list.end()) && (ipm == modyfied_list.end()))
  63. {
  64. QString file2 = filepath;
  65. file2.insert(0, "..");
  66. ti->rename_file(ni, file2.toStdString());
  67. #ifdef DEBUG
  68. cfg->log()<<QString("we already have ")+ ti->files().at(i).path.c_str() + " renamed to match local file";
  69. #endif
  70. // fp[ni] = 1; //neded to get missed chunks ?
  71. continue;
  72. }
  73. else if(ipt != updated_g_s_list_torrent.end())
  74. {
  75. fp[ni] = 1;
  76. if(ipc != torrent_cmp_list.end())
  77. {
  78. char num[10];
  79. snprintf(num, 9, "%d", ipc->id);
  80. ti->rename_file(ni, num);
  81. #ifdef DEBUG
  82. cfg->log()<<QString("renaming ")+ ti->files().at(i).path.c_str() + " to " + (const char*)num + " (name mapping found in cmp list)";
  83. #endif
  84. }
  85. else
  86. {
  87. int id = (!torrent_cmp_list.empty())?torrent_cmp_list.back().id + 1:0;
  88. char num[10];
  89. snprintf(num, 9, "%d", id);
  90. if(ipt != updated_g_s_list_torrent.end())
  91. {
  92. #ifdef DEBUG
  93. cfg->log()<<QString("adding ")+ ti->files().at(i).path.c_str() + " to torrent_cmp list with name " + (const char*)num + " (also found in updated_g_s_list_torrent)";
  94. assert(id < 4000);
  95. #endif
  96. torrent_cmp_list.push_back(s_list_cmp_entry(*ipt, id));
  97. }
  98. else
  99. {
  100. #ifdef DEBUG
  101. cfg->log()<<QString("adding ")+ ti->files().at(i).path.c_str() + " to torrent_cmp list with name " + (const char*)num + "(not in updated_g_s_list_torrent currently)";
  102. assert(id < 4000);
  103. #endif
  104. if(iph != updated_g_s_list_http.end())
  105. torrent_cmp_list.push_back(s_list_cmp_entry(*iph, id));
  106. else
  107. torrent_cmp_list.push_back(s_list_cmp_entry(file.toUtf8().data(), id));
  108. }
  109. ti->rename_file(ni, num);
  110. #ifdef DEBUG
  111. cfg->log()<<QString("renaming ")+ti->files().at(i).path.c_str() + " to " + (const char*)num + " (new mapping)";
  112. #endif
  113. }
  114. #ifdef DEBUG
  115. cfg->log()<<QString(ti->files().at(i).path.c_str()) + " file in list for downloading via torrent, handling";
  116. #endif
  117. }
  118. else
  119. {
  120. std::string m = get_random(10);
  121. ti->rename_file(ni, m);
  122. if(iph != updated_g_s_list_http.end())
  123. {
  124. torrent_cmp_tmp_list.push_back(s_list_cmp_s_entry(*iph, m));
  125. torrent_cmp_list.remove(*ipc);
  126. }
  127. #ifdef DEBUG
  128. cfg->log()<<QString(ti->files().at(i).path.c_str()) + " file not in torrent file list";
  129. #endif
  130. }
  131. }
  132. }
  133. }
  134. remove_torrents(sess);
  135. libtorrent::add_torrent_params params;
  136. params.ti = ti;
  137. params.auto_managed = false;
  138. if(!QFile(cfg->game_path() + "/torrent_upd").exists())
  139. QDir().mkpath(cfg->game_path() + "/torrent_upd");
  140. params.save_path = QString(cfg->game_path() + "/torrent_upd").toUtf8().data();
  141. params.storage_mode = libtorrent::storage_mode_sparse;
  142. if(QFile(this_app->applicationDirPath() + "/config/torrent_resume").exists())
  143. {
  144. QFile in(this_app->applicationDirPath() + "/config/torrent_resume");
  145. in.open(QIODevice::ReadOnly);
  146. QByteArray arr = in.readAll();
  147. in.close();
  148. arr = lzUncompress(&arr);
  149. std::vector<char> buf;
  150. for(int i = 0; i < arr.size(); ++i)
  151. buf.push_back(arr[i]);
  152. arr.clear();
  153. params.flags |= libtorrent::add_torrent_params::flag_override_resume_data;
  154. params.resume_data = &buf;
  155. torrent_h = sess->add_torrent(params);
  156. }
  157. else
  158. torrent_h = sess->add_torrent(params);
  159. emit assign_torrent_h(torrent_h);
  160. torrent_h.prioritize_files(fp);
  161. torrent_h.set_sequential_download(true);
  162. if(torrent_h.status().paused)
  163. torrent_h.resume();
  164. torrent_h.force_reannounce();
  165. if(cfg->torrent_dht())
  166. torrent_h.force_dht_announce();
  167. }
  168. catch(const libtorrent::libtorrent_exception &e)
  169. {
  170. #ifdef DEBUG
  171. cfg->log()<<QString("we have following problem with torrent: ")+e.what();
  172. #endif
  173. }
  174. if(t_m_t_obj)
  175. {
  176. QMetaObject::invokeMethod(t_m_t_obj, "exit_cleanup", Qt::QueuedConnection);
  177. t_m_t_obj = NULL;
  178. }
  179. #ifndef DISABLE_THREADS
  180. t_m_t_obj = new torrent_monitor_thread(torrent_h, updated_size, &updated_g_s_list_http, &updated_g_s_list_torrent,
  181. &updated_g_f_list, &torrent_cmp_list, &torrent_cmp_tmp_list, &modyfied_list, cfg, torrent_monitor_running, lists_mutex);
  182. t_m_t_obj->moveToThread(&monitor_thr);
  183. connect(t_m_t_obj, SIGNAL(set_progress2(int)), this, SIGNAL(set_progress2(int)));
  184. qRegisterMetaType<libtorrent::torrent_handle>("libtorrent::torrent_handle");
  185. connect(this, SIGNAL(assign_torrent_h(libtorrent::torrent_handle)), t_m_t_obj, SLOT(assign_torrent_h(libtorrent::torrent_handle)));
  186. connect(t_m_t_obj, SIGNAL(start_post_upd()), this, SLOT(start_post_upd()));
  187. connect(t_m_t_obj, SIGNAL(req_new_t_h()), this, SLOT(req_new_t_h()));
  188. connect(t_m_t_obj, SIGNAL(download_finished()), this, SIGNAL(download_finished()));
  189. connect(t_m_t_obj, SIGNAL(change_upd_current_state_lbl(QString)), this, SIGNAL(change_upd_current_state_lbl(QString)));
  190. connect(t_m_t_obj, SIGNAL(get_next_file()), this, SLOT(get_next_file()));
  191. connect(t_m_t_obj, SIGNAL(ready_for_shutdown()), this, SIGNAL(torrent_monitor_thr_r_f_s()));
  192. connect(this, SIGNAL(set_is_f_file(bool)), t_m_t_obj, SLOT(set_is_f_file(bool)));
  193. connect(this, SIGNAL(set_dl_size_cmp(quint64)), t_m_t_obj, SLOT(set_dl_size_cmp(quint64)));
  194. connect(this, SIGNAL(set_have_http_files(bool)), t_m_t_obj, SLOT(set_have_http_files(bool)));
  195. connect(this, SIGNAL(exit_cleanup()), t_m_t_obj, SLOT(exit_cleanup()));
  196. #endif
  197. if(cfg->upd_methd() == 1)
  198. emit set_show_upd_current_lbl(true);
  199. }
  200. else if(torrent_h.is_valid())
  201. {
  202. torrent_h.resume();
  203. torrent_h.force_reannounce();
  204. if(cfg->torrent_dht())
  205. torrent_h.force_dht_announce();
  206. emit assign_torrent_h(torrent_h);
  207. }
  208. }
  209. else
  210. {
  211. QMessageBox msg;
  212. msg.setWindowTitle(" ");
  213. msg.setText(tr("Failed to get metadata from server\nRetry ?"));
  214. msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  215. if(msg.exec() == QMessageBox::Yes)
  216. {
  217. QNetworkRequest req;
  218. req.setRawHeader("User-Agent", update_ua_header.toUtf8());
  219. req.setUrl(QUrl(torrent_link));
  220. QNetworkReply *r = qnm.get(req);
  221. connect(r, SIGNAL(finished()), SLOT(torrent_request_finished()));
  222. }
  223. else if(cfg->is_game_exists())
  224. {
  225. if(cfg->torrent_seed())
  226. torrent_seed();
  227. emit set_enabled_login_field(true);
  228. emit set_enabled_password_field(true);
  229. emit set_enabled_login(true);
  230. }
  231. }
  232. lists_mutex.unlock();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement