mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
Add more memory safety, fix print(nil) crash
This commit is contained in:
parent
b9758ddaea
commit
c6fbd3dc49
@ -23,7 +23,7 @@ struct VData{
|
||||
|
||||
class Client {
|
||||
private:
|
||||
std::set<VData*> VehicleData; //ID and Data;
|
||||
std::set<std::unique_ptr<VData>> 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<VData*> GetAllCars();
|
||||
std::set<std::unique_ptr<VData>>& GetAllCars();
|
||||
const std::set<std::unique_ptr<VData>>& GetAllCars() const;
|
||||
void SetTCPSock(SOCKET CSock);
|
||||
void SetStatus(int status);
|
||||
void DeleteCar(int ident);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<VData>(new VData{ident,Data}));
|
||||
}
|
||||
|
||||
std::set<VData*> Client::GetAllCars(){
|
||||
std::set<std::unique_ptr<VData>>& Client::GetAllCars(){
|
||||
return VehicleData;
|
||||
}
|
||||
|
||||
const std::set<std::unique_ptr<VData>> &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;
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user