code:
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <exception>
#include <limits>
#include <memory>
#include <optional>
#include <span>
#include <type_traits>
#include <utility>
#include <vector>

#include "common/common_types.h"
#include "video_core/vulkan_common/vulkan.h"

#ifdef _MSC_VER
#pragma warning(disable : 26812) // Disable prefer enum class over enum
#endif

VK_DEFINE_HANDLE(VmaAllocator)
VK_DEFINE_HANDLE(VmaAllocation)

namespace Vulkan::vk {

/**
 * Span for Vulkan arrays.
 * Based on std::span but optimized for array access instead of iterators.
 * Size returns uint32_t instead of size_t to ease interaction with Vulkan functions.
 */
template <typename T>
class Span {
public:
    using value_type = T;
    using size_type = u32;
    using difference_type = std::ptrdiff_t;
    using reference = const T&;
    using const_reference = const T&;
    using pointer = const T*;
    using const_pointer = const T*;
    using iterator = const T*;
    using const_iterator = const T*;

    /// Construct an empty span.
    constexpr Span() noexcept = default;

    /// Construct an empty span
    constexpr Span(std::nullptr_t) noexcept {}

    /// Construct a span from a single element.
    constexpr Span(const T& value) noexcept : ptr{&value}, num{1} {}

    /// Construct a span from a range.
    template <typename Range>
    // requires std::data(const Range&)
    // requires std::size(const Range&)
    constexpr Span(const Range& range) : ptr{std::data(range)}, num{std::size(range)} {}

    /// Construct a span from a pointer and a size.
    /// This is intended for subranges.
    constexpr Span(const T* ptr_, std::size_t num_) noexcept : ptr{ptr_}, num{num_} {}

    /// Returns the data pointer by the span.
    constexpr const T* data() const noexcept {
        return ptr;
    }

    /// Returns the number of elements in the span.
    /// @note Returns a 32 bits integer because most Vulkan functions expect this type.
    constexpr u32 size() const noexcept {
        return static_cast<u32>(num);
    }

    /// Returns true when the span is empty.
    constexpr bool empty() const noexcept {
        return num == 0;
    }

    /// Returns a reference to the element in the passed index.
    /// @pre: index < size()
    constexpr const T& operator[](std::size_t index) const noexcept {
        return ptr[index];
    }

    /// Returns an iterator to the beginning of the span.
    constexpr const T* begin() const noexcept {
        return ptr;
    }

    /// Returns an iterator to the end of the span.
    constexpr const T* end() const noexcept {
        return ptr + num;
    }

    /// Returns an iterator to the beginning of the span.
    constexpr const T* cbegin() const noexcept {
        return ptr;
    }

    /// Returns an iterator to the end of the span.
    constexpr const T* cend() const noexcept {
        return ptr + num;
    }

private:
    const T* ptr = nullptr;
    std::size_t num = 0;
};

/// Vulkan exception generated from a VkResult.
class Exception final : public std::exception {
public:
    /// Construct the exception with a result.
    /// @pre result != VK_SUCCESS
    explicit Exception(VkResult result_) : result{result_} {}
    virtual ~Exception() = default;

    const char* what() const noexcept override;
    VkResult GetResult() const noexcept {
        return result;
    }

private:
    VkResult result;
};

/// Throws a Vulkan exception if result is not success.
inline void Check(VkResult result) {
    if (result != VK_SUCCESS) {
        throw Exception(result);
    }
}

/// Throws a Vulkan exception if result is an error.
/// @return result
inline VkResult Filter(VkResult result) {
    if (result < 0) {
        throw Exception(result);
    }
    return result;
}

/// Table holding Vulkan instance function pointers.
struct InstanceDispatch {
    PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr{};

    PFN_vkCreateInstance vkCreateInstance{};
    PFN_vkDestroyInstance vkDestroyInstance{};
    PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties{};
    PFN_vkEnumerateInstanceLayerProperties vkEnumerateInstanceLayerProperties{};

    PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessengerEXT{};
    PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT{};
    PFN_vkCreateDevice vkCreateDevice{};
    PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT{};
    PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallbackEXT{};
    PFN_vkDestroyDevice vkDestroyDevice{};
    PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR{};
    PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties{};
    PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices{};
    PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr{};
    PFN_vkGetPhysicalDeviceFeatures2 vkGetPhysicalDeviceFeatures2{};
    PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties{};
    PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties{};
    PFN_vkGetPhysicalDeviceMemoryProperties2 vkGetPhysicalDeviceMemoryProperties2{};
    PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties{};
    PFN_vkGetPhysicalDeviceProperties2 vkGetPhysicalDeviceProperties2{};
    PFN_vkGetPhysicalDeviceToolProperties vkGetPhysicalDeviceToolProperties{};
    PFN_vkGetPhysicalDeviceQueueFamilyProperties vkGetPhysicalDeviceQueueFamilyProperties{};
    PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR{};
    PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR{};
    PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModesKHR{};
    PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR{};
    PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR{};
    PFN_vkQueuePresentKHR vkQueuePresentKHR{};
};

/// Table holding Vulkan device function pointers.
struct DeviceDispatch : InstanceDispatch {
    PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR{};
    PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers{};
    PFN_vkAllocateDescriptorSets vkAllocateDescriptorSets{};
    PFN_vkAllocateMemory vkAllocateMemory{};
    PFN_vkBeginCommandBuffer vkBeginCommandBuffer{};
    PFN_vkBindBufferMemory vkBindBufferMemory{};
    PFN_vkBindImageMemory vkBindImageMemory{};
    PFN_vkCmdBeginConditionalRenderingEXT vkCmdBeginConditionalRenderingEXT{};
    PFN_vkCmdBeginDebugUtilsLabelEXT vkCmdBeginDebugUtilsLabelEXT{};
    PFN_vkCmdBeginQuery vkCmdBeginQuery{};
    PFN_vkCmdBeginRenderPass vkCmdBeginRenderPass{};
    PFN_vkCmdBeginTransformFeedbackEXT vkCmdBeginTransformFeedbackEXT{};
    PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets{};
    PFN_vkCmdBindIndexBuffer vkCmdBindIndexBuffer{};
    PFN_vkCmdBindPipeline vkCmdBindPipeline{};
    PFN_vkCmdBindTransformFeedbackBuffersEXT vkCmdBindTransformFeedbackBuffersEXT{};
    PFN_vkCmdBindVertexBuffers vkCmdBindVertexBuffers{};
    PFN_vkCmdBindVertexBuffers2EXT vkCmdBindVertexBuffers2EXT{};
    PFN_vkCmdBlitImage vkCmdBlitImage{};
    PFN_vkCmdClearAttachments vkCmdClearAttachments{};
    PFN_vkCmdClearColorImage vkCmdClearColorImage{};
    PFN_vkCmdCopyBuffer vkCmdCopyBuffer{};
    PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage{};
    PFN_vkCmdCopyImage vkCmdCopyImage{};
    PFN_vkCmdCopyImageToBuffer vkCmdCopyImageToBuffer{};
    PFN_vkCmdCopyQueryPoolResults vkCmdCopyQueryPoolResults{};
    PFN_vkCmdDispatch vkCmdDispatch{};
    PFN_vkCmdDispatchIndirect vkCmdDispatchIndirect{};
    PFN_vkCmdDraw vkCmdDraw{};
    PFN_vkCmdDrawIndexed vkCmdDrawIndexed{};
    PFN_vkCmdDrawIndirect vkCmdDrawIndirect{};
    PFN_vkCmdDrawIndexedIndirect vkCmdDrawIndexedIndirect{};
    PFN_vkCmdDrawIndirectCount vkCmdDrawIndirectCount{};
    PFN_vkCmdDrawIndexedIndirectCount vkCmdDrawIndexedIndirectCount{};
    PFN_vkCmdDrawIndirectByteCountEXT vkCmdDrawIndirectByteCountEXT{};
    PFN_vkCmdEndConditionalRenderingEXT vkCmdEndConditionalRenderingEXT{};
    PFN_vkCmdEndDebugUtilsLabelEXT vkCmdEndDebugUtilsLabelEXT{};
    PFN_vkCmdEndQuery vkCmdEndQuery{};
    PFN_vkCmdEndRenderPass vkCmdEndRenderPass{};
    PFN_vkCmdEndTransformFeedbackEXT vkCmdEndTransformFeedbackEXT{};
    PFN_vkCmdFillBuffer vkCmdFillBuffer{};
    PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier{};
    PFN_vkCmdPushConstants vkCmdPushConstants{};
    PFN_vkCmdPushDescriptorSetWithTemplateKHR vkCmdPushDescriptorSetWithTemplateKHR{};
    PFN_vkCmdResolveImage vkCmdResolveImage{};
    PFN_vkCmdSetBlendConstants vkCmdSetBlendConstants{};
    PFN_vkCmdSetCullModeEXT vkCmdSetCullModeEXT{};
    PFN_vkCmdSetDepthBias vkCmdSetDepthBias{};
    PFN_vkCmdSetDepthBias2EXT vkCmdSetDepthBias2EXT{};
    PFN_vkCmdSetDepthBounds vkCmdSetDepthBounds{};
    PFN_vkCmdSetDepthBoundsTestEnableEXT vkCmdSetDepthBoundsTestEnableEXT{};
    PFN_vkCmdSetDepthCompareOpEXT vkCmdSetDepthCompareOpEXT{};
    PFN_vkCmdSetDepthTestEnableEXT vkCmdSetDepthTestEnableEXT{};
    PFN_vkCmdSetDepthWriteEnableEXT vkCmdSetDepthWriteEnableEXT{};
    PFN_vkCmdSetPrimitiveRestartEnableEXT vkCmdSetPrimitiveRestartEnableEXT{};
    PFN_vkCmdSetRasterizerDiscardEnableEXT vkCmdSetRasterizerDiscardEnableEXT{};
    PFN_vkCmdSetDepthBiasEnableEXT vkCmdSetDepthBiasEnableEXT{};
    PFN_vkCmdSetLogicOpEnableEXT vkCmdSetLogicOpEnableEXT{};
    PFN_vkCmdSetDepthClampEnableEXT vkCmdSetDepthClampEnableEXT{};
    PFN_vkCmdSetEvent vkCmdSetEvent{};
    PFN_vkCmdSetFrontFaceEXT vkCmdSetFrontFaceEXT{};
    PFN_vkCmdSetPatchControlPointsEXT vkCmdSetPatchControlPointsEXT{};
    PFN_vkCmdSetLogicOpEXT vkCmdSetLogicOpEXT{};
    PFN_vkCmdSetLineWidth vkCmdSetLineWidth{};
    PFN_vkCmdSetPrimitiveTopologyEXT vkCmdSetPrimitiveTopologyEXT{};
    PFN_vkCmdSetScissor vkCmdSetScissor{};
    PFN_vkCmdSetStencilCompareMask vkCmdSetStencilCompareMask{};
    PFN_vkCmdSetStencilOpEXT vkCmdSetStencilOpEXT{};
    PFN_vkCmdSetStencilReference vkCmdSetStencilReference{};
    PFN_vkCmdSetStencilTestEnableEXT vkCmdSetStencilTestEnableEXT{};
    PFN_vkCmdSetStencilWriteMask vkCmdSetStencilWriteMask{};
    PFN_vkCmdSetVertexInputEXT vkCmdSetVertexInputEXT{};
    PFN_vkCmdSetViewport vkCmdSetViewport{};
    PFN_vkCmdSetColorWriteMaskEXT vkCmdSetColorWriteMaskEXT{};
    PFN_vkCmdSetColorBlendEnableEXT vkCmdSetColorBlendEnableEXT{};
    PFN_vkCmdSetColorBlendEquationEXT vkCmdSetColorBlendEquationEXT{};
    PFN_vkCmdWaitEvents vkCmdWaitEvents{};
    PFN_vkCreateBuffer vkCreateBuffer{};
    PFN_vkCreateBufferView vkCreateBufferView{};
    PFN_vkCreateCommandPool vkCreateCommandPool{};
    PFN_vkCreateComputePipelines vkCreateComputePipelines{};
    PFN_vkCreateDescriptorPool vkCreateDescriptorPool{};
    PFN_vkCreateDescriptorSetLayout vkCreateDescriptorSetLayout{};
    PFN_vkCreateDescriptorUpdateTemplate vkCreateDescriptorUpdateTemplate{};
    PFN_vkCreateEvent vkCreateEvent{};
    PFN_vkCreateFence vkCreateFence{};
    PFN_vkCreateFramebuffer vkCreateFramebuffer{};
    PFN_vkCreateGraphicsPipelines vkCreateGraphicsPipelines{};
    PFN_vkCreateImage vkCreateImage{};
    PFN_vkCreateImageView vkCreateImageView{};
    PFN_vkCreatePipelineCache vkCreatePipelineCache{};
    PFN_vkCreatePipelineLayout vkCreatePipelineLayout{};
    PFN_vkCreateQueryPool vkCreateQueryPool{};
    PFN_vkCreateRenderPass vkCreateRenderPass{};
    PFN_vkCreateSampler vkCreateSampler{};
    PFN_vkCreateSemaphore vkCreateSemaphore{};
    PFN_vkCreateShaderModule vkCreateShaderModule{};
    PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR{};
    PFN_vkDestroyBuffer vkDestroyBuffer{};
    PFN_vkDestroyBufferView vkDestroyBufferView{};
    PFN_vkDestroyCommandPool vkDestroyCommandPool{};
    PFN_vkDestroyDescriptorPool vkDestroyDescriptorPool{};
    PFN_vkDestroyDescriptorSetLayout vkDestroyDescriptorSetLayout{};
    PFN_vkDestroyDescriptorUpdateTemplate vkDestroyDescriptorUpdateTemplate{};
    PFN_vkDestroyEvent vkDestroyEvent{};
    PFN_vkDestroyFence vkDestroyFence{};
    PFN_vkDestroyFramebuffer vkDestroyFramebuffer{};
    PFN_vkDestroyImage vkDestroyImage{};
    PFN_vkDestroyImageView vkDestroyImageView{};
    PFN_vkDestroyPipeline vkDestroyPipeline{};
    PFN_vkDestroyPipelineCache vkDestroyPipelineCache{};
    PFN_vkDestroyPipelineLayout vkDestroyPipelineLayout{};
    PFN_vkDestroyQueryPool vkDestroyQueryPool{};
    PFN_vkDestroyRenderPass vkDestroyRenderPass{};
    PFN_vkDestroySampler vkDestroySampler{};
    PFN_vkDestroySemaphore vkDestroySemaphore{};
    PFN_vkDestroyShaderModule vkDestroyShaderModule{};
    PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR{};
    PFN_vkDeviceWaitIdle vkDeviceWaitIdle{};
    PFN_vkEndCommandBuffer vkEndCommandBuffer{};
    PFN_vkFreeCommandBuffers vkFreeCommandBuffers{};
    PFN_vkFreeDescriptorSets vkFreeDescriptorSets{};
    PFN_vkFreeMemory vkFreeMemory{};
    PFN_vkGetBufferMemoryRequirements2 vkGetBufferMemoryRequirements2{};
    PFN_vkGetDeviceQueue vkGetDeviceQueue{};
    PFN_vkGetEventStatus vkGetEventStatus{};
    PFN_vkGetFenceStatus vkGetFenceStatus{};
    PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements{};
    PFN_vkGetPipelineCacheData vkGetPipelineCacheData{};
    PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR{};
#ifdef _WIN32
    PFN_vkGetMemoryWin32HandleKHR vkGetMemoryWin32HandleKHR{};
#endif
    PFN_vkGetPipelineExecutablePropertiesKHR vkGetPipelineExecutablePropertiesKHR{};
    PFN_vkGetPipelineExecutableStatisticsKHR vkGetPipelineExecutableStatisticsKHR{};
    PFN_vkGetQueryPoolResults vkGetQueryPoolResults{};
    PFN_vkGetSemaphoreCounterValue vkGetSemaphoreCounterValue{};
    PFN_vkMapMemory vkMapMemory{};
    PFN_vkQueueSubmit vkQueueSubmit{};
    PFN_vkResetFences vkResetFences{};
    PFN_vkResetQueryPool vkResetQueryPool{};
    PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT{};
    PFN_vkSetDebugUtilsObjectTagEXT vkSetDebugUtilsObjectTagEXT{};
    PFN_vkUnmapMemory vkUnmapMemory{};
    PFN_vkUpdateDescriptorSetWithTemplate vkUpdateDescriptorSetWithTemplate{};
    PFN_vkUpdateDescriptorSets vkUpdateDescriptorSets{};
    PFN_vkWaitForFences vkWaitForFences{};
    PFN_vkWaitSemaphores vkWaitSemaphores{};
};

/// Loads instance agnostic function pointers.
/// @return True on success, false on error.
bool Load(InstanceDispatch&) noexcept;

/// Loads instance function pointers.
/// @return True on success, false on error.
bool Load(VkInstance, InstanceDispatch&) noexcept;

void Destroy(VkInstance, const InstanceDispatch&) noexcept;
void Destroy(VkDevice, const InstanceDispatch&) noexcept;

void Destroy(VkDevice, VkBuffer, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkBufferView, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkCommandPool, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkDescriptorPool, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkDescriptorSetLayout, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkDescriptorUpdateTemplate, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkDeviceMemory, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkEvent, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkFence, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkFramebuffer, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkImage, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkImageView, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkPipeline, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkPipelineCache, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkPipelineLayout, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkQueryPool, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkRenderPass, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkSampler, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkSwapchainKHR, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkSemaphore, const DeviceDispatch&) noexcept;
void Destroy(VkDevice, VkShaderModule, const DeviceDispatch&) noexcept;
void Destroy(VkInstance, VkDebugUtilsMessengerEXT, const InstanceDispatch&) noexcept;
void Destroy(VkInstance, VkDebugReportCallbackEXT, const InstanceDispatch&) noexcept;
void Destroy(VkInstance, VkSurfaceKHR, const InstanceDispatch&) noexcept;

VkResult Free(VkDevice, VkDescriptorPool, Span<VkDescriptorSet>, const DeviceDispatch&) noexcept;
VkResult Free(VkDevice, VkCommandPool, Span<VkCommandBuffer>, const DeviceDispatch&) noexcept;

template <typename Type, typename OwnerType, typename Dispatch>
class Handle;

/// Handle with an owning type.
/// Analogue to std::unique_ptr.
template <typename Type, typename OwnerType, typename Dispatch>
class Handle {
public:
    /// Construct a handle and hold it's ownership.
    explicit Handle(Type handle_, OwnerType owner_, const Dispatch& dld_) noexcept
        : handle{handle_}, owner{owner_}, dld{&dld_} {}

    /// Construct an empty handle.
    Handle() = default;

    /// Construct an empty handle.
    Handle(std::nullptr_t) {}

    /// Copying Vulkan objects is not supported and will never be.
    Handle(const Handle&) = delete;
    Handle& operator=(const Handle&) = delete;

    /// Construct a handle transferring the ownership from another handle.
    Handle(Handle&& rhs) noexcept
        : handle{std::exchange(rhs.handle, nullptr)}, owner{rhs.owner}, dld{rhs.dld} {}

    /// Assign the current handle transferring the ownership from another handle.
    /// Destroys any previously held object.
    Handle& operator=(Handle&& rhs) noexcept {
        Release();
        handle = std::exchange(rhs.handle, nullptr);
        owner = rhs.owner;
        dld = rhs.dld;
        return *this;
    }

    /// Destroys the current handle if it existed.
    ~Handle() noexcept {
        Release();
    }

    /// Destroys any held object.
    void reset() noexcept {
        Release();
        handle = nullptr;
    }

    /// Returns the address of the held object.
    /// Intended for Vulkan structures that expect a pointer to an array.
    const Type* address() const noexcept {
        return std::addressof(handle);
    }

    /// Returns the held Vulkan handle.
    Type operator*() const noexcept {
        return handle;
    }

    /// Returns true when there's a held object.
    explicit operator bool() const noexcept {
        return handle != nullptr;
    }

protected:
    Type handle = nullptr;
    OwnerType owner = nullptr;
    const Dispatch* dld = nullptr;

private:
    /// Destroys the held object if it exists.
    void Release() noexcept {
        if (handle) {
            Destroy(owner, handle, *dld);
        }
    }
};

/// Dummy type used to specify a handle has no owner.
struct NoOwner {};

/// Handle without an owning type.
/// Analogue to std::unique_ptr
template <typename Type, typename Dispatch>
class Handle<Type, NoOwner, Dispatch> {
public:
    /// Construct a handle and hold it's ownership.
    explicit Handle(Type handle_, const Dispatch& dld_) noexcept : handle{handle_}, dld{&dld_} {}

