Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic message formatting #57

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions code/include/rnd/custom_message.h

This file was deleted.

92 changes: 92 additions & 0 deletions code/include/rnd/custom_messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef _RND_CUSTOM_MESSAGES_H_
#define _RND_CUSTOM_MESSAGES_H_

#include "common/utils.h"
#include "game/message.h"

#define MAX_MSG_SIZE 512
#define MAX_UNFORMATTED_SIZE 354

bool SetCustomMessage(u16, game::MessageResEntry*);

typedef struct {
u16 id;
u16 field_2;
u32 field_4;
u32 flags;
char text[MAX_UNFORMATTED_SIZE];
char cols[4];
char icons[6];
char delays[6];
u16 sfxAndFlags;
} UnformattedMessage;

typedef enum {
BLANK = 0x00,
A_BUTTON,
B_BUTTON,
X_BUTTON,
Y_BUTTON,
L_BUTTON,
R_BUTTON,
START_BUTTON,
HOME_BUTTON,
POWER_BUTTON,
D_PAD,
D_PAD_UP,
D_PAD_RIGHT,
D_PAD_DOWN,
D_PAD_LEFT,
P_PAD_VERTICAL,
P_PAD_HORIZONTAL,
C_PAD,
C_PAD_UP,
C_PAD_RIGHT,
C_PAD_DOWN,
C_PAD_LEFT,
C_PAD_VERTICAL,
C_PAD_HORIZONTAL,
I_TOUCH,
X_TOUCH,
Y_TOUCH,
II_TOUCH,
ITEMS_TOUCH,

MASKS_TOUCH = 0x1E,
OCARINA_TOUCH,
PICTOBOX_TOUCH,
TARGET_RETICLE,

SUN = 0x28,
MOON,
ONE_BLOCK,
TWO_BLOCK,
THREE_BLOCK,
GEAR_TOUCH,
MAP_TOUCH,
FLAG,
TATL,
TILDE,
NOTEBOOK,
FIRST_PERSON,
ZL_BUTTON,
ZR_BUTTON,
MAJORA,
} iconType;

typedef enum {
WHITE,
RED,
GREEN,
BLUE,
YELLOW,
CYAN,
MAGENTA,
GREY,
ORANGE,
DARK_GREY,
BLACK,
DEFAULT,
} colType;

#endif
66 changes: 5 additions & 61 deletions code/source/game/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Brought in from the Project Restoration libraries. Modified for custom messages.
*/
#include "game/message.h"
#include "rnd/custom_message.h"
#include "rnd/custom_messages.h"

#include "common/utils.h"

Expand All @@ -19,83 +19,27 @@ extern "C" {
#endif

namespace game {
#ifdef ENABLE_DEBUG
static bool declareTestMessage = false;
static MessageResEntry ptrCustomMessageEntries[1] = {0};
volatile const u32 numCustomMessageEntries = 1;
#else
volatile const MessageResEntry* ptrCustomMessageEntries = {0};
volatile const u32 numCustomMessageEntries = {0};
#endif
MessageResEntry customIceMessage = {0};
MessageResEntry customSwordMessage = {0};
MessageResEntry customEntry = {0};

MessageMgr& MessageMgr::Instance() {
return rnd::util::GetPointer<MessageMgr&()>(0x1C51D0)();
}

bool MessageData::Get(u32 id, Message* msg) const {
#ifdef ENABLE_DEBUG
if (!declareTestMessage) {
ptrCustomMessageEntries[0].id = 0x6133;
ptrCustomMessageEntries[0].field_2 = 0xFFFF;
ptrCustomMessageEntries[0].field_4 = 0x3FFFFFFF;
ptrCustomMessageEntries[0].flags = 0xFF0000;
ptrCustomMessageEntries[0].texts[0].offset =
"Hmmph... I've been made a \x7f:\x00\x01\x00\x46OOL\x7f\x00:\x00\x0b\x00 "
"of!\x7f\x00\x31";
ptrCustomMessageEntries[0].texts[0].length = 55;
declareTestMessage = true;
}
#endif
int start = 0;
int end = res_header->num_msgs - 1;
int customEnd = numCustomMessageEntries - 1;
bool isCustom = false;
const auto get_entry = [this](size_t idx) {
return reinterpret_cast<const MessageResEntry*>((const u8*)res_entries + res_entry_size * idx);
};
const auto get_custom_entry = [this](size_t idx) {
return reinterpret_cast<const MessageResEntry*>((const u8*)ptrCustomMessageEntries + idx);
};

const MessageResEntry* entry = nullptr;

if (id == 0x0012) {
customIceMessage.id = 0x0012;
customIceMessage.field_2 = 0xFFFF;
customIceMessage.field_4 = 0x3FFFFFFF;
customIceMessage.flags = 0xFF0000;
// customIceMessage.texts[0].offset = "Hmmph... I've been made a
// \x7f:\x00\x01\x00\x46OOL\x7f\x00:\x00\x0b\x00 of!\x7f\x00\x31";
customIceMessage.texts[0].offset = iceTrapMsg.data;
customIceMessage.texts[0].length = iceTrapMsg.size;
entry = &customIceMessage;
isCustom = true;
} else if (id == 0x0037) {
customSwordMessage.id = 0x0037;
customSwordMessage.field_2 = 0xFFFF;
customSwordMessage.field_4 = 0x3FFFFFF;
customSwordMessage.flags = 0x4D0000;
customSwordMessage.texts[0].offset = kokiriSwordMsg.data;
customSwordMessage.texts[0].length = kokiriSwordMsg.size;
entry = &customSwordMessage;
if (SetCustomMessage(id, &customEntry)) {
entry = &customEntry;
isCustom = true;
}
while (!entry && start <= customEnd) {
const int current_entry_idx = (start + customEnd) / 2;
const auto* candidate = get_custom_entry(current_entry_idx);
if (candidate->id < id)
start = current_entry_idx + 1;
else if (candidate->id > id)
customEnd = current_entry_idx - 1;
else {
entry = candidate;
isCustom = true;
}
}

if (!entry) {
if (!isCustom) {
start = 0;
while (!entry && start <= end) {
const int current_entry_idx = (start + end) / 2;
Expand Down
Loading
Loading