Improve keyboard support and add port selection

This commit is contained in:
Sanae 2022-06-18 18:48:37 -06:00
parent b71a6fc27b
commit 7c7b06542b
7 changed files with 128 additions and 33 deletions

View file

@ -9,12 +9,14 @@
#include "logger.hpp"
#include "sead/prim/seadSafeString.h"
typedef void (*KeyboardSetup)(nn::swkbd::KeyboardConfig&);
class Keyboard {
public:
Keyboard(ulong strSize);
void keyboardThread();
void openKeyboard(const char* initialText);
void openKeyboard(const char* initialText, KeyboardSetup setup);
const char* getResult() {
if (mThread->isDone()) {
@ -36,6 +38,7 @@ class Keyboard {
bool mIsDoneKeyboard;
sead::FixedSafeString<0x10> mInitialText;
KeyboardSetup mSetupFunc;
const char16_t *mHeaderText = u"Enter Server IP Here!";
const char16_t *mSubText = u"Must be a Valid Address.";

View file

@ -29,7 +29,8 @@ class StageSceneStateServerConfig : public al::HostStateBase<al::Scene>, public
GAMEMODECONFIG,
GAMEMODESWITCH,
RECONNECT,
SETIP
SETIP,
SETPORT
};
virtual al::MessageSystem* getMessageSystem(void) const override;
@ -38,7 +39,8 @@ class StageSceneStateServerConfig : public al::HostStateBase<al::Scene>, public
virtual void kill(void) override;
void exeMainMenu();
void exeOpenKeyboard();
void exeOpenKeyboardIP();
void exeOpenKeyboardPort();
void exeRestartServer();
void exeGamemodeConfig();
void exeGamemodeSelect();
@ -75,7 +77,8 @@ class StageSceneStateServerConfig : public al::HostStateBase<al::Scene>, public
namespace {
NERVE_HEADER(StageSceneStateServerConfig, MainMenu)
NERVE_HEADER(StageSceneStateServerConfig, OpenKeyboard)
NERVE_HEADER(StageSceneStateServerConfig, OpenKeyboardIP)
NERVE_HEADER(StageSceneStateServerConfig, OpenKeyboardPort)
NERVE_HEADER(StageSceneStateServerConfig, RestartServer)
NERVE_HEADER(StageSceneStateServerConfig, GamemodeConfig)
NERVE_HEADER(StageSceneStateServerConfig, GamemodeSelect)

View file

@ -150,10 +150,16 @@ class Client {
static void clearArrays();
static Keyboard* getKeyboard();
static const char* getCurrentIP();
static void setLastUsedIP(const char* ip);
static const int getCurrentPort();
static void setLastUsedPort(const int port);
static void setTagState(bool state);
static int getConnectCount() {
@ -171,6 +177,7 @@ class Client {
static void updateShines();
static void openKeyboardIP();
static void openKeyboardPort();
static GameModeInfoBase* getModeInfo() {
return sInstance ? sInstance->mModeInfo : nullptr;
@ -243,7 +250,7 @@ class Client {
sead::FixedSafeString<0x10> mServerIP;
int mServerPort = 1027; // TODO: implement a way to set this the same way the IP can
int mServerPort = 0;
bool isFirstConnect = true;

View file

@ -27,12 +27,7 @@ void Keyboard::keyboardThread() {
nn::swkbd::ShowKeyboardArg keyboardArg = nn::swkbd::ShowKeyboardArg();
nn::swkbd::MakePreset(&keyboardArg.keyboardConfig, nn::swkbd::Preset::Default);
keyboardArg.keyboardConfig.keyboardMode = nn::swkbd::KeyboardMode::ModeNumeric;
keyboardArg.keyboardConfig.leftOptionalSymbolKey = '.';
keyboardArg.keyboardConfig.textMaxLength = 15;
keyboardArg.keyboardConfig.textMinLength = 1;
keyboardArg.keyboardConfig.isUseUtf8 = true;
keyboardArg.keyboardConfig.inputFormMode = nn::swkbd::InputFormMode::OneLine;
mSetupFunc(keyboardArg.keyboardConfig);
nn::swkbd::SetHeaderText(&keyboardArg.keyboardConfig, mHeaderText);
nn::swkbd::SetSubText(&keyboardArg.keyboardConfig, mSubText);
@ -55,9 +50,10 @@ void Keyboard::keyboardThread() {
}
void Keyboard::openKeyboard(const char* initialText) {
void Keyboard::openKeyboard(const char* initialText, KeyboardSetup setupFunc) {
mInitialText = initialText;
mSetupFunc = setupFunc;
mThread->start();
}

View file

@ -39,6 +39,7 @@ bool comboBtnHook(int port) {
void saveWriteHook(al::ByamlWriter* saveByml) {
const char *serverIP = Client::getCurrentIP();
const int serverPort = Client::getCurrentPort();
if (serverIP) {
saveByml->addString("ServerIP", serverIP);
@ -46,17 +47,28 @@ void saveWriteHook(al::ByamlWriter* saveByml) {
saveByml->addString("ServerIP", "0.0.0.0");
}
if (serverPort) {
saveByml->addInt("ServerPort", serverPort);
} else {
saveByml->addInt("ServerPort", 0);
}
saveByml->pop();
}
bool saveReadHook(int* padRumbleInt, al::ByamlIter const& saveByml, char const* padRumbleKey) {
const char *serverIP = "";
int serverPort = 0;
if (al::tryGetByamlString(&serverIP, saveByml, "ServerIP")) {
Client::setLastUsedIP(serverIP);
}
if (al::tryGetByamlS32(&serverPort, saveByml, "ServerPort")) {
Client::setLastUsedPort(serverPort);
}
return al::tryGetByamlS32(padRumbleInt, saveByml, padRumbleKey);
}

View file

@ -16,6 +16,8 @@
#include "math/seadVector.h"
#include "nn/err.h"
#include "nn/result.h"
#include "nn/swkbd/swkbd.h"
#include "nn/util.h"
#include "packets/ChangeStagePacket.h"
#include "packets/InitPacket.h"
#include "packets/Packet.h"
@ -200,19 +202,17 @@ void Client::stopConnection() {
*/
bool Client::startConnection() {
if (mServerIP.isEmpty()) {
mKeyboard->setHeaderText(u"Save File does not contain an IP!");
mKeyboard->setSubText(u"Please set a Server IP Below.");
mKeyboard->openKeyboard("0.0.0.0");
while (true) {
if (mKeyboard->isThreadDone()) {
mServerIP = mKeyboard->getResult();
break;
}
nn::os::YieldThread(); // allow other threads to run
mServerIP = "0.0.0.0";
Client::openKeyboardIP();
}
if (!mServerPort) {
mKeyboard->setHeaderText(u"Save File does not contain a port!");
mKeyboard->setSubText(u"Please set a Server Port Below.");
mServerPort = 1027;
Client::openKeyboardPort();
}
bool result = mSocket->init(mServerIP.cstr(), mServerPort).isSuccess();
@ -260,7 +260,15 @@ void Client::openKeyboardIP() {
return;
}
sInstance->mKeyboard->openKeyboard(sInstance->mServerIP.cstr()); // opens swkbd with the initial text set to the last saved IP
// opens swkbd with the initial text set to the last saved IP
sInstance->mKeyboard->openKeyboard(sInstance->mServerIP.cstr(), [] (nn::swkbd::KeyboardConfig& config) {
config.keyboardMode = nn::swkbd::KeyboardMode::ModeNumeric;
config.leftOptionalSymbolKey = '.';
config.textMaxLength = 15;
config.textMinLength = 1;
config.isUseUtf8 = true;
config.inputFormMode = nn::swkbd::InputFormMode::OneLine;
});
while (true) {
if (sInstance->mKeyboard->isThreadDone()) {
@ -271,6 +279,38 @@ void Client::openKeyboardIP() {
}
}
/**
* @brief Opens up OS's software keyboard in order to change the currently used server port.
*
*/
void Client::openKeyboardPort() {
if (!sInstance) {
Logger::log("Static Instance is null!\n");
return;
}
// opens swkbd with the initial text set to the last saved port
char buf[6];
nn::util::SNPrintf(buf, 6, "%u", sInstance->mServerPort);
sInstance->mKeyboard->openKeyboard(buf, [] (nn::swkbd::KeyboardConfig& config) {
config.keyboardMode = nn::swkbd::KeyboardMode::ModeNumeric;
config.textMaxLength = 5;
config.textMinLength = 2;
config.isUseUtf8 = true;
config.inputFormMode = nn::swkbd::InputFormMode::OneLine;
});
while (true) {
if (sInstance->mKeyboard->isThreadDone()) {
sInstance->mServerPort = ::atoi(sInstance->mKeyboard->getResult());
break;
}
nn::os::YieldThread(); // allow other threads to run
}
}
/**
* @brief main thread function for read thread, responsible for receiving and processing packets from server
*
@ -1297,6 +1337,18 @@ PuppetActor *Client::getDebugPuppet() {
}
}
/**
* @brief
*
* @return Keyboard*
*/
Keyboard* Client::getKeyboard() {
if (sInstance) {
return sInstance->mKeyboard;
}
return nullptr;
}
/**
* @brief
*

View file

@ -28,15 +28,16 @@ StageSceneStateServerConfig::StageSceneStateServerConfig(const char *name, al::S
al::setPaneString(mMainOptions, "TxtOption", u"Server Configuration", 0);
mMainOptionsList->initDataNoResetSelected(4);
mMainOptionsList->initDataNoResetSelected(5);
sead::SafeArray<sead::WFixedSafeString<0x200>, 4>* mainMenuOptions =
new sead::SafeArray<sead::WFixedSafeString<0x200>, 4>();
sead::SafeArray<sead::WFixedSafeString<0x200>, 5>* mainMenuOptions =
new sead::SafeArray<sead::WFixedSafeString<0x200>, 5>();
mainMenuOptions->mBuffer[0].copy(u"Gamemode Config");
mainMenuOptions->mBuffer[1].copy(u"Change Gamemode");
mainMenuOptions->mBuffer[2].copy(u"Reconnect to Server");
mainMenuOptions->mBuffer[3].copy(u"Change Server IP");
mainMenuOptions->mBuffer[ServerConfigOption::GAMEMODECONFIG].copy(u"Gamemode Config");
mainMenuOptions->mBuffer[ServerConfigOption::GAMEMODESWITCH].copy(u"Change Gamemode");
mainMenuOptions->mBuffer[ServerConfigOption::RECONNECT].copy(u"Reconnect to Server");
mainMenuOptions->mBuffer[ServerConfigOption::SETIP].copy(u"Change Server IP");
mainMenuOptions->mBuffer[ServerConfigOption::SETPORT].copy(u"Change Server Port");
mMainOptionsList->addStringData(mainMenuOptions->mBuffer, "TxtContent");
@ -147,7 +148,11 @@ void StageSceneStateServerConfig::exeMainMenu() {
break;
}
case ServerConfigOption::SETIP: {
al::setNerve(this, &nrvStageSceneStateServerConfigOpenKeyboard);
al::setNerve(this, &nrvStageSceneStateServerConfigOpenKeyboardIP);
break;
}
case ServerConfigOption::SETPORT: {
al::setNerve(this, &nrvStageSceneStateServerConfigOpenKeyboardPort);
break;
}
default:
@ -157,11 +162,27 @@ void StageSceneStateServerConfig::exeMainMenu() {
}
}
void StageSceneStateServerConfig::exeOpenKeyboard() {
void StageSceneStateServerConfig::exeOpenKeyboardIP() {
if (al::isFirstStep(this)) {
mCurrentList->deactivate();
Client::getKeyboard()->setHeaderText(u"Set a Server IP Below.");
Client::getKeyboard()->setSubText(u"");
Client::openKeyboardIP();
// anything that happens after this will be ran after the keyboard closes
al::startHitReaction(mCurrentMenu, "リセット", 0);
al::setNerve(this, &nrvStageSceneStateServerConfigMainMenu);
}
}
void StageSceneStateServerConfig::exeOpenKeyboardPort() {
if (al::isFirstStep(this)) {
mCurrentList->deactivate();
Client::getKeyboard()->setHeaderText(u"Set a Server Port Below.");
Client::getKeyboard()->setSubText(u"");
Client::openKeyboardIP();
// anything that happens after this will be ran after the keyboard closes
al::startHitReaction(mCurrentMenu, "リセット", 0);
@ -272,7 +293,8 @@ void StageSceneStateServerConfig::subMenuUpdate() {
namespace {
NERVE_IMPL(StageSceneStateServerConfig, MainMenu)
NERVE_IMPL(StageSceneStateServerConfig, OpenKeyboard)
NERVE_IMPL(StageSceneStateServerConfig, OpenKeyboardIP)
NERVE_IMPL(StageSceneStateServerConfig, OpenKeyboardPort)
NERVE_IMPL(StageSceneStateServerConfig, RestartServer)
NERVE_IMPL(StageSceneStateServerConfig, GamemodeConfig)
NERVE_IMPL(StageSceneStateServerConfig, GamemodeSelect)