Reverse-Engineer the game & debugging

From Torch Wiki
Jump to navigation Jump to search

Currently there is no current copy of the games code publicly available. So you can only resort to decompiling the games dlls to access the code and find out how different Methods work. However keep in mind that the Keen Software House EULA applies.

https://www.spaceengineersgame.com/eula.html (Last updated on July 3rd, 2019)

For decompiling there are two results that pop up in google when looking for it.

In both you can basically load the games dll files and look up the classes you need to see.

The decompiled source code of dotPeek is easier to reed. But dnSpy has other advantaged which will come in handy in the next tutorial.

But what tool you use is completely up to you. There are even some with an Visual Studio integration.

How to debug your code

Since you cannot really test any runtime relevant code with just your plugin you have to get a bit more creative.

You could either spent some time downloading torches source code, alter a bit so that it loads your plugin without being in the zip file so you can use Visual Studio for debugging... Which Honestly is a bit annoying to set up.

Or you use the neat little Tool called dnSpy https://github.com/0xd4d/dnSpy/releases

dnSpy allows you to either to attach to running torch instances, or start torch directly through it. Which allows you to decompile its code, set breakpoints, break on exceptions and what not.

How to set it up

Basically you just download dnSpy from the source listed above, unpack it and you are good to go.

Depending on how you usually run your torch server it may be helpful to run it with administrator privileges as you have access to more information then.

Once you start it (and in my case set the theme to light) you can launch Torch.

Start Debugging

Either Press F5 to launch a software using dnSpy, or ctrl + alt + P to attach to a running process. You can also debug your productive servers that way, however stopping debugging will kill the application that has been debugged. So you better don't want to do that. Also the simulation speed will be much worse while debugging. So keep that in mind.

We assume you start torch for debugging. so you hit F5 (or use) the Menubar Debug -> Start Debugging and get a window that looks a little something like this:

DnSpy assembly selection.png

Of course your language setting may be different.

Just select your torch.server.exe and hit OK.

Setting Breakpoints

Once the server runs you can open the Module Explorer (Ctrl + Alt + U) and look for our Plugin.

DnSpy module explorer.png

There we see our plugins dll loaded.

Double click it and you can explore it using the Assembly Explorer (Ctrl + Alt + L)

DnSpy assembly explorer.png

As an example we open the TestCommands.cs and next to the line numbers we can set a breakpoint via simple click.

DnSpy breakpoints.png

Here you also can see how our decompiled sourcecode looks. Quite similar to what we created before. But they added some tokes, altered enumerations and added default parameters we did not set.

Analysing your Plugin

So lets just enter the command we just set a breakpoint to. You will notice it automatically opens the file where the breakpoint is set. And you can inspect your code now.

  • You can use Local-Variables (Alt + 4)
  • Look at the Stacktrace (Ctrl + Alt + C)
  • Check out your Threads (Ctrl + Alt + H)

You can open all of these windows manually also and arrange them as you need. dnSpy will save your view layout so you only need to do that once.

DnSpy debug lokals.png

DnSpy debug stacktrace.png

Once you are done inspecting you can just continue using (F5) or go step by step through your Program.

These buttons will help you:

DnSpy debug step by step.png

Exception Settings

If your Server dies due to a fatal exception dnSpy will tell you which it was. And also has a possability for you to set up breakpoints when exceptions are thrown.

For that the Exception Tab is used. (Ctrl + Alt + E)

There you can select on which exceptions your Server should halt, so you can look into things.

DnSpy debug exceptions.png

However: The game throws a ton of exceptions to even start up. So it takes a bit fine tuning to find the right exceptions to stop to. If you use your own exceptions in your Plugin it makes it a bit easier.