    /// Construct an empty handle.
    Handle() noexcept = default;

    /// Copying Vulkan objects is not supported and will never be.
    Handle(const Handle&) = delete;
    Handle& operator=(const Handle&) = delete;

    /// Construct a handle transferring ownership from another handle.
    Handle(Handle&& rhs) noexcept : handle{std::exchange(rhs.handle, nullptr)}, dld{rhs.dld} {}

    /// Assign the current handle transferring the ownership from another handle.
    /// Destroys any previously held object.
    Handle& operator=(Handle&& rhs) noexcept {
        Release();
        handle = std::exchange(rhs.handle, nullptr);
        dld = rhs.dld;
        return *this;
    }

    /// Destroys the current handle if it existed.
    ~Handle() noexcept {
        Release();
    }

    /// Destroys any held object.
    void reset() noexcept {
        Release();
        handle = nullptr;
    }

    /// Returns the address of the held object.
    /// Intended for Vulkan structures that expect a pointer to an array.
    const Type* address() const noexcept {
        return std::addressof(handle);
    }

    /// Returns the held Vulkan handle.
    Type operator*() const noexcept {
        return handle;
    }

    /// Returns true when there's a held object.
    operator bool() const noexcept {
        return handle != nullptr;
    }

protected:
    Type handle = nullptr;
    const Dispatch* dld = nullptr;

private:
    /// Destroys the held object if it exists.
    void Release() noexcept {
        if (handle) {
            Destroy(handle, *dld);
        }
    }
};

/// Array of a pool allocation.
/// Analogue to std::vector
template <typename AllocationType, typename PoolType>
class PoolAllocations {
public:
    /// Construct an empty allocation.
    PoolAllocations() = default;

    /// Construct an allocation. Errors are reported through IsOutOfPoolMemory().
    explicit PoolAllocations(std::unique_ptr<AllocationType[]> allocations_, std::size_t num_,
                             VkDevice device_, PoolType pool_, const DeviceDispatch& dld_) noexcept
        : allocations{std::move(allocations_)}, num{num_}, device{device_}, pool{pool_},
          dld{&dld_} {}

    /// Copying Vulkan allocations is not supported and will never be.
    PoolAllocations(const PoolAllocations&) = delete;
    PoolAllocations& operator=(const PoolAllocations&) = delete;

    /// Construct an allocation transferring ownership from another allocation.
    PoolAllocations(PoolAllocations&& rhs) noexcept
        : allocations{std::move(rhs.allocations)}, num{rhs.num}, device{rhs.device}, pool{rhs.pool},
          dld{rhs.dld} {}

    /// Assign an allocation transferring ownership from another allocation.
    PoolAllocations& operator=(PoolAllocations&& rhs) noexcept {
        allocations = std::move(rhs.allocations);
        num = rhs.num;
        device = rhs.device;
        pool = rhs.pool;
        dld = rhs.dld;
        return *this;
    }

    /// Returns the number of allocations.
    std::size_t size() const noexcept {
        return num;
    }

    /// Returns a pointer to the array of allocations.
    AllocationType const* data() const noexcept {
        return allocations.get();
    }

    /// Returns the allocation in the specified index.
    /// @pre index < size()
    AllocationType operator[](std::size_t index) const noexcept {
        return allocations[index];
    }

    /// True when a pool fails to construct.
    bool IsOutOfPoolMemory() const noexcept {
        return !device;
    }

private:
    std::unique_ptr<AllocationType[]> allocations;
    std::size_t num = 0;
    VkDevice device = nullptr;
    PoolType pool = nullptr;
    const DeviceDispatch* dld = nullptr;
};

using DebugUtilsMessenger = Handle<VkDebugUtilsMessengerEXT, VkInstance, InstanceDispatch>;
using DebugReportCallback = Handle<VkDebugReportCallbackEXT, VkInstance, InstanceDispatch>;
using DescriptorSetLayout = Handle<VkDescriptorSetLayout, VkDevice, DeviceDispatch>;
using DescriptorUpdateTemplate = Handle<VkDescriptorUpdateTemplate, VkDevice, DeviceDispatch>;
using Pipeline = Handle<VkPipeline, VkDevice, DeviceDispatch>;
using PipelineLayout = Handle<VkPipelineLayout, VkDevice, DeviceDispatch>;
using QueryPool = Handle<VkQueryPool, VkDevice, DeviceDispatch>;
using RenderPass = Handle<VkRenderPass, VkDevice, DeviceDispatch>;
using Sampler = Handle<VkSampler, VkDevice, DeviceDispatch>;
using SurfaceKHR = Handle<VkSurfaceKHR, VkInstance, InstanceDispatch>;

using DescriptorSets = PoolAllocations<VkDescriptorSet, VkDescriptorPool>;
using CommandBuffers = PoolAllocations<VkCommandBuffer, VkCommandPool>;

/// Vulkan instance owning handle.
class Instance : public Handle<VkInstance, NoOwner, InstanceDispatch> {
    using Handle<VkInstance, NoOwner, InstanceDispatch>::Handle;

public:
    /// Creates a Vulkan instance.
    /// @throw Exception on initialization error.
    static Instance Create(u32 version, Span<const char*> layers, Span<const char*> extensions,
                           InstanceDispatch& dispatch);

    /// Enumerates physical devices.
    /// @return Physical devices and an empty handle on failure.
    /// @throw Exception on Vulkan error.
    std::vector<VkPhysicalDevice> EnumeratePhysicalDevices() const;

    /// Creates a debug callback messenger.
    /// @throw Exception on creation failure.
    DebugUtilsMessenger CreateDebugUtilsMessenger(
        const VkDebugUtilsMessengerCreateInfoEXT& create_info) const;

    /// Creates a debug report callback.
    /// @throw Exception on creation failure.
    DebugReportCallback CreateDebugReportCallback(
        const VkDebugReportCallbackCreateInfoEXT& create_info) const;

    /// Returns dispatch table.
    const InstanceDispatch& Dispatch() const noexcept {
        return *dld;
    }
};

class Image {
public:
    explicit Image(VkImage handle_, VkDevice owner_, VmaAllocator allocator_,
                   VmaAllocation allocation_, const DeviceDispatch& dld_) noexcept
        : handle{handle_}, owner{owner_}, allocator{allocator_},
          allocation{allocation_}, dld{&dld_} {}
    Image() = default;

    Image(const Image&) = delete;
    Image& operator=(const Image&) = delete;

    Image(Image&& rhs) noexcept
        : handle{std::exchange(rhs.handle, nullptr)}, owner{rhs.owner}, allocator{rhs.allocator},
          allocation{rhs.allocation}, dld{rhs.dld} {}

    Image& operator=(Image&& rhs) noexcept {
        Release();
        handle = std::exchange(rhs.handle, nullptr);
        owner = rhs.owner;
        allocator = rhs.allocator;
        allocation = rhs.allocation;
        dld = rhs.dld;
        return *this;
    }

    ~Image() noexcept {
        Release();
    }

    VkImage operator*() const noexcept {
        return handle;
    }

    void reset() noexcept {
        Release();
        handle = nullptr;
    }

    explicit operator bool() const noexcept {
        return handle != nullptr;
    }

    void SetObjectNameEXT(const char* name) const;

private:
    void Release() const noexcept;

    VkImage handle = nullptr;
    VkDevice owner = nullptr;
    VmaAllocator allocator = nullptr;
    VmaAllocation allocation = nullptr;
    const DeviceDispatch* dld = nullptr;
};

class Buffer {
public:
    explicit Buffer(VkBuffer handle_, VkDevice owner_, VmaAllocator allocator_,
                    VmaAllocation allocation_, std::span<u8> mapped_, bool is_coherent_,
                    const DeviceDispatch& dld_) noexcept
        : handle{handle_}, owner{owner_}, allocator{allocator_},
          allocation{allocation_}, mapped{mapped_}, is_coherent{is_coherent_}, dld{&dld_} {}
    Buffer() = default;

    Buffer(const Buffer&) = delete;
    Buffer& operator=(const Buffer&) = delete;

    Buffer(Buffer&& rhs) noexcept
        : handle{std::exchange(rhs.handle, nullptr)}, owner{rhs.owner}, allocator{rhs.allocator},
          allocation{rhs.allocation}, mapped{rhs.mapped},
          is_coherent{rhs.is_coherent}, dld{rhs.dld} {}

    Buffer& operator=(Buffer&& rhs) noexcept {
        Release();
        handle = std::exchange(rhs.handle, nullptr);
        owner = rhs.owner;
        allocator = rhs.allocator;
        allocation = rhs.allocation;
        mapped = rhs.mapped;
        is_coherent = rhs.is_coherent;
        dld = rhs.dld;
        return *this;
    }

    ~Buffer() noexcept {
        Release();
    }

    VkBuffer operator*() const noexcept {
        return handle;
    }

    void reset() noexcept {
        Release();
        handle = nullptr;
    }

    explicit operator bool() const noexcept {
        return handle != nullptr;
    }

    /// Returns the host mapped memory, an empty span otherwise.
    std::span<u8> Mapped() noexcept {
        return mapped;
    }

    std::span<const u8> Mapped() const noexcept {
        return mapped;
    }

    /// Returns true if the buffer is mapped to the host.
    bool IsHostVisible() const noexcept {
        return !mapped.empty();
    }

    void Flush() const;

    void Invalidate() const;

    void SetObjectNameEXT(const char* name) const;

private:
    void Release() const noexcept;

    VkBuffer handle = nullptr;
    VkDevice owner = nullptr;
    VmaAllocator allocator = nullptr;
    VmaAllocation allocation = nullptr;
    std::span<u8> mapped = {};
    bool is_coherent = false;
    const DeviceDispatch* dld = nullptr;
};

class Queue {
public:
    /// Construct an empty queue handle.
    constexpr Queue() noexcept = default;

    /// Construct a queue handle.
    constexpr Queue(VkQueue queue_, const DeviceDispatch& dld_) noexcept
        : queue{queue_}, dld{&dld_} {}

    VkResult Submit(Span<VkSubmitInfo> submit_infos,
                    VkFence fence = VK_NULL_HANDLE) const noexcept {
        return dld->vkQueueSubmit(queue, submit_infos.size(), submit_infos.data(), fence);
    }

