In computing, compilation is the process of taking a high-level language and translating it to a lower-level language (for example, C# to Intermediate Language Assembly or machine code). Decompilation is the reverse process, where you attempt to reconstruct high-level code from lower-level code. As code in a high-level language is usually easier to understand than that in a low-level language, making changes to Terraria is easier if you decompile it first. This isn't as good as having the original code, but you will (assuming proficiency in C#) be able to see how the game works.
You will need the following:
Decompiling the Game
- Download ILSpy if you have not already done so. Place it on the desktop and extract it.
- Run ILSpy. Choose File, then Open. Navigate to your Steam's Terraria folder, which is usually located in C:\Program Files (x86)\Steam\SteamApps\Common\Terraria\ and choose a file to decompile:
- Original game (no mods): Terraria.exe
- Original game (with tConfig): TerrariaOriginalBackup.exe
- tConfig: tConfig.exe
- Regardless of what you have selected, it will generate a file tree, previewing what it has to do. After this, choose File, then Save Code. Choose a directory to save the code to.
- ILSpy will then reverse-engineer the game into a few dozen C# files in the chosen directory, and it will also conveniently generate a project file if you use Visual Studio.
- Please note: This process will take some time, so be patient! On a modern, high quality computer, it will take about 10 minutes. If your machine is a bit older, then it could take up 30 minutes or more!
- After ILSpy has finished running, you should have a number of C# files in the folder you chose.
Examining the Code
- You can now view the code with an editor of your choice.
- Because compilation loses some information such as local variable names and comments, the code may be hard to understand. For example, you will see a lot of variables called things like num6 -- that's just an artifact of compiling and decompiling.
- As the code is quite messy, here is a trick I use all the time to find something in the code:
- Use the Default Files to find an item, a Wooden Arrow for example.
- Open the .ini file and take note of the "type=40" value.
- If we wanted to see the item's behavior, search for something like "type == 40" in the Item.cs file.
- Note that certain functions may not be where you expect them to be! For example, anything that handles wires and mechanics are handled in the WorldGen class.
- Please refrain from posting or linking to any entire .cs files on the wiki or the official forums. Using a code snippets sparingly is permitted.
- This is the process of taking the raw C# code and changing it back into a working .exe file. This section is currently empty, and there are no plans to elaborate. However, if there is sufficient interest, note me on my user page, as I may expand this guide.
- When you examine the code, there may be several errors reported if you are using Visual Studio. They don't affect the usefulness of the code, but must be corrected if you are to compile the game.
Error: color.R = Main.mouseTextColor / 2;
Solution: color.R = (byte) (Main.mouseTextColor / 2);
Error: this.velocity != value2;
Solution: Delete the line!
Error: int num57 = num52 ?? -1;
Solution: int num57 = num52; if (num52 == 0) num57 = -1;
Error: int num76 = num71 ?? -1;
Solution: int num76 = num71; if (num71 == 0) num76 = -1;