How to debug RimWorld from Visual Studio - Mhburg/AwsomeInventory GitHub Wiki
Environment: Visual Studio Community 2019+, Unity Tools for Visual Studio
Steps
- Install Visual Studio Community 2019 or better and choose workload for developing with Unity. If VS is already installed, please refer to Visual Studio tool for Unity
Download Unity-debugging-2019.x.zip from dnSpy. More information can be found on its wiki.- For the moment, you will have to follow this tutorial to acquire a debug build dll for unity
- Backup RimWorld folder. More specifically, backup the dll below,
📁---Installation_path_of_RimWorld
| 📁---RimWorld
| 📁---MonoBleedingEdge
| 📁---EmbedRuntime
| ⚙️---mono-2.0-bdwgc.dll
- Unzip the file from dnSpy, copy the
mono-2.0-bdwgc.dll
in folderunity-2019.1.8
or newer version and drop it in the directory in step 3 to replace the vanilla one. - Add RimWorld.exe as an existing project to solution as such.
- Right click on the added RimWorld.exe and add environment variable
Variable=dnspy_unity_dbg2, Value=--debugger-agent=transport=dt_socket,server=y,address=127.0.0.1:56000,suspend=y,no-hide-debugger. And set it as the startup project via the right-click context menu. - Update the build information of your project and have it produce "Portable" debugging information. Uncheck "Optimize Code" if it is checked. Example
- Almost there. Now, build the solution, start it without debugging. The default hotkey is "Ctrl+F5". And nothing will happen until a unity debugger is attached. Press "Attach Unity Debugger", click "Input IP" on the pop up window and "OK". RimWorld now will spin up with a debugger attached. Example
Tips
- If RimWorld is installed through Steam, start Steam before the debug session, otherwise, there will be loading errors related to not found mods.
- Don't forget to build your solution and copy the pdb files along with the dll files to mod folders.
- If the game is patched with a transpiler written with Harmony. There is a chance that transpiler could throw an exception given the replaced runtime might emit different IL code than the original one.
- At times, when a break point is set, it reads the break point will not currently be hit. Make sure symbols are loaded, check it in Debug->Window->Module. It could show the warning even though symbols are loaded. Don't worry, VS is not exactly working seamlessly with an Mono runtime in an out-of-process debug session. Just ignore the message and the code will hit the break point.