From 8e2cd25617b59d74a3752f77f4135f00c1a19e7a Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 15 Apr 2023 15:54:44 -0700 Subject: [PATCH] Made it so changing server settings works in CoopNet --- src/pc/djui/djui_panel_host_message.c | 6 ++-- src/pc/djui/djui_panel_join_direct.c | 2 +- src/pc/djui/djui_panel_join_lobbies.c | 2 +- src/pc/djui/djui_panel_join_message.c | 2 +- src/pc/djui/djui_panel_pause.c | 2 +- src/pc/network/coopnet/coopnet.c | 22 +++++++++----- src/pc/network/discord/activity.c | 2 +- src/pc/network/discord/discord.c | 8 ++--- src/pc/network/network.c | 38 ++++++++++-------------- src/pc/network/network.h | 9 +++--- src/pc/network/network_player.c | 4 +-- src/pc/network/packets/packet_join.c | 4 +-- src/pc/network/packets/packet_kick.c | 24 ++++++++------- src/pc/network/packets/packet_mod_list.c | 4 +-- src/pc/network/socket/socket.c | 4 +-- src/pc/pc_main.c | 8 ++--- 16 files changed, 72 insertions(+), 69 deletions(-) diff --git a/src/pc/djui/djui_panel_host_message.c b/src/pc/djui/djui_panel_host_message.c index 1e6835d3..32c7c60e 100644 --- a/src/pc/djui/djui_panel_host_message.c +++ b/src/pc/djui/djui_panel_host_message.c @@ -14,7 +14,7 @@ #include "audio/external.h" #include "sounds.h" -void djui_panel_do_host(void) { +void djui_panel_do_host(bool reconnecting) { stop_demo(NULL); djui_panel_shutdown(); extern s16 gCurrSaveFileNum; @@ -30,7 +30,7 @@ void djui_panel_do_host(void) { if (configNetworkSystem >= NS_MAX) { configNetworkSystem = NS_MAX; } network_set_system(configNetworkSystem); - network_init(NT_SERVER); + network_init(NT_SERVER, reconnecting); djui_panel_modlist_create(NULL); fake_lvl_init_from_save_file(); @@ -46,7 +46,7 @@ void djui_panel_do_host(void) { void djui_panel_host_message_do_host(UNUSED struct DjuiBase* caller) { network_reset_reconnect_and_rehost(); - djui_panel_do_host(); + djui_panel_do_host(false); } void djui_panel_host_message_create(struct DjuiBase* caller) { diff --git a/src/pc/djui/djui_panel_join_direct.c b/src/pc/djui/djui_panel_join_direct.c index 3db2308f..b5ef555b 100644 --- a/src/pc/djui/djui_panel_join_direct.c +++ b/src/pc/djui/djui_panel_join_direct.c @@ -147,7 +147,7 @@ void djui_panel_join_direct_do_join(struct DjuiBase* caller) { network_reset_reconnect_and_rehost(); djui_panel_join_direct_ip_text_set_new(); network_set_system(NS_SOCKET); - network_init(NT_CLIENT); + network_init(NT_CLIENT, false); djui_panel_join_message_create(caller); } diff --git a/src/pc/djui/djui_panel_join_lobbies.c b/src/pc/djui/djui_panel_join_lobbies.c index 5ea54474..8256b729 100644 --- a/src/pc/djui/djui_panel_join_lobbies.c +++ b/src/pc/djui/djui_panel_join_lobbies.c @@ -22,7 +22,7 @@ void djui_panel_join_lobby(struct DjuiBase* caller) { gCoopNetDesiredLobby = (uint64_t)caller->tag; network_reset_reconnect_and_rehost(); network_set_system(NS_COOPNET); - network_init(NT_CLIENT); + network_init(NT_CLIENT, false); djui_panel_join_message_create(caller); } diff --git a/src/pc/djui/djui_panel_join_message.c b/src/pc/djui/djui_panel_join_message.c index 9c41e186..1144095d 100644 --- a/src/pc/djui/djui_panel_join_message.c +++ b/src/pc/djui/djui_panel_join_message.c @@ -22,7 +22,7 @@ void djui_panel_join_message_error(char* message) { void djui_panel_join_message_cancel(struct DjuiBase* caller) { if (network_is_reconnecting()) { return; } network_reset_reconnect_and_rehost(); - network_shutdown(true, false, false); + network_shutdown(true, false, false, false); djui_panel_menu_back(caller); } diff --git a/src/pc/djui/djui_panel_pause.c b/src/pc/djui/djui_panel_pause.c index 2b76d709..e1bdbae0 100644 --- a/src/pc/djui/djui_panel_pause.c +++ b/src/pc/djui/djui_panel_pause.c @@ -22,7 +22,7 @@ static void djui_panel_pause_resume(UNUSED struct DjuiBase* caller) { static void djui_panel_pause_quit_yes(UNUSED struct DjuiBase* caller) { network_reset_reconnect_and_rehost(); - network_shutdown(true, false, false); + network_shutdown(true, false, false, false); } static void djui_panel_pause_quit(struct DjuiBase* caller) { diff --git a/src/pc/network/coopnet/coopnet.c b/src/pc/network/coopnet/coopnet.c index c7d6fca4..82936b61 100644 --- a/src/pc/network/coopnet/coopnet.c +++ b/src/pc/network/coopnet/coopnet.c @@ -17,6 +17,7 @@ uint64_t gCoopNetDesiredLobby = 0; static uint64_t sLocalLobbyId = 0; static uint64_t sLocalLobbyOwnerId = 0; static enum NetworkType sNetworkType; +static bool sReconecting = false; static CoopNetRc coopnet_initialize(void); @@ -74,12 +75,14 @@ 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); + network_shutdown(false, false, true, false); } } -static bool ns_coopnet_initialize(enum NetworkType networkType) { +static bool ns_coopnet_initialize(enum NetworkType networkType, bool reconnecting) { sNetworkType = networkType; + sReconecting = reconnecting; + if (reconnecting) { return true; } return coopnet_is_connected() ? true : (coopnet_initialize() == COOPNET_OK); @@ -111,10 +114,14 @@ void ns_coopnet_update(void) { coopnet_update(); if (gNetworkType != NT_NONE && sNetworkType != NT_NONE) { if (sNetworkType == NT_SERVER) { - LOG_INFO("Create lobby"); - char mode[64] = ""; - mods_get_main_mod_name(mode, 64); - coopnet_lobby_create(CN_GAME_STR, get_version(), configPlayerName, mode, (uint16_t)configAmountofPlayers, configPassword); + if (sReconecting) { + + } else { + LOG_INFO("Create lobby"); + char mode[64] = ""; + mods_get_main_mod_name(mode, 64); + coopnet_lobby_create(CN_GAME_STR, get_version(), configPlayerName, mode, (uint16_t)configAmountofPlayers, configPassword); + } } else if (sNetworkType == NT_CLIENT) { LOG_INFO("Join lobby"); coopnet_lobby_join(gCoopNetDesiredLobby, ""); @@ -133,7 +140,8 @@ static int ns_coopnet_network_send(u8 localIndex, void* address, u8* data, u16 d return 0; } -static void ns_coopnet_shutdown(void) { +static void ns_coopnet_shutdown(bool reconnecting) { + if (reconnecting) { return; } LOG_INFO("Coopnet shutdown!"); coopnet_shutdown(); gCoopNetCallbacks.OnLobbyListGot = NULL; diff --git a/src/pc/network/discord/activity.c b/src/pc/network/discord/activity.c index e787927c..6a7100e5 100644 --- a/src/pc/network/discord/activity.c +++ b/src/pc/network/discord/activity.c @@ -25,7 +25,7 @@ static void on_activity_join_callback(UNUSED void* data, enum EDiscordResult res return; } network_reset_reconnect_and_rehost(); - network_init(NT_CLIENT); + network_init(NT_CLIENT, false); gCurActivity.type = DiscordActivityType_Playing; if (snprintf(gCurActivity.party.id, 128, DISCORD_ID_FORMAT, lobby->id) < 0) { diff --git a/src/pc/network/discord/discord.c b/src/pc/network/discord/discord.c index 0311d760..58457725 100644 --- a/src/pc/network/discord/discord.c +++ b/src/pc/network/discord/discord.c @@ -106,8 +106,8 @@ static void ns_discord_update(void) { discord_network_flush(); } -static bool ns_discord_initialize(enum NetworkType networkType) { - if (gDiscordReconnecting) { return true; } +static bool ns_discord_initialize(enum NetworkType networkType, bool reconnecting) { + if (reconnecting) { return true; } #ifdef DEBUG set_instance_env_variable(); #endif @@ -179,8 +179,8 @@ static bool ns_discord_initialize(enum NetworkType networkType) { return true; } -static void ns_discord_shutdown(void) { - if (gDiscordReconnecting) { return; } +static void ns_discord_shutdown(bool reconnecting) { + if (reconnecting) { return; } if (!gDiscordInitialized) { return; } discord_lobby_leave(); gActivityLock = false; diff --git a/src/pc/network/network.c b/src/pc/network/network.c index 7a2fec19..52c8b6e8 100644 --- a/src/pc/network/network.c +++ b/src/pc/network/network.c @@ -58,7 +58,6 @@ u32 gNetworkAreaTimer = 0; void* gNetworkServerAddr = NULL; bool gNetworkSentJoin = false; u16 gNetworkRequestLocationTimer = 0; -bool gDiscordReconnecting = false; u8 gDebugPacketIdBuffer[256] = { 0xFF }; u8 gDebugPacketSentBuffer[256] = { 0 }; @@ -101,7 +100,7 @@ void network_set_system(enum NetworkSystemType nsType) { } } -bool network_init(enum NetworkType inNetworkType) { +bool network_init(enum NetworkType inNetworkType, bool reconnecting) { // reset override hide hud extern u8 gOverrideHideHud; gOverrideHideHud = 0; @@ -132,7 +131,7 @@ bool network_init(enum NetworkType inNetworkType) { // initialize the network system gNetworkSentJoin = false; - int rc = gNetworkSystem->initialize(inNetworkType); + int rc = gNetworkSystem->initialize(inNetworkType, reconnecting); if (!rc) { LOG_ERROR("failed to initialize network system"); return false; @@ -415,7 +414,6 @@ void network_reset_reconnect_and_rehost(void) { sNetworkReconnectTimer = 0; sNetworkRehostTimer = 0; sNetworkReconnectType = NS_SOCKET; - gDiscordReconnecting = false; } void network_reconnect_begin(void) { @@ -425,17 +423,15 @@ void network_reconnect_begin(void) { sNetworkReconnectTimer = 2 * 30; -#ifdef DISCORD_SDK - sNetworkReconnectType = (gNetworkSystem == &gNetworkSystemDiscord) - ? NS_DISCORD +#ifdef COOPNET + sNetworkReconnectType = (gNetworkSystem == &gNetworkSystemCoopNet) + ? NS_COOPNET : NS_SOCKET; #else sNetworkReconnectType = NS_SOCKET; #endif - gDiscordReconnecting = true; - network_shutdown(false, false, false); - gDiscordReconnecting = false; + network_shutdown(false, false, false, true); djui_connect_menu_open(); } @@ -446,11 +442,11 @@ static void network_reconnect_update(void) { if (sNetworkReconnectType == NS_SOCKET) { network_set_system(NS_SOCKET); + } else if (sNetworkReconnectType == NS_COOPNET) { + network_set_system(NS_COOPNET); } - gDiscordReconnecting = true; - network_init(NT_CLIENT); - gDiscordReconnecting = false; + network_init(NT_CLIENT, true); network_send_mod_list_request(); } @@ -468,21 +464,17 @@ void network_rehost_begin(void) { network_player_disconnected(i); } - gDiscordReconnecting = true; - network_shutdown(false, false, false); - gDiscordReconnecting = false; + network_shutdown(false, false, false, true); sNetworkRehostTimer = 2; } static void network_rehost_update(void) { - extern void djui_panel_do_host(void); + extern void djui_panel_do_host(bool reconnecting); if (sNetworkRehostTimer <= 0) { return; } if (--sNetworkRehostTimer != 0) { return; } - gDiscordReconnecting = true; - djui_panel_do_host(); - gDiscordReconnecting = false; + djui_panel_do_host(true); } static void network_update_area_timer(void) { @@ -605,7 +597,7 @@ void network_register_mod(char* modName) { string_linked_list_append(&gRegisteredMods, modName); } -void network_shutdown(bool sendLeaving, bool exiting, bool popup) { +void network_shutdown(bool sendLeaving, bool exiting, bool popup, bool reconnecting) { if (gDjuiChatBox != NULL) { djui_base_destroy(&gDjuiChatBox->base); gDjuiChatBox = NULL; @@ -619,7 +611,7 @@ void network_shutdown(bool sendLeaving, bool exiting, bool popup) { if (gNetworkPlayerLocal != NULL && sendLeaving) { network_send_leaving(gNetworkPlayerLocal->globalIndex); } network_player_shutdown(popup); - gNetworkSystem->shutdown(); + gNetworkSystem->shutdown(reconnecting); if (gNetworkServerAddr != NULL) { free(gNetworkServerAddr); @@ -627,7 +619,7 @@ void network_shutdown(bool sendLeaving, bool exiting, bool popup) { } gNetworkPlayerServer = NULL; - if (sNetworkReconnectTimer <= 0 || sNetworkReconnectType != NS_DISCORD) { + if (sNetworkReconnectTimer <= 0 || sNetworkReconnectType != NS_COOPNET) { gNetworkType = NT_NONE; } diff --git a/src/pc/network/network.h b/src/pc/network/network.h index 7af402ce..0d16e7e4 100644 --- a/src/pc/network/network.h +++ b/src/pc/network/network.h @@ -42,7 +42,7 @@ enum NetworkSystemType { }; struct NetworkSystem { - bool (*initialize)(enum NetworkType); + bool (*initialize)(enum NetworkType, bool reconnecting); s64 (*get_id)(u8 localIndex); char* (*get_id_str)(u8 localIndex); void (*save_id)(u8 localIndex, s64 networkId); @@ -51,7 +51,7 @@ struct NetworkSystem { bool (*match_addr)(void* addr1, void* addr2); void (*update)(void); int (*send)(u8 localIndex, void* addr, u8* data, u16 dataLength); - void (*shutdown)(void); + void (*shutdown)(bool reconnecting); bool requireServerBroadcast; char* name; }; @@ -91,11 +91,10 @@ extern u8 gDebugPacketIdBuffer[]; extern u8 gDebugPacketSentBuffer[]; extern u8 gDebugPacketOnBuffer; extern u32 gNetworkStartupTimer; -extern bool gDiscordReconnecting; // network.c void network_set_system(enum NetworkSystemType nsType); -bool network_init(enum NetworkType inNetworkType); +bool network_init(enum NetworkType inNetworkType, bool reconnecting); void network_on_init_area(void); void network_on_loaded_area(void); bool network_allow_unknown_local_index(enum PacketType packetType); @@ -109,6 +108,6 @@ bool network_is_reconnecting(void); void network_rehost_begin(void); void network_update(void); void network_register_mod(char* modName); -void network_shutdown(bool sendLeaving, bool exiting, bool popup); +void network_shutdown(bool sendLeaving, bool exiting, bool popup, bool reconnecting); #endif diff --git a/src/pc/network/network_player.c b/src/pc/network/network_player.c index d1c86231..0bc501c3 100644 --- a/src/pc/network/network_player.c +++ b/src/pc/network/network_player.c @@ -196,7 +196,7 @@ void network_player_update(void) { #ifndef DEVELOPMENT if (elapsed > NETWORK_PLAYER_TIMEOUT * 1.5f) { LOG_INFO("dropping due to no server connectivity"); - network_shutdown(false, false, true); + network_shutdown(false, false, true, false); } #endif @@ -321,7 +321,7 @@ u8 network_player_disconnected(u8 globalIndex) { LOG_ERROR("player disconnected, but it's local.. this shouldn't happen!"); return UNKNOWN_GLOBAL_INDEX; } else { - network_shutdown(true, false, true); + network_shutdown(true, false, true, false); } } diff --git a/src/pc/network/packets/packet_join.c b/src/pc/network/packets/packet_join.c index a171d232..2a9cc1af 100644 --- a/src/pc/network/packets/packet_join.c +++ b/src/pc/network/packets/packet_join.c @@ -165,7 +165,7 @@ void network_receive_join(struct Packet* p) { packet_read(p, &remoteVersion, sizeof(u8) * MAX_VERSION_LENGTH); LOG_INFO("server has version: %s", version); if (memcmp(version, remoteVersion, MAX_VERSION_LENGTH) != 0) { - network_shutdown(true, false, false); + network_shutdown(true, false, false, false); LOG_ERROR("version mismatch"); char mismatchMessage[256] = { 0 }; snprintf(mismatchMessage, 256, "\\#ffa0a0\\Error:\\#c8c8c8\\ Version mismatch.\n\nYour version: \\#a0a0ff\\%s\\#c8c8c8\\\nTheir version: \\#a0a0ff\\%s\\#c8c8c8\\\n\nSomeone is out of date!\n", version, remoteVersion); @@ -196,7 +196,7 @@ void network_receive_join(struct Packet* p) { } if (string_linked_list_mismatch(&gRegisteredMods, &head)) { - network_shutdown(true, false, false); + network_shutdown(true, false, false, false); struct StringBuilder* builder = string_builder_create(512); string_builder_append(builder, "\\#ffa0a0\\Error:\\#c8c8c8\\ mods don't match.\n\n"); diff --git a/src/pc/network/packets/packet_kick.c b/src/pc/network/packets/packet_kick.c index 5ead7675..9f2d006d 100644 --- a/src/pc/network/packets/packet_kick.c +++ b/src/pc/network/packets/packet_kick.c @@ -4,7 +4,8 @@ #include "pc/djui/djui.h" #include "pc/utils/misc.h" -f32 lastReconnectTime = -9999999; +f32 sLastReconnectTime = -9999999; +f32 sLastNotifyTime = -9999999; void network_send_kick(u8 localIndex, enum KickReasonType kickReason) { u8 kickReasonType = kickReason; @@ -30,21 +31,24 @@ void network_receive_kick(struct Packet* p) { packet_read(p, &kickReasonType, sizeof(u8)); enum KickReasonType kickReason = kickReasonType; - switch (kickReason) { - case EKT_FULL_PARTY: djui_popup_create(DLANG(NOTIF, DISCONNECT_FULL), 1); break; - case EKT_KICKED: djui_popup_create(DLANG(NOTIF, DISCONNECT_KICK), 1); break; - case EKT_BANNED: djui_popup_create(DLANG(NOTIF, DISCONNECT_BAN), 1); break; - default: djui_popup_create(DLANG(NOTIF, DISCONNECT_CLOSED), 1); break; + f32 now = clock_elapsed(); + if ((now - sLastNotifyTime) > 3) { + sLastNotifyTime = now; + switch (kickReason) { + case EKT_FULL_PARTY: djui_popup_create(DLANG(NOTIF, DISCONNECT_FULL), 1); break; + case EKT_KICKED: djui_popup_create(DLANG(NOTIF, DISCONNECT_KICK), 1); break; + case EKT_BANNED: djui_popup_create(DLANG(NOTIF, DISCONNECT_BAN), 1); break; + default: djui_popup_create(DLANG(NOTIF, DISCONNECT_CLOSED), 1); break; + } } if (kickReason == EKT_REJOIN) { - f32 now = clock_elapsed(); - if ((now - lastReconnectTime) > 3) { - lastReconnectTime = now; + if ((now - sLastReconnectTime) > 3) { + sLastReconnectTime = now; network_reconnect_begin(); djui_popup_create(DLANG(NOTIF, DISCONNECT_REJOIN), 1); } } else { - network_shutdown(false, false, false); + network_shutdown(false, false, false, false); } } diff --git a/src/pc/network/packets/packet_mod_list.c b/src/pc/network/packets/packet_mod_list.c index dfe32c5e..9c0f07b1 100644 --- a/src/pc/network/packets/packet_mod_list.c +++ b/src/pc/network/packets/packet_mod_list.c @@ -140,7 +140,7 @@ void network_receive_mod_list(struct Packet* p) { packet_read(p, &remoteVersion, sizeof(u8) * MAX_VERSION_LENGTH); LOG_INFO("server has version: %s", version); if (memcmp(version, remoteVersion, MAX_VERSION_LENGTH) != 0) { - network_shutdown(true, false, false); + network_shutdown(true, false, false, false); LOG_ERROR("version mismatch"); char mismatchMessage[256] = { 0 }; snprintf(mismatchMessage, 256, "\\#ffa0a0\\Error:\\#c8c8c8\\ Version mismatch.\n\nYour version: \\#a0a0ff\\%s\\#c8c8c8\\\nTheir version: \\#a0a0ff\\%s\\#c8c8c8\\\n\nSomeone is out of date!\n", version, remoteVersion); @@ -241,7 +241,7 @@ void network_receive_mod_list_entry(struct Packet* p) { // sanity check mod size if (mod->size >= MAX_MOD_SIZE) { djui_popup_create(DLANG(NOTIF, DISCONNECT_BIG_MOD), 4); - network_shutdown(false, false, false); + network_shutdown(false, false, false, false); return; } diff --git a/src/pc/network/socket/socket.c b/src/pc/network/socket/socket.c index fb428469..f1b0a552 100644 --- a/src/pc/network/socket/socket.c +++ b/src/pc/network/socket/socket.c @@ -59,7 +59,7 @@ static int socket_receive(SOCKET socket, struct sockaddr_in* rxAddr, u8* buffer, return NO_ERROR; } -static bool ns_socket_initialize(enum NetworkType networkType) { +static bool ns_socket_initialize(enum NetworkType networkType, UNUSED bool reconnecting) { // sanity check port unsigned int port = (networkType == NT_CLIENT) ? configJoinPort : configHostPort; @@ -180,7 +180,7 @@ static int ns_socket_send(u8 localIndex, void* address, u8* data, u16 dataLength return rc; } -static void ns_socket_shutdown(void) { +static void ns_socket_shutdown(UNUSED bool reconnecting) { socket_shutdown(sCurSocket); sCurSocket = INVALID_SOCKET; for (u16 i = 0; i < MAX_PLAYERS; i++) { diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 010c37a1..a70363b2 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -254,7 +254,7 @@ void game_deinit(void) { audio_custom_shutdown(); audio_shutdown(); gfx_shutdown(); - network_shutdown(true, true, false); + network_shutdown(true, true, false, false); smlua_shutdown(); mods_shutdown(); inited = false; @@ -367,15 +367,15 @@ void main_func(void) { snprintf(gGetHostName, MAX_CONFIG_STRING, "%s", gCLIOpts.JoinIp); snprintf(configJoinIp, MAX_CONFIG_STRING, "%s", gCLIOpts.JoinIp); configJoinPort = gCLIOpts.NetworkPort; - network_init(NT_CLIENT); + network_init(NT_CLIENT, false); } else if (gCLIOpts.Network == NT_SERVER) { network_set_system(NS_SOCKET); configHostPort = gCLIOpts.NetworkPort; - network_init(NT_SERVER); + network_init(NT_SERVER, false); djui_panel_shutdown(); djui_panel_modlist_create(NULL); } else { - network_init(NT_NONE); + network_init(NT_NONE, false); } audio_init();