예제 #1
0
파일: Buffer.c 프로젝트: alyawn/craftd-huh
void
SV_BufferAddMetadata (CDBuffer* self, SVMetadata* 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 == SVTypeShortByteShort) {
            SV_BufferAddFormat(self, "sbs",
                data->item[i]->data.sbs.first,
                data->item[i]->data.sbs.second,
                data->item[i]->data.sbs.third
            );
        }
        else {
            SV_BufferAddFormat(self, formats[data->item[i]->type], data->item[i]->data);
        }
    }

    SV_BufferAddByte(self, 127);
}
예제 #2
0
CDBuffer*
SV_PacketToBuffer (SVPacket* self)
{
    CDBuffer* data = CD_CreateBuffer();

    assert(self);

    SV_BufferAddByte(data, self->type);

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

        case SVResponse: {
            switch (self->type) {
                case SVLogin: {
                    SVPacketLogin* packet = (SVPacketLogin*) self->data;

                    SV_BufferAddFormat(data, "iUlb",
                        packet->response.id,
                        packet->response.serverName,
                        packet->response.mapSeed,
                        packet->response.dimension
                    );
                } break;

                case SVHandshake: {
                    SVPacketHandshake* packet = (SVPacketHandshake*) self->data;

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

                case SVChat: {
                    SVPacketChat* packet = (SVPacketChat*) self->data;

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

                case SVTimeUpdate: {
                    SVPacketTimeUpdate* packet = (SVPacketTimeUpdate*) self->data;

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

                case SVEntityEquipment: {
                    SVPacketEntityEquipment* packet = (SVPacketEntityEquipment*) self->data;

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

                case SVSpawnPosition: {
                    SVPacketSpawnPosition* packet = (SVPacketSpawnPosition*) self->data;

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

                case SVUpdateHealth: {
                    SVPacketUpdateHealth* packet = (SVPacketUpdateHealth*) self->data;

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

                case SVPlayerMoveLook: {
                    SVPacketPlayerMoveLook* packet = (SVPacketPlayerMoveLook*) self->data;

                    SV_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 SVUseBed: {
                    SVPacketUseBed* packet = (SVPacketUseBed*) self->data;

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

                case SVAnimation: {
                    SVPacketAnimation* packet = (SVPacketAnimation*) self->data;

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

                case SVNamedEntitySpawn: {
                    SVPacketNamedEntitySpawn* packet = (SVPacketNamedEntitySpawn*) self->data;

                    SV_BufferAddFormat(data, "iUiiibbs",
                        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 SVPickupSpawn: {
                    SVPacketPickupSpawn* packet = (SVPacketPickupSpawn*) self->data;

                    SV_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 SVCollectItem: {
                    SVPacketCollectItem* packet = (SVPacketCollectItem*) self->data;

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

                case SVSpawnObject: {
                    SVPacketSpawnObject* packet = (SVPacketSpawnObject*) self->data;

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

                case SVSpawnMob: {
                    SVPacketSpawnMob* packet = (SVPacketSpawnMob*) self->data;

                    SV_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 SVPainting: {
                    SVPacketPainting* packet = (SVPacketPainting*) self->data;

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

                case SVEntityVelocity: {
                    SVPacketEntityVelocity* packet = (SVPacketEntityVelocity*) self->data;

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

                case SVEntityDestroy: {
                    SVPacketEntityDestroy* packet = (SVPacketEntityDestroy*) self->data;

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

                case SVEntityCreate: {
                    SVPacketEntityCreate* packet = (SVPacketEntityCreate*) self->data;

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

                case SVEntityRelativeMove: {
                    SVPacketEntityRelativeMove* packet = (SVPacketEntityRelativeMove*) self->data;

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

                case SVEntityLook: {
                    SVPacketEntityLook* packet = (SVPacketEntityLook*) self->data;

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

                case SVEntityLookMove: {
                    SVPacketEntityLookMove* packet = (SVPacketEntityLookMove*) self->data;

                    SV_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 SVEntityTeleport: {
                    SVPacketEntityTeleport* packet = (SVPacketEntityTeleport*) self->data;

                    SV_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 SVEntityStatus: {
                    SVPacketEntityStatus* packet = (SVPacketEntityStatus*) self->data;

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

                case SVEntityAttach: {
                    SVPacketEntityAttach* packet = (SVPacketEntityAttach*) self->data;

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

                case SVEntityMetadata: {
                    SVPacketEntityMetadata* packet = (SVPacketEntityMetadata*) self->data;

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

                case SVPreChunk: {
                    SVPacketPreChunk* packet = (SVPacketPreChunk*) self->data;

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

                case SVMapChunk: {
                    SVPacketMapChunk* packet = (SVPacketMapChunk*) self->data;

                    SV_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
                    );

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

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

                case SVMultiBlockChange: {
                    SVPacketMultiBlockChange* packet = (SVPacketMultiBlockChange*) self->data;

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

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

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

                case SVBlockChange: {
                    SVPacketBlockChange* packet = (SVPacketBlockChange*) self->data;

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

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

                case SVPlayNoteBlock: {
                    SVPacketPlayNoteBlock* packet = (SVPacketPlayNoteBlock*) self->data;

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

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

                case SVExplosion: {
                    SVPacketExplosion* packet = (SVPacketExplosion*) self->data;

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

                        packet->response.radius
                    );

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

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

                case SVOpenWindow: {
                    SVPacketOpenWindow* packet = (SVPacketOpenWindow*) self->data;

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

                case SVCloseWindow: {
                    SVPacketCloseWindow* packet = (SVPacketCloseWindow*) self->data;

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

                case SVSetSlot: {
                    SVPacketSetSlot* packet = (SVPacketSetSlot*) self->data;

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

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

                case SVWindowItems: {
                    SVPacketWindowItems* packet = (SVPacketWindowItems*) self->data;

                    SV_BufferAddByte(data, packet->response.id);
                    SV_BufferAddShort(data, packet->response.length);

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

                case SVUpdateProgressBar: {
                    SVPacketUpdateProgressBar* packet = (SVPacketUpdateProgressBar*) self->data;

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

                case SVTransaction: {
                    SVPacketTransaction* packet = (SVPacketTransaction*) self->data;

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

                case SVUpdateSign: {
                    SVPacketUpdateSign* packet = (SVPacketUpdateSign*) self->data;

                    SV_BufferAddFormat(data, "isiUUUU",
                        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 SVDisconnect: {
                    SVPacketDisconnect* packet = (SVPacketDisconnect*) self->data;

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

                default: break;
            }
        } break;
    }

    return data;
}