    VkResult Present(const VkPresentInfoKHR& present_info) const noexcept {
        return dld->vkQueuePresentKHR(queue, &present_info);
    }

private:
    VkQueue queue = nullptr;
    const DeviceDispatch* dld = nullptr;
};

class BufferView : public Handle<VkBufferView, VkDevice, DeviceDispatch> {
    using Handle<VkBufferView, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;
};

class ImageView : public Handle<VkImageView, VkDevice, DeviceDispatch> {
    using Handle<VkImageView, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;
};

class DeviceMemory : public Handle<VkDeviceMemory, VkDevice, DeviceDispatch> {
    using Handle<VkDeviceMemory, VkDevice, DeviceDispatch>::Handle;

public:
    int GetMemoryFdKHR() const;

#ifdef _WIN32
    HANDLE GetMemoryWin32HandleKHR() const;
#endif

    /// Set object name.
    void SetObjectNameEXT(const char* name) const;

    u8* Map(VkDeviceSize offset, VkDeviceSize size) const {
        void* data;
        Check(dld->vkMapMemory(owner, handle, offset, size, 0, &data));
        return static_cast<u8*>(data);
    }

    void Unmap() const noexcept {
        dld->vkUnmapMemory(owner, handle);
    }
};

class Fence : public Handle<VkFence, VkDevice, DeviceDispatch> {
    using Handle<VkFence, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;

    VkResult Wait(u64 timeout = std::numeric_limits<u64>::max()) const noexcept {
        return dld->vkWaitForFences(owner, 1, &handle, true, timeout);
    }

    VkResult GetStatus() const noexcept {
        return dld->vkGetFenceStatus(owner, handle);
    }

    void Reset() const {
        Check(dld->vkResetFences(owner, 1, &handle));
    }
};

class Framebuffer : public Handle<VkFramebuffer, VkDevice, DeviceDispatch> {
    using Handle<VkFramebuffer, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;
};

class DescriptorPool : public Handle<VkDescriptorPool, VkDevice, DeviceDispatch> {
    using Handle<VkDescriptorPool, VkDevice, DeviceDispatch>::Handle;

public:
    DescriptorSets Allocate(const VkDescriptorSetAllocateInfo& ai) const;

    /// Set object name.
    void SetObjectNameEXT(const char* name) const;
};

class CommandPool : public Handle<VkCommandPool, VkDevice, DeviceDispatch> {
    using Handle<VkCommandPool, VkDevice, DeviceDispatch>::Handle;

public:
    CommandBuffers Allocate(std::size_t num_buffers,
                            VkCommandBufferLevel level = VK_COMMAND_BUFFER_LEVEL_PRIMARY) const;

    /// Set object name.
    void SetObjectNameEXT(const char* name) const;
};

class SwapchainKHR : public Handle<VkSwapchainKHR, VkDevice, DeviceDispatch> {
    using Handle<VkSwapchainKHR, VkDevice, DeviceDispatch>::Handle;

public:
    std::vector<VkImage> GetImages() const;
};

class Event : public Handle<VkEvent, VkDevice, DeviceDispatch> {
    using Handle<VkEvent, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;

    VkResult GetStatus() const noexcept {
        return dld->vkGetEventStatus(owner, handle);
    }
};

class ShaderModule : public Handle<VkShaderModule, VkDevice, DeviceDispatch> {
    using Handle<VkShaderModule, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;
};

class PipelineCache : public Handle<VkPipelineCache, VkDevice, DeviceDispatch> {
    using Handle<VkPipelineCache, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;

    VkResult Read(size_t* size, void* data) const noexcept {
        return dld->vkGetPipelineCacheData(owner, handle, size, data);
    }
};

class Semaphore : public Handle<VkSemaphore, VkDevice, DeviceDispatch> {
    using Handle<VkSemaphore, VkDevice, DeviceDispatch>::Handle;

public:
    /// Set object name.
    void SetObjectNameEXT(const char* name) const;

    [[nodiscard]] u64 GetCounter() const {
        u64 value;
        Check(dld->vkGetSemaphoreCounterValue(owner, handle, &value));
        return value;
    }

    /**
     * Waits for a timeline semaphore on the host.
     *
     * @param value   Value to wait
     * @param timeout Time in nanoseconds to timeout
     * @return        True on successful wait, false on timeout
     */
    bool Wait(u64 value, u64 timeout = std::numeric_limits<u64>::max()) const {
        const VkSemaphoreWaitInfo wait_info{
            .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
            .pNext = nullptr,
            .flags = 0,
            .semaphoreCount = 1,
            .pSemaphores = &handle,
            .pValues = &value,
        };
        const VkResult result = dld->vkWaitSemaphores(owner, &wait_info, timeout);
        switch (result) {
        case VK_SUCCESS:
            return true;
        case VK_TIMEOUT:
            return false;
        default:
            throw Exception(result);
        }
    }
};

class Device : public Handle<VkDevice, NoOwner, DeviceDispatch> {
    using Handle<VkDevice, NoOwner, DeviceDispatch>::Handle;

public:
    static Device Create(VkPhysicalDevice physical_device, Span<VkDeviceQueueCreateInfo> queues_ci,
                         Span<const char*> enabled_extensions, const void* next,
                         DeviceDispatch& dispatch);

    Queue GetQueue(u32 family_index) const noexcept;

    BufferView CreateBufferView(const VkBufferViewCreateInfo& ci) const;

    ImageView CreateImageView(const VkImageViewCreateInfo& ci) const;

    Semaphore CreateSemaphore() const;

    Semaphore CreateSemaphore(const VkSemaphoreCreateInfo& ci) const;

    Fence CreateFence(const VkFenceCreateInfo& ci) const;

    DescriptorPool CreateDescriptorPool(const VkDescriptorPoolCreateInfo& ci) const;

    RenderPass CreateRenderPass(const VkRenderPassCreateInfo& ci) const;

    DescriptorSetLayout CreateDescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo& ci) const;

    PipelineCache CreatePipelineCache(const VkPipelineCacheCreateInfo& ci) const;

    PipelineLayout CreatePipelineLayout(const VkPipelineLayoutCreateInfo& ci) const;

    Pipeline CreateGraphicsPipeline(const VkGraphicsPipelineCreateInfo& ci,
                                    VkPipelineCache cache = nullptr) const;

    Pipeline CreateComputePipeline(const VkComputePipelineCreateInfo& ci,
                                   VkPipelineCache cache = nullptr) const;

    Sampler CreateSampler(const VkSamplerCreateInfo& ci) const;

    Framebuffer CreateFramebuffer(const VkFramebufferCreateInfo& ci) const;

    CommandPool CreateCommandPool(const VkCommandPoolCreateInfo& ci) const;

    DescriptorUpdateTemplate CreateDescriptorUpdateTemplate(
        const VkDescriptorUpdateTemplateCreateInfo& ci) const;

    QueryPool CreateQueryPool(const VkQueryPoolCreateInfo& ci) const;

    ShaderModule CreateShaderModule(const VkShaderModuleCreateInfo& ci) const;

    Event CreateEvent() const;

    SwapchainKHR CreateSwapchainKHR(const VkSwapchainCreateInfoKHR& ci) const;

    DeviceMemory TryAllocateMemory(const VkMemoryAllocateInfo& ai) const noexcept;

    DeviceMemory AllocateMemory(const VkMemoryAllocateInfo& ai) const;

    VkMemoryRequirements GetBufferMemoryRequirements(VkBuffer buffer,
                                                     void* pnext = nullptr) const noexcept;

    VkMemoryRequirements GetImageMemoryRequirements(VkImage image) const noexcept;

    std::vector<VkPipelineExecutablePropertiesKHR> GetPipelineExecutablePropertiesKHR(
        VkPipeline pipeline) const;

    std::vector<VkPipelineExecutableStatisticKHR> GetPipelineExecutableStatisticsKHR(
        VkPipeline pipeline, u32 executable_index) const;

    void UpdateDescriptorSets(Span<VkWriteDescriptorSet> writes,
                              Span<VkCopyDescriptorSet> copies) const noexcept;

    void UpdateDescriptorSet(VkDescriptorSet set, VkDescriptorUpdateTemplate update_template,
                             const void* data) const noexcept {
        dld->vkUpdateDescriptorSetWithTemplate(handle, set, update_template, data);
    }

    VkResult AcquireNextImageKHR(VkSwapchainKHR swapchain, u64 timeout, VkSemaphore semaphore,
                                 VkFence fence, u32* image_index) const noexcept {
        return dld->vkAcquireNextImageKHR(handle, swapchain, timeout, semaphore, fence,
                                          image_index);
    }

    VkResult WaitIdle() const noexcept {
        return dld->vkDeviceWaitIdle(handle);
    }

    void ResetQueryPool(VkQueryPool query_pool, u32 first, u32 count) const noexcept {
        dld->vkResetQueryPool(handle, query_pool, first, count);
    }

    VkResult GetQueryResults(VkQueryPool query_pool, u32 first, u32 count, std::size_t data_size,
                             void* data, VkDeviceSize stride,
                             VkQueryResultFlags flags) const noexcept {
        return dld->vkGetQueryPoolResults(handle, query_pool, first, count, data_size, data, stride,
                                          flags);
    }
};

class PhysicalDevice {
public:
    constexpr PhysicalDevice() noexcept = default;

    constexpr PhysicalDevice(VkPhysicalDevice physical_device_,
                             const InstanceDispatch& dld_) noexcept
        : physical_device{physical_device_}, dld{&dld_} {}

    constexpr operator VkPhysicalDevice() const noexcept {
        return physical_device;
    }

    VkPhysicalDeviceProperties GetProperties() const noexcept;

    void GetProperties2(VkPhysicalDeviceProperties2&) const noexcept;

    VkPhysicalDeviceFeatures GetFeatures() const noexcept;

    void GetFeatures2(VkPhysicalDeviceFeatures2&) const noexcept;

    VkFormatProperties GetFormatProperties(VkFormat) const noexcept;

    std::vector<VkExtensionProperties> EnumerateDeviceExtensionProperties() const;

    std::vector<VkQueueFamilyProperties> GetQueueFamilyProperties() const;

    std::vector<VkPhysicalDeviceToolProperties> GetPhysicalDeviceToolProperties() const;

    bool GetSurfaceSupportKHR(u32 queue_family_index, VkSurfaceKHR) const;

    VkSurfaceCapabilitiesKHR GetSurfaceCapabilitiesKHR(VkSurfaceKHR) const;

