Advanced Prerequisites - tModLoader/tModLoader GitHub Wiki

Advanced Prerequisites

Basically all the previous prerequisites. See Beginner and Intermediate prerequisite guides first.

Advanced IDE Capabilities

If you haven't already, now is the time to explore the capabilities of your IDE. Please see the Why Use an IDE guide to familiarize yourself with everything your IDE can do to improve your mod making productivity. Of particular note, make sure you are fully taking advantage of IntelliSense and that you know how to use the Debugger.

tModLoader Source code

You may need to decompile tModLoader to understand the correct way to do something, or to figure out the recommended approach to what you are doing. Anyone attempting to do Advanced level guides probably needs their own copy of decompiled tModLoader.dll.

But wait, I found the code on Google!

No. That code is bad and will not work. It has tens of thousands of decompilation errors. Just decompile it yourself, it is easy. Read on.

Simple Way

A simpler way is to download a recent release of ILspy (These instructions last tested with ILSpy 6.2.1), click "Assets", and download the zip with "binaries" in its name, unzip the it to a folder, launch ILSpy.exe. (If you are on Linux or Mac, you will need to use AvaloniaILSpy instead of ILSpy. The following instructions should be mostly the same.)

Then, to make adaption easier, go into View->Options->Decompiler tab, scroll down and enable "Always qualify member references", and press "Ok"; This will make it so instead of just width = 20; it will say base.width = 20;, making adjusting the code easier.

Then use File->Open to open tModLoader.dll from the install directory. In addition, you should also use File->Open to open FNA.dll, found in [Install Directory]\Libraries\FNA\1.0.0\FNA.dll.

Finally make sure tModLoader is selected on the navigation pane and then press the save code button and navigate to a convenient empty folder (You'll want to choose a folder that you can easily find later, I suggest making a folder in the Saves directory: C:\Documents\My Games\Terraria\tModLoader\DecompiledTModLoader). This will take a minute or so depending on your computer. The folder that you saved the code to now has a tModLoader.csproj file, opening this file should open Visual Studio if you have it installed (highly recommended). If you do not have Visual Studio installed, you'll have to navigate the files individually through file explorer.

There are a few drawbacks to the simple way. The biggest drawback is you can't debug tModLoader itself. This is an advanced technique, but if you wish to do this later, come back to these instructions and read the Hard Way below. The other drawback is you might find the code to have decompilation errors. With the latest version of ILspy, these are very limited and easily fixed.

Simple Way, Adaption Guide Extra Steps

If you are following the Advanced Vanilla Code Adaption guide, you may want to do one additional step before exporting the code. The default behavior for ILSpy is to omit qualifying member references. What this means is that code you find in Projectile.cs, for example, would read as if (type == 601) rather than if (this.type == 601). The second approach greatly simplifies adapting vanilla code, as it allows using a simple Find/Replace operation to convert the code into code suitable for pasting into our ModProjectile class. Without this approach, the code is very difficult to fix. To enable this second approach, go to View->Options->Decompiler and scroll down to the Other section. Find Always qualify member reference values, make sure it is checked, then click OK. From now on ILSpy will output code with this extra text to greatly simplify adaption.

Hard Way

The best way to get the source code is to download the tModLoader repository and go through the setup instructions. This will net you a completely functional solution that you can open in Visual Studio. This method will take more time and it's not usually required unless you need to debug Terraria code itself or contribute to tModLoader directly.