In this thread I'm going to show you how to make a game-client or client-game communication with packets, instead of using the old quest-client, client-quest communication. Lets start with the game-client, in this example I will send 1 variable to the client.
First start with the HEADER, open your binary source and navigate to UserInterface/Packet.h. Now you will see many headers, create a new one, but search for an empty number. I will use 57, because its not used. GC means it's used for Game -> Client packet, it's just a prefix.
Now add the structure for the packet, this is most important part. Structure is the "body" of the packet, it contains the HEADER as BYTE and the other optional variables. As I said I just want to send one int type to the client, so add it.
typedef struct command_metin2dev_packet
Now navigate to UserInterface/PythonNetworkStream.cpp and add your header to the CMainPacketHeaderMap class. The first parameter of the Set is the HEADER, second is the size of the structure. We will use just static size packets in this tutorial, but the third argument can be dynamic size too.
Set(HEADER_GC_METIN2DEV, CNetworkPacketHeaderMap::TPacketType(sizeof(TPacketGCMetin2Dev), STATIC_SIZE_PACKET));
Now navigate to UserInterface/PythonNtworkStreamPhaseGame.cpp and add the function to the switch.
ret = RecvM2DevPacket();
The name of the function will be RecvM2DevPacket:
Now declarate the function, navigate to UserInterface/PythonNetworkStream.h and add it as public:
Now add the receiver part of the code. Recv "picks" out xy bytes from the buffer and the return type of it is false if there was no data in the buffer by that size otherwise true, which means it was successful.
xy = size of the structure
if (!Recv(sizeof(TPacketGCMetin2Dev), &Metin2DevGC))
Tracen("Recv Metin2DevGC Packet Error");
Now we are calling the BINARY_M2DEV_Test function in game.py and passing the received data.
PyCallClassMemberFunc(m_apoPhaseWnd[PHASE_WINDOW_GAME], "BINARY_M2DEV_Test", Py_BuildValue("(i)", Metin2DevGC.M2int));
This was the client-side of the game-client communication, lets start the server-side:
First of all we need to add the header again, navigate to game/packet.h and add this:
And the structure:
typedef struct packet_metin2dev_packet
Now navigate to game/char.cpp and create a function which sends the packet.
Declare it in the game/char.h:
Now lets add the content of the function. Create a new instance of the structure, set the values of it and send it to the client.
Metin2DevGC.byHeader = HEADER_GC_METIN2DEV;
Metin2DevGC.M2int = GetPlayerID();
Now add the last function to game.py, this will be called by the binary:
def BINARY_M2DEV_Test(self, M2int):
Finally, lets check how it works:
If you have any question or suggestion, please just reply to this topic.
Kind Regards, Sanchez