diff --git a/include/Client.hpp b/include/Client.hpp index 120059b..bc20c33 100644 --- a/include/Client.hpp +++ b/include/Client.hpp @@ -23,7 +23,7 @@ struct VData{ class Client { private: - std::set VehicleData; //ID and Data; + std::set> VehicleData; //ID and Data; std::string Name = "Unknown Client"; sockaddr_in UDPADDR; std::string Role; @@ -39,7 +39,8 @@ public: void SetDID(const std::string& did); std::string GetCarData(int ident); void SetUDPAddr(sockaddr_in Addr); - std::set GetAllCars(); + std::set>& GetAllCars(); + const std::set>& GetAllCars() const; void SetTCPSock(SOCKET CSock); void SetStatus(int status); void DeleteCar(int ident); diff --git a/src/Lua/LuaSystem.cpp b/src/Lua/LuaSystem.cpp index cef2737..b84943f 100644 --- a/src/Lua/LuaSystem.cpp +++ b/src/Lua/LuaSystem.cpp @@ -314,7 +314,7 @@ int lua_GetCars(lua_State* L) { Client* c = GetClient(ID); if (c != nullptr) { int i = 1; - for (VData* v : c->GetAllCars()) { + for (auto& v : c->GetAllCars()) { if (v != nullptr) { lua_pushinteger(L, v->ID); lua_pushstring(L, v->Data.substr(3).c_str()); @@ -519,7 +519,12 @@ extern "C" { int lua_Print(lua_State* L) { int Arg = lua_gettop(L); for (int i = 1; i <= Arg; i++) { - ConsoleOut(lua_tostring(L, i) + std::string("\n")); + auto str = lua_tostring(L, i); + if (str != nullptr) { + ConsoleOut(str + std::string(Sec("\n"))); + } else { + ConsoleOut(Sec("nil\n")); + } } return 0; } diff --git a/src/Network/Client.cpp b/src/Network/Client.cpp index 8c7959b..b60c6a5 100644 --- a/src/Network/Client.cpp +++ b/src/Network/Client.cpp @@ -46,22 +46,14 @@ SOCKET Client::GetTCPSock(){ return TCPSOCK; } void Client::DeleteCar(int ident){ - for(VData* v : VehicleData){ + for(auto& v : VehicleData){ if(v != nullptr && v->ID == ident){ VehicleData.erase(v); - delete v; - v = nullptr; break; } } } void Client::ClearCars(){ - for(VData* v : VehicleData){ - if(v != nullptr){ - delete v; - v = nullptr; - } - } VehicleData.clear(); } int Client::GetOpenCarID(){ @@ -69,7 +61,7 @@ int Client::GetOpenCarID(){ bool found; do { found = true; - for (VData*v : VehicleData) { + for (auto& v : VehicleData) { if (v != nullptr && v->ID == OpenID){ OpenID++; found = false; @@ -79,14 +71,19 @@ int Client::GetOpenCarID(){ return OpenID; } void Client::AddNewCar(int ident,const std::string& Data){ - VehicleData.insert(new VData{ident,Data}); + VehicleData.insert(std::unique_ptr(new VData{ident,Data})); } -std::set Client::GetAllCars(){ +std::set>& Client::GetAllCars(){ return VehicleData; } + +const std::set> &Client::GetAllCars() const { + return VehicleData; +} + std::string Client::GetCarData(int ident){ - for(VData*v : VehicleData){ + for(auto& v : VehicleData){ if(v != nullptr && v->ID == ident){ return v->Data; } @@ -95,7 +92,7 @@ std::string Client::GetCarData(int ident){ return ""; } void Client::SetCarData(int ident,const std::string&Data){ - for(VData*v : VehicleData){ + for(auto& v : VehicleData){ if(v != nullptr && v->ID == ident){ v->Data = Data; return; diff --git a/src/Network/GParser.cpp b/src/Network/GParser.cpp index 9191735..1f408b7 100644 --- a/src/Network/GParser.cpp +++ b/src/Network/GParser.cpp @@ -128,7 +128,7 @@ void SyncClient(Client*c){ for (auto& client : CI->Clients) { if(client != nullptr){ if (client.get() != c) { - for (VData *v : client->GetAllCars()) { + for (auto& v : client->GetAllCars()) { if(v != nullptr){ Respond(c, v->Data, true); std::this_thread::sleep_for(std::chrono::seconds(2)); diff --git a/src/Network/InitClient.cpp b/src/Network/InitClient.cpp index 8e0bdbe..83c8ad5 100644 --- a/src/Network/InitClient.cpp +++ b/src/Network/InitClient.cpp @@ -63,7 +63,7 @@ void OnDisconnect(Client*c,bool kicked){ info(c->GetName() + Sec(" Connection Terminated")); if(c == nullptr)return; std::string Packet; - for(VData*v : c->GetAllCars()){ + for(auto& v : c->GetAllCars()){ if(v != nullptr) { Packet = "Od:" + std::to_string(c->GetID()) + "-" + std::to_string(v->ID); SendToAll(c, Packet, false, true);