reformat all

This commit is contained in:
Lion Kortlepel
2020-12-23 23:34:30 +01:00
parent 0f4c08c068
commit a944565fb9
13 changed files with 171 additions and 147 deletions
+2 -1
View File
@@ -34,5 +34,6 @@ void InitRes() {
} }
} }
std::replace(FileList.begin(), FileList.end(), '\\', '/'); std::replace(FileList.begin(), FileList.end(), '\\', '/');
if(ModsLoaded)info("Loaded " + std::to_string(ModsLoaded) + " Mods"); if (ModsLoaded)
info("Loaded " + std::to_string(ModsLoaded) + " Mods");
} }
+2 -1
View File
@@ -27,7 +27,8 @@ void Args(int argc, char* argv[]) {
if (p != std::string::npos || n != 3 || CustomIP.substr(0, 3) == ("127")) { if (p != std::string::npos || n != 3 || CustomIP.substr(0, 3) == ("127")) {
CustomIP.clear(); CustomIP.clear();
warn("IP Specified is invalid! Ignoring"); warn("IP Specified is invalid! Ignoring");
} else info("Server started with custom IP"); } else
info("Server started with custom IP");
} }
} }
void InitServer(int argc, char* argv[]) { void InitServer(int argc, char* argv[]) {
+20 -10
View File
@@ -94,15 +94,19 @@ std::any TriggerLuaEvent(const std::string& Event, bool local, Lua* Caller, std:
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait); R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
Type = R.type().name(); Type = R.type().name();
if (Type.find("int") != std::string::npos) { if (Type.find("int") != std::string::npos) {
if(std::any_cast<int>(R))Ret++; if (std::any_cast<int>(R))
}else if(Event == "onPlayerAuth") return R; Ret++;
} else if (Event == "onPlayerAuth")
return R;
} }
} else { } else {
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait); R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
Type = R.type().name(); Type = R.type().name();
if (Type.find("int") != std::string::npos) { if (Type.find("int") != std::string::npos) {
if(std::any_cast<int>(R))Ret++; if (std::any_cast<int>(R))
}else if(Event == "onPlayerAuth") return R; Ret++;
} else if (Event == "onPlayerAuth")
return R;
} }
} }
} }
@@ -298,8 +302,10 @@ int lua_GetGuest(lua_State* L) {
if (lua_isnumber(L, 1)) { if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1)); int ID = int(lua_tonumber(L, 1));
Client* c = GetClient(ID); Client* c = GetClient(ID);
if (c != nullptr)lua_pushboolean(L, c->isGuest); if (c != nullptr)
else return 0; lua_pushboolean(L, c->isGuest);
else
return 0;
} else { } else {
SendError(L, "GetGuest not enough arguments"); SendError(L, "GetGuest not enough arguments");
return 0; return 0;
@@ -360,7 +366,8 @@ int lua_dropPlayer(lua_State* L) {
c->SetStatus(-2); c->SetStatus(-2);
info(("Closing socket due to kick")); info(("Closing socket due to kick"));
CloseSocketProper(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
} else SendError(L, ("DropPlayer not enough arguments")); } else
SendError(L, ("DropPlayer not enough arguments"));
return 0; return 0;
} }
int lua_sendChat(lua_State* L) { int lua_sendChat(lua_State* L) {
@@ -607,13 +614,16 @@ int lua_TempFix(lua_State*L) {
if (lua_isnumber(L, 1)) { if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1)); int ID = int(lua_tonumber(L, 1));
Client* c = GetClient(ID); Client* c = GetClient(ID);
if (c == nullptr)return 0; if (c == nullptr)
return 0;
std::string Ret; std::string Ret;
if (c->isGuest) { if (c->isGuest) {
Ret = "Guest-" + c->GetName(); Ret = "Guest-" + c->GetName();
}else Ret = c->GetName(); } else
Ret = c->GetName();
lua_pushstring(L, Ret.c_str()); lua_pushstring(L, Ret.c_str());
} else SendError(L, "GetDID not enough arguments"); } else
SendError(L, "GetDID not enough arguments");
return 1; return 1;
} }
void Lua::Init() { void Lua::Init() {
+12 -11
View File
@@ -6,22 +6,22 @@
/// Created by Anonymous275 on 7/31/2020 /// Created by Anonymous275 on 7/31/2020
/// ///
#include <Lua/LuaSystem.hpp>
#include "Security/Enc.h"
#include "UnixCompat.h"
#include "Curl/Http.h" #include "Curl/Http.h"
#include "Settings.h" #include "Json.h"
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Network.h"
#include "Security/Enc.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <Lua/LuaSystem.hpp>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <thread> #include <thread>
#include "Json.h"
std::string GetClientInfo(const std::string& PK) { std::string GetClientInfo(const std::string& PK) {
if (!PK.empty()) { if (!PK.empty()) {
return PostHTTP("https://auth.beammp.com/pkToUser", R"({"key":")"+PK+"\"}",true);; return PostHTTP("https://auth.beammp.com/pkToUser", R"({"key":")" + PK + "\"}", true);
;
} }
return ""; return "";
} }
@@ -38,7 +38,6 @@ void ClientKick(Client* c, const std::string& R){
CloseSocketProper(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
} }
void Authentication(SOCKET TCPSock) { void Authentication(SOCKET TCPSock) {
DebugPrintTID(); DebugPrintTID();
auto* c = CreateClient(TCPSock); auto* c = CreateClient(TCPSock);
@@ -109,7 +108,8 @@ void Authentication(SOCKET TCPSock) {
Client& Client = *c; Client& Client = *c;
CI->AddClient(std::move(c)); CI->AddClient(std::move(c));
TCPClient(&Client); TCPClient(&Client);
} else ClientKick(c,"Server full!"); } else
ClientKick(c, "Server full!");
} }
void HandleDownload(SOCKET TCPSock) { void HandleDownload(SOCKET TCPSock) {
@@ -136,7 +136,8 @@ void Identify(SOCKET TCPSock){
Authentication(TCPSock); Authentication(TCPSock);
} else if (Code == 'D') { } else if (Code == 'D') {
HandleDownload(TCPSock); HandleDownload(TCPSock);
}else CloseSocketProper(TCPSock); } else
CloseSocketProper(TCPSock);
} }
void TCPServerMain() { void TCPServerMain() {
+3 -3
View File
@@ -151,7 +151,8 @@ void SyncClient(Client* c) {
if (client.get() != c) { if (client.get() != c) {
for (auto& v : client->GetAllCars()) { for (auto& v : client->GetAllCars()) {
if (v != nullptr) { if (v != nullptr) {
if(c->GetStatus() < 0)return; if (c->GetStatus() < 0)
return;
Respond(c, v->Data, true); Respond(c, v->Data, true);
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
} }
@@ -238,8 +239,7 @@ void GlobalParser(Client* c, const std::string& Pack) {
#endif #endif
if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos) if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos)
break; break;
Res = TriggerLuaEvent("onChatMessage", false, nullptr,std::make_unique<LuaArg>(LuaArg { Res = TriggerLuaEvent("onChatMessage", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID(), c->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }), true);
{ c->GetID(), c->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }),true);
if (std::any_cast<int>(Res)) if (std::any_cast<int>(Res))
break; break;
SendToAll(nullptr, Packet, true, true); SendToAll(nullptr, Packet, true, true);
+2 -1
View File
@@ -43,7 +43,8 @@ std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json
Assert(curl); Assert(curl);
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
if(json)curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); if (json)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
+10 -7
View File
@@ -5,12 +5,12 @@
/// ///
/// Created by Anonymous275 on 8/1/2020 /// Created by Anonymous275 on 8/1/2020
/// ///
#include "Lua/LuaSystem.hpp"
#include "Client.hpp" #include "Client.hpp"
#include "UnixCompat.h"
#include "Settings.h"
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Lua/LuaSystem.hpp"
#include "Network.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <memory> #include <memory>
int OpenID() { int OpenID() {
@@ -87,8 +87,10 @@ void OnDisconnect(Client* c, bool kicked) {
SendToAll(c, Packet, false, true); SendToAll(c, Packet, false, true);
Packet.clear(); Packet.clear();
TriggerLuaEvent(("onPlayerDisconnect"), false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false); TriggerLuaEvent(("onPlayerDisconnect"), false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false);
if(c->GetTCPSock())CloseSocketProper(c->GetTCPSock()); if (c->GetTCPSock())
if(c->GetDownSock())CloseSocketProper(c->GetDownSock()); CloseSocketProper(c->GetTCPSock());
if (c->GetDownSock())
CloseSocketProper(c->GetDownSock());
CI->RemoveClient(c); CI->RemoveClient(c);
} }
void OnConnect(Client* c) { void OnConnect(Client* c) {
@@ -98,7 +100,8 @@ void OnConnect(Client* c) {
info("Assigned ID " + std::to_string(c->GetID()) + " to " + c->GetName()); info("Assigned ID " + std::to_string(c->GetID()) + " to " + c->GetName());
TriggerLuaEvent("onPlayerConnecting", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false); TriggerLuaEvent("onPlayerConnecting", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false);
SyncResources(c); SyncResources(c);
if (c->GetStatus() < 0)return; if (c->GetStatus() < 0)
return;
Respond(c, "M" + MapName, true); //Send the Map on connect Respond(c, "M" + MapName, true); //Send the Map on connect
info(c->GetName() + " : Connected"); info(c->GetName() + " : Connected");
TriggerLuaEvent("onPlayerJoining", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false); TriggerLuaEvent("onPlayerJoining", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false);
+30 -22
View File
@@ -6,13 +6,13 @@
/// Created by Anonymous275 on 8/1/2020 /// Created by Anonymous275 on 8/1/2020
/// ///
#include "Security/Enc.h"
#include "UnixCompat.h"
#include "Settings.h"
#include "Client.hpp" #include "Client.hpp"
#include <filesystem>
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Network.h"
#include "Security/Enc.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <filesystem>
#include <fstream> #include <fstream>
bool TCPSendRaw(SOCKET C, char* Data, int32_t Size) { bool TCPSendRaw(SOCKET C, char* Data, int32_t Size) {
@@ -32,21 +32,25 @@ bool TCPSendRaw(SOCKET C, char* Data, int32_t Size){
void SplitLoad(Client* c, int64_t Sent, int64_t Size, bool D, const std::string& Name) { void SplitLoad(Client* c, int64_t Sent, int64_t Size, bool D, const std::string& Name) {
std::ifstream f(Name.c_str(), std::ios::binary); std::ifstream f(Name.c_str(), std::ios::binary);
int32_t Split = 0x7735940; //125MB int32_t Split = 0x7735940; //125MB
int64_t Diff;
char* Data; char* Data;
if(Size > Split)Data = new char[Split]; if (Size > Split)
else Data = new char[Size]; Data = new char[Split];
else
Data = new char[Size];
SOCKET TCPSock; SOCKET TCPSock;
if(D)TCPSock = c->GetDownSock(); if (D)
else TCPSock = c->GetTCPSock(); TCPSock = c->GetDownSock();
else
TCPSock = c->GetTCPSock();
info("Split load Socket " + std::to_string(TCPSock)); info("Split load Socket " + std::to_string(TCPSock));
while (c->GetStatus() > -1 && Sent < Size) { while (c->GetStatus() > -1 && Sent < Size) {
Diff = Size - Sent; int64_t Diff = Size - Sent;
if (Diff > Split) { if (Diff > Split) {
f.seekg(Sent, std::ios_base::beg); f.seekg(Sent, std::ios_base::beg);
f.read(Data, Split); f.read(Data, Split);
if (!TCPSendRaw(TCPSock, Data, Split)) { if (!TCPSendRaw(TCPSock, Data, Split)) {
if(c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)
c->SetStatus(-1);
break; break;
} }
Sent += Split; Sent += Split;
@@ -54,7 +58,8 @@ void SplitLoad(Client*c,int64_t Sent,int64_t Size, bool D,const std::string& Nam
f.seekg(Sent, std::ios_base::beg); f.seekg(Sent, std::ios_base::beg);
f.read(Data, Diff); f.read(Data, Diff);
if (!TCPSendRaw(TCPSock, Data, int32_t(Diff))) { if (!TCPSendRaw(TCPSock, Data, int32_t(Diff))) {
if(c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)
c->SetStatus(-1);
break; break;
} }
Sent += Diff; Sent += Diff;
@@ -64,7 +69,6 @@ void SplitLoad(Client*c,int64_t Sent,int64_t Size, bool D,const std::string& Nam
f.close(); f.close();
} }
void SendFile(Client* c, const std::string& Name) { void SendFile(Client* c, const std::string& Name) {
Assert(c); Assert(c);
info(c->GetName() + " requesting : " + Name.substr(Name.find_last_of('/'))); info(c->GetName() + " requesting : " + Name.substr(Name.find_last_of('/')));
@@ -73,7 +77,8 @@ void SendFile(Client*c, const std::string& Name) {
TCPSend(c, "CO"); TCPSend(c, "CO");
warn("File " + Name + " could not be accessed!"); warn("File " + Name + " could not be accessed!");
return; return;
}else TCPSend(c, "AG"); } else
TCPSend(c, "AG");
///Wait for connections ///Wait for connections
int T = 0; int T = 0;
@@ -82,14 +87,13 @@ void SendFile(Client*c, const std::string& Name) {
T++; T++;
} }
if (c->GetDownSock() < 1) { if (c->GetDownSock() < 1) {
error("Client doesn't have a download socket!"); error("Client doesn't have a download socket!");
if(c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)
c->SetStatus(-1);
return; return;
} }
int64_t Size = std::filesystem::file_size(Name), MSize = Size / 2; int64_t Size = std::filesystem::file_size(Name), MSize = Size / 2;
std::thread Dt(SplitLoad, c, 0, MSize, false, Name); std::thread Dt(SplitLoad, c, 0, MSize, false, Name);
@@ -97,7 +101,8 @@ void SendFile(Client*c, const std::string& Name) {
SplitLoad(c, MSize, Size, true, Name); SplitLoad(c, MSize, Size, true, Name);
if(Dt.joinable())Dt.join(); if (Dt.joinable())
Dt.join();
} }
void Parse(Client* c, const std::string& Packet) { void Parse(Client* c, const std::string& Packet) {
@@ -115,7 +120,8 @@ void Parse(Client* c, const std::string& Packet) {
if (SubCode == 'R') { if (SubCode == 'R') {
debug("Sending Mod Info"); debug("Sending Mod Info");
std::string ToSend = FileList + FileSizes; std::string ToSend = FileList + FileSizes;
if (ToSend.empty())ToSend = "-"; if (ToSend.empty())
ToSend = "-";
TCPSend(c, ToSend); TCPSend(c, ToSend);
} }
return; return;
@@ -126,7 +132,8 @@ void Parse(Client* c, const std::string& Packet) {
void SyncResources(Client* c) { void SyncResources(Client* c) {
Assert(c); Assert(c);
if (c == nullptr)return; if (c == nullptr)
return;
#ifndef DEBUG #ifndef DEBUG
try { try {
#endif #endif
@@ -134,7 +141,8 @@ void SyncResources(Client* c) {
std::string Data; std::string Data;
while (c->GetStatus() > -1) { while (c->GetStatus() > -1) {
Data = TCPRcv(c); Data = TCPRcv(c);
if(Data == "Done")break; if (Data == "Done")
break;
Parse(c, Data); Parse(c, Data);
} }
#ifndef DEBUG #ifndef DEBUG
+5 -4
View File
@@ -5,16 +5,17 @@
/// ///
/// Created by Anonymous275 on 8/1/2020 /// Created by Anonymous275 on 8/1/2020
/// ///
#include "Compressor.h"
#include "Logger.h"
#include "Network.h"
#include "Security/Enc.h" #include "Security/Enc.h"
#include "UnixCompat.h" #include "UnixCompat.h"
#include "Compressor.h"
#include "Network.h"
#include "Logger.h"
#include <thread> #include <thread>
bool TCPSend(Client* c, const std::string& Data) { bool TCPSend(Client* c, const std::string& Data) {
Assert(c); Assert(c);
if (c == nullptr)return false; if (c == nullptr)
return false;
int32_t Size, Sent, Temp; int32_t Size, Sent, Temp;
std::string Send(4, 0); std::string Send(4, 0);
+6 -8
View File
@@ -6,18 +6,18 @@
/// Created by Anonymous275 on 5/8/2020 /// Created by Anonymous275 on 5/8/2020
/// ///
#include "Security/Enc.h"
#include "Compressor.h"
#include "UnixCompat.h"
#include "Client.hpp" #include "Client.hpp"
#include "Settings.h" #include "Compressor.h"
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Network.h"
#include "Security/Enc.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <cmath>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <cmath>
SOCKET UDPSock; SOCKET UDPSock;
void UDPSend(Client* c, std::string Data) { void UDPSend(Client* c, std::string Data) {
@@ -62,7 +62,6 @@ void UDPSend(Client* c, std::string Data) {
#endif // WIN32 #endif // WIN32
} }
void SendLarge(Client* c, std::string Data) { void SendLarge(Client* c, std::string Data) {
Assert(c); Assert(c);
if (Data.length() > 400) { if (Data.length() > 400) {
@@ -88,7 +87,6 @@ std::string UDPRcvFromClient(sockaddr_in& client) {
return Ret.substr(0, Rcv); return Ret.substr(0, Rcv);
} }
void UDPParser(Client* c, std::string Packet) { void UDPParser(Client* c, std::string Packet) {
if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) { if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) {
abort(); abort();