    std::vector<VkSurfaceFormatKHR> GetSurfaceFormatsKHR(VkSurfaceKHR) const;

    std::vector<VkPresentModeKHR> GetSurfacePresentModesKHR(VkSurfaceKHR) const;

    VkPhysicalDeviceMemoryProperties2 GetMemoryProperties(
        void* next_structures = nullptr) const noexcept;

private:
    VkPhysicalDevice physical_device = nullptr;
    const InstanceDispatch* dld = nullptr;
};

class CommandBuffer {
public:
    CommandBuffer() noexcept = default;

    explicit CommandBuffer(VkCommandBuffer handle_, const DeviceDispatch& dld_) noexcept
        : handle{handle_}, dld{&dld_} {}

    const VkCommandBuffer* address() const noexcept {
        return &handle;
    }

    VkCommandBuffer operator*() const noexcept {
        return handle;
    }

    void Begin(const VkCommandBufferBeginInfo& begin_info) const {
        Check(dld->vkBeginCommandBuffer(handle, &begin_info));
    }

    void End() const {
        Check(dld->vkEndCommandBuffer(handle));
    }

    void BeginRenderPass(const VkRenderPassBeginInfo& renderpass_bi,
                         VkSubpassContents contents) const noexcept {
        dld->vkCmdBeginRenderPass(handle, &renderpass_bi, contents);
    }

    void EndRenderPass() const noexcept {
        dld->vkCmdEndRenderPass(handle);
    }

    void BeginQuery(VkQueryPool query_pool, u32 query, VkQueryControlFlags flags) const noexcept {
        dld->vkCmdBeginQuery(handle, query_pool, query, flags);
    }

    void EndQuery(VkQueryPool query_pool, u32 query) const noexcept {
        dld->vkCmdEndQuery(handle, query_pool, query);
    }

    void BindDescriptorSets(VkPipelineBindPoint bind_point, VkPipelineLayout layout, u32 first,
                            Span<VkDescriptorSet> sets, Span<u32> dynamic_offsets) const noexcept {
        dld->vkCmdBindDescriptorSets(handle, bind_point, layout, first, sets.size(), sets.data(),
                                     dynamic_offsets.size(), dynamic_offsets.data());
    }

    void PushDescriptorSetWithTemplateKHR(VkDescriptorUpdateTemplate update_template,
                                          VkPipelineLayout layout, u32 set,
                                          const void* data) const noexcept {
        dld->vkCmdPushDescriptorSetWithTemplateKHR(handle, update_template, layout, set, data);
    }

    void BindPipeline(VkPipelineBindPoint bind_point, VkPipeline pipeline) const noexcept {
        dld->vkCmdBindPipeline(handle, bind_point, pipeline);
    }

    void BindIndexBuffer(VkBuffer buffer, VkDeviceSize offset,
                         VkIndexType index_type) const noexcept {
        dld->vkCmdBindIndexBuffer(handle, buffer, offset, index_type);
    }

    void BindVertexBuffers(u32 first, u32 count, const VkBuffer* buffers,
                           const VkDeviceSize* offsets) const noexcept {
        dld->vkCmdBindVertexBuffers(handle, first, count, buffers, offsets);
    }

    void BindVertexBuffer(u32 binding, VkBuffer buffer, VkDeviceSize offset) const noexcept {
        BindVertexBuffers(binding, 1, &buffer, &offset);
    }

    void Draw(u32 vertex_count, u32 instance_count, u32 first_vertex,
              u32 first_instance) const noexcept {
        dld->vkCmdDraw(handle, vertex_count, instance_count, first_vertex, first_instance);
    }

    void DrawIndexed(u32 index_count, u32 instance_count, u32 first_index, u32 vertex_offset,
                     u32 first_instance) const noexcept {
        dld->vkCmdDrawIndexed(handle, index_count, instance_count, first_index, vertex_offset,
                              first_instance);
    }

    void DrawIndirect(VkBuffer src_buffer, VkDeviceSize src_offset, u32 draw_count,
                      u32 stride) const noexcept {
        dld->vkCmdDrawIndirect(handle, src_buffer, src_offset, draw_count, stride);
    }

    void DrawIndexedIndirect(VkBuffer src_buffer, VkDeviceSize src_offset, u32 draw_count,
                             u32 stride) const noexcept {
        dld->vkCmdDrawIndexedIndirect(handle, src_buffer, src_offset, draw_count, stride);
    }

    void DrawIndirectCount(VkBuffer src_buffer, VkDeviceSize src_offset, VkBuffer count_buffer,
                           VkDeviceSize count_offset, u32 draw_count, u32 stride) const noexcept {
        dld->vkCmdDrawIndirectCount(handle, src_buffer, src_offset, count_buffer, count_offset,
                                    draw_count, stride);
    }

    void DrawIndexedIndirectCount(VkBuffer src_buffer, VkDeviceSize src_offset,
                                  VkBuffer count_buffer, VkDeviceSize count_offset, u32 draw_count,
                                  u32 stride) const noexcept {
        dld->vkCmdDrawIndexedIndirectCount(handle, src_buffer, src_offset, count_buffer,
                                           count_offset, draw_count, stride);
    }

    void DrawIndirectByteCountEXT(u32 instance_count, u32 first_instance, VkBuffer counter_buffer,
                                  VkDeviceSize counter_buffer_offset, u32 counter_offset,
                                  u32 stride) {
        dld->vkCmdDrawIndirectByteCountEXT(handle, instance_count, first_instance, counter_buffer,
                                           counter_buffer_offset, counter_offset, stride);
    }

    void ClearAttachments(Span<VkClearAttachment> attachments,
                          Span<VkClearRect> rects) const noexcept {
        dld->vkCmdClearAttachments(handle, attachments.size(), attachments.data(), rects.size(),
                                   rects.data());
    }

    void ClearColorImage(VkImage image, VkImageLayout layout, VkClearColorValue color,
                         Span<VkImageSubresourceRange> ranges) {
        dld->vkCmdClearColorImage(handle, image, layout, &color, ranges.size(), ranges.data());
    }

    void BlitImage(VkImage src_image, VkImageLayout src_layout, VkImage dst_image,
                   VkImageLayout dst_layout, Span<VkImageBlit> regions,
                   VkFilter filter) const noexcept {
        dld->vkCmdBlitImage(handle, src_image, src_layout, dst_image, dst_layout, regions.size(),
                            regions.data(), filter);
    }

    void ResolveImage(VkImage src_image, VkImageLayout src_layout, VkImage dst_image,
                      VkImageLayout dst_layout, Span<VkImageResolve> regions) {
        dld->vkCmdResolveImage(handle, src_image, src_layout, dst_image, dst_layout, regions.size(),
                               regions.data());
    }

    void Dispatch(u32 x, u32 y, u32 z) const noexcept {
        dld->vkCmdDispatch(handle, x, y, z);
    }

    void DispatchIndirect(VkBuffer indirect_buffer, VkDeviceSize offset) const noexcept {
        dld->vkCmdDispatchIndirect(handle, indirect_buffer, offset);
    }

    void PipelineBarrier(VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
                         VkDependencyFlags dependency_flags, Span<VkMemoryBarrier> memory_barriers,
                         Span<VkBufferMemoryBarrier> buffer_barriers,
                         Span<VkImageMemoryBarrier> image_barriers) const noexcept {
        dld->vkCmdPipelineBarrier(handle, src_stage_mask, dst_stage_mask, dependency_flags,
                                  memory_barriers.size(), memory_barriers.data(),
                                  buffer_barriers.size(), buffer_barriers.data(),
                                  image_barriers.size(), image_barriers.data());
    }

    void PipelineBarrier(VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
                         VkDependencyFlags dependency_flags = 0) const noexcept {
        PipelineBarrier(src_stage_mask, dst_stage_mask, dependency_flags, {}, {}, {});
    }

    void PipelineBarrier(VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
                         VkDependencyFlags dependency_flags,
                         const VkMemoryBarrier& memory_barrier) const noexcept {
        PipelineBarrier(src_stage_mask, dst_stage_mask, dependency_flags, memory_barrier, {}, {});
    }

    void PipelineBarrier(VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
                         VkDependencyFlags dependency_flags,
                         const VkBufferMemoryBarrier& buffer_barrier) const noexcept {
        PipelineBarrier(src_stage_mask, dst_stage_mask, dependency_flags, {}, buffer_barrier, {});
    }

    void PipelineBarrier(VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask,
                         VkDependencyFlags dependency_flags,
                         const VkImageMemoryBarrier& image_barrier) const noexcept {
        PipelineBarrier(src_stage_mask, dst_stage_mask, dependency_flags, {}, {}, image_barrier);
    }

    void CopyBufferToImage(VkBuffer src_buffer, VkImage dst_image, VkImageLayout dst_image_layout,
                           Span<VkBufferImageCopy> regions) const noexcept {
        dld->vkCmdCopyBufferToImage(handle, src_buffer, dst_image, dst_image_layout, regions.size(),
                                    regions.data());
    }

    void CopyBuffer(VkBuffer src_buffer, VkBuffer dst_buffer,
                    Span<VkBufferCopy> regions) const noexcept {
        dld->vkCmdCopyBuffer(handle, src_buffer, dst_buffer, regions.size(), regions.data());
    }

    void CopyImage(VkImage src_image, VkImageLayout src_layout, VkImage dst_image,
                   VkImageLayout dst_layout, Span<VkImageCopy> regions) const noexcept {
        dld->vkCmdCopyImage(handle, src_image, src_layout, dst_image, dst_layout, regions.size(),
                            regions.data());
    }

    void CopyImageToBuffer(VkImage src_image, VkImageLayout src_layout, VkBuffer dst_buffer,
                           Span<VkBufferImageCopy> regions) const noexcept {
        dld->vkCmdCopyImageToBuffer(handle, src_image, src_layout, dst_buffer, regions.size(),
                                    regions.data());
    }

    void CopyQueryPoolResults(VkQueryPool query_pool, u32 first_query, u32 query_count,
                              VkBuffer dst_buffer, VkDeviceSize dst_offset, VkDeviceSize stride,
                              VkQueryResultFlags flags) const noexcept {
        dld->vkCmdCopyQueryPoolResults(handle, query_pool, first_query, query_count, dst_buffer,
                                       dst_offset, stride, flags);
    }

    void FillBuffer(VkBuffer dst_buffer, VkDeviceSize dst_offset, VkDeviceSize size,
                    u32 data) const noexcept {
        dld->vkCmdFillBuffer(handle, dst_buffer, dst_offset, size, data);
    }

