diff --git a/memory.js b/memory.js index bf14cb7..430ad38 100644 --- a/memory.js +++ b/memory.js @@ -131,6 +131,7 @@ class ConstructedData extends ProcessableData { */ readString(size){ let minibuffer = this.data.buffer.slice(this.cursor, this.cursor + size); + this.cursor += size; return new TextDecoder().decode(minibuffer); } /** @@ -145,21 +146,21 @@ class ConstructedData extends ProcessableData { */ writeFlags(flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8){ let value = ( - (flag1 << 1) | - (flag2 << 2) | - (flag3 << 3) | - (flag4 << 4) | - (flag5 << 5) | - (flag6 << 6) | - (flag7 << 7) | - (flag8 << 8) + (flag1 << 0) | + (flag2 << 1) | + (flag3 << 2) | + (flag4 << 3) | + (flag5 << 4) | + (flag6 << 5) | + (flag7 << 6) | + (flag8 << 7) ); this.writeUInt8(value); } /** @returns {number[]} */ readFlags(){ let value = this.readUInt8(); - let result = ( + let result = [ (value >> 0 & 0x1), (value >> 1 & 0x1), (value >> 2 & 0x1), @@ -168,7 +169,7 @@ class ConstructedData extends ProcessableData { (value >> 5 & 0x1), (value >> 6 & 0x1), (value >> 7 & 0x1) - ); + ]; return result; } /** @@ -353,12 +354,245 @@ class ConstructedData extends ProcessableData { } } /** @param {Date|string} dateorString */ - writeDate(dateorString){ + writeTime(dateorString){ let date = new Date(dateorString); this.writeUInt32(+date); } /** @returns {Date} */ - readDate(){ + readTime(){ return new Date(this.readUInt32()); } -}; \ No newline at end of file +}; + +class DataAnchor extends ConstructedData { + /** + * @param {string|number} name + * @returns {{read:Function,write:Function,sign:number,name:string,primitive:boolean}} + */ + getProcessor(name) + { + switch(name) + { + case "uint8": + case "byte": + case 1: return { + read : () => this.readUInt8(), + write : value => this.writeUInt8(value), + sign: 1, + name: "uint8", + primitive: true + } + case "uint16": + case 2: return { + read : () => this.readUInt16(), + write : value => this.writeUInt16(value), + sign: 2, + name: "uint16", + primitive: true + } + case "uint32": + case 3: return { + read : () => this.readUInt32(), + write : value => this.writeUInt32(value), + sign: 3, + name: "uint32", + primitive: true + } + case "int8": + case 4: return { + read : () => this.readInt8(), + write : value => this.writeInt8(value), + sign: 4, + name: "int8", + primitive: true + } + case "int16": + case 5: return { + read : () => this.readInt16(), + write : value => this.writeInt16(value), + sign: 5, + name: "int16", + primitive: true + } + case "int32": + case 6: return { + read : () => this.readInt32(), + write : value => this.writeInt32(value), + sign: 6, + name: "int32", + primitive: true + } + case "float32": + case 7: return { + read : () => this.readFloat32(), + write : value => this.writeFloat32(value), + sign: 7, + name: "float32", + primitive: true + } + case "float64": + case 8: return { + read : () => this.readFloat64(), + write : value => this.writeFloat64(value), + sign: 8, + name: "float64", + primitive: true + } + case "string": + case 9: return { + read : size => this.readString(size), + write : value => this.writeString(value), + sign: 9, + name: "string", + primitive: false + } + case "rgb": + case 10: return { + read : () => this.readRGB(), + write : value => this.writeRGB(value), + sign: 10, + name: "rgb", + primitive: true + } + case "rgba": + case 11: return { + read : () => this.readRGBA(), + write : value => this.writeRGBA(value), + sign: 11, + name: "rgba", + primitive: true + } + case "varnumber": + case 12: return { + read : () => this.readVarNumber(), + write : value => this.writeVarNumber(value), + sign: 12, + name: "varnumber", + primitive: true + } + case "time": + case 13: return { + read : () => this.readTime(), + write : value => this.writeTime(value), + sign: 13, + name: "time", + primitive: true + } + }; + } +} + +class DataStructure { + /** @type {boolean} */ + sign = true; + /** @type {string} */ + type = "uint8"; + /** @type {boolean} */ + list = true; + /** @type {boolean} */ + fixedLength = true; + /** @type {any} */ + value = null; + size = 1; + /** @param {DataAnchor} da */ + setData(da){ + let dataCost = da.getProcessor(this.type); + if(this.sign == true) + { + // Verinin içine veri türüni işliyor + da.writeUInt8(dataCost.sign); + } + if(this.fixedLength == false && this.list == true) + { + // Eğer sabit uzunlukta değilse ne kadar uzun olduğunu bildirmeli + da.writeVarNumber(this.size) + } + if(dataCost.primitive) + { + if(this.list == true) + { + for (const data of this.value) + { + dataCost.write(data); + } + }else{ + dataCost.write(this.value); + } + }else{ + if(this.list == true) + { + for (const data of this.value) + { + da.writeVarNumber(data.length); + dataCost.write(data); + } + }else{ + da.writeVarNumber(this.value.length); + dataCost.write(this.value); + } + } + } + /** @param {DataAnchor} da */ + getData(da){ + let result = null, sign; + if(this.sign == true) + { + // Verinin içine veri türüni işliyor + sign = da.readUInt8(); + } + let dataCost = da.getProcessor(this.type); + if(this.fixedLength == false && this.list == true) + { + // Eğer sabit uzunlukta değilse ne kadar uzun olduğunu bildirmeli + this.size = da.readVarNumber(); + } + if(dataCost.primitive) + { + if(this.list == true) + { + result = []; + for (const data of this.value) + { + result.push(dataCost.read()); + } + }else{ + result = dataCost.read(); + } + }else{ + if(this.list == true) + { + result = []; + for (const data of this.value) + { + let length = da.readVarNumber(data.length); + result.push(dataCost.read(length)); + } + }else{ + let length = da.readVarNumber(data.length); + result = dataCost.read(length); + } + } + return result; + } + /** @param {string} dataSigner */ + setHeadSigner(dataSigner){ + let regex = /^(\w+)(\[\d*\])?$/.exec(dataSigner); + + if(regex[1] == 'any') + { + this.sign = true; + }else{ + this.sign = false; + this.type = regex[1]; + } + + if(regex[2]) + { + this.list = true; + this.fixedLength = regex[2] != '[]' + this.size = regex[2].slice(1,-1) | 0; + }else{ + this.list = false; + } + } +}