From 81fdd730287c4aea650946e3b0314604f457b438 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Fri, 5 Nov 2021 20:21:07 -0700 Subject: [PATCH 1/5] start work on new cpe type for plugin messages --- src/Event.c | 32 ++++++++++++++++++++------------ src/Event.h | 12 ++++++++++++ src/Protocol.c | 6 ++++++ src/Protocol.h | 1 + 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/Event.c b/src/Event.c index 934eaba5c..2cc4421f6 100644 --- a/src/Event.c +++ b/src/Event.c @@ -1,18 +1,19 @@ #include "Event.h" #include "Logger.h" -struct _EntityEventsList EntityEvents; -struct _TabListEventsList TabListEvents; -struct _TextureEventsList TextureEvents; -struct _GfxEventsList GfxEvents; -struct _UserEventsList UserEvents; -struct _BlockEventsList BlockEvents; -struct _WorldEventsList WorldEvents; -struct _ChatEventsList ChatEvents; -struct _WindowEventsList WindowEvents; -struct _InputEventsList InputEvents; -struct _PointerEventsList PointerEvents; -struct _NetEventsList NetEvents; +struct _EntityEventsList EntityEvents; +struct _TabListEventsList TabListEvents; +struct _TextureEventsList TextureEvents; +struct _GfxEventsList GfxEvents; +struct _UserEventsList UserEvents; +struct _BlockEventsList BlockEvents; +struct _WorldEventsList WorldEvents; +struct _ChatEventsList ChatEvents; +struct _WindowEventsList WindowEvents; +struct _InputEventsList InputEvents; +struct _PointerEventsList PointerEvents; +struct _NetEventsList NetEvents; +struct _PluginMessageEventsList PluginMessageEvents; void Event_Register(struct Event_Void* handlers, void* obj, Event_Void_Callback handler) { int i; @@ -170,3 +171,10 @@ void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDel handlers->Handlers[i](handlers->Objs[i], xDelta, yDelta); } } + +void Event_RaiseData(struct Event_Data* handlers, cc_uint8* data) { + int i; + for (i = 0; i < handlers->Count; i++) { + handlers->Handlers[i](handlers->Objs[i], data); + } +} diff --git a/src/Event.h b/src/Event.h index 1b4c34c6b..168600435 100644 --- a/src/Event.h +++ b/src/Event.h @@ -63,6 +63,12 @@ struct Event_RawMove { void* Objs[EVENT_MAX_CALLBACKS]; int Count; }; +typedef void (*Event_Data_Callback)(void* obj, cc_uint8* data); +struct Event_Data { + Event_Data_Callback Handlers[EVENT_MAX_CALLBACKS]; + void* Objs[EVENT_MAX_CALLBACKS]; int Count; +}; + /* Registers a callback function for the given event. */ /* NOTE: Trying to register a callback twice or over EVENT_MAX_CALLBACKS callbacks will terminate the game. */ CC_API void Event_Register(struct Event_Void* handlers, void* obj, Event_Void_Callback handler); @@ -94,6 +100,8 @@ void Event_RaiseInput(struct Event_Input* handlers, int key, cc_bool repeating); void Event_RaiseString(struct Event_String* handlers, const cc_string* str); /* Calls all registered callbacks for an event which has raw pointer movement arguments. */ void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDelta); +/* Calls all registered callbacks for an event which has a data argument. */ +void Event_RaiseData(struct Event_Data* handlers, cc_uint8* data); void Event_UnregisterAll(void); /* NOTE: Event_UnregisterAll must be updated if events lists are changed */ @@ -177,4 +185,8 @@ CC_VAR extern struct _NetEventsList { struct Event_Void Connected; /* Connection to a server was established. */ struct Event_Void Disconnected; /* Connection to the server was lost. */ } NetEvents; + +CC_VAR extern struct _PluginMessageEventsList { + struct Event_Data Received; +} PluginMessageEvents; #endif diff --git a/src/Protocol.c b/src/Protocol.c index 849c4f762..d7d80dcac 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1530,6 +1530,11 @@ static void CPE_UndefineModel(cc_uint8* data) { if (id < MAX_CUSTOM_MODELS) CustomModel_Undefine(&custom_models[id]); } +static void CPE_PluginMessage(cc_uint8* data) { + cc_uint8 channel = data[0]; + Event_RaiseData(&PluginMessageEvents.Received, data + 1); +} + static void CPE_Reset(void) { cpe_serverExtensionsCount = 0; cpe_pingTicks = 0; cpe_sendHeldBlock = false; cpe_useMessageTypes = false; @@ -1575,6 +1580,7 @@ static void CPE_Reset(void) { Net_Set(OPCODE_DEFINE_MODEL, CPE_DefineModel, 116); Net_Set(OPCODE_DEFINE_MODEL_PART, CPE_DefineModelPart, 104); Net_Set(OPCODE_UNDEFINE_MODEL, CPE_UndefineModel, 2); + Net_Set(OPCODE_PLUGIN_MESSAGE, CPE_PluginMessage, 2); } static void CPE_Tick(void) { diff --git a/src/Protocol.h b/src/Protocol.h index 90388b39d..c2a642db9 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -35,6 +35,7 @@ enum OPCODE_ { OPCODE_SET_SPAWNPOINT, OPCODE_VELOCITY_CONTROL, OPCODE_DEFINE_EFFECT, OPCODE_SPAWN_EFFECT, OPCODE_DEFINE_MODEL, OPCODE_DEFINE_MODEL_PART, OPCODE_UNDEFINE_MODEL, + OPCODE_PLUGIN_MESSAGE, OPCODE_COUNT }; From d9674364751bcf70267f3da6b9add6966098c349 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Fri, 5 Nov 2021 20:35:07 -0700 Subject: [PATCH 2/5] make Event_PluginMessage instead, add "PluginMessages" to cpe string list --- src/Event.c | 4 ++-- src/Event.h | 10 +++++----- src/Protocol.c | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Event.c b/src/Event.c index 2cc4421f6..87109a5f4 100644 --- a/src/Event.c +++ b/src/Event.c @@ -172,9 +172,9 @@ void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDel } } -void Event_RaiseData(struct Event_Data* handlers, cc_uint8* data) { +void Event_RaisePluginMessage(struct Event_PluginMessage* handlers, cc_uint8 channel, cc_uint8* data) { int i; for (i = 0; i < handlers->Count; i++) { - handlers->Handlers[i](handlers->Objs[i], data); + handlers->Handlers[i](handlers->Objs[i], channel, data); } } diff --git a/src/Event.h b/src/Event.h index 168600435..26fbfb7b5 100644 --- a/src/Event.h +++ b/src/Event.h @@ -63,9 +63,9 @@ struct Event_RawMove { void* Objs[EVENT_MAX_CALLBACKS]; int Count; }; -typedef void (*Event_Data_Callback)(void* obj, cc_uint8* data); -struct Event_Data { - Event_Data_Callback Handlers[EVENT_MAX_CALLBACKS]; +typedef void (*Event_PluginMessage_Callback)(void* obj, cc_uint8 channel, cc_uint8* data); +struct Event_PluginMessage { + Event_PluginMessage_Callback Handlers[EVENT_MAX_CALLBACKS]; void* Objs[EVENT_MAX_CALLBACKS]; int Count; }; @@ -101,7 +101,7 @@ void Event_RaiseString(struct Event_String* handlers, const cc_string* str); /* Calls all registered callbacks for an event which has raw pointer movement arguments. */ void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDelta); /* Calls all registered callbacks for an event which has a data argument. */ -void Event_RaiseData(struct Event_Data* handlers, cc_uint8* data); +void Event_RaisePluginMessage(struct Event_PluginMessage* handlers, cc_uint8 channel, cc_uint8* data); void Event_UnregisterAll(void); /* NOTE: Event_UnregisterAll must be updated if events lists are changed */ @@ -187,6 +187,6 @@ CC_VAR extern struct _NetEventsList { } NetEvents; CC_VAR extern struct _PluginMessageEventsList { - struct Event_Data Received; + struct Event_PluginMessage Received; } PluginMessageEvents; #endif diff --git a/src/Protocol.c b/src/Protocol.c index d7d80dcac..fb61d210b 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -758,7 +758,7 @@ static const char* cpe_clientExtensions[] = { "EnvWeatherType", "MessageTypes", "HackControl", "PlayerClick", "FullCP437", "LongerMessages", "BlockDefinitions", "BlockDefinitionsExt", "BulkBlockUpdate", "TextColors", "EnvMapAspect", "EntityProperty", "ExtEntityPositions", "TwoWayPing", "InventoryOrder", "InstantMOTD", "FastMap", "SetHotbar", - "SetSpawnpoint", "VelocityControl", "CustomParticles", "CustomModels", + "SetSpawnpoint", "VelocityControl", "CustomParticles", "CustomModels", "PluginMessages" /* NOTE: These must be placed last for when EXTENDED_TEXTURES or EXTENDED_BLOCKS are not defined */ "ExtendedTextures", "ExtendedBlocks" }; @@ -1532,7 +1532,7 @@ static void CPE_UndefineModel(cc_uint8* data) { static void CPE_PluginMessage(cc_uint8* data) { cc_uint8 channel = data[0]; - Event_RaiseData(&PluginMessageEvents.Received, data + 1); + Event_RaisePluginMessage(&PluginMessageEvents.Received, channel, data + 1); } static void CPE_Reset(void) { @@ -1580,7 +1580,7 @@ static void CPE_Reset(void) { Net_Set(OPCODE_DEFINE_MODEL, CPE_DefineModel, 116); Net_Set(OPCODE_DEFINE_MODEL_PART, CPE_DefineModelPart, 104); Net_Set(OPCODE_UNDEFINE_MODEL, CPE_UndefineModel, 2); - Net_Set(OPCODE_PLUGIN_MESSAGE, CPE_PluginMessage, 2); + Net_Set(OPCODE_PLUGIN_MESSAGE, CPE_PluginMessage, 66); } static void CPE_Tick(void) { From bf6059c75e4231941c21957540ca3a6125b5e5f9 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Fri, 5 Nov 2021 21:03:21 -0700 Subject: [PATCH 3/5] support for sending, exports `CPE_SendPluginMessage` --- src/Protocol.c | 21 ++++++++++++++++++--- src/Protocol.h | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Protocol.c b/src/Protocol.c index fb61d210b..09e1263bc 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -63,7 +63,7 @@ cc_bool cpe_needD3Fix; static int cpe_serverExtensionsCount, cpe_pingTicks; static int cpe_envMapVer = 2, cpe_blockDefsExtVer = 2, cpe_customModelsVer = 2; static cc_bool cpe_sendHeldBlock, cpe_useMessageTypes, cpe_extEntityPos, cpe_blockPerms, cpe_fastMap; -static cc_bool cpe_twoWayPing, cpe_extTextures, cpe_extBlocks; +static cc_bool cpe_twoWayPing, cpe_pluginMessages, cpe_extTextures, cpe_extBlocks; /*########################################################################################################################* *-----------------------------------------------------Common handlers-----------------------------------------------------* @@ -795,6 +795,19 @@ void CPE_SendPlayerClick(int button, cc_bool pressed, cc_uint8 targetId, struct Server.SendData(data, 15); } +void CPE_SendPluginMessage(cc_uint8 channel, cc_uint8* data) { + cc_uint8 buffer[66]; + + if (!cpe_pluginMessages) return; + + buffer[0] = OPCODE_PLUGIN_MESSAGE; + { + buffer[1] = channel; + Mem_Copy(buffer + 2, data, 64); + } + Server.SendData(buffer, 66); +} + static void CPE_SendExtInfo(int extsCount) { cc_uint8 data[67]; data[0] = OPCODE_EXT_INFO; @@ -932,6 +945,8 @@ static void CPE_ExtEntry(cc_uint8* data) { if (version == 2) { Protocol.Sizes[OPCODE_DEFINE_MODEL_PART] = 167; } + } else if (String_CaselessEqualsConst(&ext, "PluginMessages")) { + cpe_pluginMessages = true; } #ifdef EXTENDED_TEXTURES else if (String_CaselessEqualsConst(&ext, "ExtendedTextures")) { @@ -1540,8 +1555,8 @@ static void CPE_Reset(void) { cpe_sendHeldBlock = false; cpe_useMessageTypes = false; cpe_envMapVer = 2; cpe_blockDefsExtVer = 2; cpe_customModelsVer = 2; cpe_needD3Fix = false; cpe_extEntityPos = false; cpe_twoWayPing = false; - cpe_extTextures = false; cpe_fastMap = false; cpe_extBlocks = false; - Game_UseCPEBlocks = false; cpe_blockPerms = false; + cpe_pluginMessages = false; cpe_extTextures = false; cpe_fastMap = false; + cpe_extBlocks = false; Game_UseCPEBlocks = false; cpe_blockPerms = false; if (!Game_UseCPE) return; Net_Set(OPCODE_EXT_INFO, CPE_ExtInfo, 67); diff --git a/src/Protocol.h b/src/Protocol.h index c2a642db9..b7ec9cae5 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -64,4 +64,6 @@ void Classic_WritePosition(Vec3 pos, float yaw, float pitch); void Classic_WriteSetBlock(int x, int y, int z, cc_bool place, BlockID block); void Classic_SendLogin(void); void CPE_SendPlayerClick(int button, cc_bool pressed, cc_uint8 targetId, struct RayTracer* t); + +CC_API void CPE_SendPluginMessage(cc_uint8 channel, cc_uint8* data); #endif From 5b9ab638cd834295c553129223fd384cae36dbac Mon Sep 17 00:00:00 2001 From: SpiralP Date: Sat, 6 Nov 2021 14:59:06 -0700 Subject: [PATCH 4/5] update comments --- src/Event.h | 5 +++-- src/Protocol.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Event.h b/src/Event.h index 26fbfb7b5..ea60edce1 100644 --- a/src/Event.h +++ b/src/Event.h @@ -63,6 +63,7 @@ struct Event_RawMove { void* Objs[EVENT_MAX_CALLBACKS]; int Count; }; +/* "data" will be 64 bytes in length. */ typedef void (*Event_PluginMessage_Callback)(void* obj, cc_uint8 channel, cc_uint8* data); struct Event_PluginMessage { Event_PluginMessage_Callback Handlers[EVENT_MAX_CALLBACKS]; @@ -100,7 +101,7 @@ void Event_RaiseInput(struct Event_Input* handlers, int key, cc_bool repeating); void Event_RaiseString(struct Event_String* handlers, const cc_string* str); /* Calls all registered callbacks for an event which has raw pointer movement arguments. */ void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDelta); -/* Calls all registered callbacks for an event which has a data argument. */ +/* Calls all registered callbacks for an event which has a channel and a 64 byte data argument. */ void Event_RaisePluginMessage(struct Event_PluginMessage* handlers, cc_uint8 channel, cc_uint8* data); void Event_UnregisterAll(void); @@ -187,6 +188,6 @@ CC_VAR extern struct _NetEventsList { } NetEvents; CC_VAR extern struct _PluginMessageEventsList { - struct Event_PluginMessage Received; + struct Event_PluginMessage Received; /* Received a PluginMessage from the server. */ } PluginMessageEvents; #endif diff --git a/src/Protocol.h b/src/Protocol.h index b7ec9cae5..9e6e39361 100644 --- a/src/Protocol.h +++ b/src/Protocol.h @@ -65,5 +65,6 @@ void Classic_WriteSetBlock(int x, int y, int z, cc_bool place, BlockID block); void Classic_SendLogin(void); void CPE_SendPlayerClick(int button, cc_bool pressed, cc_uint8 targetId, struct RayTracer* t); +/* Send a PluginMessage to the server; data must contain 64 bytes. */ CC_API void CPE_SendPluginMessage(cc_uint8 channel, cc_uint8* data); #endif From 9f3cd77b02eb28b71f47452a44dc8ef791720e93 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Sun, 7 Nov 2021 20:45:00 -0800 Subject: [PATCH 5/5] oops comma! --- src/Protocol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol.c b/src/Protocol.c index 09e1263bc..7a3ae857d 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -758,7 +758,7 @@ static const char* cpe_clientExtensions[] = { "EnvWeatherType", "MessageTypes", "HackControl", "PlayerClick", "FullCP437", "LongerMessages", "BlockDefinitions", "BlockDefinitionsExt", "BulkBlockUpdate", "TextColors", "EnvMapAspect", "EntityProperty", "ExtEntityPositions", "TwoWayPing", "InventoryOrder", "InstantMOTD", "FastMap", "SetHotbar", - "SetSpawnpoint", "VelocityControl", "CustomParticles", "CustomModels", "PluginMessages" + "SetSpawnpoint", "VelocityControl", "CustomParticles", "CustomModels", "PluginMessages", /* NOTE: These must be placed last for when EXTENDED_TEXTURES or EXTENDED_BLOCKS are not defined */ "ExtendedTextures", "ExtendedBlocks" };