News:

Don't forget to visit the main site! There's lots of helpful docs, patches, and more!

Main Menu

PrimeAPI (C++ code execution in MP1)

Started by Aruki, October 04, 2016, 04:48:22 PM

Previous topic - Next topic

Aruki


PrimeAPI is a custom build script and a set of headers that allows you to write your own code in C++ that interfaces with classes/functions from Metroid Prime 1, compile it to a .rel module, and then link it and run your code ingame. It requires an installation of the Dolphin SDK and the compiler that comes with it, CodeWarrior v2.3.3 (must be a version that has access to GC/Wii compiling functionality, which is normally license-gated). The build script is written in Python 3; it can be found in scripts/BuildModule.py.

Once everything is set up, it's really easy to get your code ingame. The module must have a _prolog function with the INIT_MODULE macro, and you can use the PATCH_SYMBOL macro to define overrides for ingame functions. That's pretty much all there is to it. You can see a test module I wrote which updates the player's health every frame here. The source code for that module:

#include <PrimeAPI.h>
#include <CStateManager.h>

// Forward decls
void _prolog();
bool UpdatePlayerHealth(CStateManager&);

// Patches
PATCH_SYMBOL(CPlayer::IsUnderBetaMetroidAttack(CStateManager&) const, UpdatePlayerHealth(CStateManager&))

// Impls
void _prolog()
{
MODULE_INIT;
}

bool UpdatePlayerHealth(CStateManager& rStateMgr)
{
// Fetch current health
float *pPlayerHealth = (float*) (*((char**) 0x804578CC) + 0x2AC);
float health = *pPlayerHealth;

// Update current health
health += 1.f;
if (health >= 100.f)
health = 1.f;

*pPlayerHealth = health;

// Let the caller know we are not being attacked by a beta metroid
return false;
}


Another simple patch I tested that makes Samus take damage whenever an enemy gets hurt:

PATCH_SYMBOL(CAi::HealthInfo(CStateManager&), CPlayer::HealthInfo(CStateManager&))

The headers are really lacking atm since I've barely gotten started on this, and the build system could stand to be improved a fair bit too, but there's a lot of work to really make it robust and easy to make things in, so I'm hoping to see more contributors. In its current state it's already pretty fun to mess around with so hopefully it just gets better from here!

interdpth

Guess I'm releasing GBATroid C api soon haha.
NICE!