ByNameModding / BNM-Android

Modding il2cpp games by classes, methods, fields names on Android.
MIT License
203 stars 39 forks source link

Question? How to read text from byte array #27

Closed dvgmdvgm closed 7 months ago

dvgmdvgm commented 7 months ago

Hello. I have arg (byte[] value); How to read this byte array as a utf8 string?

Previously i can do this with frida like this: Memory.readUtf8String(value.add(0x20), Memory.readInt(value.add(0x18))) Where 0x18 is "value"'s length.

Can you help me?

BNM-Dev commented 7 months ago

If string C like, this code should work:

void ctorHook(void *instance, monoArray<uint8_t> *value) {
    // ... other code
    auto utf8 = std::string{(char *)value->m_Items, (char *)value->m_Items + value->capacity};
    BNM_LOG_INFO("utf8 string: %s", utf8.c_str());
    // ... other code
}

If not, you can use C# string API:

BNM::Method<void *> get_UTF8; // System.Text.Encoding.get_UTF8
BNM::Method<monoString *> GetString; // System.Text.Encoding.GetString(byte[] bytes)

void ctorHook(void *instance, monoArray<uint8_t> *value) {
    // ... other code
    auto string = GetString[get_UTF8()].Virtualize().cast<monoString *>()(value);
    BNM_LOG_INFO("utf8 string: %s", string->str().c_str());
    // ... other code
}
dvgmdvgm commented 7 months ago
auto utf8 = std::string{(char *)value->m_Items, (char *)value->m_Items + value->capacity};

first example working perfectly. Thank you