コード例 #1
0
ファイル: Buffer.c プロジェクト: gmoonen/craftd
void
CD_BufferAddMetadata (CDBuffer* self, MCMetadata* data)
{
    // Format strings of the different metadata types
    static char* formats[] = { "b", "s", "i", "f", "S" };

    for (size_t i = 0; i < data->length; i++) {
        if (data->item[i]->type == MCTypeShortByteShort) {
            CD_BufferAddFormat(self, "sbs",
                data->item[i]->data.sbs.first,
                data->item[i]->data.sbs.second,
                data->item[i]->data.sbs.third
            );
        }
        else {
            CD_BufferAddFormat(self, formats[data->item[i]->type], data->item[i]->data);
        }
    }

    CD_BufferAddByte(self, 127);
}
コード例 #2
0
ファイル: Packet.c プロジェクト: Youx/craftd
CDBuffer*
CD_PacketToBuffer (CDPacket* self)
{
    CDBuffer* data = CD_CreateBuffer();

    assert(self);

    CD_BufferAddByte(data, self->type);

    switch (self->chain) {
        case CDRequest: {
            switch (self->type) {
                default: break;
            }
        } break;

        case CDResponse: {
            switch (self->type) {
                case CDLogin: {
                    CDPacketLogin* packet = (CDPacketLogin*) self->data;

                    CD_BufferAddFormat(data, "iSSlb",
                        packet->response.id,
                        packet->response.serverName,
                        packet->response.motd,
                        packet->response.mapSeed,
                        packet->response.dimension
                    );
                } break;

                case CDHandshake: {
                    CDPacketHandshake* packet = (CDPacketHandshake*) self->data;

                    CD_BufferAddString(data, packet->response.hash);
                } break;

                case CDChat: {
                    CDPacketChat* packet = (CDPacketChat*) self->data;

                    CD_BufferAddString(data, packet->response.message);
                } break;

                case CDTimeUpdate: {
                    CDPacketTimeUpdate* packet = (CDPacketTimeUpdate*) self->data;

                    CD_BufferAddLong(data, packet->response.time);
                } break;

                case CDEntityEquipment: {
                    CDPacketEntityEquipment* packet = (CDPacketEntityEquipment*) self->data;

                    CD_BufferAddFormat(data, "isss",
                        packet->response.entity.id,
                        packet->response.slot,
                        packet->response.item,
                        packet->response.damage
                    );
                } break;

                case CDSpawnPosition: {
                    CDPacketSpawnPosition* packet = (CDPacketSpawnPosition*) self->data;

                    CD_BufferAddFormat(data, "iii",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z
                    );
                } break;

                case CDUpdateHealth: {
                    CDPacketUpdateHealth* packet = (CDPacketUpdateHealth*) self->data;

                    CD_BufferAddShort(data, packet->response.health);
                } break;

                case CDPlayerMoveLook: {
                    CDPacketPlayerMoveLook* packet = (CDPacketPlayerMoveLook*) self->data;

                    CD_BufferAddFormat(data, "ddddffB",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.stance,
                        packet->response.position.z,
                        packet->response.yaw,
                        packet->response.pitch,
                        packet->response.is.onGround
                    );
                } break;

                case CDUseBed: {
                    CDPacketUseBed* packet = (CDPacketUseBed*) self->data;

                    CD_BufferAddFormat(data, "ibibi",
                        packet->response.entity.id,
                        packet->response.inBed,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z
                    );
                } break;

                case CDAnimation: {
                    CDPacketAnimation* packet = (CDPacketAnimation*) self->data;

                    CD_BufferAddFormat(data, "ib",
                        packet->response.entity.id,
                        packet->response.type
                    );
                } break;

                case CDNamedEntitySpawn: {
                    CDPacketNamedEntitySpawn* packet = (CDPacketNamedEntitySpawn*) self->data;

                    CD_BufferAddFormat(data, "iSiiibbs",
                        packet->response.entity.id,
                        packet->response.name,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,
                        packet->response.rotation,
                        packet->response.pitch,
                        packet->response.item
                    );
                } break;

                case CDPickupSpawn: {
                    CDPacketPickupSpawn* packet = (CDPacketPickupSpawn*) self->data;

                    CD_BufferAddFormat(data, "isbsiiibbb",
                        packet->response.entity.id,
                        packet->response.item.id,
                        packet->response.item.count,
                        packet->response.item.uses,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,
                        packet->response.rotation,
                        packet->response.pitch,
                        packet->response.roll
                    );
                } break;

                case CDCollectItem: {
                    CDPacketCollectItem* packet = (CDPacketCollectItem*) self->data;

                    CD_BufferAddFormat(data, "ii",
                        packet->response.collected,
                        packet->response.collector
                    );
                } break;

                case CDSpawnObject: {
                    CDPacketSpawnObject* packet = (CDPacketSpawnObject*) self->data;

                    CD_BufferAddFormat(data, "ibiii",
                        packet->response.entity.id,
                        packet->response.type,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z
                    );
                } break;

                case CDSpawnMob: {
                    CDPacketSpawnMob* packet = (CDPacketSpawnMob*) self->data;

                    CD_BufferAddFormat(data, "ibiiibbM",
                        packet->response.id,
                        packet->response.type,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,
                        packet->response.yaw,
                        packet->response.pitch,
                        packet->response.metadata
                    );
                } break;

                case CDPainting: {
                    CDPacketPainting* packet = (CDPacketPainting*) self->data;

                    CD_BufferAddFormat(data, "iSiiii",
                        packet->response.entity.id,
                        packet->response.title,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,
                        packet->response.type
                    );
                } break;

                case CDEntityVelocity: {
                    CDPacketEntityVelocity* packet = (CDPacketEntityVelocity*) self->data;

                    CD_BufferAddFormat(data, "isss",
                        packet->response.entity.id,
                        packet->response.velocity.x,
                        packet->response.velocity.y,
                        packet->response.velocity.z
                    );
                } break;

                case CDEntityDestroy: {
                    CDPacketEntityDestroy* packet = (CDPacketEntityDestroy*) self->data;

                    CD_BufferAddInteger(data, packet->response.entity.id);
                } break;

                case CDEntityCreate: {
                    CDPacketEntityCreate* packet = (CDPacketEntityCreate*) self->data;

                    CD_BufferAddInteger(data, packet->response.entity.id);
                } break;

                case CDEntityRelativeMove: {
                    CDPacketEntityRelativeMove* packet = (CDPacketEntityRelativeMove*) self->data;

                    CD_BufferAddFormat(data, "ibbb",
                        packet->response.entity.id,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z
                    );
                } break;

                case CDEntityLook: {
                    CDPacketEntityLook* packet = (CDPacketEntityLook*) self->data;

                    CD_BufferAddFormat(data, "ibb",
                        packet->response.entity.id,
                        packet->response.yaw,
                        packet->response.pitch
                    );
                } break;

                case CDEntityLookMove: {
                    CDPacketEntityLookMove* packet = (CDPacketEntityLookMove*) self->data;

                    CD_BufferAddFormat(data, "ibbbbb",
                        packet->response.entity.id,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,
                        packet->response.yaw,
                        packet->response.pitch
                    );
                } break;

                case CDEntityTeleport: {
                    CDPacketEntityTeleport* packet = (CDPacketEntityTeleport*) self->data;

                    CD_BufferAddFormat(data, "iiiibb",
                        packet->response.entity.id,
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,
                        packet->response.rotation,
                        packet->response.pitch
                    );
                } break;

                case CDEntityStatus: {
                    CDPacketEntityStatus* packet = (CDPacketEntityStatus*) self->data;

                    CD_BufferAddFormat(data, "ib",
                        packet->response.entity.id,
                        packet->response.status
                    );
                } break;

                case CDEntityAttach: {
                    CDPacketEntityAttach* packet = (CDPacketEntityAttach*) self->data;

                    CD_BufferAddFormat(data, "ii",
                        packet->response.entity.id,
                        packet->response.vehicle.id
                    );
                } break;

                case CDEntityMetadata: {
                    CDPacketEntityMetadata* packet = (CDPacketEntityMetadata*) self->data;

                    CD_BufferAddFormat(data, "iM",
                        packet->response.entity.id,
                        packet->response.metadata
                    );
                } break;

                case CDPreChunk: {
                    CDPacketPreChunk* packet = (CDPacketPreChunk*) self->data;

                    CD_BufferAddFormat(data, "iiB",
                        packet->response.position.x,
                        packet->response.position.z,
                        packet->response.mode
                    );
                } break;

                case CDMapChunk: {
                    CDPacketMapChunk* packet = (CDPacketMapChunk*) self->data;

                    CD_BufferAddFormat(data, "isibbb",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,

                        packet->response.size.x - 1,
                        packet->response.size.y - 1,
                        packet->response.size.z - 1
                    );

                    CD_BufferAddInteger(data, packet->response.length);

                    CD_BufferAdd(data, (CDPointer) packet->response.item, packet->response.length * MCByteSize);
                } break;

                case CDMultiBlockChange: {
                    CDPacketMultiBlockChange* packet = (CDPacketMultiBlockChange*) self->data;

                    CD_BufferAddFormat(data, "ii",
                        packet->response.position.x,
                        packet->response.position.z
                    );

                    CD_BufferAddShort(data, packet->response.length);

                    CD_BufferAdd(data, (CDPointer) packet->response.coordinate, packet->response.length * MCShortSize);
                    CD_BufferAdd(data, (CDPointer) packet->response.type,       packet->response.length * MCByteSize);
                    CD_BufferAdd(data, (CDPointer) packet->response.metadata,   packet->response.length * MCByteSize);
                } break;

                case CDBlockChange: {
                    CDPacketBlockChange* packet = (CDPacketBlockChange*) self->data;

                    CD_BufferAddFormat(data, "ibibb",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,

                        packet->response.type,
                        packet->response.metadata
                    );
                } break;

                case CDPlayNoteBlock: {
                    CDPacketPlayNoteBlock* packet = (CDPacketPlayNoteBlock*) self->data;

                    CD_BufferAddFormat(data, "isibb",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,

                        packet->response.instrument,
                        packet->response.pitch
                    );
                } break;

                case CDExplosion: {
                    CDPacketExplosion* packet = (CDPacketExplosion*) self->data;

                    CD_BufferAddFormat(data, "dddf",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,

                        packet->response.radius
                    );

                    CD_BufferAddInteger(data, packet->response.length);

                    CD_BufferAdd(data, (CDPointer) packet->response.item, packet->response.length * 3 * MCByteSize);
                } break;

                case CDOpenWindow: {
                    CDPacketOpenWindow* packet = (CDPacketOpenWindow*) self->data;

                    CD_BufferAddFormat(data, "bbSb",
                        packet->response.id,
                        packet->response.type,
                        packet->response.title,
                        packet->response.slots
                    );
                } break;

                case CDCloseWindow: {
                    CDPacketCloseWindow* packet = (CDPacketCloseWindow*) self->data;

                    CD_BufferAddByte(data, packet->response.id);
                } break;

                case CDSetSlot: {
                    CDPacketSetSlot* packet = (CDPacketSetSlot*) self->data;

                    CD_BufferAddFormat(data, "bss",
                        packet->response.id,
                        packet->response.slot,
                        packet->response.item.id
                    );

                    if (packet->response.item.id != -1) {
                        CD_BufferAddFormat(data, "bs",
                            packet->response.item.count,
                            packet->response.item.uses
                        );
                    }
                } break;

                case CDWindowItems: {
                    CDPacketWindowItems* packet = (CDPacketWindowItems*) self->data;

                    CD_BufferAddByte(data, packet->response.id);
                    CD_BufferAddShort(data, packet->response.length);

                    for (size_t i = 0; i < packet->response.length; i++) {
                        if (packet->response.item[i].id == -1) {
                            CD_BufferAddShort(data, -1);
                        }
                        else {
                            CD_BufferAddFormat(data, "sbs",
                                packet->response.item[i].id,
                                packet->response.item[i].count,
                                packet->response.item[i].uses
                            );
                        }
                    }
                } break;

                case CDUpdateProgressBar: {
                    CDPacketUpdateProgressBar* packet = (CDPacketUpdateProgressBar*) self->data;

                    CD_BufferAddFormat(data, "bss",
                        packet->response.id,
                        packet->response.bar,
                        packet->response.value
                    );
                } break;

                case CDTransaction: {
                    CDPacketTransaction* packet = (CDPacketTransaction*) self->data;

                    CD_BufferAddFormat(data, "bsB",
                        packet->response.id,
                        packet->response.action,
                        packet->response.accepted
                    );
                } break;

                case CDUpdateSign: {
                    CDPacketUpdateSign* packet = (CDPacketUpdateSign*) self->data;

                    CD_BufferAddFormat(data, "isiSSSS",
                        packet->response.position.x,
                        packet->response.position.y,
                        packet->response.position.z,

                        packet->response.first,
                        packet->response.second,
                        packet->response.third,
                        packet->response.fourth
                    );
                } break;

                case CDDisconnect: {
                    CDPacketDisconnect* packet = (CDPacketDisconnect*) self->data;

                    CD_BufferAddString(data, packet->response.reason);
                } break;

                default: break;
            }
        } break;
    }

    return data;
}