03-10-2026, 11:07 AM
[Tutorial Series] Building game trainers from scratch - Part 1: Decompilation & game structure (Stardew Valley, macOS)
Description
Part 1: Decompilation & Game Structure
This is part 1 of a series where we build a fully automatic game trainer for Stardew Valley from scratch. By the end of the series, you'll have a trainer that:
What you'll need for this part:
Quick note on SMAPI: Yeah, you can install SMAPI and CJB Cheats Menu for infinite stamina. But that's a C# mod running inside the game's official modding framework. We're doing raw memory hacking, no modding framework, no game API. The trainer we build works below the game's awareness (including in multiplayer), and every technique here transfers to games that don't have modding frameworks. If you can hack Stardew at the memory level, you can hack anything.
Why Stardew Valley?
You'd think a farming game would be a simple target. It's not. Stardew Valley has properties that make memory hacking genuinely tricky, and that's exactly why it's a good learning target:
Setting up ILSpy
Since Stardew Valley is written in C#, the compiled DLL contains enough metadata to fully decompile it back to readable source code. This is a huge advantage, we can read the actual game logic instead of guessing from disassembly.
The tool we'll use is ILSpy (specifically the command-line version, ilspycmd).
Step 1: Install .NET 8 runtimeILSpy needs the .NET 8 runtime. Install it via Homebrew. If you don't have Homebrew yet, install it first from brew.sh.
Code: brew install dotnet@8 This takes a minute or two. You'll see Homebrew downloading and installing packages, when it's done you'll get back to your terminal prompt.
Step 2: Install ilspycmd Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" dotnet tool install -g ilspycmd You should see something like Tool 'ilspycmd' was successfully installed. The DOTNET_ROOT prefix tells it where to find the .NET runtime, Homebrew puts it in a non-standard location, so without this prefix the command would fail with a "runtime not found" error.
Step 3: Find the game DLLStardew Valley's game logic lives in a single DLL:
Code: /Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll You can verify it exists by running ls -la "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll", it should be about 5.9 MB. This one file contains ALL the game logic. farming, combat, NPCs, stamina, everything. Because it's a .NET assembly (not compiled native code), we can fully decompile it back to readable C#.
Step 4: Decompile the Farmer classThe player character is represented by the Farmer class. Let's decompile it:
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" \ -t StardewValley.Farmer This will print thousands of lines of C# to your terminal, that's the entire Farmer class decompiled. You might want to redirect it to a file so you can search through it more easily:
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" \ -t StardewValley.Farmer > farmer.cs Now you can open farmer.cs in any text editor. We only care about the stamina-related parts.
Tracing the stamina write chain
Now let's figure out what happens when you swing an axe and stamina decreases. We need to trace the code path from the tool action down to the actual memory write.
The high-level setter: Farmer.StaminaIf you saved to farmer.cs, search for the word Stamina with a capital S. In your terminal you can run grep -n "Stamina" farmer.cs to see all the lines where it appears, or just use Cmd+F in your text editor. You're looking for the property setter.
Note: ILSpy uses verbose formatting (braces on separate lines, this. prefixes on fields). I've cleaned up the code samples below for readability, your output will look slightly different but the logic is identical. If you see this.fieldChangeEvent where I wrote fieldChangeEvent, or extra { } braces around single-line blocks, that's just ILSpy's style.
The setter looks like this:
Code: public float Stamina { set { if (!hasBuff("statue_of_blessings_2") || !(value < stamina)) stamina = Math.Min(MaxStamina, Math.Max(value, -16f)); } } This is the stamina setter that game code calls (e.g., Tool.DoFunction() calls farmer.Stamina = newValue). It does two things:
The backing field: Farmer.stamina (lowercase)Look closely at the code above, the setter writes to stamina (lowercase s), not Stamina (uppercase S). C# treats these as different names. Search for the lowercase version and you'll find another property:
Code: public float stamina { set { netStamina.Value = value; } } This is just a wrapper that writes to netStamina. So what's netStamina?
The network wrapper: NetFloatnetStamina is declared in the Farmer class as:
Code: private readonly NetFloat netStamina = new NetFloat(270f); The 270f is the default starting stamina. NetFloat is part of the Netcode namespace, it's a subclass of NetField<float, NetFloat>, which itself extends NetFieldBase. It's a generic wrapper that makes the float value network-syncable for multiplayer. Every float in the game that needs to sync between players uses this same class.
To see how it actually writes the value, we need to decompile NetFloat separately (it's in the Netcode namespace, not StardewValley):
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" \ -t Netcode.NetFloat > netfloat.cs Open netfloat.cs and search for Set. You'll find this method (it's called whenever you assign to .Value):
Code: public override void Set(float newValue) { if (canShortcutSet()) value = newValue; // fast path - direct memory write else if (newValue != value) { cleanSet(newValue); // slow path - sync to other players MarkDirty(); } } Now we need to find canShortcutSet(). It's not in netfloat.cs, it's defined in the grandparent class NetFieldBase. To find it, we decompile the full assembly and search for it (this takes about a minute since it decompiles everything):
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" 2>/dev/null \ | grep -A 10 "protected bool canShortcutSet" You'll get:
Code: protected bool canShortcutSet() { if (Dirty && fieldChangeEvent == null) return fieldChangeVisibleEvent == null; return false; }
What this tells usThe Set() method has two paths:
Now here's the key part. That fast path, value = newValue in C#, looks simple, but what does the CPU actually execute? When the .NET JIT compiler turns that line of C# into machine code, it produces this single x86-64 instruction:
Code: MOVSS [RBX+0x4C], XMM1 If you're not familiar with assembly: MOVSS means "move scalar single-precision float." XMM1 is a CPU register holding the new stamina value. [RBX+0x4C] means "the memory address stored in the RBX register, plus 0x4C bytes", that's where the NetFloat object's value field lives. So this instruction takes the new stamina number from a CPU register and stores it into the object's memory. That's it. That one instruction is where stamina actually changes.
The full chainHere's the complete path from tool swing to memory write:
Code: Tool.DoFunction() → Farmer.Stamina { set } // clamp to [-16, MaxStamina], check buff → Farmer.stamina { set } // backing field wrapper → netStamina.Value = value // triggers NetFloat.Set() → NetFloat.Set(float) // virtual override → value = newValue // MOVSS [RBX+0x4C], XMM1 Five levels of indirection just to change one number. That's what .NET's abstraction layers look like under the hood.
Understanding NetFloat's memory layout
To hack stamina, we need to know how a NetFloat object is laid out in memory, where exactly in the object is the float stored? We know from the MOVSS instruction that the value is at offset +0x4C from the object's start. But what else is around it? From the decompiled code, we can piece together the relevant fields:
OffsetSizeFieldDescription+0x008 bytesMethodTable*Pointer to the class metadata (every .NET object has this)......(other fields)Internal NetFieldBase state+0x4C4 bytesvalueThe actual float value (stamina)+0x504 bytesdirty flag0 or 1+0x544 bytespreviousValueCopy of the last value (used for network sync)
The important pattern here is what we call the "twin pattern": at offset +0x4C, you'll see:
Code: [float F] [0 or 1] [float F] Two copies of the same float value, separated by a 4-byte dirty flag. This pattern is very distinctive, if you find two identical floats 8 bytes apart with a 0 or 1 between them, you're almost certainly looking at a NetFloat. This is going to be critical in Part 2 (and later in the trainer code) because we can scan for this pattern to find stamina automatically, without needing to manually chase pointer chains from some static address.
Why there are 900+ NetFloat objectsHere's the trap that makes naive approaches fail: NetFloat isn't just used for stamina. It's used for every networked float in the game. When a save is loaded, the game creates hundreds of NetFloat objects for all sorts of values, health multipliers, speed modifiers, fishing bar positions, buff timers, and much more.
The MOVSS [RBX+0x4C], XMM1 instruction inside NetFloat.Set() is a generic method. It's the same compiled code for ALL NetFloat writes. You can't just disable that instruction, it would freeze every networked float in the game simultaneously. We need a way to target stamina specifically.
This is the central challenge of the entire series: how do you selectively freeze one value when the write instruction is shared by 900+ others?
How .NET JIT compilation works (and why it matters)
One more concept before we move on, it explains why everything in this series has to be dynamic.
Most games you'd hack are compiled to native machine code ahead of time. The game ships as a binary, and the code is at fixed (or predictable) addresses. Stardew doesn't work that way.
Stardew Valley ships as a .NET DLL containing IL bytecode (Intermediate Language), think of it as a halfway point between source code and machine code. When you launch the game, the .NET runtime's JIT compiler (Just-In-Time) translates this IL into actual x86-64 machine code that your CPU can execute. This translation happens while the game is running, functions get compiled the first time they're called.
Why does this matter for us?
What we know so far
After decompiling the game, here's what we've learned:
In Part 2, we'll open Bit Slicer, find stamina in a live game, set a hardware watchpoint to catch the write instruction in action, and NOP it manually to see stamina freeze for the first time. That hands-on success is what motivates everything that comes after.
Upcoming posts in this series
Description
Part 1: Decompilation & Game Structure
This is part 1 of a series where we build a fully automatic game trainer for Stardew Valley from scratch. By the end of the series, you'll have a trainer that:
- Launches the game with a single command
- Automatically finds and freezes stamina when you load a save
- Shows an on-screen HUD with toggle controls
- Works in multiplayer
- Survives game updates (nothing hardcoded)
- No sudo, no external tools, no manual steps
What you'll need for this part:
- macOS (Apple Silicon or Intel)
- Stardew Valley installed
- Homebrew (brew)
- About 20 minutes
Quick note on SMAPI: Yeah, you can install SMAPI and CJB Cheats Menu for infinite stamina. But that's a C# mod running inside the game's official modding framework. We're doing raw memory hacking, no modding framework, no game API. The trainer we build works below the game's awareness (including in multiplayer), and every technique here transfers to games that don't have modding frameworks. If you can hack Stardew at the memory level, you can hack anything.
Why Stardew Valley?
You'd think a farming game would be a simple target. It's not. Stardew Valley has properties that make memory hacking genuinely tricky, and that's exactly why it's a good learning target:
- JIT-compiled C# - The game ships as a .NET DLL, not native machine code. The .NET runtime compiles C# into x86-64 machine code at runtime (Just-In-Time compilation). Every time you launch, code lives at different addresses. Nothing is static.
- Generic shared methods - Stamina is stored in a NetFloat, which is the same class used for hundreds of other game values. The instruction that writes stamina is the EXACT same instruction that writes health modifiers, speed buffs, fishing bar positions, everything. You can't just disable it.
- Garbage collector - .NET moves objects around in memory whenever it wants (garbage collection). A pointer you found 5 minutes ago can suddenly be invalid.
- Networked data structures - Even in single-player, the game uses multiplayer-ready wrappers (NetFloat, NetInt) around every value. Extra indirection everywhere.
- Rosetta 2 (Apple Silicon only) - On M1/M2/M3/M4 Macs, the game runs through Apple's x86-64 to ARM64 translation layer. This creates problems that don't exist on Intel or Windows. We'll dig into this in Part 3.
Setting up ILSpy
Since Stardew Valley is written in C#, the compiled DLL contains enough metadata to fully decompile it back to readable source code. This is a huge advantage, we can read the actual game logic instead of guessing from disassembly.
The tool we'll use is ILSpy (specifically the command-line version, ilspycmd).
Step 1: Install .NET 8 runtimeILSpy needs the .NET 8 runtime. Install it via Homebrew. If you don't have Homebrew yet, install it first from brew.sh.
Code: brew install dotnet@8 This takes a minute or two. You'll see Homebrew downloading and installing packages, when it's done you'll get back to your terminal prompt.
Step 2: Install ilspycmd Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" dotnet tool install -g ilspycmd You should see something like Tool 'ilspycmd' was successfully installed. The DOTNET_ROOT prefix tells it where to find the .NET runtime, Homebrew puts it in a non-standard location, so without this prefix the command would fail with a "runtime not found" error.
Step 3: Find the game DLLStardew Valley's game logic lives in a single DLL:
Code: /Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll You can verify it exists by running ls -la "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll", it should be about 5.9 MB. This one file contains ALL the game logic. farming, combat, NPCs, stamina, everything. Because it's a .NET assembly (not compiled native code), we can fully decompile it back to readable C#.
Step 4: Decompile the Farmer classThe player character is represented by the Farmer class. Let's decompile it:
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" \ -t StardewValley.Farmer This will print thousands of lines of C# to your terminal, that's the entire Farmer class decompiled. You might want to redirect it to a file so you can search through it more easily:
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" \ -t StardewValley.Farmer > farmer.cs Now you can open farmer.cs in any text editor. We only care about the stamina-related parts.
Tracing the stamina write chain
Now let's figure out what happens when you swing an axe and stamina decreases. We need to trace the code path from the tool action down to the actual memory write.
The high-level setter: Farmer.StaminaIf you saved to farmer.cs, search for the word Stamina with a capital S. In your terminal you can run grep -n "Stamina" farmer.cs to see all the lines where it appears, or just use Cmd+F in your text editor. You're looking for the property setter.
Note: ILSpy uses verbose formatting (braces on separate lines, this. prefixes on fields). I've cleaned up the code samples below for readability, your output will look slightly different but the logic is identical. If you see this.fieldChangeEvent where I wrote fieldChangeEvent, or extra { } braces around single-line blocks, that's just ILSpy's style.
The setter looks like this:
Code: public float Stamina { set { if (!hasBuff("statue_of_blessings_2") || !(value < stamina)) stamina = Math.Min(MaxStamina, Math.Max(value, -16f)); } } This is the stamina setter that game code calls (e.g., Tool.DoFunction() calls farmer.Stamina = newValue). It does two things:
- Buff check - If the player has the "statue of blessings" buff AND the new value is less than current stamina, the write is blocked entirely (stamina can't decrease while blessed)
- Clamping - The value is clamped between -16 and MaxStamina (default 270). Yes, stamina can go negative, that's the exhaustion mechanic.
The backing field: Farmer.stamina (lowercase)Look closely at the code above, the setter writes to stamina (lowercase s), not Stamina (uppercase S). C# treats these as different names. Search for the lowercase version and you'll find another property:
Code: public float stamina { set { netStamina.Value = value; } } This is just a wrapper that writes to netStamina. So what's netStamina?
The network wrapper: NetFloatnetStamina is declared in the Farmer class as:
Code: private readonly NetFloat netStamina = new NetFloat(270f); The 270f is the default starting stamina. NetFloat is part of the Netcode namespace, it's a subclass of NetField<float, NetFloat>, which itself extends NetFieldBase. It's a generic wrapper that makes the float value network-syncable for multiplayer. Every float in the game that needs to sync between players uses this same class.
To see how it actually writes the value, we need to decompile NetFloat separately (it's in the Netcode namespace, not StardewValley):
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" \ -t Netcode.NetFloat > netfloat.cs Open netfloat.cs and search for Set. You'll find this method (it's called whenever you assign to .Value):
Code: public override void Set(float newValue) { if (canShortcutSet()) value = newValue; // fast path - direct memory write else if (newValue != value) { cleanSet(newValue); // slow path - sync to other players MarkDirty(); } } Now we need to find canShortcutSet(). It's not in netfloat.cs, it's defined in the grandparent class NetFieldBase. To find it, we decompile the full assembly and search for it (this takes about a minute since it decompiles everything):
Code: DOTNET_ROOT="$(brew --prefix dotnet@8)/libexec" ilspycmd \ "/Applications/Stardew Valley.app/Contents/MacOS/Stardew Valley.dll" 2>/dev/null \ | grep -A 10 "protected bool canShortcutSet" You'll get:
Code: protected bool canShortcutSet() { if (Dirty && fieldChangeEvent == null) return fieldChangeVisibleEvent == null; return false; }
What this tells usThe Set() method has two paths:
- Fast path (canShortcutSet() returns true): Just write the value directly to memory. No network sync, no events, just a raw store. This kicks in when the field is already "dirty" (modified since last sync) AND nobody is listening for change events. In single-player, fields become dirty right after the save loads and there are no multiplayer event listeners, so the fast path is always used in single-player.
- Slow path: Write the value AND tell other players about it. Used in multiplayer when the field hasn't been modified yet or when something is listening for changes.
Now here's the key part. That fast path, value = newValue in C#, looks simple, but what does the CPU actually execute? When the .NET JIT compiler turns that line of C# into machine code, it produces this single x86-64 instruction:
Code: MOVSS [RBX+0x4C], XMM1 If you're not familiar with assembly: MOVSS means "move scalar single-precision float." XMM1 is a CPU register holding the new stamina value. [RBX+0x4C] means "the memory address stored in the RBX register, plus 0x4C bytes", that's where the NetFloat object's value field lives. So this instruction takes the new stamina number from a CPU register and stores it into the object's memory. That's it. That one instruction is where stamina actually changes.
The full chainHere's the complete path from tool swing to memory write:
Code: Tool.DoFunction() → Farmer.Stamina { set } // clamp to [-16, MaxStamina], check buff → Farmer.stamina { set } // backing field wrapper → netStamina.Value = value // triggers NetFloat.Set() → NetFloat.Set(float) // virtual override → value = newValue // MOVSS [RBX+0x4C], XMM1 Five levels of indirection just to change one number. That's what .NET's abstraction layers look like under the hood.
Understanding NetFloat's memory layout
To hack stamina, we need to know how a NetFloat object is laid out in memory, where exactly in the object is the float stored? We know from the MOVSS instruction that the value is at offset +0x4C from the object's start. But what else is around it? From the decompiled code, we can piece together the relevant fields:
OffsetSizeFieldDescription+0x008 bytesMethodTable*Pointer to the class metadata (every .NET object has this)......(other fields)Internal NetFieldBase state+0x4C4 bytesvalueThe actual float value (stamina)+0x504 bytesdirty flag0 or 1+0x544 bytespreviousValueCopy of the last value (used for network sync)
The important pattern here is what we call the "twin pattern": at offset +0x4C, you'll see:
Code: [float F] [0 or 1] [float F] Two copies of the same float value, separated by a 4-byte dirty flag. This pattern is very distinctive, if you find two identical floats 8 bytes apart with a 0 or 1 between them, you're almost certainly looking at a NetFloat. This is going to be critical in Part 2 (and later in the trainer code) because we can scan for this pattern to find stamina automatically, without needing to manually chase pointer chains from some static address.
Why there are 900+ NetFloat objectsHere's the trap that makes naive approaches fail: NetFloat isn't just used for stamina. It's used for every networked float in the game. When a save is loaded, the game creates hundreds of NetFloat objects for all sorts of values, health multipliers, speed modifiers, fishing bar positions, buff timers, and much more.
The MOVSS [RBX+0x4C], XMM1 instruction inside NetFloat.Set() is a generic method. It's the same compiled code for ALL NetFloat writes. You can't just disable that instruction, it would freeze every networked float in the game simultaneously. We need a way to target stamina specifically.
This is the central challenge of the entire series: how do you selectively freeze one value when the write instruction is shared by 900+ others?
How .NET JIT compilation works (and why it matters)
One more concept before we move on, it explains why everything in this series has to be dynamic.
Most games you'd hack are compiled to native machine code ahead of time. The game ships as a binary, and the code is at fixed (or predictable) addresses. Stardew doesn't work that way.
Stardew Valley ships as a .NET DLL containing IL bytecode (Intermediate Language), think of it as a halfway point between source code and machine code. When you launch the game, the .NET runtime's JIT compiler (Just-In-Time) translates this IL into actual x86-64 machine code that your CPU can execute. This translation happens while the game is running, functions get compiled the first time they're called.
Why does this matter for us?
- No fixed addresses - The JIT compiles code to whatever memory address happens to be available. NetFloat.Set() is at a different address every time you launch the game. If we hardcode an address, it'll be wrong next session.
- Multiple copies of the same function - The JIT uses "tiered compilation." It first compiles a quick-and-dirty version of a function, then later recompiles it with full optimization once it sees the function is being called a lot. So there can be 2, 3, even 6 copies of the same function in memory at once. We'll see this firsthand in Part 2 when we look at the compiled code in memory.
- Code pages are writable - The JIT needs to write new machine code while the game is running (for those recompilations). So the memory pages where code lives are marked as read-write-execute (RWX). This is actually good news for us, on many games, code pages are read-only and you need kernel tricks to modify them. Here, we get write access for free.
- On Apple Silicon: Rosetta 2 - Here's where it gets really interesting. The JIT generates x86-64 code, but M1/M2/M3/M4 CPUs are ARM64. Apple's Rosetta 2 takes the JIT's x86-64 output and translates it AGAIN into ARM64. So there are two translation layers: C# → x86-64 (by JIT) → ARM64 (by Rosetta). This second layer has its own caching behavior and write protection that creates problems you won't find on Intel Macs or Windows. We'll deal with this in Part 3.
What we know so far
After decompiling the game, here's what we've learned:
- Stamina is stored in a NetFloat object at field offset +0x4C
- The write instruction is MOVSS [RBX+0x4C], XMM1 - shared by ALL NetFloat objects
- NetFloat has a twin pattern: [value] [dirty flag] [previousValue] - we can scan for this
- The MethodTable pointer at offset +0x00 identifies which class an object belongs to - we can use this to filter
- All addresses are dynamic (JIT compilation) - nothing can be hardcoded
- The .NET GC can move objects around - pointers can become invalid
In Part 2, we'll open Bit Slicer, find stamina in a live game, set a hardware watchpoint to catch the write instruction in action, and NOP it manually to see stamina freeze for the first time. That hands-on success is what motivates everything that comes after.
Upcoming posts in this series
- Part 1: Decompilation & game structure (this post)
- Part 2: Memory scanning with Bit Slicer (next)
- Part 3: Rosetta 2 & failed approaches
- Part 4: Writing the trainer (DYLD injection)
- Part 5: Adding an on-screen HUD
- Part 6: Lessons learned & what's next

