#include "stdafx.h" #include "UI.h" #include "UIScene_SocialMenu.h" #include "UICustomStrings.h" #include "SocialOnlineService.h" #include "../../Minecraft.h" #include "../../Textures.h" #include "../../Tesselator.h" #if defined(_WINDOWS64) || defined(__linux__) #include "../../Windows64/KeyboardMouseInput.h" #include "../../Windows64/Windows64_Xuid.h" extern wchar_t g_Win64UsernameW[17]; void Win64_SetUsernameOverride(const wstring &name); wstring Win64_GetLocalUsername(); void Win64_UseOnlineUsername(const wstring &name); void Win64_UseLocalUsername(); #endif namespace { const float kTabX = 756.0f; const float kTabY = 245.0f; const float kTabW = 399.0f; const float kTabH = 76.0f; const float kSkinTitleX = 440.0f; const float kSkinTitleY = 257.0f; const int kUsernameLimit = 16; const int kUsernameMinimum = 3; const int kStatusVisibleChars = 20; const unsigned long long kStatusScrollStepMs = 180ULL; const unsigned long long kStatusScrollPauseMs = 1500ULL; bool IsUsernameChar(wchar_t c) { return (c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z') || (c >= L'0' && c <= L'9') || c == L'_'; } wstring SanitizeUsername(const wstring &name) { wstring result; for (wchar_t c : name) { if (IsUsernameChar(c) && static_cast(result.length()) < kUsernameLimit) result += c; } return result; } const wchar_t *kOnlineProfileFile = L"online_profile.txt"; const wchar_t *kAcceptRequestsFile = L"accept_friend_requests.txt"; const wchar_t *kHideUUIDFile = L"hide_uuid.txt"; void GetSettingPath(const wchar_t *fileName, wchar_t *out, size_t outLen) { wchar_t exePath[MAX_PATH] = {}; GetModuleFileNameW(nullptr, exePath, MAX_PATH); wchar_t *lastSlash = wcsrchr(exePath, L'\\'); if (lastSlash) *(lastSlash + 1) = L'\0'; _snwprintf_s(out, outLen, _TRUNCATE, L"%ls%ls", exePath, fileName); } bool LoadSettingFlag(const wchar_t *fileName, bool defaultValue) { wchar_t path[MAX_PATH] = {}; GetSettingPath(fileName, path, MAX_PATH); FILE *f = nullptr; if (_wfopen_s(&f, path, L"r") != 0 || !f) return defaultValue; int c = fgetc(f); fclose(f); if (c == '1') return true; if (c == '0') return false; return defaultValue; } void SaveSettingFlag(const wchar_t *fileName, bool enabled) { wchar_t path[MAX_PATH] = {}; GetSettingPath(fileName, path, MAX_PATH); FILE *f = nullptr; if (_wfopen_s(&f, path, L"w") != 0 || !f) return; fputc(enabled ? '1' : '0', f); fclose(f); } const int s_defaultSkinTextures[] = { TN_MOB_CHAR, TN_MOB_CHAR1, TN_MOB_CHAR2, TN_MOB_CHAR3, TN_MOB_CHAR4, TN_MOB_CHAR5, TN_MOB_CHAR6, TN_MOB_CHAR7, TN_MOB_ALEX, TN_MOB_ALEX1, TN_MOB_ALEX2, TN_MOB_ALEX3, TN_MOB_ALEX4, TN_MOB_ALEX5, TN_MOB_ALEX6, TN_MOB_ALEX7, TN_MOB_DEVALEX, TN_MOB_DEVSTEVE, }; int GetDefaultSkinTexture(DWORD skinId) { int index = 0; if (!GET_IS_DLC_SKIN_FROM_BITMASK(skinId) && GET_UGC_SKIN_ID_FROM_BITMASK(skinId) == 0) { int defaultSkin = static_cast(GET_DEFAULT_SKIN_ID_FROM_BITMASK(skinId)); if (defaultSkin > eDefaultSkins_ServerSelected && defaultSkin < eDefaultSkins_Count) index = defaultSkin - eDefaultSkins_Skin0; } return s_defaultSkinTextures[index]; } void DrawSkinRect(Tesselator *t, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1) { t->begin(); t->vertexUV(x0, y1, 0.0f, u0, v1); t->vertexUV(x1, y1, 0.0f, u1, v1); t->vertexUV(x1, y0, 0.0f, u1, v0); t->vertexUV(x0, y0, 0.0f, u0, v0); t->end(); } } UIScene_SocialMenu::UIScene_SocialMenu(int iPad, void *initData, UILayer *parentLayer) : UIScene(iPad, parentLayer) { initialiseMovie(); m_activeTab = isInWorld() ? eTab_Friends : eTab_Profile; m_bInitialised = false; m_bNeedsFriendsRepopulate = false; m_bPendingAddFriend = false; m_bIgnoreInput = false; #if defined(_WINDOWS64) || defined(__linux__) m_username = g_Win64UsernameW; #endif m_labelProfileTab.init(L"Profile"); m_labelFriendsTab.init(L"Friends"); m_labelSkinTitle.init(m_username); m_labelUsername.init(L"Username"); IggyValueSetBooleanRS(m_editUsername.getIggyValuePath(), 0, "m_bDebugInput", true); m_editUsername.init(m_username, eControl_Username); m_editUsername.SetCharLimit(kUsernameLimit); #if defined(_WINDOWS64) || defined(__linux__) m_editUsername.setCharFilter(&IsUsernameChar); #endif m_bOnlineProfileOn = LoadSettingFlag(kOnlineProfileFile, false); m_bAcceptRequestsOn = LoadSettingFlag(kAcceptRequestsFile, true); m_bHideUUIDOn = LoadSettingFlag(kHideUUIDFile, false); m_checkOnlineProfile.init(L"Online Profile", eControl_OnlineProfile, m_bOnlineProfileOn); m_checkAcceptRequests.init(L"Accepting Friend Requests", eControl_AcceptRequests, m_bAcceptRequestsOn); m_checkHideUUID.init(L"Hide UUID", eControl_HideUUID, m_bHideUUIDOn); m_buttonLinkAccount.init(L"Link Discord Account", eControl_LinkAccount); m_bPendingUnlink = false; m_linkSecondsShown = -1; m_bWasConnecting = false; m_renameLockShown = -1; m_statusScrollOffset = 0; m_statusScrollDirection = 1; m_statusScrollNextMs = 0; m_nextFriendsRefreshMs = GetTickCount64() + 20000; m_friendsList.init(eControl_FriendsList); IggyValueSetS32RS(m_friendsList.getIggyValuePath(), 0, "m_iVerticalSpacing", 0); m_friendsSelection = eFriendsItem_AddFriend; populateFriendsList(); IggyValueSetBooleanRS(m_description.getIggyValuePath(), 0, "m_bHideScrollButtons", true); m_focusId = eControl_Username; } bool UIScene_SocialMenu::isOnlineActive() { return SocialOnlineService::HasSession() && m_bOnlineProfileOn; } void UIScene_SocialMenu::populateFriendsList() { bool online = isOnlineActive() && SocialOnlineService::HasFriendsData(); m_friends = online ? SocialOnlineService::GetFriends() : vector(); m_incoming = online ? SocialOnlineService::GetIncomingRequests() : vector(); m_friendNames.clear(); for (const auto &person : m_friends) m_friendNames.push_back(person.username); m_pendingRequests.clear(); for (const auto &person : m_incoming) m_pendingRequests.push_back(person.username); m_friendsList.clearList(); m_friendsSelectable.clear(); m_friendsList.AddNewButton(L"Add Friend", eFriendsItem_AddFriend); m_friendsSelectable.push_back(eFriendsItem_AddFriend); wstring pendingLabel = L"Pending Requests"; if (!m_pendingRequests.empty()) pendingLabel += L" (" + std::to_wstring(m_pendingRequests.size()) + L")"; m_friendsList.AddNewButton(pendingLabel, eFriendsItem_PendingRequests); m_friendsSelectable.push_back(eFriendsItem_PendingRequests); m_friendsList.AddNewLabel(L""); if (!m_bOnlineProfileOn || !SocialOnlineService::IsLinked()) { m_friendsList.AddNewLabel(L"Online profile required"); } else if (!online) { m_friendsList.AddNewLabel(SocialOnlineService::IsConnecting() || SocialOnlineService::HasSession() ? L"Loading friends..." : L"Social server unreachable"); } else { size_t onlineCount = 0; for (const auto &person : m_friends) { if (!person.status.empty() && person.status != L"offline") ++onlineCount; } m_friendsList.AddNewLabel(L"Friends List - " + std::to_wstring(onlineCount) + L"/" + std::to_wstring(m_friendNames.size()) + L" online"); for (size_t i = 0; i < m_friendNames.size(); ++i) { int id = eFriendsItem_FriendBase + static_cast(i); m_friendsList.AddNewButton(m_friendNames[i] + L" - " + SocialOnlineService::StatusLabel(m_friends[i].status), id); m_friendsSelectable.push_back(id); } } if (std::find(m_friendsSelectable.begin(), m_friendsSelectable.end(), m_friendsSelection) == m_friendsSelectable.end()) m_friendsSelection = eFriendsItem_AddFriend; } void UIScene_SocialMenu::focusFriendsList() { SetFocusToElement(eControl_FriendsList); IggyDataValue result; IggyDataValue value[1]; value[0].type = IGGY_DATATYPE_number; value[0].number = 2.0; IggyPlayerCallMethodRS(getMovie(), &result, IggyPlayerRootPath(getMovie()), m_funcFocusList, 1, value); m_friendsList.HighlightItem(m_friendsSelection); } void UIScene_SocialMenu::updateDescription() { m_shownMyStatus = SocialOnlineService::GetMyStatus(); wchar_t startTags[64]; swprintf(startTags, 64, L"", app.GetHTMLColour(eHTMLColor_White)); if (SocialOnlineService::GetLinkState() == SocialOnlineService::eLink_Starting) { m_description.setLabel(wstring(startTags) + L"Contacting the social server..." + L""); return; } if (SocialOnlineService::GetLinkState() == SocialOnlineService::eLink_Pending) { int seconds = SocialOnlineService::GetLinkSecondsLeft(); wchar_t timeBuffer[16]; swprintf(timeBuffer, 16, L"%d:%02d", seconds / 60, seconds % 60); m_description.setLabel(wstring(startTags) + L"In Discord, run:
/link " + SocialOnlineService::GetLinkCode() + L"

