Something went wrong. Try again.
the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
Something went wrong. Try again.
8.5 kB · 276 lines
C++
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276#include "stdafx.h"#include "BasicTypeContainers.h"
#include "DataOutputStream.h"
//Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.//Parameters://out - the underlying output stream, to be saved for later use.DataOutputStream::DataOutputStream( OutputStream *out ) : stream( out ), written( 0 ){}
// 4J Stu - We cannot always delete the stream when we are destroyed, but we want to clear it up as there// are occasions when we don't have a handle to the child stream elsewhere and want to delete itvoid DataOutputStream::deleteChildStream(){ delete stream;}
//Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.//If no exception is thrown, the counter written is incremented by 1.//Implements the write method of OutputStream.//Parameters://b - the byte to be written.void DataOutputStream::write(unsigned int b){ stream->write( b ); // TODO 4J Stu - Exception handling? written++;}
void DataOutputStream::flush(){ stream->flush();}
//Writes b.length bytes from the specified byte array to this output stream.//The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).//Parameters://b - the data.void DataOutputStream::write(byteArray b){ write(b, 0, b.length);}
//Writes len bytes from the specified byte array starting at offset off to the underlying output stream.//If no exception is thrown, the counter written is incremented by len.//Parameters://b - the data.//off - the start offset in the data.//len - the number of bytes to write.void DataOutputStream::write(byteArray b, unsigned int offset, unsigned int length){ stream->write(b, offset, length); // TODO 4J Stu - Some form of error checking? written += length;}
//Closes this output stream and releases any system resources associated with the stream.//The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.void DataOutputStream::close(){ stream->close();}
//Writes out a byte to the underlying output stream as a 1-byte value. If no exception is thrown, the counter written is incremented by 1.//Parameters://v - a byte value to be written.void DataOutputStream::writeByte(byte a){ stream->write( a );}
//Converts the double argument to a long using the doubleToLongBits method in class Double,//and then writes that long value to the underlying output stream as an 8-byte quantity,//high byte first. If no exception is thrown, the counter written is incremented by 8.//Parameters://v - a double value to be written.void DataOutputStream::writeDouble(double a){ __int64 bits = Double::doubleToLongBits( a );
writeLong( bits ); // TODO 4J Stu - Error handling? written += 8;}
//Converts the float argument to an int using the floatToIntBits method in class Float,//and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.//If no exception is thrown, the counter written is incremented by 4.//Parameters://v - a float value to be written.void DataOutputStream::writeFloat(float a){ int bits = Float::floatToIntBits( a );
writeInt( bits ); // TODO 4J Stu - Error handling? written += 4;}
//Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.//Parameters://v - an int to be written.void DataOutputStream::writeInt(int a){ stream->write( (a >> 24) & 0xff ); stream->write( (a >> 16) & 0xff ); stream->write( (a >> 8) & 0xff ); stream->write( a & 0xff ); // TODO 4J Stu - Error handling? written += 4;}
//Writes a long to the underlying output stream as eight bytes, high byte first.//In no exception is thrown, the counter written is incremented by 8.//Parameters://v - a long to be written.void DataOutputStream::writeLong(__int64 a){ stream->write( (a >> 56) & 0xff ); stream->write( (a >> 48) & 0xff ); stream->write( (a >> 40) & 0xff ); stream->write( (a >> 32) & 0xff ); stream->write( (a >> 24) & 0xff ); stream->write( (a >> 16) & 0xff ); stream->write( (a >> 8) & 0xff ); stream->write( a & 0xff ); // TODO 4J Stu - Error handling? written += 4;}
//Writes a short to the underlying output stream as two bytes, high byte first.//If no exception is thrown, the counter written is incremented by 2.//Parameters://v - a short to be written.void DataOutputStream::writeShort(short a){ stream->write( (a >> 8) & 0xff ); stream->write( a & 0xff ); // TODO 4J Stu - Error handling? written += 2;}
void DataOutputStream::writeUnsignedShort(unsigned short a){ stream->write( (a >> 8) & 0xff ); stream->write( a & 0xff ); // TODO 4J Stu - Error handling? written += 2;}
//Writes a char to the underlying output stream as a 2-byte value, high byte first.//If no exception is thrown, the counter written is incremented by 2.//Parameters://v - a char value to be written.void DataOutputStream::writeChar( wchar_t v ){ stream->write( (v >> 8) & 0xff ); stream->write( v & 0xff ); // TODO 4J Stu - Error handling? written += 2;}
//Writes a string to the underlying output stream as a sequence of characters.//Each character is written to the data output stream as if by the writeChar method.//If no exception is thrown, the counter written is incremented by twice the length of s.//Parameters://s - a String value to be written.void DataOutputStream::writeChars(const wstring& str){ for( unsigned int i = 0; i < str.length(); i++) { writeChar( str.at( i ) ); // TODO 4J Stu - Error handling? } // Incrementing handled by the writeChar function}
//Writes a boolean to the underlying output stream as a 1-byte value.//The value true is written out as the value (byte)1; the value false is written out as the value (byte)0.//If no exception is thrown, the counter written is incremented by 1.//Parameters://v - a boolean value to be written.void DataOutputStream::writeBoolean(bool b){ stream->write( b ? (byte)1 : (byte)0 ); // TODO 4J Stu - Error handling? written += 1; }
//Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.//First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow.//This value is the number of bytes actually written out, not the length of the string. Following the length,//each character of the string is output, in sequence, using the modified UTF-8 encoding for the character.//If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream.//This will be at least two plus the length of str, and at most two plus thrice the length of str.//Parameters://str - a string to be written.void DataOutputStream::writeUTF(const wstring& str){ int strlen = (int)str.length(); int utflen = 0; int c, count = 0;
/* use charAt instead of copying String to char array */ for (int i = 0; i < strlen; i++) { c = str.at(i); if ((c >= 0x0001) && (c <= 0x007F)) { utflen++; } else if (c > 0x07FF) { utflen += 3; } else { utflen += 2; } }
//if (utflen > 65535) // throw new UTFDataFormatException( // "encoded string too long: " + utflen + " bytes");
byteArray bytearr(utflen+2);
bytearr[count++] = (byte) ((utflen >> 8) & 0xFF); bytearr[count++] = (byte) ((utflen >> 0) & 0xFF);
int i=0; for (i=0; i<strlen; i++) { c = str.at(i); if (!((c >= 0x0001) && (c <= 0x007F))) break; bytearr[count++] = (byte) c; }
for (;i < strlen; i++) { c = str.at(i); if ((c >= 0x0001) && (c <= 0x007F)) { bytearr[count++] = (byte) c;
} else if (c > 0x07FF) { bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } else { bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } write(bytearr, 0, utflen+2); delete[] bytearr.data;}
// 4J Addedvoid DataOutputStream::writePlayerUID(PlayerUID player){#if defined(__PS3__) || defined(__ORBIS__) || defined (__PSVITA__) for(int idPos=0;idPos<sizeof(PlayerUID); idPos++) writeByte(((char*)&player)[idPos]);#elif defined(_DURANGO) writeUTF(player.toString());#else writeLong(player);#endif // PS3}