Added custom packets for mods-of-this-mod

Now a patch can choose to register callbacks for sending and receiving
packets. It's a bit tricky to use but at least it won't collide with
normal packets or other mod packets.
This commit is contained in:
MysterD 2020-09-04 02:04:37 -07:00
parent d5005c9f37
commit 9512fde4a5
4 changed files with 49 additions and 3 deletions

View file

@ -558,8 +558,8 @@ endif
CC_CHECK += -DCOOP CC_CHECK += -DCOOP
CFLAGS += -DCOOP CFLAGS += -DCOOP
# Enforce -Werror in debug mode # Enforce -Werror in strict mode
ifeq ($(DEBUG),1) ifeq ($(STRICT),1)
CC_CHECK += -Werror -Wno-error=unused-variable -Wno-error=unused-parameter CC_CHECK += -Werror -Wno-error=unused-variable -Wno-error=unused-parameter
CFLAGS += -Werror -Wno-error=unused-variable -Wno-error=unused-parameter CFLAGS += -Werror -Wno-error=unused-variable -Wno-error=unused-parameter
endif endif

View file

@ -95,7 +95,7 @@ void network_update(void) {
} }
// execute packet // execute packet
switch (p.buffer[0]) { switch ((u8)p.buffer[0]) {
case PACKET_ACK: network_receive_ack(&p); break; case PACKET_ACK: network_receive_ack(&p); break;
case PACKET_PLAYER: network_receive_player(&p); break; case PACKET_PLAYER: network_receive_player(&p); break;
case PACKET_OBJECT: network_receive_object(&p); break; case PACKET_OBJECT: network_receive_object(&p); break;
@ -108,6 +108,7 @@ void network_update(void) {
case PACKET_COLLECT_ITEM: network_receive_collect_item(&p); break; case PACKET_COLLECT_ITEM: network_receive_collect_item(&p); break;
case PACKET_RESERVATION_REQUEST: network_receive_reservation_request(&p); break; case PACKET_RESERVATION_REQUEST: network_receive_reservation_request(&p); break;
case PACKET_RESERVATION: network_receive_reservation(&p); break; case PACKET_RESERVATION: network_receive_reservation(&p); break;
case PACKET_CUSTOM: network_receive_custom(&p); break;
default: printf("%s received unknown packet: %d\n", NETWORKTYPESTR, p.buffer[0]); default: printf("%s received unknown packet: %d\n", NETWORKTYPESTR, p.buffer[0]);
} }

View file

@ -28,6 +28,7 @@ enum PacketType {
PACKET_COLLECT_ITEM, PACKET_COLLECT_ITEM,
PACKET_RESERVATION_REQUEST, PACKET_RESERVATION_REQUEST,
PACKET_RESERVATION, PACKET_RESERVATION,
PACKET_CUSTOM = 255,
}; };
struct Packet { struct Packet {
@ -129,4 +130,8 @@ void network_receive_reservation_request(struct Packet* p);
void network_send_reservation(void); void network_send_reservation(void);
void network_receive_reservation(struct Packet* p); void network_receive_reservation(struct Packet* p);
u8 network_register_custom_packet(void (*send_callback)(struct Packet* p, void* params), void (*receive_callback)(struct Packet* p));
void network_send_custom(u8 customId, bool reliable, void* params);
void network_receive_custom(struct Packet* p);
#endif #endif

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include "../network.h"
#define MAX_CUSTOM_PACKETS 32
struct NetworkCustomPacket {
void (*send_callback)(struct Packet* p, void* params);
void (*receive_callback)(struct Packet* p);
};
static u8 onCustomPacketId = 0;
static struct NetworkCustomPacket customPackets[MAX_CUSTOM_PACKETS];
u8 network_register_custom_packet(void (*send_callback)(struct Packet* p, void* params), void (*receive_callback)(struct Packet* p)) {
assert(onCustomPacketId < MAX_CUSTOM_PACKETS);
u8 i = onCustomPacketId;
customPackets[i].send_callback = send_callback;
customPackets[i].receive_callback = receive_callback;
onCustomPacketId++;
return i;
}
void network_send_custom(u8 customId, bool reliable, void* params) {
if (customPackets[customId].send_callback == NULL) { return; }
struct Packet p;
packet_init(&p, PACKET_CUSTOM, reliable);
packet_write(&p, &customId, sizeof(u8));
customPackets[customId].send_callback(&p, params);
network_send(&p);
}
void network_receive_custom(struct Packet* p) {
u8 customId;
packet_read(p, &customId, sizeof(u8));
if (customPackets[customId].receive_callback == NULL) { return; }
customPackets[customId].receive_callback(p);
}