From bc0f05735cf92171f3471f46b65e3b0e1735274e Mon Sep 17 00:00:00 2001 From: ownedbywuigi Date: Wed, 9 Jul 2025 23:01:39 +0100 Subject: [PATCH] More shit --- common/hex_util.h | 74 ++++++++++++++++++++++++++++++ common/scope_exit.h | 82 ++++++++++++++++++++++++++++++++++ core/fs/partition_filesystem.h | 8 ++-- core/fs/patch_manager.h | 6 +-- core/fs/program_metadata.cpp | 10 ++--- core/fs/program_metadata.h | 10 ++--- core/fs/registered_cache.cpp | 26 +++++------ core/memory/dmnt_cheat_types.h | 37 +++++++++++++++ 8 files changed, 223 insertions(+), 30 deletions(-) create mode 100755 common/hex_util.h create mode 100755 common/scope_exit.h create mode 100755 core/memory/dmnt_cheat_types.h diff --git a/common/hex_util.h b/common/hex_util.h new file mode 100755 index 0000000..5b73fd5 --- /dev/null +++ b/common/hex_util.h @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project +// SPDX-FileCopyrightText: 2014 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include +#include +#include +#include "assert.h" +#include "common_types.h" + +namespace Common { + +[[nodiscard]] constexpr u8 ToHexNibble(char c) { + if (c >= 65 && c <= 70) { + return static_cast(c - 55); + } + + if (c >= 97 && c <= 102) { + return static_cast(c - 87); + } + + return static_cast(c - 48); +} + +[[nodiscard]] std::vector HexStringToVector(std::string_view str, bool little_endian); + +template +[[nodiscard]] constexpr std::array HexStringToArray(std::string_view str) { + ASSERT_MSG(Size * 2 <= str.size(), "Invalid string size"); + + std::array out{}; + if constexpr (le) { + for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) { + out[i / 2] = static_cast((ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1])); + } + } else { + for (std::size_t i = 0; i < 2 * Size; i += 2) { + out[i / 2] = static_cast((ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1])); + } + } + return out; +} + +template +[[nodiscard]] std::string HexToString(const ContiguousContainer& data, bool upper = true) { + static_assert(std::is_same_v, + "Underlying type within the contiguous container must be u8."); + + constexpr std::size_t pad_width = 2; + + std::string out; + out.reserve(std::size(data) * pad_width); + + const auto format_str = fmt::runtime(upper ? "{:02X}" : "{:02x}"); + for (const u8 c : data) { + out += fmt::format(format_str, c); + } + + return out; +} + +[[nodiscard]] constexpr std::array AsArray(const char (&data)[33]) { + return HexStringToArray<16>(data); +} + +[[nodiscard]] constexpr std::array AsArray(const char (&data)[65]) { + return HexStringToArray<32>(data); +} + +} // namespace Common diff --git a/common/scope_exit.h b/common/scope_exit.h new file mode 100755 index 0000000..b8f1cf2 --- /dev/null +++ b/common/scope_exit.h @@ -0,0 +1,82 @@ +// SPDX-FileCopyrightText: 2014 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include "common_funcs.h" + +namespace detail { +template +class ScopeGuard { + YUZU_NON_COPYABLE(ScopeGuard); + +private: + F f; + bool active; + +public: + constexpr ScopeGuard(F f_) : f(std::move(f_)), active(true) {} + constexpr ~ScopeGuard() { + if (active) { + f(); + } + } + constexpr void Cancel() { + active = false; + } + + constexpr ScopeGuard(ScopeGuard&& rhs) : f(std::move(rhs.f)), active(rhs.active) { + rhs.Cancel(); + } + + ScopeGuard& operator=(ScopeGuard&& rhs) = delete; +}; + +template +constexpr ScopeGuard MakeScopeGuard(F f) { + return ScopeGuard(std::move(f)); +} + +enum class ScopeGuardOnExit {}; + +template +constexpr ScopeGuard operator+(ScopeGuardOnExit, F&& f) { + return ScopeGuard(std::forward(f)); +} + +} // namespace detail + +#define CONCATENATE_IMPL(s1, s2) s1##s2 +#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2) + +#ifdef __COUNTER__ +#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __COUNTER__) +#else +#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __LINE__) +#endif + +/** + * This macro is similar to SCOPE_EXIT, except the object is caller managed. This is intended to be + * used when the caller might want to cancel the ScopeExit. + */ +#define SCOPE_GUARD detail::ScopeGuardOnExit() + [&]() + +/** + * This macro allows you to conveniently specify a block of code that will run on scope exit. Handy + * for doing ad-hoc clean-up tasks in a function with multiple returns. + * + * Example usage: + * \code + * const int saved_val = g_foo; + * g_foo = 55; + * SCOPE_EXIT{ g_foo = saved_val; }; + * + * if (Bar()) { + * return 0; + * } else { + * return 20; + * } + * \endcode + */ +#define SCOPE_EXIT auto ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE_) = SCOPE_GUARD diff --git a/core/fs/partition_filesystem.h b/core/fs/partition_filesystem.h index 777b9ea..e1e51a3 100644 --- a/core/fs/partition_filesystem.h +++ b/core/fs/partition_filesystem.h @@ -6,10 +6,10 @@ #include #include #include -#include "common/common_funcs.h" -#include "common/common_types.h" -#include "common/swap.h" -#include "core/file_sys/vfs/vfs.h" +#include "../common/common_funcs.h" +#include "../common/common_types.h" +#include "../common/swap.h" +#include "../core/fs/vfs/vfs.h" namespace Loader { enum class ResultStatus : u16; diff --git a/core/fs/patch_manager.h b/core/fs/patch_manager.h index 552c0fb..0336273 100644 --- a/core/fs/patch_manager.h +++ b/core/fs/patch_manager.h @@ -8,9 +8,9 @@ #include #include #include "common/common_types.h" -#include "core/file_sys/nca_metadata.h" -#include "core/file_sys/vfs/vfs_types.h" -#include "core/memory/dmnt_cheat_types.h" +#include "../core/fs/nca_metadata.h" +#include "../core/fs/vfs/vfs_types.h" +#include "../core/memory/dmnt_cheat_types.h" namespace Core { class System; diff --git a/core/fs/program_metadata.cpp b/core/fs/program_metadata.cpp index 289969c..ad738a1 100644 --- a/core/fs/program_metadata.cpp +++ b/core/fs/program_metadata.cpp @@ -4,11 +4,11 @@ #include #include -#include "common/logging/log.h" -#include "common/scope_exit.h" -#include "core/file_sys/program_metadata.h" -#include "core/file_sys/vfs/vfs.h" -#include "core/loader/loader.h" +#include "../common/logging/log.h" +#include "../common/scope_exit.h" +#include "../core/file_sys/program_metadata.h" +#include "../core/file_sys/vfs/vfs.h" +#include "../core/loader/loader.h" namespace FileSys { diff --git a/core/fs/program_metadata.h b/core/fs/program_metadata.h index 115e6d6..333e49b 100644 --- a/core/fs/program_metadata.h +++ b/core/fs/program_metadata.h @@ -6,11 +6,11 @@ #include #include -#include "common/bit_field.h" -#include "common/common_funcs.h" -#include "common/common_types.h" -#include "common/swap.h" -#include "core/file_sys/vfs/vfs_types.h" +#include "../common/bit_field.h" +#include "../common/common_funcs.h" +#include "../common/common_types.h" +#include "../common/swap.h" +#include "../core/fs/vfs/vfs_types.h" namespace Loader { enum class ResultStatus : u16; diff --git a/core/fs/registered_cache.cpp b/core/fs/registered_cache.cpp index 85d3054..fe556c8 100644 --- a/core/fs/registered_cache.cpp +++ b/core/fs/registered_cache.cpp @@ -5,19 +5,19 @@ #include #include #include -#include "common/assert.h" -#include "common/fs/path_util.h" -#include "common/hex_util.h" -#include "common/logging/log.h" -#include "common/scope_exit.h" -#include "core/crypto/key_manager.h" -#include "core/file_sys/card_image.h" -#include "core/file_sys/common_funcs.h" -#include "core/file_sys/content_archive.h" -#include "core/file_sys/nca_metadata.h" -#include "core/file_sys/registered_cache.h" -#include "core/file_sys/submission_package.h" -#include "core/file_sys/vfs/vfs_concat.h" +#include "../common/assert.h" +#include "../common/fs/path_util.h" +#include "../common/hex_util.h" +#include "../common/logging/log.h" +#include "../common/scope_exit.h" +#include "../core/crypto/key_manager.h" +#include "card_image.h" +#include "common_funcs.h" +#include "content_archive.h" +#include "nca_metadata.h" +#include "registered_cache.h" +#include "submission_package.h" +#include "../core/fs/vfs/vfs_concat.h" #include "core/loader/loader.h" namespace FileSys { diff --git a/core/memory/dmnt_cheat_types.h b/core/memory/dmnt_cheat_types.h new file mode 100755 index 0000000..6bd3dce --- /dev/null +++ b/core/memory/dmnt_cheat_types.h @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "../common/common_types.h" + +namespace Core::Memory { + +struct MemoryRegionExtents { + u64 base{}; + u64 size{}; +}; + +struct CheatProcessMetadata { + u64 process_id{}; + u64 title_id{}; + MemoryRegionExtents main_nso_extents{}; + MemoryRegionExtents heap_extents{}; + MemoryRegionExtents alias_extents{}; + MemoryRegionExtents aslr_extents{}; + std::array main_nso_build_id{}; +}; + +struct CheatDefinition { + std::array readable_name{}; + u32 num_opcodes{}; + std::array opcodes{}; +}; + +struct CheatEntry { + bool enabled{}; + u32 cheat_id{}; + CheatDefinition definition{}; +}; + +} // namespace Core::Memory