minecraft.js
Version:
Minecraft data serialization/deserialization and networking
36 lines (30 loc) • 911 B
JavaScript
/** @constructor */
var Uint4Array = module.exports = function(buffer, byteOffset, length) {
this.buffer = buffer;
this.byteOffset = byteOffset || 0;
this.length = length || this.buffer.byteLength;
this.view = new Uint8Array(this.buffer, this.byteOffset, Math.ceil(this.length / 2));
if(this.length > this.buffer.byteLength * 2) {
//TODO: throw exception
}
};
Uint4Array.prototype.get = function(index) {
var value = this.view[Math.floor(index / 2)];
if(index % 2 == 0) {
value = value >>> 4;
} else {
value = (value << 28) >>> 28;
}
return value;
};
Uint4Array.prototype.set = function(index, value) {
value = Math.floor(value);
value = (value << 28) >>> 28;
if(index % 2 == 0) {
value = (value << 4) + this.get(index + 1);
} else {
value = (this.get(index - 1) << 4) + value;
}
this.view[Math.floor(index / 2)] = value;
return this;
};