Skip to content

Commit

Permalink
rel: Rework modding infrastructure
Browse files Browse the repository at this point in the history
* Console revamped
* Features decentralized
  • Loading branch information
PistonMiner committed Feb 21, 2021
1 parent 73843c2 commit e5832e5
Show file tree
Hide file tree
Showing 9 changed files with 944 additions and 276 deletions.
2 changes: 1 addition & 1 deletion ttyd-tools/rel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ endif
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lm
LIBS := -lgcc

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
Expand Down
130 changes: 130 additions & 0 deletions ttyd-tools/rel/include/console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#pragma once

#include "keyboard.h"

#include <gc/types.h>

namespace mod {

class ConCommand
{
public:
using ExecuteCallback = void (*)(const char *args);

ConCommand(const char *name, ExecuteCallback executeCb)
{
next = sFirst;
sFirst = this;

this->name = name;
this->executeCb = executeCb;
}

public:
const char *name;
ExecuteCallback executeCb;

private:
ConCommand *next;
static ConCommand *sFirst;

friend class ConsoleSystem;
};

class ConIntVar
{
public:
using ChangedCallback = void (*)(ConIntVar *self, int new_value);

ConIntVar(const char *name, int value, ChangedCallback changedCb = nullptr)
{
next = sFirst;
sFirst = this;

this->name = name;
this->value = value;
this->changedCb = changedCb;
}

public:
const char *name;
int value;
ChangedCallback changedCb;

private:
ConIntVar *next;
static ConIntVar *sFirst;

friend class ConsoleSystem;
};


class ConsoleSystem;
extern ConsoleSystem *gConsole;

enum LogLevel
{
LogLevel_Debug = 0,
LogLevel_Info,
LogLevel_Warning,
LogLevel_Error,
};

class ConsoleSystem
{
public:
ConsoleSystem()
: mKeyboard(1)
{
gConsole = this;
}

void init();
void update();

void logInfo(const char *fmt, ...);
void logWarning(const char *fmt, ...);
void logError(const char *fmt, ...);
void logDebug(const char *fmt, ...);

void logColor(const char *text, gc::color4 color);

void overlay(const char *fmt, ...);

void setMonospace(bool monospace);

private:
void drawLine(int line, const char *text, gc::color4 color = {0xff,0xff,0xff,0xff});

void updatePrompt();
void processCommand(const char *text);
void disp();

private:
constexpr static int kMaxColumns = 608 / 8 - 2;
constexpr static int kNumRowsMonospaceFont = 480 / 8 - 2;
constexpr static int kNumRowsVariableWidthFont = 440 / 28;
constexpr static int kMaxRows = kNumRowsMonospaceFont;
static_assert(kMaxRows >= kNumRowsMonospaceFont);
static_assert(kMaxRows >= kNumRowsVariableWidthFont);

bool mIsMonospace;
int mRowCount;

char mOverlayBuffer[256] = "";

struct LogLine
{
uint64_t time;
gc::color4 color;
char text[kMaxColumns + 1];
};
LogLine mLogLines[kMaxRows - 1] = {};

bool mPromptActive = false;
char mPromptBuffer[64] = "";
int mBackspaceHoldTimer = 0;
Keyboard mKeyboard;
};

}
69 changes: 44 additions & 25 deletions ttyd-tools/rel/include/mod.h
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
#pragma once

#include "timer.h"
#include "keyboard.h"
#include "util.h"
#include "console.h"

#include <cstdint>

struct ModInitFunction
{
ModInitFunction(void (*f)())
{
next = sFirst;
sFirst = this;
initFunction = f;
}

ModInitFunction *next;
void (*initFunction)();

static ModInitFunction *sFirst;
};

struct ModUpdateFunction
{
ModUpdateFunction(void (*f)())
{
next = sFirst;
sFirst = this;
updateFunction = f;
}

ModUpdateFunction *next;
void (*updateFunction)();

static ModUpdateFunction *sFirst;
};

#define MOD_INTERNAL_ADD_FUNCTION(type) \
static void MOD_ANONYMOUS(mod_if_func)(); \
static type MOD_ANONYMOUS(mod_if_obj)(MOD_ANONYMOUS(mod_if_func)); \
static void MOD_ANONYMOUS(mod_if_func)()

#define MOD_INIT_FUNCTION() \
MOD_INTERNAL_ADD_FUNCTION(ModInitFunction)
#define MOD_UPDATE_FUNCTION() \
MOD_INTERNAL_ADD_FUNCTION(ModUpdateFunction)

namespace mod {

class Mod
{
public:
Mod();
void init();

private:
void updateEarly();
void draw();

void updateConsole();
void processCommand(const char *command);
void updateHeapInfo();

void drawConsole();
void drawMovementInfo();
void drawHeapInfo();

private:
void (*mPFN_makeKey_trampoline)() = nullptr;

bool mShowUi = true;
char mDisplayBuffer[256];

bool mShowMovementInfo = false;

char mCommandBuffer[64] = "";
int mBackspaceHoldTimer = 0;

bool mConsoleActive = false;

int mDebugHeapId = -1;
char mDebugHeapText[64];

Keyboard *mKeyboard = nullptr;
ConsoleSystem mConsole;
};

}
31 changes: 31 additions & 0 deletions ttyd-tools/rel/include/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <gc/os.h>

#include <cstdint>

#define MOD_ARRAYSIZE(x) \
(sizeof((x)) / sizeof((x)[0]))

#define MOD_CONCAT_IMPL(s1, s2) s1##s2
#define MOD_CONCAT(s1, s2) MOD_CONCAT_IMPL(s1, s2)
#define MOD_ANONYMOUS(str) MOD_CONCAT(str, __LINE__)

#define MOD_ASSERT(x) \
do \
{ \
if (!(x)) \
{ \
gc::os::OSPanic(__FILE__, __LINE__, "Mod assertion failed: %s\n", #x); \
} \
} \
while(false)

namespace mod::util {

inline int64_t GetTbRate()
{
return *(int32_t *)0x800000f8 / 4;
}

}
36 changes: 36 additions & 0 deletions ttyd-tools/rel/source/con_mario.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "mod.h"
#include "console.h"

#include <ttyd/mario.h>

#include <cstdio>

namespace mod {

ConCommand mario_set_pos("mario_set_pos", [](const char *args) {
float x, y, z;
if (sscanf(args, "%f %f %f", &x, &y, &z) != 3)
return;

ttyd::mario::Player *mp = ttyd::mario::marioGetPtr();
mp->playerPosition[0] = x;
mp->playerPosition[1] = y;
mp->playerPosition[2] = z;
});

ConIntVar mario_show_pos("mario_show_pos", 0);
MOD_UPDATE_FUNCTION()
{
if (!mario_show_pos.value)
return;

ttyd::mario::Player *player = ttyd::mario::marioGetPtr();
gConsole->overlay(
"Pos: %.2f %.2f %.2f\n"
"SpdY: %.2f\n",
player->playerPosition[0], player->playerPosition[1], player->playerPosition[2],
player->wJumpVelocityY
);
}

}
Loading

0 comments on commit e5832e5

Please sign in to comment.