Skip to content

Commit

Permalink
chat; even with 7241a2a, from PR 217
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilNarayana committed Nov 1, 2021
1 parent e689275 commit 84bded8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
50 changes: 50 additions & 0 deletions Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern bool g_needInputForFrame;

#ifdef LOCAL_TESTING
bool isLocalConnected = false;
int localChatMessageId = 0;
#endif

namespace ExpansionInterface
Expand Down Expand Up @@ -1884,6 +1885,26 @@ void CEXISlippi::prepareOnlineMatchState()
u32 rngOffset = 0;
std::string p1Name = "";
std::string p2Name = "";
u8 chatMessageId = 0;
u8 sentChatMessageId = 0;

#ifdef LOCAL_TESTING
chatMessageId = localChatMessageId;
localChatMessageId = 0;
// in CSS p1 is always current player and p2 is opponent
p1Name = "Player 1";
p2Name = "Player 2";
#endif

// Set chat message if any
if (slippi_netplay)
{
chatMessageId = slippi_netplay->GetSlippiRemoteChatMessage();
sentChatMessageId = slippi_netplay->GetSlippiRemoteSentChatMessage();
// in CSS p1 is always current player and p2 is opponent
p1Name = userInfo.display_name;
p2Name = oppName;
}

auto directMode = SlippiMatchmaking::OnlinePlayMode::DIRECT;

Expand Down Expand Up @@ -1969,6 +1990,10 @@ void CEXISlippi::prepareOnlineMatchState()
// Add delay frames to output
m_read_queue.push_back((u8)SConfig::GetInstance().m_slippiOnlineDelay);

// Add chat messages id
m_read_queue.push_back((u8)sentChatMessageId);
m_read_queue.push_back((u8)chatMessageId);

// Add names to output
p1Name = ConvertStringForGame(p1Name, MAX_NAME_LENGTH);
m_read_queue.insert(m_read_queue.end(), p1Name.begin(), p1Name.end());
Expand Down Expand Up @@ -2073,6 +2098,28 @@ void CEXISlippi::prepareFileLoad(u8* payload)
m_read_queue.insert(m_read_queue.end(), buf.begin(), buf.end());
}

void CEXISlippi::handleChatMessage(u8 *payload)
{

int messageId = payload[0];
INFO_LOG(SLIPPI, "SLIPPI CHAT INPUT: 0x%x", messageId);

#ifdef LOCAL_TESTING
localChatMessageId = 11;
#endif

if (slippi_netplay)
{

auto userInfo = user->GetUserInfo();
auto packet = std::make_unique<sf::Packet>();
// OSD::AddMessage("[Me]: "+ msg, OSD::Duration::VERY_LONG, OSD::Color::YELLOW);
slippi_netplay->remoteSentChatMessageId = messageId;
slippi_netplay->WriteChatMessageToPacket(*packet, messageId);
slippi_netplay->SendAsync(std::move(packet));
}
}

void CEXISlippi::logMessageFromGame(u8* payload)
{
if (payload[0] == 0)
Expand Down Expand Up @@ -2315,6 +2362,9 @@ void CEXISlippi::DMAWrite(u32 _uAddr, u32 _uSize)
case CMD_LOG_MESSAGE:
logMessageFromGame(&memPtr[bufLoc + 1]);
break;
case CMD_SEND_CHAT_MESSAGE:
handleChatMessage(&memPtr[bufLoc + 1]);
break;
case CMD_UPDATE:
handleUpdateAppRequest();
break;
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/Core/HW/EXI/EXI_DeviceSlippi.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class CEXISlippi : public IEXIDevice
CMD_UPDATE = 0xB8,
CMD_GET_ONLINE_STATUS = 0xB9,
CMD_CLEANUP_CONNECTION = 0xBA,
CMD_SEND_CHAT_MESSAGE = 0xBB,
CMD_GET_NEW_SEED = 0xBC,

// Misc
Expand Down Expand Up @@ -108,6 +109,7 @@ class CEXISlippi : public IEXIDevice
{CMD_GET_MATCH_STATE, 0},
{CMD_FIND_OPPONENT, 19},
{CMD_SET_MATCH_SELECTIONS, 6},
{CMD_SEND_CHAT_MESSAGE, 2},
{CMD_OPEN_LOGIN, 0},
{CMD_LOGOUT, 0},
{CMD_UPDATE, 0},
Expand Down Expand Up @@ -179,6 +181,7 @@ class CEXISlippi : public IEXIDevice
void prepareIsFileReady();

// misc stuff
void handleChatMessage(u8 *payload);
void logMessageFromGame(u8* payload);
void prepareFileLength(u8* payload);
void prepareFileLoad(u8* payload);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/NetPlayProto.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ enum
NP_MSG_SLIPPI_PAD_ACK = 0x81,
NP_MSG_SLIPPI_MATCH_SELECTIONS = 0x82,
NP_MSG_SLIPPI_CONN_SELECTED = 0x83,
NP_MSG_SLIPPI_CHAT_MESSAGE = 0x84,

NP_MSG_GOLF_REQUEST = 0x90,
NP_MSG_GOLF_SWITCH = 0x91,
Expand Down
42 changes: 42 additions & 0 deletions Source/Core/Core/Slippi/SlippiNetplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ unsigned int SlippiNetplayClient::OnData(sf::Packet& packet)
}
break;

