[Help] Writing patches for MCP

Post » Mon May 07, 2012 8:15 pm

I was wondering how to write a "patch" for the Morrowind Code Patch framework. As far as I undersand it, it is a python script that (un-)applies patches which themselves are binaries (diffs?).

The reason why I'm asking is that I would like to implement two features:
  • spellcost reduction during spellmaking
  • weighted magnitude for spells depending on skill level
My english isn't perfect, sorry about that.
User avatar
Sarah MacLeod
 
Posts: 3422
Joined: Tue Nov 07, 2006 1:39 am

Post » Mon May 07, 2012 12:13 pm

The patch file is a bunch of diffs: { int32 patchid, fileoffset, length; byte data[length] }; Patchid corresponds to the id numbers in describe.txt, and each patchid can appear in multiple diffs as required.

About half the patches rewrite functions in-place, while others need to call additional code, this code is placed in the free space at the end of code segments. Currently there is space to add code at address 0x745C60. All added code should be assigned to patchid 0 to reduce diff fragmentation. For the same reason you should pad / align functions with 0xCC.
User avatar
Reven Lord
 
Posts: 3452
Joined: Mon May 21, 2007 9:56 pm

Post » Tue May 08, 2012 1:52 am

Thanks for your reply! The way I'v asked my question seemed to imply that I know what I was going to do - unfortunately I don't. I have an idea about how code injection works, but I've never done something like this before. What tools do you use to write your patches? The Readme states that Tp21 already traced some information about how the game handles data structures. Is this information publicly available somehow?

For example the process of spell making. My guess is, what your area of effect cost re-calculation is doing is to you redirect the original function call to a customized version of your own. In this new function you find the values you need in the memory where the engine normally puts those actor values and use them to calculate the cost of a spell using your own formula. The return value is placed at the same spot where the engine would have expected it to appear after calling the original function.

Is this guess of mine at least half right?

Hmmm... maybe I'm asking too much. But if you have the time to direct me to tutorials, books, tools I would need to re-trace the steps you have made in "patching" the spell cost calculations I would be grateful! Thanks for your time.
User avatar
Hayley O'Gara
 
Posts: 3465
Joined: Wed Nov 22, 2006 2:53 am

Post » Mon May 07, 2012 5:57 pm

For example the process of spell making. My guess is, what your area of effect cost re-calculation is doing is to you redirect the original function call to a customized version of your own. In this new function you find the values you need in the memory where the engine normally puts those actor values and use them to calculate the cost of a spell using your own formula. The return value is placed at the same spot where the engine would have expected it to appear after calling the original function.
This is exactly what happens for the least complex patches. Sometimes the target mechanic is copied and pasted across several functions that do similar work, so you need to identify every copy and update it. Towards the harder end of things, your change will cause side effects in three other places which require additions to handle the new cases you added to the gameplay.

Your intended changes already have parallel features that decouple effects from spells (cast cost fixed at spell creation, and the weakness/resist effect fixes spell magnitude on hit). I do note there is a http://planetelderscrolls.gamespy.com/View.php?view=Mods.Detail&id=7807 out there already.

You should know assembler and common idioms and object lifetime behaviour of C++. As for tools, I use IDA Pro for disassembly and labelling the code, nasm for assembling new code, and a hex editor for searching for data and editing asm/data. There is no public documentation on how the data structures are laid out, because the effort of clearly communicating everything for a tiny audience is several times more work than making the patch. There is minimal data about in-game structures in http://mwse.svn.sourceforge.net/viewvc/mwse/trunk/MWSE/mwseTypes.h?revision=236&view=markup and in the http://planetelderscrolls.gamespy.com/View.php?view=utilities.detail&id=51. In general, the gameplay-specific data structures have a four character identifier at the start of the object e.g. 'MACP' for the player. This helps you identify objects on the stack frame.

Most of your time will be spent anolyzing functions in IDA to identify data and checking behaviour is correct under all situations. Examine the diff for the sepllmaker area effect cost as a starting point, and to see the multiple patches required for a single mechanic.
User avatar
Jose ordaz
 
Posts: 3552
Joined: Mon Aug 27, 2007 10:14 pm


Return to III - Morrowind