summaryrefslogtreecommitdiffstats
path: root/src/core/hle
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle')
-rw-r--r--src/core/hle/service/acc/acc.cpp54
-rw-r--r--src/core/hle/service/acc/profile_manager.cpp147
-rw-r--r--src/core/hle/service/acc/profile_manager.h21
-rw-r--r--src/core/hle/service/am/am.cpp33
4 files changed, 226 insertions, 29 deletions
diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp
index e61748ca3..cf065c2e0 100644
--- a/src/core/hle/service/acc/acc.cpp
+++ b/src/core/hle/service/acc/acc.cpp
@@ -2,9 +2,13 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <algorithm>
#include <array>
+#include "common/common_paths.h"
#include "common/common_types.h"
+#include "common/file_util.h"
#include "common/logging/log.h"
+#include "common/string_util.h"
#include "common/swap.h"
#include "core/core_timing.h"
#include "core/hle/ipc_helpers.h"
@@ -16,6 +20,9 @@
#include "core/hle/service/acc/profile_manager.h"
namespace Service::Account {
+
+constexpr u32 MAX_JPEG_IMAGE_SIZE = 0x20000;
+
// TODO: RE this structure
struct UserData {
INSERT_PADDING_WORDS(1);
@@ -27,6 +34,11 @@ struct UserData {
};
static_assert(sizeof(UserData) == 0x80, "UserData structure has incorrect size");
+static std::string GetImagePath(UUID uuid) {
+ return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
+ "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
+}
+
class IProfile final : public ServiceFramework<IProfile> {
public:
explicit IProfile(UUID user_id, ProfileManager& profile_manager)
@@ -73,11 +85,11 @@ private:
}
void LoadImage(Kernel::HLERequestContext& ctx) {
- LOG_WARNING(Service_ACC, "(STUBBED) called");
+ LOG_DEBUG(Service_ACC, "called");
// smallest jpeg https://github.com/mathiasbynens/small/blob/master/jpeg.jpg
- // TODO(mailwl): load actual profile image from disk, width 256px, max size 0x20000
- constexpr u32 jpeg_size = 107;
- static constexpr std::array<u8, jpeg_size> jpeg{
+ // used as a backup should the one on disk not exist
+ constexpr u32 backup_jpeg_size = 107;
+ static constexpr std::array<u8, backup_jpeg_size> backup_jpeg{
0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04,
0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a,
@@ -87,18 +99,42 @@ private:
0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08, 0x01, 0x01,
0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
};
- ctx.WriteBuffer(jpeg);
+
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(RESULT_SUCCESS);
- rb.Push<u32>(jpeg_size);
+
+ const FileUtil::IOFile image(GetImagePath(user_id), "rb");
+
+ if (!image.IsOpen()) {
+ LOG_WARNING(Service_ACC,
+ "Failed to load user provided image! Falling back to built-in backup...");
+ ctx.WriteBuffer(backup_jpeg);
+ rb.Push<u32>(backup_jpeg_size);
+ } else {
+ const auto size = std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE);
+ std::vector<u8> buffer(size);
+ image.ReadBytes(buffer.data(), buffer.size());
+
+ ctx.WriteBuffer(buffer.data(), buffer.size());
+ rb.Push<u32>(buffer.size());
+ }
}
void GetImageSize(Kernel::HLERequestContext& ctx) {
- LOG_WARNING(Service_ACC, "(STUBBED) called");
- constexpr u32 jpeg_size = 107;
+ LOG_DEBUG(Service_ACC, "called");
+ constexpr u32 backup_jpeg_size = 107;
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(RESULT_SUCCESS);
- rb.Push<u32>(jpeg_size);
+
+ const FileUtil::IOFile image(GetImagePath(user_id), "rb");
+
+ if (!image.IsOpen()) {
+ LOG_WARNING(Service_ACC,
+ "Failed to load user provided image! Falling back to built-in backup...");
+ rb.Push<u32>(backup_jpeg_size);
+ } else {
+ rb.Push<u32>(std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE));
+ }
}
const ProfileManager& profile_manager;
diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp
index bcb3475db..06f7d1b15 100644
--- a/src/core/hle/service/acc/profile_manager.cpp
+++ b/src/core/hle/service/acc/profile_manager.cpp
@@ -4,32 +4,57 @@
#include <random>
#include <boost/optional.hpp>
+#include "common/file_util.h"
#include "core/hle/service/acc/profile_manager.h"
#include "core/settings.h"
namespace Service::Account {
+
+struct UserRaw {
+ UUID uuid;
+ UUID uuid2;
+ u64 timestamp;
+ ProfileUsername username;
+ INSERT_PADDING_BYTES(0x80);
+};
+static_assert(sizeof(UserRaw) == 0xC8, "UserRaw has incorrect size.");
+
+struct ProfileDataRaw {
+ INSERT_PADDING_BYTES(0x10);
+ std::array<UserRaw, MAX_USERS> users;
+};
+static_assert(sizeof(ProfileDataRaw) == 0x650, "ProfileDataRaw has incorrect size.");
+
// TODO(ogniK): Get actual error codes
constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1);
constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2);
constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20);
-const UUID& UUID::Generate() {
+constexpr char ACC_SAVE_AVATORS_BASE_PATH[] = "/system/save/8000000000000010/su/avators/";
+
+UUID UUID::Generate() {
std::random_device device;
std::mt19937 gen(device());
std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max());
- uuid[0] = distribution(gen);
- uuid[1] = distribution(gen);
- return *this;
+ return UUID{distribution(gen), distribution(gen)};
}
ProfileManager::ProfileManager() {
- // TODO(ogniK): Create the default user we have for now until loading/saving users is added
- auto user_uuid = UUID{1, 0};
- ASSERT(CreateNewUser(user_uuid, Settings::values.username).IsSuccess());
- OpenUser(user_uuid);
+ ParseUserSaveFile();
+
+ if (user_count == 0)
+ CreateNewUser(UUID::Generate(), "yuzu");
+
+ auto current = std::clamp<int>(Settings::values.current_user, 0, MAX_USERS - 1);
+ if (UserExistsIndex(current))
+ current = 0;
+
+ OpenUser(*GetUser(current));
}
-ProfileManager::~ProfileManager() = default;
+ProfileManager::~ProfileManager() {
+ WriteUserSaveFile();
+}
/// After a users creation it needs to be "registered" to the system. AddToProfiles handles the
/// internal management of the users profiles
@@ -101,6 +126,12 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username)
return CreateNewUser(uuid, username_output);
}
+boost::optional<UUID> ProfileManager::GetUser(std::size_t index) const {
+ if (index >= MAX_USERS)
+ return boost::none;
+ return profiles[index].user_uuid;
+}
+
/// Returns a users profile index based on their user id.
boost::optional<std::size_t> ProfileManager::GetUserIndex(const UUID& uuid) const {
if (!uuid) {
@@ -164,6 +195,12 @@ bool ProfileManager::UserExists(UUID uuid) const {
return (GetUserIndex(uuid) != boost::none);
}
+bool ProfileManager::UserExistsIndex(std::size_t index) const {
+ if (index >= MAX_USERS)
+ return false;
+ return profiles[index].user_uuid.uuid != INVALID_UUID;
+}
+
/// Opens a specific user
void ProfileManager::OpenUser(UUID uuid) {
auto idx = GetUserIndex(uuid);
@@ -239,4 +276,96 @@ bool ProfileManager::CanSystemRegisterUser() const {
// emulate qlaunch. Update this to dynamically change.
}
+bool ProfileManager::RemoveUser(UUID uuid) {
+ auto index = GetUserIndex(uuid);
+ if (index == boost::none) {
+ return false;
+ }
+
+ profiles[*index] = ProfileInfo{};
+ std::stable_partition(profiles.begin(), profiles.end(),
+ [](const ProfileInfo& profile) { return profile.user_uuid; });
+ return true;
+}
+
+bool ProfileManager::SetProfileBase(UUID uuid, const ProfileBase& profile_new) {
+ auto index = GetUserIndex(uuid);
+ if (profile_new.user_uuid == UUID(INVALID_UUID) || index == boost::none) {
+ return false;
+ }
+
+ auto& profile = profiles[*index];
+ profile.user_uuid = profile_new.user_uuid;
+ profile.username = profile_new.username;
+ profile.creation_time = profile_new.timestamp;
+
+ return true;
+}
+
+void ProfileManager::ParseUserSaveFile() {
+ FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
+ ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat",
+ "rb");
+
+ if (!save.IsOpen()) {
+ LOG_WARNING(Service_ACC, "Failed to load profile data from save data... Generating new "
+ "user 'yuzu' with random UUID.");
+ return;
+ }
+
+ ProfileDataRaw data;
+ if (save.ReadBytes(&data, sizeof(ProfileDataRaw)) != sizeof(ProfileDataRaw)) {
+ LOG_WARNING(Service_ACC, "profiles.dat is smaller than expected... Generating new user "
+ "'yuzu' with random UUID.");
+ return;
+ }
+
+ for (std::size_t i = 0; i < MAX_USERS; ++i) {
+ const auto& user = data.users[i];
+
+ if (user.uuid != UUID(INVALID_UUID))
+ AddUser({user.uuid, user.username, user.timestamp, {}, false});
+ }
+
+ std::stable_partition(profiles.begin(), profiles.end(),
+ [](const ProfileInfo& profile) { return profile.user_uuid; });
+}
+
+void ProfileManager::WriteUserSaveFile() {
+ ProfileDataRaw raw{};
+
+ for (std::size_t i = 0; i < MAX_USERS; ++i) {
+ raw.users[i].username = profiles[i].username;
+ raw.users[i].uuid2 = profiles[i].user_uuid;
+ raw.users[i].uuid = profiles[i].user_uuid;
+ raw.users[i].timestamp = profiles[i].creation_time;
+ }
+
+ const auto raw_path =
+ FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010";
+ if (FileUtil::Exists(raw_path) && !FileUtil::IsDirectory(raw_path))
+ FileUtil::Delete(raw_path);
+
+ const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
+ ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat";
+
+ if (!FileUtil::CreateFullPath(path)) {
+ LOG_WARNING(Service_ACC, "Failed to create full path of profiles.dat. Create the directory "
+ "nand/system/save/8000000000000010/su/avators to mitigate this "
+ "issue.");
+ return;
+ }
+
+ FileUtil::IOFile save(path, "wb");
+
+ if (!save.IsOpen()) {
+ LOG_WARNING(Service_ACC, "Failed to write save data to file... No changes to user data "
+ "made in current session will be saved.");
+ return;
+ }
+
+ save.Resize(sizeof(ProfileDataRaw));
+ save.WriteBytes(&raw, sizeof(ProfileDataRaw));
+}
+
}; // namespace Service::Account
diff --git a/src/core/hle/service/acc/profile_manager.h b/src/core/hle/service/acc/profile_manager.h
index bffd4cf4d..235208d56 100644
--- a/src/core/hle/service/acc/profile_manager.h
+++ b/src/core/hle/service/acc/profile_manager.h
@@ -36,7 +36,7 @@ struct UUID {
}
// TODO(ogniK): Properly generate uuids based on RFC-4122
- const UUID& Generate();
+ static UUID Generate();
// Set the UUID to {0,0} to be considered an invalid user
void Invalidate() {
@@ -45,6 +45,15 @@ struct UUID {
std::string Format() const {
return fmt::format("0x{:016X}{:016X}", uuid[1], uuid[0]);
}
+
+ std::string FormatSwitch() const {
+ std::array<u8, 16> s{};
+ std::memcpy(s.data(), uuid.data(), sizeof(u128));
+ return fmt::format("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{"
+ ":02x}{:02x}{:02x}{:02x}{:02x}",
+ s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11],
+ s[12], s[13], s[14], s[15]);
+ }
};
static_assert(sizeof(UUID) == 16, "UUID is an invalid size!");
@@ -81,12 +90,13 @@ static_assert(sizeof(ProfileBase) == 0x38, "ProfileBase is an invalid size");
/// objects
class ProfileManager {
public:
- ProfileManager(); // TODO(ogniK): Load from system save
+ ProfileManager();
~ProfileManager();
ResultCode AddUser(const ProfileInfo& user);
ResultCode CreateNewUser(UUID uuid, const ProfileUsername& username);
ResultCode CreateNewUser(UUID uuid, const std::string& username);
+ boost::optional<UUID> GetUser(std::size_t index) const;
boost::optional<std::size_t> GetUserIndex(const UUID& uuid) const;
boost::optional<std::size_t> GetUserIndex(const ProfileInfo& user) const;
bool GetProfileBase(boost::optional<std::size_t> index, ProfileBase& profile) const;
@@ -100,6 +110,7 @@ public:
std::size_t GetUserCount() const;
std::size_t GetOpenUserCount() const;
bool UserExists(UUID uuid) const;
+ bool UserExistsIndex(std::size_t index) const;
void OpenUser(UUID uuid);
void CloseUser(UUID uuid);
UserIDArray GetOpenUsers() const;
@@ -108,7 +119,13 @@ public:
bool CanSystemRegisterUser() const;
+ bool RemoveUser(UUID uuid);
+ bool SetProfileBase(UUID uuid, const ProfileBase& profile);
+
private:
+ void ParseUserSaveFile();
+ void WriteUserSaveFile();
+
std::array<ProfileInfo, MAX_USERS> profiles{};
std::size_t user_count = 0;
boost::optional<std::size_t> AddToProfiles(const ProfileInfo& profile);
diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index ecf72ae24..4ed66d817 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -4,11 +4,13 @@
#include <array>
#include <cinttypes>
+#include <cstring>
#include <stack>
#include "core/core.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/process.h"
+#include "core/hle/service/acc/profile_manager.h"
#include "core/hle/service/am/am.h"
#include "core/hle/service/am/applet_ae.h"
#include "core/hle/service/am/applet_oe.h"
@@ -26,6 +28,16 @@
namespace Service::AM {
+constexpr u32 POP_LAUNCH_PARAMETER_MAGIC = 0xC79497CA;
+
+struct LaunchParameters {
+ u32_le magic;
+ u32_le is_account_selected;
+ u128 current_user;
+ INSERT_PADDING_BYTES(0x70);
+};
+static_assert(sizeof(LaunchParameters) == 0x88);
+
IWindowController::IWindowController() : ServiceFramework("IWindowController") {
// clang-format off
static const FunctionInfo functions[] = {
@@ -724,20 +736,23 @@ void IApplicationFunctions::EndBlockingHomeButton(Kernel::HLERequestContext& ctx
}
void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) {
- constexpr std::array<u8, 0x88> data{{
- 0xca, 0x97, 0x94, 0xc7, // Magic
- 1, 0, 0, 0, // IsAccountSelected (bool)
- 1, 0, 0, 0, // User Id (word 0)
- 0, 0, 0, 0, // User Id (word 1)
- 0, 0, 0, 0, // User Id (word 2)
- 0, 0, 0, 0 // User Id (word 3)
- }};
+ LaunchParameters params{};
- std::vector<u8> buffer(data.begin(), data.end());
+ params.magic = POP_LAUNCH_PARAMETER_MAGIC;
+ params.is_account_selected = 1;
+
+ Account::ProfileManager profile_manager{};
+ const auto uuid = profile_manager.GetUser(Settings::values.current_user);
+ ASSERT(uuid != boost::none);
+ params.current_user = uuid->uuid;
IPC::ResponseBuilder rb{ctx, 2, 0, 1};
rb.Push(RESULT_SUCCESS);
+
+ std::vector<u8> buffer(sizeof(LaunchParameters));
+ std::memcpy(buffer.data(), &params, buffer.size());
+
rb.PushIpcInterface<AM::IStorage>(buffer);
LOG_DEBUG(Service_AM, "called");