Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lists_mutex.lock();
- QNetworkReply *repl = qobject_cast<QNetworkReply*>(sender());
- if(repl->error() == QNetworkReply::NoError)
- {
- if(sess)
- {
- #ifdef DEBUG
- try{
- #endif
- remove_torrents(sess);
- #ifdef DEBUG
- }
- catch(...)
- {
- cfg->log()<<"libtorrent exception: ";
- }
- #endif
- }
- QByteArray data = repl->readAll();
- data = lzUncompress(&data);
- if(data.size())
- {
- #ifdef DEBUG
- cfg->log()<<QString("torrent data recieved, loading");
- #endif
- try{
- libtorrent::torrent_info *ti = new libtorrent::torrent_info(data.data(), data.size());
- std::vector<int> fp(ti->files().num_files(), 0);
- {
- int ni =0;
- for(libtorrent::torrent_info::file_iterator i = ti->begin_files(), end = ti->end_files(); i != end; ++i, ++ni)
- {
- {
- QString file = QString::fromStdString(ti->files().at(i).path);
- file.remove(ti->name().c_str());
- file.replace("\\", "/");
- file = file.toLower();
- QString filepath = file;
- if(file.indexOf("/l10n/") != -1)
- {
- std::string hz = file.toStdString();
- std::string::size_type p1 = 0, p2 = 0;
- p1 = hz.find("/l10n/");
- p1+= strlen("/l10n/");
- p2 = hz.find("/", p1+1);
- if(QString(hz.substr(p1, p2-p1).c_str()) != cfg->current_lang())
- {
- std::string rand = get_random(10);
- ti->rename_file(ni, rand);
- #ifdef DEBUG
- cfg->log()<<QString("file ")+ ti->files().at(i).path.c_str() + " skipped, not our langpack";
- #endif
- continue;
- }
- filepath.remove(p1, p2-p1);
- }
- 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());
- std::list<s_list_entry>::iterator iph = std::find(updated_g_s_list_http.begin(), updated_g_s_list_http.end(), file.toUtf8().data());
- 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));
- std::list<s_list_cmp_entry>::iterator ipc = std::find(torrent_cmp_list.begin(), torrent_cmp_list.end(), file.toUtf8().data());
- std::list<s_list_entry>::iterator ipm = std::find(modyfied_list.begin(), modyfied_list.end(), file.toUtf8().toLower().data());
- 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()))
- {
- QString file2 = filepath;
- file2.insert(0, "..");
- ti->rename_file(ni, file2.toStdString());
- #ifdef DEBUG
- cfg->log()<<QString("we already have ")+ ti->files().at(i).path.c_str() + " renamed to match local file";
- #endif
- // fp[ni] = 1; //neded to get missed chunks ?
- continue;
- }
- else if(ipt != updated_g_s_list_torrent.end())
- {
- fp[ni] = 1;
- if(ipc != torrent_cmp_list.end())
- {
- char num[10];
- snprintf(num, 9, "%d", ipc->id);
- ti->rename_file(ni, num);
- #ifdef DEBUG
- cfg->log()<<QString("renaming ")+ ti->files().at(i).path.c_str() + " to " + (const char*)num + " (name mapping found in cmp list)";
- #endif
- }
- else
- {
- int id = (!torrent_cmp_list.empty())?torrent_cmp_list.back().id + 1:0;
- char num[10];
- snprintf(num, 9, "%d", id);
- if(ipt != updated_g_s_list_torrent.end())
- {
- #ifdef DEBUG
- 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)";
- assert(id < 4000);
- #endif
- torrent_cmp_list.push_back(s_list_cmp_entry(*ipt, id));
- }
- else
- {
- #ifdef DEBUG
- 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)";
- assert(id < 4000);
- #endif
- if(iph != updated_g_s_list_http.end())
- torrent_cmp_list.push_back(s_list_cmp_entry(*iph, id));
- else
- torrent_cmp_list.push_back(s_list_cmp_entry(file.toUtf8().data(), id));
- }
- ti->rename_file(ni, num);
- #ifdef DEBUG
- cfg->log()<<QString("renaming ")+ti->files().at(i).path.c_str() + " to " + (const char*)num + " (new mapping)";
- #endif
- }
- #ifdef DEBUG
- cfg->log()<<QString(ti->files().at(i).path.c_str()) + " file in list for downloading via torrent, handling";
- #endif
- }
- else
- {
- std::string m = get_random(10);
- ti->rename_file(ni, m);
- if(iph != updated_g_s_list_http.end())
- {
- torrent_cmp_tmp_list.push_back(s_list_cmp_s_entry(*iph, m));
- torrent_cmp_list.remove(*ipc);
- }
- #ifdef DEBUG
- cfg->log()<<QString(ti->files().at(i).path.c_str()) + " file not in torrent file list";
- #endif
- }
- }
- }
- }
- remove_torrents(sess);
- libtorrent::add_torrent_params params;
- params.ti = ti;
- params.auto_managed = false;
- if(!QFile(cfg->game_path() + "/torrent_upd").exists())
- QDir().mkpath(cfg->game_path() + "/torrent_upd");
- params.save_path = QString(cfg->game_path() + "/torrent_upd").toUtf8().data();
- params.storage_mode = libtorrent::storage_mode_sparse;
- if(QFile(this_app->applicationDirPath() + "/config/torrent_resume").exists())
- {
- QFile in(this_app->applicationDirPath() + "/config/torrent_resume");
- in.open(QIODevice::ReadOnly);
- QByteArray arr = in.readAll();
- in.close();
- arr = lzUncompress(&arr);
- std::vector<char> buf;
- for(int i = 0; i < arr.size(); ++i)
- buf.push_back(arr[i]);
- arr.clear();
- params.flags |= libtorrent::add_torrent_params::flag_override_resume_data;
- params.resume_data = &buf;
- torrent_h = sess->add_torrent(params);
- }
- else
- torrent_h = sess->add_torrent(params);
- emit assign_torrent_h(torrent_h);
- torrent_h.prioritize_files(fp);
- torrent_h.set_sequential_download(true);
- if(torrent_h.status().paused)
- torrent_h.resume();
- torrent_h.force_reannounce();
- if(cfg->torrent_dht())
- torrent_h.force_dht_announce();
- }
- catch(const libtorrent::libtorrent_exception &e)
- {
- #ifdef DEBUG
- cfg->log()<<QString("we have following problem with torrent: ")+e.what();
- #endif
- }
- if(t_m_t_obj)
- {
- QMetaObject::invokeMethod(t_m_t_obj, "exit_cleanup", Qt::QueuedConnection);
- t_m_t_obj = NULL;
- }
- #ifndef DISABLE_THREADS
- t_m_t_obj = new torrent_monitor_thread(torrent_h, updated_size, &updated_g_s_list_http, &updated_g_s_list_torrent,
- &updated_g_f_list, &torrent_cmp_list, &torrent_cmp_tmp_list, &modyfied_list, cfg, torrent_monitor_running, lists_mutex);
- t_m_t_obj->moveToThread(&monitor_thr);
- connect(t_m_t_obj, SIGNAL(set_progress2(int)), this, SIGNAL(set_progress2(int)));
- qRegisterMetaType<libtorrent::torrent_handle>("libtorrent::torrent_handle");
- connect(this, SIGNAL(assign_torrent_h(libtorrent::torrent_handle)), t_m_t_obj, SLOT(assign_torrent_h(libtorrent::torrent_handle)));
- connect(t_m_t_obj, SIGNAL(start_post_upd()), this, SLOT(start_post_upd()));
- connect(t_m_t_obj, SIGNAL(req_new_t_h()), this, SLOT(req_new_t_h()));
- connect(t_m_t_obj, SIGNAL(download_finished()), this, SIGNAL(download_finished()));
- connect(t_m_t_obj, SIGNAL(change_upd_current_state_lbl(QString)), this, SIGNAL(change_upd_current_state_lbl(QString)));
- connect(t_m_t_obj, SIGNAL(get_next_file()), this, SLOT(get_next_file()));
- connect(t_m_t_obj, SIGNAL(ready_for_shutdown()), this, SIGNAL(torrent_monitor_thr_r_f_s()));
- connect(this, SIGNAL(set_is_f_file(bool)), t_m_t_obj, SLOT(set_is_f_file(bool)));
- connect(this, SIGNAL(set_dl_size_cmp(quint64)), t_m_t_obj, SLOT(set_dl_size_cmp(quint64)));
- connect(this, SIGNAL(set_have_http_files(bool)), t_m_t_obj, SLOT(set_have_http_files(bool)));
- connect(this, SIGNAL(exit_cleanup()), t_m_t_obj, SLOT(exit_cleanup()));
- #endif
- if(cfg->upd_methd() == 1)
- emit set_show_upd_current_lbl(true);
- }
- else if(torrent_h.is_valid())
- {
- torrent_h.resume();
- torrent_h.force_reannounce();
- if(cfg->torrent_dht())
- torrent_h.force_dht_announce();
- emit assign_torrent_h(torrent_h);
- }
- }
- else
- {
- QMessageBox msg;
- msg.setWindowTitle(" ");
- msg.setText(tr("Failed to get metadata from server\nRetry ?"));
- msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
- if(msg.exec() == QMessageBox::Yes)
- {
- QNetworkRequest req;
- req.setRawHeader("User-Agent", update_ua_header.toUtf8());
- req.setUrl(QUrl(torrent_link));
- QNetworkReply *r = qnm.get(req);
- connect(r, SIGNAL(finished()), SLOT(torrent_request_finished()));
- }
- else if(cfg->is_game_exists())
- {
- if(cfg->torrent_seed())
- torrent_seed();
- emit set_enabled_login_field(true);
- emit set_enabled_password_field(true);
- emit set_enabled_login(true);
- }
- }
- lists_mutex.unlock();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement