Author Topic: [HOWTO] Debug external DLLs with DnSpy  (Read 1197 times)

Offline Asteroid

  • Jr. Member
  • **
  • Posts: 65
[HOWTO] Debug external DLLs with DnSpy
« on: September 22, 2019, 01:03:23 AM »
EDIT: While this does work, head over here for an even more major breakthrough!


Hi guys!

I found a way to attach to the game and step into the (decompiled) code of otherwise impossible to debug DLLs such as AIWarExternalCode.dll, as you can see in the attached screenshot. The best part is, it doesn't require installing or launching Unity, so it might be of use to people under Linux such as Badger.

  • Download dnSpy decompiler - if you're under Linux you probably want to get the .NET Core version (I've only tested under Windows).
  • Download from the same release page - the year must match the version of Unity currently used by the AIW2Unity project, 2019.1.7f1 as of writing this.
  • Make a backup of AIWar2\Builds\MonoBleedingEdge\EmbedRuntime\mono-2.0-bdwgc.dll
  • Find inside the correct version of mono-2.0-bdwgc.dll to match the version of Unity currently used by the AIW2Unity project - currently this is the one under unity-2019.1.7\win64\
  • Replace AIWar2\Builds\MonoBleedingEdge\EmbedRuntime\mono-2.0-bdwgc.dll with the file from you identified as the correct one.
  • Launch AIWar2\Builds\AIWar2.exe
  • Launch dnSpy
  • Choose "Debug -> Start Debugging..." from the menus or press F5.
  • Choose Unity (Connect) as debug engine and press Ok.
  • The debugger is now attached! You can go to the Modules view ("Debug -> Windows -> Modules" or Ctrl-Alt-U) and double-click an assembly to decompile it. The ones starting with "data-" followed by a seemingly random string are the dynamically loaded ones such as AIWarExternalCode.dll.
  • Now you can browse the classes and decompiled source of the assembly you opened and set breakpoints, examine variables, ...

On my end I did two extra steps that might've helped, haven't tested without them yet but I suspect they are not necessary:
- Decommented the code that loads the .pdb file in ArcenExternalTypeManager.cs:122, and recompiled the ArcenUniversal dll.
- Copied AIWarExternalCode.pdb to ArcenGames\AIWar2\GameData\ModdableLogicDLLs\ so it's next to the AIWarExternalCode.dll.

Needless to say the above procedure will yield much better results if the dll you're trying to decompile is a Debug one - I think dnSpy will manage to decompile a release assembly, but the resulting source code will be much different from the original.

If you try it, reply here to let me know if it worked for you.

You can find a more complete version of these instructions here, along with other possibilities offered by dnSpy such as turning a release build into a dev build (untested):
« Last Edit: September 22, 2019, 03:44:00 AM by Asteroid »