diff --git a/lib/coopnet/include/libcoopnet.h b/lib/coopnet/include/libcoopnet.h index 15357b53e..738c96abf 100644 --- a/lib/coopnet/include/libcoopnet.h +++ b/lib/coopnet/include/libcoopnet.h @@ -28,7 +28,7 @@ typedef struct { void (*OnConnected)(uint64_t aUserId); void (*OnDisconnected)(void); void (*OnLobbyCreated)(uint64_t aLobbyId, const char* aGame, const char* aVersion, const char* aHostName, const char* aMode, uint16_t aMaxConnections); - void (*OnLobbyJoined)(uint64_t aLobbyId, uint64_t aUserId, uint64_t aOwnerId); + void (*OnLobbyJoined)(uint64_t aLobbyId, uint64_t aUserId, uint64_t aOwnerId, uint64_t aDestId); void (*OnLobbyLeft)(uint64_t aLobbyId, uint64_t aUserId); void (*OnLobbyListGot)(uint64_t aLobbyId, uint64_t aOwnerId, uint16_t aConnections, uint16_t aMaxConnections, const char* aGame, const char* aVersion, const char* aHostName, const char* aMode); void (*OnLobbyListFinish)(void); diff --git a/lib/coopnet/linux/libcoopnet.a b/lib/coopnet/linux/libcoopnet.a index c4de1e65a..e6b1d9c00 100644 Binary files a/lib/coopnet/linux/libcoopnet.a and b/lib/coopnet/linux/libcoopnet.a differ diff --git a/src/pc/network/coopnet/coopnet.c b/src/pc/network/coopnet/coopnet.c index 9bdec2b7e..511602103 100644 --- a/src/pc/network/coopnet/coopnet.c +++ b/src/pc/network/coopnet/coopnet.c @@ -53,11 +53,18 @@ static void coopnet_on_receive(uint64_t userId, const uint8_t* data, uint64_t da network_receive(localIndex, &userId, (u8*)data, dataLength); } -static void coopnet_on_lobby_joined(uint64_t lobbyId, uint64_t userId, uint64_t ownerId) { +static void coopnet_on_lobby_joined(uint64_t lobbyId, uint64_t userId, uint64_t ownerId, uint64_t destId) { LOG_INFO("coopnet_on_lobby_joined!"); coopnet_set_user_id(0, ownerId); sLocalLobbyId = lobbyId; sLocalLobbyOwnerId = ownerId; + + if (userId == coopnet_get_local_user_id()) { + coopnet_clear_dest_ids(); + } + + coopnet_save_dest_id(userId, destId); + if (userId == coopnet_get_local_user_id() && gNetworkType == NT_CLIENT) { network_send_mod_list_request(); } @@ -65,6 +72,7 @@ static void coopnet_on_lobby_joined(uint64_t lobbyId, uint64_t userId, uint64_t static void coopnet_on_lobby_left(uint64_t lobbyId, uint64_t userId) { LOG_INFO("coopnet_on_lobby_left!"); + coopnet_clear_dest_id(userId); if (lobbyId == sLocalLobbyId && userId == coopnet_get_local_user_id()) { network_shutdown(false, false, true); } @@ -78,11 +86,13 @@ static bool ns_coopnet_initialize(enum NetworkType networkType) { } static char* ns_coopnet_get_id_str(u8 localIndex) { - static char id_str[22] = { 0 }; + static char id_str[32] = { 0 }; if (localIndex == UNKNOWN_LOCAL_INDEX) { - snprintf(id_str, 22, "???"); + snprintf(id_str, 32, "???"); } else { - snprintf(id_str, 22, "%lld", (long long int)ns_coopnet_get_id(localIndex)); + uint64_t userId = ns_coopnet_get_id(localIndex); + uint64_t destId = coopnet_get_dest_id(userId); + snprintf(id_str, 32, "%" PRIu64 "", destId); } return id_str; } diff --git a/src/pc/network/coopnet/coopnet_id.c b/src/pc/network/coopnet/coopnet_id.c index 699c9801e..c7c790859 100644 --- a/src/pc/network/coopnet/coopnet_id.c +++ b/src/pc/network/coopnet/coopnet_id.c @@ -6,6 +6,54 @@ static uint64_t sLocalUserId = 0; static uint64_t sNetworkUserIds[MAX_PLAYERS] = { 0 }; +#define MAX_DEST_IDS (MAX_PLAYERS * 2) +struct DestinationId { + uint64_t userId; + uint64_t destId; +}; +struct DestinationId sDestinationIds[MAX_DEST_IDS] = { 0 }; + +void coopnet_save_dest_id(uint64_t userId, uint64_t destId) { + struct DestinationId* dest = NULL; + for (int i = 0; i < MAX_DEST_IDS; i++) { + if (sDestinationIds[i].userId == userId) { + sDestinationIds[i].destId = destId; + return; + } else if (dest == NULL && sDestinationIds[i].userId == 0) { + dest = &sDestinationIds[i]; + } + } + if (dest) { + dest->userId = userId; + dest->destId = destId; + } +} + +void coopnet_clear_dest_id(uint64_t userId) { + for (int i = 0; i < MAX_DEST_IDS; i++) { + if (sDestinationIds[i].userId == userId) { + sDestinationIds[i].userId = 0; + sDestinationIds[i].destId = 0; + } + } +} + +void coopnet_clear_dest_ids(void) { + for (int i = 0; i < MAX_DEST_IDS; i++) { + sDestinationIds[i].userId = 0; + sDestinationIds[i].destId = 0; + } +} + +uint64_t coopnet_get_dest_id(uint64_t userId) { + for (int i = 0; i < MAX_DEST_IDS; i++) { + if (sDestinationIds[i].userId == userId) { + return sDestinationIds[i].destId; + } + } + return 0; +} + u8 coopnet_user_id_to_local_index(uint64_t userId) { for (int i = 1; i < MAX_PLAYERS; i++) { if (gNetworkPlayers[i].connected && sNetworkUserIds[i] == userId) { diff --git a/src/pc/network/coopnet/coopnet_id.h b/src/pc/network/coopnet/coopnet_id.h index 1978c3791..36ab56430 100644 --- a/src/pc/network/coopnet/coopnet_id.h +++ b/src/pc/network/coopnet/coopnet_id.h @@ -2,6 +2,11 @@ #include "PR/ultratypes.h" +void coopnet_save_dest_id(uint64_t userId, uint64_t destId); +void coopnet_clear_dest_id(uint64_t userId); +void coopnet_clear_dest_ids(void); +uint64_t coopnet_get_dest_id(uint64_t userId); + u8 coopnet_user_id_to_local_index(uint64_t userId); void coopnet_set_user_id(uint8_t localIndex, uint64_t userId); uint64_t coopnet_get_local_user_id(void);