1
0

Merge branch 'tabcompletion'

This commit is contained in:
madmaxoft 2013-08-01 09:36:33 +02:00
commit 3683601b8b
15 changed files with 406 additions and 221 deletions

View File

@ -193,6 +193,7 @@ enum
PACKET_PLAYER_LIST_ITEM = 0xc9,
PACKET_PLAYER_ABILITIES = 0xca,
PACKET_INCREMENT_STATISTIC = 0xc8,
PACKET_TAB_COMPLETION = 0xcb,
PACKET_LOCALE_AND_VIEW = 0xcc,
PACKET_CLIENT_STATUSES = 0xcd,
PACKET_PLUGIN_MESSAGE = 0xfa,
@ -579,6 +580,7 @@ bool cConnection::DecodeClientsPackets(const char * a_Data, int a_Size)
case PACKET_PLAYER_POSITION_LOOK: HANDLE_CLIENT_READ(HandleClientPlayerPositionLook); break;
case PACKET_PLUGIN_MESSAGE: HANDLE_CLIENT_READ(HandleClientPluginMessage); break;
case PACKET_SLOT_SELECT: HANDLE_CLIENT_READ(HandleClientSlotSelect); break;
case PACKET_TAB_COMPLETION: HANDLE_CLIENT_READ(HandleClientTabCompletion); break;
case PACKET_UPDATE_SIGN: HANDLE_CLIENT_READ(HandleClientUpdateSign); break;
case PACKET_USE_ENTITY: HANDLE_CLIENT_READ(HandleClientUseEntity); break;
case PACKET_WINDOW_CLICK: HANDLE_CLIENT_READ(HandleClientWindowClick); break;
@ -687,6 +689,7 @@ bool cConnection::DecodeServersPackets(const char * a_Data, int a_Size)
case PACKET_SPAWN_OBJECT_VEHICLE: HANDLE_SERVER_READ(HandleServerSpawnObjectVehicle); break;
case PACKET_SPAWN_PAINTING: HANDLE_SERVER_READ(HandleServerSpawnPainting); break;
case PACKET_SPAWN_PICKUP: HANDLE_SERVER_READ(HandleServerSpawnPickup); break;
case PACKET_TAB_COMPLETION: HANDLE_SERVER_READ(HandleServerTabCompletion); break;
case PACKET_TIME_UPDATE: HANDLE_SERVER_READ(HandleServerTimeUpdate); break;
case PACKET_UPDATE_HEALTH: HANDLE_SERVER_READ(HandleServerUpdateHealth); break;
case PACKET_UPDATE_SIGN: HANDLE_SERVER_READ(HandleServerUpdateSign); break;
@ -1072,6 +1075,19 @@ bool cConnection::HandleClientSlotSelect(void)
bool cConnection::HandleClientTabCompletion(void)
{
HANDLE_CLIENT_PACKET_READ(ReadBEUTF16String16, AString, Query);
Log("Received a PACKET_TAB_COMPLETION query from the client");
Log(" Query = \"%s\"", Query.c_str());
COPY_TO_SERVER();
return true;
}
bool cConnection::HandleClientUpdateSign(void)
{
HANDLE_CLIENT_PACKET_READ(ReadBEInt, int, BlockX);
@ -2103,6 +2119,34 @@ bool cConnection::HandleServerSpawnPickup(void)
bool cConnection::HandleServerTabCompletion(void)
{
HANDLE_SERVER_PACKET_READ(ReadBEUTF16String16, AString, Results);
Log("Received a PACKET_TAB_COMPLETION from the server, results given:");
// Parse the zero-terminated list of results:
size_t len = Results.size();
size_t last = 0;
for (size_t i = 0; i < len; i++)
{
if (Results[i] == 0)
{
Log(" \"%s\"", Results.substr(last, i - last).c_str());
last = i + 1;
}
}
if (last < len)
{
Log(" \"%s\"", Results.substr(last, len - last).c_str());
}
COPY_TO_CLIENT();
return true;
}
bool cConnection::HandleServerTimeUpdate(void)
{
HANDLE_SERVER_PACKET_READ(ReadBEInt64, Int64, WorldAge);

View File

@ -128,6 +128,7 @@ protected:
bool HandleClientPlayerPositionLook(void);
bool HandleClientPluginMessage(void);
bool HandleClientSlotSelect(void);
bool HandleClientTabCompletion(void);
bool HandleClientUpdateSign(void);
bool HandleClientUseEntity(void);
bool HandleClientWindowClick(void);
@ -177,6 +178,7 @@ protected:
bool HandleServerSpawnObjectVehicle(void);
bool HandleServerSpawnPainting(void);
bool HandleServerSpawnPickup(void);
bool HandleServerTabCompletion(void);
bool HandleServerTimeUpdate(void);
bool HandleServerUpdateHealth(void);
bool HandleServerUpdateSign(void);

View File

@ -1235,6 +1235,23 @@ void cClientHandle::HandleUnmount(void)
void cClientHandle::HandleTabCompletion(const AString & a_Text)
{
AStringVector Results;
m_Player->GetWorld()->TabCompleteUserName(a_Text, Results);
cRoot::Get()->GetPluginManager()->TabCompleteCommand(a_Text, Results, m_Player);
if (Results.empty())
{
return;
}
std::sort(Results.begin(), Results.end());
SendTabCompletionResults(Results);
}
void cClientHandle::SendData(const char * a_Data, int a_Size)
{
{
@ -1781,6 +1798,15 @@ void cClientHandle::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTy
void cClientHandle::SendTabCompletionResults(const AStringVector & a_Results)
{
m_Protocol->SendTabCompletionResults(a_Results);
}
void cClientHandle::SendTeleportEntity(const cEntity & a_Entity)
{
m_Protocol->SendTeleportEntity(a_Entity);

View File

@ -121,11 +121,12 @@ public:
void SendPlayerSpawn (const cPlayer & a_Player);
void SendRespawn (void);
void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch); // a_Src coords are Block * 8
void SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data);
void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data);
void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock);
void SendSpawnMob (const cMonster & a_Mob);
void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch);
void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType);
void SendTabCompletionResults(const AStringVector & a_Results);
void SendTeleportEntity (const cEntity & a_Entity);
void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ);
void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay);
@ -184,6 +185,7 @@ public:
bool HandleHandshake (const AString & a_Username);
void HandleEntityAction (int a_EntityID, char a_ActionID);
void HandleUnmount (void);
void HandleTabCompletion (const AString & a_Text);
/** Called when the protocol has finished logging the user in.
Return true to allow the user in; false to kick them.

View File

@ -1335,6 +1335,28 @@ bool cPluginManager::ExecuteConsoleCommand(const AStringVector & a_Split, cComma
void cPluginManager::TabCompleteCommand(const AString & a_Text, AStringVector & a_Results, cPlayer * a_Player)
{
for (CommandMap::iterator itr = m_Commands.begin(), end = m_Commands.end(); itr != end; ++itr)
{
if (NoCaseCompare(itr->first.substr(0, a_Text.length()), a_Text) != 0)
{
// Command name doesn't match
continue;
}
if ((a_Player != NULL) && !a_Player->HasPermission(itr->second.m_Permission))
{
// Player doesn't have permission for the command
continue;
}
a_Results.push_back(itr->first);
}
}
bool cPluginManager::AddPlugin(cPlugin * a_Plugin)
{
m_Plugins[a_Plugin->GetDirectory()] = a_Plugin;

View File

@ -197,6 +197,11 @@ public: // tolua_export
/// Executes the command split into a_Split, as if it was given on the console. Returns true if executed. Output is sent to the a_Output callback
bool ExecuteConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output);
/** Appends all commands beginning with a_Text (case-insensitive) into a_Results.
If a_Player is not NULL, only commands for which the player has permissions are added.
*/
void TabCompleteCommand(const AString & a_Text, AStringVector & a_Results, cPlayer * a_Player);
private:
friend class cRoot;

