Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Parser } from 'binary-parser';
- import checkStatus from '../checkStatus';
- const { error } = console;
- const node = Parser.start()
- .skip(1)
- .int32le('nodeCount')
- .array('nodes', {
- type: Parser.start()
- .floatle('x')
- .floatle('y')
- .floatle('z'),
- length: 'nodeCount'
- });
- const anim = Parser.start()
- .int32le('frameCount')
- .array('frames', {
- type: node,
- length: 'frameCount'
- })
- .skip(96);
- export default class {
- constructor(path = 'gamedata/animations', ext = 'bin') {
- this._cachedBinary = null;
- this.path = path;
- this.ext = ext;
- }
- createCacheBinary() {
- if (!this._cachedBinary) this._cachedBinary = new Map();
- return true;
- }
- clearCachedBinary() {
- if (this._cachedBinary) {
- this._cachedBinary.Clear();
- this._cachedBinary = null;
- }
- }
- async readDataBinary(animName, useCache = true) {
- if (useCache && this.createCacheBinary() && this._cachedBinary.has(animName)) {
- return this._cachedBinary.get(animName);
- }
- try {
- const response = await fetch(`${this.path}/${animName}.${this.ext}`).then(
- resp => checkStatus(resp) && resp
- );
- const buffer = await response.arrayBuffer();
- const data = anim.parse(Buffer.from(buffer));
- if (useCache) this._cachedBinary.set(animName, data);
- return data;
- } catch (err) {
- error(err);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement