From 0696a7abeb43dcab0d7922dd94665b15cded2841 Mon Sep 17 00:00:00 2001 From: Sc2ad Date: Sat, 14 Oct 2023 23:59:20 -0700 Subject: [PATCH] Fix typedefs-wrapper compile for ndk --- src/utils/typedefs-wrapper.cpp | 607 ++++++++++++++++----------------- 1 file changed, 300 insertions(+), 307 deletions(-) diff --git a/src/utils/typedefs-wrapper.cpp b/src/utils/typedefs-wrapper.cpp index 8cb21451..bad86a25 100644 --- a/src/utils/typedefs-wrapper.cpp +++ b/src/utils/typedefs-wrapper.cpp @@ -1,352 +1,333 @@ -#include "../../shared/utils/typedefs-wrappers.hpp" -#include "../../shared/utils/typedefs-string.hpp" +#include +#include #include "../../shared/utils/il2cpp-functions.hpp" +#include "../../shared/utils/typedefs-string.hpp" +#include "../../shared/utils/typedefs-wrappers.hpp" #include "../../shared/utils/typedefs.h" -#include -#include + std::unordered_map Counter::addrRefCount; std::shared_mutex Counter::mutex; namespace il2cpp_utils { namespace detail { - template - struct deletable_facet : Facet { - template - deletable_facet(Args&& ...args) : Facet(std::forward(args)...) {} - ~deletable_facet() {} - }; - - // Note that char is actually required here over char8_t-- this is due to NDK not having a char8_t specialization for this yet. - deletable_facet> conv; - - void convstr(char const* inp, char16_t* outp, int sz) { - std::mbstate_t state; - char const* from_next; - char16_t* to_next; - conv.in(state, inp, inp + sz, from_next, outp, outp + sz, to_next); - } - std::size_t convstr(char16_t const* inp, char* outp, int isz, int osz) { - std::mbstate_t state; - char16_t const* from_next; - char* to_next; - auto convOut = conv.out(state, inp, inp + isz, from_next, outp, outp + osz, to_next); - if (convOut != std::codecvt_base::ok) { - throw convOut; - } - return (std::size_t)(to_next - outp); +template +struct deletable_facet : Facet { + template + deletable_facet(Args&&... args) : Facet(std::forward(args)...) {} + ~deletable_facet() {} +}; + +// Note that char is actually required here over char8_t-- this is due to NDK not having a char8_t specialization for this yet. +deletable_facet> conv; + +void convstr(char const* inp, char16_t* outp, int sz) { + std::mbstate_t state; + char8_t const* from_next; + char16_t* to_next; + conv.in(state, reinterpret_cast(inp), reinterpret_cast(inp + sz), from_next, outp, outp + sz, to_next); +} +std::size_t convstr(char16_t const* inp, char* outp, int isz, int osz) { + std::mbstate_t state; + char16_t const* from_next; + char8_t* to_next; + auto convOut = conv.out(state, inp, inp + isz, from_next, reinterpret_cast(outp), reinterpret_cast(outp + osz), to_next); + if (convOut != std::codecvt_base::ok) { + throw convOut; } + return (std::size_t)(to_next - reinterpret_cast(outp)); +} - Il2CppString* alloc_str(std::string_view str) { - il2cpp_functions::Init(); - return il2cpp_functions::string_new_len(str.data(), str.size()); - } - Il2CppString* alloc_str(std::u16string_view str) { - il2cpp_functions::Init(); - return il2cpp_functions::string_new_utf16((Il2CppChar const*) str.data(), str.size()); - } +Il2CppString* alloc_str(std::string_view str) { + il2cpp_functions::Init(); + return il2cpp_functions::string_new_len(str.data(), str.size()); +} +Il2CppString* alloc_str(std::u16string_view str) { + il2cpp_functions::Init(); + return il2cpp_functions::string_new_utf16((Il2CppChar const*)str.data(), str.size()); +} - Il2CppString* CreateString(int length) { - static MethodInfo const* methodInfo = il2cpp_utils::FindMethod(classof(Il2CppString*), "CreateString", std::array{ il2cpp_utils::ExtractIndependentType(), il2cpp_utils::ExtractIndependentType()}); - return CRASH_UNLESS(il2cpp_utils::RunStaticMethodUnsafe(methodInfo, Il2CppChar('\0'), length)); - } +Il2CppString* CreateString(int length) { + static MethodInfo const* methodInfo = il2cpp_utils::FindMethod(classof(Il2CppString*), "CreateString", + std::array{ il2cpp_utils::ExtractIndependentType(), il2cpp_utils::ExtractIndependentType() }); + return CRASH_UNLESS(il2cpp_utils::RunStaticMethodUnsafe(methodInfo, Il2CppChar('\0'), length)); +} - Il2CppString* strappend(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { - if (!lhs && !rhs) return nullptr; - - il2cpp_functions::Init(); - if (!lhs && rhs) - return il2cpp_functions::string_new_utf16(rhs->chars, rhs->length); - else if (lhs && !rhs) - return il2cpp_functions::string_new_utf16(lhs->chars, lhs->length); - else - { - size_t fullLength = lhs->length + rhs->length; - Il2CppString* result = CreateString(fullLength); - memcpy(result->chars, lhs->chars, lhs->length * sizeof(Il2CppChar)); - Il2CppChar* pastFirstString = result->chars + lhs->length; - memcpy(pastFirstString, rhs->chars, rhs->length * sizeof(*rhs->chars)); - return result; - } +Il2CppString* strappend(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { + if (!lhs && !rhs) return nullptr; + + il2cpp_functions::Init(); + if (!lhs && rhs) + return il2cpp_functions::string_new_utf16(rhs->chars, rhs->length); + else if (lhs && !rhs) + return il2cpp_functions::string_new_utf16(lhs->chars, lhs->length); + else { + size_t fullLength = lhs->length + rhs->length; + Il2CppString* result = CreateString(fullLength); + memcpy(result->chars, lhs->chars, lhs->length * sizeof(Il2CppChar)); + Il2CppChar* pastFirstString = result->chars + lhs->length; + memcpy(pastFirstString, rhs->chars, rhs->length * sizeof(*rhs->chars)); + return result; } +} - Il2CppString* strappend(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { - if (lhs) - { - int fullLength = lhs->length + rhs.size(); - Il2CppString* result = CreateString(fullLength); - memcpy(result->chars, lhs->chars, lhs->length * sizeof(Il2CppChar)); - Il2CppChar* pastFirstString = result->chars + lhs->length; - static_assert(sizeof(*pastFirstString) == sizeof(*rhs.data())); - memcpy(pastFirstString, rhs.data(), rhs.size() * sizeof(*rhs.data())); - return result; - } - else - { - return alloc_str(rhs); - } +Il2CppString* strappend(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { + if (lhs) { + int fullLength = lhs->length + rhs.size(); + Il2CppString* result = CreateString(fullLength); + memcpy(result->chars, lhs->chars, lhs->length * sizeof(Il2CppChar)); + Il2CppChar* pastFirstString = result->chars + lhs->length; + static_assert(sizeof(*pastFirstString) == sizeof(*rhs.data())); + memcpy(pastFirstString, rhs.data(), rhs.size() * sizeof(*rhs.data())); + return result; + } else { + return alloc_str(rhs); } +} - Il2CppString* strappend(Il2CppString const* lhs, std::string_view const rhs) noexcept { - if (lhs) - { - int fullLength = lhs->length + rhs.size(); - Il2CppString* result = CreateString(fullLength); - memcpy(result->chars, lhs->chars, lhs->length * sizeof(Il2CppChar)); - Il2CppChar* pastFirstString = result->chars + lhs->length; - convstr(rhs.data(), pastFirstString, rhs.size()); - return result; - } - else - { - return alloc_str(rhs); - } +Il2CppString* strappend(Il2CppString const* lhs, std::string_view const rhs) noexcept { + if (lhs) { + int fullLength = lhs->length + rhs.size(); + Il2CppString* result = CreateString(fullLength); + memcpy(result->chars, lhs->chars, lhs->length * sizeof(Il2CppChar)); + Il2CppChar* pastFirstString = result->chars + lhs->length; + convstr(rhs.data(), pastFirstString, rhs.size()); + return result; + } else { + return alloc_str(rhs); } +} - Il2CppString* strappend(std::string_view const lhs, Il2CppString const* rhs) noexcept { - if (rhs) - { - int fullLength = rhs->length + lhs.size(); - Il2CppString* result = CreateString(fullLength); - convstr(lhs.data(), result->chars, lhs.size()); - Il2CppChar* pastFirstString = result->chars + lhs.size(); - memcpy(pastFirstString, rhs->chars, rhs->length * sizeof(*pastFirstString)); - return result; - } - else - { - return alloc_str(lhs); - } +Il2CppString* strappend(std::string_view const lhs, Il2CppString const* rhs) noexcept { + if (rhs) { + int fullLength = rhs->length + lhs.size(); + Il2CppString* result = CreateString(fullLength); + convstr(lhs.data(), result->chars, lhs.size()); + Il2CppChar* pastFirstString = result->chars + lhs.size(); + memcpy(pastFirstString, rhs->chars, rhs->length * sizeof(*pastFirstString)); + return result; + } else { + return alloc_str(lhs); } +} - Il2CppString* strappend(std::u16string_view const lhs, Il2CppString const* rhs) noexcept { - if (rhs) - { - int fullLength = rhs->length + lhs.size(); - Il2CppString* result = CreateString(fullLength); - static_assert(sizeof(Il2CppChar) == sizeof(*lhs.data())); - memcpy(result->chars, lhs.data(), lhs.size() * sizeof(*lhs.data())); - Il2CppChar* pastFirstString = result->chars + lhs.size(); - memcpy(pastFirstString, rhs->chars, rhs->length * sizeof(*pastFirstString)); - return result; - } - else - { - return alloc_str(lhs); - } +Il2CppString* strappend(std::u16string_view const lhs, Il2CppString const* rhs) noexcept { + if (rhs) { + int fullLength = rhs->length + lhs.size(); + Il2CppString* result = CreateString(fullLength); + static_assert(sizeof(Il2CppChar) == sizeof(*lhs.data())); + memcpy(result->chars, lhs.data(), lhs.size() * sizeof(*lhs.data())); + Il2CppChar* pastFirstString = result->chars + lhs.size(); + memcpy(pastFirstString, rhs->chars, rhs->length * sizeof(*pastFirstString)); + return result; + } else { + return alloc_str(lhs); } +} - template +template requires(std::is_same_v || std::is_same_v) - bool strcomp(Il2CppString const* lhs, T const rhs) noexcept - { - if (!lhs || lhs->length != (int)rhs.size()) return false; - - Il2CppChar const* first = lhs->chars; - auto const* second = rhs.data(); - Il2CppChar const* firstEnd = first + lhs->length; - auto const* secondEnd = second + (int)rhs.size(); - - while (first != firstEnd && second != secondEnd) - { - if (*first != *second) return false; - first++; second++; - } - - return first == firstEnd && second == secondEnd; +bool strcomp(Il2CppString const* lhs, T const rhs) noexcept { + if (!lhs || lhs->length != (int)rhs.size()) return false; + + Il2CppChar const* first = lhs->chars; + auto const* second = rhs.data(); + Il2CppChar const* firstEnd = first + lhs->length; + auto const* secondEnd = second + (int)rhs.size(); + + while (first != firstEnd && second != secondEnd) { + if (*first != *second) return false; + first++; + second++; } - bool strcomp(Il2CppString const* lhs, std::string_view const rhs) noexcept { - return strcomp>(lhs, rhs); - } - - bool strcomp(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { - return strcomp>(lhs, rhs); - } + return first == firstEnd && second == secondEnd; +} + +bool strcomp(Il2CppString const* lhs, std::string_view const rhs) noexcept { + return strcomp>(lhs, rhs); +} - bool strcomp(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { - if (lhs == rhs) return true; - if (!lhs || !rhs || lhs->length != rhs->length) return false; +bool strcomp(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { + return strcomp>(lhs, rhs); +} - Il2CppChar const* first = lhs->chars; - Il2CppChar const* second = rhs->chars; - Il2CppChar const* firstEnd = first + lhs->length; - Il2CppChar const* secondEnd = second + rhs->length; +bool strcomp(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { + if (lhs == rhs) return true; + if (!lhs || !rhs || lhs->length != rhs->length) return false; - while (first != firstEnd && second != secondEnd) - { - if (*first != *second) return false; - first++; second++; - } + Il2CppChar const* first = lhs->chars; + Il2CppChar const* second = rhs->chars; + Il2CppChar const* firstEnd = first + lhs->length; + Il2CppChar const* secondEnd = second + rhs->length; - return first == firstEnd && second == secondEnd; + while (first != firstEnd && second != secondEnd) { + if (*first != *second) return false; + first++; + second++; } - template + return first == firstEnd && second == secondEnd; +} + +template requires(std::is_same_v || std::is_same_v) - bool strless(Il2CppString const* lhs, T const rhs) noexcept { - if (!lhs) return true; - - Il2CppChar const* first = lhs->chars; - auto const* second = rhs.data(); - Il2CppChar const* firstEnd = lhs->chars + lhs->length; - auto const* secondEnd = rhs.data() + rhs.size(); - - while (first != firstEnd && second != secondEnd) - { - if (*first == *second) - { - first++; second++; - continue; - } - return *first < *second; +bool strless(Il2CppString const* lhs, T const rhs) noexcept { + if (!lhs) return true; + + Il2CppChar const* first = lhs->chars; + auto const* second = rhs.data(); + Il2CppChar const* firstEnd = lhs->chars + lhs->length; + auto const* secondEnd = rhs.data() + rhs.size(); + + while (first != firstEnd && second != secondEnd) { + if (*first == *second) { + first++; + second++; + continue; } - // if we got here, and second is not second end, we had a shorter first, so it should be true - // if second is the end, we are longer, so it should be false - return second != secondEnd; + return *first < *second; } + // if we got here, and second is not second end, we had a shorter first, so it should be true + // if second is the end, we are longer, so it should be false + return second != secondEnd; +} - bool strless(Il2CppString const* lhs, std::string_view const rhs) noexcept { - return strless>(lhs, rhs); - } - - bool strless(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { - return strless>(lhs, rhs); - } - - bool strless(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { - if (!lhs && !rhs) return false; - if (!lhs) return true; - if (!rhs) return false; - - Il2CppChar const* first = lhs->chars; - Il2CppChar const* second = rhs->chars; - Il2CppChar const* firstEnd = first + lhs->length; - Il2CppChar const* secondEnd = second + rhs->length; - - while (first != firstEnd && second != secondEnd) - { - if (*first == *second) - { - first++; second++; - continue; - } - return *first < *second; +bool strless(Il2CppString const* lhs, std::string_view const rhs) noexcept { + return strless>(lhs, rhs); +} + +bool strless(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { + return strless>(lhs, rhs); +} + +bool strless(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { + if (!lhs && !rhs) return false; + if (!lhs) return true; + if (!rhs) return false; + + Il2CppChar const* first = lhs->chars; + Il2CppChar const* second = rhs->chars; + Il2CppChar const* firstEnd = first + lhs->length; + Il2CppChar const* secondEnd = second + rhs->length; + + while (first != firstEnd && second != secondEnd) { + if (*first == *second) { + first++; + second++; + continue; } - // if we got here, and second is not second end, we had a shorter first, so it should be true - // if second is the end, we are longer, so it should be false - return second != secondEnd; + return *first < *second; } + // if we got here, and second is not second end, we had a shorter first, so it should be true + // if second is the end, we are longer, so it should be false + return second != secondEnd; +} - template +template requires(std::is_same_v || std::is_same_v) - bool strstart(Il2CppString const* lhs, T const rhs) noexcept { - if (!lhs || lhs->length < (decltype(lhs->length))rhs.size()) return false; - - Il2CppChar const* first = lhs->chars; - auto const* second = rhs.data(); - auto const* secondEnd = second + rhs.size(); - - while (second != secondEnd) - { - if (*first == *second) - { - first++; second++; - continue; - } - // we got a mismatch! return false; - return false; +bool strstart(Il2CppString const* lhs, T const rhs) noexcept { + if (!lhs || lhs->length < (decltype(lhs->length))rhs.size()) return false; + + Il2CppChar const* first = lhs->chars; + auto const* second = rhs.data(); + auto const* secondEnd = second + rhs.size(); + + while (second != secondEnd) { + if (*first == *second) { + first++; + second++; + continue; } - // if we got through the entire string it was all equal, return true - return true; + // we got a mismatch! return false; + return false; } + // if we got through the entire string it was all equal, return true + return true; +} - bool strstart(Il2CppString const* lhs, std::string_view const rhs) noexcept { - return strstart>(lhs, rhs); - } - - bool strstart(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { - return strstart>(lhs, rhs); - } +bool strstart(Il2CppString const* lhs, std::string_view const rhs) noexcept { + return strstart>(lhs, rhs); +} + +bool strstart(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { + return strstart>(lhs, rhs); +} + +bool strstart(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { + // if either instance is nullptr, return false, if our length is smaller than prefix length, also return false + if (!lhs || !rhs || lhs->length < rhs->length) return false; + + Il2CppChar const* first = lhs->chars; + Il2CppChar const* second = rhs->chars; + Il2CppChar const* secondEnd = second + lhs->length; - bool strstart(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { - // if either instance is nullptr, return false, if our length is smaller than prefix length, also return false - if (!lhs || !rhs || lhs->length < rhs->length) return false; - - Il2CppChar const* first = lhs->chars; - Il2CppChar const* second = rhs->chars; - Il2CppChar const* secondEnd = second + lhs->length; - - while (second != secondEnd) - { - if (*first == *second) - { - first++; second++; - continue; - } - // we got a mismatch! return false; - return false; + while (second != secondEnd) { + if (*first == *second) { + first++; + second++; + continue; } - // if we got through the entire string it was all equal, return true - return true; + // we got a mismatch! return false; + return false; } + // if we got through the entire string it was all equal, return true + return true; +} - - template +template requires(std::is_same_v || std::is_same_v) - bool strend(Il2CppString const* lhs, T const rhs) noexcept { - if (!lhs || lhs->length < (decltype(lhs->length))rhs.size()) return false; - - Il2CppChar const* first = lhs->chars + lhs->length - 1; - auto const* secondBegin = rhs.data() - 1; - auto const* second = secondBegin + rhs.size(); - - while (second != secondBegin) - { - if (*first == *second) - { - first--; second--; - continue; - } - // we got a mismatch! return false; - return false; +bool strend(Il2CppString const* lhs, T const rhs) noexcept { + if (!lhs || lhs->length < (decltype(lhs->length))rhs.size()) return false; + + Il2CppChar const* first = lhs->chars + lhs->length - 1; + auto const* secondBegin = rhs.data() - 1; + auto const* second = secondBegin + rhs.size(); + + while (second != secondBegin) { + if (*first == *second) { + first--; + second--; + continue; } - // if we got through the entire string it was all equal, return true - return true; + // we got a mismatch! return false; + return false; } + // if we got through the entire string it was all equal, return true + return true; +} - bool strend(Il2CppString const* lhs, std::string_view const rhs) noexcept { - return strend>(lhs, rhs); - } - - bool strend(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { - return strend>(lhs, rhs); - } +bool strend(Il2CppString const* lhs, std::string_view const rhs) noexcept { + return strend>(lhs, rhs); +} + +bool strend(Il2CppString const* lhs, std::u16string_view const rhs) noexcept { + return strend>(lhs, rhs); +} + +bool strend(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { + if (!lhs || !rhs || lhs->length < rhs->length) return false; + + Il2CppChar const* first = lhs->chars + lhs->length - 1; + Il2CppChar const* secondBegin = rhs->chars - 1; + Il2CppChar const* second = secondBegin + rhs->length; - bool strend(Il2CppString const* lhs, Il2CppString const* rhs) noexcept { - if (!lhs || !rhs || lhs->length < rhs->length) return false; - - Il2CppChar const* first = lhs->chars + lhs->length - 1; - Il2CppChar const* secondBegin = rhs->chars - 1; - Il2CppChar const* second = secondBegin + rhs->length; - - while (second != secondBegin) - { - if (*first == *second) - { - first--; second--; - continue; - } - // we got a mismatch! return false; - return false; + while (second != secondBegin) { + if (*first == *second) { + first--; + second--; + continue; } - // if we got through the entire string it was all equal, return true - return true; + // we got a mismatch! return false; + return false; } - -} + // if we got through the entire string it was all equal, return true + return true; } +} // namespace detail +} // namespace il2cpp_utils + StringW::operator std::string() const { std::string val(inst->length * sizeof(wchar_t) + 1, '\0'); auto resSize = il2cpp_utils::detail::convstr(inst->chars, val.data(), inst->length, val.size()); @@ -355,30 +336,42 @@ StringW::operator std::string() const { } StringW::operator std::u16string() const { - return {inst->chars, inst->chars + inst->length}; + return { inst->chars, inst->chars + inst->length }; } StringW::operator std::wstring() const { - return {inst->chars, inst->chars + inst->length}; + return { inst->chars, inst->chars + inst->length }; } StringW::operator std::u16string_view const() const { - return {inst->chars, static_cast(inst->length)}; + return { inst->chars, static_cast(inst->length) }; } StringW::operator std::u16string_view() { - return {inst->chars, static_cast(inst->length)}; + return { inst->chars, static_cast(inst->length) }; } -StringW::iterator StringW::begin() { return inst->chars; } -StringW::const_iterator StringW::begin() const { return inst->chars; } -StringW::iterator StringW::end() { return inst->chars + inst->length; } -StringW::const_iterator StringW::end() const { return inst->chars + inst->length; } +StringW::iterator StringW::begin() { + return inst->chars; +} +StringW::const_iterator StringW::begin() const { + return inst->chars; +} +StringW::iterator StringW::end() { + return inst->chars + inst->length; +} +StringW::const_iterator StringW::end() const { + return inst->chars + inst->length; +} StringW::operator std::span() { - return {begin(), end()}; + return { begin(), end() }; +} +StringW::operator std::span const() const { + return { begin(), end() }; +} +Il2CppChar const& StringW::operator[](size_t const& idx) const { + return inst->chars[idx]; } -StringW::operator std::span const () const { - return {begin(), end()}; +Il2CppChar& StringW::operator[](size_t const& idx) { + return inst->chars[idx]; } -Il2CppChar const& StringW::operator [](size_t const& idx) const { return inst->chars[idx]; } -Il2CppChar& StringW::operator [](size_t const& idx) { return inst->chars[idx]; }