From 013da806ec20b62a742aded9a9d2b8131193f30d Mon Sep 17 00:00:00 2001 From: jfhumann Date: Fri, 18 Apr 2014 21:09:44 +0200 Subject: Did some static analysis, fixed some bugs and optimized a lot of code --- src/Protocol/Protocol125.cpp | 32 ++++++++------ src/Protocol/Protocol132.cpp | 18 +++++--- src/Protocol/Protocol14x.cpp | 12 +++--- src/Protocol/Protocol16x.cpp | 17 +++++--- src/Protocol/Protocol17x.cpp | 85 +++++++++++++++++++++---------------- src/Protocol/ProtocolRecognizer.cpp | 13 +++--- 6 files changed, 100 insertions(+), 77 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/Protocol125.cpp b/src/Protocol/Protocol125.cpp index bf946ef19..7ae359e10 100644 --- a/src/Protocol/Protocol125.cpp +++ b/src/Protocol/Protocol125.cpp @@ -537,9 +537,10 @@ void cProtocol125::SendHealth(void) { cCSLock Lock(m_CSPacket); WriteByte (PACKET_UPDATE_HEALTH); - WriteShort((short)m_Client->GetPlayer()->GetHealth()); - WriteShort((short)m_Client->GetPlayer()->GetFoodLevel()); - WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); + cPlayer * Player = m_Client->GetPlayer(); + WriteShort((short)Player->GetHealth()); + WriteShort((short)Player->GetFoodLevel()); + WriteFloat((float)Player->GetFoodSaturationLevel()); Flush(); } @@ -647,13 +648,14 @@ void cProtocol125::SendPickupSpawn(const cPickup & a_Pickup) cCSLock Lock(m_CSPacket); WriteByte (PACKET_PICKUP_SPAWN); WriteInt (a_Pickup.GetUniqueID()); - WriteShort (a_Pickup.GetItem().m_ItemType); - WriteChar (a_Pickup.GetItem().m_ItemCount); - WriteShort (a_Pickup.GetItem().m_ItemDamage); + const cItem & Item = a_Pickup.GetItem(); + WriteShort (Item.m_ItemType); + WriteChar (Item.m_ItemCount); + WriteShort (Item.m_ItemDamage); WriteVectorI((Vector3i)(a_Pickup.GetPosition() * 32)); - WriteByte ((char)(a_Pickup.GetSpeed().x * 8)); - WriteByte ((char)(a_Pickup.GetSpeed().y * 8)); - WriteByte ((char)(a_Pickup.GetSpeed().z * 8)); + WriteByte ((char)(a_Pickup.GetSpeedX() * 8)); + WriteByte ((char)(a_Pickup.GetSpeedY() * 8)); + WriteByte ((char)(a_Pickup.GetSpeedZ() * 8)); Flush(); } @@ -800,10 +802,11 @@ void cProtocol125::SendRemoveEntityEffect(const cEntity & a_Entity, int a_Effect void cProtocol125::SendRespawn(void) { cCSLock Lock(m_CSPacket); + cPlayer * Player = m_Client->GetPlayer(); WriteByte (PACKET_RESPAWN); - WriteInt ((int)(m_Client->GetPlayer()->GetWorld()->GetDimension())); + WriteInt ((int)(Player->GetWorld()->GetDimension())); WriteByte (2); // TODO: Difficulty; 2 = Normal - WriteChar ((char)m_Client->GetPlayer()->GetGameMode()); + WriteChar ((char)Player->GetGameMode()); WriteShort (256); // Current world height WriteString("default"); } @@ -815,10 +818,11 @@ void cProtocol125::SendRespawn(void) void cProtocol125::SendExperience(void) { cCSLock Lock(m_CSPacket); + cPlayer * Player = m_Client->GetPlayer(); WriteByte (PACKET_EXPERIENCE); - WriteFloat (m_Client->GetPlayer()->GetXpPercentage()); - WriteShort (m_Client->GetPlayer()->GetXpLevel()); - WriteShort (m_Client->GetPlayer()->GetCurrentXp()); + WriteFloat (Player->GetXpPercentage()); + WriteShort (Player->GetXpLevel()); + WriteShort (Player->GetCurrentXp()); Flush(); } diff --git a/src/Protocol/Protocol132.cpp b/src/Protocol/Protocol132.cpp index ce5942a5c..1ef92b8f0 100644 --- a/src/Protocol/Protocol132.cpp +++ b/src/Protocol/Protocol132.cpp @@ -408,7 +408,8 @@ void cProtocol132::SendWholeInventory(const cWindow & a_Window) super::SendWholeInventory(a_Window); // Send the player inventory and hotbar: - const cInventory & Inventory = m_Client->GetPlayer()->GetInventory(); + cPlayer * Player = m_Client->GetPlayer(); + const cInventory & Inventory = Player->GetInventory(); int BaseOffset = a_Window.GetNumSlots() - (cInventory::invNumSlots - cInventory::invInventoryOffset); // Number of non-inventory slots char WindowID = a_Window.GetWindowID(); for (short i = 0; i < cInventory::invInventoryCount; i++) @@ -422,7 +423,7 @@ void cProtocol132::SendWholeInventory(const cWindow & a_Window) } // for i - Hotbar[] // Send even the item being dragged: - SendInventorySlot(-1, -1, m_Client->GetPlayer()->GetDraggingItem()); + SendInventorySlot(-1, -1, Player->GetDraggingItem()); } @@ -800,10 +801,12 @@ void cProtocol132::SendCompass(const cWorld & a_World) void cProtocol132::SendEncryptionKeyRequest(void) { cCSLock Lock(m_CSPacket); + cServer * Server = cRoot::Get()->GetServer(); WriteByte(0xfd); - WriteString(cRoot::Get()->GetServer()->GetServerID()); - WriteShort((short)(cRoot::Get()->GetServer()->GetPublicKeyDER().size())); - SendData(cRoot::Get()->GetServer()->GetPublicKeyDER().data(), cRoot::Get()->GetServer()->GetPublicKeyDER().size()); + WriteString(Server->GetServerID()); + const AString & PublicKeyDER = Server->GetPublicKeyDER(); + WriteShort((short) (PublicKeyDER.size())); + SendData(PublicKeyDER.data(), PublicKeyDER.size()); WriteShort(4); WriteInt((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) Flush(); @@ -874,10 +877,11 @@ void cProtocol132::StartEncryption(const Byte * a_Key) // Prepare the m_AuthServerID: cSHA1Checksum Checksum; - AString ServerID = cRoot::Get()->GetServer()->GetServerID(); + cServer * Server = cRoot::Get()->GetServer(); + AString ServerID = Server->GetServerID(); Checksum.Update((const Byte *)ServerID.c_str(), ServerID.length()); Checksum.Update(a_Key, 16); - Checksum.Update((const Byte *)cRoot::Get()->GetServer()->GetPublicKeyDER().data(), cRoot::Get()->GetServer()->GetPublicKeyDER().size()); + Checksum.Update((const Byte *) Server->GetPublicKeyDER().data(), Server->GetPublicKeyDER().size()); Byte Digest[20]; Checksum.Finalize(Digest); cSHA1Checksum::DigestToJava(Digest, m_AuthServerID); diff --git a/src/Protocol/Protocol14x.cpp b/src/Protocol/Protocol14x.cpp index f694af1eb..d6a103d35 100644 --- a/src/Protocol/Protocol14x.cpp +++ b/src/Protocol/Protocol14x.cpp @@ -103,9 +103,9 @@ void cProtocol142::SendPickupSpawn(const cPickup & a_Pickup) WriteInt (a_Pickup.GetUniqueID()); WriteItem (a_Pickup.GetItem()); WriteVectorI((Vector3i)(a_Pickup.GetPosition() * 32)); - WriteChar ((char)(a_Pickup.GetSpeed().x * 8)); - WriteChar ((char)(a_Pickup.GetSpeed().y * 8)); - WriteChar ((char)(a_Pickup.GetSpeed().z * 8)); + WriteChar((char) (a_Pickup.GetSpeedX() * 8)); + WriteChar((char) (a_Pickup.GetSpeedY() * 8)); + WriteChar((char) (a_Pickup.GetSpeedZ() * 8)); Flush(); } @@ -170,9 +170,9 @@ void cProtocol146::SendPickupSpawn(const cPickup & a_Pickup) WriteInt ((int)(a_Pickup.GetPosY() * 32)); WriteInt ((int)(a_Pickup.GetPosZ() * 32)); WriteInt (1); - WriteShort((short)(a_Pickup.GetSpeed().x * 32)); - WriteShort((short)(a_Pickup.GetSpeed().y * 32)); - WriteShort((short)(a_Pickup.GetSpeed().z * 32)); + WriteShort((short)(a_Pickup.GetSpeedX() * 32)); + WriteShort((short) (a_Pickup.GetSpeedY() * 32)); + WriteShort((short) (a_Pickup.GetSpeedZ() * 32)); WriteByte(0); WriteByte(0); diff --git a/src/Protocol/Protocol16x.cpp b/src/Protocol/Protocol16x.cpp index 3da23a1dc..35bc8665d 100644 --- a/src/Protocol/Protocol16x.cpp +++ b/src/Protocol/Protocol16x.cpp @@ -131,9 +131,10 @@ void cProtocol161::SendHealth(void) { cCSLock Lock(m_CSPacket); WriteByte (PACKET_UPDATE_HEALTH); - WriteFloat((float)m_Client->GetPlayer()->GetHealth()); - WriteShort((short)m_Client->GetPlayer()->GetFoodLevel()); - WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); + cPlayer * Player = m_Client->GetPlayer(); + WriteFloat((float)Player->GetHealth()); + WriteShort((short)Player->GetFoodLevel()); + WriteFloat((float)Player->GetFoodSaturationLevel()); Flush(); } @@ -144,11 +145,12 @@ void cProtocol161::SendHealth(void) void cProtocol161::SendPlayerMaxSpeed(void) { cCSLock Lock(m_CSPacket); + cPlayer * Player = m_Client->GetPlayer(); WriteByte(PACKET_ENTITY_PROPERTIES); - WriteInt(m_Client->GetPlayer()->GetUniqueID()); + WriteInt(Player->GetUniqueID()); WriteInt(1); WriteString("generic.movementSpeed"); - WriteDouble(0.1 * m_Client->GetPlayer()->GetMaxSpeed()); + WriteDouble(0.1 * Player->GetMaxSpeed()); Flush(); } @@ -276,11 +278,12 @@ cProtocol162::cProtocol162(cClientHandle * a_Client) : void cProtocol162::SendPlayerMaxSpeed(void) { cCSLock Lock(m_CSPacket); + cPlayer * Player = m_Client->GetPlayer(); WriteByte(PACKET_ENTITY_PROPERTIES); - WriteInt(m_Client->GetPlayer()->GetUniqueID()); + WriteInt(Player->GetUniqueID()); WriteInt(1); WriteString("generic.movementSpeed"); - WriteDouble(0.1 * m_Client->GetPlayer()->GetMaxSpeed()); + WriteDouble(0.1 * Player->GetMaxSpeed()); WriteShort(0); Flush(); } diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index a4319df37..135518b67 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -514,9 +514,10 @@ void cProtocol172::SendGameMode(eGameMode a_GameMode) void cProtocol172::SendHealth(void) { cPacketizer Pkt(*this, 0x06); // Update Health packet - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetHealth()); - Pkt.WriteShort(m_Client->GetPlayer()->GetFoodLevel()); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetFoodSaturationLevel()); + cPlayer * Player = m_Client->GetPlayer(); + Pkt.WriteFloat((float)Player->GetHealth()); + Pkt.WriteShort(Player->GetFoodLevel()); + Pkt.WriteFloat((float)Player->GetFoodSaturationLevel()); } @@ -549,12 +550,13 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) { // Send the Join Game packet: { + cServer * Server = cRoot::Get()->GetServer(); cPacketizer Pkt(*this, 0x01); // Join Game packet Pkt.WriteInt(a_Player.GetUniqueID()); - Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (cRoot::Get()->GetServer()->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 + Pkt.WriteByte((Byte)a_Player.GetEffectiveGameMode() | (Server->IsHardcore() ? 0x08 : 0)); // Hardcore flag bit 4 Pkt.WriteChar((char)a_World.GetDimension()); Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte(std::min(cRoot::Get()->GetServer()->GetMaxPlayers(), 60)); + Pkt.WriteByte(std::min(Server->GetMaxPlayers(), 60)); Pkt.WriteString("default"); // Level type - wtf? } @@ -673,22 +675,23 @@ void cProtocol172::SendPlayerAbilities(void) { cPacketizer Pkt(*this, 0x39); // Player Abilities packet Byte Flags = 0; - if (m_Client->GetPlayer()->IsGameModeCreative()) + cPlayer * Player = m_Client->GetPlayer(); + if (Player->IsGameModeCreative()) { Flags |= 0x01; Flags |= 0x08; // Godmode, used for creative } - if (m_Client->GetPlayer()->IsFlying()) + if (Player->IsFlying()) { Flags |= 0x02; } - if (m_Client->GetPlayer()->CanFly()) + if (Player->CanFly()) { Flags |= 0x04; } Pkt.WriteByte(Flags); - Pkt.WriteFloat((float)(0.05 * m_Client->GetPlayer()->GetFlyingMaxSpeed())); - Pkt.WriteFloat((float)(0.1 * m_Client->GetPlayer()->GetMaxSpeed())); + Pkt.WriteFloat((float)(0.05 * Player->GetFlyingMaxSpeed())); + Pkt.WriteFloat((float)(0.1 * Player->GetMaxSpeed())); } @@ -739,17 +742,18 @@ void cProtocol172::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) void cProtocol172::SendPlayerMaxSpeed(void) { cPacketizer Pkt(*this, 0x20); // Entity Properties - Pkt.WriteInt(m_Client->GetPlayer()->GetUniqueID()); + cPlayer * Player = m_Client->GetPlayer(); + Pkt.WriteInt(Player->GetUniqueID()); Pkt.WriteInt(1); // Count Pkt.WriteString("generic.movementSpeed"); // The default game speed is 0.1, multiply that value by the relative speed: - Pkt.WriteDouble(0.1 * m_Client->GetPlayer()->GetNormalMaxSpeed()); - if (m_Client->GetPlayer()->IsSprinting()) + Pkt.WriteDouble(0.1 * Player->GetNormalMaxSpeed()); + if (Player->IsSprinting()) { Pkt.WriteShort(1); // Modifier count Pkt.WriteInt64(0x662a6b8dda3e4c1c); Pkt.WriteInt64(0x881396ea6097278d); // UUID of the modifier - Pkt.WriteDouble(m_Client->GetPlayer()->GetSprintingMaxSpeed() - m_Client->GetPlayer()->GetNormalMaxSpeed()); + Pkt.WriteDouble(Player->GetSprintingMaxSpeed() - Player->GetNormalMaxSpeed()); Pkt.WriteByte(2); } else @@ -765,16 +769,17 @@ void cProtocol172::SendPlayerMaxSpeed(void) void cProtocol172::SendPlayerMoveLook(void) { cPacketizer Pkt(*this, 0x08); // Player Position And Look packet - Pkt.WriteDouble(m_Client->GetPlayer()->GetPosX()); + cPlayer * Player = m_Client->GetPlayer(); + Pkt.WriteDouble(Player->GetPosX()); // Protocol docs say this is PosY, but #323 says this is eye-pos // Moreover, the "+ 0.001" is there because otherwise the player falls through the block they were standing on. - Pkt.WriteDouble(m_Client->GetPlayer()->GetStance() + 0.001); + Pkt.WriteDouble(Player->GetStance() + 0.001); - Pkt.WriteDouble(m_Client->GetPlayer()->GetPosZ()); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetYaw()); - Pkt.WriteFloat((float)m_Client->GetPlayer()->GetPitch()); - Pkt.WriteBool(m_Client->GetPlayer()->IsOnGround()); + Pkt.WriteDouble(Player->GetPosZ()); + Pkt.WriteFloat((float)Player->GetYaw()); + Pkt.WriteFloat((float)Player->GetPitch()); + Pkt.WriteBool(Player->IsOnGround()); } @@ -840,9 +845,10 @@ void cProtocol172::SendRemoveEntityEffect(const cEntity & a_Entity, int a_Effect void cProtocol172::SendRespawn(void) { cPacketizer Pkt(*this, 0x07); // Respawn packet - Pkt.WriteInt(m_Client->GetPlayer()->GetWorld()->GetDimension()); + cPlayer * Player = m_Client->GetPlayer(); + Pkt.WriteInt(Player->GetWorld()->GetDimension()); Pkt.WriteByte(2); // TODO: Difficulty (set to Normal) - Pkt.WriteByte((Byte)m_Client->GetPlayer()->GetEffectiveGameMode()); + Pkt.WriteByte((Byte)Player->GetEffectiveGameMode()); Pkt.WriteString("default"); } @@ -853,9 +859,10 @@ void cProtocol172::SendRespawn(void) void cProtocol172::SendExperience (void) { cPacketizer Pkt(*this, 0x1F); //Experience Packet - Pkt.WriteFloat(m_Client->GetPlayer()->GetXpPercentage()); - Pkt.WriteShort(m_Client->GetPlayer()->GetXpLevel()); - Pkt.WriteShort(m_Client->GetPlayer()->GetCurrentXp()); + cPlayer * Player = m_Client->GetPlayer(); + Pkt.WriteFloat(Player->GetXpPercentage()); + Pkt.WriteShort(Player->GetXpLevel()); + Pkt.WriteShort(Player->GetCurrentXp()); } @@ -1485,15 +1492,16 @@ void cProtocol172::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) { // Send the response: AString Response = "{\"version\":{\"name\":\"1.7.2\",\"protocol\":4},\"players\":{"; + cServer * Server = cRoot::Get()->GetServer(); AppendPrintf(Response, "\"max\":%u,\"online\":%u,\"sample\":[]},", - cRoot::Get()->GetServer()->GetMaxPlayers(), - cRoot::Get()->GetServer()->GetNumPlayers() + Server->GetMaxPlayers(), + Server->GetNumPlayers() ); AppendPrintf(Response, "\"description\":{\"text\":\"%s\"},", - cRoot::Get()->GetServer()->GetDescription().c_str() + Server->GetDescription().c_str() ); AppendPrintf(Response, "\"favicon\":\"data:image/png;base64,%s\"", - cRoot::Get()->GetServer()->GetFaviconData().c_str() + Server->GetFaviconData().c_str() ); Response.append("}"); @@ -1582,12 +1590,13 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) return; } + cServer * Server = cRoot::Get()->GetServer(); // If auth is required, then send the encryption request: - if (cRoot::Get()->GetServer()->ShouldAuthenticate()) + if (Server->ShouldAuthenticate()) { cPacketizer Pkt(*this, 0x01); - Pkt.WriteString(cRoot::Get()->GetServer()->GetServerID()); - const AString & PubKeyDer = cRoot::Get()->GetServer()->GetPublicKeyDER(); + Pkt.WriteString(Server->GetServerID()); + const AString & PubKeyDer = Server->GetPublicKeyDER(); Pkt.WriteShort(PubKeyDer.size()); Pkt.WriteBuf(PubKeyDer.data(), PubKeyDer.size()); Pkt.WriteShort(4); @@ -2136,10 +2145,11 @@ void cProtocol172::StartEncryption(const Byte * a_Key) // Prepare the m_AuthServerID: cSHA1Checksum Checksum; - const AString & ServerID = cRoot::Get()->GetServer()->GetServerID(); + cServer * Server = cRoot::Get()->GetServer(); + const AString & ServerID = Server->GetServerID(); Checksum.Update((const Byte *)ServerID.c_str(), ServerID.length()); Checksum.Update(a_Key, 16); - Checksum.Update((const Byte *)cRoot::Get()->GetServer()->GetPublicKeyDER().data(), cRoot::Get()->GetServer()->GetPublicKeyDER().size()); + Checksum.Update((const Byte *)Server->GetPublicKeyDER().data(), Server->GetPublicKeyDER().size()); Byte Digest[20]; Checksum.Finalize(Digest); cSHA1Checksum::DigestToJava(Digest, m_AuthServerID); @@ -2481,11 +2491,12 @@ void cProtocol172::cPacketizer::WriteEntityMetadata(const cEntity & a_Entity) if (((cMinecart &)a_Entity).GetPayload() == cMinecart::mpNone) { cRideableMinecart & RideableMinecart = ((cRideableMinecart &)a_Entity); - if (!RideableMinecart.GetContent().IsEmpty()) + const cItem & MinecartContent = RideableMinecart.GetContent(); + if (!MinecartContent.IsEmpty()) { WriteByte(0x54); - int Content = RideableMinecart.GetContent().m_ItemType; - Content |= RideableMinecart.GetContent().m_ItemDamage << 8; + int Content = MinecartContent.m_ItemType; + Content |= MinecartContent.m_ItemDamage << 8; WriteInt(Content); WriteByte(0x55); WriteInt(RideableMinecart.GetBlockHeight()); diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 3f7d7b254..f9a7ba92b 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -980,6 +980,7 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema void cProtocolRecognizer::SendLengthlessServerPing(void) { AString Reply; + cServer * Server = cRoot::Get()->GetServer(); switch (cRoot::Get()->GetPrimaryServerVersion()) { case PROTO_VERSION_1_2_5: @@ -987,11 +988,11 @@ void cProtocolRecognizer::SendLengthlessServerPing(void) { // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3099#Server_List_Ping_.280xFE.29 Printf(Reply, "%s%s%i%s%i", - cRoot::Get()->GetServer()->GetDescription().c_str(), + Server->GetDescription().c_str(), cChatColor::Delimiter.c_str(), - cRoot::Get()->GetServer()->GetNumPlayers(), + Server->GetNumPlayers(), cChatColor::Delimiter.c_str(), - cRoot::Get()->GetServer()->GetMaxPlayers() + Server->GetMaxPlayers() ); break; } @@ -1021,9 +1022,9 @@ void cProtocolRecognizer::SendLengthlessServerPing(void) // http://wiki.vg/wiki/index.php?title=Server_List_Ping&oldid=3100 AString NumPlayers; - Printf(NumPlayers, "%d", cRoot::Get()->GetServer()->GetNumPlayers()); + Printf(NumPlayers, "%d", Server->GetNumPlayers()); AString MaxPlayers; - Printf(MaxPlayers, "%d", cRoot::Get()->GetServer()->GetMaxPlayers()); + Printf(MaxPlayers, "%d", Server->GetMaxPlayers()); AString ProtocolVersionNum; Printf(ProtocolVersionNum, "%d", cRoot::Get()->GetPrimaryServerVersion()); @@ -1037,7 +1038,7 @@ void cProtocolRecognizer::SendLengthlessServerPing(void) Reply.push_back(0); Reply.append(ProtocolVersionTxt); Reply.push_back(0); - Reply.append(cRoot::Get()->GetServer()->GetDescription()); + Reply.append(Server->GetDescription()); Reply.push_back(0); Reply.append(NumPlayers); Reply.push_back(0); -- cgit v1.2.3