start rewriting packet sending

This commit is contained in:
UnknownShadow200 2019-05-02 12:03:19 +10:00
parent 60404fb684
commit 2c0f99ebe5
7 changed files with 168 additions and 183 deletions

View File

@ -17,6 +17,7 @@
#include "Block.h" #include "Block.h"
#include "Menus.h" #include "Menus.h"
#include "Gui.h" #include "Gui.h"
#include "PacketHandlers.h"
static bool input_buttonsDown[3]; static bool input_buttonsDown[3];
static int input_pickingId = -1; static int input_pickingId = -1;
@ -44,8 +45,7 @@ static void InputHandler_ButtonStateUpdate(MouseButton button, bool pressed) {
} }
input_buttonsDown[button] = pressed; input_buttonsDown[button] = pressed;
Server.SendPlayerClick(button, pressed, CPE_SendPlayerClick(button, pressed, (EntityID)input_pickingId, &Game_SelectedPos);
(EntityID)input_pickingId, &Game_SelectedPos);
} }
static void InputHandler_ButtonStateChanged(MouseButton button, bool pressed) { static void InputHandler_ButtonStateChanged(MouseButton button, bool pressed) {

View File

@ -62,24 +62,24 @@ static bool cpe_twoWayPing, cpe_extTextures, cpe_extBlocks;
#define Classic_TabList_Reset(id) (classic_tabList[id >> 3] &= (uint8_t)~(1 << (id & 0x7))) #define Classic_TabList_Reset(id) (classic_tabList[id >> 3] &= (uint8_t)~(1 << (id & 0x7)))
#ifndef EXTENDED_BLOCKS #ifndef EXTENDED_BLOCKS
#define Handlers_ReadBlock(data, value) value = *data++; #define Protocol_ReadBlock(data, value) value = *data++;
#else #else
#define Handlers_ReadBlock(data, value)\ #define Protocol_ReadBlock(data, value)\
if (cpe_extBlocks) {\ if (cpe_extBlocks) {\
value = Stream_GetU16_BE(data); data += 2;\ value = Stream_GetU16_BE(data); data += 2;\
} else { value = *data++; } } else { value = *data++; }
#endif #endif
#ifndef EXTENDED_BLOCKS #ifndef EXTENDED_BLOCKS
#define Handlers_WriteBlock(data, value) *data++ = value; #define Protocol_WriteBlock(data, value) *data++ = value;
#else #else
#define Handlers_WriteBlock(data, value)\ #define Protocol_WriteBlock(data, value)\
if (cpe_extBlocks) {\ if (cpe_extBlocks) {\
Stream_SetU16_BE(data, value); data += 2;\ Stream_SetU16_BE(data, value); data += 2;\
} else { *data++ = (BlockRaw)value; } } else { *data++ = (BlockRaw)value; }
#endif #endif
static void Handlers_ReadString(uint8_t** ptr, String* str) { static void Protocol_ReadString(uint8_t** ptr, String* str) {
int i, length = 0; int i, length = 0;
uint8_t* data = *ptr; uint8_t* data = *ptr;
for (i = STRING_SIZE - 1; i >= 0; i--) { for (i = STRING_SIZE - 1; i >= 0; i--) {
@ -92,7 +92,7 @@ static void Handlers_ReadString(uint8_t** ptr, String* str) {
*ptr = data + STRING_SIZE; *ptr = data + STRING_SIZE;
} }
static void Handlers_WriteString(uint8_t* data, const String* value) { static void Protocol_WriteString(uint8_t* data, const String* value) {
int i, count = min(value->length, STRING_SIZE); int i, count = min(value->length, STRING_SIZE);
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
char c = value->buffer[i]; char c = value->buffer[i];
@ -103,14 +103,14 @@ static void Handlers_WriteString(uint8_t* data, const String* value) {
for (; i < STRING_SIZE; i++) { data[i] = ' '; } for (; i < STRING_SIZE; i++) { data[i] = ' '; }
} }
static void Handlers_RemoveEndPlus(String* value) { static void Protocol_RemoveEndPlus(String* value) {
/* Workaround for MCDzienny (and others) use a '+' at the end to distinguish classicube.net accounts */ /* Workaround for MCDzienny (and others) use a '+' at the end to distinguish classicube.net accounts */
/* from minecraft.net accounts. Unfortunately they also send this ending + to the client. */ /* from minecraft.net accounts. Unfortunately they also send this ending + to the client. */
if (!value->length || value->buffer[value->length - 1] != '+') return; if (!value->length || value->buffer[value->length - 1] != '+') return;
value->length--; value->length--;
} }
static void Handlers_AddTablistEntry(EntityID id, const String* playerName, const String* listName, const String* groupName, uint8_t groupRank) { static void Protocol_AddTablistEntry(EntityID id, const String* playerName, const String* listName, const String* groupName, uint8_t groupRank) {
String rawName; char rawBuffer[STRING_SIZE]; String rawName; char rawBuffer[STRING_SIZE];
String_InitArray(rawName, rawBuffer); String_InitArray(rawName, rawBuffer);
@ -118,10 +118,10 @@ static void Handlers_AddTablistEntry(EntityID id, const String* playerName, cons
TabList_Set(id, &rawName, listName, groupName, groupRank); TabList_Set(id, &rawName, listName, groupName, groupRank);
} }
static void Handlers_CheckName(EntityID id, String* name, String* skin) { static void Protocol_CheckName(EntityID id, String* name, String* skin) {
String colorlessName; char colorlessBuffer[STRING_SIZE]; String colorlessName; char colorlessBuffer[STRING_SIZE];
Handlers_RemoveEndPlus(name); Protocol_RemoveEndPlus(name);
/* Server is only allowed to change our own name colours. */ /* Server is only allowed to change our own name colours. */
if (id == ENTITIES_SELF_ID) { if (id == ENTITIES_SELF_ID) {
String_InitArray(colorlessName, colorlessBuffer); String_InitArray(colorlessName, colorlessBuffer);
@ -130,12 +130,12 @@ static void Handlers_CheckName(EntityID id, String* name, String* skin) {
} }
if (!skin->length) String_Copy(skin, name); if (!skin->length) String_Copy(skin, name);
Handlers_RemoveEndPlus(skin); Protocol_RemoveEndPlus(skin);
String_StripCols(skin); String_StripCols(skin);
} }
static void Classic_ReadAbsoluteLocation(uint8_t* data, EntityID id, bool interpolate); static void Classic_ReadAbsoluteLocation(uint8_t* data, EntityID id, bool interpolate);
static void Handlers_AddEntity(uint8_t* data, EntityID id, const String* displayName, const String* skinName, bool readPosition) { static void Protocol_AddEntity(uint8_t* data, EntityID id, const String* displayName, const String* skinName, bool readPosition) {
struct LocalPlayer* p; struct LocalPlayer* p;
struct NetPlayer* pl; struct NetPlayer* pl;
@ -167,7 +167,7 @@ static void Handlers_AddEntity(uint8_t* data, EntityID id, const String* display
p->SpawnHeadX = p->Base.HeadX; p->SpawnHeadX = p->Base.HeadX;
} }
void Handlers_RemoveEntity(EntityID id) { void Protocol_RemoveEntity(EntityID id) {
struct Entity* entity = Entities.List[id]; struct Entity* entity = Entities.List[id];
if (!entity) return; if (!entity) return;
if (id != ENTITIES_SELF_ID) Entities_Remove(id); if (id != ENTITIES_SELF_ID) Entities_Remove(id);
@ -178,7 +178,7 @@ void Handlers_RemoveEntity(EntityID id) {
Classic_TabList_Reset(id); Classic_TabList_Reset(id);
} }
static void Handlers_UpdateLocation(EntityID playerId, struct LocationUpdate* update, bool interpolate) { static void Protocol_UpdateLocation(EntityID playerId, struct LocationUpdate* update, bool interpolate) {
struct Entity* entity = Entities.List[playerId]; struct Entity* entity = Entities.List[playerId];
if (entity) { if (entity) {
entity->VTABLE->SetLocation(entity, update, interpolate); entity->VTABLE->SetLocation(entity, update, interpolate);
@ -305,7 +305,7 @@ void Classic_WriteChat(const String* text, bool partial) {
*data++ = OPCODE_MESSAGE; *data++ = OPCODE_MESSAGE;
{ {
*data++ = Server.SupportsPartialMessages ? partial : ENTITIES_SELF_ID; *data++ = Server.SupportsPartialMessages ? partial : ENTITIES_SELF_ID;
Handlers_WriteString(data, text); data += STRING_SIZE; Protocol_WriteString(data, text); data += STRING_SIZE;
} }
Server.WriteBuffer = data; Server.WriteBuffer = data;
} }
@ -318,7 +318,7 @@ void Classic_WritePosition(Vector3 pos, float rotY, float headX) {
*data++ = OPCODE_ENTITY_TELEPORT; *data++ = OPCODE_ENTITY_TELEPORT;
{ {
payload = cpe_sendHeldBlock ? Inventory_SelectedBlock : ENTITIES_SELF_ID; payload = cpe_sendHeldBlock ? Inventory_SelectedBlock : ENTITIES_SELF_ID;
Handlers_WriteBlock(data, payload); Protocol_WriteBlock(data, payload);
x = (int)(pos.X * 32); x = (int)(pos.X * 32);
y = (int)(pos.Y * 32) + 51; y = (int)(pos.Y * 32) + 51;
z = (int)(pos.Z * 32); z = (int)(pos.Z * 32);
@ -347,21 +347,21 @@ void Classic_WriteSetBlock(int x, int y, int z, bool place, BlockID block) {
Stream_SetU16_BE(data, y); data += 2; Stream_SetU16_BE(data, y); data += 2;
Stream_SetU16_BE(data, z); data += 2; Stream_SetU16_BE(data, z); data += 2;
*data++ = place; *data++ = place;
Handlers_WriteBlock(data, block); Protocol_WriteBlock(data, block);
} }
Server.WriteBuffer = data; Server.WriteBuffer = data;
} }
void Classic_WriteLogin(const String* username, const String* verKey) { void Classic_SendLogin(const String* username, const String* verKey) {
uint8_t* data = Server.WriteBuffer; uint8_t data[131];
*data++ = OPCODE_HANDSHAKE; data[0] = OPCODE_HANDSHAKE;
{ {
*data++ = 7; /* protocol version */ data[1] = 7; /* protocol version */
Handlers_WriteString(data, username); data += STRING_SIZE; Protocol_WriteString(&data[2], username);
Handlers_WriteString(data, verKey); data += STRING_SIZE; Protocol_WriteString(&data[66], verKey);
*data++ = Game_UseCPE ? 0x42 : 0x00; data[130] = Game_UseCPE ? 0x42 : 0x00;
} }
Server.WriteBuffer = data; Server.SendData(data, 131);
} }
static void Classic_Handshake(uint8_t* data) { static void Classic_Handshake(uint8_t* data) {
@ -371,8 +371,8 @@ static void Classic_Handshake(uint8_t* data) {
Server.MOTD.length = 0; Server.MOTD.length = 0;
data++; /* protocol version */ data++; /* protocol version */
Handlers_ReadString(&data, &Server.Name); Protocol_ReadString(&data, &Server.Name);
Handlers_ReadString(&data, &Server.MOTD); Protocol_ReadString(&data, &Server.MOTD);
Chat_SetLogName(&Server.Name); Chat_SetLogName(&Server.Name);
hacks = &LocalPlayer_Instance.Hacks; hacks = &LocalPlayer_Instance.Hacks;
@ -536,7 +536,7 @@ static void Classic_SetBlock(uint8_t* data) {
z = Stream_GetU16_BE(&data[4]); z = Stream_GetU16_BE(&data[4]);
data += 6; data += 6;
Handlers_ReadBlock(data, block); Protocol_ReadBlock(data, block);
if (World_Contains(x, y, z)) { if (World_Contains(x, y, z)) {
Game_UpdateBlock(x, y, z, block); Game_UpdateBlock(x, y, z, block);
} }
@ -551,12 +551,12 @@ static void Classic_AddEntity(uint8_t* data) {
String_InitArray(skin, skinBuffer); String_InitArray(skin, skinBuffer);
id = *data++; id = *data++;
Handlers_ReadString(&data, &name); Protocol_ReadString(&data, &name);
Handlers_CheckName(id, &name, &skin); Protocol_CheckName(id, &name, &skin);
Handlers_AddEntity(data, id, &name, &skin, true); Protocol_AddEntity(data, id, &name, &skin, true);
/* Workaround for some servers that declare support for ExtPlayerList but don't send ExtAddPlayerName */ /* Workaround for some servers that declare support for ExtPlayerList but don't send ExtAddPlayerName */
Handlers_AddTablistEntry(id, &name, &name, &group, 0); Protocol_AddTablistEntry(id, &name, &name, &group, 0);
Classic_TabList_Set(id); Classic_TabList_Set(id);
} }
@ -578,7 +578,7 @@ static void Classic_RelPosAndOrientationUpdate(uint8_t* data) {
headX = Math_Packed2Deg(*data++); headX = Math_Packed2Deg(*data++);
LocationUpdate_MakePosAndOri(&update, pos, rotY, headX, true); LocationUpdate_MakePosAndOri(&update, pos, rotY, headX, true);
Handlers_UpdateLocation(id, &update, true); Protocol_UpdateLocation(id, &update, true);
} }
static void Classic_RelPositionUpdate(uint8_t* data) { static void Classic_RelPositionUpdate(uint8_t* data) {
@ -591,7 +591,7 @@ static void Classic_RelPositionUpdate(uint8_t* data) {
pos.Z = (int8_t)(*data++) / 32.0f; pos.Z = (int8_t)(*data++) / 32.0f;
LocationUpdate_MakePos(&update, pos, true); LocationUpdate_MakePos(&update, pos, true);
Handlers_UpdateLocation(id, &update, true); Protocol_UpdateLocation(id, &update, true);
} }
static void Classic_OrientationUpdate(uint8_t* data) { static void Classic_OrientationUpdate(uint8_t* data) {
@ -603,12 +603,12 @@ static void Classic_OrientationUpdate(uint8_t* data) {
headX = Math_Packed2Deg(*data++); headX = Math_Packed2Deg(*data++);
LocationUpdate_MakeOri(&update, rotY, headX); LocationUpdate_MakeOri(&update, rotY, headX);
Handlers_UpdateLocation(id, &update, true); Protocol_UpdateLocation(id, &update, true);
} }
static void Classic_RemoveEntity(uint8_t* data) { static void Classic_RemoveEntity(uint8_t* data) {
EntityID id = *data; EntityID id = *data;
Handlers_RemoveEntity(id); Protocol_RemoveEntity(id);
} }
static void Classic_Message(uint8_t* data) { static void Classic_Message(uint8_t* data) {
@ -624,7 +624,7 @@ static void Classic_Message(uint8_t* data) {
if (type == 0xFF) String_AppendConst(&text, "&e"); if (type == 0xFF) String_AppendConst(&text, "&e");
type = MSG_TYPE_NORMAL; type = MSG_TYPE_NORMAL;
} }
Handlers_ReadString(&data, &text); Protocol_ReadString(&data, &text);
/* WoM detail messages (used e.g. for fCraft server compass) */ /* WoM detail messages (used e.g. for fCraft server compass) */
if (String_CaselessStarts(&text, &detailMsg)) { if (String_CaselessStarts(&text, &detailMsg)) {
@ -640,7 +640,7 @@ static void Classic_Kick(uint8_t* data) {
String reason; char reasonBuffer[STRING_SIZE]; String reason; char reasonBuffer[STRING_SIZE];
String_InitArray(reason, reasonBuffer); String_InitArray(reason, reasonBuffer);
Handlers_ReadString(&data, &reason); Protocol_ReadString(&data, &reason);
Game_Disconnect(&title, &reason); Game_Disconnect(&title, &reason);
} }
@ -677,7 +677,7 @@ static void Classic_ReadAbsoluteLocation(uint8_t* data, EntityID id, bool interp
if (id == ENTITIES_SELF_ID) classic_receivedFirstPos = true; if (id == ENTITIES_SELF_ID) classic_receivedFirstPos = true;
LocationUpdate_MakePosAndOri(&update, pos, rotY, headX, false); LocationUpdate_MakePosAndOri(&update, pos, rotY, headX, false);
Handlers_UpdateLocation(id, &update, interpolate); Protocol_UpdateLocation(id, &update, interpolate);
} }
static void Classic_Reset(void) { static void Classic_Reset(void) {
@ -724,63 +724,54 @@ const char* cpe_clientExtensions[30] = {
static void CPE_SetMapEnvUrl(uint8_t* data); static void CPE_SetMapEnvUrl(uint8_t* data);
#define Ext_Deg2Packed(x) ((int)((x) * 65536.0f / 360.0f)) #define Ext_Deg2Packed(x) ((int)((x) * 65536.0f / 360.0f))
void CPE_WritePlayerClick(MouseButton button, bool pressed, uint8_t targetId, struct PickedPos* pos) { void CPE_SendPlayerClick(MouseButton button, bool pressed, uint8_t targetId, struct PickedPos* pos) {
struct Entity* p = &LocalPlayer_Instance.Base; struct Entity* p = &LocalPlayer_Instance.Base;
uint8_t* data = Server.WriteBuffer; uint8_t data[15];
*data++ = OPCODE_PLAYER_CLICK;
data[0] = OPCODE_PLAYER_CLICK;
{ {
*data++ = button; data[1] = button;
*data++ = !pressed; data[2] = !pressed;
Stream_SetU16_BE(data, Ext_Deg2Packed(p->HeadY)); data += 2; Stream_SetU16_BE(&data[3], Ext_Deg2Packed(p->HeadY));
Stream_SetU16_BE(data, Ext_Deg2Packed(p->HeadX)); data += 2; Stream_SetU16_BE(&data[5], Ext_Deg2Packed(p->HeadX));
*data++ = targetId; data[7] = targetId;
Stream_SetU16_BE(data, pos->BlockPos.X); data += 2; Stream_SetU16_BE(&data[8], pos->BlockPos.X);
Stream_SetU16_BE(data, pos->BlockPos.Y); data += 2; Stream_SetU16_BE(&data[10], pos->BlockPos.Y);
Stream_SetU16_BE(data, pos->BlockPos.Z); data += 2; Stream_SetU16_BE(&data[12], pos->BlockPos.Z);
*data = 255; data[14] = 255;
/* Our own face values differ from CPE block face */ /* Our own face values differ from CPE block face */
switch (pos->Closest) { switch (pos->Closest) {
case FACE_XMAX: *data = 0; break; case FACE_XMAX: data[14] = 0; break;
case FACE_XMIN: *data = 1; break; case FACE_XMIN: data[14] = 1; break;
case FACE_YMAX: *data = 2; break; case FACE_YMAX: data[14] = 2; break;
case FACE_YMIN: *data = 3; break; case FACE_YMIN: data[14] = 3; break;
case FACE_ZMAX: *data = 4; break; case FACE_ZMAX: data[14] = 4; break;
case FACE_ZMIN: *data = 5; break; case FACE_ZMIN: data[14] = 5; break;
} }
data++;
} }
Server.WriteBuffer += 15; Server.SendData(data, 15);
} }
static void CPE_WriteExtInfo(const String* appName, int extensionsCount) { static void CPE_SendExtInfo(const String* appName, int extsCount) {
uint8_t* data = Server.WriteBuffer; uint8_t data[67];
*data++ = OPCODE_EXT_INFO; data[0] = OPCODE_EXT_INFO;
{ {
Handlers_WriteString(data, appName); data += STRING_SIZE; Protocol_WriteString(&data[1], appName);
Stream_SetU16_BE(data, extensionsCount); data += 2; Stream_SetU16_BE(&data[65], extsCount);
} }
Server.WriteBuffer = data; Server.SendData(data, 67);
} }
static void CPE_WriteExtEntry(const String* extensionName, int extensionVersion) { static void CPE_SendExtEntry(const String* extName, int extVersion) {
uint8_t* data = Server.WriteBuffer; uint8_t data[69];
*data++ = OPCODE_EXT_ENTRY; data[0] = OPCODE_EXT_ENTRY;
{ {
Handlers_WriteString(data, extensionName); data += STRING_SIZE; Handlers_WriteString(&data[1], extName);
Stream_SetU32_BE(data, extensionVersion); data += 4; Stream_SetU32_BE(&data[65], extVersion);
} }
Server.WriteBuffer = data; Server.SendData(data, 69);
}
static void CPE_WriteCustomBlockLevel(uint8_t version) {
uint8_t* data = Server.WriteBuffer;
*data++ = OPCODE_CUSTOM_BLOCK_LEVEL;
{
*data++ = version;
}
Server.WriteBuffer = data;
} }
static void CPE_WriteTwoWayPing(bool serverToClient, int payload) { static void CPE_WriteTwoWayPing(bool serverToClient, int payload) {
@ -797,6 +788,7 @@ static void CPE_SendCpeExtInfoReply(void) {
int count = Array_Elems(cpe_clientExtensions); int count = Array_Elems(cpe_clientExtensions);
String name; String name;
int i, ver; int i, ver;
uint8_t data[69];
if (cpe_serverExtensionsCount) return; if (cpe_serverExtensionsCount) return;
@ -812,9 +804,7 @@ static void CPE_SendCpeExtInfoReply(void) {
#else #else
if (!Game_AllowCustomBlocks) count -= 2; if (!Game_AllowCustomBlocks) count -= 2;
#endif #endif
CPE_SendExtInfo(&Server.AppName, count);
CPE_WriteExtInfo(&Server.AppName, count);
Net_SendPacket();
for (i = 0; i < Array_Elems(cpe_clientExtensions); i++) { for (i = 0; i < Array_Elems(cpe_clientExtensions); i++) {
name = String_FromReadonly(cpe_clientExtensions[i]); name = String_FromReadonly(cpe_clientExtensions[i]);
@ -838,9 +828,7 @@ static void CPE_SendCpeExtInfoReply(void) {
#ifndef EXTENDED_BLOCKS #ifndef EXTENDED_BLOCKS
if (String_CaselessEqualsConst(&name, "ExtendedBlocks")) continue; if (String_CaselessEqualsConst(&name, "ExtendedBlocks")) continue;
#endif #endif
CPE_SendExtEntry(&name, ver);
CPE_WriteExtEntry(&name, ver);
Net_SendPacket();
} }
} }
@ -849,7 +837,7 @@ static void CPE_ExtInfo(uint8_t* data) {
String appName; char appNameBuffer[STRING_SIZE]; String appName; char appNameBuffer[STRING_SIZE];
String_InitArray(appName, appNameBuffer); String_InitArray(appName, appNameBuffer);
Handlers_ReadString(&data, &appName); Protocol_ReadString(&data, &appName);
Chat_Add1("Server software: %s", &appName); Chat_Add1("Server software: %s", &appName);
cpe_needD3Fix = String_CaselessStarts(&appName, &d3Server); cpe_needD3Fix = String_CaselessStarts(&appName, &d3Server);
@ -864,7 +852,7 @@ static void CPE_ExtEntry(uint8_t* data) {
int extVersion; int extVersion;
String_InitArray(ext, extNameBuffer); String_InitArray(ext, extNameBuffer);
Handlers_ReadString(&data, &ext); Protocol_ReadString(&data, &ext);
extVersion = Stream_GetU32_BE(data); extVersion = Stream_GetU32_BE(data);
Platform_Log2("cpe ext: %s, %i", &ext, &extVersion); Platform_Log2("cpe ext: %s, %i", &ext, &extVersion);
@ -934,8 +922,10 @@ static void CPE_SetClickDistance(uint8_t* data) {
} }
static void CPE_CustomBlockLevel(uint8_t* data) { static void CPE_CustomBlockLevel(uint8_t* data) {
CPE_WriteCustomBlockLevel(1); /* reply with version 1 level support */
Net_SendPacket(); uint8_t reply[2] = { OPCODE_CUSTOM_BLOCK_LEVEL, 1 };
Server.SendData(reply, 2);
Game_UseCPEBlocks = true; Game_UseCPEBlocks = true;
Event_RaiseVoid(&BlockEvents.PermissionsChanged); Event_RaiseVoid(&BlockEvents.PermissionsChanged);
} }
@ -944,7 +934,7 @@ static void CPE_HoldThis(uint8_t* data) {
BlockID block; BlockID block;
bool canChange; bool canChange;
Handlers_ReadBlock(data, block); Protocol_ReadBlock(data, block);
canChange = *data == 0; canChange = *data == 0;
Inventory.CanChangeSelected = true; Inventory.CanChangeSelected = true;
@ -960,7 +950,7 @@ static void CPE_SetTextHotkey(uint8_t* data) {
data += STRING_SIZE; /* skip label */ data += STRING_SIZE; /* skip label */
String_InitArray(action, actionBuffer); String_InitArray(action, actionBuffer);
Handlers_ReadString(&data, &action); Protocol_ReadString(&data, &action);
keyCode = Stream_GetU32_BE(data); data += 4; keyCode = Stream_GetU32_BE(data); data += 4;
keyMods = *data; keyMods = *data;
@ -991,18 +981,18 @@ static void CPE_ExtAddPlayerName(uint8_t* data) {
String_InitArray(groupName, groupNameBuffer); String_InitArray(groupName, groupNameBuffer);
id = data[1]; data += 2; id = data[1]; data += 2;
Handlers_ReadString(&data, &playerName); Protocol_ReadString(&data, &playerName);
Handlers_ReadString(&data, &listName); Protocol_ReadString(&data, &listName);
Handlers_ReadString(&data, &groupName); Protocol_ReadString(&data, &groupName);
groupRank = *data; groupRank = *data;
String_StripCols(&playerName); String_StripCols(&playerName);
Handlers_RemoveEndPlus(&playerName); Protocol_RemoveEndPlus(&playerName);
Handlers_RemoveEndPlus(&listName); Protocol_RemoveEndPlus(&listName);
/* Workarond for server software that declares support for ExtPlayerList, but sends AddEntity then AddPlayerName */ /* Workarond for server software that declares support for ExtPlayerList, but sends AddEntity then AddPlayerName */
Classic_TabList_Reset(id); Classic_TabList_Reset(id);
Handlers_AddTablistEntry(id, &playerName, &listName, &groupName, groupRank); Protocol_AddTablistEntry(id, &playerName, &listName, &groupName, groupRank);
} }
static void CPE_ExtAddEntity(uint8_t* data) { static void CPE_ExtAddEntity(uint8_t* data) {
@ -1013,11 +1003,11 @@ static void CPE_ExtAddEntity(uint8_t* data) {
String_InitArray(skin, skinBuffer); String_InitArray(skin, skinBuffer);
id = *data++; id = *data++;
Handlers_ReadString(&data, &name); Protocol_ReadString(&data, &name);
Handlers_ReadString(&data, &skin); Protocol_ReadString(&data, &skin);
Handlers_CheckName(id, &name, &skin); Protocol_CheckName(id, &name, &skin);
Handlers_AddEntity(data, id, &name, &skin, false); Protocol_AddEntity(data, id, &name, &skin, false);
} }
static void CPE_ExtRemovePlayerName(uint8_t* data) { static void CPE_ExtRemovePlayerName(uint8_t* data) {
@ -1080,7 +1070,7 @@ static void CPE_SetEnvCol(uint8_t* data) {
static void CPE_SetBlockPermission(uint8_t* data) { static void CPE_SetBlockPermission(uint8_t* data) {
BlockID block; BlockID block;
Handlers_ReadBlock(data, block); Protocol_ReadBlock(data, block);
Blocks.CanPlace[block] = *data++ != 0; Blocks.CanPlace[block] = *data++ != 0;
Blocks.CanDelete[block] = *data++ != 0; Blocks.CanDelete[block] = *data++ != 0;
@ -1094,7 +1084,7 @@ static void CPE_ChangeModel(uint8_t* data) {
String_InitArray(model, modelBuffer); String_InitArray(model, modelBuffer);
id = *data++; id = *data++;
Handlers_ReadString(&data, &model); Protocol_ReadString(&data, &model);
entity = Entities.List[id]; entity = Entities.List[id];
if (entity) { Entity_SetModel(entity, &model); } if (entity) { Entity_SetModel(entity, &model); }
@ -1151,11 +1141,11 @@ static void CPE_ExtAddEntity2(uint8_t* data) {
String_InitArray(skin, skinBuffer); String_InitArray(skin, skinBuffer);
id = *data++; id = *data++;
Handlers_ReadString(&data, &name); Protocol_ReadString(&data, &name);
Handlers_ReadString(&data, &skin); Protocol_ReadString(&data, &skin);
Handlers_CheckName(id, &name, &skin); Protocol_CheckName(id, &name, &skin);
Handlers_AddEntity(data, id, &name, &skin, true); Protocol_AddEntity(data, id, &name, &skin, true);
} }
#define BULK_MAX_BLOCKS 256 #define BULK_MAX_BLOCKS 256
@ -1217,7 +1207,7 @@ static void CPE_SetMapEnvUrl(uint8_t* data) {
String url; char urlBuffer[STRING_SIZE]; String url; char urlBuffer[STRING_SIZE];
String_InitArray(url, urlBuffer); String_InitArray(url, urlBuffer);
Handlers_ReadString(&data, &url); Protocol_ReadString(&data, &url);
if (!Game_AllowServerTextures) return; if (!Game_AllowServerTextures) return;
if (!url.length) { if (!url.length) {
@ -1318,8 +1308,8 @@ static void CPE_TwoWayPing(uint8_t* data) {
static void CPE_SetInventoryOrder(uint8_t* data) { static void CPE_SetInventoryOrder(uint8_t* data) {
BlockID block, order; BlockID block, order;
Handlers_ReadBlock(data, block); Protocol_ReadBlock(data, block);
Handlers_ReadBlock(data, order); Protocol_ReadBlock(data, order);
Inventory_Remove(block); Inventory_Remove(block);
if (order) { Inventory.Map[order - 1] = block; } if (order) { Inventory.Map[order - 1] = block; }
@ -1403,12 +1393,12 @@ static BlockID BlockDefs_DefineBlockCommonStart(uint8_t** ptr, bool uniqueSideTe
uint8_t sound; uint8_t sound;
uint8_t* data = *ptr; uint8_t* data = *ptr;
Handlers_ReadBlock(data, block); Protocol_ReadBlock(data, block);
didBlockLight = Blocks.BlocksLight[block]; didBlockLight = Blocks.BlocksLight[block];
Block_ResetProps(block); Block_ResetProps(block);
String_InitArray(name, nameBuffer); String_InitArray(name, nameBuffer);
Handlers_ReadString(&data, &name); Protocol_ReadString(&data, &name);
Block_SetName(block, &name); Block_SetName(block, &name);
Block_SetCollide(block, *data++); Block_SetCollide(block, *data++);
@ -1479,7 +1469,7 @@ static void BlockDefs_UndefineBlock(uint8_t* data) {
BlockID block; BlockID block;
bool didBlockLight; bool didBlockLight;
Handlers_ReadBlock(data, block); Protocol_ReadBlock(data, block);
didBlockLight = Blocks.BlocksLight[block]; didBlockLight = Blocks.BlocksLight[block];
Block_ResetProps(block); Block_ResetProps(block);
@ -1551,14 +1541,14 @@ static void BlockDefs_Reset(void) {
/*########################################################################################################################* /*########################################################################################################################*
*-----------------------------------------------------Public handlers-----------------------------------------------------* *-----------------------------------------------------Public handlers-----------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void Handlers_Reset(void) { void Protocol_Reset(void) {
Classic_Reset(); Classic_Reset();
CPE_Reset(); CPE_Reset();
BlockDefs_Reset(); BlockDefs_Reset();
WoM_Reset(); WoM_Reset();
} }
void Handlers_Tick(void) { void Protocol_Tick(void) {
Classic_Tick(); Classic_Tick();
CPE_Tick(); CPE_Tick();
WoM_Tick(); WoM_Tick();

View File

@ -3,20 +3,20 @@
#include "Input.h" #include "Input.h"
#include "String.h" #include "String.h"
#include "Vectors.h" #include "Vectors.h"
/* Implements network protocol handlers for original classic, CPE, and WoM textures. /* Implements network protocols for original classic, CPE, and WoM textures.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/ */
struct PickedPos; struct PickedPos;
struct Stream; struct Stream;
void Handlers_RemoveEntity(EntityID id); void Protocol_RemoveEntity(EntityID id);
void Handlers_Reset(void); void Protocol_Reset(void);
void Handlers_Tick(void); void Protocol_Tick(void);
extern bool cpe_needD3Fix; extern bool cpe_needD3Fix;
void Classic_WriteChat(const String* text, bool partial); void Classic_WriteChat(const String* text, bool partial);
void Classic_WritePosition(Vector3 pos, float rotY, float headX); void Classic_WritePosition(Vector3 pos, float rotY, float headX);
void Classic_WriteSetBlock(int x, int y, int z, bool place, BlockID block); void Classic_WriteSetBlock(int x, int y, int z, bool place, BlockID block);
void Classic_WriteLogin(const String* username, const String* verKey); void Classic_SendLogin(const String* username, const String* verKey);
void CPE_WritePlayerClick(MouseButton button, bool pressed, uint8_t targetId, struct PickedPos* pos); void CPE_SendPlayerClick(MouseButton button, bool pressed, uint8_t targetId, struct PickedPos* pos);
#endif #endif

View File

@ -445,13 +445,13 @@ ReturnCode File_Append(FileHandle* file, const String* path) {
return File_Seek(*file, 0, FILE_SEEKFROM_END); return File_Seek(*file, 0, FILE_SEEKFROM_END);
} }
ReturnCode File_Read(FileHandle file, uint8_t* buffer, uint32_t count, uint32_t* bytesRead) { ReturnCode File_Read(FileHandle file, uint8_t* data, uint32_t count, uint32_t* bytesRead) {
BOOL success = ReadFile(file, buffer, count, bytesRead, NULL); BOOL success = ReadFile(file, data, count, bytesRead, NULL);
return success ? 0 : GetLastError(); return success ? 0 : GetLastError();
} }
ReturnCode File_Write(FileHandle file, const uint8_t* buffer, uint32_t count, uint32_t* bytesWrote) { ReturnCode File_Write(FileHandle file, const uint8_t* data, uint32_t count, uint32_t* bytesWrote) {
BOOL success = WriteFile(file, buffer, count, bytesWrote, NULL); BOOL success = WriteFile(file, data, count, bytesWrote, NULL);
return success ? 0 : GetLastError(); return success ? 0 : GetLastError();
} }
@ -579,13 +579,13 @@ ReturnCode File_Append(FileHandle* file, const String* path) {
return File_Seek(*file, 0, FILE_SEEKFROM_END); return File_Seek(*file, 0, FILE_SEEKFROM_END);
} }
ReturnCode File_Read(FileHandle file, uint8_t* buffer, uint32_t count, uint32_t* bytesRead) { ReturnCode File_Read(FileHandle file, uint8_t* data, uint32_t count, uint32_t* bytesRead) {
*bytesRead = read(file, buffer, count); *bytesRead = read(file, data, count);
return *bytesRead == -1 ? errno : 0; return *bytesRead == -1 ? errno : 0;
} }
ReturnCode File_Write(FileHandle file, const uint8_t* buffer, uint32_t count, uint32_t* bytesWrote) { ReturnCode File_Write(FileHandle file, const uint8_t* data, uint32_t count, uint32_t* bytesWrote) {
*bytesWrote = write(file, buffer, count); *bytesWrote = write(file, data, count);
return *bytesWrote == -1 ? errno : 0; return *bytesWrote == -1 ? errno : 0;
} }
@ -1375,29 +1375,29 @@ ReturnCode Socket_Connect(SocketHandle socket, const String* ip, int port) {
return res == -1 ? Socket__Error() : 0; return res == -1 ? Socket__Error() : 0;
} }
ReturnCode Socket_Read(SocketHandle socket, uint8_t* buffer, uint32_t count, uint32_t* modified) { ReturnCode Socket_Read(SocketHandle socket, uint8_t* data, uint32_t count, uint32_t* modified) {
#ifdef CC_BUILD_WEB #ifdef CC_BUILD_WEB
/* recv only reads one WebSocket frame at most, hence call it multiple times */ /* recv only reads one WebSocket frame at most, hence call it multiple times */
int recvCount = 0, pending; int recvCount = 0, pending;
*modified = 0; *modified = 0;
while (count && !Socket_Available(socket, &pending) && pending) { while (count && !Socket_Available(socket, &pending) && pending) {
recvCount = recv(socket, buffer, count, 0); recvCount = recv(socket, data, count, 0);
if (recvCount == -1) return Socket__Error(); if (recvCount == -1) return Socket__Error();
*modified += recvCount; *modified += recvCount;
buffer += recvCount; count -= recvCount; data += recvCount; count -= recvCount;
} }
return 0; return 0;
#else #else
int recvCount = recv(socket, buffer, count, 0); int recvCount = recv(socket, data, count, 0);
if (recvCount != -1) { *modified = recvCount; return 0; } if (recvCount != -1) { *modified = recvCount; return 0; }
*modified = 0; return Socket__Error(); *modified = 0; return Socket__Error();
#endif #endif
} }
ReturnCode Socket_Write(SocketHandle socket, uint8_t* buffer, uint32_t count, uint32_t* modified) { ReturnCode Socket_Write(SocketHandle socket, const uint8_t* data, uint32_t count, uint32_t* modified) {
int sentCount = send(socket, buffer, count, 0); int sentCount = send(socket, data, count, 0);
if (sentCount != -1) { *modified = sentCount; return 0; } if (sentCount != -1) { *modified = sentCount; return 0; }
*modified = 0; return Socket__Error(); *modified = 0; return Socket__Error();
} }

View File

@ -139,9 +139,9 @@ ReturnCode File_Open(FileHandle* file, const String* path);
/* Attempts to open (or create) a file, for appending data to the end of the file. */ /* Attempts to open (or create) a file, for appending data to the end of the file. */
ReturnCode File_Append(FileHandle* file, const String* path); ReturnCode File_Append(FileHandle* file, const String* path);
/* Attempts to read data from the file. */ /* Attempts to read data from the file. */
ReturnCode File_Read(FileHandle file, uint8_t* buffer, uint32_t count, uint32_t* bytesRead); ReturnCode File_Read(FileHandle file, uint8_t* data, uint32_t count, uint32_t* bytesRead);
/* Attempts to write data to the file. */ /* Attempts to write data to the file. */
ReturnCode File_Write(FileHandle file, const uint8_t* buffer, uint32_t count, uint32_t* bytesWrote); ReturnCode File_Write(FileHandle file, const uint8_t* data, uint32_t count, uint32_t* bytesWrote);
/* Attempts to close the given file. */ /* Attempts to close the given file. */
ReturnCode File_Close(FileHandle file); ReturnCode File_Close(FileHandle file);
/* Attempts to seek to a position in the given file. */ /* Attempts to seek to a position in the given file. */
@ -211,9 +211,9 @@ CC_API ReturnCode Socket_GetError(SocketHandle socket, ReturnCode* result);
/* Attempts to initalise a connection to the given IP address:port. */ /* Attempts to initalise a connection to the given IP address:port. */
CC_API ReturnCode Socket_Connect(SocketHandle socket, const String* ip, int port); CC_API ReturnCode Socket_Connect(SocketHandle socket, const String* ip, int port);
/* Attempts to read data from the given socket. */ /* Attempts to read data from the given socket. */
CC_API ReturnCode Socket_Read(SocketHandle socket, uint8_t* buffer, uint32_t count, uint32_t* modified); CC_API ReturnCode Socket_Read(SocketHandle socket, uint8_t* data, uint32_t count, uint32_t* modified);
/* Attempts to write data to the given socket. */ /* Attempts to write data to the given socket. */
CC_API ReturnCode Socket_Write(SocketHandle socket, uint8_t* buffer, uint32_t count, uint32_t* modified); CC_API ReturnCode Socket_Write(SocketHandle socket, const uint8_t* data, uint32_t count, uint32_t* modified);
/* Attempts to close the given socket. */ /* Attempts to close the given socket. */
CC_API ReturnCode Socket_Close(SocketHandle socket); CC_API ReturnCode Socket_Close(SocketHandle socket);
/* Attempts to poll the given socket for readability or writability. */ /* Attempts to poll the given socket for readability or writability. */

View File

@ -213,7 +213,7 @@ static void SPConnection_SendChat(const String* text) {
} }
static void SPConnection_SendPosition(Vector3 pos, float rotY, float headX) { } static void SPConnection_SendPosition(Vector3 pos, float rotY, float headX) { }
static void SPConnection_SendPlayerClick(MouseButton button, bool pressed, EntityID targetId, struct PickedPos* pos) { } static void SPConnection_SendData(const uint8_t* data, uint32_t len) { }
static void SPConnection_Tick(struct ScheduledTask* task) { static void SPConnection_Tick(struct ScheduledTask* task) {
if (Server.Disconnected) return; if (Server.Disconnected) return;
@ -233,7 +233,7 @@ static void SPConnection_Init(void) {
Server.SendBlock = SPConnection_SendBlock; Server.SendBlock = SPConnection_SendBlock;
Server.SendChat = SPConnection_SendChat; Server.SendChat = SPConnection_SendChat;
Server.SendPosition = SPConnection_SendPosition; Server.SendPosition = SPConnection_SendPosition;
Server.SendPlayerClick = SPConnection_SendPlayerClick; Server.SendData = SPConnection_SendData;
Server.SupportsFullCP437 = !Game_ClassicMode; Server.SupportsFullCP437 = !Game_ClassicMode;
Server.SupportsPartialMessages = true; Server.SupportsPartialMessages = true;
@ -271,9 +271,8 @@ static void MPConnection_FinishConnect(void) {
net_readCurrent = net_readBuffer; net_readCurrent = net_readBuffer;
Server.WriteBuffer = net_writeBuffer; Server.WriteBuffer = net_writeBuffer;
Handlers_Reset(); Protocol_Reset();
Classic_WriteLogin(&Game_Username, &Game_Mppass); Classic_SendLogin(&Game_Username, &Game_Mppass);
Net_SendPacket();
net_lastPacket = DateTime_CurrentUTC_MS(); net_lastPacket = DateTime_CurrentUTC_MS();
} }
@ -350,13 +349,12 @@ static void MPConnection_SendBlock(int x, int y, int z, BlockID old, BlockID now
} }
static void MPConnection_SendChat(const String* text) { static void MPConnection_SendChat(const String* text) {
String left, part; String left;
if (!text->length || net_connecting) return; if (!text->length || net_connecting) return;
left = *text; left = *text;
while (left.length > STRING_SIZE) { while (left.length > STRING_SIZE) {
part = String_UNSAFE_Substring(&left, 0, STRING_SIZE); Classic_WriteChat(&left, true);
Classic_WriteChat(&part, true);
Net_SendPacket(); Net_SendPacket();
left = String_UNSAFE_SubstringAt(&left, STRING_SIZE); left = String_UNSAFE_SubstringAt(&left, STRING_SIZE);
} }
@ -370,11 +368,6 @@ static void MPConnection_SendPosition(Vector3 pos, float rotY, float headX) {
Net_SendPacket(); Net_SendPacket();
} }
static void MPConnection_SendPlayerClick(MouseButton button, bool pressed, EntityID targetId, struct PickedPos* pos) {
CPE_WritePlayerClick(button, pressed, targetId, pos);
Net_SendPacket();
}
static void MPConnection_CheckDisconnection(double delta) { static void MPConnection_CheckDisconnection(double delta) {
const static String title = String_FromConst("Disconnected!"); const static String title = String_FromConst("Disconnected!");
const static String reason = String_FromConst("You've lost connection to the server"); const static String reason = String_FromConst("You've lost connection to the server");
@ -484,7 +477,7 @@ static void MPConnection_Tick(struct ScheduledTask* task) {
/* Network is ticked 60 times a second. We only send position updates 20 times a second */ /* Network is ticked 60 times a second. We only send position updates 20 times a second */
if ((ticks % 3) == 0) { if ((ticks % 3) == 0) {
Server_CheckAsyncResources(); Server_CheckAsyncResources();
Handlers_Tick(); Protocol_Tick();
/* Have any packets been written? */ /* Have any packets been written? */
if (Server.WriteBuffer != net_writeBuffer) { if (Server.WriteBuffer != net_writeBuffer) {
Net_SendPacket(); Net_SendPacket();
@ -493,24 +486,25 @@ static void MPConnection_Tick(struct ScheduledTask* task) {
ticks++; ticks++;
} }
void Net_SendPacket(void) { static void MPConnection_SendData(const uint8_t* data, uint32_t len) {
uint32_t left, wrote; uint32_t wrote;
uint8_t* cur;
ReturnCode res; ReturnCode res;
left = (uint32_t)(Server.WriteBuffer - net_writeBuffer);
Server.WriteBuffer = net_writeBuffer;
if (Server.Disconnected) return; if (Server.Disconnected) return;
while (len) {
res = Socket_Write(net_socket, data, len, &wrote);
/* NOTE: Not immediately disconnecting here, as otherwise we sometimes miss out on kick messages */ /* NOTE: Not immediately disconnecting here, as otherwise we sometimes miss out on kick messages */
cur = net_writeBuffer; if (res || !wrote) { net_writeFailed = true; return; }
while (left) { data += wrote; len -= wrote;
res = Socket_Write(net_socket, cur, left, &wrote);
if (res || !wrote) { net_writeFailed = true; break; }
cur += wrote; left -= wrote;
} }
} }
void Net_SendPacket(void) {
uint32_t len = (uint32_t)(Server.WriteBuffer - net_writeBuffer);
Server.WriteBuffer = net_writeBuffer;
Server.SendData(net_writeBuffer, len);
}
static void MPConnection_Init(void) { static void MPConnection_Init(void) {
Server_ResetState(); Server_ResetState();
Server.IsSinglePlayer = false; Server.IsSinglePlayer = false;
@ -520,7 +514,7 @@ static void MPConnection_Init(void) {
Server.SendBlock = MPConnection_SendBlock; Server.SendBlock = MPConnection_SendBlock;
Server.SendChat = MPConnection_SendChat; Server.SendChat = MPConnection_SendChat;
Server.SendPosition = MPConnection_SendPosition; Server.SendPosition = MPConnection_SendPosition;
Server.SendPlayerClick = MPConnection_SendPlayerClick; Server.SendData = MPConnection_SendData;
net_readCurrent = net_readBuffer; net_readCurrent = net_readBuffer;
Server.WriteBuffer = net_writeBuffer; Server.WriteBuffer = net_writeBuffer;
@ -533,7 +527,7 @@ static void MPConnection_OnNewMap(void) {
/* wipe all existing entities */ /* wipe all existing entities */
for (i = 0; i < ENTITIES_MAX_COUNT; i++) { for (i = 0; i < ENTITIES_MAX_COUNT; i++) {
Handlers_RemoveEntity((EntityID)i); Protocol_RemoveEntity((EntityID)i);
} }
} }
@ -548,7 +542,7 @@ static void MPConnection_Reset(void) {
net_writeFailed = false; net_writeFailed = false;
Block_SetUsedCount(256); Block_SetUsedCount(256);
Handlers_Reset(); Protocol_Reset();
Server_Free(); Server_Free();
} }

View File

@ -59,8 +59,9 @@ CC_VAR extern struct _ServerConnectionData {
void (*SendChat)(const String* text); void (*SendChat)(const String* text);
/* Sends a position update to the server. */ /* Sends a position update to the server. */
void (*SendPosition)(Vector3 pos, float rotY, float headX); void (*SendPosition)(Vector3 pos, float rotY, float headX);
/* Sends a PlayerClick packet to the server. */ /* Sends raw data to the server. */
void (*SendPlayerClick)(MouseButton button, bool pressed, EntityID targetId, struct PickedPos* pos); /* NOTE: Prefer SendBlock/Position/Chat instead, this does not work in singleplayer. */
void (*SendData)(const uint8_t* data, uint32_t len);
/* The current name of the server. (Shows as first line when loading) */ /* The current name of the server. (Shows as first line when loading) */
String Name; String Name;