I’m currently working on reverse-engineering an Unity game on iOS: it uses IL2CPP to convert managed code to C++, so my idea would be to inject a dynamic library into its process (dylib which is supposed to contain other IL2CPP-generated code to replace the original functions).
I’m using a jailbroken iPhone with Theos in order to hook the stuff I’m interested in.
At this point, I’ve been able to create custom Il2CppObject structs with the only purpose to replicate the original game behaviour: I can use them to read data coming from function parameters, for example.
I’ve got problems calling my custom functions, though, as these methods are supposed to be loaded when unity runtime starts up and then initialised when required (MetadataCache::InitializeMethodMetadata): after having digged a bit into the IL2CPP source code, I found out that global-metadata initialisation and execution are handled by the MetadataCache and MetadataLoader classes.
Long story short, I’ve tried to initialise both of them “manually”… but they heavily depend on other functions being called before (Runtime::Init, MetadataCache::Register and so on), which makes it quite impossible to use, unless actually initialising the entire Unity runtime engine.
Of course I’ve tried to go trough that way: basically, have an unity game load two different unity engines (one in the main binary, and the other in the injected dynamic library), just to be able to invoke my custom IL2CPP-generated methods.
Not only this idea seems pretty much like an overkill… but also, it does not seem to work: inside my dylib load method, I tried to call both “UnityInitRuntime” and “UnityInitApplicationNoGraphics” methods, but the game crashes at startup with the following exception:
Even when trying to avoid to load the entire runtime (and register MetadataCache manually, instead - via “s_Il2CppCodegenRegistration()”) the game stays stuck at landing screen for 30 seconds… and then I get another unexpected behaviour:
Basically, no matter what I do (or how I do it), I still get exceptions: these are just a couple of the literally dozens of paths I’ve been trying to follow in order to make this thing work… I just can’t seem to be able to do it.
So I feel like it’s time to come to the official Unity forum and ask for help: I know I’m trying to do something which is not supposed to happen in common use-cases, but am I actually doing something wrong? Shall I surrender to the idea of not being able to use my custom methods at all… just cause “they require the IL2CPP runtime library, which requires to be initialised from Unity, which is simply throwing errors when invoked from a dylib”?
Thanks to anyone who could help me figure this out.
The for-loop is commented: it means that the actual content has been removed from the image. In fact it has been posted in the second one.
If you had paid enough attention, you’d have understood that the my problem is not really related to that code I posted, but more to the fact that I could not initialise Unity runtime from inside my dynamic library.