Making a Hacked Client
Hey guys! Since I won't be doing anything productive with my time, I decided to make a tutorial on how to make your own Terraria hacked client :) So, let's get started.
**HIGHLY RECOMMENDED**: Some basic knowledge of C#. If you end up copy and pasting the code, you won't learn anything new.
Visual Studio: This is the IDE that I will be using in this tutorial. While others exist, this is what I recommend for .NET code.
A copy of the decompiled Terraria: You can try to decompile Terraria by yourself using various tools such as ILSpy or dotPeek. But for the sake of simplicity, we will be using a pre-existing source by MPGH user Xenoxiluna found here (Thank you!!!)
1. So after downloading the decompiled source, make a new folder and extract the files from the source into the folder.
2. Then, open up Visual Studio (or the IDE of your choice) and press File>Open Project/Solution> and select the Terraria.csproj.
2a. I highly recommend that you poke around the source code and see how things function.
3. After a while, Visual Studio (refered to as VS from now on) should display a list of folders to the right. Let's create our own "hack" class for better organization. Right click the Terraria project, and press Add>New Class. I'll call our new class Hacks.cs but you can call it whatever you want.
4. Navigate to Terraria>SocialAPI and comment out everything in the LoadSteam() method. This lets us run our client directly without connecting to steam.
5. Now try to build and run your project. If you get an error with something along the lines of "cannot locate /.xnb", download the Content folder attached and move it to your "FolderName"/bin/Debug.
6. And now we can start the fun part of the tutorial, making hacks!
Before we actually begin, we have to have a way for our client to communicate with the hacks. For this, we'll have our client listen to all messages we send, and if it begins with a "~", we'll stop it from going to the server and interpret it. To do this, we'll add a "hook" to our chat.
Using the solution explorer to the right, find the "main" class of Terraria. It should be in Terraria>Main.cs. Now that we have the main class open, let's find out how the client handles chat messages. I encourage you to find it for yourself, but if you're lazy, the line number is about 11844 in the main class. It should look something like this:
The "NetMessage.SendData" sends our message to the server, so we want to make sure to not do that when our client detects that it is a hack message. To actually hook our class into the chat, let's go back to our chat class and make a new method called onChat. The signature should be "public static bool onChat(string message)", returning true if we should send the message to our server, and false if we shouldn't. Now, we edit the code in the main class to make it:
Back in our hack class, our onChat method should look something like this:
Now we have a method with communicating with our hacks, let's get started with the fun.
Let's make a dictionary for our hacks that stores the state of the hack. Add this to the top of your Hacks class.
To handle the toggling of these hacks, let's make another method within our Hacks class called "Toggle" that recieves a string of the Hack name, toggles it to the other state, and echos a message to our client.
Now that we have that, we can go back to our onChat method and implement the toggle method like so:
Now that we can control the state of our hacks, let's have them impact our game.
So again, I highly recommend that you look through the code and try to find these methods by yourself, but if you're lazy, read on! Let's first implement our godmode hack.
Find the method that controls when your player takes damage. In Terraria>Player, you should find a method that looks like this:
Bingo! This controls how much damage our player takes. To not take any damage, we can just make it return 0.0. Modify the code a little, and we have ourselves a working godmode hack! Final code:
Okay, I am going to complicate things a bit in order to make it easier to add more hacks in the future. Let's find our player's Update() method, which is frequently called and add our own hook onto it. At around lines 15652 in the Terraria>Player.cs class, you should find something that looks like this:
Let's create a method in our Hacks class called onUpdate() and call it whenever the player Update() is called.
This will be a bit short in order to guage interest in these tutorials. If you want to see more, leave a comment with suggestions or feedback, thanks!
Set up
**HIGHLY RECOMMENDED**: Some basic knowledge of C#. If you end up copy and pasting the code, you won't learn anything new.
Visual Studio: This is the IDE that I will be using in this tutorial. While others exist, this is what I recommend for .NET code.
A copy of the decompiled Terraria: You can try to decompile Terraria by yourself using various tools such as ILSpy or dotPeek. But for the sake of simplicity, we will be using a pre-existing source by MPGH user Xenoxiluna found here (Thank you!!!)
Preparing the client
1. So after downloading the decompiled source, make a new folder and extract the files from the source into the folder.
2. Then, open up Visual Studio (or the IDE of your choice) and press File>Open Project/Solution> and select the Terraria.csproj.
2a. I highly recommend that you poke around the source code and see how things function.
3. After a while, Visual Studio (refered to as VS from now on) should display a list of folders to the right. Let's create our own "hack" class for better organization. Right click the Terraria project, and press Add>New Class. I'll call our new class Hacks.cs but you can call it whatever you want.
4. Navigate to Terraria>SocialAPI and comment out everything in the LoadSteam() method. This lets us run our client directly without connecting to steam.
5. Now try to build and run your project. If you get an error with something along the lines of "cannot locate /.xnb", download the Content folder attached and move it to your "FolderName"/bin/Debug.
6. And now we can start the fun part of the tutorial, making hacks!
Basic toggle hacks
Before we actually begin, we have to have a way for our client to communicate with the hacks. For this, we'll have our client listen to all messages we send, and if it begins with a "~", we'll stop it from going to the server and interpret it. To do this, we'll add a "hook" to our chat.
Hooking the chat
Using the solution explorer to the right, find the "main" class of Terraria. It should be in Terraria>Main.cs. Now that we have the main class open, let's find out how the client handles chat messages. I encourage you to find it for yourself, but if you're lazy, the line number is about 11844 in the main class. It should look something like this:
Code:
if (Main.inputTextEnter && Main.chatRelease)
{
if (Main.chatText != "")
{
NetMessage.SendData(25, -1, -1, Main.chatText, Main.myPlayer, 0f, 0f, 0f, 0, 0, 0);
}
...
Code:
if (Main.inputTextEnter && Main.chatRelease)
{
if (Main.chatText != "" && Hacks.onChat(Main.chatText))
{
NetMessage.SendData(25, -1, -1, Main.chatText, Main.myPlayer, 0f, 0f, 0f, 0, 0, 0);
}
...
Code:
public static bool onChat(string message)
{
if (message.StartsWith("~"))
{
if (message.StartsWith("~god"))
{
//In the next section!
}
else if (message.StartsWith("~infmana"))
{
//Are you excited?
}
//You can keep chaining these else if's for future code
return false; //Message should NOT go to the server, so we return false.
}
return true; //Message does NOT start with ~, so it is okay to send it to the server.
}
Toggling hacks on and off
Let's make a dictionary for our hacks that stores the state of the hack. Add this to the top of your Hacks class.
Code:
public static Dictionary<String, bool> HackStates = new Dictionary<String, bool>()
{
{"Godmode", false},
{"InfiniteMana", false }
};
Code:
public static void Toggle(String hack)
{
//Toggles the hack
HackStates[hack] = !HackStates[hack];
//Writes a new message to the text box of our client, string represents the message, and the number represents the RGB value of the color. Only you can see this message.
Main.NewText("[MPGH Tutorial Client] Set the hack state of " + hack + " to "HackStates[hack], 255, 0, 0);
}
Code:
if (message.StartsWith("~"))
{
if (message.StartsWith("~god"))
{
Toggle("Godmode");
}
else if (message.StartsWith("~infmana"))
{
Toggle("InfiniteMana");
}
...
}
Putting the hacks in action!
So again, I highly recommend that you look through the code and try to find these methods by yourself, but if you're lazy, read on! Let's first implement our godmode hack.
Godmode
Find the method that controls when your player takes damage. In Terraria>Player, you should find a method that looks like this:
Code:
public double Hurt(int Damage, int hitDirection, bool pvp = false, bool quiet = false, string deathText = " was slain...", bool Crit = false, int cooldownCounter = -1)
{
bool flag = !this.immune;
if (cooldownCounter == 0)
{
flag = (this.hurtCooldowns[cooldownCounter] <= 0);
}
...
}
Code:
public double Hurt(int Damage, int hitDirection, bool pvp = false, bool quiet = false, string deathText = " was slain...", bool Crit = false, int cooldownCounter = -1)
{
//HACK: Godmode hack
if (Hacks.HackStates["Godmode"]) //If our godmode is on, return 0 damage
{
return 0.0;
}
bool flag = !this.immune;
if (cooldownCounter == 0)
{
flag = (this.hurtCooldowns[cooldownCounter] <= 0);
}
...
}
Infinite Mana
Okay, I am going to complicate things a bit in order to make it easier to add more hacks in the future. Let's find our player's Update() method, which is frequently called and add our own hook onto it. At around lines 15652 in the Terraria>Player.cs class, you should find something that looks like this:
Code:
public void Update(int i)
{
if (this.launcherWait > 0)
{
this.launcherWait--;
}
...
Code:
//In player.cs
public void Update(int i)
{
Hacks.onUpdate()
if (this.launcherWait > 0)
{
this.launcherWait--;
}
...
}
//In Hacks.cs
public static void onUpdate()
{
if (HackStates["InfiniteMana"])
{
Main.player[Main.myPlayer].statMana = Main.player[Main.myPlayer].statManaMax; //Set our current mana to our max mana
}
}
This will be a bit short in order to guage interest in these tutorials. If you want to see more, leave a comment with suggestions or feedback, thanks!