    void PushConstants(VkPipelineLayout layout, VkShaderStageFlags flags, u32 offset, u32 size,
                       const void* values) const noexcept {
        dld->vkCmdPushConstants(handle, layout, flags, offset, size, values);
    }

    template <typename T>
    void PushConstants(VkPipelineLayout layout, VkShaderStageFlags flags,
                       const T& data) const noexcept {
        static_assert(std::is_trivially_copyable_v<T>, "<data> is not trivially copyable");
        dld->vkCmdPushConstants(handle, layout, flags, 0, static_cast<u32>(sizeof(T)), &data);
    }

    void SetViewport(u32 first, Span<VkViewport> viewports) const noexcept {
        dld->vkCmdSetViewport(handle, first, viewports.size(), viewports.data());
    }

    void SetScissor(u32 first, Span<VkRect2D> scissors) const noexcept {
        dld->vkCmdSetScissor(handle, first, scissors.size(), scissors.data());
    }

    void SetBlendConstants(const float blend_constants[4]) const noexcept {
        dld->vkCmdSetBlendConstants(handle, blend_constants);
    }

    void SetStencilCompareMask(VkStencilFaceFlags face_mask, u32 compare_mask) const noexcept {
        dld->vkCmdSetStencilCompareMask(handle, face_mask, compare_mask);
    }

    void SetStencilReference(VkStencilFaceFlags face_mask, u32 reference) const noexcept {
        dld->vkCmdSetStencilReference(handle, face_mask, reference);
    }

    void SetStencilWriteMask(VkStencilFaceFlags face_mask, u32 write_mask) const noexcept {
        dld->vkCmdSetStencilWriteMask(handle, face_mask, write_mask);
    }

    void SetDepthBias(float constant_factor, float clamp, float slope_factor) const noexcept {
        dld->vkCmdSetDepthBias(handle, constant_factor, clamp, slope_factor);
    }

    void SetDepthBias(float constant_factor, float clamp, float slope_factor,
                      VkDepthBiasRepresentationInfoEXT* extra) const noexcept {
        VkDepthBiasInfoEXT info{
            .sType = VK_STRUCTURE_TYPE_DEPTH_BIAS_INFO_EXT,
            .pNext = extra,
            .depthBiasConstantFactor = constant_factor,
            .depthBiasClamp = clamp,
            .depthBiasSlopeFactor = slope_factor,
        };
        dld->vkCmdSetDepthBias2EXT(handle, &info);
    }

    void SetDepthBounds(float min_depth_bounds, float max_depth_bounds) const noexcept {
        dld->vkCmdSetDepthBounds(handle, min_depth_bounds, max_depth_bounds);
    }

    void SetEvent(VkEvent event, VkPipelineStageFlags stage_flags) const noexcept {
        dld->vkCmdSetEvent(handle, event, stage_flags);
    }

    void WaitEvents(Span<VkEvent> events, VkPipelineStageFlags src_stage_mask,
                    VkPipelineStageFlags dst_stage_mask, Span<VkMemoryBarrier> memory_barriers,
                    Span<VkBufferMemoryBarrier> buffer_barriers,
                    Span<VkImageMemoryBarrier> image_barriers) const noexcept {
        dld->vkCmdWaitEvents(handle, events.size(), events.data(), src_stage_mask, dst_stage_mask,
                             memory_barriers.size(), memory_barriers.data(), buffer_barriers.size(),
                             buffer_barriers.data(), image_barriers.size(), image_barriers.data());
    }

    void BindVertexBuffers2EXT(u32 first_binding, u32 binding_count, const VkBuffer* buffers,
                               const VkDeviceSize* offsets, const VkDeviceSize* sizes,
                               const VkDeviceSize* strides) const noexcept {
        dld->vkCmdBindVertexBuffers2EXT(handle, first_binding, binding_count, buffers, offsets,
                                        sizes, strides);
    }

    void SetCullModeEXT(VkCullModeFlags cull_mode) const noexcept {
        dld->vkCmdSetCullModeEXT(handle, cull_mode);
    }

    void SetDepthBoundsTestEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetDepthBoundsTestEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetDepthCompareOpEXT(VkCompareOp compare_op) const noexcept {
        dld->vkCmdSetDepthCompareOpEXT(handle, compare_op);
    }

    void SetDepthTestEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetDepthTestEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetDepthWriteEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetDepthWriteEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetPrimitiveRestartEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetPrimitiveRestartEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetRasterizerDiscardEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetRasterizerDiscardEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetDepthBiasEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetDepthBiasEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetLogicOpEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetLogicOpEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetDepthClampEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetDepthClampEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetFrontFaceEXT(VkFrontFace front_face) const noexcept {
        dld->vkCmdSetFrontFaceEXT(handle, front_face);
    }

    void SetLogicOpEXT(VkLogicOp logic_op) const noexcept {
        dld->vkCmdSetLogicOpEXT(handle, logic_op);
    }

    void SetPatchControlPointsEXT(uint32_t patch_control_points) const noexcept {
        dld->vkCmdSetPatchControlPointsEXT(handle, patch_control_points);
    }

    void SetColorWriteMaskEXT(u32 first, Span<VkColorComponentFlags> masks) const noexcept {
        dld->vkCmdSetColorWriteMaskEXT(handle, first, masks.size(), masks.data());
    }

    void SetColorBlendEnableEXT(u32 first, Span<VkBool32> enables) const noexcept {
        dld->vkCmdSetColorBlendEnableEXT(handle, first, enables.size(), enables.data());
    }

    void SetColorBlendEquationEXT(u32 first,
                                  Span<VkColorBlendEquationEXT> equations) const noexcept {
        dld->vkCmdSetColorBlendEquationEXT(handle, first, equations.size(), equations.data());
    }

    void SetLineWidth(float line_width) const noexcept {
        dld->vkCmdSetLineWidth(handle, line_width);
    }

    void SetPrimitiveTopologyEXT(VkPrimitiveTopology primitive_topology) const noexcept {
        dld->vkCmdSetPrimitiveTopologyEXT(handle, primitive_topology);
    }

    void SetStencilOpEXT(VkStencilFaceFlags face_mask, VkStencilOp fail_op, VkStencilOp pass_op,
                         VkStencilOp depth_fail_op, VkCompareOp compare_op) const noexcept {
        dld->vkCmdSetStencilOpEXT(handle, face_mask, fail_op, pass_op, depth_fail_op, compare_op);
    }

    void SetStencilTestEnableEXT(bool enable) const noexcept {
        dld->vkCmdSetStencilTestEnableEXT(handle, enable ? VK_TRUE : VK_FALSE);
    }

    void SetVertexInputEXT(
        vk::Span<VkVertexInputBindingDescription2EXT> bindings,
        vk::Span<VkVertexInputAttributeDescription2EXT> attributes) const noexcept {
        dld->vkCmdSetVertexInputEXT(handle, bindings.size(), bindings.data(), attributes.size(),
                                    attributes.data());
    }

    void BindTransformFeedbackBuffersEXT(u32 first, u32 count, const VkBuffer* buffers,
                                         const VkDeviceSize* offsets,
                                         const VkDeviceSize* sizes) const noexcept {
        dld->vkCmdBindTransformFeedbackBuffersEXT(handle, first, count, buffers, offsets, sizes);
    }

    void BeginTransformFeedbackEXT(u32 first_counter_buffer, u32 counter_buffers_count,
                                   const VkBuffer* counter_buffers,
                                   const VkDeviceSize* counter_buffer_offsets) const noexcept {
        dld->vkCmdBeginTransformFeedbackEXT(handle, first_counter_buffer, counter_buffers_count,
                                            counter_buffers, counter_buffer_offsets);
    }

    void EndTransformFeedbackEXT(u32 first_counter_buffer, u32 counter_buffers_count,
                                 const VkBuffer* counter_buffers,
                                 const VkDeviceSize* counter_buffer_offsets) const noexcept {
        dld->vkCmdEndTransformFeedbackEXT(handle, first_counter_buffer, counter_buffers_count,
                                          counter_buffers, counter_buffer_offsets);
    }

    void BeginConditionalRenderingEXT(
        const VkConditionalRenderingBeginInfoEXT& info) const noexcept {
        dld->vkCmdBeginConditionalRenderingEXT(handle, &info);
    }

    void EndConditionalRenderingEXT() const noexcept {
        dld->vkCmdEndConditionalRenderingEXT(handle);
    }

    void BeginDebugUtilsLabelEXT(const char* label, std::span<float, 4> color) const noexcept {
        const VkDebugUtilsLabelEXT label_info{
            .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
            .pNext = nullptr,
            .pLabelName = label,
            .color{color[0], color[1], color[2], color[3]},
        };
        dld->vkCmdBeginDebugUtilsLabelEXT(handle, &label_info);
    }

    void EndDebugUtilsLabelEXT() const noexcept {
        dld->vkCmdEndDebugUtilsLabelEXT(handle);
    }

private:
    VkCommandBuffer handle;
    const DeviceDispatch* dld;
};

u32 AvailableVersion(const InstanceDispatch& dld) noexcept;

std::optional<std::vector<VkExtensionProperties>> EnumerateInstanceExtensionProperties(
    const InstanceDispatch& dld);

std::optional<std::vector<VkLayerProperties>> EnumerateInstanceLayerProperties(
    const InstanceDispatch& dld);

} // namespace Vulkan::vk


code:
# SPDX-FileCopyrightText: 2018 yuzu Emulator Project
# SPDX-License-Identifier: GPL-2.0-or-later

add_library(web_service STATIC
    announce_room_json.cpp
    announce_room_json.h
    precompiled_headers.h
    telemetry_json.cpp
    telemetry_json.h
    verify_login.cpp
    verify_login.h
    verify_user_jwt.cpp
    verify_user_jwt.h
    web_backend.cpp
    web_backend.h
    web_result.h
)

create_target_directory_groups(web_service)
target_link_libraries(web_service PRIVATE common network nlohmann_json::nlohmann_json httplib::httplib cpp-jwt::cpp-jwt)

if (YUZU_USE_PRECOMPILED_HEADERS)
    target_precompile_headers(web_service PRIVATE precompiled_headers.h)
endif()

repo_name: litucks/torzu

license: mit


code:
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <future>
#include <nlohmann/json.hpp>
#include "common/detached_tasks.h"
#include "common/logging/log.h"
#include "web_service/announce_room_json.h"
#include "web_service/web_backend.h"

