shift to sead singleton for Client, new custom heap for Client usage

This commit is contained in:
CraftyBoss 2022-07-08 01:02:28 -07:00
parent 1df21b2a3e
commit 88de1dcecf
9 changed files with 101 additions and 52 deletions

View file

@ -35,7 +35,13 @@ class PuppetActor : public al::LiveActor {
virtual void makeActorDead(void) override; virtual void makeActorDead(void) override;
virtual void attackSensor(al::HitSensor *, al::HitSensor *) override; virtual void attackSensor(al::HitSensor *, al::HitSensor *) override;
virtual bool receiveMsg(const al::SensorMsg *, al::HitSensor *, al::HitSensor *) override; virtual bool receiveMsg(const al::SensorMsg*, al::HitSensor*, al::HitSensor*) override;
virtual const char* getName() const override {
if (mInfo)
return mInfo->puppetName;
return mActorName;
}
void initOnline(PuppetInfo *pupInfo); void initOnline(PuppetInfo *pupInfo);
@ -50,8 +56,6 @@ class PuppetActor : public al::LiveActor {
PuppetInfo* getInfo() { return mInfo; } PuppetInfo* getInfo() { return mInfo; }
const char *getPuppetName() { return mInfo->puppetName; }
bool addCapture(PuppetHackActor *capture, const char *hackType); bool addCapture(PuppetHackActor *capture, const char *hackType);
al::LiveActor* getCurrentModel(); al::LiveActor* getCurrentModel();

View file

@ -28,8 +28,10 @@ class PuppetHolder {
void clearPuppets() { mPuppetArr.clear(); } void clearPuppets() { mPuppetArr.clear(); }
bool resizeHolder(int size);
private: private:
sead::PtrArray<PuppetActor> mPuppetArr; sead::PtrArray<PuppetActor> mPuppetArr = sead::PtrArray<PuppetActor>();
PuppetActor *mDebugPuppet; PuppetActor *mDebugPuppet;

View file

@ -32,6 +32,7 @@
#include "game/GameData/GameDataHolderWriter.h" #include "game/GameData/GameDataHolderWriter.h"
#include "game/GameData/GameDataFunction.h" #include "game/GameData/GameDataFunction.h"
#include "heap/seadHeap.h"
#include "layouts/HideAndSeekIcon.h" #include "layouts/HideAndSeekIcon.h"
#include "rs/util.hpp" #include "rs/util.hpp"
@ -76,10 +77,10 @@ struct UIDIndexNode {
class HideAndSeekIcon; class HideAndSeekIcon;
class Client { class Client {
public: SEAD_SINGLETON_DISPOSER(Client)
static Client *sInstance;
Client(int bufferSize); public:
Client();
void init(al::LayoutInitInfo const &initInfo, GameDataHolderAccessor holder); void init(al::LayoutInitInfo const &initInfo, GameDataHolderAccessor holder);
@ -145,7 +146,7 @@ class Client {
static GameModeConfigMenu* tryCreateModeMenu(); static GameModeConfigMenu* tryCreateModeMenu();
static int getMaxPlayerCount() { return sInstance ? sInstance->maxPuppets : 10;} static int getMaxPlayerCount() { return sInstance ? sInstance->maxPuppets + 1 : 10;}
static void toggleCurrentMode(); static void toggleCurrentMode();
@ -204,8 +205,6 @@ class Client {
// public for debug purposes // public for debug purposes
SocketClient *mSocket; SocketClient *mSocket;
int maxPuppets;
private: private:
void updatePlayerInfo(PlayerInf *packet); void updatePlayerInfo(PlayerInf *packet);
void updateHackCapInfo(HackCapInf *packet); void updateHackCapInfo(HackCapInf *packet);
@ -287,6 +286,8 @@ class Client {
u8 mScenario = 0; u8 mScenario = 0;
sead::Heap *mHeap = nullptr; // Heap that Client::sInstance was created in
// --- Mode Info --- // --- Mode Info ---
GameModeBase* mCurMode = nullptr; GameModeBase* mCurMode = nullptr;
@ -299,6 +300,8 @@ class Client {
// --- Puppet Info --- // --- Puppet Info ---
int maxPuppets = 9; // default max player count is 10, so default max puppets will be 9
PuppetInfo *mPuppetInfoArr[MAXPUPINDEX]; PuppetInfo *mPuppetInfoArr[MAXPUPINDEX];
PuppetHolder *mPuppetHolder = nullptr; PuppetHolder *mPuppetHolder = nullptr;

View file

@ -37,7 +37,7 @@ void NameTag::appear() {
return; return;
} }
setText(mPuppet->getPuppetName()); setText(mPuppet->getName());
al::startAction(this, "Appear", 0); al::startAction(this, "Appear", 0);
LayoutActor::appear(); LayoutActor::appear();

View file

@ -88,9 +88,9 @@ void drawMainHook(HakoniwaSequence *curSequence, sead::Viewport *viewport, sead:
gTextWriter->setCursorFromTopLeft(sead::Vector2f(10.f, (dispHeight / 3) + 30.f)); gTextWriter->setCursorFromTopLeft(sead::Vector2f(10.f, (dispHeight / 3) + 30.f));
gTextWriter->setScaleFromFontHeight(20.f); gTextWriter->setScaleFromFontHeight(20.f);
gTextWriter->printf("Client Socket Connection Status: %s\n", Client::sInstance->mSocket->getStateChar()); gTextWriter->printf("Client Socket Connection Status: %s\n", Client::instance()->mSocket->getStateChar());
gTextWriter->printf("nn::socket::GetLastErrno: 0x%x\n", Client::sInstance->mSocket->socket_errno); gTextWriter->printf("nn::socket::GetLastErrno: 0x%x\n", Client::instance()->mSocket->socket_errno);
gTextWriter->printf("Packet Queue Length: %d\n", Client::sInstance->mSocket->mPacketQueue.size()); gTextWriter->printf("Packet Queue Length: %d\n", Client::instance()->mSocket->mPacketQueue.size());
gTextWriter->printf("Total Connected Players: %d\n", Client::getConnectCount() + 1); gTextWriter->printf("Total Connected Players: %d\n", Client::getConnectCount() + 1);
al::Scene *curScene = curSequence->curScene; al::Scene *curScene = curSequence->curScene;
@ -283,7 +283,7 @@ ulong constructHook() { // hook for constructing anything we need to globally b
: [result] "=r"( : [result] "=r"(
initInfo)); // Save our scenes init info to a gloabl ptr so we can access it later initInfo)); // Save our scenes init info to a gloabl ptr so we can access it later
Client::sInstance = new Client(playBufSize); Client::createInstance(al::getCurrentHeap());
return 0x20; return 0x20;
} }
@ -294,7 +294,7 @@ bool threadInit(HakoniwaSequence *mainSeq) { // hook for initializing client cl
al::initLayoutInitInfo(&lytInfo, mainSeq->mLytKit, 0, mainSeq->mAudioDirector, initInfo->mSystemInfo->mLayoutSys, initInfo->mSystemInfo->mMessageSys, initInfo->mSystemInfo->mGamePadSys); al::initLayoutInitInfo(&lytInfo, mainSeq->mLytKit, 0, mainSeq->mAudioDirector, initInfo->mSystemInfo->mLayoutSys, initInfo->mSystemInfo->mMessageSys, initInfo->mSystemInfo->mGamePadSys);
Client::sInstance->init(lytInfo, mainSeq->mGameDataHolder); Client::instance()->init(lytInfo, mainSeq->mGameDataHolder);
return GameDataFunction::isPlayDemoOpening(mainSeq->mGameDataHolder); return GameDataFunction::isPlayDemoOpening(mainSeq->mGameDataHolder);
} }
@ -344,9 +344,9 @@ bool hakoniwaSequenceHook(HakoniwaSequence* sequence) {
if (al::isPadTriggerRight(-1)) debugPuppetIndex++; if (al::isPadTriggerRight(-1)) debugPuppetIndex++;
if(debugPuppetIndex < 0) { if(debugPuppetIndex < 0) {
debugPuppetIndex = playBufSize - 2; debugPuppetIndex = Client::getMaxPlayerCount() - 2;
} }
if (debugPuppetIndex >= playBufSize - 1) if (debugPuppetIndex >= Client::getMaxPlayerCount() - 1)
debugPuppetIndex = 0; debugPuppetIndex = 0;
} }

View file

@ -227,6 +227,7 @@ void PuppetActor::makeActorAlive() {
} }
al::LiveActor::makeActorAlive(); al::LiveActor::makeActorAlive();
} }
} }

View file

@ -1,13 +1,52 @@
#include "puppets/PuppetHolder.hpp" #include "puppets/PuppetHolder.hpp"
#include <math.h>
#include "actors/PuppetActor.h"
#include "al/util.hpp" #include "al/util.hpp"
#include "container/seadPtrArray.h"
#include "heap/seadHeap.h"
#include "heap/seadHeapMgr.h"
#include "logger.hpp" #include "logger.hpp"
PuppetHolder::PuppetHolder(int size) { PuppetHolder::PuppetHolder(int size) {
mPuppetArr = sead::PtrArray<PuppetActor>();
if(!mPuppetArr.tryAllocBuffer(size, nullptr)) { if(!mPuppetArr.tryAllocBuffer(size, nullptr)) {
Logger::log("Buffer Alloc Failed on Puppet Holder!\n"); Logger::log("Buffer Alloc Failed on Puppet Holder!\n");
} }
} }
/**
* @brief resizes puppet ptr array by creating a new ptr array and storing previous ptrs in it, before freeing the previous array
*
* @param size the size of the new ptr array
* @return returns true if resizing was sucessful
*/
bool PuppetHolder::resizeHolder(int size) {
if (mPuppetArr.capacity() == size)
return true; // no need to resize if we're already at the same capacity
sead::Heap *seqHeap = sead::HeapMgr::instance()->findHeapByName("SequenceHeap", 0);
if (!mPuppetArr.isBufferReady())
return mPuppetArr.tryAllocBuffer(size, seqHeap);
sead::PtrArray<PuppetActor> newPuppets = sead::PtrArray<PuppetActor>();
if (newPuppets.tryAllocBuffer(size, seqHeap)) {
int curPupCount = mPuppetArr.size();
for (int i = 0; i < curPupCount > size ? size : curPupCount; i++) {
newPuppets.pushBack(mPuppetArr[i]);
}
mPuppetArr.freeBuffer();
mPuppetArr = newPuppets;
return true;
} else {
return false;
}
}
bool PuppetHolder::tryRegisterPuppet(PuppetActor *puppet) { bool PuppetHolder::tryRegisterPuppet(PuppetActor *puppet) {
if(!mPuppetArr.isFull()) { if(!mPuppetArr.isFull()) {

View file

@ -60,7 +60,7 @@ void initPuppetActors(al::Scene *scene, al::ActorInitInfo const &rootInfo, char
al::PlacementInfo playerPlacement = al::PlacementInfo(); al::PlacementInfo playerPlacement = al::PlacementInfo();
al::getPlacementInfoByIndex(&playerPlacement, rootPlacement, 0); al::getPlacementInfoByIndex(&playerPlacement, rootPlacement, 0);
for (size_t i = 0; i < Client::sInstance->maxPuppets; i++) for (size_t i = 0; i < Client::getMaxPlayerCount(); i++)
{ {
createPuppetActorFromFactory(rootInfo, playerPlacement, false); createPuppetActorFromFactory(rootInfo, playerPlacement, false);
} }

View file

@ -12,6 +12,9 @@
#include "game/Player/PlayerActorHakoniwa.h" #include "game/Player/PlayerActorHakoniwa.h"
#include "game/SaveData/SaveDataAccessFunction.h" #include "game/SaveData/SaveDataAccessFunction.h"
#include "game/StageScene/StageScene.h" #include "game/StageScene/StageScene.h"
#include "heap/seadDisposer.h"
#include "heap/seadHeap.h"
#include "heap/seadFrameHeap.h"
#include "heap/seadHeapMgr.h" #include "heap/seadHeapMgr.h"
#include "helpers.hpp" #include "helpers.hpp"
#include "layouts/HideAndSeekIcon.h" #include "layouts/HideAndSeekIcon.h"
@ -35,7 +38,7 @@
#include "server/HideAndSeekConfigMenu.hpp" #include "server/HideAndSeekConfigMenu.hpp"
#include "server/HideAndSeekMode.hpp" #include "server/HideAndSeekMode.hpp"
Client* Client::sInstance; SEAD_SINGLETON_DISPOSER_IMPL(Client)
typedef void (Client::*ClientThreadFunc)(void); typedef void (Client::*ClientThreadFunc)(void);
@ -44,21 +47,21 @@ typedef void (Client::*ClientThreadFunc)(void);
* *
* @param bufferSize defines the maximum amount of puppets the client can handle * @param bufferSize defines the maximum amount of puppets the client can handle
*/ */
Client::Client(int bufferSize) { Client::Client() {
mHeap = sead::FrameHeap::create(0x100000, "ClientHeap", sead::HeapMgr::instance()->getCurrentHeap(), 8, sead::Heap::cHeapDirection_Forward, false);
sead::ScopedCurrentHeapSetter heapSetter(mHeap); // every new call after this will use ClientHeap instead of SequenceHeap
this->mReadThread = new al::AsyncFunctorThread("ClientReadThread", al::FunctorV0M<Client*, ClientThreadFunc>(this, &Client::readFunc), 0, 0x10000, {0}); this->mReadThread = new al::AsyncFunctorThread("ClientReadThread", al::FunctorV0M<Client*, ClientThreadFunc>(this, &Client::readFunc), 0, 0x10000, {0});
// this->recvThread = new al::AsyncFunctorThread("ClientRecvThread",
// al::FunctorV0M<typeof(this), typeof(&Client::recvFunc)>(this, &Client::recvFunc), 0, 0x10000,
// {0});
mKeyboard = new Keyboard(nn::swkbd::GetRequiredStringBufferSize()); mKeyboard = new Keyboard(nn::swkbd::GetRequiredStringBufferSize());
mSocket = new SocketClient("SocketClient"); mSocket = new SocketClient("SocketClient");
maxPuppets = bufferSize - 1;
mPuppetHolder = new PuppetHolder(maxPuppets); mPuppetHolder = new PuppetHolder(maxPuppets);
for (size_t i = 0; i < bufferSize + 1; i++) for (size_t i = 0; i < maxPuppets; i++)
{ {
mPuppetInfoArr[i] = new PuppetInfo(); mPuppetInfoArr[i] = new PuppetInfo();
@ -83,6 +86,7 @@ Client::Client(int bufferSize) {
nn::account::Nickname playerName; nn::account::Nickname playerName;
nn::account::GetNickname(&playerName, mUserID); nn::account::GetNickname(&playerName, mUserID);
Logger::setLogName(playerName.name); // set Debug logger name to player name
mUsername = playerName.name; mUsername = playerName.name;
@ -90,13 +94,8 @@ Client::Client(int bufferSize) {
Logger::log("%s Build Number: %s\n", playerName.name, TOSTRING(BUILDVERSTR)); Logger::log("%s Build Number: %s\n", playerName.name, TOSTRING(BUILDVERSTR));
Logger::setLogName(playerName.name); // set Debug logger name to player name mServerMode = GameMode::HIDEANDSEEK; // temp for testing
mServerMode = GameMode::HIDEANDSEEK; // temp for testing
if(!sInstance) {
sInstance = this;
}
} }
/** /**
@ -106,7 +105,7 @@ Client::Client(int bufferSize) {
*/ */
void Client::init(al::LayoutInitInfo const &initInfo, GameDataHolderAccessor holder) { void Client::init(al::LayoutInitInfo const &initInfo, GameDataHolderAccessor holder) {
mConnectionWait = new al::WindowConfirmWait("ServerWaitConnect", "WindowConfirmWait", initInfo); mConnectionWait = new (mHeap) al::WindowConfirmWait("ServerWaitConnect", "WindowConfirmWait", initInfo);
mConnectionWait->setTxtMessage(u"Connecting to Server."); mConnectionWait->setTxtMessage(u"Connecting to Server.");
@ -115,6 +114,7 @@ void Client::init(al::LayoutInitInfo const &initInfo, GameDataHolderAccessor hol
mHolder = holder; mHolder = holder;
StartThreads(); StartThreads();
} }
/** /**
@ -352,7 +352,7 @@ void Client::readFunc() {
waitForGameInit = false; waitForGameInit = false;
} }
// we can use the start of readFunc to display an al::WindowConfirmWait while the server // we can use the start of readFunc to display an al::WindowConfirmWait while the client
// connects // connects
mConnectionWait->appear(); mConnectionWait->appear();
@ -1094,7 +1094,7 @@ void Client::setStageInfo(GameDataHolderAccessor holder) {
sInstance->mStageName = GameDataFunction::getCurrentStageName(holder); sInstance->mStageName = GameDataFunction::getCurrentStageName(holder);
sInstance->mScenario = holder.mData->mGameDataFile->getScenarioNo(); //holder.mData->mGameDataFile->getMainScenarioNoCurrent(); sInstance->mScenario = holder.mData->mGameDataFile->getScenarioNo(); //holder.mData->mGameDataFile->getMainScenarioNoCurrent();
Client::sInstance->mPuppetHolder->setStageInfo(sInstance->mStageName.cstr(), sInstance->mScenario); sInstance->mPuppetHolder->setStageInfo(sInstance->mStageName.cstr(), sInstance->mScenario);
} }
} }
@ -1106,8 +1106,8 @@ void Client::setStageInfo(GameDataHolderAccessor holder) {
* @return false * @return false
*/ */
bool Client::tryAddPuppet(PuppetActor *puppet) { bool Client::tryAddPuppet(PuppetActor *puppet) {
if(Client::sInstance) { if(sInstance) {
return Client::sInstance->mPuppetHolder->tryRegisterPuppet(puppet); return sInstance->mPuppetHolder->tryRegisterPuppet(puppet);
}else { }else {
return false; return false;
} }
@ -1121,8 +1121,8 @@ bool Client::tryAddPuppet(PuppetActor *puppet) {
* @return false * @return false
*/ */
bool Client::tryAddDebugPuppet(PuppetActor *puppet) { bool Client::tryAddDebugPuppet(PuppetActor *puppet) {
if(Client::sInstance) { if(sInstance) {
return Client::sInstance->mPuppetHolder->tryRegisterDebugPuppet(puppet); return sInstance->mPuppetHolder->tryRegisterDebugPuppet(puppet);
}else { }else {
return false; return false;
} }
@ -1135,8 +1135,8 @@ bool Client::tryAddDebugPuppet(PuppetActor *puppet) {
* @return PuppetActor* * @return PuppetActor*
*/ */
PuppetActor *Client::getPuppet(int idx) { PuppetActor *Client::getPuppet(int idx) {
if(Client::sInstance) { if(sInstance) {
return Client::sInstance->mPuppetHolder->getPuppetActor(idx); return sInstance->mPuppetHolder->getPuppetActor(idx);
}else { }else {
return nullptr; return nullptr;
} }
@ -1148,8 +1148,8 @@ PuppetActor *Client::getPuppet(int idx) {
* @return PuppetInfo* * @return PuppetInfo*
*/ */
PuppetInfo *Client::getLatestInfo() { PuppetInfo *Client::getLatestInfo() {
if(Client::sInstance) { if(sInstance) {
return Client::getPuppetInfo(Client::sInstance->mPuppetHolder->getSize() - 1); return Client::getPuppetInfo(sInstance->mPuppetHolder->getSize() - 1);
}else { }else {
return nullptr; return nullptr;
} }
@ -1162,9 +1162,9 @@ PuppetInfo *Client::getLatestInfo() {
* @return PuppetInfo* * @return PuppetInfo*
*/ */
PuppetInfo *Client::getPuppetInfo(int idx) { PuppetInfo *Client::getPuppetInfo(int idx) {
if(Client::sInstance) { if(sInstance) {
// unsafe get // unsafe get
PuppetInfo *curInfo = Client::sInstance->mPuppetInfoArr[idx]; PuppetInfo *curInfo = sInstance->mPuppetInfoArr[idx];
if (!curInfo) { if (!curInfo) {
Logger::log("Attempting to Access Puppet Out of Bounds! Value: %d\n", idx); Logger::log("Attempting to Access Puppet Out of Bounds! Value: %d\n", idx);
@ -1317,8 +1317,8 @@ void Client::clearArrays() {
* @return PuppetInfo* * @return PuppetInfo*
*/ */
PuppetInfo *Client::getDebugPuppetInfo() { PuppetInfo *Client::getDebugPuppetInfo() {
if(Client::sInstance) { if(sInstance) {
return &Client::sInstance->mDebugPuppetInfo; return &sInstance->mDebugPuppetInfo;
}else { }else {
return nullptr; return nullptr;
} }
@ -1330,8 +1330,8 @@ PuppetInfo *Client::getDebugPuppetInfo() {
* @return PuppetActor* * @return PuppetActor*
*/ */
PuppetActor *Client::getDebugPuppet() { PuppetActor *Client::getDebugPuppet() {
if(Client::sInstance) { if(sInstance) {
return Client::sInstance->mPuppetHolder->getDebugPuppet(); return sInstance->mPuppetHolder->getDebugPuppet();
}else { }else {
return nullptr; return nullptr;
} }