AString cMojangAPI::MakeUUIDDashed(const AString & a_UUID) { // Note: we only check the string's length, not the actual content switch (a_UUID.size()) { case 36: { // Already is a dashed UUID, only lowercase return StrToLower(a_UUID); } case 32: { // Insert dashes at the proper positions: AString res; res.reserve(36); res.append(a_UUID, 0, 8); res.push_back('-'); res.append(a_UUID, 8, 4); res.push_back('-'); res.append(a_UUID, 12, 4); res.push_back('-'); res.append(a_UUID, 16, 4); res.push_back('-'); res.append(a_UUID, 20, 12); return StrToLower(res); } } LOGWARNING("%s: Not an UUID: \"%s\".", __FUNCTION__, a_UUID.c_str()); return ""; }
AString cProtocol125::GetEntityMetaData(const cEntity & a_Entity) { // We should send all the metadata here AString MetaData; // Common metadata (index 0, byte): MetaData.push_back(0); MetaData.push_back(GetEntityMetadataFlags(a_Entity)); // TODO: Add more entity-specific metadata MetaData.push_back(0x7f); // End metadata return MetaData; }
AString cWebAdmin::GetHTMLEscapedString(const AString & a_Input) { AString dst; dst.reserve(a_Input.length()); // Loop over input and substitute HTML characters for their alternatives: size_t len = a_Input.length(); for (size_t i = 0; i < len; i++) { switch (a_Input[i]) { case '&': dst.append("&"); break; case '\'': dst.append("'"); break; case '"': dst.append("""); break; case '<': dst.append("<"); break; case '>': dst.append(">"); break; default: { dst.push_back(a_Input[i]); break; } } // switch (a_Input[i]) } // for i - a_Input[] return dst; }
AStringVector cFile::GetFolderContents(const AString & a_Folder) { AStringVector AllFiles; #ifdef _WIN32 // If the folder name doesn't contain the terminating slash / backslash, add it: AString FileFilter = a_Folder; if ( !FileFilter.empty() && (FileFilter[FileFilter.length() - 1] != '\\') && (FileFilter[FileFilter.length() - 1] != '/') ) { FileFilter.push_back('\\'); } // Find all files / folders: FileFilter.append("*.*"); HANDLE hFind; WIN32_FIND_DATAA FindFileData; if ((hFind = FindFirstFileA(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE) { do { AllFiles.push_back(FindFileData.cFileName); } while (FindNextFileA(hFind, &FindFileData)); FindClose(hFind); } #else // _WIN32 DIR * dp; AString Folder = a_Folder; if (Folder.empty()) { Folder = "."; } if ((dp = opendir(Folder.c_str())) == nullptr) { LOGERROR("Error (%i) opening directory \"%s\"\n", errno, Folder.c_str()); } else { struct dirent *dirp; while ((dirp = readdir(dp)) != nullptr) { AllFiles.push_back(dirp->d_name); } closedir(dp); } #endif // else _WIN32 return AllFiles; }
AString cWebPlugin::SafeString( const AString & a_String ) { AString RetVal; for( unsigned int i = 0; i < a_String.size(); ++i ) { char c = a_String[i]; if( c == ' ' ) { c = '_'; } RetVal.push_back( c ); } return RetVal; }
AString cWebPlugin::SafeString(const AString & a_String) { AString RetVal; auto len = a_String.size(); RetVal.reserve(len); for (size_t i = 0; i < len; ++i) { char c = a_String[i]; if (c == ' ') { c = '_'; } RetVal.push_back(c); } return RetVal; }
void cForgeHandshake::BeginForgeHandshake(const AString & a_Name, const cUUID & a_UUID, const Json::Value & a_Properties) { ASSERT(m_IsForgeClient); m_Name = a_Name; m_UUID = a_UUID; m_Properties = a_Properties; static const std::array<AString, 5> Channels{{ "FML|HS", "FML", "FML|MP", "FML", "FORGE" }}; AString ChannelsString; for (auto & Channel: Channels) { ChannelsString.append(Channel); ChannelsString.push_back('\0'); } m_Client->SendPluginMessage("REGISTER", ChannelsString); SendServerHello(); }
bool cSocketInputStream::GetLineInternal(AString & a_OutLine) { a_OutLine.clear(); for (;;) { char c; if (recv(m_Socket, &c, 1, 0) != 1) { return false; } if (c == 10) { return true; } else if (c != 13) { a_OutLine.push_back(c); } } }
void cProtocolRecognizer::SendLengthlessServerPing(void) { AString Reply; cServer * Server = cRoot::Get()->GetServer(); switch (cRoot::Get()->GetPrimaryServerVersion()) { case PROTO_VERSION_1_2_5: case PROTO_VERSION_1_3_2: { // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3099#Server_List_Ping_.280xFE.29 Printf(Reply, "%s%s%i%s%i", Server->GetDescription().c_str(), cChatColor::Delimiter.c_str(), Server->GetNumPlayers(), cChatColor::Delimiter.c_str(), Server->GetMaxPlayers() ); break; } case PROTO_VERSION_1_4_2: case PROTO_VERSION_1_4_4: case PROTO_VERSION_1_4_6: case PROTO_VERSION_1_5_0: case PROTO_VERSION_1_5_2: case PROTO_VERSION_1_6_1: case PROTO_VERSION_1_6_2: case PROTO_VERSION_1_6_3: case PROTO_VERSION_1_6_4: { // The server list ping now has 1 more byte of "magic". Mojang just loves to complicate stuff. // http://wiki.vg/wiki/index.php?title=Protocol&oldid=3101#Server_List_Ping_.280xFE.29 // _X 2012_10_31: I know that this needn't eat the byte, since it still may be in transit. // Who cares? We're disconnecting anyway. m_Buffer.ResetRead(); if (m_Buffer.CanReadBytes(2)) { Byte val; m_Buffer.ReadByte(val); // Packet type - Serverlist ping m_Buffer.ReadByte(val); // 0x01 magic value ASSERT(val == 0x01); } // http://wiki.vg/wiki/index.php?title=Server_List_Ping&oldid=3100 AString NumPlayers; Printf(NumPlayers, "%d", Server->GetNumPlayers()); AString MaxPlayers; Printf(MaxPlayers, "%d", Server->GetMaxPlayers()); AString ProtocolVersionNum; Printf(ProtocolVersionNum, "%d", cRoot::Get()->GetPrimaryServerVersion()); AString ProtocolVersionTxt(GetVersionTextFromInt(cRoot::Get()->GetPrimaryServerVersion())); // Cannot use Printf() because of in-string NUL bytes. Reply = cChatColor::Delimiter; Reply.append("1"); Reply.push_back(0); Reply.append(ProtocolVersionNum); Reply.push_back(0); Reply.append(ProtocolVersionTxt); Reply.push_back(0); Reply.append(Server->GetDescription()); Reply.push_back(0); Reply.append(NumPlayers); Reply.push_back(0); Reply.append(MaxPlayers); break; } } // switch (m_PrimaryServerVersion) m_Client->Kick(Reply); }
void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * a_Data, size_t a_Size) { if (a_Size != 2) { SetError(Printf("Unexpected HandshakeAck packet length: %zu", a_Size)); return; } auto Phase = a_Data[1]; LOGD("Received client HandshakeAck with phase = %d", Phase); switch (Phase) { case ClientPhase::WAITINGSERVERDATA: { cByteBuffer Buf(1024); Buf.WriteBEInt8(Discriminator::RegistryData); // TODO: send real registry data bool HasMore = false; AString RegistryName = "potions"; UInt32 NumIDs = 0; UInt32 NumSubstitutions = 0; UInt32 NumDummies = 0; Buf.WriteBool(HasMore); Buf.WriteVarUTF8String(RegistryName); Buf.WriteVarInt32(NumIDs); Buf.WriteVarInt32(NumSubstitutions); Buf.WriteVarInt32(NumDummies); AString RegistryData; Buf.ReadAll(RegistryData); m_Client->SendPluginMessage("FML|HS", RegistryData); break; } case ClientPhase::WAITINGSERVERCOMPLETE: { LOGD("Client finished receiving registry data; acknowledging"); AString Ack; Ack.push_back(Discriminator::HandshakeAck); Ack.push_back(ServerPhase::WAITINGCACK); m_Client->SendPluginMessage("FML|HS", Ack); break; } case ClientPhase::PENDINGCOMPLETE: { LOGD("Client is pending completion; sending complete ack"); AString Ack; Ack.push_back(Discriminator::HandshakeAck); Ack.push_back(ServerPhase::COMPLETE); m_Client->SendPluginMessage("FML|HS", Ack); break; } case ClientPhase::COMPLETE: { // Now finish logging in m_Client->FinishAuthenticate(m_Name, m_UUID, m_Properties); break; } default: { SetError(Printf("Received unknown phase in Forge handshake acknowledgement: %d", Phase)); break; } } }