BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
185 stars 59 forks source link

Improve unstrip support #108

Open burnedram opened 11 months ago

burnedram commented 11 months ago

What

Why

I'm creating a Il2Cpp plugin for Dave the Diver and wanted to use IMGUI, and came across a few "Method unstripping failed".
So I set out to find the reason why those methods I needed didn't work and this is the result.

Translation logging

"Method unstripping failed" is a very opaque error.
This PR adds logging (to disk only) to the translation process describing why a method couldn't be translated:

The logging is saved as a .json.gz in the output's parent folder.
(I.e. it is saved in the same folder that BepInEx saves LogOutput.log)

I choose json because it is very easy to use tools such as jq to query the rather large amount of methods that can't be translated currently.

Examples

Excerpt

{
  "name": "UnityEngine.Experimental.Audio.AudioSampleProvider::get_sampleRate",
  "fullName": "System.UInt32 UnityEngine.Experimental.Audio.AudioSampleProvider::get_sampleRate()",
  "scope": "UnityEngine.AudioModule.dll",
  "namespace": "UnityEngine.Experimental.Audio",
  "type": "AudioSampleProvider",
  "method": "get_sampleRate",
  "instruction": { // The instruction that couldn't be translated
    "description": "IL_0001: ldfld System.UInt32 UnityEngine.Experimental.Audio.AudioSampleProvider::<sampleRate>k__BackingField",
    "opCode": "ldfld",
    "operandType": "InlineField",
    "operandValueType": "FieldDefinition",
    "operand": "System.UInt32 UnityEngine.Experimental.Audio.AudioSampleProvider::<sampleRate>k__BackingField"
  },
  "result": "FieldProxy", // A general error category
  // A message describing what went wrong
  "reason": "Could not find getter for proxy property System.UInt32 UnityEngine.Experimental.Audio.AudioSampleProvider::<sampleRate>k__BackingField"
}

Logs from my development

The entire log file before I made any fixes/changes: unstrip_original.json.gz Summary of error categories:

The entire log file with all commits in this PR: unstrip.json.gz Summary of error categories:

These logs are of course very specific to the game and Unity version I've been using, but they document the progress that I've made.

Results

Il2CppInterop now correctly translates the IMGUI methods I need in Dave The Diver v1.0.0.1055.steam! That were the metrics I went by atleast, but there's a whole slew of methods that now work correctly, including:

Additionally, I've added some box/unbox support, ld(s)flda support, and branch retargeting tracking. The branch retargeting was mostly working before, but it was mostly coincidental. This PR allows branch targets to be a completely different instruction (which is very common due to the processes being a translation), and it allows us to emit/insert more that one instruction in the new method and still keep the branches intact.

I more than welcome any comments, as I have many assumptions on how the library is supposed to work!

akarnokd commented 8 months ago

Hi. Sorry for pinging.

I've found this issue based on the exception message I get for Canvas.renderMode in an Unity 2023.1.9f1 IL2CPP translated game.

Since this PR isn't merged and isn't released with the BepInEx nightly, how can I try these changes to see if it fixes my problem?

Edit

Okay, figured it out. Checkout your fork, build.bat, open the BepInEx\core and replace the 4 IL2CppInterop dll of the nightly build with the various generated new dlls. Reset the BepInEx install, run the game. Now I got the method above not throwing by itself, but now I get a different runtime exception:

set_renderMode_Injected(MarshalledUnityObject.MarshalNullCheck((_0021_00210)this), value);
System.MissingMethodException: Method not found: 'IntPtr MarshalledUnityObject.MarshalNullCheck(!!0)'.
   at UnityEngine.Canvas.set_renderMode(RenderMode value)

Edit 2

Managed to get it working. I have to manually call that inject method with a properly cast target object:

var panel = new GameObject("MyOverlay");
var canvas = panel.AddComponent<Canvas>();
canvas.sortingOrder = 1000;
Canvas.set_renderMode_Injected(
    MarshalledUnityObject.MarshalNullCheck(canvas), 
    RenderMode.ScreenSpaceOverlay
);