new download system with progress bar

This commit is contained in:
Anonymous275 2020-05-05 00:49:01 +03:00
parent cc537802f7
commit f78e3927a8
3 changed files with 91 additions and 7 deletions

View File

@ -5,6 +5,7 @@
#define CURL_STATICLIB
#include "curl/curl.h"
#include <iostream>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
@ -24,4 +25,86 @@ std::string HTTP_REQUEST(const std::string& IP,int port){
curl_easy_cleanup(curl);
}
return readBuffer;
}
int nb_bar;
double last_progress, progress_bar_adv;
int progress_bar (void *bar, double t, double d)
{
if(last_progress != round(d/t*100))
{
nb_bar = 25;
progress_bar_adv = round(d/t*nb_bar);
std::cout<<"\r";
std::cout<<"Progress : [ ";
if(round(d/t*100) < 10)
{ std::cout<<"0"<<round(d/t*100)<<"% ]"; }
else
{ std::cout<<round(d/t*100)<<"% ] "; }
std::cout<<"[";
for(int i = 0 ; i <= progress_bar_adv ; i++)
{ std::cout<<"#"; }
for(int i = 0 ; i < nb_bar - progress_bar_adv; i++)
{ std::cout<<"."; }
std::cout<<"]";
last_progress = round(d/t*100);
}
return 0;
}
struct FtpFile {
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
void *stream)
{
auto *out = (struct FtpFile *)stream;
if(!out->stream) {
fopen_s(&out->stream,out->filename,"wb");
if(!out->stream)
return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
void Download(const std::string& URL,const std::string& Path)
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile = {
Path.c_str(),
nullptr
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,URL.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
//progress_bar : the fonction for the progress bar
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_bar);
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
fprintf(stderr, "Failed to download! Code : %d\n", res);
}
}
if(ftpfile.stream)fclose(ftpfile.stream);
curl_global_cleanup();
std::cout << std::endl;
}

View File

@ -11,6 +11,7 @@
#include <thread>
#pragma comment(lib, "urlmon.lib")
void Download(const std::string& URL,const std::string& OutFileName);
void StartGame(const std::string&ExeDir,const std::string&Current);
std::string HTTP_REQUEST(const std::string&url,int port);
void CheckForUpdates(const std::string& CV);
@ -23,9 +24,6 @@ void Discord_Main();
bool MPDEV = false;
void ProxyStart();
void Download(const std::string& URL,const std::string& path){
URLDownloadToFileA(nullptr, URL.c_str(), path.c_str(), 0, nullptr);
}
void SystemExec(const std::string& cmd){
system(cmd.c_str());
}
@ -85,7 +83,7 @@ int main(int argc, char* argv[]){
const unsigned long long NPos = std::string::npos;
struct stat info{};
std::string ver = "0.91", Path = CheckDir(argv[0],ver),HTTP_Result;
std::string ver = "0.92", Path = CheckDir(argv[0],ver),HTTP_Result;
CheckForUpdates(ver);
if(argc > 1){
std::string Port = argv[1];
@ -120,10 +118,13 @@ int main(int argc, char* argv[]){
std::string ExeDir = "\""+GamePath.substr(0,GamePath.find_last_of('\\')) + R"(\Bin64\BeamNG.drive.x64.exe")";
std::string Settings = Path + "\\settings\\uiapps-layouts.json";
if(stat(Settings.c_str(),&info)!=0){
std::cout << "Downloading default config..." << std::endl;
Download("https://beamng-mp.com/client-data",Settings);
std::cout << "Downloaded default config!" << std::endl;
std::cout << "Download Complete!" << std::endl;
}
std::cout << "Downloading mod..." << std::endl;
Download("https://beamng-mp.com/builds/client?did="+GlobalInfo.at(2),Path + R"(\mods\BeamMP.zip)");
std::cout << "Download Complete!" << std::endl;
if(!MPDEV){
std::thread Game(StartGame,ExeDir,(Path + "\\"));

View File

@ -38,8 +38,8 @@ void TCPSEND(const std::string&Data){
if (MPDEV)std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl;
TCPTerminate = true;
} else {
if (MPDEV && Data.length() > 1000) {
std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << Data << std::endl;
if (MPDEV && Data.length() < 100) {
std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << std::endl;
}
//std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << Data << std::endl;
}