// Copyright (C) 2024-2026 NXST contributors #include #include #include #include #include #include #include static std::map s_users; Result account::init() { Result res = accountInitialize(AccountServiceType_Application); if (R_FAILED(res)) return res; AccountUid uids[8]; s32 count = 0; accountListAllUsers(uids, 8, &count); for (s32 i = 0; i < count; ++i) { username(uids[i]); // populate cache } return 0; } void account::exit() { accountExit(); } std::vector account::ids() { std::vector result; result.reserve(s_users.size()); for (const auto& pair : s_users) { result.push_back(pair.second.id); } return result; } static User fetchUser(AccountUid id) { User user{id, ""}; nxst::AccountProfileHandle profile; AccountProfileBase base{}; if (R_SUCCEEDED(accountGetProfile(profile.get(), id))) { profile.valid = true; if (R_SUCCEEDED(accountProfileGet(profile.get(), nullptr, &base))) { user.name = std::string(base.nickname); } } return user; } std::string account::username(AccountUid id) { auto it = s_users.find(id); if (it == s_users.end()) { User user = fetchUser(id); s_users.emplace(id, user); return user.name; } return it->second.name; } std::string account::iconPath(AccountUid id) { char path[128]; snprintf(path, sizeof(path), "sdmc:/switch/NXST/cache/%016lX%016lX.jpg", id.uid[0], id.uid[1]); struct stat st; if (stat(path, &st) == 0 && st.st_size > 0) return std::string(path); mkdir("sdmc:/switch", 0755); mkdir("sdmc:/switch/NXST", 0755); mkdir("sdmc:/switch/NXST/cache", 0755); nxst::AccountProfileHandle profile; if (R_FAILED(accountGetProfile(profile.get(), id))) return ""; profile.valid = true; u32 img_size = 0; if (R_FAILED(accountProfileGetImageSize(profile.get(), &img_size)) || img_size == 0) return ""; std::vector buf(img_size); u32 out_size = 0; if (R_FAILED(accountProfileLoadImage(profile.get(), buf.data(), img_size, &out_size)) || out_size == 0) return ""; nxst::FileHandle f(fopen(path, "wb")); if (!f) return ""; fwrite(buf.data(), 1, out_size, f.get()); return std::string(path); }