Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const fs = require('fs');
- const needle = fs.readFileSync('A_WALK_L_02.BIN');
- const blocks = {
- ids : [0,8],
- do_push : 0,
- b01 : {
- ofs : 0x0c,
- start : 0x0c,
- dat : [],
- len : []
- },
- b02 : {
- ofs : needle.readUInt16LE(0x04),
- start : needle.readUInt16LE(0x04),
- dat : [],
- len : []
- },
- b03 : {
- ofs : needle.readUInt16LE(0x06),
- start : needle.readUInt16LE(0x06),
- dat : [],
- len : []
- },
- b04 : {
- ofs : needle.readUInt16LE(0x08),
- start : needle.readUInt16LE(0x08),
- dat : [],
- len : []
- },
- b05 : {
- ofs : needle.readUInt16LE(0x0a),
- start : needle.readUInt16LE(0x0a),
- dat : [],
- len : []
- }
- }
- // Step 1 : Read
- for(blocks.b01.ofs; blocks.b01.ofs < blocks.b02.start; blocks.b01.ofs += 2) {
- const instruction = needle.readUInt16LE(blocks.b01.ofs);
- const node_id = instruction >> 9;
- if(instruction === 0) {
- break;
- }
- blocks.do_push = (blocks.ids.indexOf(node_id) === -1) ? 0 : 1;
- if(blocks.do_push) {
- console.log("DOING PUSH!!!");
- blocks.b01.dat.push(instruction);
- blocks.b01.len.push(2);
- }
- if(instruction & 0x1c0) {
- if(instruction & 0x100) {
- readBlock2();
- }
- if(instruction & 0x80) {
- readBlock2();
- }
- if(instruction & 0x40) {
- readBlock2();
- }
- }
- if(instruction & 0x38) {
- if(instruction & 0x20) {
- readBlock2();
- }
- if(instruction & 0x10) {
- readBlock2();
- }
- if(instruction & 0x08) {
- readBlock2();
- }
- }
- }
- function readBlock2() {
- // Block 2
- let keyFrameCount = needle.readUInt8(blocks.b02.ofs);
- blocks.b02.ofs++;
- if(blocks.do_push) {
- blocks.b02.dat.push(keyFrameCount);
- blocks.b02.len.push(1);
- }
- // Block 3
- for(let i = 0; i < keyFrameCount; i++) {
- let keyFrame = needle.readUInt8(blocks.b03.ofs);
- blocks.b03.ofs++;
- if(blocks.do_push) {
- blocks.b03.dat.push(keyFrame);
- blocks.b03.len.push(1);
- }
- }
- keyFrameCount += 2;
- // Block 4
- const bits = [
- { m : 0xc0, a : 0x80, b : 0x40 },
- { m : 0x30, a : 0x20, b : 0x10 },
- { m : 0x0c, a : 0x08, b : 0x04 },
- { m : 0x03, a : 0x02, b : 0x01 }
- ];
- let index = 0;
- do {
- let byte = needle.readUInt8(blocks.b04.ofs);
- blocks.b04.ofs++
- if(blocks.do_push) {
- blocks.b04.dat.push(byte);
- blocks.b04.len.push(1);
- }
- if(byte === 0) {
- return;
- }
- bits.forEach( set => {
- if((byte & set.m) === 0) {
- return;
- }
- if(byte & set.a) {
- let a = needle.readUInt16LE(blocks.b05.ofs);
- blocks.b05.ofs += 2;
- let b = needle.readUInt16LE(blocks.b05.ofs);
- blocks.b05.ofs += 2;
- if(blocks.do_push) {
- blocks.b05.dat.push(a,b);
- blocks.b05.len.push(2,2);
- }
- };
- if(byte & set.b) {
- let a = needle.readUInt16LE(blocks.b05.ofs);
- blocks.b05.ofs += 2;
- if(blocks.do_push) {
- blocks.b05.dat.push(a);
- blocks.b05.len.push(2);
- }
- }
- index++;
- });
- } while(index < keyFrameCount);
- }
- delete blocks.ids;
- delete blocks.do_push;
- blocks.b01.dat.push(0);
- blocks.b01.len.push(2);
- for(let key in blocks) {
- const b = blocks[key];
- let ofs = b.start;
- for(let i = 0; i < b.dat.length; i++) {
- switch(b.len[i]) {
- case 1:
- needle.writeUInt8(b.dat[i], ofs);
- break;
- case 2:
- needle.writeUInt16LE(b.dat[i], ofs);
- break;
- }
- ofs += b.len[i];
- }
- }
- fs.writeFileSync("walk_dat.bin", needle);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement