Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const regex = new RegExp(`(?<mainType>\\w+)(?:<(?<subType>\\w+)>)?(?:\\[(?<count>\\w*)\\])?`);
- const vectorAxes = [
- ["x", "y"],
- ["x", "y", "z"],
- ["x", "y", "z", "w"],
- ];
- const Descriptor = new class Descriptor {
- constructor() {
- this.types = {};
- this.staticTypes = {};
- this.vectorClasses = {};
- this.matrixClasses = {};
- this.dataViewMap = {};
- this.createStaticTypes();
- this.createVectorTypes();
- this.createMatrixTypes();
- }
- createStaticTypes() {
- for (let power of [0, 1, 2, 3]) {
- this.createDataViewMapElement("s", "Int", power);
- this.createStaticType("s", power);
- this.createDataViewMapElement("u", "Uint", power);
- this.createStaticType("u", power);
- }
- for (let power of [2, 3]) {
- this.createDataViewMapElement("f", "Float", power);
- this.createStaticType("f", power);
- }
- }
- createDataViewMapElement(char, mapped, power) {
- const bitSize = (2 ** power) * 8;
- this.dataViewMap[`${char}${bitSize}`] = `${mapped}${bitSize}`;
- }
- createStaticType(char, power) {
- const byteSize = 2 ** power;
- const bitSize = byteSize * 8;
- const typeName = `${char}${bitSize}`;
- const dataViewType = this.dataViewMap[typeName];
- const constructor = (function () {
- return function (offset) {
- this.offset = offset;
- };
- })();
- const {prototype} = constructor;
- Object.defineProperty(prototype, 'typeName', {value: typeName});
- Object.defineProperty(prototype, 'byteSize', {value: byteSize});
- Object.defineProperty(prototype, 'getterDataViewTypeName', {value: `set${dataViewType}`});
- Object.defineProperty(prototype, 'setterDataViewTypeName', {value: `set${dataViewType}`});
- prototype.get = function (object) {
- return object['dataView'][this['getterDataViewTypeName']](this.offset);
- };
- prototype.set = function (object, value) {
- return object['dataView'][this['setterDataViewTypeName']](this.offset, value);
- };
- this.staticTypes[typeName] = constructor;
- this.types[typeName] = constructor;
- }
- createVectorTypes() {
- for (let axis of vectorAxes) {
- for (let staticType of Object.values(this.staticTypes)) {
- this.createVectorType(axis, staticType)
- }
- }
- }
- createVectorType(axis, staticType) {
- const {typeName: staticTypeName, byteSize: staticByteSize} = staticType.prototype;
- const vectorLength = axis.length;
- const vectorTypeName = `vec${vectorLength}<${staticTypeName}>`;
- const vectorByteSize = vectorLength * staticByteSize;
- const constructor = (function (length) {
- return function (offset) {
- this.offset = offset;
- this.length = length;
- };
- })(vectorLength);
- const {prototype} = constructor;
- Object.defineProperty(prototype, 'typeName', {value: vectorTypeName});
- Object.defineProperty(prototype, 'byteSize', {value: vectorByteSize});
- Object.defineProperty(prototype, 'axisType', {value: staticType});
- for (let [axisOffset, field] of axis.entries()) {
- const offset = staticByteSize * axisOffset;
- const staticProperty = new staticType(offset);
- prototype[field] = staticProperty;
- prototype[axisOffset] = staticProperty;
- }
- this.types[vectorTypeName] = constructor;
- }
- createMatrixTypes() {
- for (let matrixWidth = 2; matrixWidth <= 4; matrixWidth++) {
- for (let staticTypes of Object.values(this.staticTypes)) {
- this.createMatrixType(matrixWidth, staticTypes);
- }
- }
- }
- createMatrixType(matrixWidth, staticType) {
- const {typeName: staticTypeName, byteSize: staticByteSize} = staticType.prototype;
- const matrixTypeName = `mat${matrixWidth}<${staticTypeName}>`;
- const matrixElements = (matrixWidth ** 2);
- const matrixByteSize = matrixElements * staticByteSize;
- const constructor = (function (length) {
- return function (offset) {
- this.offset = offset;
- this.length = length;
- };
- })(matrixElements);
- const {prototype} = constructor;
- Object.defineProperty(prototype, 'typeName', {value: matrixTypeName});
- Object.defineProperty(prototype, 'byteSize', {value: matrixByteSize});
- Object.defineProperty(prototype, 'axisType', {value: staticType});
- for (let i = 0; i < matrixElements; i++) {
- const offset = staticByteSize * i;
- const staticProperty = new staticType(offset);
- prototype[i] = staticProperty;
- }
- this.types[matrixTypeName] = constructor;
- }
- register(constructor, config) {
- const {prototype} = constructor;
- const context = {
- prototype,
- offset: 0
- };
- for (let propertyDeclaration of config.properties) {
- //const property = this.createProperty(context, propertyDeclaration);
- }
- }
- createProperty(context, propertyDeclaration) {
- const {type} = propertyDeclaration;
- const match = regex.exec(type);
- const {mainType, subType, count} = match.groups;
- const typeKey = `${mainType}` + (subType ? `<${subType}>` : '');
- const typeDefinition = this.types[typeKey];
- if (count === "") {
- return new DynamicArrayProperty({
- name: propertyDeclaration.name,
- });
- }
- const parsedCount = parseInt(count);
- if (!isNaN(parsedCount)) {
- return new StaticArrayProperty({
- length: parsedCount,
- })
- }
- const {define, size} = property.type;
- const descriptor = define(property, offset);
- if (size !== undefined && offset !== undefined) {
- offset += size;
- } else {
- offset = undefined;
- }
- Object.defineProperty(prototype, property.name, descriptor);
- }
- parseProperty(propertyDeclaration) {
- const {type} = propertyDeclaration;
- const match = regex.exec(type);
- const {mainType, subType, count} = match.groups;
- const typeDefinition = this.types[typeKey];
- if (typeDefinition === undefined) {
- throw new Error(`Type (${type}) not exists`);
- }
- const {property: propertyName, ...others} = propertyDeclaration;
- return {
- name: propertyName,
- ...others,
- type: {
- ...typeDefinition
- },
- };
- }
- };
- /**
- * @property tragedia
- */
- class Chunk {
- constructor(options) {
- this.dataView = options['dataView'];
- }
- }
- Descriptor.register(Chunk, {
- name: "Chunk",
- properties: [
- {type: "u8", property: "type"},
- {type: "u8[8]", property: "blockBitWidth"},
- {type: "vec3<u16>[4]", property: "position"},
- {type: "mat4<f32>", property: "modelViewMatrix"},
- {type: "u16", property: "blockDataOffset"},
- {type: "u16", property: "blockIndexDataOffset"},
- {type: "u16", property: "terrainDataOffset"},
- {type: "u16", property: "additionalDataOffset"},
- {type: "u16", property: "overrideDataOffset"},
- {type: "u16", property: "blockDataSize"},
- {type: "u16", property: "blockIndexDataSize"},
- {type: "u16", property: "terrainDataSize"},
- {type: "u16", property: "additionalDataSize"},
- {type: "u16", property: "overrideDataSize"},
- {type: "blob"},
- {type: "blob", property: "blockData"},
- {type: "blob", property: "blockIndexData"},
- {type: "blob", property: "terrainData"},
- {type: "blob", property: "additionalData"},
- {type: "blob", property: "overrideData"},
- ],
- });
- console.log(Descriptor);
- const buffer = new ArrayBuffer(1000);
- const dataView = new DataView(buffer, 0);
- dataView.setInt8(0, 0xff);
- const chunk = new Chunk({dataView});
- chunk.get = function (propertyName) {
- const property = this.description[propertyName];
- if (property === undefined) {
- throw new Error(`Cannot get property (${propertyName})`);
- }
- return property.getValue(this);
- };
- chunk.position.index(3).x.read(chunk);
- chunk.getIndex("position", 2).set(48);
- console.log(buffer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement