NeighTools / UnityDoorstop

Doorstop -- run C# before Unity does!
GNU Lesser General Public License v2.1
454 stars 69 forks source link

Add support for debugging on old net35 mono #60

Closed aldelaro5 closed 5 months ago

aldelaro5 commented 5 months ago

After a ton of research, I figured out how to add support for debugging on old mono that targets net35. A Unity game may ship with depending on the Unity version:

Adding this feature also fixes a regression from Doorstop 3 where it was no longer possible to debug using dnspy on these old mono because Doorstop 4 tried to enable the debugger and failed since it wasn't supported. Now, not only this works, but it no longer requires to have a patched mono.

To detect the mono, this was rather simple because checking the sources of unity's mono, I found old mono's runtime string can only start with v1 or v2 while new mono cannot and will typically start with v4. It just so happens that jit_init_version receives that string so I used it to detect which mono we are dealing with.

As for how this was done, old mono requires a different ordering of when to call jit_init_version where it must be called later than what you'll do on new mono. Since Doorstop was always doing what worked on new mono, it was crashing. After inspecting a development player of unity 5.5.4, I found that it would call debug_init right after jit_parse_options was called. What doorstop was doing is call jit_init_version right after debug_init, but it should have done it the other way around for old mono.

There was one last detail to handle: the protocol changed slightly such that to opt out of the suspend feature, it needs to have defer=y on top of the usual suspend=n. I simply changed the text it appends to accommodate depending on the mono used.

I did a bunch of tests with this using dnspy and 3 IDEs and everything seems to work on both old and new mono. That being said, I wanted to test the capabilities of this since I find this to be quite a huge deal so I did some more indepth tests and here's what I found:

Due to the fact this pr also fixes a regression, I recommend that a release of doorstop is made with it (I can then send a pr to bepinex v5 and v6 so it gets the fix and the feature).