Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <proxy.hpp>
- #include <mcnetadept.hpp>
- //Modified versions from libmcnet/src/read.c
- //These use the socketbuffer to allow for easier writing.
- //Important defines from my include:
- /*
- typedef uint8_t byte;
- typedef queue<byte> byteBuffer;
- //typedef all the things! (using info from libmcnet/include/mcnet/structs.h, which i assume is correct)
- //Note: some of these are useless, but make code more consistent.
- namespace mcVal {
- typedef int8_t _bool;
- typedef int8_t _byte;
- typedef uint8_t _ubyte;
- typedef int16_t _short;
- typedef uint16_t _ushort;
- typedef int32_t _int;
- typedef uint32_t _uint;
- typedef int64_t _long;
- typedef uint64_t _ulong;
- typedef float _float;
- typedef double _double;
- typedef byteBuffer _blob;
- typedef _blob _string;
- typedef _blob _string8;
- typedef _blob _string16;
- typedef _blob _metadata;
- typedef _blob _slot;
- typedef _blob _slots;
- }
- */
- using namespace mcVal;
- //I have to undef it here because crypto++ uses a macro with the same name
- #undef GETBYTE
- //I use a separate variable to keep track of the byteBuffer's length because for some reason queue::size sometimes does weird stuff on my system, i really don't know why. (It's like it goes INT_MAX-1 sometimes when it's empty, i should really check out why this happens, but until then, this works.)
- #define SOCKBUF byteBuffer *buf, int* remainingBytes
- #define GETBYTE getByte(buf, remainingBytes)
- //LOGGER, WARN and TRACE are from my logger, you can safely ignore them, they are defines which encapsule some log4cplus functions to make logging just that one bit easier.
- byte getByte(SOCKBUF)
- {
- LOGGER("mcnetadept.cpp::getByte")
- if (!((*remainingBytes)>0))
- {
- //Just because minecraft isn't consistent.
- WARN("Not enough data. Returning 0")
- return 0x00;
- }
- byte rc = buf->front();
- buf->pop();
- TRACE("Returning: " << hex << (int)rc << dec << " C: (" << (char)rc << ")")
- (*remainingBytes)--;
- return rc;
- }
- _bool mcnet_read_bool(SOCKBUF) {
- return (((int8_t)(GETBYTE)) == 0) ? 0 : 1;
- }
- _byte mcnet_read_byte(SOCKBUF) {
- _byte a = ((int8_t)(GETBYTE));
- return a;
- }
- _ubyte mcnet_read_ubyte(SOCKBUF) {
- return ((uint8_t)(GETBYTE));
- }
- _short mcnet_read_short(SOCKBUF) {
- LOGGER("mcnetadept.cpp::mcnet_read_short")
- _short rc = GETBYTE + (GETBYTE << 8);
- TRACE("Returning: " << hex << (int)rc << dec << " C:(" << (char)rc << ")")
- return rc;
- }
- _ushort mcnet_read_ushort(SOCKBUF) {
- return GETBYTE + (GETBYTE << 8);
- }
- _int mcnet_read_int(SOCKBUF) {
- return GETBYTE + (GETBYTE << 8) + (GETBYTE << 16) + (GETBYTE << 24);
- }
- _uint mcnet_read_uint(SOCKBUF) {
- return (_uint)mcnet_read_int(buf,remainingBytes);
- }
- _long mcnet_read_long(SOCKBUF) {
- return ((int64_t)(GETBYTE)) + (((int64_t)(GETBYTE)) << 8) + (((int64_t)(GETBYTE)) << 16) + (((int64_t)(GETBYTE)) << 24) + (((int64_t)(GETBYTE)) << 32) + (((int64_t)(GETBYTE)) << 40) + (((int64_t)(GETBYTE)) << 48) + (((int64_t)(GETBYTE)) << 56);
- }
- _ulong mcnet_read_ulong(SOCKBUF) {
- return ((uint64_t)(GETBYTE)) + (((uint64_t)(GETBYTE)) << 8) + (((uint64_t)(GETBYTE)) << 16) + (((uint64_t)(GETBYTE)) << 24) + (((uint64_t)(GETBYTE)) << 32) + (((uint64_t)(GETBYTE)) << 40) + (((uint64_t)(GETBYTE)) << 48) + (((uint64_t)(GETBYTE)) << 56);
- }
- _float mcnet_read_float(SOCKBUF) {
- _float out;
- byte* t = (byte*)&out;
- t[0] = GETBYTE;
- t[1] = GETBYTE;
- t[2] = GETBYTE;
- t[3] = GETBYTE;
- return out;
- }
- _double mcnet_read_double(SOCKBUF) {
- _double out;
- byte* t = (byte*)&out;
- t[0] = GETBYTE;
- t[1] = GETBYTE;
- t[2] = GETBYTE;
- t[3] = GETBYTE;
- t[4] = GETBYTE;
- t[5] = GETBYTE;
- t[6] = GETBYTE;
- t[7] = GETBYTE;
- return out;
- }
- //The following function(s) are actually original by me, they're just here for code cleanness (i am not sure that was a word).
- _blob mcnet_read_blob(SOCKBUF, int len) {
- LOGGER("mcnetadept.cpp::mcnet_read_blob")
- _blob rc;
- TRACE("Need to read " << len << " bytes...")
- for (int i = 0; i < len ; i++)
- rc.push((char)(int)mcnet_read_short(buf,remainingBytes));
- TRACE("Finished reading...")
- return rc;
- }
- mcVal::_string mcnet_read_string(SOCKBUF)
- {
- //As far as i gather a string is prefixed with a short
- return mcnet_read_blob(buf,remainingBytes,(int)mcnet_read_short(buf,remainingBytes));
- }
- _short mcnet_read_packetid(SOCKBUF,bool *isWeird) {
- //Side note: Why oh why aren't client and server using the same format for packet ids?
- LOGGER("mcnetadept.cpp::mcnet_read_packetid")
- (*isWeird)=true;
- _short rc = GETBYTE;
- if (buf->front()==0x0)
- {
- rc += (GETBYTE << 8);
- (*isWeird)=false;
- }
- TRACE("Returning: " << hex << (int)rc << dec << " C:(" << (char)rc << ")")
- return rc;
- }
- //The following functions are the reverse of the mcnet functions (they write instead of read) and were written by me
- //Plenty 'o defines make coding easy
- #undef SOCKBUF
- //Yeah, i'm too lazy to spec all these with proper parameter, so i use a boost::any.
- #define SOCKBUF byteBuffer *buf, boost::any value
- //Easy 'nough
- #define ACAST(type) boost::any_cast<type>(value)
- //Again, easy 'enough
- #define ADDBYTE(byte) buf->push(byte)
- #define BYTE(x,y) (x & (((uint64_t)0xFF)<<y))
- void mcnet_write_bool(SOCKBUF) {
- ADDBYTE(ACAST(_bool));
- }
- void mcnet_write_byte(SOCKBUF) {
- ADDBYTE(ACAST(_byte));
- }
- void mcnet_write_ubyte(SOCKBUF) {
- ADDBYTE(ACAST(_ubyte));
- }
- void mcnet_write_short(SOCKBUF) {
- ADDBYTE(BYTE(ACAST(_short),0));
- ADDBYTE(BYTE(ACAST(_short),8));
- }
- void mcnet_write_ushort(SOCKBUF) {
- mcnet_write_short(buf,(short)ACAST(_ushort));
- }
- void mcnet_write_int(SOCKBUF) {
- ADDBYTE(BYTE(ACAST(_int),0));
- ADDBYTE(BYTE(ACAST(_int),8));
- ADDBYTE(BYTE(ACAST(_int),16));
- ADDBYTE(BYTE(ACAST(_int),24));
- }
- void mcnet_write_uint(SOCKBUF) {
- mcnet_write_int(buf,(int)ACAST(_uint));
- }
- void mcnet_write_long(SOCKBUF) {
- ADDBYTE(BYTE(ACAST(_long),0));
- ADDBYTE(BYTE(ACAST(_long),8));
- ADDBYTE(BYTE(ACAST(_long),16));
- ADDBYTE(BYTE(ACAST(_long),24));
- ADDBYTE(BYTE(ACAST(_long),32));
- ADDBYTE(BYTE(ACAST(_long),40));
- ADDBYTE(BYTE(ACAST(_long),48));
- ADDBYTE(BYTE(ACAST(_long),56));
- }
- void mcnet_write_ulong(SOCKBUF) {
- mcnet_write_ulong(buf,(long)ACAST(_ulong));
- }
- void mcnet_write_float(SOCKBUF) {
- _float in = ACAST(_float);
- byte* t = (byte*)∈
- for(int i = 0; i < 4 ; i++)
- ADDBYTE(t[i]);
- }
- void mcnet_write_double(SOCKBUF) {
- _double in = ACAST(_double);
- byte* t = (byte*)∈
- for(int i = 0; i < 8 ; i++)
- ADDBYTE(t[i]);
- }
- void mcnet_write_blob(SOCKBUF) {
- LOGGER("mcnetadept.cpp::mcnet_write_blob")
- _blob data = ACAST(_blob);
- _short len = data.size();
- TRACE("Writing length (" << len << ")...")
- mcnet_write_short(buf,len);
- TRACE("Writing data...")
- for (int i = 0; i < len; i++)
- {
- // TRACE("Writing byte " << i << "...")
- _short b = data.front(); //My compiler was whining...
- mcnet_write_short(buf,b);
- data.pop();
- }
- TRACE("Finished writing...")
- }
- void mcnet_write_packetid(SOCKBUF, bool isWeird) {
- ADDBYTE(BYTE(ACAST(_short),0));
- if (!isWeird)
- ADDBYTE(BYTE(ACAST(_short),8));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement