UnofficialCrusaderPatch / UnofficialCrusaderPatch2

Unofficial balancing patch installer for Stronghold Crusader 1
MIT License
429 stars 59 forks source link

How do you know how/where to make changes to the exe? #270

Open molchknolle opened 5 years ago

molchknolle commented 5 years ago

Thank you so much for developing this, i've been waiting for something like it ever since the game came out!

I'm new to github and don't really know the etiquette, so maybe it's an unusual question, but could you explain how you actually do the editing of the exe? I mean, i've tried it before myself and managed - with lots of patience and dumb luck - to find and change the health of some units, but how did you manage to find out all the adresses in the exe of the things changed in the patch - for example siege engine build behaviour, building costs and so on...? Is it really just trial and error? Or do you have the exe somehow mapped out, or maybe use some specific program that you could recommend for this?

I'm asking because I would like to fork and maybe just experiment a bit for myself but so far, looking at your code, I would't have a clue what to write into the codeblox files.

ngc92 commented 5 years ago

It would be really cool if there were a general introduction for how to contribute to this project, maybe in the wiki. I have some old data (will make a seperate issue) from my own tries, but do not know how to integrate this.

A program that can be used for making changes to SHC is Cheat Engine It allows to scan a process's memory for values and changes. For example, you place a unit in the editor. Scan for changes. Let it be attacked, scan for changes again, and repeat until only one address remains, which is the unit's current health. Then you let Cheat Engine scan for instructions that modify this value, to get you the assembly where the damage is dealt. Then you can follow the instructions back to where the damage value is loaded, and find out where the unit damage values are stored (in memory). Finally, you need to translate that address to a corresponding location in the exe.

In this way at least values that are somewhat accessible directly in the game can be found. I have no idea how to go about the more indirect stuff like AI behaviour, so that would interest me very much too.

Sh0wdown commented 5 years ago

I made a video some months ago when someone asked me how to edit the cost of horse archers, maybe it will help as a concrete example. https://youtu.be/oGZihLPdYiM

Applying edits in the UCP works as follows:

In the Version.cs you can add "Changes" which represent the topmost checkboxes in the installer. They can include multiple "Headers" which represent the sub-checkboxes. In them you can add "BinaryEdits". A BinaryEdit needs a "CodeBlock" which is just a text file with an array of bytes which is searched after to find the starting address to to edits in the binary. This is implemented because the addresses vary between the crusader versions. In the BinaryEdit you can now add different "BinElements" which include stuff like arrays of bytes to write.

So as an example:

new Change("ai_attackwave", ChangeType.AILords) // new major option, first arg is a title identificator to display in the UI (Localization)
{
   new DefaultHeader("ai_attackwave") // again, the argument is just to display text in the menu
   {
      // 4D31CD
      new BinaryEdit("ai_attackwave_lord") // the argument is the codeblock file which is seeked in the binary. It should return the (virtual) address commented above
      {
         new BinBytes(0x90, 0x90), // this writes the bytes 0x90, 0x90 add the address found from the codeblock
      },
   },
},
GRhin commented 5 years ago

@Sh0wdown are you able to make your ida breakdown available? I notice in your video alot of sections have already been labeled, dont want to reinvent the wheel as it were,