refactoring
Co-authored-by: n.fedorov <mail@nfedorov.dev> Co-committed-by: n.fedorov <mail@nfedorov.dev>
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
// Copyright (C) 2024-2026 NXST contributors
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <sys/stat.h>
|
||||
#include <vector>
|
||||
|
||||
#include <nxst/domain/account.hpp>
|
||||
#include <nxst/infra/fs/handles.hpp>
|
||||
|
||||
static std::map<AccountUid, User> 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<AccountUid> account::ids() {
|
||||
std::vector<AccountUid> 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<u8> 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);
|
||||
}
|
||||
Reference in New Issue
Block a user