From 5f8d253ce0a940cdb668861c2c96749c7b9ce626 Mon Sep 17 00:00:00 2001
From: David Marcec <dmarcecguzman@gmail.com>
Date: Wed, 8 Aug 2018 21:09:45 +1000
Subject: [PATCH] Switched uuids from u128 to new UUID struct

---
 src/core/hle/service/acc/acc.cpp | 22 ++++++++++---------
 src/core/hle/service/acc/acc.h   | 37 ++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp
index e952b05181..1fb3d96f69 100644
--- a/src/core/hle/service/acc/acc.cpp
+++ b/src/core/hle/service/acc/acc.cpp
@@ -3,7 +3,10 @@
 // Refer to the license.txt file included.
 
 #include <array>
+#include "common/common_types.h"
 #include "common/logging/log.h"
+#include "common/swap.h"
+#include "core/core_timing.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/service/acc/acc.h"
 #include "core/hle/service/acc/acc_aa.h"
@@ -13,7 +16,6 @@
 #include "core/settings.h"
 
 namespace Service::Account {
-
 // TODO: RE this structure
 struct UserData {
     INSERT_PADDING_WORDS(1);
@@ -33,11 +35,11 @@ struct ProfileBase {
 static_assert(sizeof(ProfileBase) == 0x38, "ProfileBase structure has incorrect size");
 
 // TODO(ogniK): Generate a real user id based on username, md5(username) maybe?
-static constexpr u128 DEFAULT_USER_ID{1ull, 0ull};
+static UUID DEFAULT_USER_ID{1ull, 0ull};
 
 class IProfile final : public ServiceFramework<IProfile> {
 public:
-    explicit IProfile(u128 user_id) : ServiceFramework("IProfile"), user_id(user_id) {
+    explicit IProfile(UUID user_id) : ServiceFramework("IProfile"), user_id(user_id) {
         static const FunctionInfo functions[] = {
             {0, &IProfile::Get, "Get"},
             {1, &IProfile::GetBase, "GetBase"},
@@ -51,7 +53,7 @@ private:
     void Get(Kernel::HLERequestContext& ctx) {
         LOG_WARNING(Service_ACC, "(STUBBED) called");
         ProfileBase profile_base{};
-        profile_base.user_id = user_id;
+        profile_base.user_id = user_id.uuid;
         if (Settings::values.username.size() > profile_base.username.size()) {
             std::copy_n(Settings::values.username.begin(), profile_base.username.size(),
                         profile_base.username.begin());
@@ -70,7 +72,7 @@ private:
 
         // TODO(Subv): Retrieve this information from somewhere.
         ProfileBase profile_base{};
-        profile_base.user_id = user_id;
+        profile_base.user_id = user_id.uuid;
         if (Settings::values.username.size() > profile_base.username.size()) {
             std::copy_n(Settings::values.username.begin(), profile_base.username.size(),
                         profile_base.username.begin());
@@ -83,7 +85,7 @@ private:
         rb.PushRaw(profile_base);
     }
 
-    u128 user_id; ///< The user id this profile refers to.
+    UUID user_id; ///< The user id this profile refers to.
 };
 
 class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
@@ -136,7 +138,7 @@ void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
 void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
     LOG_WARNING(Service_ACC, "(STUBBED) called");
     // TODO(Subv): There is only one user for now.
-    const std::vector<u128> user_ids = {DEFAULT_USER_ID};
+    const std::vector<UUID> user_ids = {DEFAULT_USER_ID};
     ctx.WriteBuffer(user_ids);
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(RESULT_SUCCESS);
@@ -145,7 +147,7 @@ void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
 void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
     LOG_WARNING(Service_ACC, "(STUBBED) called");
     // TODO(Subv): There is only one user for now.
-    const std::vector<u128> user_ids = {DEFAULT_USER_ID};
+    const std::vector<UUID> user_ids = {DEFAULT_USER_ID};
     ctx.WriteBuffer(user_ids);
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(RESULT_SUCCESS);
@@ -153,11 +155,11 @@ void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
 
 void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
-    u128 user_id = rp.PopRaw<u128>();
+    UUID user_id = rp.PopRaw<UUID>();
     IPC::ResponseBuilder rb{ctx, 2, 0, 1};
     rb.Push(RESULT_SUCCESS);
     rb.PushIpcInterface<IProfile>(user_id);
-    LOG_DEBUG(Service_ACC, "called user_id=0x{:016X}{:016X}", user_id[1], user_id[0]);
+    LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
 }
 
 void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
diff --git a/src/core/hle/service/acc/acc.h b/src/core/hle/service/acc/acc.h
index 88cabaa017..e392b3557b 100644
--- a/src/core/hle/service/acc/acc.h
+++ b/src/core/hle/service/acc/acc.h
@@ -8,6 +8,43 @@
 
 namespace Service::Account {
 
+struct UUID {
+    // UUIDs which are 0 are considered invalid!
+    u128 uuid{0, 0};
+    UUID() = default;
+    explicit UUID(const u128& id) {
+        uuid[0] = id[0];
+        uuid[1] = id[1];
+    };
+    explicit UUID(const u64& lo, const u64& hi) {
+        uuid[0] = lo;
+        uuid[1] = hi;
+    };
+    operator bool() const {
+        return uuid[0] != 0x0 && uuid[1] != 0x0;
+    }
+
+    bool operator==(const UUID& rhs) {
+        return uuid[0] == rhs.uuid[0] && uuid[1] == rhs.uuid[1];
+    }
+
+    bool operator!=(const UUID& rhs) {
+        return uuid[0] != rhs.uuid[0] || uuid[1] != rhs.uuid[1];
+    }
+
+    // TODO(ogniK): Properly generate uuids based on RFC-4122
+    const UUID& Generate() {
+        uuid[0] = (static_cast<u64>(std::rand()) << 32) | std::rand();
+        uuid[1] = (static_cast<u64>(std::rand()) << 32) | std::rand();
+        return *this;
+    }
+
+    std::string Format() {
+        return fmt::format("0x{:016X}{:016X}", uuid[1], uuid[0]);
+    }
+};
+static_assert(sizeof(UUID) == 16, "UUID is an invalid size!");
+
 class Module final {
 public:
     class Interface : public ServiceFramework<Interface> {
-- 
GitLab