Hex Editing (3)

If we move on to Entry Level Hacking lesson 3 in the SimsCollege syllabus, Object Behavior Modification and Adding Motives, we will modify a BHAV resource in the same way that we modified the BCON resource.

This is quite a bit more complicated than modifying a BCON resource, so pay close attention to the SimsCollege explanation as well as to these notes.

First we will practice exporting the BHAV resource 4112 hex data as text:

export BHAV 4112

The text output should look like this:

hex data for Chair - Dining - Cheap.iff (2)
----
BHAV, ID# 4112
CT - sit comfort
Displaying binary data as hex
02 80 13 00 00 00 00 00 03 00 00 00
02 00 FE FE 06 00 86 00 00 0F 07 1A
20 00 07 02 29 BB BF 27 00 00 0A 00
20 00 08 03 87 89 93 E8 00 00 0A 00
20 00 09 04 F8 89 38 6B 00 00 0A 00
20 00 0A 05 DB 81 76 DA 00 00 0A 00
20 00 0C 06 98 2D B8 26 00 00 0A 00
20 00 0B FE BC 89 C3 5A 00 00 0A 00
02 00 0D FE 06 00 86 00 00 0F 0E 1A
02 00 0E FE 06 00 87 00 00 0F 0E 1A
02 00 0F FE 06 00 88 00 00 0F 0E 1A
02 00 10 FE 06 00 89 00 00 0F 0E 1A
02 00 12 FE 06 00 8A 00 00 0F 0E 1A
02 00 11 FE 06 00 8B 00 00 0F 0E 1A
02 00 FE FD 06 00 80 00 00 03 0E 1A
02 00 FE FD 06 00 81 00 00 03 0E 1A
02 00 FE FD 06 00 82 00 00 03 0E 1A
02 00 FE FD 06 00 83 00 00 03 0E 1A
02 00 FE FD 06 00 85 00 00 03 0E 1A
02 00 FE FD 06 00 84 00 00 03 0E 1A
----

The first hex line, which starts out 02 80 13 00, is the header section.
The next line, which starts out 02 00 FE FE, is data line (0).
The next line, which starts out 20 00 07 02, is data line (1), and so on.

These lines match the unformatted hex data in the HexEdit display for BHAV resource 4112 in the disassembled object folder.

BHAV 4112 hex

If you look closely, however, you will notice that the hex data
  02 00 FE FE 06 00 86 00 00 0F 07 1A
  20 00 07 02 29 BB BF 27 00 00 0A 00
  20 00 08 03 87 89 93 E8 00 00 0A 00
etc
isn't quite the same as the data in the previous IFFSnooper display.
[ 0] : 0 2: FE FE: 0 6: 0 86: F 0: 1A 7:
[ 1] : 0 20: 2 7: BB 29: 27 BF: 0 0: 0 A:
[ 2] : 0 20: 3 8: 89 87: E8 93: 0 0: 0 A:
etc
The individual byte values are the same, but the sequence is not. Why is this?
Because computers (and thus programs) can handle a series of bytes in different ways, as described below.


A quick word about byte order
IFFSnooper displays BHAV hex data in two byte chunks from right to left which is how an Intel processor would read it, especially if treating the two byte chunks as single numbers. So byte sequence 02 00 is decimal 2, not decimal 512, to an Intel processor, and (in most cases) that is how the game reads the hex data as well. Data line (0) calls SimAntics function 2, not function 512. Incidentally, this "two bytes at a time" rule also explains why IFFSnooper displays the BHAV function outcomes as false first then true.

If we keep the IFFSnooper data display open for reference, or print it out, it is easier to keep track of which line(s) and which byte(s) we should be modifying. Fortunately many of the BHAV parameters that we will want to modify only use a single byte so the other byte in the pair is zero. In other cases, such as when we see a four byte sequence 29 BB BF 27 in the parameter section of data line (1), it often turns out to be a GUID value read from right to left.


Now to implement the modifications discussed in the SimsCollege lesson. First we add a couple of lines at the end of the BHAV resource by duplicating data lines 7 and 13. These then become line 19 and 20 (hex equivalents 13 and 14).

02 00 0D FE 06 00 86 00 00 0F 0E 1A
02 00 FE FD 06 00 80 00 00 03 0E 1A

Next we change the values of 06 00 in both lines to 07 00

02 00 0D FE 07 00 86 00 00 0F 0E 1A
02 00 FE FD 07 00 80 00 00 03 0E 1A

Now we have to go back to lines 7 and 13 and change the function outcomes so that they point to our newly modified lines. Remember that we are using hex values so line 7 becomes

02 00 13 FE 06 00 86 00 00 0F 0E 1A

and line 13 becomes

02 00 14 FD 06 00 80 00 00 03 0E 1A

There is one last step before we paste our new hex data into BHAV resource 4112 using HexEdit. We have to change the BHAV resource header so that the game knows we added a couple of extra lines. So

02 80 13 00 00 00 00 00 03 00 00 00

becomes

02 80 15 00 00 00 00 00 03 00 00 00

The text output should now look like this:

hex data for Chair - Dining - Cheap.iff (2)
----
BHAV, ID# 4112
CT - sit comfort
Displaying binary data as hex
02 80 15 00 00 00 00 00 03 00 00 00
02 00 FE FE 06 00 86 00 00 0F 07 1A
20 00 07 02 29 BB BF 27 00 00 0A 00
20 00 08 03 87 89 93 E8 00 00 0A 00
20 00 09 04 F8 89 38 6B 00 00 0A 00
20 00 0A 05 DB 81 76 DA 00 00 0A 00
20 00 0C 06 98 2D B8 26 00 00 0A 00
20 00 0B FE BC 89 C3 5A 00 00 0A 00
02 00 13 FE 06 00 86 00 00 0F 0E 1A
02 00 0E FE 06 00 87 00 00 0F 0E 1A
02 00 0F FE 06 00 88 00 00 0F 0E 1A
02 00 10 FE 06 00 89 00 00 0F 0E 1A
02 00 12 FE 06 00 8A 00 00 0F 0E 1A
02 00 11 FE 06 00 8B 00 00 0F 0E 1A
02 00 14 FD 06 00 80 00 00 03 0E 1A
02 00 FE FD 06 00 81 00 00 03 0E 1A
02 00 FE FD 06 00 82 00 00 03 0E 1A
02 00 FE FD 06 00 83 00 00 03 0E 1A
02 00 FE FD 06 00 85 00 00 03 0E 1A
02 00 FE FD 06 00 84 00 00 03 0E 1A
02 00 0D FE 07 00 86 00 00 0F 0E 1A
02 00 FE FD 07 00 80 00 00 03 0E 1A
----

Next we paste the modified text into BHAV resource 4112 using HexEdit.

modified BHAV 4112 hex

Now it's time to reassemble our object. Let's call this version Chair - Dining - Cheap - 2.iff. We can verify that our new lines show up at the end of BHAV 4112 and that they refer to Hunger motives instead of Comfort.

modified BHAV 4112

As noted previously, this is practice since BHAV 4112 returns true at data line (0) -- our modified code would have no impact in the game. If you resequence BHAV 4112, you will delete all the unused lines, including our modified code.

To be continued ...