From 363b07fc0c4f02dc42979efc03b996285ea54554 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 2 Nov 2023 08:53:46 +0100 Subject: [PATCH] impr: Switch to custom std::expected implementation --- lib/external/libwolv | 2 +- lib/libimhex/include/hex/helpers/patches.hpp | 11 +++--- lib/libimhex/source/helpers/patches.cpp | 36 ++++++++++---------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/external/libwolv b/lib/external/libwolv index d902b5597..7e306b0e0 160000 --- a/lib/external/libwolv +++ b/lib/external/libwolv @@ -1 +1 @@ -Subproject commit d902b5597735ed91e9626598304853774adee5ef +Subproject commit 7e306b0e0ab13989949b1d240df78b3c989e7fdf diff --git a/lib/libimhex/include/hex/helpers/patches.hpp b/lib/libimhex/include/hex/helpers/patches.hpp index a42f75b76..e8e06a8f9 100644 --- a/lib/libimhex/include/hex/helpers/patches.hpp +++ b/lib/libimhex/include/hex/helpers/patches.hpp @@ -4,7 +4,8 @@ #include #include -#include + +#include namespace hex { @@ -18,9 +19,9 @@ namespace hex { MissingEOF }; - std::expected, IPSError> generateIPSPatch(const Patches &patches); - std::expected, IPSError> generateIPS32Patch(const Patches &patches); + wolv::util::Expected, IPSError> generateIPSPatch(const Patches &patches); + wolv::util::Expected, IPSError> generateIPS32Patch(const Patches &patches); - std::expected loadIPSPatch(const std::vector &ipsPatch); - std::expected loadIPS32Patch(const std::vector &ipsPatch); + wolv::util::Expected loadIPSPatch(const std::vector &ipsPatch); + wolv::util::Expected loadIPS32Patch(const std::vector &ipsPatch); } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/patches.cpp b/lib/libimhex/source/helpers/patches.cpp index af89169bf..dc3d35faa 100644 --- a/lib/libimhex/source/helpers/patches.cpp +++ b/lib/libimhex/source/helpers/patches.cpp @@ -18,7 +18,7 @@ namespace hex { std::memcpy((&buffer.back() - sizeof(T)) + 1, &bytes, sizeof(T)); } - std::expected, IPSError> generateIPSPatch(const Patches &patches) { + wolv::util::Expected, IPSError> generateIPSPatch(const Patches &patches) { std::vector result; pushStringBack(result, "PATCH"); @@ -43,9 +43,9 @@ namespace hex { bytes.push_back(values[i]); if (bytes.size() > 0xFFFF) - return std::unexpected(IPSError::PatchTooLarge); + return wolv::util::Unexpected(IPSError::PatchTooLarge); if (startAddress > 0xFFFF'FFFF) - return std::unexpected(IPSError::AddressOutOfRange); + return wolv::util::Unexpected(IPSError::AddressOutOfRange); u32 address = startAddress.value(); auto addressBytes = reinterpret_cast(&address); @@ -68,7 +68,7 @@ namespace hex { return result; } - std::expected, IPSError> generateIPS32Patch(const Patches &patches) { + wolv::util::Expected, IPSError> generateIPS32Patch(const Patches &patches) { std::vector result; pushStringBack(result, "IPS32"); @@ -93,9 +93,9 @@ namespace hex { bytes.push_back(values[i]); if (bytes.size() > 0xFFFF) - return std::unexpected(IPSError::PatchTooLarge); + return wolv::util::Unexpected(IPSError::PatchTooLarge); if (startAddress > 0xFFFF'FFFF) - return std::unexpected(IPSError::AddressOutOfRange); + return wolv::util::Unexpected(IPSError::AddressOutOfRange); u32 address = startAddress.value(); auto addressBytes = reinterpret_cast(&address); @@ -119,12 +119,12 @@ namespace hex { return result; } - std::expected loadIPSPatch(const std::vector &ipsPatch) { + wolv::util::Expected loadIPSPatch(const std::vector &ipsPatch) { if (ipsPatch.size() < (5 + 3)) - return std::unexpected(IPSError::InvalidPatchHeader); + return wolv::util::Unexpected(IPSError::InvalidPatchHeader); if (std::memcmp(ipsPatch.data(), "PATCH", 5) != 0) - return std::unexpected(IPSError::InvalidPatchHeader); + return wolv::util::Unexpected(IPSError::InvalidPatchHeader); Patches result; bool foundEOF = false; @@ -139,7 +139,7 @@ namespace hex { // Handle normal record if (size > 0x0000) { if (ipsOffset + size > ipsPatch.size() - 3) - return std::unexpected(IPSError::InvalidPatchFormat); + return wolv::util::Unexpected(IPSError::InvalidPatchFormat); for (u16 i = 0; i < size; i++) result[offset + i] = ipsPatch[ipsOffset + i]; @@ -148,7 +148,7 @@ namespace hex { // Handle RLE record else { if (ipsOffset + 3 > ipsPatch.size() - 3) - return std::unexpected(IPSError::InvalidPatchFormat); + return wolv::util::Unexpected(IPSError::InvalidPatchFormat); u16 rleSize = ipsPatch[ipsOffset + 0] | (ipsPatch[ipsOffset + 1] << 8); @@ -167,15 +167,15 @@ namespace hex { if (foundEOF) return result; else - return std::unexpected(IPSError::MissingEOF); + return wolv::util::Unexpected(IPSError::MissingEOF); } - std::expected loadIPS32Patch(const std::vector &ipsPatch) { + wolv::util::Expected loadIPS32Patch(const std::vector &ipsPatch) { if (ipsPatch.size() < (5 + 4)) - return std::unexpected(IPSError::InvalidPatchHeader); + return wolv::util::Unexpected(IPSError::InvalidPatchHeader); if (std::memcmp(ipsPatch.data(), "IPS32", 5) != 0) - return std::unexpected(IPSError::InvalidPatchHeader); + return wolv::util::Unexpected(IPSError::InvalidPatchHeader); Patches result; bool foundEEOF = false; @@ -190,7 +190,7 @@ namespace hex { // Handle normal record if (size > 0x0000) { if (ipsOffset + size > ipsPatch.size() - 3) - return std::unexpected(IPSError::InvalidPatchFormat); + return wolv::util::Unexpected(IPSError::InvalidPatchFormat); for (u16 i = 0; i < size; i++) result[offset + i] = ipsPatch[ipsOffset + i]; @@ -199,7 +199,7 @@ namespace hex { // Handle RLE record else { if (ipsOffset + 3 > ipsPatch.size() - 3) - return std::unexpected(IPSError::InvalidPatchFormat); + return wolv::util::Unexpected(IPSError::InvalidPatchFormat); u16 rleSize = ipsPatch[ipsOffset + 0] | (ipsPatch[ipsOffset + 1] << 8); @@ -218,7 +218,7 @@ namespace hex { if (foundEEOF) return result; else - return std::unexpected(IPSError::MissingEOF); + return wolv::util::Unexpected(IPSError::MissingEOF); } } \ No newline at end of file