SHARE
TWEET

Untitled

a guest Nov 17th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "Server.hpp"
  3. #include "Debug.hpp"
  4.  
  5. #include "Player.hpp"
  6. #include "Packet.hpp"
  7. #include "TuningFromString.hpp"
  8. #include "amx/amx.h"
  9. #pragma comment(lib, "steam_api.lib")
  10. #define DEBUG_NOSTEAM
  11.  
  12. using boost::asio::ip::tcp;
  13.  
  14. Server::Server(json::value cfg) : m_acceptor(m_io)
  15. {
  16.     m_authEnabled = SteamGameServer_Init(INADDR_ANY, 1552, 1550, 1551, eServerModeAuthenticationAndSecure, "1.0.0.0");
  17.     if(!m_authEnabled)
  18.         Debug::LogWrite("WARNING: Can't create a Steam game server; authorization disabled");
  19.     else
  20.     {
  21.         SteamGameServer()->SetMaxPlayerCount(4);
  22.         SteamGameServer()->SetPasswordProtected(false);
  23.         SteamGameServer()->SetModDir("plakmp");
  24.         SteamGameServer()->SetProduct("Plak1");
  25.         SteamGameServer()->SetGameDescription("PlakMP Server");
  26.         SteamGameServer()->LogOnAnonymous();
  27.         SteamGameServer()->EnableHeartbeats(true);
  28.         while(!SteamGameServer()->BLoggedOn())
  29.             std::this_thread::sleep_for(std::chrono::milliseconds(100));
  30.         //SteamGameServer()->SetGameData("PlakMPServer"); // we use the generic sourcesdk app: use this to distinguish from other servers
  31.         SteamGameServer()->SetMapName("Rockport");
  32.         SteamGameServer()->SetServerName("PlakMPServer");
  33.         Debug::LogWrite("Steam Public IP: 0x%08X Logged on: %d SteamID: %lu", SteamGameServer()->GetPublicIP(), (int) SteamGameServer()->BLoggedOn(), SteamGameServer()->GetSteamID());
  34.     }
  35.  
  36.     m_port = uint16_t(cfg["serverPort"].as_int());
  37.  
  38.     Debug::LogWrite("Server name: %s", cfg["serverName"].as_string().c_str());
  39.     Debug::LogWrite("Max Players: %d", cfg["maxPlayers"].as_int());
  40.  
  41.     ReadStockCars(cfg["stockCarsPath"].as_string().c_str());
  42.  
  43.     m_delay = uint32_t(cfg["delay"].as_int());
  44.     m_connectionTimeout = uint32_t(cfg["connectionTimeout"].as_int());
  45.  
  46.     m_pSocketUDP = new boost::asio::ip::udp::socket(m_io);
  47.     m_pSocketUDP->non_blocking(true);
  48.     m_pRecvBufferUDP = (char*)malloc(65535);
  49.  
  50.     InitPlugins();
  51.  
  52.     m_stop = false;
  53. }
  54.  
  55. Server::~Server()
  56. {
  57.     if (m_authEnabled)
  58.         SteamGameServer_Shutdown();
  59.  
  60.     for (StockVehicle* pSV : m_stockCars)
  61.         delete pSV;
  62.  
  63.     if (m_acceptor.is_open())
  64.         m_acceptor.close();
  65. }
  66.  
  67. void Server::AddPlayer(Player * pPlayer)
  68. {
  69.     m_playerPtrs.push_back(pPlayer);
  70. }
  71.  
  72. void Server::DeletePlayer(Player * pPlayer)
  73. {
  74.     ScOnPlayerDisconnect(pPlayer->m_uid);
  75.     if (m_authEnabled) // Steam authorization
  76.     {
  77.         SteamGameServer()->EndAuthSession(pPlayer->m_steamID);
  78.     }
  79.  
  80.     if (!pPlayer->m_pending)
  81.         BroadcastPlayerLeave(pPlayer);
  82.  
  83.     m_playerPtrs.erase(std::find(m_playerPtrs.begin(), m_playerPtrs.end(), pPlayer));
  84.  
  85.     delete pPlayer;
  86. }
  87.  
  88. void Server::Accept(Player * pPlayer)
  89. {
  90.     pPlayer->m_pending = false; // player accepted!
  91.     Debug::LogWrite("Player %s joined the game", pPlayer->m_name.c_str());
  92.  
  93.     m_packet.Reset(DN_PLAYER_ACCEPTED);
  94.     m_packet.WriteInt(pPlayer->m_uid);
  95.     m_packet.WriteInt((uint32_t)pPlayer->m_pPlayerInfo->level);
  96.     m_packet.WriteInt(pPlayer->m_pPlayerInfo->money);
  97.     CSteamID id = SteamGameServer()->GetSteamID();
  98.     m_packet.WriteData(&id, 8);
  99.  
  100.     if (!pPlayer->SendPacket(&m_packet))
  101.     {
  102.         Debug::LogWrite("Can`t send accept packet to %s", pPlayer->m_name.c_str());
  103.         pPlayer->m_destroy = true;
  104.     }
  105.  
  106.     ScOnPlayerConnect(pPlayer->m_uid);
  107. }
  108.  
  109. void Server::Kick(Player * pPlayer, const std::string& reason)
  110. {
  111.     pPlayer->m_disconnectReason = "Kicked: " + reason;
  112.     //Debug::LogWrite("Player %s kicked for '%s'", pPlayer->m_name.c_str(), reason.c_str());
  113.  
  114.     pPlayer->m_destroy = true;
  115.  
  116.     m_packet.Reset(DN_DISCONNECT);
  117.     m_packet.WriteString(reason);
  118.  
  119.     pPlayer->SendPacket(&m_packet);
  120. }
  121.  
  122. void Server::Run()
  123. {
  124.     try
  125.     {
  126.         tcp::endpoint ep(tcp::v4(), m_port);
  127.         m_acceptor.open(ep.protocol());
  128.         m_acceptor.bind(ep);
  129.         m_acceptor.listen();
  130.         m_acceptor.non_blocking(true);
  131.     }
  132.     catch (const std::exception& ex)
  133.     {
  134.         Debug::LogWrite("PlakMP Server %s can't run on port %u, ex: ", SERVER_VERSION, m_port, ex.what());
  135.         return;
  136.     }
  137.  
  138.     udp::socket socket(m_io, udp::endpoint(udp::v4(), m_port));
  139.  
  140.     Debug::LogWrite("PlakMP Server %s running on port %u", SERVER_VERSION, m_port);
  141.  
  142.     std::vector<Player*> badGuys;
  143.  
  144.     tcp::socket* pSocket = new tcp::socket(m_io);
  145.     while (!m_stop)
  146.     {
  147.         boost::system::error_code ec;
  148.         m_acceptor.accept(*pSocket, ec);
  149.  
  150.         if (!ec)
  151.         {
  152.             Player* pPlayer = new Player(0, "CONNECTING", pSocket);
  153.             AddPlayer(pPlayer);
  154.             pSocket = new tcp::socket(m_io);
  155.         }
  156.        
  157.         size_t playerCount = m_playerPtrs.size();
  158.        
  159.  
  160.         for (size_t i = 0; i < playerCount; i++)
  161.         {
  162.             Player* pPlayer = m_playerPtrs[i];
  163.  
  164.             Packet* pPacket;
  165.             uint32_t result = pPlayer->RecvPacket(&pPacket);
  166.             if (result == 2) // disconnected
  167.             {
  168.                 //Debug::LogWrite("Disconnected %s", pPlayer->m_name.c_str());
  169.                 Debug::LogWrite("Technical disconnection of %s", pPlayer->m_name.c_str());
  170.                 badGuys.push_back(pPlayer);
  171.                 continue;
  172.             }
  173.             else if (result == 1) // no packets
  174.             {
  175.                 if (pPlayer->m_lastPacketTime && time(nullptr) - pPlayer->m_lastPacketTime > m_connectionTimeout)
  176.                 {
  177.                     Debug::LogWrite("Timeout of %s", pPlayer->m_name.c_str());
  178.                     pPlayer->m_disconnectReason = "Timeout/Crash";
  179.                     badGuys.push_back(pPlayer);
  180.                 }
  181.  
  182.                 continue;
  183.             }
  184.             pPlayer->m_lastPacketTime = time(nullptr);
  185.  
  186.             if (pPlayer->m_pending)
  187.             {
  188.                 if (pPacket->Type() == UP_LOGIN_PLAYER)
  189.                 {
  190.                     LoginHandler(pPlayer, pPacket);
  191.                 }
  192.                 else
  193.                 {
  194.                     Debug::LogWrite("Invalid login packet from %s", pPlayer->GetAddress().c_str());
  195.                     badGuys.push_back(pPlayer);
  196.                     continue;
  197.                 }
  198.             }
  199.             else
  200.             {
  201.                 //Debug::LogWrite("Received packet %d from %s", pPacket->Type(), pPlayer->m_name.c_str());
  202.                 switch (pPacket->Type())
  203.                 {
  204.                 case UP_DISCONNECT:
  205.                     Debug::LogWrite("Received disconncet from %s", pPlayer->m_name.c_str());
  206.                     pPlayer->m_destroy = true;
  207.                     break;
  208.                 case UP_CHAT:
  209.                     ChatHandler(pPlayer, pPacket);
  210.                     break;
  211.                 case UP_CAR_BUY:
  212.                     BuyCarHandler(pPlayer, pPacket);
  213.                     break;
  214.                 case UP_MAP_JOIN:
  215.                     MapJoinHandler(pPlayer);
  216.                     break;
  217.                 case UP_MAP_QUIT:
  218.                     MapQuitHandler(pPlayer);
  219.                     break;
  220.                 case UP_CAR_SELECT:
  221.                     CarSelectHandler(pPlayer, pPacket);
  222.                     break;
  223.                 case UP_NPC_SPAWN:
  224.                     NpcSpawnHandler(pPlayer, pPacket);
  225.                     break;
  226.                 case UP_NPC_DESPAWN:
  227.                     NpcDespawnHandler(pPlayer, pPacket);
  228.                     break;
  229.                 case UP_NPC_SYNC:
  230.                     NpcSyncHandler(pPlayer, pPacket);
  231.                     break;
  232.                 case UP_KEEPALIVE:
  233.                     break;
  234.                 case UP_OK:
  235.                     break;
  236.                 default:
  237.                     Kick(pPlayer, std::string("Invalid packet ") + std::to_string(pPacket->Type()));
  238.                     break;
  239.                 }
  240.             }
  241.  
  242.             if (pPlayer->m_destroy)
  243.             {
  244.                 badGuys.push_back(pPlayer);
  245.                 continue;
  246.             }
  247.         }
  248.  
  249.         for (Player* pBadGuy : badGuys)
  250.             DeletePlayer(pBadGuy);
  251.  
  252.         badGuys.clear();
  253.  
  254.         if (m_authEnabled)
  255.             SteamGameServer_RunCallbacks();
  256.  
  257.         udp::endpoint remote_endpoint;
  258.         boost::system::error_code error;
  259.         size_t size = socket.receive_from(boost::asio::buffer(m_pRecvBufferUDP, 65535),
  260.             remote_endpoint, 0, error);
  261.  
  262.         if (error && error != boost::asio::error::message_size)
  263.         {
  264.             Debug::LogWrite("error: %s", error.message().c_str());
  265.         }
  266.            
  267.         if (size > 4)
  268.         {
  269.             uint32_t packetSize = *(uint32_t*)m_pRecvBufferUDP;
  270.             if (packetSize == size)
  271.             {
  272.                 m_packetUDP.Update(m_pRecvBufferUDP, packetSize);
  273.                
  274.                 switch (m_packetUDP.Type())
  275.                 {
  276.                 case UP_SYNC:
  277.                     SyncHandler(&m_packetUDP);
  278.                     break;
  279.                 }
  280.             }
  281.         }
  282.  
  283.        
  284.         if (m_delay > 0)
  285.             std::this_thread::sleep_for(std::chrono::milliseconds(m_delay));
  286.     }
  287.  
  288.     // destroy all clients and kill their threads
  289.     Debug::LogWrite("The server is stopping");
  290.  
  291.     for (Player* pPlayer : m_playerPtrs)
  292.         pPlayer->m_destroy = true;
  293. }
  294.  
  295. void Server::PlayerWorkerThread(Server * pServer) { pServer->PlayerWorker(); }
  296.  
  297. void Server::PlayerWorker()
  298. {
  299.     std::vector<Player*> badGuys;
  300.  
  301.     while (true)
  302.     {
  303.         size_t playerCount = m_playerPtrs.size();
  304.  
  305.         if (playerCount == 0)
  306.             break;
  307.  
  308.         for (size_t i = 0; i < playerCount; i++)
  309.         {
  310.             Player* pPlayer = m_playerPtrs[i];
  311.  
  312.             Packet* pPacket;
  313.             uint32_t result = pPlayer->RecvPacket(&pPacket);
  314.             if (result == 2) // disconnected
  315.             {
  316.                 //Debug::LogWrite("Disconnected %s", pPlayer->m_name.c_str());
  317.                 badGuys.push_back(pPlayer);
  318.                 continue;
  319.             }
  320.             else if (result == 1) // no packets
  321.             {
  322.                 if (pPlayer->m_lastPacketTime && time(nullptr) - pPlayer->m_lastPacketTime > m_connectionTimeout)
  323.                 {
  324.                     pPlayer->m_disconnectReason = "Timeout/Crash";
  325.                     badGuys.push_back(pPlayer);
  326.                 }
  327.                    
  328.                 continue;
  329.             }
  330.             pPlayer->m_lastPacketTime = time(nullptr);
  331.            
  332.             if (pPlayer->m_pending)
  333.             {
  334.                 if (pPacket->Type() == UP_LOGIN_PLAYER)
  335.                 {
  336.                     LoginHandler(pPlayer, pPacket);
  337.                 }
  338.                 else
  339.                 {
  340.                     Debug::LogWrite("Invalid login packet from %s", pPlayer->GetAddress().c_str());
  341.                     badGuys.push_back(pPlayer);
  342.                     continue;
  343.                 }
  344.             }
  345.             else
  346.             {
  347.                 switch (pPacket->Type())
  348.                 {
  349.                 case UP_DISCONNECT:
  350.                     pPlayer->m_destroy = true;
  351.                     break;
  352.                 case UP_CHAT:
  353.                     ChatHandler(pPlayer, pPacket);
  354.                     break;
  355.                 case UP_CAR_BUY:
  356.                     BuyCarHandler(pPlayer, pPacket);
  357.                     break;
  358.                 case UP_MAP_JOIN:
  359.                     MapJoinHandler(pPlayer);
  360.                     break;
  361.                 case UP_MAP_QUIT:
  362.                     MapQuitHandler(pPlayer);
  363.                     break;
  364.                 case UP_CAR_SELECT:
  365.                     CarSelectHandler(pPlayer, pPacket);
  366.                     break;
  367.                 case UP_SYNC:
  368.                     SyncHandler(pPacket);
  369.                     break;
  370.                 case UP_NPC_SPAWN:
  371.                     NpcSpawnHandler(pPlayer, pPacket);
  372.                     break;
  373.                 case UP_NPC_DESPAWN:
  374.                     NpcDespawnHandler(pPlayer, pPacket);
  375.                     break;
  376.                 case UP_NPC_SYNC:
  377.                     NpcSyncHandler(pPlayer, pPacket);
  378.                     break;
  379.                 case UP_KEEPALIVE:
  380.                     break;
  381.                 case UP_OK:
  382.                     break;
  383.                 default:
  384.                     Kick(pPlayer, std::string("Invalid packet ") + std::to_string(pPacket->Type()));
  385.                     break;
  386.                 }
  387.             }
  388.  
  389.             if (pPlayer->m_destroy)
  390.             {
  391.                 badGuys.push_back(pPlayer);
  392.                 continue;
  393.             }
  394.         }
  395.        
  396.         for (Player* pBadGuy : badGuys)
  397.             DeletePlayer(pBadGuy);
  398.  
  399.         badGuys.clear();
  400.  
  401.  
  402.  
  403.         if (m_authEnabled)
  404.             SteamGameServer_RunCallbacks();
  405.         if (m_delay > 0)
  406.             std::this_thread::sleep_for(std::chrono::milliseconds(m_delay));
  407.     }
  408. }
  409.  
  410. void Server::LoginHandler(Player* pPlayer, Packet* pPacket)
  411. {
  412.     if (!pPlayer->m_pending)
  413.         Kick(pPlayer, "Are you already login");
  414.  
  415.     static uint32_t lastPlayerID = 1;
  416.  
  417.     if (pPacket->ReadString() != "FULL_PLAK_FULL_PLAK_FULL_PLAK_FULL_PLAK")
  418.     {
  419.         Debug::LogWrite("Incorrect connecting keyword.");
  420.         return;
  421.     }
  422.  
  423.     if (pPacket->ReadInt() != CSPVERSION)
  424.     {
  425.         Debug::LogWrite("Invalid protocol version");
  426.         return;
  427.     }
  428.  
  429.     pPlayer->m_name = pPacket->ReadString();
  430.     pPlayer->m_uid = lastPlayerID;
  431.    
  432. #ifndef DEBUG_NOSTEAM
  433.     if (m_authEnabled)
  434.     {
  435.         // steam auth
  436.         try {
  437.             uint64_t userID;
  438.  
  439.             pPacket->ReadData(&userID, sizeof(userID));
  440.             pPlayer->m_steamID = userID;
  441.  
  442.             CSteamID steamID(userID);
  443.             uint32_t ticketSize = pPacket->ReadInt();
  444.             void* pTicket = malloc(ticketSize);
  445.             pPacket->ReadData(pTicket, ticketSize);
  446.  
  447.             EBeginAuthSessionResult result = SteamGameServer()->BeginAuthSession(pTicket, ticketSize, steamID);
  448.             if (result != EBeginAuthSessionResult::k_EBeginAuthSessionResultOK)
  449.             {
  450.                 Kick(pPlayer, "Invalid Steam ticket");
  451.                 return;
  452.             }
  453.         }
  454.         catch(const Packet::Exception &e)
  455.         {
  456.             Kick(pPlayer, "Can't auth using Steam");
  457.             return;
  458.         }
  459.     }
  460. #endif
  461.  
  462.     // const char* szAccessToken = pPacket->ReadStringInside();
  463.  
  464.     pPlayer->AcquirePlayerInfo();
  465.     if (!pPlayer->m_pPlayerInfo)
  466.     {
  467.         Kick(pPlayer, "Can't read player info");
  468.         return;
  469.     }
  470.  
  471.     if (pPlayer->m_pPlayerInfo->isBanned)
  472.     {
  473.         Kick(pPlayer, "You have been banned from this server!");
  474.         return;
  475.     }
  476.  
  477.     lastPlayerID++;
  478.  
  479.     Accept(pPlayer);
  480.     if (pPlayer->m_destroy)
  481.         return;
  482.  
  483.     SendPlayers(pPlayer);
  484.     SendStockCars(pPlayer);
  485.  
  486.     SendCars(pPlayer);
  487.     if (pPlayer->m_destroy)
  488.         return;
  489.  
  490.     BroadcastPlayerJoin(pPlayer);
  491. }
  492.  
  493. void Server::BroadcastPacket(Packet * pPacket, Player* pIgnore)
  494. {
  495.     //LOCK();
  496.  
  497.     for (Player* pPlayer : m_playerPtrs)
  498.     {
  499.         if (!pPlayer->m_pending && pPlayer != pIgnore)
  500.         {
  501.             if (!pPlayer->SendPacket(pPacket))
  502.             {
  503.                 pPlayer->m_destroy = true;
  504.             }
  505.         }
  506.     }
  507.  
  508.     //UNLOCK();
  509. }
  510.  
  511. void Server::BroadcastPacketUDP(Packet * pPacket, Player* pIgnore)
  512. {
  513.     //LOCK();
  514.  
  515.     for (Player* pPlayer : m_playerPtrs)
  516.     {
  517.         if (!pPlayer->m_pending && pPlayer != pIgnore)
  518.         {
  519.             try
  520.             {
  521.                 m_pSocketUDP->send_to(pPacket->RawData(), pPlayer->m_endpoint);
  522.             }
  523.             catch (...)
  524.             {
  525.  
  526.             }
  527.         }
  528.     }
  529.  
  530.     //UNLOCK();
  531. }
  532.  
  533. void Server::ChatHandler(Player* pSender, Packet* pPacket)
  534. {
  535.     const char* szMessage;
  536.  
  537.     try
  538.     {
  539.         szMessage = pPacket->ReadStringInside();
  540.     }
  541.     catch (const Packet::Exception)
  542.     {
  543.         Debug::LogWrite("Invalid packet in " __FUNCTION__ " from %s", pSender->m_name.c_str());
  544.         pSender->m_destroy = true;
  545.         return;
  546.     }
  547.  
  548.     if (szMessage[0] == '/')
  549.         ScOnPlayerCommandText(pSender->m_uid, szMessage);
  550.     else
  551.     {
  552.         Debug::LogWrite("[%s]: %s", pSender->m_name.c_str(), szMessage);
  553.         BroadcastChatMessage(pSender->m_uid, szMessage);
  554.     }
  555. }
  556.  
  557. void Server::BuyCarHandler(Player * pSender, Packet * pPacket)
  558. {
  559.     uint32_t stockCID;
  560.  
  561.     try
  562.     {
  563.         stockCID = pPacket->ReadInt();
  564.     }
  565.     catch (const Packet::Exception)
  566.     {
  567.         Debug::LogWrite("Invalid packet in " __FUNCTION__ " from %s", pSender->m_name.c_str());
  568.         pSender->m_destroy = true;
  569.         return;
  570.     }
  571.  
  572.     Debug::LogWrite("Player %s bought car %d", pSender->m_name.c_str(), stockCID);
  573.  
  574.     for (StockVehicle* pStockVeh : m_stockCars)
  575.     {
  576.         if (pStockVeh->veh.carID == stockCID)
  577.         {
  578.             if (pSender->m_pPlayerInfo->money >= pStockVeh->price)
  579.             {
  580.                 pSender->m_pPlayerInfo->money -= pStockVeh->price;
  581.  
  582.                 // adding the car
  583.                 PlayableVehicle* pVeh = new PlayableVehicle;
  584.                 pVeh->carHash = pStockVeh->veh.carHash;
  585.                 memcpy(pVeh->customization, pStockVeh->veh.customization, kCustomizationSize);
  586.                 pVeh->carID = 1000 + pSender->m_pPlayerInfo->playerCars.size(); // guarantees the id to be unique
  587.                 pSender->m_pPlayerInfo->selectedCID = pVeh->carID;
  588.                 pSender->m_pPlayerInfo->playerCars.push_back(pVeh);
  589.                
  590.                 SendCars(pSender);
  591.                 if (pSender->m_destroy)
  592.                     return;
  593.  
  594.                 m_packet.Reset(DN_PLAYER_CARSELECT);
  595.                 m_packet.WriteInt(pSender->m_uid);
  596.                 m_packet.WriteInt(pVeh->carHash);
  597.                 m_packet.WriteData(pVeh->customization, kCustomizationSize);
  598.                 BroadcastPacket(&m_packet, pSender);
  599.  
  600.                 if (!pSender->WritePlayerInfo())
  601.                     pSender->m_destroy = true;
  602.                 if (pSender->m_destroy)
  603.                     return;
  604.             }
  605.             return;
  606.         }
  607.     }
  608. }
  609.  
  610. void Server::MapJoinHandler(Player * pSender)
  611. {
  612.     pSender->m_spawned = true;
  613.     ScOnPlayerSpawn(pSender->m_uid);
  614.  
  615.     m_packet.Reset(DN_MAP_JOIN);
  616.     m_packet.WriteInt(pSender->m_uid);
  617.     BroadcastPacket(&m_packet, pSender);
  618. }
  619.  
  620. void Server::MapQuitHandler(Player * pSender)
  621. {
  622.     pSender->m_spawned = false;
  623.     ScOnPlayerDespawn(pSender->m_uid);
  624.  
  625.     m_packet.Reset(DN_MAP_QUIT);
  626.     m_packet.WriteInt(pSender->m_uid);
  627.     BroadcastPacket(&m_packet, pSender);
  628. }
  629.  
  630. void Server::CarSelectHandler(Player * pSender, Packet* pPacket)
  631. {
  632.     uint32_t cid;
  633.  
  634.     try
  635.     {
  636.         cid = pPacket->ReadInt();
  637.     }
  638.     catch (const Packet::Exception)
  639.     {
  640.         Debug::LogWrite("Invalid packet in " __FUNCTION__ " from %s", pSender->m_name.c_str());
  641.         pSender->m_destroy = true;
  642.         return;
  643.     }
  644.  
  645.     if (pSender->m_pPlayerInfo)
  646.     {
  647.         for (PlayableVehicle* pVeh : pSender->m_pPlayerInfo->playerCars)
  648.         {
  649.             if (pVeh->carID == cid)
  650.             {
  651.                 pSender->m_pPlayerInfo->selectedCID = cid;
  652.                 m_packet.Reset(DN_PLAYER_CARSELECT);
  653.                 m_packet.WriteInt(pSender->m_uid);
  654.                 m_packet.WriteInt(pVeh->carHash);
  655.                 m_packet.WriteData(pVeh->customization, kCustomizationSize);
  656.                 BroadcastPacket(&m_packet, pSender);
  657.                 return;
  658.             }
  659.         }
  660.     }
  661. }
  662.  
  663. void Server::SyncHandler(Packet* pPacket)
  664. {
  665.     SyncPlayerData spd;
  666.  
  667.     try
  668.     {
  669.         pPacket->ReadData(&spd, sizeof(spd));
  670.     }
  671.     catch (const Packet::Exception)
  672.     {
  673.         Debug::LogWrite("Invalid packet in " __FUNCTION__ "");
  674.         return;
  675.     }
  676.  
  677.     Player* pSender = 0;
  678.     for (Player* pPlayer : m_playerPtrs)
  679.     {
  680.         if (pPlayer->m_uid == spd.uid)
  681.         {
  682.             pSender = pPlayer;
  683.         }
  684.     }
  685.  
  686.     if (pSender == 0)
  687.     {
  688.         Debug::LogWrite("pSender == 0");
  689.     }
  690.  
  691.     pSender->m_rbData = spd.rbd;
  692.  
  693.     m_packet.Reset(DN_MULTISYNC);
  694.     m_packet.WriteData(&spd, sizeof(spd));
  695.  
  696.     BroadcastPacketUDP(&m_packet, pSender);
  697. }
  698.  
  699. void Server::NpcSpawnHandler(Player * pSender, Packet * pPacket)
  700. {
  701.     m_packet.Reset(DN_NPC_SPAWN);
  702.     uint32_t npcid = pPacket->ReadInt();
  703.     uint32_t hash = pPacket->ReadInt();
  704.     m_packet.WriteInt(pSender->m_uid);
  705.     m_packet.WriteInt(npcid);
  706.     m_packet.WriteInt(hash);
  707.     BroadcastPacket(&m_packet, pSender);
  708. }
  709.  
  710. void Server::NpcDespawnHandler(Player * pSender, Packet * pPacket)
  711. {
  712.     m_packet.Reset(DN_NPC_DESPAWN);
  713.     uint32_t npcid = pPacket->ReadInt();
  714.     m_packet.WriteInt(pSender->m_uid);
  715.     m_packet.WriteInt(npcid);
  716.     BroadcastPacket(&m_packet, pSender);
  717. }
  718.  
  719. void Server::NpcSyncHandler(Player * pSender, Packet * pPacket)
  720. {
  721.     m_packet.Reset(DN_NPC_SYNC);
  722.     uint32_t npcid = pPacket->ReadInt();
  723.     SyncPlayerData spd;
  724.     try
  725.     {
  726.         pPacket->ReadData(&spd, sizeof(spd));
  727.     }
  728.     catch (const Packet::Exception)
  729.     {
  730.         Debug::LogWrite("Invalid packet in " __FUNCTION__ " from %s", pSender->m_name.c_str());
  731.         pSender->m_destroy = true;
  732.         return;
  733.     }
  734.     m_packet.WriteInt(pSender->m_uid);
  735.     m_packet.WriteInt(npcid);
  736.     m_packet.WriteData(&spd, sizeof(spd));
  737.     BroadcastPacket(&m_packet, pSender);
  738. }
  739.  
  740. void Server::SendCars(Player * pPlayer)
  741. {
  742.     if (!pPlayer->m_pPlayerInfo)
  743.         Kick(pPlayer, "Invalid player info");
  744.  
  745.     m_packet.Reset(DN_CARLIST);
  746.     m_packet.WriteInt(pPlayer->m_pPlayerInfo->selectedCID);
  747.     m_packet.WriteInt(pPlayer->m_pPlayerInfo->playerCars.size());
  748.  
  749.     for (PlayableVehicle* pVeh : pPlayer->m_pPlayerInfo->playerCars)
  750.     {
  751.         m_packet.WriteInt(pVeh->carID);
  752.         m_packet.WriteInt(pVeh->carHash);
  753.         m_packet.WriteData(pVeh->customization, kCustomizationSize);
  754.         m_packet.WriteInt(0);
  755.         m_packet.WriteInt(0);
  756.     }
  757.  
  758.     if (!pPlayer->SendPacket(&m_packet))
  759.         pPlayer->m_destroy = true;
  760. }
  761.  
  762. void Server::SendStockCars(Player * pPlayer)
  763. {
  764.     m_packet.Reset(DN_STOCK_CARS);
  765.     m_packet.WriteInt(m_stockCars.size()); // car count
  766.  
  767.     for (StockVehicle* pVeh : m_stockCars)
  768.     {
  769.         m_packet.WriteInt(pVeh->veh.carID);
  770.         m_packet.WriteInt(pVeh->veh.carHash);
  771.         m_packet.WriteData(pVeh->veh.customization, kCustomizationSize);
  772.         m_packet.WriteInt(pVeh->price); // price (unused)
  773.         m_packet.WriteInt(0); // arrests (unused)
  774.     }
  775.  
  776.     if (!pPlayer->SendPacket(&m_packet))
  777.         pPlayer->m_destroy = true;
  778. }
  779.  
  780. void Server::SendPlayers(Player * pPlayer)
  781. {
  782.     m_packet.Reset(DN_PLAYERS);
  783.  
  784.     m_packet.WriteInt(m_playerPtrs.size());
  785.     for (Player* pl : m_playerPtrs)
  786.     {
  787.         if (pl->m_pending || pl->m_uid == pPlayer->m_uid)
  788.         {
  789.             m_packet.WriteInt(0);
  790.             continue;
  791.         }
  792.  
  793.         m_packet.WriteInt(pl->m_uid);
  794.         m_packet.WriteInt(pl->m_pPlayerInfo->level);
  795.         m_packet.WriteInt(0);
  796.         m_packet.WriteInt(pl->m_name.size() + 1);
  797.         m_packet.WriteString(pl->m_name);
  798.        
  799.         if (pl->m_pPlayerInfo->selectedCID)
  800.         {
  801.             for (PlayableVehicle* pVeh : pl->m_pPlayerInfo->playerCars)
  802.             {
  803.                 if (pVeh->carID == pl->m_pPlayerInfo->selectedCID)
  804.                 {
  805.                     m_packet.WriteInt(pVeh->carHash);
  806.                     m_packet.WriteData(pVeh->customization, kCustomizationSize);
  807.                     break;
  808.                 }
  809.             }
  810.  
  811.             m_packet.WriteChar(pl->m_spawned);
  812.         }
  813.         else
  814.             m_packet.WriteInt(0);
  815.     }
  816.  
  817.     if (!pPlayer->SendPacket(&m_packet))
  818.         pPlayer->m_destroy = true;
  819. }
  820.  
  821. void Server::BroadcastPlayerJoin(Player * pPlayer)
  822. {
  823.     //BroadcastChatMessage(0, pPlayer->m_name + " joined the game");
  824.  
  825.     m_packet.Reset(DN_PLAYER_CONNECTED);
  826.  
  827.     m_packet.WriteInt(pPlayer->m_uid);
  828.     m_packet.WriteInt(pPlayer->m_pPlayerInfo->level);
  829.     m_packet.WriteInt(0);
  830.     m_packet.WriteInt(pPlayer->m_name.size() + 1);
  831.     m_packet.WriteString(pPlayer->m_name);
  832.  
  833.     if (pPlayer->m_pPlayerInfo->selectedCID)
  834.     {
  835.         for (PlayableVehicle* pVeh : pPlayer->m_pPlayerInfo->playerCars)
  836.         {
  837.             if (pVeh->carID == pPlayer->m_pPlayerInfo->selectedCID)
  838.             {
  839.                 m_packet.WriteInt(pVeh->carHash);
  840.                 m_packet.WriteData(pVeh->customization, kCustomizationSize);
  841.                 m_packet.WriteChar(pPlayer->m_spawned);
  842.                 break;
  843.             }
  844.         }
  845.     }
  846.     else
  847.         m_packet.WriteInt(0);
  848.  
  849.     BroadcastPacket(&m_packet, pPlayer);
  850. }
  851.  
  852. void Server::BroadcastPlayerLeave(Player * pPlayer)
  853. {
  854.     //BroadcastChatMessage(0, pPlayer->m_name + " left the game (" + pPlayer->m_disconnectReason + ")");
  855.  
  856.     m_packet.Reset(DN_PLAYER_DISCONNECTED);
  857.     m_packet.WriteInt(pPlayer->m_uid);
  858.     BroadcastPacket(&m_packet, pPlayer);
  859. }
  860.  
  861. void Server::BroadcastChatMessage(uint32_t uid, const std::string& message)
  862. {
  863.     m_packet.Reset(DN_CHAT);
  864.     m_packet.WriteInt(uid);
  865.     m_packet.WriteString(message.c_str());
  866.     BroadcastPacket(&m_packet);
  867. }
  868.  
  869. void Server::ReadStockCars(const char* pszFilePath)
  870. {
  871.     // Reading stock cars
  872.     json::value cars = json::from_file(pszFilePath);
  873.     if (cars.is_null())
  874.     {
  875.         Debug::LogWrite("Can`t read stock cars");
  876.         return;
  877.     }
  878.  
  879.     size_t carsCount = cars.array_size();
  880.     for (uint32_t i = 0; i < carsCount; i++)
  881.     {
  882.         json::value car = cars[i];
  883.  
  884.         StockVehicle* pVeh = new StockVehicle;
  885.         pVeh->veh.carID = car["cid"].as_int();
  886.         pVeh->veh.carHash = car["carHash"].as_int();
  887.         pVeh->price = car["price"].as_int();
  888.         TuningFromString(car["tuning"].as_string(), pVeh->veh.customization);
  889.  
  890.         m_stockCars.push_back(pVeh);
  891.     }
  892.  
  893.     json::release(cars);
  894.  
  895.     Debug::LogWrite("%d stock cars", m_stockCars.size());
  896. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top