From c8bddb0d3e914b516e2904026d68e09d90307822 Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Thu, 2 Jul 2020 20:43:34 +0300 Subject: [PATCH] Cleanup + mod sync rewrite --- src/CoreNetwork.cpp | 35 +++++++++--------- src/Discord.cpp | 10 +++--- src/Memory/Reader.cpp | 4 +-- src/Network 2.0/GlobalHandler.cpp | 36 +++++++++---------- src/Network 2.0/VehicleData.cpp | 4 +-- src/Network 2.0/VehicleEvent.cpp | 22 ++++++------ src/Resources.cpp | 60 +++++++++++++++---------------- src/UpdateCheck.cpp | 7 ++-- src/VehicleNetwork.cpp | 32 ++++++++--------- src/http.cpp | 9 ++--- src/main.cpp | 42 ++++++++++++++-------- 11 files changed, 138 insertions(+), 123 deletions(-) diff --git a/src/CoreNetwork.cpp b/src/CoreNetwork.cpp index 5396a08..22d9c49 100644 --- a/src/CoreNetwork.cpp +++ b/src/CoreNetwork.cpp @@ -16,11 +16,12 @@ extern std::string MStatus; extern int ping; extern bool Terminate; extern bool TCPTerminate; -extern bool MPDEV; +extern bool Dev; extern std::string ListOfMods; bool Confirm = false; std::set Conf; void StartSync(const std::string &Data){ + UlStatus = "UlLoading..."; Terminate = false; Conf.clear(); std::thread t1(ProxyThread,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); @@ -74,11 +75,11 @@ bool once = false; try { std::cout << "Ready!" << std::endl; do { - if (MPDEV)std::cout << "Core Network on start!" << std::endl; + if (Dev)std::cout << "Core Network on start!" << std::endl; WSADATA wsaData; int iResult; - auto ListenSocket = INVALID_SOCKET; - auto ClientSocket = INVALID_SOCKET; + SOCKET ListenSocket; + SOCKET ClientSocket; struct addrinfo *result = nullptr; struct addrinfo hints{}; @@ -88,9 +89,9 @@ bool once = false; int recvbuflen = 64000; // Initialize Winsock - iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + iResult = WSAStartup(514, &wsaData); //2.2 if (iResult != 0) { - if (MPDEV)std::cout << "WSAStartup failed with error: " << iResult << std::endl; + if (Dev)std::cout << "WSAStartup failed with error: " << iResult << std::endl; } ZeroMemory(&hints, sizeof(hints)); @@ -102,14 +103,14 @@ bool once = false; // Resolve the server address and port iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result); if (iResult != 0) { - if (MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl; + if (Dev)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); } // Create a socket for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if (ListenSocket == INVALID_SOCKET) { - if (MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl; + if (ListenSocket == -1) { + if (Dev)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); } @@ -117,7 +118,7 @@ bool once = false; // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen); if (iResult == SOCKET_ERROR) { - if (MPDEV)Exit("(Core) bind failed with error: " + std::to_string(WSAGetLastError())); + if (Dev)Exit("(Core) bind failed with error: " + std::to_string(WSAGetLastError())); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -125,13 +126,13 @@ bool once = false; iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - if (MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl; + if (Dev)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); } ClientSocket = accept(ListenSocket, nullptr, nullptr); - if (ClientSocket == INVALID_SOCKET) { - if (MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl; + if (ClientSocket == -1) { + if (Dev)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); } @@ -149,16 +150,16 @@ bool once = false; data.resize(iResult); Response = Parse(data) + "\n"; } else if (iResult == 0) { - if (MPDEV)std::cout << "(Core) Connection closing...\n"; + if (Dev)std::cout << "(Core) Connection closing...\n"; } else { - if (MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl; + if (Dev)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); } if (!Response.empty()) { iSendResult = send(ClientSocket, Response.c_str(), Response.length(), 0); if (iSendResult == SOCKET_ERROR) { - if (MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl; + if (Dev)std::cout << "send failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); } else { @@ -169,7 +170,7 @@ bool once = false; iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { - if (MPDEV)std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl; + if (Dev)std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); Sleep(500); diff --git a/src/Discord.cpp b/src/Discord.cpp index 6d72ec6..e4ae02d 100644 --- a/src/Discord.cpp +++ b/src/Discord.cpp @@ -11,7 +11,7 @@ #include #include #include -extern bool MPDEV; +extern bool Dev; static const char* APPLICATION_ID = "629743237988352010"; static int64_t StartTime; static int SendPresence = 1; @@ -61,22 +61,22 @@ static void handleDiscordReady(const DiscordUser* connectedUser) static void handleDiscordDisconnected(int errcode, const char* message) { - if(MPDEV)printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); + if(Dev)printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); } static void handleDiscordError(int errcode, const char* message) { - if(MPDEV)printf("\nDiscord: error (%d: %s)\n", errcode, message); + if(Dev)printf("\nDiscord: error (%d: %s)\n", errcode, message); } static void handleDiscordJoin(const char* secret) { - if(MPDEV)printf("\nDiscord: join (%s)\n", secret); + if(Dev)printf("\nDiscord: join (%s)\n", secret); } static void handleDiscordSpectate(const char* secret) { - if(MPDEV)printf("\nDiscord: spectate (%s)\n", secret); + if(Dev)printf("\nDiscord: spectate (%s)\n", secret); } static void handleDiscordJoinRequest(const DiscordUser* request) diff --git a/src/Memory/Reader.cpp b/src/Memory/Reader.cpp index 5000667..6635478 100644 --- a/src/Memory/Reader.cpp +++ b/src/Memory/Reader.cpp @@ -5,7 +5,7 @@ #include #include extern std::string MStatus; -extern bool MPDEV; +extern bool Dev; Memory Game; std::string GameVer(HANDLE processHandle, long long Address){ //lib_Beam @@ -23,7 +23,7 @@ void SetPID(DWORD PID){ Game.PID = PID; } [[noreturn]] void MemoryInit(){ - if(Game.PID == 0 && !MPDEV)exit(4); + if(Game.PID == 0 && !Dev)exit(4); HANDLE processHandle; long long ExeBase; //BeamNG.drive.x64.exe long long Lib1 = 0x180000000; //libbeamng.x64.dll Contains Vehicle Data diff --git a/src/Network 2.0/GlobalHandler.cpp b/src/Network 2.0/GlobalHandler.cpp index a15e698..93bf4a7 100644 --- a/src/Network 2.0/GlobalHandler.cpp +++ b/src/Network 2.0/GlobalHandler.cpp @@ -16,16 +16,16 @@ bool Terminate = false; bool CServer = true; bool gameConected = false; SOCKET ClientSocket; -extern bool MPDEV; +extern bool Dev; int ping = 0; void GameSend(const std::string&Data){ if(TCPTerminate || !gameConected || ClientSocket == -1)return; int iSendResult = send(ClientSocket, (Data + "\n").c_str(), int(Data.length()) + 1, 0); if (iSendResult == SOCKET_ERROR) { - if (MPDEV)std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl; + if (Dev)std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl; } else { - if (MPDEV && Data.length() > 1000) { + if (Dev && Data.length() > 1000) { std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << std::endl; } //std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << Data << std::endl; @@ -45,11 +45,11 @@ void ServerSend(const std::string&Data, bool Rel){ else TCPSend(Data); }else UDPSend(Data); - if (MPDEV && Data.length() > 1000) { + if (Dev && Data.length() > 1000) { std::cout << "(Launcher->Server) Bytes sent: " + std::to_string(Data.length()) + " : " + Data.substr(0, 10) + Data.substr(Data.length() - 10) + "\n"; - }else if(MPDEV && C == 'Z'){ + }else if(Dev && C == 'Z'){ //std::cout << "(Game->Launcher) : " << Data << std::endl; } } @@ -109,14 +109,14 @@ void Reset() { std::string Compress(const std::string&Data); std::string Decompress(const std::string&Data); void TCPGameServer(const std::string& IP, int Port){ - if(MPDEV)std::cout << "Game server Started! " << IP << ":" << Port << std::endl; + if(Dev)std::cout << "Game server Started! " << IP << ":" << Port << std::endl; do { Reset(); if(CServer) { std::thread Client(TCPClientMain, IP, Port); Client.detach(); } - if(MPDEV)std::cout << "Game server on Start" << std::endl; + if(Dev)std::cout << "Game server on Start" << std::endl; WSADATA wsaData; int iResult; SOCKET ListenSocket = INVALID_SOCKET; @@ -130,7 +130,7 @@ void TCPGameServer(const std::string& IP, int Port){ // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { - if(MPDEV)std::cout << "(Proxy) WSAStartup failed with error: " << iResult << std::endl; + if(Dev)std::cout << "(Proxy) WSAStartup failed with error: " << iResult << std::endl; exit(-1); } @@ -142,7 +142,7 @@ void TCPGameServer(const std::string& IP, int Port){ // Resolve the server address and port iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT+1).c_str(), &hints, &result); if (iResult != 0) { - if(MPDEV)std::cout << "(Proxy) getaddrinfo failed with error: " << iResult << std::endl; + if(Dev)std::cout << "(Proxy) getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); break; } @@ -150,7 +150,7 @@ void TCPGameServer(const std::string& IP, int Port){ // Create a socket for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - if(MPDEV)std::cout << "(Proxy) socket failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(Proxy) socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); break; @@ -159,7 +159,7 @@ void TCPGameServer(const std::string& IP, int Port){ // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen); if (iResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(Proxy) bind failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(Proxy) bind failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -170,20 +170,20 @@ void TCPGameServer(const std::string& IP, int Port){ iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(Proxy) listen failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(Proxy) listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } Socket = accept(ListenSocket, nullptr, nullptr); if (Socket == INVALID_SOCKET) { - if(MPDEV)std::cout << "(Proxy) accept failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(Proxy) accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } closesocket(ListenSocket); - if(MPDEV)std::cout << "(Proxy) Game Connected!" << std::endl; + if(Dev)std::cout << "(Proxy) Game Connected!" << std::endl; gameConected = true; if(CServer){ std::thread t1(NetMain, IP, Port); @@ -204,13 +204,13 @@ void TCPGameServer(const std::string& IP, int Port){ ServerSend(buff,false); } else if (iResult == 0) { - if(MPDEV)std::cout << "(Proxy) Connection closing...\n"; + if(Dev)std::cout << "(Proxy) Connection closing...\n"; closesocket(Socket); WSACleanup(); Terminate = true; continue; } else { - if(MPDEV)std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(Socket); WSACleanup(); continue; @@ -219,7 +219,7 @@ void TCPGameServer(const std::string& IP, int Port){ iResult = shutdown(Socket, SD_SEND); if (iResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(Proxy) shutdown failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(Proxy) shutdown failed with error: " << WSAGetLastError() << std::endl; TCPTerminate = true; Terminate = true; closesocket(Socket); @@ -235,7 +235,7 @@ void VehicleNetworkStart(); void CoreNetworkThread(); void ProxyStart(){ std::thread t1(CoreNetworkThread); - if(MPDEV)std::cout << "Core Network Started!\n"; + if(Dev)std::cout << "Core Network Started!\n"; t1.join(); } diff --git a/src/Network 2.0/VehicleData.cpp b/src/Network 2.0/VehicleData.cpp index c1152be..3d51156 100644 --- a/src/Network 2.0/VehicleData.cpp +++ b/src/Network 2.0/VehicleData.cpp @@ -12,7 +12,7 @@ extern bool Terminate; extern int ClientID; -extern bool MPDEV; +extern bool Dev; SOCKET UDPSock; sockaddr_in ToServer{}; struct PacketData{ @@ -149,7 +149,7 @@ void HandleChunk(const std::string&Data){ void UDPParser(const std::string&Packet){ if(Packet.substr(0,4) == "ACK:"){ AckID(stoi(Packet.substr(4))); - if(MPDEV)std::cout << "Got Ack for data" << std::endl; + if(Dev)std::cout << "Got Ack for data" << std::endl; return; }else if(Packet.substr(0,3) == "BD:"){ int pos = Packet.find(':',4); diff --git a/src/Network 2.0/VehicleEvent.cpp b/src/Network 2.0/VehicleEvent.cpp index dbda16e..1d45e6c 100644 --- a/src/Network 2.0/VehicleEvent.cpp +++ b/src/Network 2.0/VehicleEvent.cpp @@ -5,27 +5,26 @@ #include #include #include -#include extern std::string UlStatus; extern bool Terminate; -extern bool MPDEV; +extern bool Dev; SOCKET TCPSock; void TCPSend(const std::string&Data){ - if(TCPSock == INVALID_SOCKET){ + if(TCPSock == -1){ Terminate = true; return; } int BytesSent = send(TCPSock, Data.c_str(), int(Data.length())+1, 0); if (BytesSent == 0){ - if(MPDEV)std::cout << "(TCP) Connection closing..." << std::endl; + if(Dev)std::cout << "(TCP) Connection closing..." << std::endl; Terminate = true; return; } else if (BytesSent < 0) { - if(MPDEV)std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl; closesocket(TCPSock); Terminate = true; return; @@ -38,18 +37,18 @@ void TCPRcv(){ char buf[4096]; int len = 4096; ZeroMemory(buf, len); - if(TCPSock == INVALID_SOCKET){ + if(TCPSock == -1){ Terminate = true; return; } int BytesRcv = recv(TCPSock, buf, len,0); if (BytesRcv == 0){ - if(MPDEV)std::cout << "(TCP) Connection closing..." << std::endl; + if(Dev)std::cout << "(TCP) Connection closing..." << std::endl; Terminate = true; return; } else if (BytesRcv < 0) { - if(MPDEV)std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(TCPSock); Terminate = true; return; @@ -84,6 +83,7 @@ void TCPClientMain(const std::string& IP,int Port){ std::cout << "Client: connect failed! Error code: " << WSAGetLastError() << std::endl; closesocket(TCPSock); WSACleanup(); + Terminate = true; return; } getsockname(TCPSock, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr)); @@ -92,12 +92,12 @@ void TCPClientMain(const std::string& IP,int Port){ while(!Terminate)TCPRcv(); GameSend("T"); ////Game Send Terminate - if( shutdown(TCPSock, SD_SEND) != 0 && MPDEV) + if( shutdown(TCPSock, SD_SEND) != 0 && Dev) std::cout << "(TCP) shutdown error code: " << WSAGetLastError() << std::endl; - if(closesocket(TCPSock) != 0 && MPDEV) + if(closesocket(TCPSock) != 0 && Dev) std::cout << "(TCP) Cannot close socket. Error code: " << WSAGetLastError() << std::endl; - if(WSACleanup() != 0 && MPDEV) + if(WSACleanup() != 0 && Dev) std::cout << "(TCP) Client: WSACleanup() failed!..." << std::endl; } diff --git a/src/Resources.cpp b/src/Resources.cpp index 18a2e5b..d3e7c33 100644 --- a/src/Resources.cpp +++ b/src/Resources.cpp @@ -9,20 +9,19 @@ #include #include #include -#include + extern std::vector GlobalInfo; void Exit(const std::string& Msg); namespace fs = std::experimental::filesystem; extern std::string UlStatus; extern bool TCPTerminate; -extern std::string ver; extern bool Terminate; extern bool Confirm; -extern bool MPDEV; +extern bool Dev; std::string ListOfMods; std::vector Split(const std::string& String,const std::string& delimiter){ std::vector Val; - size_t pos = 0; + size_t pos; std::string token,s = String; while ((pos = s.find(delimiter)) != std::string::npos) { token = s.substr(0, pos); @@ -34,25 +33,25 @@ std::vector Split(const std::string& String,const std::string& deli } void STCPSend(SOCKET socket,const std::string&Data){ - if(socket == INVALID_SOCKET){ + if(socket == -1){ Terminate = true; return; } int BytesSent = send(socket, Data.c_str(), int(Data.length())+1, 0); if (BytesSent == 0){ - if(MPDEV)std::cout << "(TCP) Connection closing..." << std::endl; + if(Dev)std::cout << "(TCP) Connection closing..." << std::endl; Terminate = true; return; } else if (BytesSent < 0) { - if(MPDEV)std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl; closesocket(socket); Terminate = true; return; } std::this_thread::sleep_for(std::chrono::milliseconds(200)); } -std::pair STCPRecv(SOCKET socket){ +std::pair STCPRecv(SOCKET socket){ char buf[64000]; int len = 64000; ZeroMemory(buf, len); @@ -87,10 +86,11 @@ void WaitForConfirm(){ } void SyncResources(SOCKET Sock){ - if(MPDEV)std::cout << "SyncResources Called" << std::endl; + std::cout << "Checking Resources..." << std::endl; + std::string HandShakeVer = "1.43"; CheckForDir(); STCPSend(Sock,"NR" + GlobalInfo.at(0) + ":" +GlobalInfo.at(2)); - STCPSend(Sock,"VC" + ver); + STCPSend(Sock,"VC" + HandShakeVer); auto Res = STCPRecv(Sock); std::string msg = Res.first; if(msg.size() < 2 || msg.substr(0,2) != "WS"){ @@ -124,18 +124,17 @@ void SyncResources(SOCKET Sock){ if(t.empty())ListOfMods = "-"; else ListOfMods = t; t.clear(); - for(auto FN = FNames.begin(),FS = FSizes.begin(); FN != FNames.end() && !Terminate; ++FN,++FS) { int pos = FN->find_last_of('/'); if (pos == std::string::npos)continue; Amount++; } + if(!FNames.empty())std::cout << "Syncing..." << std::endl; for(auto FN = FNames.begin(),FS = FSizes.begin(); FN != FNames.end() && !Terminate; ++FN,++FS) { int pos = FN->find_last_of('/'); if (pos != std::string::npos) { a = "Resources" + FN->substr(pos); } else continue; - char *Data; Pos++; if (stat(a.c_str(), &info) == 0) { if (FS->find_first_not_of("0123456789") != std::string::npos)continue; @@ -149,36 +148,37 @@ void SyncResources(SOCKET Sock){ }else remove(a.c_str()); } CheckForDir(); - std::ofstream LFS; + do { STCPSend(Sock, "f" + *FN); - //std::set> segments; + int Recv = 0,Size = std::stoi(*FS); + char*File = new char[Size]; + ZeroMemory(File,Size); do { auto Pair = STCPRecv(Sock); - Data = Pair.first; + char* Data = Pair.first; + size_t BytesRcv = Pair.second; if (strcmp(Data, "Cannot Open") == 0 || Terminate)break; - if (!LFS.is_open()) { - LFS.open(a.c_str(), std::ios_base::app | std::ios::binary); - } - LFS.write(Data,Pair.second); - /*char* Segment = new char[Pair.second]; - memcpy_s(Segment,Pair.second,Data,Pair.second); - segments.insert(std::make_pair(Segment,Pair.second)); - total += Pair.second;*/ - float per = LFS.tellp()/ std::stof(*FS) * 100; + memcpy_s(File+Recv,BytesRcv,Data,BytesRcv); + Recv += BytesRcv; + float per = float(Recv)/std::stof(*FS) * 100; std::string Percent = std::to_string(truncf(per * 10) / 10); UlStatus = "UlDownloading Resource: (" + std::to_string(Pos) + "/" + std::to_string(Amount) + "): " + a.substr(a.find_last_of('/')) + " (" + Percent.substr(0, Percent.find('.') + 2) + "%)"; - } while (LFS.tellp() != std::stoi(*FS) && LFS.tellp() < std::stoi(*FS)); + delete[] Data; + } while (Recv != Size && Recv < Size && !Terminate); UlStatus = "UlLoading Resource: (" + std::to_string(Pos) + "/" + std::to_string(Amount) + "): " + a.substr(a.find_last_of('/')); - /*for(auto p : segments){ - LFS.write(p.first,p.second); - }*/ - //segments.clear(); + std::ofstream LFS; + if (!LFS.is_open()) { + LFS.open(a.c_str(), std::ios_base::app | std::ios::binary); + } + LFS.write(File,Recv); LFS.close(); - }while(fs::file_size(a) != std::stoi(*FS)); + ZeroMemory(File,Size); + delete[] File; + }while(fs::file_size(a) != std::stoi(*FS) && !Terminate); fs::copy_file(a, "BeamNG/mods"+a.substr(a.find_last_of('/')), fs::copy_options::overwrite_existing); WaitForConfirm(); } diff --git a/src/UpdateCheck.cpp b/src/UpdateCheck.cpp index e1382fe..ffc4640 100644 --- a/src/UpdateCheck.cpp +++ b/src/UpdateCheck.cpp @@ -4,14 +4,14 @@ #include -void Download(const std::string& URL,const std::string& path); +int Download(const std::string& URL,const std::string& path); std::string HTTP_REQUEST(const std::string&url,int port); std::string HTA(const std::string& hex); void SystemExec(const std::string& cmd); void WinExec(const std::string& cmd); void Exit(const std::string& Msg); - -void CheckForUpdates(const std::string& CV){ +void ReLaunch(int argc,char*args[]); +void CheckForUpdates(int argc,char*args[],const std::string& CV){ std::string link = "https://beamng-mp.com/builds/launcher?version=true"; std::string HTTP = HTTP_REQUEST(link,443); link = "https://beamng-mp.com/builds/launcher?download=true"; @@ -21,6 +21,7 @@ void CheckForUpdates(const std::string& CV){ struct stat buffer{}; if(stat ("BeamMP-Launcher.back", &buffer) == 0)remove("BeamMP-Launcher.back"); if(HTTP > CV){ + ReLaunch(argc,args); system("cls"); std::cout << "Update found!" << std::endl; std::cout << "Updating..." << std::endl; diff --git a/src/VehicleNetwork.cpp b/src/VehicleNetwork.cpp index a737113..79de19c 100644 --- a/src/VehicleNetwork.cpp +++ b/src/VehicleNetwork.cpp @@ -10,7 +10,7 @@ #include extern bool TCPTerminate; -extern bool MPDEV; +extern bool Dev; void Print(const std::string&MSG); std::queue VNTCPQueue; //void RUDPSEND(const std::string&Data,bool Rel); @@ -24,12 +24,12 @@ void Responder(const SOCKET *CS){ VNTCPQueue.front() += "\n"; iSendResult = send(ClientSocket, VNTCPQueue.front().c_str(), VNTCPQueue.front().length(), 0); if (iSendResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(VN) send failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) send failed with error: " << WSAGetLastError() << std::endl; break; } else { if(iSendResult > 1000){ - if(MPDEV){std::cout << "(Launcher->Game VN) Bytes sent: " << iSendResult << " : " << VNTCPQueue.front().substr(0,10) - << VNTCPQueue.front().substr(VNTCPQueue.front().length()-10) << std::endl;} + if(Dev){std::cout << "(Launcher->Game VN) Bytes sent: " << iSendResult << " : " << VNTCPQueue.front().substr(0, 10) + << VNTCPQueue.front().substr(VNTCPQueue.front().length()-10) << std::endl;} } VNTCPQueue.pop(); } @@ -41,7 +41,7 @@ std::string Compress(const std::string&Data); std::string Decompress(const std::string&Data); void VehicleNetworkStart(){ do { - if(MPDEV)std::cout << "VN on Start" << std::endl; + if(Dev)std::cout << "VN on Start" << std::endl; WSADATA wsaData; int iResult; SOCKET ListenSocket = INVALID_SOCKET; @@ -57,7 +57,7 @@ void VehicleNetworkStart(){ // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { - if(MPDEV)std::cout << "(VN) WSAStartup failed with error: " << iResult << std::endl; + if(Dev)std::cout << "(VN) WSAStartup failed with error: " << iResult << std::endl; std::cin.get(); exit(-1); } @@ -71,7 +71,7 @@ void VehicleNetworkStart(){ // Resolve the server address and port iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result); if (iResult != 0) { - if(MPDEV)std::cout << "(VN) getaddrinfo failed with error: " << iResult << std::endl; + if(Dev)std::cout << "(VN) getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); break; } @@ -79,7 +79,7 @@ void VehicleNetworkStart(){ // Create a socket for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - if(MPDEV)std::cout << "(VN) socket failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); break; @@ -88,7 +88,7 @@ void VehicleNetworkStart(){ // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen); if (iResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(VN) bind failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) bind failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -99,20 +99,20 @@ void VehicleNetworkStart(){ iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(VN) listen failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { - if(MPDEV)std::cout << "(VN) accept failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } closesocket(ListenSocket); - if(MPDEV)std::cout << "(VN) Game Connected!" << std::endl; + if(Dev)std::cout << "(VN) Game Connected!" << std::endl; std::thread TCPSend(Responder,&ClientSocket); TCPSend.detach(); @@ -125,7 +125,7 @@ void VehicleNetworkStart(){ memcpy(&buff[0],recvbuf,iResult); buff.resize(iResult); //Print(buff); - if(MPDEV) { + if(Dev) { std::string cmp = Compress(buff), dcm = Decompress(cmp); std::cout << "Compressed Size : " << cmp.length() << std::endl; std::cout << "Decompressed Size : " << dcm.length() << std::endl; @@ -138,13 +138,13 @@ void VehicleNetworkStart(){ //RUDPSEND(buff,false); //std::cout << "(Game->Launcher VN) Data : " << buff.length() << std::endl; } else if (iResult == 0) { - if(MPDEV)std::cout << "(VN) Connection closing...\n"; + if(Dev)std::cout << "(VN) Connection closing...\n"; closesocket(ClientSocket); WSACleanup(); continue; } else { - if(MPDEV)std::cout << "(VN) recv failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); continue; @@ -153,7 +153,7 @@ void VehicleNetworkStart(){ iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { - if(MPDEV)std::cout << "(VN) shutdown failed with error: " << WSAGetLastError() << std::endl; + if(Dev)std::cout << "(VN) shutdown failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); continue; diff --git a/src/http.cpp b/src/http.cpp index af949ad..167440e 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -14,7 +14,7 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use } std::string HTTP_REQUEST(const std::string& IP,int port){ CURL *curl; - CURLcode res; + //CURLcode res; std::string readBuffer; curl = curl_easy_init(); if(curl) { @@ -22,7 +22,7 @@ std::string HTTP_REQUEST(const std::string& IP,int port){ curl_easy_setopt(curl, CURLOPT_PORT, port); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - res = curl_easy_perform(curl); + curl_easy_perform(curl); curl_easy_cleanup(curl); } curl_global_cleanup(); @@ -65,7 +65,7 @@ static size_t my_fwrite(void *buffer,size_t size,size_t nmemb,void *stream) return fwrite(buffer, size, nmemb, out->stream); } -void Download(const std::string& URL,const std::string& Path) +int Download(const std::string& URL,const std::string& Path) { CURL *curl; CURLcode res; @@ -85,10 +85,11 @@ void Download(const std::string& URL,const std::string& Path) res = curl_easy_perform(curl); curl_easy_cleanup(curl); if(CURLE_OK != res) { - Exit("Failed to download! Code : " + std::to_string(res)); + return res; } } if(file.stream)fclose(file.stream); curl_global_cleanup(); std::cout << std::endl; + return -1; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3d87b79..144efce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,18 +10,18 @@ #include #include -void Download(const std::string& URL,const std::string& OutFileName); +int 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); +void CheckForUpdates(int argc,char*args[],const std::string& CV); std::vector GetDiscordInfo(); extern std::vector SData; std::vector GlobalInfo; std::string getHardwareID(); -std::string ver = "1.43"; +char* ver = (char*)"1.45"; int DEFAULT_PORT = 4444; void Discord_Main(); -bool MPDEV = false; +bool Dev = false; void ProxyStart(); void ExitError(); void Check(); @@ -35,7 +35,7 @@ void WinExec(const std::string& cmd){ void Exit(const std::string& Msg){ std::cout << Msg << std::endl; - std::cout << "Press Enter to continue . . ."; + std::cout << "Press Enter to continue..."; std::cin.ignore(); exit(-1); } @@ -56,7 +56,10 @@ std::string CheckDir(int argc,char*args[]){ if(stat(DN.c_str(),&info)==0)remove(DN.c_str()); SystemExec("rename \""+ FN +"\" " + DN + ">nul"); } - if(stat("BeamNG",&info))SystemExec("mkdir BeamNG>nul"); + if(stat("BeamNG",&info)){ + SystemExec("mkdir BeamNG>nul"); + if(stat("BeamNG",&info))ReLaunch(argc,args); + } if(!stat("BeamNG\\mods",&info))SystemExec("RD /S /Q BeamNG\\mods>nul"); if(!stat("BeamNG\\mods",&info))ReLaunch(argc,args); SystemExec("mkdir BeamNG\\mods>nul"); @@ -84,7 +87,7 @@ int main(int argc, char* argv[]){ struct stat info{}; system("cls"); std::string link, HTTP_Result; - SetWindowTextA(GetConsoleWindow(),("BeamMP Launcher v" + ver).c_str()); + SetWindowTextA(GetConsoleWindow(),("BeamMP Launcher v" + std::string(ver)).c_str()); std::thread t1(Discord_Main); t1.detach(); std::cout << "Connecting to discord client..." << std::endl; @@ -102,9 +105,9 @@ int main(int argc, char* argv[]){ exit(-1); } } - }else MPDEV = true; + }else Dev = true; std::string Path = CheckDir(argc,argv); - std::thread CFU(CheckForUpdates,ver); + std::thread CFU(CheckForUpdates,argc,argv,std::string(ver)); CFU.join(); if(argc > 1){ @@ -115,7 +118,7 @@ int main(int argc, char* argv[]){ std::cout << "Running on custom port : " << DEFAULT_PORT << std::endl; } } - if(argc > 2)MPDEV = false; + if(argc > 2)Dev = false; } //Security @@ -124,17 +127,26 @@ int main(int argc, char* argv[]){ delete Sec; if(SData.size() != 3)ExitError(); std::string GamePath = SData.at(2); - if(MPDEV)std::cout << "You own BeamNG on this machine!" << std::endl; + if(Dev)std::cout << "You own BeamNG on this machine!" << std::endl; std::cout << "Game Version : " << CheckVer(GamePath) << std::endl; 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::string DUI = Path + R"(\settings\uiapps-layouts.json)"; + std::string GS = Path + R"(\settings\game-settings.ini)"; + if(stat(DUI.c_str(),&info)!=0 || stat(GS.c_str(),&info)!=0){ link = "https://beamng-mp.com/client-ui-data"; std::cout << "Downloading default config..." << std::endl; - Download(link,Settings); + if(int i = Download(link,DUI) != -1){ + std::cout << "Error! Failed to download code : " << i << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(3)); + ReLaunch(argc,argv); + } + link = "https://beamng-mp.com/client-settings-data"; + Download(link,GS); std::cout << "Download Complete!" << std::endl; } - if(!MPDEV){ + DUI.clear(); + GS.clear(); + if(!Dev){ std::cout << "Downloading mod..." << std::endl; link = "https://beamng-mp.com/builds/client?did="+GlobalInfo.at(2); Download(link,Path + R"(\mods\BeamMP.zip)");