case NetPlay::NP_MSG_SLIPPI_CHAT_MESSAGE:
{
auto playerSelection = ReadChatMessageFromPacket(packet);
auto messageId = playerSelection->messageId;

// set message id to netplay instance
remoteChatMessageId = messageId;
// Show chat message OSD
INFO_LOG(SLIPPI_ONLINE, "[Netplay] Received chat message from opponent %i", messageId);
}
break;

case NetPlay::NP_MSG_SLIPPI_CONN_SELECTED:
{
// Currently this is unused but the intent is to support two-way simultaneous connection
Expand All @@ -295,6 +307,22 @@ void SlippiNetplayClient::writeToPacket(sf::Packet& packet, SlippiPlayerSelectio
packet << s.rngOffset;
}

void SlippiNetplayClient::WriteChatMessageToPacket(sf::Packet& packet, int messageId)
{
packet << static_cast<NetPlay::MessageId>(NetPlay::NP_MSG_SLIPPI_CHAT_MESSAGE);
packet << messageId;
}

std::unique_ptr<SlippiPlayerSelections>
SlippiNetplayClient::ReadChatMessageFromPacket(sf::Packet& packet)
{
auto s = std::make_unique<SlippiPlayerSelections>();

packet >> s->messageId;

return std::move(s);
}

std::unique_ptr<SlippiPlayerSelections>
SlippiNetplayClient::readSelectionsFromPacket(sf::Packet& packet)
{
Expand Down Expand Up @@ -627,6 +655,20 @@ void SlippiNetplayClient::SetMatchSelections(SlippiPlayerSelections& s)
SendAsync(std::move(spac));
}

u8 SlippiNetplayClient::GetSlippiRemoteChatMessage()
{
u8 copiedMessageId = remoteChatMessageId;
remoteChatMessageId = 0; // Clear it out
return copiedMessageId;
}

u8 SlippiNetplayClient::GetSlippiRemoteSentChatMessage()
{
u8 copiedMessageId = remoteSentChatMessageId;
remoteSentChatMessageId = 0; // Clear it out
return copiedMessageId;
}

std::unique_ptr<SlippiRemotePadOutput> SlippiNetplayClient::GetSlippiRemotePad(int32_t curFrame)
{
std::lock_guard<std::mutex> lk(pad_mutex); // TODO: Is this the correct lock?
Expand Down
12 changes: 11 additions & 1 deletion Source/Core/Core/Slippi/SlippiNetplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class SlippiPlayerSelections

u32 rngOffset = 0;

int messageId;

void Merge(SlippiPlayerSelections& s)
{
this->rngOffset = s.rngOffset;
Expand Down Expand Up @@ -123,9 +125,17 @@ class SlippiNetplayClient
std::unique_ptr<SlippiRemotePadOutput> GetSlippiRemotePad(int32_t curFrame);
SlippiMatchInfo* GetMatchInfo();
u64 GetSlippiPing();
int32_t GetSlippiLatestRemoteFrame();
s32 GetSlippiLatestRemoteFrame();
u8 GetSlippiRemoteChatMessage();
u8 GetSlippiRemoteSentChatMessage();
s32 CalcTimeOffsetUs();

void WriteChatMessageToPacket(sf::Packet &packet, int messageId);
std::unique_ptr<SlippiPlayerSelections> ReadChatMessageFromPacket(sf::Packet &packet);

u8 remoteChatMessageId = 0; // most recent chat message id from opponent
u8 remoteSentChatMessageId = 0; // most recent chat message id that current player sent

protected:
struct
{
Expand Down

0 comments on commit 84bded8

Please sign in to comment.