From 33d52ef31d3f358a90989a75cbb3f6cd0b146692 Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 11 Sep 2020 16:07:45 -0700 Subject: [PATCH] Added version checking to clients --- Makefile | 4 ++-- src/menu/file_select.c | 12 +++++++++++- src/menu/file_select.h | 1 + src/pc/network/packets/packet_save_file.c | 12 ++++++++++++ src/pc/pc_main.c | 4 ++-- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index b1bb0683..ffaf4cc8 100644 --- a/Makefile +++ b/Makefile @@ -147,11 +147,11 @@ VERSION_ASFLAGS := --defsym $(VERSION_DEF)=1 # Stuff for showing the git hash in the intro on nightly builds # From https://stackoverflow.com/questions/44038428/include-git-commit-hash-and-or-branch-name-in-c-c-source -ifeq ($(shell git rev-parse --abbrev-ref HEAD),nightly) +#ifeq ($(shell git rev-parse --abbrev-ref HEAD),nightly) GIT_HASH=`git rev-parse --short HEAD` COMPILE_TIME=`date -u +'%Y-%m-%d %H:%M:%S UTC'` VERSION_CFLAGS += -DNIGHTLY -DGIT_HASH="\"$(GIT_HASH)\"" -DCOMPILE_TIME="\"$(COMPILE_TIME)\"" -endif +#endif # Microcode diff --git a/src/menu/file_select.c b/src/menu/file_select.c index 8b8fc441..7560b2e9 100644 --- a/src/menu/file_select.c +++ b/src/menu/file_select.c @@ -41,6 +41,8 @@ * special menu messages and phases, button states and button clicked checks. */ +static u8 joinVersionMismatch = FALSE; + #ifdef VERSION_US // The current sound mode is automatically centered on US due to // the large length difference between options. @@ -437,6 +439,7 @@ void join_server_as_client(void) { } keyboard_stop_text_input(); + joinVersionMismatch = FALSE; network_init(NT_CLIENT, configJoinIp, configJoinPort); } @@ -445,6 +448,11 @@ void joined_server_as_client(s16 fileIndex) { sSelectedFileNum = fileIndex; } +void joined_server_version_mismatch(void) { + if (gNetworkType != NT_CLIENT) { return; } + joinVersionMismatch = TRUE; +} + void render_network_mode_menu_buttons(struct Object* soundModeButton) { #define NETWORK_BUTTON_Y 0 // Host option button @@ -561,7 +569,9 @@ void print_join_mode_menu_strings(void) { print_generic_ascii_string(JOIN_LEVEL_NAME_X, 191 - (12 * 2), gTextInput); // Print status - if (gNetworkType == NT_CLIENT) { + if (joinVersionMismatch) { + print_generic_ascii_string(JOIN_LEVEL_NAME_X, 191 - (12 * 14), "Error - versions don't match. Both should rebuild!"); + } else if (gNetworkType == NT_CLIENT) { print_generic_ascii_string(JOIN_LEVEL_NAME_X, 191 - (12 * 14), "Connecting..."); } else if (strlen(gTextInput) > 0) { print_generic_ascii_string(JOIN_LEVEL_NAME_X, 191 - (12 * 14), "Press (ENTER) to join."); diff --git a/src/menu/file_select.h b/src/menu/file_select.h index 7629ebbd..1bf4b21e 100644 --- a/src/menu/file_select.h +++ b/src/menu/file_select.h @@ -144,5 +144,6 @@ Gfx *geo_file_select_strings_and_menu_cursor(s32 callContext, UNUSED struct Grap s32 lvl_init_menu_values_and_cursor_pos(UNUSED s32 arg, UNUSED s32 unused); s32 lvl_update_obj_and_load_file_selected(UNUSED s32 arg, UNUSED s32 unused); void joined_server_as_client(s16 fileIndex); +void joined_server_version_mismatch(void); #endif // FILE_SELECT_H diff --git a/src/pc/network/packets/packet_save_file.c b/src/pc/network/packets/packet_save_file.c index 8751f36a..dd75f0f4 100644 --- a/src/pc/network/packets/packet_save_file.c +++ b/src/pc/network/packets/packet_save_file.c @@ -11,6 +11,7 @@ #include "src/pc/fs/fs.h" #include "PR/os_eeprom.h" +#define HASH_LENGTH 8 extern u8* gOverrideEeprom; static u8 eeprom[512] = { 0 }; @@ -38,8 +39,11 @@ void network_send_save_file(void) { fs_close(fp); } + char hash[HASH_LENGTH] = GIT_HASH; + struct Packet p; packet_init(&p, PACKET_SAVE_FILE, true); + packet_write(&p, &hash, sizeof(u8) * HASH_LENGTH); packet_write(&p, &gCurrSaveFileNum, sizeof(s16)); packet_write(&p, &gServerSettings.playerInteractions, sizeof(u8)); packet_write(&p, &gServerSettings.playerKnockbackStrength, sizeof(u8)); @@ -52,8 +56,11 @@ void network_receive_save_file(struct Packet* p) { assert(gNetworkType == NT_CLIENT); gOverrideEeprom = eeprom; + char hash[HASH_LENGTH] = GIT_HASH; + char remoteHash[HASH_LENGTH] = { 0 }; // find all reserved objects + packet_read(p, &remoteHash, sizeof(u8) * HASH_LENGTH); packet_read(p, &gCurrSaveFileNum, sizeof(s16)); packet_read(p, &gServerSettings.playerInteractions, sizeof(u8)); packet_read(p, &gServerSettings.playerKnockbackStrength, sizeof(u8)); @@ -61,5 +68,10 @@ void network_receive_save_file(struct Packet* p) { packet_read(p, eeprom, sizeof(u8) * 512); save_file_load_all(TRUE); + if (memcmp(hash, remoteHash, HASH_LENGTH) != 0) { + joined_server_version_mismatch(); + network_shutdown(); + return; + } joined_server_as_client(gCurrSaveFileNum); } diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 5ca2a388..d064887e 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -244,8 +244,8 @@ void main_func(void) { char window_title[96] = "Super Mario 64 EX coop (" RAPI_NAME ")" - #ifdef NIGHTLY - " nightly " GIT_HASH + #ifdef GIT_HASH + " [" GIT_HASH "]" #endif ;