Jump to content

KSP Plugin debugging and profiling for Visual Studio and Monodevelop on all OS


Recommended Posts

Still working great in 1.0.5, just needed a couple small additions and some changes to existing values to get the patcher working again

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0003", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0004", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0005", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0006", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\a", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\b", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\t", 8, "Start");

MoveInitializerIntoAwake(asm, "FlightIntegrator", "sunLayerMask", 24, "Start");

MoveInitializerIntoAwake(asm, "GameSettings", "INPUT_DEVICES", 2, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u0019", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001A", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001B", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001C", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001D", 8, "Awake");

MoveInitializerIntoAwake(asm, "MapView", "\r", 8, "Start");

MoveInitializerIntoAwake(asm, "ModuleAblator", "\b\b", 8, "Start");

MoveInitializerIntoAwake(asm, "PhysicsGlobals", "\u001d\u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_MaterialQuadRelative", "\u001e\u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_MaterialQuadRelative", "\u001f\u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_MaterialQuadRelative", " \u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\n\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\v\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\f\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\r\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "SkySphereControl", "\u0002", 8, "Start");

MoveInitializerIntoAwake(asm, "SkySphereControl", "\u0003", 8, "Start");

MoveInitializerIntoAwake(asm, "SkySphereControl", "\u0004", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\b", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\t", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\n", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\v", 8, "Start");

MoveInitializerIntoAwake(asm, "UnderwaterTint", "colorID", 8, "Awake");

Link to comment
Share on other sites

Still working great in 1.0.5, just needed a couple small additions and some changes to existing values to get the patcher working again

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0003", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0004", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0005", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\u0006", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\a", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\b", 8, "Start");

MoveInitializerIntoAwake(asm, "AtmosphereFromGround", "\t", 8, "Start");

MoveInitializerIntoAwake(asm, "FlightIntegrator", "sunLayerMask", 24, "Start");

MoveInitializerIntoAwake(asm, "GameSettings", "INPUT_DEVICES", 2, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u0019", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001A", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001B", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001C", 8, "Awake");

MoveInitializerIntoAwake(asm, "HighLogic", "\u001D", 8, "Awake");

MoveInitializerIntoAwake(asm, "MapView", "\r", 8, "Start");

MoveInitializerIntoAwake(asm, "ModuleAblator", "\b\b", 8, "Start");

MoveInitializerIntoAwake(asm, "PhysicsGlobals", "\u001d\u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_MaterialQuadRelative", "\u001e\u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_MaterialQuadRelative", "\u001f\u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_MaterialQuadRelative", " \u0002", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\n\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\v\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\f\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "PQSMod_OceanFX", "\r\u0003", 8, "Awake");

MoveInitializerIntoAwake(asm, "SkySphereControl", "\u0002", 8, "Start");

MoveInitializerIntoAwake(asm, "SkySphereControl", "\u0003", 8, "Start");

MoveInitializerIntoAwake(asm, "SkySphereControl", "\u0004", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\b", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\t", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\n", 8, "Start");

MoveInitializerIntoAwake(asm, "SunShaderController", "\v", 8, "Start");

MoveInitializerIntoAwake(asm, "UnderwaterTint", "colorID", 8, "Awake");

I'm obviously doing something wrong.. I've re-compiled the patcher using above. When I try to run it it hangs.... nothing...

Yes - using .NET3.5 running it in the managed folder, etc.

Link to comment
Share on other sites

  • 2 weeks later...
Hey guys, I have followed the instructions and I managed to get my KSP into Devmode, patched the Mono.cecil, created an MDB file for my mod.
However, when I start up KSP and Visual Studio 2015, I can not attach to the running Unity process. In VS2015 Debug->Attach Unity Debugger shows no Unity processes running.

I can check with [CODE]netstat /b /a[/CODE], and indeed it shows that the KSP.exe process has a port in the "correct" range open (around 55000). But even if I add 127.0.0.1:<port> manually in the VS2015 dialog, the only thing that happens is that the mouse pointer turns into the waiting circle and then back to normal again.
I don't really know where to look for errors here...

Does anyone have Ideas how to fix that?

ps.:
What do the numbers in "PlayerConnectionConfigFile" mean? I have such a file in <KSP>/Launcher_Data, but the number is 792067676 instead of 803286173, like in the OP.
Link to comment
Share on other sites

[quote name='Kobymaru']Hey guys, I have followed the instructions and I managed to get my KSP into Devmode, patched the Mono.cecil, created an MDB file for my mod.
[snip]
...
What do the numbers in "PlayerConnectionConfigFile" mean? I have such a file in <KSP>/Launcher_Data, but the number is 792067676 instead of 803286173, like in the OP.[/QUOTE]
Did you create a PlayerConnectionConfigFile inside <KSP>/KSP_Data with the contents from the OP?
Link to comment
Share on other sites

[quote name='xEvilReeperx']Did you create a PlayerConnectionConfigFile inside <KSP>/KSP_Data with the contents from the OP?[/QUOTE]
I did not! I created it in <KSP> itself. Thanks for the hint!

Aw man, this stuff is awesome :D Proper debugging for Plugins has got to be the best thing that happened to the Modding community in a while.

ps.: All the info is in the thread, but I feel that the OP needs some updating.

[COLOR="silver"][SIZE=1]- - - Updated - - -[/SIZE][/COLOR]

Maybe sarbian can use this updated text for debugging. I added the patching step with a download to the patching tool and one note about converting PDB files that come out of Visual Studio 2015

[spoiler="New text for OP"]As we all know debugging plugin in KSP is mostly done with endless spam of Debug.Log or a bunch of custom windows we have to build. We also have [URL="http://forum.kerbalspaceprogram.com/threads/73586"]angavrilov method[/URL] but it require to recompile the whole mono and is a bit scary. So here is the result of a couple of days of experiment on my side to get a simple method working.

So how do we get debugging to work for KSP ? Simply by using the tool that unity provides us. Unity as a debugging system for game that runs in development mode, which is not the case of KSP by default.

So here is how to get that (Linux and OSX use read that part and I will explain the difference for you further down)

[SIZE=3][B]KSP[/B][/SIZE]
- First you need a [URL="http://unity3d.com/get-unity/download/archive"]Unity installation[/URL] using the exact same version as your current KSP ( For 0.90 you need Unity 4.5.5f1). That version is in the first line of the output_log.txt/Player.log.

- Go to your Unity install for that version ( "C:\Program Files (x86)\Unity\Editor" most likely) and then go in the "Data\PlaybackEngines\windowsstandalonesupport\Variations\win32_development" sub-directory. Copy the "player_win.exe" file to your KSP dev install (next to KSP.exe)

- Rename your KSP.exe into something else (or delete it). Rename "player_win.exe" in "KSP.exe"

- In KSP_Data create a text file named "PlayerConnectionConfigFile" (no extension !) and paste that line into it (it makes the mono debugger starts its listening port): [code]listen 803286173 1 1[/code]

- Launch that KSP and check that you have "Developement Build" in your lower right corner

- Your KSP will hang now with some errors, because you need to patch Mono.Cecil.dll. Close it, and download the current Patcher from [URL="https://github.com/seanmcdougall/KspDevModePatcher/releases"]here[/URL]. Unpack it and place KspDevModePatcher.exe into <KSP>/KSP_Data/Managed. Run the patcher from the command line. If the patching succeeded, your KSP should start up properly.

- Compile your plugin in "Debug" configuration to get the "MyPlugin.dll" [B]AND[/B] the "MyPlugin.pdb" (for VS) or the "MyPlugin.dll.mdb" (MonoDevelop or mono cmd line) . Copy them in your usual GameData dir

- If you compile with VS you have to generate the mono debug information. From a command line (edit the KSP path). [B]That cmd line must be run from the same dir as the dll[/B] : [code]"C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\bin\cli.bat" "C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\lib\mono\4.0\pdb2mdb.exe" MyPlugin.dll[/code]
Note that if you use Visual Studio 2015, you need a newer version of the pdb2mdb executable than is provided in the Unity 4.6.4f1 (KSP 1.0.5) installation. For that, get a recent version of Mono from [URL="http://www.mono-project.com/download/"]here [/URL]and install it somewhere. The pdb executable will be in <MonoInstallDirector>\Mono\bin .

- You will get a "MyPlugin.dll.mdb" file in next to your dll.

We are now ready on KSP side. To make your life easier you should add the copy of the dll, pdb and generation of the mdb in your project post build script since they need to be refreshed for each build.

[SIZE=3][B]Visual Studio [/B][/SIZE]
You need a version that allows plugin. So either a 201x Pro or [URL="https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx"]Visual Studio Community 2013[/URL] (it is free). You also have to install the [URL="http://unityvs.com/"]Unity VS[/URL] extension. It is free too. Get the one for your VS version and install it.
Once you have that you will have a "Attach Unity Debugger" in the Debug menu. So launch KSP and use that menu to attach VS to it. You should get an orange bar at the bottom of VS and the context should switch to debugging. You now have all the usual debugging tools (breakpoint, step by step, watches)...

[SIZE=3][B]MonoDevelop[/B][/SIZE]
In the "Run" menu you have "Attach to Process". It opens a windows where you should have "Unity Debugger" selected on the lower part and you can select your KSP process to attach to. It should switch to the debugging context

[SIZE=3][B]Profiling[/B][/SIZE]
If you have a Unity Pro license you can attach the profiler to KSP. Unfortunately you can't attach the Unity 5 profiler (which is free) to a Unity 4.x process. Unless there is an other magic trick that I did not find.

[SIZE=3][B]Linux and OSX[/B][/SIZE]
The steps are the same. The only difference is the directory where you get the executable. If you are on Linux you will just need to copy some files, so use a VM to install Unity or ask a friend.
- OSX : <Unity>/Data/PlaybackEngines/macstandalonesupport/ (here I have no idea if KSP is a universal or macosx32 binary. Try and report so I can edit)
- Linux : <Unity>/Data/PlaybackEngines/linuxstandalonesupport/linux32_withgfx_development or linux64_withgfx_development. Get the LinuxPlayer file, copy it to your KSP install and rename it KSP.x86 or KSP.x86_64 depending on the directory you got it from and make sure you set it +x

[SIZE=3][B]Missing Unity debugger for MonoDevelop (Custom install or Linux)[/B][/SIZE]
If you installed MonoDevelop yourself you most likely do not have the Unity Debugger installed.
Go in Tools->Addin Manager and install the Unity Soft Debugger addon
(I did not test those steps so I would like some feedback)[/spoiler]
Link to comment
Share on other sites

Thanks for this. Does anyone know where to grab the monodevelop Unity Soft Debugger addon for linux?

It doesn't show up in the list of available addons for me on monodevelop v4.0.12. I've added the alpha repo, still no joy. I found this but the download doesn't appear to work. I guess there is always the source code but there's got to be an easier way.

EDIT: I was using an older version of monodevelop. I've since installed the latest version from the mono projects debian repositories, and now the "attach to process" option is available. I am going to assume that the "Utilities for use with Unity" plugin includes the soft debugger and move on for now.

I run into a problem with the latest mono cecil patcher though:

[KspDevModePatcher:DEBUG:9B0D4949]  IL_0048: ldc.i4 27566
[KspDevModePatcher:DEBUG:9B0D4949]  IL_004d: call System.String A.\u000d\u0010::\u0002(System.Int32)
[KspDevModePatcher:ERROR:9B0D4949]  Exception while trying to patch assembly: Sequence contains no matching element

Full log here. Might clone the patcher repo and see if I can fix it.

Edited by Unit327
more info
Link to comment
Share on other sites

Ok so I managed to get this working on linux (mint 16). Here's how:

  1. Install mono and monodevelop from here instead of the default repositories
  2. grab the source to the unity soft debugger
  3. run bash build-mpacks.sh (you may need to edit this script to point it to /usr/lib/monodevelop/bin/mdtool.exe)
  4. open monodevelop, addins, and install MonoDevelop.Debugger.Soft.Unity_5.9.1.mpack from the file you created in the last step. If you bug me enough I can upload it for you.
  5. Use wine (I used playonlinux) to install the unity3d editor
  6. delete KSP.x86_64
  7. copy Unity/Editor/Data/PlaybackEngines/linuxstandalonesupport/Variations/linux64_withgfx_development/LinuxPlayer to KSP.x86_64
  8. run the KspDevModePatcher in KSP_Data/Managed. For some reason I had to change the source code for it to use these field names.
  9. Run KSP.x86_64
  10. Open monodevelop to your mod's solution.
  11. Run->attach to process
  12. Scroll all the way to the bottom. You will only have the gdb debugger available for most processes, but there should be one where it switches to "Unity Debugger". This is not the KSP_x86_64 process, it will be listed separately!
  13. set a breakpoint, enjoy debugging!
Link to comment
Share on other sites

First of all, this is just amazing work, proper debugging tools for KSP modding, at last! This will make my life so much easier, thanks a lot :-)

I have an issue though, when I use the development player_win.exe, I can indeed debug my mod, but it turns out FAR (Ferram Aerospace Research) stops working. The mod loads (the icon is visible in game), but everything acts as if there was no aerodynamic model at all (no drag, no lift, as if there was no atmosphere on Kerbin anymore). If I restore the original KSP.exe, it works again (even if I keep the patched DLL).

Anyone saw that issue? Any idea what is going on? Since I'm working on a mod that interacts with FAR, it would help a lot to be able to debug with FAR installed...

Link to comment
Share on other sites

You should see some message in the log about "main thread". The game block when you access some Unity call from a static constructor or a thread. This is why we have to patch the game with the patcher. it moves some static constructor calls to other places (it may degrade perf since those were in static construction so they are only init once)

FAR may do that.

Link to comment
Share on other sites

Just to say that I have been using this since a long time, it helped me a lot, especially since I started doing things such as reflection and extracting data from materials/shaders.

I'm thankful that you kept updating it and even created a patch to fix the assembly for it to work with recent KSP versions.

Link to comment
Share on other sites

  • 1 month later...
On 5/12/2015 at 3:13 AM, Unit327 said:

Install mono and monodevelop from here instead of the default repositories

For some reason installing monodevelop from there conflicts with mono-gmcs (Ubuntu). How do you compile for .NET 3.5 without gmcs?

Edited by m4v
Link to comment
Share on other sites

9 minutes ago, m4v said:

For some reason installing monodevelop from there conflicts with mono-gmcs (Ubuntu). How do you compile for .NET 3.5 without gmcs?

mcs -sdk:2 

 

Edit : you can see a full cmd line here : https://ksp.sarbian.com/jenkins/job/SmokeScreen/52/console

Edit 2 : or you can grab roslyn and use it to compile Cmd line exemple here (the /home/roslyn/csc.exe line) and binary here. That would also allow you to use C# 6 

Edited by sarbian
Link to comment
Share on other sites

FYI I was unable to run the patcher successfully on the current 1.0.5 win32 KSP, as the patcher couldn't find the ModuleAblator initializer.

I recompiled having commented out the offending line, it patched the rest ok, and KSP seems to have come up successfully

//MoveInitializerIntoAwake(asm, "ModuleAblator", "\b\b", 8, "Start");

Link to comment
Share on other sites

On 02/02/2016 at 6:35 PM, timmers_uk said:

FYI I was unable to run the patcher successfully on the current 1.0.5 win32 KSP, as the patcher couldn't find the ModuleAblator initializer.

I recompiled having commented out the offending line, it patched the rest ok, and KSP seems to have come up successfully

//MoveInitializerIntoAwake(asm, "ModuleAblator", "\b\b", 8, "Start");

I'm getting the same error, on win32.

On 03/02/2016 at 6:42 AM, sarbian said:

Are you sure you are on build 1028 of KSP ?

Yes, I'm running build 1028.

 

Edited by RockyTV
Link to comment
Share on other sites

 

On 03/02/2016 at 8:42 AM, sarbian said:

Are you sure you are on build 1028 of KSP ?

Yep I'm on build 1028, and since commenting out that line and rebuilding (the patcher), my debugging seems to be happy enough.

 

Edited by timmers_uk
Link to comment
Share on other sites

  • 3 weeks later...
On 2/8/2016 at 0:29 PM, sarbian said:

I just checked and the "\b\b" is right. I don't know why it does not work for you but if you can debug then I guess it is fine.

Yep np, this is all witchcraft anyway, so possibly a little unpredictability is expected ;)

Link to comment
Share on other sites

  • 1 month later...
25 minutes ago, Padishar said:

Huh?  I think you'll find it is...

I meant is now linked to the VS environment. Mono is there... I guess has anyone tried to get this fired up for 1.1?

Otherwise I'll start looking at it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...