Set up m2ctx for decomp.me context generation

This commit is contained in:
Rachel 2024-06-03 13:50:03 -07:00
parent 69ceb8a8d7
commit fbf73e4541
2 changed files with 70 additions and 0 deletions

31
tools/m2ctx/README.md Normal file
View File

@ -0,0 +1,31 @@
# Generate a context file for decomp.me
[Decomp.me](https://decomp.me) requires a context file declaring any imported typedefs, structs, or constants that will be used in the decompiled function.
`m2ctx` generates this context file automatically.
## How to use
Before using `m2ctx`, follow the [installation instructions](../../INSTALL.md) to make a vanilla copy of the game.
This generates some header files that may be included in decompiled files.
In this example, you are decompiling an asm file `asm/npc_trade.s` into a new C file `src/npc_trade.c`.
You've created the source file and may have added some imports you know you'll need.
Your file may look like this:
```
#include "pokemon.h"
```
Run `./tools/m2ctx/m2ctx.sh src/npc_trade.c` to generate a context file, which will be output as `ctx.c`.
Copy the contents of `ctx.c` into the "Context" section of the scratch.
### Adding imports
As you continue decompiling, you may realize that you need to add imports to your source file:
```diff
#include "pokemon.h"
+#include "party.h"
```
You can use `m2ctx` to generate an updated context file which will include declarations from the new imports.
Simply re-run the command to generate a new context file: `./tools/m2ctx/m2ctx.sh src/npc_trade.c`.

39
tools/m2ctx/m2ctx.sh Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env bash
OUT_FILE=ctx.c
GCC=gcc
FLAGS="-E -P -dD -undef"
INCLUDES="-Itools/cw/include -Isubprojects/NitroSDK-4.2.30001/include -Ibuild/subprojects/NitroSDK-4.2.30001/gen -Isubprojects/NitroSystem-071126.1/include -Isubprojects/NitroWiFi-2.1.30003/include -Isubprojects/NitroDWC-2.2.30008/include -Isubprojects/libvct-1.3.1/include -Isubprojects/libcrypto/include -Isubprojects/ppwlobby/include -Iinclude -Iasm -Ires -Ilib/gds/include -Ilib/spl/include -Ibuild -Ibuild/res/text -include pch/global_pch.h"
DEFINES="-DGAME_VERSION=PLATINUM -DGAME_LANGUAGE=ENGLISH -DPM_KEEP_ASSERTS -D_NITRO -DLINK_PPWLOBBY -DNNS_FINALROM -DSDK_4M -DSDK_ARM9 -DSDK_CODE_ARM -DSDK_CW -DSDK_CW_FORCE_EXPORT_SUPPORT -DSDK_FINALROM -DSDK_TS"
generate-ctx () {
# Remove any line containing a predefined macro. If not removed, mwccarm
# generates compiler warnings.
grep "^#include " "$1" | $GCC $FLAGS $INCLUDES $DEFINES -x c - | sed '/__STDC__\|__STDC_VERSION__\|__STDC_VERSION__\|__STDC_HOSTED__/d' > $OUT_FILE
}
usage () {
echo "Generate a context file for decomp.me."
echo "Usage: $0 [-h] [FILEPATH]"
echo ""
echo "Arguments:"
echo " FILEPATH Source file used to generate ctx.c"
echo ""
echo "Options:"
echo " -h Show this message and exit"
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h)
usage
exit 0
;;
*)
generate-ctx "$1"
exit 0
;;
esac
done