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.
13 kB · 478 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478#include "stdafx.h"
#include "IntBuffer.h"#include "FloatBuffer.h"#include "ByteBuffer.h"
ByteBuffer::ByteBuffer( unsigned int capacity ) : Buffer( capacity ){ hasBackingArray = false; buffer = new byte[capacity]; memset( buffer,0,sizeof(byte)*capacity); byteOrder = BIGENDIAN;}
//Allocates a new direct byte buffer.//The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined. Whether or not it has a backing array is unspecified.////Parameters://capacity - The new buffer's capacity, in bytes//Returns://The new byte bufferByteBuffer *ByteBuffer::allocateDirect(int capacity){ return new ByteBuffer(capacity);}
ByteBuffer::ByteBuffer( unsigned int capacity, byte *backingArray ) : Buffer( capacity ){ hasBackingArray = true; buffer = backingArray;}
ByteBuffer::~ByteBuffer(){ if( !hasBackingArray ) delete[] buffer;}
//Wraps a byte array into a buffer.//The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array//to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero,//and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.////Parameters://array - The array that will back this buffer//Returns://The new byte bufferByteBuffer *ByteBuffer::wrap(byteArray &b){ return new ByteBuffer( b.length, b.data );}
//Allocates a new byte buffer.//The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined.//It will have a backing array, and its array offset will be zero.////Parameters://capacity - The new buffer's capacity, in bytes//Returns://The new byte bufferByteBuffer *ByteBuffer::allocate(unsigned int capacity){ return new ByteBuffer( capacity );}
//Modifies this buffer's byte order.//Parameters://bo - The new byte order, either BIGENDIAN or LITTLEENDIANvoid ByteBuffer::order(ByteOrder bo){ byteOrder = bo;}
//Flips this buffer. The limit is set to the current position and then the position is set to zero.//If the mark is defined then it is discarded.////Returns://This bufferByteBuffer *ByteBuffer::flip(){ m_limit = m_position; m_position = 0; return this;}
// 4J Added so we can write this to a filebyte *ByteBuffer::getBuffer(){ return buffer;}
int ByteBuffer::getSize(){ // TODO 4J Stu - Should this be the capcity and not the limit? return m_limit;}// End 4J
//Absolute get method. Reads the byte at the given index.//Parameters://index - The index from which the byte will be read//Returns://The byte at the given index//Throws://IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limitBYTE ByteBuffer::get(int index){ assert( index < m_limit ); assert( index >= 0 );
return buffer[index];}
//Relative get method for reading an int value.//Reads the next four bytes at this buffer's current position, composing them into an int value according to the//current byte order, and then increments the position by four.////Returns://The int value at the buffer's current positionint ByteBuffer::getInt(){ assert( m_position+3 < m_limit );
int value = 0;
int b1 = buffer[ m_position ]; int b2 = buffer[ m_position+1 ]; int b3 = buffer[ m_position+2 ]; int b4 = buffer[ m_position+3 ];
m_position += 4;
if( byteOrder == BIGENDIAN ) { value = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; } else if( byteOrder == LITTLEENDIAN ) { value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24); } return value;}
//Absolute get method for reading an int value.//Reads four bytes at the given index, composing them into a int value according to the current byte order.////Parameters://index - The index from which the bytes will be read//Returns://The int value at the given indexint ByteBuffer::getInt(unsigned int index){ assert( index+3 < m_limit ); int value = 0;
int b1 = buffer[ index ]; int b2 = buffer[ index+1 ]; int b3 = buffer[ index+2 ]; int b4 = buffer[ index+3 ];
if( byteOrder == BIGENDIAN ) { value = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; } else if( byteOrder == LITTLEENDIAN ) { value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24); } return value;}
//Relative get method for reading a long value.//Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order,//and then increments the position by eight.////Returns://The long value at the buffer's current position__int64 ByteBuffer::getLong(){ assert( m_position+8 < m_limit );
__int64 value = 0;
__int64 b1 = buffer[ m_position ]; __int64 b2 = buffer[ m_position+1 ]; __int64 b3 = buffer[ m_position+2 ]; __int64 b4 = buffer[ m_position+3 ]; __int64 b5 = buffer[ m_position+4 ]; __int64 b6 = buffer[ m_position+5 ]; __int64 b7 = buffer[ m_position+6 ]; __int64 b8 = buffer[ m_position+7 ];
m_position += 8;
if( byteOrder == BIGENDIAN ) { value = (b1 << 56) | (b2 << 48) | (b3 << 40) | (b4 << 32) | (b5 << 24) | (b6 << 16) | (b7 << 8) | b8; } else if( byteOrder == LITTLEENDIAN ) { value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24) | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56); } return value;}
//Relative get method for reading a short value.//Reads the next two bytes at this buffer's current position, composing them into a short value according to the current//byte order, and then increments the position by two.////Returns://The short value at the buffer's current positionshort ByteBuffer::getShort(){ assert( m_position+1 < m_limit );
short value = 0;
short b1 = buffer[ m_position ]; short b2 = buffer[ m_position+1 ];
m_position += 2;
if( byteOrder == BIGENDIAN ) { value = (b1 << 8) | b2; } else if( byteOrder == LITTLEENDIAN ) { value = b1 | (b2 << 8); } return value;}
void ByteBuffer::getShortArray(shortArray &s){ // TODO 4J Stu - Should this function be writing from the start of the buffer, or from position? // And should it update position? assert( s.length >= m_limit/2 );
// 4J Stu - Assumes big endian memcpy( s.data, buffer, (m_limit-m_position) );}
//Absolute put method (optional operation).//Writes the given byte into this buffer at the given index.////Parameters://index - The index at which the byte will be written//b - The byte value to be written//Returns://This buffer//Throws://IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit//ReadOnlyBufferException - If this buffer is read-onlyByteBuffer *ByteBuffer::put(int index, byte b){ assert( index < m_limit ); assert( index >= 0 );
buffer[index] = b; return this;}
//Relative put method for writing an int value (optional operation).//Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position,//and then increments the position by four.////Parameters://value - The int value to be written//Returns://This bufferByteBuffer *ByteBuffer::putInt(int value){ assert( m_position+3 < m_limit );
if( byteOrder == BIGENDIAN ) { buffer[m_position] = (value >> 24) & 0xFF; buffer[m_position+1] = (value >> 16) & 0xFF; buffer[m_position+2] = (value >> 8) & 0xFF; buffer[m_position+3] = value & 0xFF; } else if( byteOrder == LITTLEENDIAN ) { buffer[m_position] = value & 0xFF; buffer[m_position+1] = (value >> 8) & 0xFF; buffer[m_position+2] = (value >> 16) & 0xFF; buffer[m_position+3] = (value >> 24) & 0xFF; }
m_position += 4;
return this;}
//Absolute put method for writing an int value (optional operation).//Writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.////Parameters://index - The index at which the bytes will be written//value - The int value to be written//Returns://This bufferByteBuffer *ByteBuffer::putInt(unsigned int index, int value){ assert( index+3 < m_limit );
if( byteOrder == BIGENDIAN ) { buffer[index] = (value >> 24) & 0xFF; buffer[index+1] = (value >> 16) & 0xFF; buffer[index+2] = (value >> 8) & 0xFF; buffer[index+3] = value & 0xFF; } else if( byteOrder == LITTLEENDIAN ) { buffer[index] = value & 0xFF; buffer[index+1] = (value >> 8) & 0xFF; buffer[index+2] = (value >> 16) & 0xFF; buffer[index+3] = (value >> 24) & 0xFF; }
return this;}
//Relative put method for writing a short value (optional operation).//Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position,//and then increments the position by two.////Parameters://value - The short value to be written//Returns://This bufferByteBuffer *ByteBuffer::putShort(short value){ assert( m_position+1 < m_limit );
if( byteOrder == BIGENDIAN ) { buffer[m_position] = (value >> 8) & 0xFF; buffer[m_position+1] = value & 0xFF; } else if( byteOrder == LITTLEENDIAN ) { buffer[m_position] = value & 0xFF; buffer[m_position+1] = (value >> 8) & 0xFF; }
m_position += 2;
return this;}
ByteBuffer *ByteBuffer::putShortArray(shortArray &s){ // TODO 4J Stu - Should this function be writing from the start of the buffer, or from position? // And should it update position? assert( s.length*2 <= m_limit); // 4J Stu - Assumes big endian memcpy( buffer, s.data, s.length*2 );
return this;}
//Relative put method for writing a long value (optional operation).//Writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position,//and then increments the position by eight.////Parameters://value - The long value to be written//Returns://This bufferByteBuffer *ByteBuffer::putLong(__int64 value){ assert( m_position+7 < m_limit );
if( byteOrder == BIGENDIAN ) { buffer[m_position] = (value >> 56) & 0xFF; buffer[m_position+1] = (value >> 48) & 0xFF; buffer[m_position+2] = (value >> 40) & 0xFF; buffer[m_position+3] = (value >> 32) & 0xFF; buffer[m_position+4] = (value >> 24) & 0xFF; buffer[m_position+5] = (value >> 16) & 0xFF; buffer[m_position+6] = (value >> 8) & 0xFF; buffer[m_position+7] = value & 0xFF; } else if( byteOrder == LITTLEENDIAN ) { buffer[m_position] = value & 0xFF; buffer[m_position+1] = (value >> 8) & 0xFF; buffer[m_position+2] = (value >> 16) & 0xFF; buffer[m_position+3] = (value >> 24) & 0xFF; buffer[m_position+4] = (value >> 32) & 0xFF; buffer[m_position+5] = (value >> 40) & 0xFF; buffer[m_position+6] = (value >> 48) & 0xFF; buffer[m_position+7] = (value >> 56) & 0xFF; }
return this;}
//Relative bulk put method (optional operation).//This method transfers the entire content of the given source byte array into this buffer.//An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation//// dst.put(a, 0, a.length) //Returns://This bufferByteBuffer *ByteBuffer::put(byteArray inputArray){ if( inputArray.length > remaining() ) assert( false ); //TODO 4J Stu - Some kind of exception?
std::copy( inputArray.data, inputArray.data + inputArray.length, buffer+m_position );
m_position += inputArray.length;
return this;}
byteArray ByteBuffer::array(){ return byteArray( buffer, m_capacity );}
//Creates a view of this byte buffer as an int buffer.//The content of the new buffer will start at this buffer's current position. Changes to this buffer's content//will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.////The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer//divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and//it will be read-only if, and only if, this buffer is read-only.////Returns://A new int buffer IntBuffer *ByteBuffer::asIntBuffer(){ // TODO 4J Stu - Is it safe to just cast our byte array pointer to another type? return new IntBuffer( (m_limit-m_position)/4, (int *) (buffer+m_position) );}
//Creates a view of this byte buffer as a float buffer.//The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be//visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.////The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer//divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct,//and it will be read-only if, and only if, this buffer is read-only.////Returns://A new float bufferFloatBuffer *ByteBuffer::asFloatBuffer(){ // TODO 4J Stu - Is it safe to just cast our byte array pointer to another type? return new FloatBuffer( (m_limit-m_position)/4, (float *) (buffer+m_position) );}
#ifdef __PS3__// we're using the RSX now to upload textures to vram, so we need th main ram textures allocated from io spaceByteBuffer_IO::ByteBuffer_IO( unsigned int capacity ) : ByteBuffer(capacity, (byte*)RenderManager.allocIOMem(capacity, 64)){ memset( buffer,0,sizeof(byte)*capacity); byteOrder = BIGENDIAN;}
ByteBuffer_IO::~ByteBuffer_IO(){// delete buffer; RenderManager.freeIOMem(buffer);}#endif // __PS3__