namespace AnnounceMultiplayerRoom {

static void to_json(nlohmann::json& json, const Member& member) {
    if (!member.username.empty()) {
        json["username"] = member.username;
    }
    json["nickname"] = member.nickname;
    if (!member.avatar_url.empty()) {
        json["avatarUrl"] = member.avatar_url;
    }
    json["gameName"] = member.game.name;
    json["gameId"] = member.game.id;
}

static void from_json(const nlohmann::json& json, Member& member) {
    member.nickname = json.at("nickname").get<std::string>();
    member.game.name = json.at("gameName").get<std::string>();
    member.game.id = json.at("gameId").get<u64>();
    try {
        member.username = json.at("username").get<std::string>();
        member.avatar_url = json.at("avatarUrl").get<std::string>();
    } catch (const nlohmann::detail::out_of_range&) {
        member.username = member.avatar_url = "";
        LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
    }
}

static void to_json(nlohmann::json& json, const Room& room) {
    json["port"] = room.information.port;
    json["name"] = room.information.name;
    if (!room.information.description.empty()) {
        json["description"] = room.information.description;
    }
    json["preferredGameName"] = room.information.preferred_game.name;
    json["preferredGameId"] = room.information.preferred_game.id;
    json["maxPlayers"] = room.information.member_slots;
    json["netVersion"] = room.net_version;
    json["hasPassword"] = room.has_password;
    if (room.members.size() > 0) {
        nlohmann::json member_json = room.members;
        json["players"] = member_json;
    }
}

static void from_json(const nlohmann::json& json, Room& room) {
    room.verify_uid = json.at("externalGuid").get<std::string>();
    room.ip = json.at("address").get<std::string>();
    room.information.name = json.at("name").get<std::string>();
    try {
        room.information.description = json.at("description").get<std::string>();
    } catch (const nlohmann::detail::out_of_range&) {
        room.information.description = "";
        LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.information.name);
    }
    room.information.host_username = json.at("owner").get<std::string>();
    room.information.port = json.at("port").get<u16>();
    room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
    room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
    room.information.member_slots = json.at("maxPlayers").get<u32>();
    room.net_version = json.at("netVersion").get<u32>();
    room.has_password = json.at("hasPassword").get<bool>();
    try {
        room.members = json.at("players").get<std::vector<Member>>();
    } catch (const nlohmann::detail::out_of_range& e) {
        LOG_DEBUG(Network, "Out of range {}", e.what());
    }
}

} // namespace AnnounceMultiplayerRoom

namespace WebService {

void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
                                  const u16 port, const u32 max_player, const u32 net_version,
                                  const bool has_password,
                                  const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
    room.information.name = name;
    room.information.description = description;
    room.information.port = port;
    room.information.member_slots = max_player;
    room.net_version = net_version;
    room.has_password = has_password;
    room.information.preferred_game = preferred_game;
}
void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
    room.members.push_back(member);
}

WebService::WebResult RoomJson::Update() {
    if (room_id.empty()) {
        LOG_ERROR(WebService, "Room must be registered to be updated");
        return WebService::WebResult{WebService::WebResult::Code::LibError,
                                     "Room is not registered", ""};
    }
    nlohmann::json json{{"players", room.members}};
    return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
}

WebService::WebResult RoomJson::Register() {
    nlohmann::json json = room;
    auto result = client.PostJson("/lobby", json.dump(), false);
    if (result.result_code != WebService::WebResult::Code::Success) {
        return result;
    }
    auto reply_json = nlohmann::json::parse(result.returned_data);
    room = reply_json.get<AnnounceMultiplayerRoom::Room>();
    room_id = reply_json.at("id").get<std::string>();
    return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_uid};
}

void RoomJson::ClearPlayers() {
    room.members.clear();
}

AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
    auto reply = client.GetJson("/lobby", true).returned_data;
    if (reply.empty()) {
        return {};
    }
    return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
}

void RoomJson::Delete() {
    if (room_id.empty()) {
        LOG_ERROR(WebService, "Room must be registered to be deleted");
        return;
    }
    Common::DetachedTasks::AddTask([host_{this->host}, username_{this->username},
                                    token_{this->token}, room_id_{this->room_id}]() {
        // create a new client here because the this->client might be destroyed.
        Client{host_, username_, token_}.DeleteJson(fmt::format("/lobby/{}", room_id_), "", false);
    });
}

} // namespace WebService


code:
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <functional>
#include <string>
#include "common/announce_multiplayer_room.h"
#include "web_service/web_backend.h"

namespace WebService {

/**
 * Implementation of AnnounceMultiplayerRoom::Backend that (de)serializes room information into/from
 * JSON, and submits/gets it to/from the yuzu web service
 */
class RoomJson : public AnnounceMultiplayerRoom::Backend {
public:
    RoomJson(const std::string& host_, const std::string& username_, const std::string& token_)
        : client(host_, username_, token_), host(host_), username(username_), token(token_) {}
    ~RoomJson() = default;
    void SetRoomInformation(const std::string& name, const std::string& description, const u16 port,
                            const u32 max_player, const u32 net_version, const bool has_password,
                            const AnnounceMultiplayerRoom::GameInfo& preferred_game) override;
    void AddPlayer(const AnnounceMultiplayerRoom::Member& member) override;
    WebResult Update() override;
    WebResult Register() override;
    void ClearPlayers() override;
    AnnounceMultiplayerRoom::RoomList GetRoomList() override;
    void Delete() override;

private:
    AnnounceMultiplayerRoom::Room room;
    Client client;
    std::string host;
    std::string username;
    std::string token;
    std::string room_id;
};

} // namespace WebService


code:
// SPDX-FileCopyrightText: 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/common_precompiled_headers.h"


code:
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <nlohmann/json.hpp>
#include "common/detached_tasks.h"
#include "web_service/telemetry_json.h"
#include "web_service/web_backend.h"
#include "web_service/web_result.h"

namespace WebService {

namespace Telemetry = Common::Telemetry;

struct TelemetryJson::Impl {
    Impl(std::string host_, std::string username_, std::string token_)
        : host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {}

    nlohmann::json& TopSection() {
        return sections[static_cast<u8>(Telemetry::FieldType::None)];
    }

    const nlohmann::json& TopSection() const {
        return sections[static_cast<u8>(Telemetry::FieldType::None)];
    }

    template <class T>
    void Serialize(Telemetry::FieldType type, const std::string& name, T value) {
        sections[static_cast<u8>(type)][name] = value;
    }

    void SerializeSection(Telemetry::FieldType type, const std::string& name) {
        TopSection()[name] = sections[static_cast<unsigned>(type)];
    }

    nlohmann::json output;
    std::array<nlohmann::json, 7> sections;
    std::string host;
    std::string username;
    std::string token;
};

TelemetryJson::TelemetryJson(std::string host, std::string username, std::string token)
    : impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}
TelemetryJson::~TelemetryJson() = default;

void TelemetryJson::Visit(const Telemetry::Field<bool>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<double>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<float>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<u8>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<u16>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<u32>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<u64>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<s8>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<s16>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<s32>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<s64>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
}

void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) {
    impl->Serialize(field.GetType(), field.GetName(), std::string(field.GetValue()));
}

void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) {
    impl->Serialize(field.GetType(), field.GetName(), field.GetValue().count());
}

void TelemetryJson::Complete() {
    impl->SerializeSection(Telemetry::FieldType::App, "App");
    impl->SerializeSection(Telemetry::FieldType::Session, "Session");
    impl->SerializeSection(Telemetry::FieldType::Performance, "Performance");
    impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
    impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");

    auto content = impl->TopSection().dump();
    // Send the telemetry async but don't handle the errors since they were written to the log
    Common::DetachedTasks::AddTask([host{impl->host}, content]() {
        Client{host, "", ""}.PostJson("/telemetry", content, true);
    });
}

bool TelemetryJson::SubmitTestcase() {
    impl->SerializeSection(Telemetry::FieldType::App, "App");
    impl->SerializeSection(Telemetry::FieldType::Session, "Session");
    impl->SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback");
    impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
    impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");

    auto content = impl->TopSection().dump();
    Client client(impl->host, impl->username, impl->token);
    auto value = client.PostJson("/gamedb/testcase", content, false);

    return value.result_code == WebResult::Code::Success;
}

} // namespace WebService


code:
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <chrono>
#include <string>
#include "common/telemetry.h"

namespace WebService {

/**
 * Implementation of VisitorInterface that serialized telemetry into JSON, and submits it to the
 * yuzu web service
 */
class TelemetryJson : public Common::Telemetry::VisitorInterface {
public:
    TelemetryJson(std::string host, std::string username, std::string token);
    ~TelemetryJson() override;

    void Visit(const Common::Telemetry::Field<bool>& field) override;
    void Visit(const Common::Telemetry::Field<double>& field) override;
    void Visit(const Common::Telemetry::Field<float>& field) override;
    void Visit(const Common::Telemetry::Field<u8>& field) override;
    void Visit(const Common::Telemetry::Field<u16>& field) override;
    void Visit(const Common::Telemetry::Field<u32>& field) override;
    void Visit(const Common::Telemetry::Field<u64>& field) override;
    void Visit(const Common::Telemetry::Field<s8>& field) override;
    void Visit(const Common::Telemetry::Field<s16>& field) override;
    void Visit(const Common::Telemetry::Field<s32>& field) override;
    void Visit(const Common::Telemetry::Field<s64>& field) override;
    void Visit(const Common::Telemetry::Field<std::string>& field) override;
    void Visit(const Common::Telemetry::Field<const char*>& field) override;
    void Visit(const Common::Telemetry::Field<std::chrono::microseconds>& field) override;

    void Complete() override;
    bool SubmitTestcase() override;

private:
    struct Impl;
    std::unique_ptr<Impl> impl;
};

} // namespace WebService


code:
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <nlohmann/json.hpp>
#include "web_service/verify_login.h"
#include "web_service/web_backend.h"
#include "web_service/web_result.h"

namespace WebService {

bool VerifyLogin(const std::string& host, const std::string& username, const std::string& token) {
    Client client(host, username, token);
    auto reply = client.GetJson("/profile", false).returned_data;
    if (reply.empty()) {
        return false;
    }
    nlohmann::json json = nlohmann::json::parse(reply);
    const auto iter = json.find("username");

    if (iter == json.end()) {
        return username.empty();
    }

    return *iter == username;
}

} // namespace WebService


