BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

How a port is made: from an address to a proven function

BF22142Heroes

Every piece of this port started as an address: a place in the original program where a function begins. This page follows one function from that address to a Rust function somebody can trust, and says at each step what has been proven and what has not. If you want to contribute, this is the method; if you only read the wiki, it tells you what the words ported, tested and played mean on every other page.

One function of the original on its way into the port 1 read it at its address the listing and, for floats, the instructions; a note with the address: a reading 2 the Rust function, with the citation the same statements in the same order; the address in its doc comment: ported 3 a test from the address written from what the original does; an oracle case calls the original itself: tested (strict) 4 the mutation the test is seen red once: against the tree without the port, or with one statement changed 5 emulator rows, 6 the replay one statement held in a CPU emulator; recorded input against the original server, tick by tick 7 a played scenario a server and a client, keys pressed, what a player sees asserted: wired and played the count reads every citation and every test: strict = a test names the function; generous = any proof
The steps and what each one proves. A function may stop at any step; the count says honestly where each stands.

1. Read the function at its address#

The original programs were analysed once into a listing: every function with its address, its decompiled form and its disassembly. A function is named by program and address, as everywhere in this wiki: bf2_w32ded.exe!0x0062a420 is a function of Battlefield 2's dedicated server, BFHeroes.exe@bfh126!0x007d0ead one of Heroes' version 1.126.

You read what it does: its inputs, the fields it reads and writes, the order of its statements. For anything about floating point the decompiler is not enough and you read the instructions, because the order of operations and where a value is stored to memory decide the last bit (float precision). What you find goes into a note under notes/ with the address beside each claim. A claim without evidence is marked as an inference there, and this wiki calls it a reading.

2. Write the Rust function, with the citation#

The port is a Rust function that does what the original does, in the original's order. Its doc comment cites the address:

rust
/// `Heightmap::getHeightLocal` (`BF2.exe!0x006a8730`): the height at a local point, two triangles a cell.
pub fn height_local(&self, x: f32, z: f32) -> f32 { .. }

That citation is what makes the function ported in the count: a tool reads every citation in the tree and matches it to the original's function list. A function that differs on purpose says so in the same comment ("a filed difference": the port does something else, the note says what and why).

3. Write a test from the address#

Not from the Rust: from the original. You take what the function does at its address and write a test that would fail for any code that does something else: the special cases, the rounding, the order. The test's own text cites the address, so the count can tie the proof to the function.

Where the function is pure (numbers in, numbers out) the best test is an oracle case: the original function itself is called, in a small host process, with generated inputs, and the port must answer the same bits (test rigs). No game runs for it.

4. The mutation: prove the test can fail#

A test that cannot fail proves nothing. So before the test counts, it is shown red once:

  • for a new port, the test is written first and fails against the tree without the port ("test red"), then the port turns it green;
  • for code that already exists, one statement of the Rust function is changed on purpose (a sign, a constant, a comparison), the test must fail, and the change is undone.

A test that stays green under the mutation is not about that statement, and is rewritten.

5. Emulator rows, where a function cannot be called whole#

Many functions need a live game around them: objects, managers, a level. They cannot be called in the oracle's host. One statement of them still can be held: the original's instructions for that statement run in a CPU emulator over generated inputs, once for each floating point precision the game runs at, and the results are stored as rows. The port's test must reproduce every row. Such a test says which statement it holds and that the function is not held whole.

6. The vehicle replay and the other differentials#

Some behaviour only exists across many functions and many ticks: a tank turning on a slope. For these the same recorded input goes to the port's server and is compared with what the original server did with it, tick by tick. The vehicle replay is the strictest: after a change to physics, "the replay is identical" means no float of any node moved in any tick of any capture. A change that is meant to move something says which rows moved and why.

7. A played scenario#

Last, the thing is played: a scenario starts the port's server and a client (the desktop program or the page in a browser), presses keys, and asserts on what a player would see: the HUD's variables, a screenshot's pixels, the score. This is the only step that proves the function is wired: that something calls it at the right moment. A perfect function nobody calls is a common kind of fault.

What each step earns in the count#

You didThe page may sayClass in the parity count
read it, wrote the notea readingnone: the function is not ported
wrote the Rust with its citationportedported, not tested
a test from the address, or an oracle caseported and testeda1: strict
a test calls it but was not written about ita2: generous
it ran in a passing scenario's traced runplayedb: generous
its Battlefield 2 twin has a testd: generous
a scenario's text names the addressplayedf: generous

The strict number counts a1 alone. That is deliberate: it is the only class that says somebody read this function and wrote down what it must do.

The words on a row#

Sweeps (a pass over every widget, every HUD variable, every ability) write one row a thing and give it a state. The wiki uses the same words and nothing stronger:

WordMeaning
SAMEread in the original and in the port, body against body; they do the same
DIFFERS / filed differencethey do not; the row says what the original does and what a player sees
TEST REDa test from the address exists and fails against the port: the difference is proven, not yet mended
MENDED / portedthe port was changed and the test is green
PLAYEDa scenario showed it in a client
NOT WIREDthe function is ported and nothing calls it
NOT SETTLEDthe reading is not finished

Rules that hold for every step#

  • Only the port's own servers are used, on 127.0.0.1. The original client never runs as a program in a check: only its code, inside the oracle's host or the emulator.
  • The games' files are never in the tree. A player's own install is read where it lies.
  • A fix lands with a check that fails before it and passes after it.
  • What is not proven is said. A page that says "as read" means exactly that.

Where to start#

Build the tree first: docs/release/BUILDING.md in the repository has the table of programs and what each needs, and docs/release/LAYOUT.md says what every folder and crate is. Then: build and run, how to read the notes, the test rigs and the questions people ask.

How it is tested#

This page describes a procedure, and the procedure is its own test: a port is accepted only with a test that was seen red, one mutation that the test caught, and, for the server's physics, an identical vehicle replay. The landing tool runs those checks on the merged tree (docs/release/CONTRIBUTING.md). The page's example addresses are real; the wiki's test checks its links and its diagram.

In the code#

This page names no crate of its own. How the codebase is organised maps the crates by area, and docs/release/LAYOUT.md in the tree has one line for every folder and crate.

Source: docs/dev/guide/how-a-port-is-made.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.