Skip to content

Cleaning Code

Anders Jenbo edited this page Mar 17, 2021 · 7 revisions

You can get an overview of what functions need to be cleaned up on the Binary identical functions milestone.

Tools you will need

The gold compiler

Visual Studio 6 Pro/Enterprise

If you have a copy of VC 6 Pro or Enterprise you can use the Diablo.dsp project file, or simply set MAKE_BUILD=pdb in your environment and run make from the project root (Visual Studio must be installed in your home folder as VC6).

Docker

If VC is not available to you we have a Docker image that can be used instead. On Debian/Ubuntu Docker can be installed by running the following commands:

sudo apt install docker
docker adduser ${USER} docker
reboot

After installing you must create a docker hub account, and then sign in to Docker on your machine:

docker login

To compile you then run the following command from the project root:

docker run --rm -v $(pwd):/root/devilution -e MAKE_BUILD=pdb diasurgical/riivaaja

Note: Windows users must replace $(pwd) with the full path to the project root.

Compare the generated code

You will need Diablo.exe patched to 1.09b

First download Devilution Comparer.

If you are not on Windows you need a Wine, if you are already using the docker image it can be used as a headless wine, to do this create a file called wine with the following content:

#!/bin/sh
docker run -v $(pwd):/root/devilution --entrypoint "/usr/bin/wine" diasurgical/riivaaja:stable $(basename $1) $2 $3

Mark it as executable chmod +x wine and move it sudo mv wine /usr/local/bin/

Download the latest comparer-config.toml.

Run the following command to compare your function to the original:

./devilution-comparer Diablo_109b.exe Diablo.exe FUNCTION

This will generate two files orig.asm and compare.asm, use a diff tool like DiffMerge to compare the two files. For the result to be considered bin exact the two should match up, how ever there are some exceptions to this:

  • Jumps in to other functions (usually on the last line)
  • Global memory addresses (usually in the range 0x400000 - 0x700000) can differ
  • If the function has a switch case extra junk may appear at the end of the new function

Helpful resources

The clean up process

Cleaning up the code

Guide to x86 Assembly

Handling Git

If you are somewhat unfamiliar with Git here are some easy steps to follow.

Start by making your own fork of Devilution: https://github.com/diasurgical/devilution

Clone your repo to your local machine and link it to the upstream Diasurgical:

git clone https://github.com/YOURNAME/devilution.git
git remote add devilution https://github.com/diasurgical/devilution.git

Each time you start working on a function:

git fetch --all -p
git checkout -b FUNCTION_NAME devilution/master

Each time you're done:

git commit -am "FUNCTION_NAME bin exact"
git push origin FUNCTION_NAME

Then go to https://github.com/diasurgical/devilution/pulls and open a new pull request based on your branch.