the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2
3#include "IntBuffer.h"
4#include "FloatBuffer.h"
5#include "ByteBuffer.h"
6
7ByteBuffer::ByteBuffer( unsigned int capacity ) : Buffer( capacity )
8{
9 hasBackingArray = false;
10 buffer = new byte[capacity];
11 memset( buffer,0,sizeof(byte)*capacity);
12 byteOrder = BIGENDIAN;
13}
14
15//Allocates a new direct byte buffer.
16//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.
17//
18//Parameters:
19//capacity - The new buffer's capacity, in bytes
20//Returns:
21//The new byte buffer
22ByteBuffer *ByteBuffer::allocateDirect(int capacity)
23{
24 return new ByteBuffer(capacity);
25}
26
27
28ByteBuffer::ByteBuffer( unsigned int capacity, byte *backingArray ) : Buffer( capacity )
29{
30 hasBackingArray = true;
31 buffer = backingArray;
32}
33
34ByteBuffer::~ByteBuffer()
35{
36 if( !hasBackingArray )
37 delete[] buffer;
38}
39
40//Wraps a byte array into a buffer.
41//The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array
42//to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero,
43//and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
44//
45//Parameters:
46//array - The array that will back this buffer
47//Returns:
48//The new byte buffer
49ByteBuffer *ByteBuffer::wrap(byteArray &b)
50{
51 return new ByteBuffer( b.length, b.data );
52}
53
54//Allocates a new byte buffer.
55//The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined.
56//It will have a backing array, and its array offset will be zero.
57//
58//Parameters:
59//capacity - The new buffer's capacity, in bytes
60//Returns:
61//The new byte buffer
62ByteBuffer *ByteBuffer::allocate(unsigned int capacity)
63{
64 return new ByteBuffer( capacity );
65}
66
67//Modifies this buffer's byte order.
68//Parameters:
69//bo - The new byte order, either BIGENDIAN or LITTLEENDIAN
70void ByteBuffer::order(ByteOrder bo)
71{
72 byteOrder = bo;
73}
74
75//Flips this buffer. The limit is set to the current position and then the position is set to zero.
76//If the mark is defined then it is discarded.
77//
78//Returns:
79//This buffer
80ByteBuffer *ByteBuffer::flip()
81{
82 m_limit = m_position;
83 m_position = 0;
84 return this;
85}
86
87// 4J Added so we can write this to a file
88byte *ByteBuffer::getBuffer()
89{
90 return buffer;
91}
92
93int ByteBuffer::getSize()
94{
95 // TODO 4J Stu - Should this be the capcity and not the limit?
96 return m_limit;
97}
98// End 4J
99
100//Absolute get method. Reads the byte at the given index.
101//Parameters:
102//index - The index from which the byte will be read
103//Returns:
104//The byte at the given index
105//Throws:
106//IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
107BYTE ByteBuffer::get(int index)
108{
109 assert( index < m_limit );
110 assert( index >= 0 );
111
112 return buffer[index];
113}
114
115//Relative get method for reading an int value.
116//Reads the next four bytes at this buffer's current position, composing them into an int value according to the
117//current byte order, and then increments the position by four.
118//
119//Returns:
120//The int value at the buffer's current position
121int ByteBuffer::getInt()
122{
123 assert( m_position+3 < m_limit );
124
125 int value = 0;
126
127 int b1 = buffer[ m_position ];
128 int b2 = buffer[ m_position+1 ];
129 int b3 = buffer[ m_position+2 ];
130 int b4 = buffer[ m_position+3 ];
131
132 m_position += 4;
133
134 if( byteOrder == BIGENDIAN )
135 {
136 value = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
137 }
138 else if( byteOrder == LITTLEENDIAN )
139 {
140 value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
141 }
142 return value;
143}
144
145//Absolute get method for reading an int value.
146//Reads four bytes at the given index, composing them into a int value according to the current byte order.
147//
148//Parameters:
149//index - The index from which the bytes will be read
150//Returns:
151//The int value at the given index
152int ByteBuffer::getInt(unsigned int index)
153{
154 assert( index+3 < m_limit );
155 int value = 0;
156
157 int b1 = buffer[ index ];
158 int b2 = buffer[ index+1 ];
159 int b3 = buffer[ index+2 ];
160 int b4 = buffer[ index+3 ];
161
162 if( byteOrder == BIGENDIAN )
163 {
164 value = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
165 }
166 else if( byteOrder == LITTLEENDIAN )
167 {
168 value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
169 }
170 return value;
171}
172
173//Relative get method for reading a long value.
174//Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order,
175//and then increments the position by eight.
176//
177//Returns:
178//The long value at the buffer's current position
179__int64 ByteBuffer::getLong()
180{
181 assert( m_position+8 < m_limit );
182
183 __int64 value = 0;
184
185 __int64 b1 = buffer[ m_position ];
186 __int64 b2 = buffer[ m_position+1 ];
187 __int64 b3 = buffer[ m_position+2 ];
188 __int64 b4 = buffer[ m_position+3 ];
189 __int64 b5 = buffer[ m_position+4 ];
190 __int64 b6 = buffer[ m_position+5 ];
191 __int64 b7 = buffer[ m_position+6 ];
192 __int64 b8 = buffer[ m_position+7 ];
193
194 m_position += 8;
195
196 if( byteOrder == BIGENDIAN )
197 {
198 value = (b1 << 56) | (b2 << 48) | (b3 << 40) | (b4 << 32) | (b5 << 24) | (b6 << 16) | (b7 << 8) | b8;
199 }
200 else if( byteOrder == LITTLEENDIAN )
201 {
202 value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24) | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
203 }
204 return value;
205}
206
207//Relative get method for reading a short value.
208//Reads the next two bytes at this buffer's current position, composing them into a short value according to the current
209//byte order, and then increments the position by two.
210//
211//Returns:
212//The short value at the buffer's current position
213short ByteBuffer::getShort()
214{
215 assert( m_position+1 < m_limit );
216
217 short value = 0;
218
219 short b1 = buffer[ m_position ];
220 short b2 = buffer[ m_position+1 ];
221
222 m_position += 2;
223
224 if( byteOrder == BIGENDIAN )
225 {
226 value = (b1 << 8) | b2;
227 }
228 else if( byteOrder == LITTLEENDIAN )
229 {
230 value = b1 | (b2 << 8);
231 }
232 return value;
233}
234
235void ByteBuffer::getShortArray(shortArray &s)
236{
237 // TODO 4J Stu - Should this function be writing from the start of the buffer, or from position?
238 // And should it update position?
239 assert( s.length >= m_limit/2 );
240
241 // 4J Stu - Assumes big endian
242 memcpy( s.data, buffer, (m_limit-m_position) );
243}
244
245//Absolute put method (optional operation).
246//Writes the given byte into this buffer at the given index.
247//
248//Parameters:
249//index - The index at which the byte will be written
250//b - The byte value to be written
251//Returns:
252//This buffer
253//Throws:
254//IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
255//ReadOnlyBufferException - If this buffer is read-only
256ByteBuffer *ByteBuffer::put(int index, byte b)
257{
258 assert( index < m_limit );
259 assert( index >= 0 );
260
261 buffer[index] = b;
262 return this;
263}
264
265
266//Relative put method for writing an int value (optional operation).
267//Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position,
268//and then increments the position by four.
269//
270//Parameters:
271//value - The int value to be written
272//Returns:
273//This buffer
274ByteBuffer *ByteBuffer::putInt(int value)
275{
276 assert( m_position+3 < m_limit );
277
278 if( byteOrder == BIGENDIAN )
279 {
280 buffer[m_position] = (value >> 24) & 0xFF;
281 buffer[m_position+1] = (value >> 16) & 0xFF;
282 buffer[m_position+2] = (value >> 8) & 0xFF;
283 buffer[m_position+3] = value & 0xFF;
284 }
285 else if( byteOrder == LITTLEENDIAN )
286 {
287 buffer[m_position] = value & 0xFF;
288 buffer[m_position+1] = (value >> 8) & 0xFF;
289 buffer[m_position+2] = (value >> 16) & 0xFF;
290 buffer[m_position+3] = (value >> 24) & 0xFF;
291 }
292
293 m_position += 4;
294
295 return this;
296}
297
298//Absolute put method for writing an int value (optional operation).
299//Writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.
300//
301//Parameters:
302//index - The index at which the bytes will be written
303//value - The int value to be written
304//Returns:
305//This buffer
306ByteBuffer *ByteBuffer::putInt(unsigned int index, int value)
307{
308 assert( index+3 < m_limit );
309
310 if( byteOrder == BIGENDIAN )
311 {
312 buffer[index] = (value >> 24) & 0xFF;
313 buffer[index+1] = (value >> 16) & 0xFF;
314 buffer[index+2] = (value >> 8) & 0xFF;
315 buffer[index+3] = value & 0xFF;
316 }
317 else if( byteOrder == LITTLEENDIAN )
318 {
319 buffer[index] = value & 0xFF;
320 buffer[index+1] = (value >> 8) & 0xFF;
321 buffer[index+2] = (value >> 16) & 0xFF;
322 buffer[index+3] = (value >> 24) & 0xFF;
323 }
324
325 return this;
326}
327
328//Relative put method for writing a short value (optional operation).
329//Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position,
330//and then increments the position by two.
331//
332//Parameters:
333//value - The short value to be written
334//Returns:
335//This buffer
336ByteBuffer *ByteBuffer::putShort(short value)
337{
338 assert( m_position+1 < m_limit );
339
340 if( byteOrder == BIGENDIAN )
341 {
342 buffer[m_position] = (value >> 8) & 0xFF;
343 buffer[m_position+1] = value & 0xFF;
344 }
345 else if( byteOrder == LITTLEENDIAN )
346 {
347 buffer[m_position] = value & 0xFF;
348 buffer[m_position+1] = (value >> 8) & 0xFF;
349 }
350
351 m_position += 2;
352
353 return this;
354}
355
356ByteBuffer *ByteBuffer::putShortArray(shortArray &s)
357{
358 // TODO 4J Stu - Should this function be writing from the start of the buffer, or from position?
359 // And should it update position?
360 assert( s.length*2 <= m_limit);
361
362 // 4J Stu - Assumes big endian
363 memcpy( buffer, s.data, s.length*2 );
364
365 return this;
366}
367
368//Relative put method for writing a long value (optional operation).
369//Writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position,
370//and then increments the position by eight.
371//
372//Parameters:
373//value - The long value to be written
374//Returns:
375//This buffer
376ByteBuffer *ByteBuffer::putLong(__int64 value)
377{
378 assert( m_position+7 < m_limit );
379
380 if( byteOrder == BIGENDIAN )
381 {
382 buffer[m_position] = (value >> 56) & 0xFF;
383 buffer[m_position+1] = (value >> 48) & 0xFF;
384 buffer[m_position+2] = (value >> 40) & 0xFF;
385 buffer[m_position+3] = (value >> 32) & 0xFF;
386 buffer[m_position+4] = (value >> 24) & 0xFF;
387 buffer[m_position+5] = (value >> 16) & 0xFF;
388 buffer[m_position+6] = (value >> 8) & 0xFF;
389 buffer[m_position+7] = value & 0xFF;
390 }
391 else if( byteOrder == LITTLEENDIAN )
392 {
393 buffer[m_position] = value & 0xFF;
394 buffer[m_position+1] = (value >> 8) & 0xFF;
395 buffer[m_position+2] = (value >> 16) & 0xFF;
396 buffer[m_position+3] = (value >> 24) & 0xFF;
397 buffer[m_position+4] = (value >> 32) & 0xFF;
398 buffer[m_position+5] = (value >> 40) & 0xFF;
399 buffer[m_position+6] = (value >> 48) & 0xFF;
400 buffer[m_position+7] = (value >> 56) & 0xFF;
401 }
402
403 return this;
404}
405
406//Relative bulk put method (optional operation).
407//This method transfers the entire content of the given source byte array into this buffer.
408//An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation
409//
410// dst.put(a, 0, a.length)
411//Returns:
412//This buffer
413ByteBuffer *ByteBuffer::put(byteArray inputArray)
414{
415 if( inputArray.length > remaining() )
416 assert( false ); //TODO 4J Stu - Some kind of exception?
417
418 std::copy( inputArray.data, inputArray.data + inputArray.length, buffer+m_position );
419
420 m_position += inputArray.length;
421
422 return this;
423}
424
425byteArray ByteBuffer::array()
426{
427 return byteArray( buffer, m_capacity );
428}
429
430//Creates a view of this byte buffer as an int buffer.
431//The content of the new buffer will start at this buffer's current position. Changes to this buffer's content
432//will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
433//
434//The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer
435//divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and
436//it will be read-only if, and only if, this buffer is read-only.
437//
438//Returns:
439//A new int buffer
440IntBuffer *ByteBuffer::asIntBuffer()
441{
442 // TODO 4J Stu - Is it safe to just cast our byte array pointer to another type?
443 return new IntBuffer( (m_limit-m_position)/4, (int *) (buffer+m_position) );
444}
445
446//Creates a view of this byte buffer as a float buffer.
447//The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be
448//visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
449//
450//The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer
451//divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct,
452//and it will be read-only if, and only if, this buffer is read-only.
453//
454//Returns:
455//A new float buffer
456FloatBuffer *ByteBuffer::asFloatBuffer()
457{
458 // TODO 4J Stu - Is it safe to just cast our byte array pointer to another type?
459 return new FloatBuffer( (m_limit-m_position)/4, (float *) (buffer+m_position) );
460}
461
462
463
464#ifdef __PS3__
465// we're using the RSX now to upload textures to vram, so we need th main ram textures allocated from io space
466ByteBuffer_IO::ByteBuffer_IO( unsigned int capacity )
467 : ByteBuffer(capacity, (byte*)RenderManager.allocIOMem(capacity, 64))
468{
469 memset( buffer,0,sizeof(byte)*capacity);
470 byteOrder = BIGENDIAN;
471}
472
473ByteBuffer_IO::~ByteBuffer_IO()
474{
475// delete buffer;
476 RenderManager.freeIOMem(buffer);
477}
478#endif // __PS3__