Skip to content
Closed
11 changes: 11 additions & 0 deletions include/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <span>
#include <string>
#include <vector>

#pragma once

using ByteSpan = std::span<const char>;

std::string bytespan_to_string(ByteSpan span);

std::vector<char> strtovec(std::string_view str);
9 changes: 6 additions & 3 deletions include/Network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
///

#pragma once
#include "Helpers.h"
#include <span>
#include <string>

#ifdef __linux__
#include "linuxfixes.h"
#include <bits/types/siginfo_t.h>
#include <cstdint>
#include <vector>
#include <sys/ucontext.h>
#endif

Expand All @@ -39,16 +42,16 @@ extern std::string PrivateKey;
extern std::string ListOfMods;
int KillSocket(uint64_t Dead);
void UUl(const std::string& R);
void UDPSend(std::string Data);
void UDPSend(const std::vector<char>& Data);
bool CheckBytes(int32_t Bytes);
void GameSend(std::string_view Data);
void SendLarge(std::string Data);
void SendLarge(const std::vector<char>& Data);
std::string TCPRcv(uint64_t Sock);
void SyncResources(uint64_t TCPSock);
std::string GetAddr(const std::string& IP);
void ServerParser(std::string_view Data);
std::string Login(const std::string& fields);
void TCPSend(const std::string& Data, uint64_t Sock);
void TCPSend(const std::vector<char>& Data, uint64_t Sock);
void TCPClientMain(const std::string& IP, int Port);
void UDPClientMain(const std::string& IP, int Port);
void TCPGameServer(const std::string& IP, int Port);
11 changes: 11 additions & 0 deletions include/NetworkHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#if defined(__linux__)
#include "linuxfixes.h"
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <vector>

void ReceiveFromGame(SOCKET socket, std::vector<char>& out_data);
5 changes: 3 additions & 2 deletions src/Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ std::vector<char> Comp(std::span<const char> input) {
auto max_size = compressBound(input.size());
std::vector<char> output(max_size);
uLongf output_size = output.size();
int res = compress(
int res = compress2(
reinterpret_cast<Bytef*>(output.data()),
&output_size,
reinterpret_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.size()));
static_cast<uLongf>(input.size()),
3);
if (res != Z_OK) {
error("zlib compress() failed: " + std::to_string(res));
throw std::runtime_error("zlib compress() failed");
Expand Down
29 changes: 25 additions & 4 deletions src/GameStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <windows.h>
#elif defined(__linux__)
#include "vdf_parser.hpp"
#include <cerrno>
#include <cstring>
#include <pwd.h>
#include <spawn.h>
#include <sys/types.h>
Expand Down Expand Up @@ -51,7 +53,6 @@ std::string GetGamePath() {
std::string Ver = CheckVer(GetGameDir());
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
Path += Ver + "\\";
info("Game user path: '" + Path + "'");
return Path;
}
#elif defined(__linux__)
Expand All @@ -64,7 +65,6 @@ std::string GetGamePath() {
std::string Ver = CheckVer(GetGameDir());
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
Path += Ver + "/";
info("Game user path: '" + Path + "'");
return Path;
}
#endif
Expand Down Expand Up @@ -92,11 +92,27 @@ void StartGame(std::string Dir) {
}
#elif defined(__linux__)
void StartGame(std::string Dir) {
int status;
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
char* argv[] = { filename.data(), NULL };
pid_t pid;
int result = posix_spawn(&pid, filename.c_str(), NULL, NULL, argv, environ);

posix_spawn_file_actions_t file_actions;
auto status = posix_spawn_file_actions_init(&file_actions);
// disable stdout
if (status != 0) {
error(std::string("posix_spawn_file_actions_init failed: ") + std::strerror(errno));
}
status = posix_spawn_file_actions_addclose(&file_actions, STDOUT_FILENO);
if (status != 0) {
error(std::string("posix_spawn_file_actions_addclose for STDOUT failed: ") + std::strerror(errno));
}
status = posix_spawn_file_actions_addclose(&file_actions, STDERR_FILENO);
if (status != 0) {
error(std::string("posix_spawn_file_actions_addclose for STDERR failed: ") + std::strerror(errno));
}

// launch the game
int result = posix_spawn(&pid, filename.c_str(), &file_actions, NULL, argv, environ);

if (result != 0) {
error("Failed to Launch the game! launcher closing soon");
Expand All @@ -106,6 +122,11 @@ void StartGame(std::string Dir) {
error("Game Closed! launcher closing soon");
}

status = posix_spawn_file_actions_destroy(&file_actions);
if (status != 0) {
warn(std::string("posix_spawn_file_actions_destroy failed: ") + std::strerror(errno));
}

std::this_thread::sleep_for(std::chrono::seconds(5));
exit(2);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "Helpers.h"

std::string bytespan_to_string(ByteSpan span) {
return std::string(span.data(), span.size());
}

std::vector<char> strtovec(std::string_view str) {
return std::vector<char>(str.begin(), str.end());
}
12 changes: 6 additions & 6 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,35 @@ void addToLog(const std::string& Line) {
}
void info(const std::string& toPrint) {
std::string Print = getDate() + "[INFO] " + toPrint + "\n";
std::cout << Print;
std::cout << Print << std::flush;
addToLog(Print);
}
void debug(const std::string& toPrint) {
if (!Dev)
return;
std::string Print = getDate() + "[DEBUG] " + toPrint + "\n";
std::cout << Print;
std::cout << Print << std::flush;
addToLog(Print);
}
void warn(const std::string& toPrint) {
std::string Print = getDate() + "[WARN] " + toPrint + "\n";
std::cout << Print;
std::cout << Print << std::flush;
addToLog(Print);
}
void error(const std::string& toPrint) {
std::string Print = getDate() + "[ERROR] " + toPrint + "\n";
std::cout << Print;
std::cout << Print << std::flush;
addToLog(Print);
}
void fatal(const std::string& toPrint) {
std::string Print = getDate() + "[FATAL] " + toPrint + "\n";
std::cout << Print;
std::cout << Print << std::flush;
addToLog(Print);
std::this_thread::sleep_for(std::chrono::seconds(5));
_Exit(-1);
}
void except(const std::string& toPrint) {
std::string Print = getDate() + "[EXCEP] " + toPrint + "\n";
std::cout << Print;
std::cout << Print << std::flush;
addToLog(Print);
}
Loading