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

[MKW] Display the current status of the "Open Host" feature #70

Closed
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
33 changes: 33 additions & 0 deletions payload/import/mkw/ui/control/menuInstructionTextControl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "import/mkw/ui/ui.hpp"
#include "uiControl.hpp"

namespace mkw::UI
{

#if RMC

// https://github.com/mkw-sp/mkw-sp/blob/main/payload/game/ui/ctrl/CtrlMenuInstructionText.hh
class MenuInstructionTextControl : public LayoutUIControl
{
public:
MenuInstructionTextControl();
~MenuInstructionTextControl() override;

void setMessage(s32 messageId, FormatParam* formatParam = nullptr)
{
LONGCALL void setMessage(
MenuInstructionTextControl * menuInstructionTextControl,
s32 messageId, FormatParam* formatParam = nullptr
) AT(RMCXD_PORT(0x807E9A38, 0x80841960, 0x807E90A4, 0x807D7DF8));

setMessage(this, messageId, formatParam);
}
};

static_assert(sizeof(MenuInstructionTextControl) == 0x174);

#endif

} // namespace mkw::UI
51 changes: 51 additions & 0 deletions payload/import/mkw/ui/control/uiControl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <wwfcCommon.h>

namespace mkw::UI
{

#if RMC

// https://github.com/mkw-sp/mkw-sp/blob/main/payload/game/ui/UIControl.hh
class UIControl
{
public:
UIControl();
virtual ~UIControl();
virtual void dt(s32 type);
virtual void vf_08();
virtual void vf_0C();
virtual void vf_10();
virtual void vf_14();
virtual void vf_18();
virtual void vf_1C();
virtual void vf_20();
virtual void vf_24();
virtual void vf_28();
virtual void vf_2C();
virtual void vf_30();
virtual void vf_34();

private:
/* 0x04 */ u8 _04[0x98 - 0x04];
};

static_assert(sizeof(UIControl) == 0x98);

class LayoutUIControl : public UIControl
{
public:
LayoutUIControl();
~LayoutUIControl() override;
virtual void vf_38();

private:
/* 0x098 */ u8 _098[0x174 - 0x098];
};

static_assert(sizeof(LayoutUIControl) == 0x174);

#endif

} // namespace mkw::UI
5 changes: 3 additions & 2 deletions payload/import/mkw/ui/menuInputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MenuInputManager
class Handler final : public IHandler
{
public:
Handler(T* object, void (T::*function)(u32))
Handler(T* object, void (T::*function)(u32 localPlayerId))
{
m_object = object;
m_function = function;
Expand All @@ -47,9 +47,10 @@ class MenuInputManager

private:
T* m_object;
void (T::*m_function)(u32);
void (T::*m_function)(u32 localPlayerId);
};

MenuInputManager();
virtual ~MenuInputManager();

private:
Expand Down
20 changes: 15 additions & 5 deletions payload/import/mkw/ui/page/openHostPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ class OpenHostPage : public Page
transition(resolve());
}

static bool IsOpenHostEnabled()
{
return s_openHostEnabled;
}

static void SetOpenHostValue(bool openHostEnabled)
{
s_openHostEnabled = openHostEnabled;
}

private:
OpenHostPage();

Expand Down Expand Up @@ -121,7 +131,7 @@ class OpenHostPage : public Page
L"the server.\n\n"
L"Please try again later.";
} else {
if (s_enableOpenHost) {
if (s_openHostEnabled) {
formatParam.strings[0] = L"Open Host is now enabled!";
} else {
formatParam.strings[0] = L"Open Host is now disabled!";
Expand Down Expand Up @@ -155,22 +165,22 @@ class OpenHostPage : public Page
}
s_sentOpenHostValue = true;

bool enableOpenHost = choice == 0;
bool openHostEnabled = choice == 0;

char openHostValue[2];
openHostValue[0] = '0' + enableOpenHost;
openHostValue[0] = '0' + openHostEnabled;
openHostValue[1] = '\0';
GameSpy::gpiSendLocalInfo(
gpConnection, "\\wwfc_openhost\\", openHostValue
);

s_enableOpenHost = enableOpenHost;
s_openHostEnabled = openHostEnabled;
}

static State s_state;
static mkw::UI::MenuInputManager::Handler<OpenHostPage>* s_onOption;
static mkw::UI::YesNoPage::Handler<OpenHostPage>* s_onYesOrNo;
static bool s_enableOpenHost;
static bool s_openHostEnabled;
static bool s_sentOpenHostValue;
};

