Advertisement
Guest User

Untitled

a guest
Aug 5th, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. "use strict";
  2.  
  3. const fs = require('fs');
  4. const needle = fs.readFileSync('A_WALK_L_02.BIN');
  5.  
  6. const blocks = {
  7. ids : [0,8],
  8. do_push : 0,
  9. b01 : {
  10. ofs : 0x0c,
  11. start : 0x0c,
  12. dat : [],
  13. len : []
  14. },
  15. b02 : {
  16. ofs : needle.readUInt16LE(0x04),
  17. start : needle.readUInt16LE(0x04),
  18. dat : [],
  19. len : []
  20. },
  21. b03 : {
  22. ofs : needle.readUInt16LE(0x06),
  23. start : needle.readUInt16LE(0x06),
  24. dat : [],
  25. len : []
  26. },
  27. b04 : {
  28. ofs : needle.readUInt16LE(0x08),
  29. start : needle.readUInt16LE(0x08),
  30. dat : [],
  31. len : []
  32. },
  33. b05 : {
  34. ofs : needle.readUInt16LE(0x0a),
  35. start : needle.readUInt16LE(0x0a),
  36. dat : [],
  37. len : []
  38. }
  39. }
  40.  
  41. // Step 1 : Read
  42.  
  43. for(blocks.b01.ofs; blocks.b01.ofs < blocks.b02.start; blocks.b01.ofs += 2) {
  44.  
  45. const instruction = needle.readUInt16LE(blocks.b01.ofs);
  46. const node_id = instruction >> 9;
  47.  
  48. if(instruction === 0) {
  49. break;
  50. }
  51.  
  52. blocks.do_push = (blocks.ids.indexOf(node_id) === -1) ? 0 : 1;
  53. if(blocks.do_push) {
  54. console.log("DOING PUSH!!!");
  55. blocks.b01.dat.push(instruction);
  56. blocks.b01.len.push(2);
  57. }
  58.  
  59. if(instruction & 0x1c0) {
  60.  
  61. if(instruction & 0x100) {
  62. readBlock2();
  63. }
  64.  
  65. if(instruction & 0x80) {
  66. readBlock2();
  67. }
  68.  
  69. if(instruction & 0x40) {
  70. readBlock2();
  71. }
  72.  
  73. }
  74.  
  75. if(instruction & 0x38) {
  76.  
  77. if(instruction & 0x20) {
  78. readBlock2();
  79. }
  80.  
  81. if(instruction & 0x10) {
  82. readBlock2();
  83. }
  84.  
  85. if(instruction & 0x08) {
  86. readBlock2();
  87. }
  88.  
  89. }
  90.  
  91. }
  92.  
  93. function readBlock2() {
  94.  
  95. // Block 2
  96.  
  97. let keyFrameCount = needle.readUInt8(blocks.b02.ofs);
  98. blocks.b02.ofs++;
  99.  
  100. if(blocks.do_push) {
  101. blocks.b02.dat.push(keyFrameCount);
  102. blocks.b02.len.push(1);
  103. }
  104.  
  105. // Block 3
  106.  
  107. for(let i = 0; i < keyFrameCount; i++) {
  108.  
  109. let keyFrame = needle.readUInt8(blocks.b03.ofs);
  110. blocks.b03.ofs++;
  111.  
  112. if(blocks.do_push) {
  113. blocks.b03.dat.push(keyFrame);
  114. blocks.b03.len.push(1);
  115. }
  116.  
  117. }
  118.  
  119. keyFrameCount += 2;
  120.  
  121. // Block 4
  122.  
  123. const bits = [
  124. { m : 0xc0, a : 0x80, b : 0x40 },
  125. { m : 0x30, a : 0x20, b : 0x10 },
  126. { m : 0x0c, a : 0x08, b : 0x04 },
  127. { m : 0x03, a : 0x02, b : 0x01 }
  128. ];
  129.  
  130. let index = 0;
  131.  
  132. do {
  133.  
  134. let byte = needle.readUInt8(blocks.b04.ofs);
  135. blocks.b04.ofs++
  136.  
  137. if(blocks.do_push) {
  138. blocks.b04.dat.push(byte);
  139. blocks.b04.len.push(1);
  140. }
  141.  
  142. if(byte === 0) {
  143. return;
  144. }
  145.  
  146. bits.forEach( set => {
  147.  
  148. if((byte & set.m) === 0) {
  149. return;
  150. }
  151.  
  152. if(byte & set.a) {
  153. let a = needle.readUInt16LE(blocks.b05.ofs);
  154. blocks.b05.ofs += 2;
  155. let b = needle.readUInt16LE(blocks.b05.ofs);
  156. blocks.b05.ofs += 2;
  157.  
  158. if(blocks.do_push) {
  159. blocks.b05.dat.push(a,b);
  160. blocks.b05.len.push(2,2);
  161. }
  162.  
  163. };
  164.  
  165. if(byte & set.b) {
  166. let a = needle.readUInt16LE(blocks.b05.ofs);
  167. blocks.b05.ofs += 2;
  168.  
  169. if(blocks.do_push) {
  170. blocks.b05.dat.push(a);
  171. blocks.b05.len.push(2);
  172. }
  173. }
  174.  
  175. index++;
  176.  
  177. });
  178.  
  179. } while(index < keyFrameCount);
  180.  
  181.  
  182. }
  183.  
  184. delete blocks.ids;
  185. delete blocks.do_push;
  186.  
  187. blocks.b01.dat.push(0);
  188. blocks.b01.len.push(2);
  189.  
  190. for(let key in blocks) {
  191.  
  192. const b = blocks[key];
  193. let ofs = b.start;
  194.  
  195. for(let i = 0; i < b.dat.length; i++) {
  196.  
  197. switch(b.len[i]) {
  198. case 1:
  199. needle.writeUInt8(b.dat[i], ofs);
  200. break;
  201. case 2:
  202. needle.writeUInt16LE(b.dat[i], ofs);
  203. break;
  204. }
  205. ofs += b.len[i];
  206.  
  207. }
  208.  
  209. }
  210.  
  211. fs.writeFileSync("walk_dat.bin", needle);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement