Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Parser } from 'binary-parser';
  2. import checkStatus from '../checkStatus';
  3.  
  4. const { error } = console;
  5.  
  6. const node = Parser.start()
  7.     .skip(1)
  8.     .int32le('nodeCount')
  9.     .array('nodes', {
  10.         type: Parser.start()
  11.             .floatle('x')
  12.             .floatle('y')
  13.             .floatle('z'),
  14.         length: 'nodeCount'
  15.     });
  16.  
  17. const anim = Parser.start()
  18.     .int32le('frameCount')
  19.     .array('frames', {
  20.         type: node,
  21.         length: 'frameCount'
  22.     })
  23.     .skip(96);
  24.  
  25. export default class {
  26.     constructor(path = 'gamedata/animations', ext = 'bin') {
  27.         this._cachedBinary = null;
  28.         this.path = path;
  29.         this.ext = ext;
  30.     }
  31.  
  32.     createCacheBinary() {
  33.         if (!this._cachedBinary) this._cachedBinary = new Map();
  34.         return true;
  35.     }
  36.  
  37.     clearCachedBinary() {
  38.         if (this._cachedBinary) {
  39.             this._cachedBinary.Clear();
  40.             this._cachedBinary = null;
  41.         }
  42.     }
  43.  
  44.     async readDataBinary(animName, useCache = true) {
  45.         if (useCache && this.createCacheBinary() && this._cachedBinary.has(animName)) {
  46.             return this._cachedBinary.get(animName);
  47.         }
  48.         try {
  49.             const response = await fetch(`${this.path}/${animName}.${this.ext}`).then(
  50.                 resp => checkStatus(resp) && resp
  51.             );
  52.             const buffer = await response.arrayBuffer();
  53.             const data = anim.parse(Buffer.from(buffer));
  54.             if (useCache) this._cachedBinary.set(animName, data);
  55.             return data;
  56.         } catch (err) {
  57.             error(err);
  58.         }
  59.         return false;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement