From ee0547e4c4d38d681d9cd3b9f7071c4dade9110d Mon Sep 17 00:00:00 2001
From: Morph <39850852+Morph1984@users.noreply.github.com>
Date: Sat, 5 Feb 2022 00:40:48 -0500
Subject: [PATCH] service: Migrate to the new UUID implementation

---
 src/core/hle/service/friend/friend.cpp        | 18 ++++++++--------
 src/core/hle/service/mii/mii_manager.cpp      | 21 ++++++-------------
 src/core/hle/service/mii/mii_manager.h        | 10 ++++-----
 src/core/hle/service/ns/pdm_qry.cpp           |  6 +++---
 src/core/hle/service/time/clock_types.h       |  8 +++----
 src/core/hle/service/time/steady_clock_core.h |  8 +++----
 src/core/hle/service/time/time_manager.cpp    |  4 ++--
 .../hle/service/time/time_sharedmemory.cpp    |  2 +-
 src/core/hle/service/time/time_sharedmemory.h |  4 ++--
 9 files changed, 36 insertions(+), 45 deletions(-)

diff --git a/src/core/hle/service/friend/friend.cpp b/src/core/hle/service/friend/friend.cpp
index 9f9cea1e0..3c621f7f0 100644
--- a/src/core/hle/service/friend/friend.cpp
+++ b/src/core/hle/service/friend/friend.cpp
@@ -4,7 +4,7 @@
 
 #include <queue>
 #include "common/logging/log.h"
-#include "common/uuid.h"
+#include "common/new_uuid.h"
 #include "core/core.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/kernel/k_event.h"
@@ -170,10 +170,10 @@ private:
     void GetPlayHistoryRegistrationKey(Kernel::HLERequestContext& ctx) {
         IPC::RequestParser rp{ctx};
         const auto local_play = rp.Pop<bool>();
-        const auto uuid = rp.PopRaw<Common::UUID>();
+        const auto uuid = rp.PopRaw<Common::NewUUID>();
 
         LOG_WARNING(Service_Friend, "(STUBBED) called, local_play={}, uuid=0x{}", local_play,
-                    uuid.Format());
+                    uuid.RawString());
 
         IPC::ResponseBuilder rb{ctx, 2};
         rb.Push(ResultSuccess);
@@ -182,11 +182,11 @@ private:
     void GetFriendList(Kernel::HLERequestContext& ctx) {
         IPC::RequestParser rp{ctx};
         const auto friend_offset = rp.Pop<u32>();
-        const auto uuid = rp.PopRaw<Common::UUID>();
+        const auto uuid = rp.PopRaw<Common::NewUUID>();
         [[maybe_unused]] const auto filter = rp.PopRaw<SizedFriendFilter>();
         const auto pid = rp.Pop<u64>();
         LOG_WARNING(Service_Friend, "(STUBBED) called, offset={}, uuid=0x{}, pid={}", friend_offset,
-                    uuid.Format(), pid);
+                    uuid.RawString(), pid);
 
         IPC::ResponseBuilder rb{ctx, 3};
         rb.Push(ResultSuccess);
@@ -202,7 +202,7 @@ private:
 
 class INotificationService final : public ServiceFramework<INotificationService> {
 public:
-    explicit INotificationService(Core::System& system_, Common::UUID uuid_)
+    explicit INotificationService(Core::System& system_, Common::NewUUID uuid_)
         : ServiceFramework{system_, "INotificationService"}, uuid{uuid_},
           service_context{system_, "INotificationService"} {
         // clang-format off
@@ -293,7 +293,7 @@ private:
         bool has_received_friend_request;
     };
 
-    Common::UUID uuid;
+    Common::NewUUID uuid;
     KernelHelpers::ServiceContext service_context;
 
     Kernel::KEvent* notification_event;
@@ -310,9 +310,9 @@ void Module::Interface::CreateFriendService(Kernel::HLERequestContext& ctx) {
 
 void Module::Interface::CreateNotificationService(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
-    auto uuid = rp.PopRaw<Common::UUID>();
+    auto uuid = rp.PopRaw<Common::NewUUID>();
 
-    LOG_DEBUG(Service_Friend, "called, uuid=0x{}", uuid.Format());
+    LOG_DEBUG(Service_Friend, "called, uuid=0x{}", uuid.RawString());
 
     IPC::ResponseBuilder rb{ctx, 2, 0, 1};
     rb.Push(ResultSuccess);
diff --git a/src/core/hle/service/mii/mii_manager.cpp b/src/core/hle/service/mii/mii_manager.cpp
index ca4ed35bb..aa1a9a6c7 100644
--- a/src/core/hle/service/mii/mii_manager.cpp
+++ b/src/core/hle/service/mii/mii_manager.cpp
@@ -118,16 +118,6 @@ u16 GenerateCrc16(const void* data, std::size_t size) {
     return Common::swap16(static_cast<u16>(crc));
 }
 
-Common::UUID GenerateValidUUID() {
-    auto uuid{Common::UUID::Generate()};
-
-    // Bit 7 must be set, and bit 6 unset for the UUID to be valid
-    uuid.uuid[1] &= 0xFFFFFFFFFFFFFF3FULL;
-    uuid.uuid[1] |= 0x0000000000000080ULL;
-
-    return uuid;
-}
-
 template <typename T>
 T GetRandomValue(T min, T max) {
     std::random_device device;
@@ -141,7 +131,8 @@ T GetRandomValue(T max) {
     return GetRandomValue<T>({}, max);
 }
 
-MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Common::UUID& user_id) {
+MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race,
+                                  const Common::NewUUID& user_id) {
     MiiStoreBitFields bf{};
 
     if (gender == Gender::All) {
@@ -320,7 +311,7 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo
     return {DefaultMiiName, bf, user_id};
 }
 
-MiiStoreData BuildDefaultStoreData(const DefaultMii& info, const Common::UUID& user_id) {
+MiiStoreData BuildDefaultStoreData(const DefaultMii& info, const Common::NewUUID& user_id) {
     MiiStoreBitFields bf{};
 
     bf.font_region.Assign(info.font_region);
@@ -381,13 +372,13 @@ MiiStoreData BuildDefaultStoreData(const DefaultMii& info, const Common::UUID& u
 MiiStoreData::MiiStoreData() = default;
 
 MiiStoreData::MiiStoreData(const MiiStoreData::Name& name, const MiiStoreBitFields& bit_fields,
-                           const Common::UUID& user_id) {
+                           const Common::NewUUID& user_id) {
     data.name = name;
-    data.uuid = GenerateValidUUID();
+    data.uuid = Common::NewUUID::MakeRandomRFC4122V4();
 
     std::memcpy(data.data.data(), &bit_fields, sizeof(MiiStoreBitFields));
     data_crc = GenerateCrc16(data.data.data(), sizeof(data));
-    device_crc = GenerateCrc16(&user_id, sizeof(Common::UUID));
+    device_crc = GenerateCrc16(&user_id, sizeof(Common::NewUUID));
 }
 
 MiiManager::MiiManager() : user_id{Service::Account::ProfileManager().GetLastOpenedUser()} {}
diff --git a/src/core/hle/service/mii/mii_manager.h b/src/core/hle/service/mii/mii_manager.h
index 8e048fc56..580a64fc9 100644
--- a/src/core/hle/service/mii/mii_manager.h
+++ b/src/core/hle/service/mii/mii_manager.h
@@ -8,7 +8,7 @@
 #include <vector>
 #include "common/bit_field.h"
 #include "common/common_funcs.h"
-#include "common/uuid.h"
+#include "common/new_uuid.h"
 #include "core/hle/result.h"
 #include "core/hle/service/mii/types.h"
 
@@ -29,7 +29,7 @@ enum class SourceFlag : u32 {
 DECLARE_ENUM_FLAG_OPERATORS(SourceFlag);
 
 struct MiiInfo {
-    Common::UUID uuid;
+    Common::NewUUID uuid;
     std::array<char16_t, 11> name;
     u8 font_region;
     u8 favorite_color;
@@ -192,7 +192,7 @@ struct MiiStoreData {
 
     MiiStoreData();
     MiiStoreData(const Name& name, const MiiStoreBitFields& bit_fields,
-                 const Common::UUID& user_id);
+                 const Common::NewUUID& user_id);
 
     // This corresponds to the above structure MiiStoreBitFields. I did it like this because the
     // BitField<> type makes this (and any thing that contains it) not trivially copyable, which is
@@ -202,7 +202,7 @@ struct MiiStoreData {
         static_assert(sizeof(MiiStoreBitFields) == sizeof(data), "data field has incorrect size.");
 
         Name name{};
-        Common::UUID uuid{Common::INVALID_UUID};
+        Common::NewUUID uuid{};
     } data;
 
     u16 data_crc{};
@@ -326,7 +326,7 @@ public:
     ResultCode GetIndex(const MiiInfo& info, u32& index);
 
 private:
-    const Common::UUID user_id{Common::INVALID_UUID};
+    const Common::NewUUID user_id{};
     u64 update_counter{};
 };
 
diff --git a/src/core/hle/service/ns/pdm_qry.cpp b/src/core/hle/service/ns/pdm_qry.cpp
index e2fab5c3f..3eda444d2 100644
--- a/src/core/hle/service/ns/pdm_qry.cpp
+++ b/src/core/hle/service/ns/pdm_qry.cpp
@@ -5,7 +5,7 @@
 #include <memory>
 
 #include "common/logging/log.h"
-#include "common/uuid.h"
+#include "common/new_uuid.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/service/ns/pdm_qry.h"
 #include "core/hle/service/service.h"
@@ -49,7 +49,7 @@ void PDM_QRY::QueryPlayStatisticsByApplicationIdAndUserAccountId(Kernel::HLERequ
     const auto unknown = rp.Pop<bool>();
     rp.Pop<u8>(); // Padding
     const auto application_id = rp.Pop<u64>();
-    const auto user_account_uid = rp.PopRaw<Common::UUID>();
+    const auto user_account_uid = rp.PopRaw<Common::NewUUID>();
 
     // TODO(German77): Read statistics of the game
     PlayStatistics statistics{
@@ -59,7 +59,7 @@ void PDM_QRY::QueryPlayStatisticsByApplicationIdAndUserAccountId(Kernel::HLERequ
 
     LOG_WARNING(Service_NS,
                 "(STUBBED) called. unknown={}. application_id=0x{:016X}, user_account_uid=0x{}",
-                unknown, application_id, user_account_uid.Format());
+                unknown, application_id, user_account_uid.RawString());
 
     IPC::ResponseBuilder rb{ctx, 12};
     rb.Push(ResultSuccess);
diff --git a/src/core/hle/service/time/clock_types.h b/src/core/hle/service/time/clock_types.h
index 392e16863..23d6c859b 100644
--- a/src/core/hle/service/time/clock_types.h
+++ b/src/core/hle/service/time/clock_types.h
@@ -6,7 +6,7 @@
 
 #include "common/common_funcs.h"
 #include "common/common_types.h"
-#include "common/uuid.h"
+#include "common/new_uuid.h"
 #include "core/hle/service/time/errors.h"
 #include "core/hle/service/time/time_zone_types.h"
 
@@ -21,7 +21,7 @@ enum class TimeType : u8 {
 /// https://switchbrew.org/wiki/Glue_services#SteadyClockTimePoint
 struct SteadyClockTimePoint {
     s64 time_point;
-    Common::UUID clock_source_id;
+    Common::NewUUID clock_source_id;
 
     ResultCode GetSpanBetween(SteadyClockTimePoint other, s64& span) const {
         span = 0;
@@ -36,7 +36,7 @@ struct SteadyClockTimePoint {
     }
 
     static SteadyClockTimePoint GetRandom() {
-        return {0, Common::UUID::Generate()};
+        return {0, Common::NewUUID::MakeRandom()};
     }
 };
 static_assert(sizeof(SteadyClockTimePoint) == 0x18, "SteadyClockTimePoint is incorrect size");
@@ -45,7 +45,7 @@ static_assert(std::is_trivially_copyable_v<SteadyClockTimePoint>,
 
 struct SteadyClockContext {
     u64 internal_offset;
-    Common::UUID steady_time_point;
+    Common::NewUUID steady_time_point;
 };
 static_assert(sizeof(SteadyClockContext) == 0x18, "SteadyClockContext is incorrect size");
 static_assert(std::is_trivially_copyable_v<SteadyClockContext>,
diff --git a/src/core/hle/service/time/steady_clock_core.h b/src/core/hle/service/time/steady_clock_core.h
index d80a2385f..dfc9fade4 100644
--- a/src/core/hle/service/time/steady_clock_core.h
+++ b/src/core/hle/service/time/steady_clock_core.h
@@ -4,7 +4,7 @@
 
 #pragma once
 
-#include "common/uuid.h"
+#include "common/new_uuid.h"
 #include "core/hle/service/time/clock_types.h"
 
 namespace Core {
@@ -18,11 +18,11 @@ public:
     SteadyClockCore() = default;
     virtual ~SteadyClockCore() = default;
 
-    const Common::UUID& GetClockSourceId() const {
+    const Common::NewUUID& GetClockSourceId() const {
         return clock_source_id;
     }
 
-    void SetClockSourceId(const Common::UUID& value) {
+    void SetClockSourceId(const Common::NewUUID& value) {
         clock_source_id = value;
     }
 
@@ -49,7 +49,7 @@ public:
     }
 
 private:
-    Common::UUID clock_source_id{Common::UUID::Generate()};
+    Common::NewUUID clock_source_id{Common::NewUUID::MakeRandom()};
     bool is_initialized{};
 };
 
diff --git a/src/core/hle/service/time/time_manager.cpp b/src/core/hle/service/time/time_manager.cpp
index c1e4e6cce..15a2d99e8 100644
--- a/src/core/hle/service/time/time_manager.cpp
+++ b/src/core/hle/service/time/time_manager.cpp
@@ -45,7 +45,7 @@ struct TimeManager::Impl final {
           time_zone_content_manager{system} {
 
         const auto system_time{Clock::TimeSpanType::FromSeconds(GetExternalRtcValue())};
-        SetupStandardSteadyClock(system, Common::UUID::Generate(), system_time, {}, {});
+        SetupStandardSteadyClock(system, Common::NewUUID::MakeRandom(), system_time, {}, {});
         SetupStandardLocalSystemClock(system, {}, system_time.ToSeconds());
 
         Clock::SystemClockContext clock_context{};
@@ -132,7 +132,7 @@ struct TimeManager::Impl final {
         return 0;
     }
 
-    void SetupStandardSteadyClock(Core::System& system_, Common::UUID clock_source_id,
+    void SetupStandardSteadyClock(Core::System& system_, Common::NewUUID clock_source_id,
                                   Clock::TimeSpanType setup_value,
                                   Clock::TimeSpanType internal_offset, bool is_rtc_reset_detected) {
         standard_steady_clock_core.SetClockSourceId(clock_source_id);
diff --git a/src/core/hle/service/time/time_sharedmemory.cpp b/src/core/hle/service/time/time_sharedmemory.cpp
index ed9f75ed6..ac31cd9ca 100644
--- a/src/core/hle/service/time/time_sharedmemory.cpp
+++ b/src/core/hle/service/time/time_sharedmemory.cpp
@@ -20,7 +20,7 @@ SharedMemory::SharedMemory(Core::System& system_) : system(system_) {
 
 SharedMemory::~SharedMemory() = default;
 
-void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id,
+void SharedMemory::SetupStandardSteadyClock(const Common::NewUUID& clock_source_id,
                                             Clock::TimeSpanType current_time_point) {
     const Clock::TimeSpanType ticks_time_span{Clock::TimeSpanType::FromTicks(
         system.CoreTiming().GetClockTicks(), Core::Hardware::CNTFREQ)};
diff --git a/src/core/hle/service/time/time_sharedmemory.h b/src/core/hle/service/time/time_sharedmemory.h
index 9307ea795..4063ce4e0 100644
--- a/src/core/hle/service/time/time_sharedmemory.h
+++ b/src/core/hle/service/time/time_sharedmemory.h
@@ -5,7 +5,7 @@
 #pragma once
 
 #include "common/common_types.h"
-#include "common/uuid.h"
+#include "common/new_uuid.h"
 #include "core/hle/kernel/k_shared_memory.h"
 #include "core/hle/service/time/clock_types.h"
 
@@ -52,7 +52,7 @@ public:
     };
     static_assert(sizeof(Format) == 0xd8, "Format is an invalid size");
 
-    void SetupStandardSteadyClock(const Common::UUID& clock_source_id,
+    void SetupStandardSteadyClock(const Common::NewUUID& clock_source_id,
                                   Clock::TimeSpanType current_time_point);
     void UpdateLocalSystemClockContext(const Clock::SystemClockContext& context);
     void UpdateNetworkSystemClockContext(const Clock::SystemClockContext& context);