Expand Down
1 change: 1 addition & 0 deletions payload/import/mkw/ui/page/pageId.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace mkw::UI
enum class PageId {
MessagePopup = 0x4D,
YesNoPopup = 0x4E,
WifiFriendMenu = 0x8D,
};

#endif
Expand Down
10 changes: 9 additions & 1 deletion payload/import/mkw/ui/page/wifiFriendMenuPage.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "import/mkw/ui/control/menuInstructionTextControl.hpp"
#include "openHostPage.hpp"

namespace mkw::UI
Expand Down Expand Up @@ -40,8 +41,15 @@ class WifiFriendMenuPage : public OpenHostPage
OpenHostPage::onRefocus();
}

MenuInstructionTextControl& menuInstructionTextControl()
{
return m_menuInstructionTextControl;
}

private:
/* 0x044 */ u8 _044[0xF34 - 0x044];
/* 0x044 */ u8 _044[0xB98 - 0x044];
/* 0xB98 */ MenuInstructionTextControl m_menuInstructionTextControl;
/* 0xD0C */ u8 _D0C[0xF34 - 0xD0C];
};

static_assert(sizeof(WifiFriendMenuPage) == 0xF34);
Expand Down
2 changes: 1 addition & 1 deletion payload/import/mkw/ui/page/yesNoPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class YesNoPage : public Page
class Handler final : public IHandler
{
public:
Handler(T* object, void (T::*function)(int, void*))
Handler(T* object, void (T::*function)(int choice, void* pushButton))
{
m_object = object;
m_function = function;
Expand Down
8 changes: 4 additions & 4 deletions payload/wwfcError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ char* HandleWWFCErrorMessage(char* strstrResult, const char* command)
}

char value[512];
if (GameSpy::gpiValueForKey(
if (!GameSpy::gpiValueForKey(
command, "\\wwfc_err\\", value, sizeof(value)
) == GameSpy::GPIFalse) {
)) {
return strstrResult;
}

Expand All @@ -83,9 +83,9 @@ char* HandleWWFCErrorMessage(char* strstrResult, const char* command)
return strstrResult;
}

if (GameSpy::gpiValueForKey(
if (!GameSpy::gpiValueForKey(
command, "\\wwfc_errmsg\\", value, sizeof(value)
) == GameSpy::GPIFalse) {
)) {
return strstrResult;
}

Expand Down
83 changes: 66 additions & 17 deletions payload/wwfcFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace mkw::UI
OpenHostPage::State OpenHostPage::s_state = OpenHostPage::State::Previous;
MenuInputManager::Handler<OpenHostPage>* OpenHostPage::s_onOption = nullptr;
YesNoPage::Handler<OpenHostPage>* OpenHostPage::s_onYesOrNo = nullptr;
bool OpenHostPage::s_enableOpenHost = false;
bool OpenHostPage::s_openHostEnabled = false;
bool OpenHostPage::s_sentOpenHostValue = false;

const wchar_t* WifiMenuPage::s_messageOfTheDay =
Expand All @@ -43,23 +43,15 @@ namespace wwfc::Feature

#if RMC

extern "C" {
__attribute__((__used__)) static GameSpy::GPResult
GetMessageOfTheDay(const char* message)
// Get the Message Of The Day from a message
static void GetMessageOfTheDay(const char* message)
{
using namespace mkw::UI;

const char loginChallenge2Message[] = "\\lc\\2";
if (strncmp(
message, loginChallenge2Message, sizeof(loginChallenge2Message) - 1
)) {
return GameSpy::GPNoError;
}

const char motdKey[] = "\\wwfc_motd\\";
char value[512];
if (!GameSpy::gpiValueForKey(message, motdKey, value, sizeof(value))) {
return GameSpy::GPNoError;
return;
}

wchar_t* messageOfTheDayBuffer = WifiMenuPage::MessageOfTheDayBuffer();
Expand All @@ -71,31 +63,60 @@ GetMessageOfTheDay(const char* message)
);
if (messageOfTheDayLength == -1 ||
messageOfTheDayLength == messageOfTheDayBufferSize) {
return GameSpy::GPNoError;
return;
}
messageOfTheDayBuffer[messageOfTheDayLength / sizeof(wchar_t)] = L'\0';

WifiMenuPage::SetMessageOfTheDay(messageOfTheDayBuffer);
}

// Get the "Open Host" setting value from a message
static void GetOpenHostValue(const char* message)
{
const char openHostKey[] = "\\wwfc_openhost\\";
char value[2];
if (!GameSpy::gpiValueForKey(message, openHostKey, value, sizeof(value))) {
return;
}

return GameSpy::GPNoError;
bool openHostEnabled = value[0] == '1';
mkw::UI::OpenHostPage::SetOpenHostValue(openHostEnabled);
}

extern "C" {
__attribute__((__used__)) static GameSpy::GPResult
GetValuesFromLoginChallenge2Message(
GameSpy::GPResult gpResult, const char* message
)
{
const char loginChallenge2Message[] = "\\lc\\2";
if (strncmp(
message, loginChallenge2Message, sizeof(loginChallenge2Message) - 1
)) {
return gpResult;
}

GetMessageOfTheDay(message);
GetOpenHostValue(message);

return gpResult;
}
}

// Get the "Message Of The Day" from the "Login Challenge" message
WWFC_DEFINE_PATCH = {
Patch::BranchWithCTR(
WWFC_PATCH_LEVEL_FEATURE,
RMCXD_PORT(0x80101074, 0x80100FD4, 0x80100F94, 0x801010EC), //
ASM_LAMBDA(
// clang-format off
mr r3, r26;
mr r4, r26;

bl _restgpr_26;
lwz r0, 0x2D4(r1);
mtlr r0;
addi r1, r1, 0x2D0;

b GetMessageOfTheDay;
b GetValuesFromLoginChallenge2Message;
// clang-format on
)
),
Expand All @@ -121,6 +142,34 @@ WWFC_DEFINE_PATCH = {
),
};

// Display the current status of the "Open Host" feature
WWFC_DEFINE_PATCH = {
Patch::CallWithCTR( //
WWFC_PATCH_LEVEL_FEATURE, //
RMCXD_PORT(0x8064CF48, 0x80619C34, 0x8064C5B4, 0x8063B260), //
// clang-format off
[]() -> void {
using namespace mkw::UI;

Section* section = SectionManager::Instance()->currentSection();
WifiFriendMenuPage* wifiFriendMenuPage =
section->page<WifiFriendMenuPage>(PageId::WifiFriendMenu);

FormatParam formatParam{};
if (OpenHostPage::IsOpenHostEnabled()) {
formatParam.strings[0] = L"Open Host is enabled.";
} else {
formatParam.strings[0] = L"Open Host is disabled.";
}

MenuInstructionTextControl& menuIntructionTextControl =
wifiFriendMenuPage->menuInstructionTextControl();
menuIntructionTextControl.setMessage(0x19CA, &formatParam);
}
// clang-format on
),
};

// Allow users to open rooms without having any friends added
WWFC_DEFINE_PATCH = {
Patch::CallWithCTR( //
Expand Down