View File

@ -86,11 +86,12 @@ public:
virtual void SendPlayerSpawn (const cPlayer & a_Player) = 0;
virtual void SendRespawn (void) = 0;
virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) = 0; // a_Src coords are Block * 8
virtual void SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) = 0;
virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) = 0;
virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) = 0;
virtual void SendSpawnMob (const cMonster & a_Mob) = 0;
virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) = 0;
virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) = 0;
virtual void SendTabCompletionResults(const AStringVector & a_Results) = 0;
virtual void SendTeleportEntity (const cEntity & a_Entity) = 0;
virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) = 0;
virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) = 0;

View File

@ -782,6 +782,16 @@ void cProtocol125::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp
void cProtocol125::SendTabCompletionResults(const AStringVector & a_Results)
{
// This protocol version doesn't support tab completion
UNUSED(a_Results);
}
void cProtocol125::SendTeleportEntity(const cEntity & a_Entity)
{
cCSLock Lock(m_CSPacket);

View File

@ -63,11 +63,12 @@ public:
virtual void SendPlayerSpawn (const cPlayer & a_Player) override;
virtual void SendRespawn (void) override;
virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8
virtual void SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override;
virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override;
virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override;
virtual void SendSpawnMob (const cMonster & a_Mob) override;
virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override;
virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) override;
virtual void SendTabCompletionResults(const AStringVector & a_Results) override;
virtual void SendTeleportEntity (const cEntity & a_Entity) override;
virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override;
virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override;

View File

