#include "stdafx.h" #include "InputOutputStream.h" #include "PacketListener.h" #include "AuthResponsePacket.h" AuthResponsePacket::AuthResponsePacket() { } AuthResponsePacket::AuthResponsePacket(const wstring& chosenScheme, const wstring& mojangUuid, const wstring& username) { this->chosenScheme = chosenScheme; this->mojangUuid = mojangUuid; this->username = username; } void AuthResponsePacket::read(DataInputStream *dis) { chosenScheme = readUtf(dis, 32); mojangUuid = readUtf(dis, 64); username = readUtf(dis, 16); // MC usernames are max 16 chars if (chosenScheme != L"mojang" && chosenScheme != L"offline" && chosenScheme != L"elyby") chosenScheme = L""; // Validate username: only alphanumeric + underscore (standard MC rules) for (wchar_t c : username) { if (!((c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z') || (c >= L'0' && c <= L'9') || c == L'_')) { username = L""; break; } } } void AuthResponsePacket::write(DataOutputStream *dos) { writeUtf(chosenScheme, dos); writeUtf(mojangUuid, dos); writeUtf(username, dos); } void AuthResponsePacket::handle(PacketListener *listener) { listener->handleAuthResponse(shared_from_this()); } int AuthResponsePacket::getEstimatedSize() { return static_cast(3 * sizeof(short) + (chosenScheme.length() + mojangUuid.length() + username.length()) * sizeof(wchar_t)); }