VERY experimental Nintendo Switch 2 emulator
C++
[core/file_sys] implement IApplicationFunctions::GetPseudoDeviceId; common PatchManager::GetNACP for base|updates (#4159)
lizzie8fe1e6efa2fe
diff --git a/src/common/uuid.cpp b/src/common/uuid.cppindex d4a5733c26..51fe0abaa5 100644--- a/src/common/uuid.cpp+++ b/src/common/uuid.cpp@@ -208,4 +208,12 @@ UUID UUID::MakeRandomRFC4122V4() {return uuid;}+UUID UUID::MakeRFC4122V5(std::span<u8, 20> sha1) {+ UUID uuid{};+ std::memcpy(&uuid.uuid, sha1.data(), sizeof(UUID));+ uuid.uuid[8] = 0x80 | (uuid.uuid[8] & 0x3F);+ uuid.uuid[6] = 0x50 | (uuid.uuid[6] & 0xF);+ return uuid;+}+} // namespace Commondiff --git a/src/common/uuid.h b/src/common/uuid.hindex 60c5d75f12..c3cbd7b479 100644--- a/src/common/uuid.h+++ b/src/common/uuid.h@@ -1,3 +1,6 @@+// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project+// SPDX-License-Identifier: GPL-3.0-or-later+// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project// SPDX-License-Identifier: GPL-2.0-or-later@@ -5,6 +8,7 @@#include <array>#include <functional>+#include <span>#include <string>#include "common/common_types.h"@@ -86,28 +90,20 @@ struct UUID {};}- /**- * Creates a random UUID.- *- * @returns A random UUID.- */- static UUID MakeRandom();+ /// @brief Creates a random UUID.+ /// @returns A random UUID.+ [[nodiscard]] static UUID MakeRandom();- /**- * Creates a random UUID with a seed.- *- * @param seed A seed to initialize the Mersenne-Twister RNG- *- * @returns A random UUID.- */- static UUID MakeRandomWithSeed(u32 seed);+ /// @brief Creates a random UUID with a seed.+ /// @param seed A seed to initialize the Mersenne-Twister RNG+ /// @returns A random UUID.+ [[nodiscard]] static UUID MakeRandomWithSeed(u32 seed);- /**- * Creates a random UUID. The generated UUID is RFC 4122 Version 4 compliant.- *- * @returns A random UUID that is RFC 4122 Version 4 compliant.- */- static UUID MakeRandomRFC4122V4();+ /// @brief Creates a random UUID. The generated UUID is RFC 4122 Version 4 compliant.+ /// @returns A random UUID that is RFC 4122 Version 4 compliant.+ [[nodiscard]] static UUID MakeRandomRFC4122V4();++ [[nodiscard]] static UUID MakeRFC4122V5(std::span<u8, 20> sha1);friend constexpr bool operator==(const UUID& lhs, const UUID& rhs) = default;};diff --git a/src/core/file_sys/control_metadata.h b/src/core/file_sys/control_metadata.hindex 7b90ba235d..670e8f7389 100644--- a/src/core/file_sys/control_metadata.h+++ b/src/core/file_sys/control_metadata.h@@ -274,23 +274,22 @@ public:explicit NACP(VirtualFile file);~NACP();- const LanguageEntry& GetLanguageEntry() const;- std::string GetApplicationName() const;- std::string GetDeveloperName() const;- u64 GetTitleId() const;- u64 GetDLCBaseTitleId() const;- std::string GetVersionString() const;- u64 GetDefaultNormalSaveSize() const;- u64 GetDefaultJournalSaveSize() const;- u32 GetSupportedLanguages() const;- std::vector<std::string> GetApplicationNames() const;- std::vector<u8> GetRawBytes() const;- bool GetUserAccountSwitchLock() const;- u64 GetDeviceSaveDataSize() const;- u32 GetParentalControlFlag() const;- const std::array<u8, 0x20>& GetRatingAge() const;--private:+ [[nodiscard]] const LanguageEntry& GetLanguageEntry() const;+ [[nodiscard]] std::string GetApplicationName() const;+ [[nodiscard]] std::string GetDeveloperName() const;+ [[nodiscard]] u64 GetTitleId() const;+ [[nodiscard]] u64 GetDLCBaseTitleId() const;+ [[nodiscard]] std::string GetVersionString() const;+ [[nodiscard]] u64 GetDefaultNormalSaveSize() const;+ [[nodiscard]] u64 GetDefaultJournalSaveSize() const;+ [[nodiscard]] u32 GetSupportedLanguages() const;+ [[nodiscard]] std::vector<std::string> GetApplicationNames() const;+ [[nodiscard]] std::vector<u8> GetRawBytes() const;+ [[nodiscard]] bool GetUserAccountSwitchLock() const;+ [[nodiscard]] u64 GetDeviceSaveDataSize() const;+ [[nodiscard]] u32 GetParentalControlFlag() const;+ [[nodiscard]] const std::array<u8, 0x20>& GetRatingAge() const;+RawNACP raw{};std::vector<LanguageEntry> language_entries;};diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cppindex 80372ec94e..6ae4679054 100644--- a/src/core/file_sys/patch_manager.cpp+++ b/src/core/file_sys/patch_manager.cpp@@ -1156,4 +1156,14 @@ PatchManager::Metadata PatchManager::ParseControlNCA(const NCA& nca) const {return {std::move(nacp), icon_file};}++[[nodiscard]] PatchManager::Metadata PatchManager::GetMetadataFromBaseOrUpdate(Core::System& system, u64 application_id) noexcept {+ const FileSys::PatchManager pm{application_id, system.GetFileSystemController(), system.GetContentProvider()};+ auto metadata = pm.GetControlMetadata();+ if (metadata.first != nullptr)+ return metadata;+ const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(application_id), system.GetFileSystemController(), system.GetContentProvider()};+ return pm_update.GetControlMetadata();+}+} // namespace FileSysdiff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.hindex 755d924fd4..5d71dbcf20 100644--- a/src/core/file_sys/patch_manager.h+++ b/src/core/file_sys/patch_manager.h@@ -105,6 +105,9 @@ public:// Version of GetControlMetadata that takes an arbitrary NCA[[nodiscard]] Metadata ParseControlNCA(const NCA& nca) const;+ /// @brief Gets NACP metadata (accounting for any patches or updates)+ [[nodiscard]] static PatchManager::Metadata GetMetadataFromBaseOrUpdate(Core::System& system, u64 application_id) noexcept;+private:[[nodiscard]] std::vector<VirtualFile> CollectPatches(const std::vector<VirtualDir>& patch_dirs,const std::string& build_id) const;diff --git a/src/core/hle/service/am/service/application_functions.cpp b/src/core/hle/service/am/service/application_functions.cppindex 787c88f52d..25a666233f 100644--- a/src/core/hle/service/am/service/application_functions.cpp+++ b/src/core/hle/service/am/service/application_functions.cpp@@ -4,6 +4,8 @@// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project// SPDX-License-Identifier: GPL-2.0-or-later+#include <openssl/evp.h>+#include "common/settings.h"#include "common/uuid.h"#include "core/file_sys/control_metadata.h"@@ -154,20 +156,7 @@ Result IApplicationFunctions::GetDesiredLanguage(Out<u64> out_language_code) {// Default to 0 (all languages supported)u32 supported_languages = 0;- const auto res = [this] {- const FileSys::PatchManager pm{m_applet->program_id, system.GetFileSystemController(),- system.GetContentProvider()};- auto metadata = pm.GetControlMetadata();- if (metadata.first != nullptr) {- return metadata;- }-- const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(m_applet->program_id),- system.GetFileSystemController(),- system.GetContentProvider()};- return pm_update.GetControlMetadata();- }();-+ const auto res = FileSys::PatchManager::GetMetadataFromBaseOrUpdate(system, m_applet->program_id);if (res.first != nullptr) {supported_languages = res.first->GetSupportedLanguages();}@@ -205,20 +194,7 @@ Result IApplicationFunctions::SetTerminateResult(Result terminate_result) {Result IApplicationFunctions::GetDisplayVersion(Out<DisplayVersion> out_display_version) {LOG_DEBUG(Service_AM, "called");- const auto res = [this] {- const FileSys::PatchManager pm{m_applet->program_id, system.GetFileSystemController(),- system.GetContentProvider()};- auto metadata = pm.GetControlMetadata();- if (metadata.first != nullptr) {- return metadata;- }-- const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(m_applet->program_id),- system.GetFileSystemController(),- system.GetContentProvider()};- return pm_update.GetControlMetadata();- }();-+ const auto res = FileSys::PatchManager::GetMetadataFromBaseOrUpdate(system, m_applet->program_id);if (res.first != nullptr) {const auto& version = res.first->GetVersionString();std::memcpy(out_display_version->string.data(), version.data(),@@ -347,8 +323,21 @@ Result IApplicationFunctions::NotifyRunning(Out<bool> out_became_running) {}Result IApplicationFunctions::GetPseudoDeviceId(Out<Common::UUID> out_pseudo_device_id) {- LOG_WARNING(Service_AM, "(STUBBED) called");- *out_pseudo_device_id = {};+ LOG_WARNING(Service_AM, "(stubbed)");++ // This should be hashed with the device specific hash+ // for now this will do+ const auto res = FileSys::PatchManager::GetMetadataFromBaseOrUpdate(system, m_applet->program_id);+ u8 hash[EVP_MAX_MD_SIZE];+ unsigned int hash_len = 0;+ auto const seed = res.first->raw.seed_for_pseudo_device_id;+ EVP_MD_CTX *ctx = EVP_MD_CTX_new();+ auto const algorithm = EVP_sha1();+ EVP_DigestInit_ex(ctx, algorithm, nullptr);+ EVP_DigestUpdate(ctx, &seed, sizeof(seed));+ EVP_DigestFinal_ex(ctx, hash, &hash_len);+ EVP_MD_CTX_free(ctx);+ *out_pseudo_device_id = Common::UUID::MakeRFC4122V5(std::span<u8, 20>{hash, std::size(hash)});R_SUCCEED();}diff --git a/src/core/hle/service/am/service/library_applet_self_accessor.cpp b/src/core/hle/service/am/service/library_applet_self_accessor.cppindex b16bf87979..907be49204 100644--- a/src/core/hle/service/am/service/library_applet_self_accessor.cpp+++ b/src/core/hle/service/am/service/library_applet_self_accessor.cpp@@ -252,19 +252,7 @@ Result ILibraryAppletSelfAccessor::GetMainAppletApplicationDesiredLanguage(// Default to 0 (all languages supported)u32 supported_languages = 0;- const auto res = [this, identity] {- const FileSys::PatchManager pm{identity.application_id, system.GetFileSystemController(),- system.GetContentProvider()};- auto metadata = pm.GetControlMetadata();- if (metadata.first != nullptr) {- return metadata;- }-- const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(identity.application_id),- system.GetFileSystemController(),- system.GetContentProvider()};- return pm_update.GetControlMetadata();- }();+ const auto res = FileSys::PatchManager::GetMetadataFromBaseOrUpdate(system, identity.application_id);if (res.first != nullptr) {supported_languages = res.first->GetSupportedLanguages();