@ -70,6 +70,7 @@ enum
PACKET_BLOCK_BREAK_ANIM = 0x37,
PACKET_SOUND_EFFECT = 0x3e,
PACKET_SOUND_PARTICLE_EFFECT = 0x3d,
PACKET_TAB_COMPLETION = 0xcb,
PACKET_LOCALE_VIEW_DISTANCE = 0xcc,
PACKET_CLIENT_STATUSES = 0xcd,
PACKET_ENCRYPTION_KEY_RESP = 0xfc,
@ -426,6 +427,31 @@ void cProtocol132::SendSpawnMob(const cMonster & a_Mob)
void cProtocol132::SendTabCompletionResults(const AStringVector & a_Results)
{
if (a_Results.empty())
{
// No results to send
return;
}
AString Serialized(a_Results[0]);
for (AStringVector::const_iterator itr = a_Results.begin() + 1, end = a_Results.end(); itr != end; ++itr)
{
Serialized.push_back(0);
Serialized.append(*itr);
} // for itr - a_Results[]
cCSLock Lock(m_CSPacket);
WriteByte(PACKET_TAB_COMPLETION);
WriteString(Serialized);
Flush();
}
void cProtocol132::SendUnloadChunk(int a_ChunkX, int a_ChunkZ)
{
// Not used in 1.3.2
@ -484,9 +510,10 @@ int cProtocol132::ParsePacket(unsigned char a_PacketType)
switch (a_PacketType)
{
default: return super::ParsePacket(a_PacketType); // off-load previously known packets into cProtocol125
case PACKET_LOCALE_VIEW_DISTANCE: return ParseLocaleViewDistance();
case PACKET_CLIENT_STATUSES: return ParseClientStatuses();
case PACKET_ENCRYPTION_KEY_RESP: return ParseEncryptionKeyResponse();
case PACKET_LOCALE_VIEW_DISTANCE: return ParseLocaleViewDistance();
case PACKET_TAB_COMPLETION: return ParseTabCompletion();
}
}
@ -632,6 +659,17 @@ int cProtocol132::ParsePlayerAbilities(void)
int cProtocol132::ParseTabCompletion(void)
{
HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Text);
m_Client->HandleTabCompletion(Text);
return PARSE_OK;
}
void cProtocol132::SendData(const char * a_Data, int a_Size)
{
m_DataToSend.append(a_Data, a_Size);

View File

@ -40,8 +40,9 @@ public:
virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override;
virtual void SendPlayerSpawn (const cPlayer & a_Player) override;
virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8
virtual void SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override;
virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override;
virtual void SendSpawnMob (const cMonster & a_Mob) override;
virtual void SendTabCompletionResults(const AStringVector & a_Results) override;
virtual void SendUnloadChunk (int a_ChunkX, int a_ChunkZ) override;
virtual void SendWholeInventory (const cWindow & a_Window) override;
@ -60,6 +61,7 @@ public:
virtual int ParseClientStatuses (void);
virtual int ParseEncryptionKeyResponse(void);
virtual int ParseLocaleViewDistance (void);
virtual int ParseTabCompletion (void);
protected:
bool m_IsEncrypted;

View File

@ -511,6 +511,16 @@ void cProtocolRecognizer::SendSpawnVehicle(const cEntity & a_Vehicle, char a_Veh
void cProtocolRecognizer::SendTabCompletionResults(const AStringVector & a_Results)
{
ASSERT(m_Protocol != NULL);
m_Protocol->SendTabCompletionResults(a_Results);
}
void cProtocolRecognizer::SendTeleportEntity(const cEntity & a_Entity)
{
ASSERT(m_Protocol != NULL);

View File

@ -93,11 +93,12 @@ public:
virtual void SendPlayerSpawn (const cPlayer & a_Player) override;
virtual void SendRespawn (void) override;
virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override;
virtual void SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override;
virtual void SendSoundParticleEffect (int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) override;
virtual void SendSpawnFallingBlock (const cFallingBlock & a_FallingBlock) override;
virtual void SendSpawnMob (const cMonster & a_Mob) override;
virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override;
virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType) override;
virtual void SendTabCompletionResults(const AStringVector & a_Results) override;
virtual void SendTeleportEntity (const cEntity & a_Entity) override;
virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override;
virtual void SendTimeUpdate (Int64 a_WorldAge, Int64 a_TimeOfDay) override;

View File

@ -2364,6 +2364,24 @@ int cWorld::SpawnMob(double a_PosX, double a_PosY, double a_PosZ, int a_EntityTy
void cWorld::TabCompleteUserName(const AString & a_Text, AStringVector & a_Results)
{
cCSLock Lock(m_CSPlayers);
for (cPlayerList::iterator itr = m_Players.begin(), end = m_Players.end(); itr != end; ++itr)
{
if (NoCaseCompare((*itr)->GetName().substr(0, a_Text.length()), a_Text) != 0)
{
// Player name doesn't match
continue;
}
a_Results.push_back((*itr)->GetName());
}
}
cFluidSimulator * cWorld::InitializeFluidSimulator(cIniFile & a_IniFile, const char * a_FluidName, BLOCKTYPE a_SimulateBlock, BLOCKTYPE a_StationaryBlock)
{
AString SimulatorNameKey;

View File

@ -519,6 +519,9 @@ public:
/// Returns a random number from the m_TickRand in range [0 .. a_Range]. To be used only in the tick thread!
int GetTickRandomNumber(unsigned a_Range) { return (int)(m_TickRand.randInt(a_Range)); }
/// Appends all usernames starting with a_Text (case-insensitive) into Results
void TabCompleteUserName(const AString & a_Text, AStringVector & a_Results);
private:
friend class cRoot;