Expires in " + timeBuffer + L""); return; } bool online = SocialOnlineService::IsLinked() && m_bOnlineProfileOn; wstring uuid = L"Unknown"; #if defined(_WINDOWS64) || defined(__linux__) unsigned long long xuid = online ? SocialOnlineService::GetOnlineId() : static_cast(Win64Xuid::ResolvePersistentXuid()); wchar_t buffer[32]; swprintf(buffer, 32, L"%016llX", xuid); uuid = buffer; #endif if (m_bHideUUIDOn) uuid = L"Hidden"; const wchar_t *status = L"Offline (Not Linked)"; wstring shownStatus; if (online && SocialOnlineService::HasSession()) { shownStatus = SocialOnlineService::StatusLabel(SocialOnlineService::GetMyStatus()); status = shownStatus.c_str(); } else if (online && SocialOnlineService::IsConnecting()) status = L"Connecting..."; else if (online) status = L"Offline (Server Unreachable)"; else if (SocialOnlineService::IsLinked()) status = L"Offline"; wstring statusLine = wstring(L"Status: ") + status; if (statusLine != m_statusLine) { m_statusLine = statusLine; m_statusScrollOffset = 0; m_statusScrollDirection = 1; m_statusScrollNextMs = GetTickCount64() + kStatusScrollPauseMs; } wstring visibleStatus = m_statusLine; if (static_cast(m_statusLine.length()) > kStatusVisibleChars) visibleStatus = m_statusLine.substr(m_statusScrollOffset, kStatusVisibleChars); m_description.setLabel(wstring(startTags) + L"UUID:
" + uuid + L"

