OpenTPW

tpi-layouts: reproduce the Theme Park Inc thing-body layouts#

source/OpenTPW.Files/Formats/Save/ThemeParkIncBodyLayout.Data.cs holds the layouts of the guest, staff, gardener, staff HQ, advisor controller, park analyser, research lab, weather, challenge system and tag system bodies, written from the load routines of the no-CD Theme Park Inc Game.exe. The unit tests build bodies from those layouts, so they alone cannot show that a layout is right; the widths and offsets they quote come from this harness, which runs the original load routines in an x86 emulator.

It needs your own copy of the executable (never commit it or bytes of it) and Python with unicorn:

python3 -m venv /tmp/tpi-venv && /tmp/tpi-venv/bin/pip install unicorn
export TPI_GAME_EXE="$HOME/Downloads/TPW-drive/tpinc-nocd/Game.exe"
cd tools/tpi-layouts
/tmp/tpi-venv/bin/python dump.py mechanic 3 0 30 1 0 7   # every read: stream offset, size, member name
/tmp/tpi-venv/bin/python golden.py                      # the widths quoted in ThemeParkIncThingBodyTests
/tmp/tpi-venv/bin/python verify.py                      # the C# layout text against the emulation (exit 1 on a mismatch)
/tmp/tpi-venv/bin/python navigator.py                   # the person navigator's serializer 0x63ad30: load then save gives the record back (T4)

How it works: emu.py maps the PE sections, patches the import table to stubs, sets up a GDT for fs: and replaces the stream object (0x7cc930 calls [vtable + 8]) with a hook that serves generated bytes and records each read. The field-name helper 0x441da0 is hooked to record the member name of the next read. models.py lists each class's load routine (thunk addresses from the World loader's dispatch at 0x64c771, table 0x64d7e0) and its constructors. A routine is called as (stream, 0 = load, version 1, id) with this zero-filled, which is how the loader calls it. run.py produces the stream on demand, so ring counts can be chosen. Limits: the sprite-table counts the guest load divides by are filled with 4, a garbage sprite index or position can crash a routine on state the harness does not have (random bytes are therefore not used), and nothing here proves a layout against a saved guest, staff member or gardener (SAVE-944).

navigator.py is the navigator port's T4 (docs/reverse/TPI-NAVIGATOR.md, NV2): it hooks the stream's write (0x7cc960 calls [vtable + 0xc]) as well, loads a 177-byte record of distinct bytes into a navigator filled with 0xab and saves it again. The two must be equal; it lists the 33 transfers with their record offsets, member names and navigator offsets, the layout SaveNavigation and GuestNavigation.ToRecord follow, and checks that the load leaves the unnamed +0x30 zero.

Source: tools/tpi-layouts/README.md in the project repository.