gulp-armapbo
Version:
The plugin for Gulp which allows to pack ArmA pbo files from sources.
105 lines (103 loc) • 3.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class StackBuffer {
constructor() {
this._data = Buffer.allocUnsafe(StackBuffer.size);
this._fullfilment = 0;
}
add(buffer, offset, length) {
if (this._fullfilment + length > StackBuffer.size) { //shift the buffer contents left until there is enough space for the new bunch of bytes
const takeExisting = StackBuffer.size - length;
this._data.copyWithin(0, this._fullfilment - takeExisting, this._fullfilment);
buffer.copy(this._data, takeExisting, offset, offset + length);
this._fullfilment = StackBuffer.size;
}
else { //add some bytes onto the free space of the buffer
this._fullfilment += buffer.copy(this._data, this._fullfilment, offset, offset + length);
}
}
intersect(buffer, length) {
if (length && this._fullfilment) {
let intersection;
let offset = 0;
while (true) {
const next = this._intersectBufferAtOffset(buffer, length, offset);
if (!intersection || intersection.length < next.length) {
intersection = next;
}
if (next.position >= 0 && next.position <= this._fullfilment - 1) {
offset = next.position + 1;
}
else {
break;
}
}
return intersection;
}
else {
return { position: -1, length: 0 };
}
}
get fullfilment() {
return this._fullfilment;
}
_intersectBufferAtOffset(buffer, bLength, offset) {
let position = this._data.indexOf(buffer[0], offset);
let length = 0;
if (position >= 0 && position < this._fullfilment) {
length++;
for (let bufIndex = 1, dataIndex = position + 1; bufIndex < bLength && dataIndex < this.fullfilment; bufIndex++, dataIndex++) {
if (this._data[dataIndex] === buffer[bufIndex]) {
length++;
}
else {
break;
}
}
}
else {
position = -1;
}
return { position, length };
}
checkWhitespace(buffer, length) {
let count = 0;
for (let i = 0; i < length; i++) {
if (buffer[i] === 0x20) {
count++;
}
else {
break;
}
}
return count;
}
checkSequence(buffer, length) {
let result = { sourceBytes: 0, sequenceBytes: 0 };
const maxSourceBytes = Math.max(this._fullfilment, length);
for (let i = 1; i < maxSourceBytes; i++) {
const sequence = this._checkSequence(buffer, length, i);
if (sequence.sourceBytes > result.sourceBytes) {
result = sequence;
}
}
return result;
}
_checkSequence(buffer, length, sequenceBytes) {
const result = { sequenceBytes, sourceBytes: 0 };
while (result.sourceBytes < length) {
for (let i = this._fullfilment - sequenceBytes; i < this._fullfilment && result.sourceBytes < length; i++) {
if (buffer[result.sourceBytes] === this._data[i]) {
result.sourceBytes++;
}
else {
return result;
}
}
}
return result;
}
}
StackBuffer.size = 0b0000111111111111;
exports.StackBuffer = StackBuffer;
//# sourceMappingURL=stackBuffer.js.map