From 20ab5ec9f6b11658bde98db6dcad3b55bd010ef6 Mon Sep 17 00:00:00 2001 From: MysterD Date: Wed, 23 Sep 2020 20:44:44 -0700 Subject: [PATCH] When client selects to save, server will save --- src/game/save_file.c | 5 +++++ src/pc/network/packets/packet.c | 1 + src/pc/network/packets/packet.h | 5 +++++ src/pc/network/packets/packet_save_file.c | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 src/pc/network/packets/packet_save_file.c diff --git a/src/game/save_file.c b/src/game/save_file.c index dbd6d4a0..700c4aa2 100644 --- a/src/game/save_file.c +++ b/src/game/save_file.c @@ -346,6 +346,11 @@ static void save_file_bswap(struct SaveBuffer *buf) { } void save_file_do_save(s32 fileIndex) { + if (gNetworkType != NT_SERVER) { + if (gNetworkType == NT_CLIENT) { network_send_save_file(fileIndex); } + return; + } + if (fileIndex < 0 || fileIndex >= NUM_SAVE_FILES) return; diff --git a/src/pc/network/packets/packet.c b/src/pc/network/packets/packet.c index d243b6c3..bc98073e 100644 --- a/src/pc/network/packets/packet.c +++ b/src/pc/network/packets/packet.c @@ -30,6 +30,7 @@ void packet_receive(struct Packet* p) { case PACKET_KICK: network_receive_kick(p); break; case PACKET_KEEP_ALIVE: network_receive_keep_alive(p); break; case PACKET_LEAVING: network_receive_leaving(p); break; + case PACKET_SAVE_FILE: network_receive_save_file(p); break; /// case PACKET_CUSTOM: network_receive_custom(p); break; default: LOG_ERROR("received unknown packet: %d", p->buffer[0]); diff --git a/src/pc/network/packets/packet.h b/src/pc/network/packets/packet.h index 7d7fdfd9..bfb34659 100644 --- a/src/pc/network/packets/packet.h +++ b/src/pc/network/packets/packet.h @@ -28,6 +28,7 @@ enum PacketType { PACKET_KICK, PACKET_KEEP_ALIVE, PACKET_LEAVING, + PACKET_SAVE_FILE, /// PACKET_CUSTOM = 255, }; @@ -143,4 +144,8 @@ void network_receive_keep_alive(struct Packet* p); void network_send_leaving(u8 globalIndex); void network_receive_leaving(struct Packet* p); +// packet_save_file.c +void network_send_save_file(s32 fileIndex); +void network_receive_save_file(struct Packet* p); + #endif diff --git a/src/pc/network/packets/packet_save_file.c b/src/pc/network/packets/packet_save_file.c new file mode 100644 index 00000000..61152869 --- /dev/null +++ b/src/pc/network/packets/packet_save_file.c @@ -0,0 +1,18 @@ +#include +#include "../network.h" +#include "game/save_file.h" + +void network_send_save_file(s32 fileIndex) { + assert(gNetworkType == NT_CLIENT); + struct Packet p; + packet_init(&p, PACKET_SAVE_FILE, true); + packet_write(&p, &fileIndex, sizeof(s32)); + network_send_to(gNetworkPlayerServer->localIndex, &p); +} + +void network_receive_save_file(struct Packet* p) { + if (gNetworkType != NT_SERVER) { return; } + s32 fileIndex = 0; + packet_read(p, &fileIndex, sizeof(s32)); + save_file_do_save(fileIndex); +}