code:
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <string>

namespace WebService {

/**
 * Checks if username and token is valid
 * @param host the web API URL
 * @param username yuzu username to use for authentication.
 * @param token yuzu token to use for authentication.
 * @returns a bool indicating whether the verification succeeded
 */
bool VerifyLogin(const std::string& host, const std::string& username, const std::string& token);

} // namespace WebService


code:
// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" // for deprecated OpenSSL functions
#endif
#include <jwt/jwt.hpp>
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

#include <system_error>
#include "common/logging/log.h"
#include "web_service/verify_user_jwt.h"
#include "web_service/web_backend.h"
#include "web_service/web_result.h"

namespace WebService {

static std::string public_key;
std::string GetPublicKey(const std::string& host) {
    if (public_key.empty()) {
        Client client(host, "", ""); // no need for credentials here
        public_key = client.GetPlain("/jwt/external/key.pem", true).returned_data;
        if (public_key.empty()) {
            LOG_ERROR(WebService, "Could not fetch external JWT public key, verification may fail");
        } else {
            LOG_INFO(WebService, "Fetched external JWT public key (size={})", public_key.size());
        }
    }
    return public_key;
}

VerifyUserJWT::VerifyUserJWT(const std::string& host) : pub_key(GetPublicKey(host)) {}

Network::VerifyUser::UserData VerifyUserJWT::LoadUserData(const std::string& verify_uid,
                                                          const std::string& token) {
    const std::string audience = fmt::format("external-{}", verify_uid);
    using namespace jwt::params;
    std::error_code error;

    // We use the Citra backend so the issuer is citra-core
    auto decoded =
        jwt::decode(token, algorithms({"rs256"}), error, secret(pub_key), issuer("citra-core"),
                    aud(audience), validate_iat(true), validate_jti(true));
    if (error) {
        LOG_INFO(WebService, "Verification failed: category={}, code={}, message={}",
                 error.category().name(), error.value(), error.message());
        return {};
    }
    Network::VerifyUser::UserData user_data{};
    if (decoded.payload().has_claim("username")) {
        user_data.username = decoded.payload().get_claim_value<std::string>("username");
    }
    if (decoded.payload().has_claim("displayName")) {
        user_data.display_name = decoded.payload().get_claim_value<std::string>("displayName");
    }
    if (decoded.payload().has_claim("avatarUrl")) {
        user_data.avatar_url = decoded.payload().get_claim_value<std::string>("avatarUrl");
    }
    if (decoded.payload().has_claim("roles")) {
        auto roles = decoded.payload().get_claim_value<std::vector<std::string>>("roles");
        user_data.moderator = std::find(roles.begin(), roles.end(), "moderator") != roles.end();
    }
    return user_data;
}

} // namespace WebService


code:
// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <fmt/format.h>
#include "network/verify_user.h"
#include "web_service/web_backend.h"

namespace WebService {

std::string GetPublicKey(const std::string& host);

class VerifyUserJWT final : public Network::VerifyUser::Backend {
public:
    VerifyUserJWT(const std::string& host);
    ~VerifyUserJWT() = default;

    Network::VerifyUser::UserData LoadUserData(const std::string& verify_uid,
                                               const std::string& token) override;

private:
    std::string pub_key;
};

} // namespace WebService


code:
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <array>
#include <mutex>
#include <string>

#include <fmt/format.h>

#ifdef __GNUC__
#pragma GCC diagnostic push
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#endif
#include <httplib.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#include "common/logging/log.h"
#include "web_service/web_backend.h"
#include "web_service/web_result.h"

namespace WebService {

constexpr std::array<const char, 1> API_VERSION{'1'};

constexpr std::size_t TIMEOUT_SECONDS = 30;

struct Client::Impl {
    Impl(std::string host_, std::string username_, std::string token_)
        : host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {
        std::scoped_lock lock{jwt_cache.mutex};
        if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
            jwt = jwt_cache.jwt;
        }

        // Normalize host expression
        if (!this->host.empty() && this->host.back() == '/') {
            static_cast<void>(this->host.pop_back());
        }
    }

    /// A generic function handles POST, GET and DELETE request together
    WebResult GenericRequest(const std::string& method, const std::string& path,
                             const std::string& data, bool allow_anonymous,
                             const std::string& accept) {
        if (jwt.empty()) {
            UpdateJWT();
        }

        if (jwt.empty() && !allow_anonymous) {
            LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
            return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""};
        }

        auto result = GenericRequest(method, path, data, accept, jwt);
        if (result.result_string == "401") {
            // Try again with new JWT
            UpdateJWT();
            result = GenericRequest(method, path, data, accept, jwt);
        }

        return result;
    }

    /**
     * A generic function with explicit authentication method specified
     * JWT is used if the jwt parameter is not empty
     * username + token is used if jwt is empty but username and token are
     * not empty anonymous if all of jwt, username and token are empty
     */
    WebResult GenericRequest(const std::string& method, const std::string& path,
                             const std::string& data, const std::string& accept,
                             const std::string& jwt_ = "", const std::string& username_ = "",
                             const std::string& token_ = "") {
        if (cli == nullptr) {
            cli = std::make_unique<httplib::Client>(host.c_str());
            cli->set_connection_timeout(TIMEOUT_SECONDS);
            cli->set_read_timeout(TIMEOUT_SECONDS);
            cli->set_write_timeout(TIMEOUT_SECONDS);
        }
        if (!cli->is_valid()) {
            LOG_ERROR(WebService, "Invalid URL {}", host + path);
            return WebResult{WebResult::Code::InvalidURL, "Invalid URL", ""};
        }

        httplib::Headers params;
        if (!jwt_.empty()) {
            params = {
                {std::string("Authorization"), fmt::format("Bearer {}", jwt_)},
            };
        } else if (!username_.empty()) {
            params = {
                {std::string("x-username"), username_},
                {std::string("x-token"), token_},
            };
        }

        params.emplace(std::string("api-version"),
                       std::string(API_VERSION.begin(), API_VERSION.end()));
        if (method != "GET") {
            params.emplace(std::string("Content-Type"), std::string("application/json"));
        }

        httplib::Request request;
        request.method = method;
        request.path = path;
        request.headers = params;
        request.body = data;

        httplib::Result result = cli->send(request);

        if (!result) {
            LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
            return WebResult{WebResult::Code::LibError, "Null response", ""};
        }

        httplib::Response response = result.value();

        if (response.status >= 400) {
            LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
                      response.status);
            return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""};
        }

        auto content_type = response.headers.find("content-type");

        if (content_type == response.headers.end()) {
            LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
            return WebResult{WebResult::Code::WrongContent, "", ""};
        }

        if (content_type->second.find(accept) == std::string::npos) {
            LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
                      content_type->second);
            return WebResult{WebResult::Code::WrongContent, "Wrong content", ""};
        }
        return WebResult{WebResult::Code::Success, "", response.body};
    }

    // Retrieve a new JWT from given username and token
    void UpdateJWT() {
        if (username.empty() || token.empty()) {
            return;
        }

        auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
        if (result.result_code != WebResult::Code::Success) {
            LOG_ERROR(WebService, "UpdateJWT failed");
        } else {
            std::scoped_lock lock{jwt_cache.mutex};
            jwt_cache.username = username;
            jwt_cache.token = token;
            jwt_cache.jwt = jwt = result.returned_data;
        }
    }

    std::string host;
    std::string username;
    std::string token;
    std::string jwt;
    std::unique_ptr<httplib::Client> cli;

    struct JWTCache {
        std::mutex mutex;
        std::string username;
        std::string token;
        std::string jwt;
    };
    static inline JWTCache jwt_cache;
};

Client::Client(std::string host, std::string username, std::string token)
    : impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}

Client::~Client() = default;

WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) {
    return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
}

WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
    return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
}

WebResult Client::DeleteJson(const std::string& path, const std::string& data,
                             bool allow_anonymous) {
    return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
}

WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
    return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
}

WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
    return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
}

WebResult Client::GetExternalJWT(const std::string& audience) {
    return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
                                "text/html");
}

} // namespace WebService


code:
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <memory>
#include <string>

namespace WebService {

struct WebResult;

class Client {
public:
    Client(std::string host, std::string username, std::string token);
    ~Client();

    /**
     * Posts JSON to the specified path.
     * @param path the URL segment after the host address.
     * @param data String of JSON data to use for the body of the POST request.
     * @param allow_anonymous If true, allow anonymous unauthenticated requests.
     * @return the result of the request.
     */
    WebResult PostJson(const std::string& path, const std::string& data, bool allow_anonymous);

    /**
     * Gets JSON from the specified path.
     * @param path the URL segment after the host address.
     * @param allow_anonymous If true, allow anonymous unauthenticated requests.
     * @return the result of the request.
     */
    WebResult GetJson(const std::string& path, bool allow_anonymous);

    /**
     * Deletes JSON to the specified path.
     * @param path the URL segment after the host address.
     * @param data String of JSON data to use for the body of the DELETE request.
     * @param allow_anonymous If true, allow anonymous unauthenticated requests.
     * @return the result of the request.
     */
    WebResult DeleteJson(const std::string& path, const std::string& data, bool allow_anonymous);

    /**
     * Gets a plain string from the specified path.
     * @param path the URL segment after the host address.
     * @param allow_anonymous If true, allow anonymous unauthenticated requests.
     * @return the result of the request.
     */
    WebResult GetPlain(const std::string& path, bool allow_anonymous);

    /**
     * Gets an PNG image from the specified path.
     * @param path the URL segment after the host address.
     * @param allow_anonymous If true, allow anonymous unauthenticated requests.
     * @return the result of the request.
     */
    WebResult GetImage(const std::string& path, bool allow_anonymous);

    /**
     * Requests an external JWT for the specific audience provided.
     * @param audience the audience of the JWT requested.
     * @return the result of the request.
     */
    WebResult GetExternalJWT(const std::string& audience);

private:
    struct Impl;
    std::unique_ptr<Impl> impl;
};

} // namespace WebService


code:
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <string>
#include "common/common_types.h"

namespace WebService {
struct WebResult {
    enum class Code : u32 {
        Success,
        InvalidURL,
        CredentialsMissing,
        LibError,
        HttpError,
        WrongContent,
        NoWebservice,
    };
    Code result_code;
    std::string result_string;
    std::string returned_data;
};
} // namespace WebService