the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 276 lines 8.5 kB view raw
1#include "stdafx.h" 2#include "BasicTypeContainers.h" 3 4#include "DataOutputStream.h" 5 6//Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero. 7//Parameters: 8//out - the underlying output stream, to be saved for later use. 9DataOutputStream::DataOutputStream( OutputStream *out ) : stream( out ), written( 0 ) 10{ 11} 12 13// 4J Stu - We cannot always delete the stream when we are destroyed, but we want to clear it up as there 14// are occasions when we don't have a handle to the child stream elsewhere and want to delete it 15void DataOutputStream::deleteChildStream() 16{ 17 delete stream; 18} 19 20//Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. 21//If no exception is thrown, the counter written is incremented by 1. 22//Implements the write method of OutputStream. 23//Parameters: 24//b - the byte to be written. 25void DataOutputStream::write(unsigned int b) 26{ 27 stream->write( b ); 28 // TODO 4J Stu - Exception handling? 29 written++; 30} 31 32void DataOutputStream::flush() 33{ 34 stream->flush(); 35} 36 37//Writes b.length bytes from the specified byte array to this output stream. 38//The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length). 39//Parameters: 40//b - the data. 41void DataOutputStream::write(byteArray b) 42{ 43 write(b, 0, b.length); 44} 45 46//Writes len bytes from the specified byte array starting at offset off to the underlying output stream. 47//If no exception is thrown, the counter written is incremented by len. 48//Parameters: 49//b - the data. 50//off - the start offset in the data. 51//len - the number of bytes to write. 52void DataOutputStream::write(byteArray b, unsigned int offset, unsigned int length) 53{ 54 stream->write(b, offset, length); 55 // TODO 4J Stu - Some form of error checking? 56 written += length; 57} 58 59//Closes this output stream and releases any system resources associated with the stream. 60//The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream. 61void DataOutputStream::close() 62{ 63 stream->close(); 64} 65 66//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. 67//Parameters: 68//v - a byte value to be written. 69void DataOutputStream::writeByte(byte a) 70{ 71 stream->write( a ); 72} 73 74//Converts the double argument to a long using the doubleToLongBits method in class Double, 75//and then writes that long value to the underlying output stream as an 8-byte quantity, 76//high byte first. If no exception is thrown, the counter written is incremented by 8. 77//Parameters: 78//v - a double value to be written. 79void DataOutputStream::writeDouble(double a) 80{ 81 __int64 bits = Double::doubleToLongBits( a ); 82 83 writeLong( bits ); 84 // TODO 4J Stu - Error handling? 85 written += 8; 86} 87 88//Converts the float argument to an int using the floatToIntBits method in class Float, 89//and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. 90//If no exception is thrown, the counter written is incremented by 4. 91//Parameters: 92//v - a float value to be written. 93void DataOutputStream::writeFloat(float a) 94{ 95 int bits = Float::floatToIntBits( a ); 96 97 writeInt( bits ); 98 // TODO 4J Stu - Error handling? 99 written += 4; 100} 101 102//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. 103//Parameters: 104//v - an int to be written. 105void DataOutputStream::writeInt(int a) 106{ 107 stream->write( (a >> 24) & 0xff ); 108 stream->write( (a >> 16) & 0xff ); 109 stream->write( (a >> 8) & 0xff ); 110 stream->write( a & 0xff ); 111 // TODO 4J Stu - Error handling? 112 written += 4; 113} 114 115//Writes a long to the underlying output stream as eight bytes, high byte first. 116//In no exception is thrown, the counter written is incremented by 8. 117//Parameters: 118//v - a long to be written. 119void DataOutputStream::writeLong(__int64 a) 120{ 121 stream->write( (a >> 56) & 0xff ); 122 stream->write( (a >> 48) & 0xff ); 123 stream->write( (a >> 40) & 0xff ); 124 stream->write( (a >> 32) & 0xff ); 125 stream->write( (a >> 24) & 0xff ); 126 stream->write( (a >> 16) & 0xff ); 127 stream->write( (a >> 8) & 0xff ); 128 stream->write( a & 0xff ); 129 // TODO 4J Stu - Error handling? 130 written += 4; 131} 132 133//Writes a short to the underlying output stream as two bytes, high byte first. 134//If no exception is thrown, the counter written is incremented by 2. 135//Parameters: 136//v - a short to be written. 137void DataOutputStream::writeShort(short a) 138{ 139 stream->write( (a >> 8) & 0xff ); 140 stream->write( a & 0xff ); 141 // TODO 4J Stu - Error handling? 142 written += 2; 143} 144 145void DataOutputStream::writeUnsignedShort(unsigned short a) 146{ 147 stream->write( (a >> 8) & 0xff ); 148 stream->write( a & 0xff ); 149 // TODO 4J Stu - Error handling? 150 written += 2; 151} 152 153//Writes a char to the underlying output stream as a 2-byte value, high byte first. 154//If no exception is thrown, the counter written is incremented by 2. 155//Parameters: 156//v - a char value to be written. 157void DataOutputStream::writeChar( wchar_t v ) 158{ 159 stream->write( (v >> 8) & 0xff ); 160 stream->write( v & 0xff ); 161 // TODO 4J Stu - Error handling? 162 written += 2; 163} 164 165//Writes a string to the underlying output stream as a sequence of characters. 166//Each character is written to the data output stream as if by the writeChar method. 167//If no exception is thrown, the counter written is incremented by twice the length of s. 168//Parameters: 169//s - a String value to be written. 170void DataOutputStream::writeChars(const wstring& str) 171{ 172 for( unsigned int i = 0; i < str.length(); i++) 173 { 174 writeChar( str.at( i ) ); 175 // TODO 4J Stu - Error handling? 176 } 177 // Incrementing handled by the writeChar function 178} 179 180//Writes a boolean to the underlying output stream as a 1-byte value. 181//The value true is written out as the value (byte)1; the value false is written out as the value (byte)0. 182//If no exception is thrown, the counter written is incremented by 1. 183//Parameters: 184//v - a boolean value to be written. 185void DataOutputStream::writeBoolean(bool b) 186{ 187 stream->write( b ? (byte)1 : (byte)0 ); 188 // TODO 4J Stu - Error handling? 189 written += 1; 190} 191 192//Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner. 193//First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. 194//This value is the number of bytes actually written out, not the length of the string. Following the length, 195//each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. 196//If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. 197//This will be at least two plus the length of str, and at most two plus thrice the length of str. 198//Parameters: 199//str - a string to be written. 200void DataOutputStream::writeUTF(const wstring& str) 201{ 202 int strlen = (int)str.length(); 203 int utflen = 0; 204 int c, count = 0; 205 206 /* use charAt instead of copying String to char array */ 207 for (int i = 0; i < strlen; i++) 208 { 209 c = str.at(i); 210 if ((c >= 0x0001) && (c <= 0x007F)) 211 { 212 utflen++; 213 } 214 else if (c > 0x07FF) 215 { 216 utflen += 3; 217 } 218 else 219 { 220 utflen += 2; 221 } 222 } 223 224 //if (utflen > 65535) 225 // throw new UTFDataFormatException( 226 // "encoded string too long: " + utflen + " bytes"); 227 228 byteArray bytearr(utflen+2); 229 230 bytearr[count++] = (byte) ((utflen >> 8) & 0xFF); 231 bytearr[count++] = (byte) ((utflen >> 0) & 0xFF); 232 233 int i=0; 234 for (i=0; i<strlen; i++) 235 { 236 c = str.at(i); 237 if (!((c >= 0x0001) && (c <= 0x007F))) break; 238 bytearr[count++] = (byte) c; 239 } 240 241 for (;i < strlen; i++) 242 { 243 c = str.at(i); 244 if ((c >= 0x0001) && (c <= 0x007F)) 245 { 246 bytearr[count++] = (byte) c; 247 248 } 249 else if (c > 0x07FF) 250 { 251 bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); 252 bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); 253 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 254 } 255 else 256 { 257 bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); 258 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 259 } 260 } 261 write(bytearr, 0, utflen+2); 262 delete[] bytearr.data; 263} 264 265// 4J Added 266void DataOutputStream::writePlayerUID(PlayerUID player) 267{ 268#if defined(__PS3__) || defined(__ORBIS__) || defined (__PSVITA__) 269 for(int idPos=0;idPos<sizeof(PlayerUID); idPos++) 270 writeByte(((char*)&player)[idPos]); 271#elif defined(_DURANGO) 272 writeUTF(player.toString()); 273#else 274 writeLong(player); 275#endif // PS3 276}