2018-12-24 12:30:07 -06:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
2018-12-18 08:07:25 -06:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-12-24 12:30:07 -06:00
|
|
|
#include <string>
|
2021-08-04 02:55:48 -05:00
|
|
|
#include <string_view>
|
2018-12-24 12:30:07 -06:00
|
|
|
|
2018-12-18 08:07:25 -06:00
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
constexpr u128 INVALID_UUID{{0, 0}};
|
|
|
|
|
2021-08-04 02:55:48 -05:00
|
|
|
/**
|
|
|
|
* Converts a hex string to a 128-bit unsigned integer.
|
|
|
|
*
|
|
|
|
* The hex string can be formatted in lowercase or uppercase, with or without the "0x" prefix.
|
|
|
|
*
|
|
|
|
* This function will assert and return INVALID_UUID under the following conditions:
|
|
|
|
* - If the hex string is more than 32 characters long
|
|
|
|
* - If the hex string contains non-hexadecimal characters
|
|
|
|
*
|
|
|
|
* @param hex_string Hexadecimal string
|
|
|
|
*
|
|
|
|
* @returns A 128-bit unsigned integer if successfully converted, INVALID_UUID otherwise.
|
|
|
|
*/
|
|
|
|
[[nodiscard]] u128 HexStringToU128(std::string_view hex_string);
|
|
|
|
|
2018-12-18 08:07:25 -06:00
|
|
|
struct UUID {
|
|
|
|
// UUIDs which are 0 are considered invalid!
|
2021-01-15 02:33:33 -06:00
|
|
|
u128 uuid;
|
|
|
|
UUID() = default;
|
2018-12-18 08:07:25 -06:00
|
|
|
constexpr explicit UUID(const u128& id) : uuid{id} {}
|
|
|
|
constexpr explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {}
|
2021-08-04 02:55:48 -05:00
|
|
|
explicit UUID(std::string_view hex_string) {
|
|
|
|
uuid = HexStringToU128(hex_string);
|
|
|
|
}
|
2018-12-18 08:07:25 -06:00
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr explicit operator bool() const {
|
2021-07-20 11:36:57 -05:00
|
|
|
return uuid != INVALID_UUID;
|
2018-12-18 08:07:25 -06:00
|
|
|
}
|
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr bool operator==(const UUID& rhs) const {
|
2021-07-20 11:36:57 -05:00
|
|
|
return uuid == rhs.uuid;
|
2018-12-18 08:07:25 -06:00
|
|
|
}
|
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr bool operator!=(const UUID& rhs) const {
|
2018-12-18 08:07:25 -06:00
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ogniK): Properly generate uuids based on RFC-4122
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] static UUID Generate();
|
2018-12-18 08:07:25 -06:00
|
|
|
|
|
|
|
// Set the UUID to {0,0} to be considered an invalid user
|
2018-12-24 12:30:07 -06:00
|
|
|
constexpr void Invalidate() {
|
2018-12-18 08:07:25 -06:00
|
|
|
uuid = INVALID_UUID;
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:52:37 -05:00
|
|
|
[[nodiscard]] constexpr bool IsInvalid() const {
|
|
|
|
return uuid == INVALID_UUID;
|
|
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool IsValid() const {
|
|
|
|
return !IsInvalid();
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:09:53 -05:00
|
|
|
// TODO(ogniK): Properly generate a Nintendo ID
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr u64 GetNintendoID() const {
|
2020-04-29 06:09:53 -05:00
|
|
|
return uuid[0];
|
|
|
|
}
|
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] std::string Format() const;
|
|
|
|
[[nodiscard]] std::string FormatSwitch() const;
|
2018-12-18 08:07:25 -06:00
|
|
|
};
|
|
|
|
static_assert(sizeof(UUID) == 16, "UUID is an invalid size!");
|
|
|
|
|
|
|
|
} // namespace Common
|
2021-08-05 23:41:55 -05:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<Common::UUID> {
|
|
|
|
size_t operator()(const Common::UUID& uuid) const noexcept {
|
|
|
|
return uuid.uuid[1] ^ uuid.uuid[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|