Before we make a technical addition to our mod, we have to determine HOW and with what tools do we do it.
As of today, original PVZ release has no modding support implemented, and something tells me that it never will.
That figures, since the game is almost 20 years old. Due to that inconvenience, you'll have to go the hard way - you'll have to modify the executable by hand, which is completely legal, as far as I am aware.
It's not hard, but beforehand, you'll have to keep in mind a few conceprs.
You may know that computers treat every piece of information as ones and zeros, but you may have never actually think this phrase through.
The lowest unit of information is called a bit, it can hold values 0 and 1, thus two states in total, which may represent a basic logical true or false. Collection of 8 bits is called a byte, the second lowest unit with 256 states.
2 bytes are called a word, with 65536 possible states, 4 bytes are a standart integer (double word), with over 4 million states, and 8 bytes, or 64 bits, with 1.844674407371E+19 exact states, which is pretty big, so noone uses this.
Every number, value, string, address, programm and et cetera is represented in computer memory as a sequence of binary numbers (or hexadecimal codes if you shorten them) which, depending on the context, are interpreted in different
ways.
The games that you play and utilities that you run are no exception - they are executable files, which means that information stored them is executed as instructions by the CPU. Again, these instructions are just consequent
bytes that cannot be well understood by man's mind, as opposed to machine's. Here in handy come programming languages, which abstract away the instructions into strings of comprehensible words, that then are compiled into an executable by an algorithm.
That's how all of your favorite video games are made.
Now, if we have to plainly read and edit an executable in order to modify it's contents, we'll have to work with lowest level programming language, assembly, which barely has any abstractions, just readable instruction names.
Because of that, reading the game's code in assembly doesn't actually require any decompilation, you'll just a basic hex editor, which I'll explain later, and a little bit of patience.
Before you begin modifying the PVZ executable, let's have a quick rundown on how computers work.
Your PC is basically just a small calculator called CPU incased in the system block with other pieces of hardware such as non-volatile disk memory, RAM, GPU, monitor, keyboard, mouse, BIOS/UEFI and etc.
CPU is the most crucial part for handling and executing software, because it can perform memory managment and basic arithmetic operations and, basically, everything. So the CPU is the heart and the brain of your computer, okay?
CPU has a number of multi-purpose registers, 64 bit cells of memory right next to the CPU, using which data needs to be transferred. The registers are: -AX, the accumulator, -CX, the counter, -DX, the data holder, -BX,
the array base pointer. The first character in place of the '-' indicates the capacity of the register:
RAX - full 64 bit,
EAX - half 32 bit,
AX - quarter 16 bit,
AL and AH - 1/8th 8 bit, the lowest and the highest byte respectively.
There is also -SP and -BP registers, which point to the stack, which will be explained later.
Using CPU instructions you will copy data to and from the registers, perform math with them, et cetera.
Now, with this out of the way, let's explain the executable's structure. I am assuming you're on the x86-64 machine, if you not, then this tutorial will be pretty much non applicable for you.
The executable is divided in several sectors: .data, .text, .rodata, .bss and stack. Data is where the variables are stored; Text is where the executed code is stored; Rodata is where the constants are stored; Bss is for variables the value of which is unknown at initialisation (this is why it's called
the Bull Shit Sector); Stack is a data structure that support first in, last out, meaning you can stack values on it like a pyramid.
Each sector has it's own restrictions, for example you can't overwrite the data in the .rodata, or you can't treat stuff from .text like data, nor you can execute the .bss sector. Everything follows it's order.
Now, to the assembler, and by that I mean a few basic instructions that you should know.
MOV REG, X - Move value X to the register REG
ADD REG, X - Add value X to the register REG
SUB REG, X - Substract value X from the register REG
MUL REG - Multiply the register REG by a value from -AX register
DIV REG - Divide AX by REG, put the result in AL and the remainder in AH
INC REG - increment REG by one
DEC REG - decrement REG by one
PUSH REG - push REG on top of the stack
POP REG - retrieve the top of the stack to the REG
JUMP X - Go to address X (a label)
CALL X - Go to address X, return later
ENTER - Create a stack frame
LEAVE - Delete a stack frame
RET - Return
This is the basis that you will need to know before modding any executable, such as PVZ. Now, let's get to the modding itself.
A hex editor is an utility that lets you view the code of the executable as assembly code and do all the crazy things I described earlier. Some editors work in real time, like Cheat Engine and Olly Dbg,
some don't, like HxD.
Here is a list of the software that qualifies as a hex editor. All of it's is freeware or shareware, so you can download it for free from the official websites:
