This is not made by me !!!

Credits: hypersnyper

Welcome to the second installment of the The More You Know™ Lua tutorial series. The idea of these is to give small but extremely useful pockets of information for Lua scripters.


Packet Editing


1. What are Packets?
Packets are small chunks of data exchanged between the client (WoW) and the server (ArcEmu, etc). Packets are used for all communication between players and servers. They are very small, usually in total only a few bytes in size. Thus, packet editing gives you essentially byte-by-byte control of what happens between players and the server.

2. It's complicated.
This isn't easy. There is little documentation about packets available, and most of the packet structures are discovered by looking through the core. C++ knowledge is advised. But, on the upside, you can have heaps of fun once you DO work out how to use them .

3. Packet structures
Every packet that's sent has a type. As in, one packet might be designed to make a monster move, another might be designed to play a sound. And every single one of those types has it's own packet structure. A packet structure is basically a blueprint of what the packet will contain. Packet structures say what bytes will go where within that packet. And, you can't edit packets without knowing their structure.
There's a catch - it's really hard to find these structures. There's the official packet documentation - [Only registered and activated users can see links. ] - but there's hardly anything there. You're best bet is searching the core for what you want.

4. Working through an example
Today I'm going to show you how to change the weather in the game, the manual way. From here on I'm going to assume that you have the [Only registered and activated users can see links. ] in your scripts folder.

Step 1: Create the packet object
Code:
local packet = LuaPacket:CreatePacket(type, size)
Remember how we said that packets have types? Well that's what you do in this step. Every packet type is listed in defined variables.lua, under "opcodes.h" subheading. Hopefully you've already researched what packet you need, so you won't need this. I know I want me a SMSG_WEATHER type packet to change the weather.
size refers to how many bytes of data the packet will contain. The easiest thing to do is make this some high number like 100 to start with.
This command uses the LuaPacket class to create a new packet object that we will use in our other functions.
Code:
local packet = LuaPacket:CreatePacket(SMSG_WEATHER, 100)
Step 2: Add dataz to it
Code:
packet:Write<datatype>(data)
This is where you're packet structure comes in. I've updated the wiki with the SMSG_WEATHER structure for you all to see: [Only registered and activated users can see links. ] . By looking at that site, we need to give the type, density and sound of the weather we want, and then also tack on a 0 at the end (in that order).
The <datatype> is given on that webpage - it's right next the name of the data we're going to add. As in, "type" is a uint32, "density" will be a float, etc. Depending on what those data types are, the <datatype> will change:
Code:
WriteByte			+- Writes an int8
WriteUByte		+- Writes an uint8
WriteShort			+- Writes an int16
WriteUShort		+- Writes an uint16
WriteLong			+- Writes an int32
WriteULong		+- Writes an uint32
WriteFloat			+- Writes a float 
WriteDouble		+- Writes a double
WriteGUID			+- Writes an uint64
So, in my case, to add the data to our initialised packet:
Code:
packet:WriteULong(8) --weather type first: datatype uint32, value: 8 (snow)packet:WriteFloat(2.0) --density next: datatype float, value: 2.0 (max density)packet:WriteULong(8538) --sound next: datatype uint32, sound id 8538 (it's a snowy sound)packet:WriteUByte(0) --our forced "0" we must tack on the end with SMSG_WEATHER.
Step 3: Send the packet
Now that we have built a packet, we have to send it. You have many options for sending packets:
Code:
Unit:SendPacket(packet, self)                        +- Sends the packet to the nearby units. if self is true, the packet will also be sent to Unit.Plyr:SendPacketToGroup(packet)                       +- Sends the packet to Plyr's group.Plyr:SendPacketToGuild(packet)                       +- Sends the packet to Plyr's guild.Plyr:SendPacketToPlayer(packet)                      +- Sends the packet to Plyr.Unit:SendPacketToZone(packet,zone_id)                +- Sends the packet to Zone zone_id. Unit:SendPacketToInstance(packet,instance_id)        +- Sends the packet to Instance instance_id.Unit:SendPacketToWorld(packet)                       +- Sends the packet to the world.
Hmm... who should I make it snow for? Just one player? A guild? The world? I've decided that I want to send it to just one player, so they get freaked out when it starts snowing in Orgrimmar.
Code:
player:SendPacketToPlayer(packet) --sends our packet.
5. Done
Well, you're done. That poor player is now getting snowed on. And yes, it's a pain, and takes a long time to do, but it is pretty advanced work. Yes, it IS much easier just to use player:SetPlayerWeather(8, 2.0), but this was to show you how to get down to one of the most advanced edits available in HypArc.