" + visibleStatus + L""); } void UIScene_SocialMenu::tickStatusScroll() { int maxOffset = static_cast(m_statusLine.length()) - kStatusVisibleChars; SocialOnlineService::ELinkState linkState = SocialOnlineService::GetLinkState(); if (maxOffset <= 0 || linkState == SocialOnlineService::eLink_Pending || linkState == SocialOnlineService::eLink_Starting) return; unsigned long long now = GetTickCount64(); if (now < m_statusScrollNextMs) return; m_statusScrollOffset += m_statusScrollDirection; bool atEnd = m_statusScrollOffset <= 0 || m_statusScrollOffset >= maxOffset; if (m_statusScrollOffset < 0) m_statusScrollOffset = 0; if (m_statusScrollOffset > maxOffset) m_statusScrollOffset = maxOffset; if (atEnd) { m_statusScrollDirection = -m_statusScrollDirection; m_statusScrollNextMs = now + kStatusScrollPauseMs; } else { m_statusScrollNextMs = now + kStatusScrollStepMs; } updateDescription(); } void UIScene_SocialMenu::updateLinkControls() { SocialOnlineService::ELinkState state = SocialOnlineService::GetLinkState(); bool linked = state == SocialOnlineService::eLink_Linked; if (!linked && m_bOnlineProfileOn) { m_checkOnlineProfile.setChecked(false); m_bOnlineProfileOn = false; SaveSettingFlag(kOnlineProfileFile, false); } m_checkOnlineProfile.SetEnable(linked); if (state == SocialOnlineService::eLink_Pending || state == SocialOnlineService::eLink_Starting) m_buttonLinkAccount.setLabel(L"Cancel Linking"); else if (linked) m_buttonLinkAccount.setLabel(L"Unlink Discord Account"); else m_buttonLinkAccount.setLabel(L"Link Discord Account"); m_linkSecondsShown = -1; updateDescription(); } void UIScene_SocialMenu::pressLinkButton() { SocialOnlineService::ELinkState state = SocialOnlineService::GetLinkState(); if (state == SocialOnlineService::eLink_Pending || state == SocialOnlineService::eLink_Starting) { SocialOnlineService::CancelLink(); updateLinkControls(); } else if (state == SocialOnlineService::eLink_Linked) { UINT options[2]; options[0] = UICustomStrings::Get(L"Unlink"); options[1] = IDS_CONFIRM_CANCEL; ui.RequestAlertMessage(UICustomStrings::Get(L"Unlink Discord Account"), UICustomStrings::Get(L"Unlinking turns off your online profile on this PC. You can link again at any time."), options, 2, m_iPad, &UIScene_SocialMenu::UnlinkConfirmCallback, this); } else if (SocialOnlineService::StartLink()) { updateLinkControls(); } } int UIScene_SocialMenu::UnlinkConfirmCallback(LPVOID lpParam, int iPad, const C4JStorage::EMessageResult result) { UIScene_SocialMenu *pClass = static_cast(lpParam); if (result == C4JStorage::EMessage_ResultAccept) pClass->m_bPendingUnlink = true; return 0; } void UIScene_SocialMenu::placeSkinTitle() { m_labelSkinTitle.setXPos(static_cast(kSkinTitleX)); m_labelSkinTitle.setYPos(static_cast(kSkinTitleY)); } bool UIScene_SocialMenu::usingOnlineName() { return SocialOnlineService::IsLinked() && m_bOnlineProfileOn && !SocialOnlineService::GetLinkedName().empty(); } wstring UIScene_SocialMenu::FormatWait(int seconds) { int hours = seconds / 3600; int minutes = (seconds % 3600 + 59) / 60; if (minutes == 60) { ++hours; minutes = 0; } if (hours > 0) return std::to_wstring(hours) + L"h " + std::to_wstring(minutes) + L"m"; return std::to_wstring(minutes < 1 ? 1 : minutes) + L"m"; } void UIScene_SocialMenu::updateUsernameLabel() { if (!usingOnlineName()) { m_labelUsername.setLabel(L"Username"); m_renameLockShown = -1; return; } int seconds = SocialOnlineService::GetRenameSecondsLeft(); m_renameLockShown = seconds > 0 ? (seconds + 59) / 60 : 0; if (seconds > 0) m_labelUsername.setLabel(L"Online Username (change in " + FormatWait(seconds) + L")"); else m_labelUsername.setLabel(L"Online Username"); } void UIScene_SocialMenu::syncUsernameMode() { #if defined(_WINDOWS64) || defined(__linux__) if (usingOnlineName()) { Win64_UseOnlineUsername(SocialOnlineService::GetLinkedName()); m_username = SocialOnlineService::GetLinkedName(); } else { Win64_UseLocalUsername(); m_username = Win64_GetLocalUsername(); } #endif if (m_editUsername.isDirectEditing()) m_editUsername.cancelDirectEdit(); m_labelSkinTitle.setLabel(m_username); m_editUsername.setLabel(m_username); updateUsernameLabel(); } bool UIScene_SocialMenu::checkUsernameEditable() { if (!usingOnlineName()) return true; if (!SocialOnlineService::HasSession()) { ui.PlayUISFX(eSFX_CraftFail); showMessage(L"Not Connected", L"Your online name can only be changed while connected to the social server."); return false; } int seconds = SocialOnlineService::GetRenameSecondsLeft(); if (seconds > 0) { ui.PlayUISFX(eSFX_CraftFail); showMessage(L"Name Recently Changed", L"Your online name was changed in the last 24 hours. You can change it again in " + FormatWait(seconds) + L"."); return false; } return true; } void UIScene_SocialMenu::applyUsername(const wstring &name) { wstring sanitized = SanitizeUsername(name); if (usingOnlineName()) { if (sanitized.empty() || sanitized == m_username) { m_editUsername.setLabel(m_username); return; } if (static_cast(sanitized.length()) < kUsernameMinimum || !checkUsernameEditable()) { ui.PlayUISFX(eSFX_CraftFail); m_editUsername.setLabel(m_username); return; } m_editUsername.setLabel(sanitized); m_labelUsername.setLabel(L"Online Username (saving...)"); SocialOnlineService::RenameAccount(sanitized); return; } if (!sanitized.empty() && static_cast(sanitized.length()) < kUsernameMinimum) { ui.PlayUISFX(eSFX_CraftFail); m_editUsername.setLabel(m_username); return; } #if defined(_WINDOWS64) || defined(__linux__) Win64_SetUsernameOverride(sanitized); m_username = Win64_GetLocalUsername(); #endif m_labelSkinTitle.setLabel(m_username); m_editUsername.setLabel(m_username); } int UIScene_SocialMenu::UsernameKeyboardCallback(LPVOID lpParam, const bool bRes) { UIScene_SocialMenu *pClass = static_cast(lpParam); pClass->m_bIgnoreInput = false; if (bRes) { uint16_t text[64]; ZeroMemory(text, sizeof(text)); #if defined(_WINDOWS64) || defined(__linux__) Win64_GetKeyboardText(text, 64); #endif wstring name; for (int i = 0; i < 63 && text[i]; ++i) name += static_cast(text[i]); pClass->applyUsername(name); } return 0; } void UIScene_SocialMenu::openKeyboard() { if (!checkUsernameEditable()) return; #if defined(_WINDOWS64) || defined(__linux__) if (g_KBMInput.IsKBMActive()) { m_editUsername.beginDirectEdit(kUsernameLimit); return; } UIKeyboardInitData kbData; kbData.title = usingOnlineName() ? L"Online Username" : L"Username"; kbData.defaultText = m_username.c_str(); kbData.maxChars = kUsernameLimit; kbData.callback = &UIScene_SocialMenu::UsernameKeyboardCallback; kbData.lpParam = this; kbData.pcMode = false; m_bIgnoreInput = true; ui.NavigateToScene(m_iPad, eUIScene_Keyboard, &kbData); #endif } int UIScene_SocialMenu::AddFriendKeyboardCallback(LPVOID lpParam, const bool bRes) { UIScene_SocialMenu *pClass = static_cast(lpParam); pClass->m_bIgnoreInput = false; if (bRes) { uint16_t text[64]; ZeroMemory(text, sizeof(text)); #if defined(_WINDOWS64) || defined(__linux__) Win64_GetKeyboardText(text, 64); #endif wstring name; for (int i = 0; i < 63 && text[i]; ++i) name += static_cast(text[i]); pClass->m_pendingFriendName = name; pClass->m_bPendingAddFriend = true; } return 0; } void UIScene_SocialMenu::openAddFriendKeyboard() { #if defined(_WINDOWS64) || defined(__linux__) UIKeyboardInitData kbData; kbData.title = L"Friend's Username"; kbData.defaultText = L""; kbData.maxChars = kUsernameLimit; kbData.callback = &UIScene_SocialMenu::AddFriendKeyboardCallback; kbData.lpParam = this; kbData.pcMode = g_KBMInput.IsKBMActive(); m_bIgnoreInput = true; ui.NavigateToScene(m_iPad, eUIScene_Keyboard, &kbData); #endif } void UIScene_SocialMenu::showMessage(const wchar_t *title, const wstring &body) { UINT options[1]; options[0] = IDS_CONFIRM_OK; ui.RequestAlertMessage(UICustomStrings::Get(title), UICustomStrings::Get(body.c_str()), options, 1, m_iPad); } void UIScene_SocialMenu::sendFriendRequest(const wstring &name) { wstring sanitized = SanitizeUsername(name); if (sanitized.empty() || sanitized.length() != name.length() || static_cast(sanitized.length()) < kUsernameMinimum) { ui.PlayUISFX(eSFX_CraftFail); showMessage(L"Invalid Friend", L"No player with that username could be found."); return; } if (!isOnlineActive()) { ui.PlayUISFX(eSFX_CraftFail); showMessage(L"Online Profile Required", L"Link your Discord account and turn on your online profile to add friends."); return; } SocialOnlineService::SendFriendRequest(sanitized); } void UIScene_SocialMenu::handleServiceEvent(const SocialOnlineService::Event &e) { switch (e.type) { case SocialOnlineService::eEvent_LinkStarted: updateLinkControls(); break; case SocialOnlineService::eEvent_Linked: ui.PlayUISFX(eSFX_Press); updateLinkControls(); syncUsernameMode(); showMessage(L"Account Linked", L"Linked to " + e.name + L". You can now turn on your online profile."); break; case SocialOnlineService::eEvent_LinkExpired: updateLinkControls(); showMessage(L"Link Code Expired", L"The link code expired before it was used. Press Link Discord Account to get a new one."); break; case SocialOnlineService::eEvent_LinkFailed: updateLinkControls(); if (e.status == L"unreachable") showMessage(L"Server Unreachable", L"Could not reach the social server at " + SocialOnlineService::GetApiUrl() + L"."); else showMessage(L"Linking Failed", L"The social server could not link this PC (" + e.status + L")."); break; case SocialOnlineService::eEvent_SessionReady: if (SocialOnlineService::HasAccountSettings() && SocialOnlineService::IsAcceptingRequests() != m_bAcceptRequestsOn) { m_checkAcceptRequests.setChecked(SocialOnlineService::IsAcceptingRequests()); m_bAcceptRequestsOn = SocialOnlineService::IsAcceptingRequests(); SaveSettingFlag(kAcceptRequestsFile, SocialOnlineService::IsAcceptingRequests()); } m_bNeedsFriendsRepopulate = true; syncUsernameMode(); updateDescription(); break; case SocialOnlineService::eEvent_SessionFailed: m_bNeedsFriendsRepopulate = true; syncUsernameMode(); updateDescription(); break; case SocialOnlineService::eEvent_RenameResult: if (e.status == L"ok") { syncUsernameMode(); showMessage(L"Name Changed", L"Your online name is now " + e.name + L"."); } else { ui.PlayUISFX(eSFX_CraftFail); syncUsernameMode(); if (e.status == L"taken") showMessage(L"Name Taken", L"That username is already in use."); else if (e.status == L"blocked") showMessage(L"Name Not Allowed", L"That username is not allowed."); else if (e.status == L"invalid") showMessage(L"Invalid Name", L"Usernames must be 3-16 letters, numbers or underscores."); else if (e.status == L"cooldown") showMessage(L"Name Recently Changed", L"You can change your online name again in " + FormatWait(SocialOnlineService::GetRenameSecondsLeft()) + L"."); else if (e.status != L"failed") showMessage(L"Name Not Changed", L"Your online name could not be changed (" + e.status + L")."); } break; case SocialOnlineService::eEvent_SessionRevoked: updateLinkControls(); syncUsernameMode(); m_bNeedsFriendsRepopulate = true; if (e.status == L"banned") showMessage(L"Account Banned", L"This online account has been banned."); else showMessage(L"Account Unlinked", L"This PC is no longer linked to your Discord account. Link it again to use your online profile."); break; case SocialOnlineService::eEvent_FriendsUpdated: m_bNeedsFriendsRepopulate = true; break; case SocialOnlineService::eEvent_FriendRequestResult: if (e.status == L"sent") showMessage(L"Friend Request Sent", L"Your friend request to " + e.name + L" has been sent."); else if (e.status == L"accepted") showMessage(L"Friend Added", e.name + L" had already sent you a request, so you are now friends."); else if (e.status == L"already_sent") showMessage(L"Friend Request Sent", L"You have already sent " + e.name + L" a friend request."); else if (e.status == L"already_friends") showMessage(L"Already Friends", L"You are already friends with " + e.name + L"."); else if (e.status == L"not_accepting") showMessage(L"Not Accepting Requests", e.name + L" is not accepting friend requests."); else if (e.status == L"self") showMessage(L"Invalid Friend", L"You cannot add yourself as a friend."); else if (e.status == L"you_blocked") showMessage(L"Player Blocked", L"You have blocked " + e.name + L". Unblock them with /friend unblock in Discord first."); else { ui.PlayUISFX(eSFX_CraftFail); showMessage(L"Invalid Friend", L"No player with that username could be found."); } break; case SocialOnlineService::eEvent_InviteResult: if (e.status == L"sent") showMessage(L"Invite Sent", L"Invited " + e.name + L" to your world."); else if (e.status == L"sent_offline") showMessage(L"Invite Sent", e.name + L" is offline. They'll get a Discord message about your invite."); else if (e.status == L"not_hosting") showMessage(L"Can't Invite", L"Host a world with Online Game turned on to invite friends."); else if (e.status == L"not_friends") showMessage(L"Can't Invite", L"You can only invite your friends."); else if (e.status == L"session") showMessage(L"Reconnecting", L"Your online session expired. Reconnecting, try again in a moment."); else showMessage(L"Can't Invite", L"The invite could not be sent. Try again in a moment."); break; case SocialOnlineService::eEvent_ActionFailed: if (e.status == L"unreachable") showMessage(L"Server Unreachable", L"Could not reach the social server. Try again in a moment."); else if (e.status == L"session") showMessage(L"Reconnecting", L"Your online session expired. Reconnecting, try again in a moment."); else showMessage(L"Something Went Wrong", L"The social server returned an error (" + e.status + L")."); break; default: break; } } wstring UIScene_SocialMenu::getMoviePath() { return L"SocialMenu"; } bool UIScene_SocialMenu::isInWorld() { Minecraft *pMinecraft = Minecraft::GetInstance(); return pMinecraft != nullptr && pMinecraft->level != nullptr; } void UIScene_SocialMenu::setActiveTab(int tab) { if (tab < 0) tab = eTab_Count - 1; if (tab >= eTab_Count) tab = 0; if (isInWorld()) tab = eTab_Friends; m_activeTab = tab; IggyDataValue result; IggyDataValue value[1]; value[0].type = IGGY_DATATYPE_number; value[0].number = static_cast(tab == eTab_Friends ? 2 : 0); IggyPlayerCallMethodRS(getMovie(), &result, IggyPlayerRootPath(getMovie()), m_funcSetActiveTab, 1, value); placeSkinTitle(); bool profile = tab == eTab_Profile; m_labelUsername.setVisible(profile); m_editUsername.setVisible(profile); m_checkOnlineProfile.setVisible(profile); m_checkAcceptRequests.setVisible(profile); m_checkHideUUID.setVisible(profile); m_buttonLinkAccount.setVisible(profile); if (profile) SetFocusToElement(m_focusId); else focusFriendsList(); updateTooltips(); } void UIScene_SocialMenu::tick() { if (!hasMovie() || (m_bInitialised && !bHasFocus)) { UIScene::tick(); return; } if (!m_bInitialised) { m_bInitialised = true; m_unusedTabOff.setVisible(false); m_unusedTab.setVisible(false); updateLinkControls(); syncUsernameMode(); setActiveTab(m_activeTab); updateDescription(); } tickStatusScroll(); if (m_bPendingUnlink) { m_bPendingUnlink = false; SocialOnlineService::Unlink(); updateLinkControls(); syncUsernameMode(); m_bNeedsFriendsRepopulate = true; } if (usingOnlineName()) { int seconds = SocialOnlineService::GetRenameSecondsLeft(); int minutes = seconds > 0 ? (seconds + 59) / 60 : 0; if (minutes != m_renameLockShown && !m_editUsername.isDirectEditing()) updateUsernameLabel(); } SocialOnlineService::Update(); if (m_activeTab == eTab_Friends && SocialOnlineService::HasSession() && GetTickCount64() >= m_nextFriendsRefreshMs) { m_nextFriendsRefreshMs = GetTickCount64() + 20000; SocialOnlineService::RefreshFriends(); } SocialOnlineService::Event serviceEvent; while (SocialOnlineService::PopEvent(serviceEvent)) handleServiceEvent(serviceEvent); if (SocialOnlineService::GetMyStatus() != m_shownMyStatus) updateDescription(); if (SocialOnlineService::GetLinkState() == SocialOnlineService::eLink_Pending) { int seconds = SocialOnlineService::GetLinkSecondsLeft(); if (seconds != m_linkSecondsShown) { m_linkSecondsShown = seconds; updateDescription(); } } bool connecting = SocialOnlineService::IsConnecting(); if (connecting != m_bWasConnecting) { m_bWasConnecting = connecting; updateDescription(); m_bNeedsFriendsRepopulate = true; } if (m_bPendingAddFriend) { m_bPendingAddFriend = false; sendFriendRequest(m_pendingFriendName); } if (m_bNeedsFriendsRepopulate) { m_bNeedsFriendsRepopulate = false; populateFriendsList(); if (m_activeTab == eTab_Friends) focusFriendsList(); } UIScene::tick(); } void UIScene_SocialMenu::FriendRequestResponded(void *param, const wstring &name, bool accept) { UIScene_SocialMenu *pClass = static_cast(param); for (const auto &person : pClass->m_incoming) { if (person.username == name) { SocialOnlineService::RespondToRequest(person.onlineId, accept); break; } } pClass->m_bNeedsFriendsRepopulate = true; } void UIScene_SocialMenu::handleReload() { UIScene::handleReload(); m_bInitialised = false; m_bNeedsFriendsRepopulate = true; } void UIScene_SocialMenu::handleGainFocus(bool navBack) { UIScene::handleGainFocus(navBack); if (navBack && m_activeTab == eTab_Friends) { SocialOnlineService::RefreshFriends(); m_bNeedsFriendsRepopulate = true; } } void UIScene_SocialMenu::FriendRemoved(void *param, int friendIndex) { UIScene_SocialMenu *pClass = static_cast(param); if (friendIndex >= 0 && friendIndex < static_cast(pClass->m_friends.size())) { SocialOnlineService::RemoveFriend(pClass->m_friends[friendIndex].onlineId); pClass->m_bNeedsFriendsRepopulate = true; } } void UIScene_SocialMenu::FriendInvited(void *param, int friendIndex) { UIScene_SocialMenu *pClass = static_cast(param); if (friendIndex >= 0 && friendIndex < static_cast(pClass->m_friends.size())) SocialOnlineService::SendInvite(pClass->m_friends[friendIndex].onlineId); } void UIScene_SocialMenu::FriendBlocked(void *param, int friendIndex) { UIScene_SocialMenu *pClass = static_cast(param); if (friendIndex >= 0 && friendIndex < static_cast(pClass->m_friends.size())) { SocialOnlineService::BlockUser(pClass->m_friends[friendIndex].onlineId); pClass->m_bNeedsFriendsRepopulate = true; } } void UIScene_SocialMenu::updateTooltips() { int a = IDS_TOOLTIPS_SELECT; ui.SetTooltips(m_iPad, a, IDS_TOOLTIPS_BACK, -1, -1, -1, -1, -2, UICustomStrings::Get(L"Select Tab")); } void UIScene_SocialMenu::updateComponents() { m_parentLayer->showComponent(m_iPad, eUIComponent_Panorama, !isInWorld()); m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, false); } void UIScene_SocialMenu::customDraw(IggyCustomDrawCallbackRegion *region) { if (wcscmp(static_cast(region->name), L"SkinFace") != 0) return; Minecraft *pMinecraft = Minecraft::GetInstance(); if (pMinecraft == nullptr || pMinecraft->textures == nullptr) return; CustomDrawData *customDrawRegion = ui.setupCustomDraw(this, region); DWORD skinId = app.GetPlayerSkinId(m_iPad); wstring skinPath = app.getSkinPathFromId(skinId); int backupTexture = GetDefaultSkinTexture(skinId); Textures *textures = pMinecraft->textures; int textureId = textures->loadMemTexture(skinPath, backupTexture); if (textureId >= 0) { textures->bind(textureId); int textureHeight = textures->getHeight(skinPath, backupTexture); float texH = textureHeight > 0 ? static_cast(textureHeight) : 64.0f; const float texW = 64.0f; float x0 = customDrawRegion->x0; float y0 = customDrawRegion->y0; float x1 = customDrawRegion->x1; float y1 = customDrawRegion->y1; RenderManager.StateSetBlendEnable(true); RenderManager.StateSetBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); Tesselator *t = Tesselator::getInstance(); DrawSkinRect(t, x0, y0, x1, y1, 8.0f / texW, 8.0f / texH, 16.0f / texW, 16.0f / texH); DrawSkinRect(t, x0, y0, x1, y1, 40.0f / texW, 8.0f / texH, 48.0f / texW, 16.0f / texH); } delete customDrawRegion; ui.endCustomDraw(region); } void UIScene_SocialMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled) { if (m_bIgnoreInput) return; #if defined(_WINDOWS64) || defined(__linux__) if (isDirectEditBlocking()) { handled = true; return; } #endif ui.AnimateKeyPress(iPad, key, repeat, pressed, released); switch (key) { case ACTION_MENU_CANCEL: if (pressed) navigateBack(); handled = true; break; case ACTION_MENU_LEFT_SCROLL: case ACTION_MENU_RIGHT_SCROLL: if (pressed && !repeat && !isInWorld()) { ui.PlayUISFX(eSFX_Focus); setActiveTab(m_activeTab + (key == ACTION_MENU_RIGHT_SCROLL ? 1 : -1)); } handled = true; break; case ACTION_MENU_OK: if (pressed && !repeat && m_activeTab == eTab_Friends) { handlePress(static_cast(eControl_FriendsList), static_cast(m_friendsSelection)); } else if (pressed && !repeat && m_activeTab == eTab_Profile) { UIControl_CheckBox *check = nullptr; if (m_focusId == eControl_OnlineProfile) check = &m_checkOnlineProfile; else if (m_focusId == eControl_AcceptRequests) check = &m_checkAcceptRequests; else if (m_focusId == eControl_HideUUID) check = &m_checkHideUUID; if (check != nullptr) { if (check->IsEnabled()) { ui.PlayUISFX(eSFX_Press); bool checked = !check->IsChecked(); check->setChecked(checked); handleCheckboxToggled(static_cast(m_focusId), checked); } else { ui.PlayUISFX(eSFX_CraftFail); } } else { handlePress(static_cast(m_focusId), 0); } } handled = true; break; case ACTION_MENU_UP: case ACTION_MENU_DOWN: if (pressed && m_activeTab == eTab_Profile) { const int order[] = { eControl_Username, eControl_OnlineProfile, eControl_AcceptRequests, eControl_HideUUID, eControl_LinkAccount }; const int count = static_cast(sizeof(order) / sizeof(order[0])); int current = 0; for (int i = 0; i < count; ++i) { if (order[i] == m_focusId) current = i; } int next = current + (key == ACTION_MENU_DOWN ? 1 : -1); if (next >= 0 && next < count) { ui.PlayUISFX(eSFX_Focus); m_focusId = order[next]; SetFocusToElement(m_focusId); } } else if (pressed && m_activeTab == eTab_Friends) { int count = static_cast(m_friendsSelectable.size()); if (count > 0) { int current = 0; for (int i = 0; i < count; ++i) { if (m_friendsSelectable[i] == m_friendsSelection) current = i; } int next = current + (key == ACTION_MENU_DOWN ? 1 : -1); if (next < 0) next = count - 1; if (next >= count) next = 0; ui.PlayUISFX(eSFX_Focus); m_friendsSelection = m_friendsSelectable[next]; m_friendsList.HighlightItem(m_friendsSelection); } } handled = true; break; } } void UIScene_SocialMenu::handlePress(F64 controlId, F64 childId) { if (m_bIgnoreInput) return; #if defined(_WINDOWS64) || defined(__linux__) if (isDirectEditBlocking()) return; #endif int id = static_cast(controlId); if (m_activeTab == eTab_Friends) { if (id == eControl_FriendsList) { if (std::find(m_friendsSelectable.begin(), m_friendsSelectable.end(), static_cast(childId)) != m_friendsSelectable.end()) m_friendsSelection = static_cast(childId); ui.PlayUISFX(eSFX_Press); #if defined(_WINDOWS64) || defined(__linux__) if (m_friendsSelection == eFriendsItem_AddFriend) { openAddFriendKeyboard(); return; } if (m_friendsSelection == eFriendsItem_PendingRequests) { if (!isOnlineActive()) { ui.PlayUISFX(eSFX_CraftFail); showMessage(L"Online Profile Required", L"Link your Discord account and turn on your online profile to see friend requests."); return; } m_pendingRequestsData.requests = &m_pendingRequests; m_pendingRequestsData.onRespond = &UIScene_SocialMenu::FriendRequestResponded; m_pendingRequestsData.param = this; ui.NavigateToScene(m_iPad, eUIScene_PendingRequestsMenu, &m_pendingRequestsData); return; } int friendIndex = m_friendsSelection - eFriendsItem_FriendBase; if (friendIndex >= 0 && friendIndex < static_cast(m_friendNames.size())) { m_friendMenuData.name = m_friendNames[friendIndex]; m_friendMenuData.onlineId = m_friends[friendIndex].onlineId; m_friendMenuData.status = m_friends[friendIndex].status; m_friendMenuData.friendOnline = false; m_friendMenuData.friendInWorld = false; m_friendMenuData.friendIndex = friendIndex; m_friendMenuData.onRemove = &UIScene_SocialMenu::FriendRemoved; m_friendMenuData.onBlock = &UIScene_SocialMenu::FriendBlocked; m_friendMenuData.onInvite = &UIScene_SocialMenu::FriendInvited; m_friendMenuData.param = this; ui.NavigateToScene(m_iPad, eUIScene_FriendMenu, &m_friendMenuData); } #endif } return; } if (id == eControl_Username) { ui.PlayUISFX(eSFX_Press); openKeyboard(); } else if (id == eControl_LinkAccount) { ui.PlayUISFX(eSFX_Press); pressLinkButton(); } } UIControl *UIScene_SocialMenu::GetMainPanel() { return m_activeTab == eTab_Friends ? &m_friendsPanel : nullptr; } void UIScene_SocialMenu::handleFocusChange(F64 controlId, F64 childId) { int id = static_cast(controlId); if (id == eControl_FriendsList && m_activeTab == eTab_Friends) { m_friendsList.updateChildFocus(static_cast(childId)); if (std::find(m_friendsSelectable.begin(), m_friendsSelectable.end(), static_cast(childId)) != m_friendsSelectable.end()) m_friendsSelection = static_cast(childId); } else if (m_activeTab == eTab_Profile && (id == eControl_Username || id == eControl_OnlineProfile || id == eControl_AcceptRequests || id == eControl_HideUUID || id == eControl_LinkAccount)) { m_focusId = id; } } void UIScene_SocialMenu::handleCheckboxToggled(F64 controlId, bool selected) { switch (static_cast(controlId)) { case eControl_OnlineProfile: m_bOnlineProfileOn = selected; SaveSettingFlag(kOnlineProfileFile, selected); if (selected) SocialOnlineService::RefreshFriends(); m_bNeedsFriendsRepopulate = true; syncUsernameMode(); updateDescription(); break; case eControl_AcceptRequests: m_bAcceptRequestsOn = selected; SaveSettingFlag(kAcceptRequestsFile, selected); if (isOnlineActive()) SocialOnlineService::SetAcceptingRequests(selected); break; case eControl_HideUUID: m_bHideUUIDOn = selected; SaveSettingFlag(kHideUUIDFile, selected); updateDescription(); break; } } #if defined(_WINDOWS64) || defined(__linux__) void UIScene_SocialMenu::getDirectEditInputs(vector &inputs) { if (m_activeTab == eTab_Profile) inputs.push_back(&m_editUsername); } void UIScene_SocialMenu::onDirectEditFinished(UIControl_TextInput *input, UIControl_TextInput::EDirectEditResult result) { if (input != &m_editUsername) return; if (result == UIControl_TextInput::eDirectEdit_Confirmed) applyUsername(input->getEditBuffer()); else m_editUsername.setLabel(m_username); } bool UIScene_SocialMenu::handleMouseClick(F32 x, F32 y) { if (y >= kTabY && y <= kTabY + kTabH && x >= kTabX && x <= kTabX + kTabW * eTab_Count) { int tab = static_cast((x - kTabX) / kTabW); if (!isInWorld() && tab != m_activeTab) { ui.PlayUISFX(eSFX_Focus); setActiveTab(tab); } return true; } return UIScene::handleMouseClick(x, y); } #endif