Advertisement
Guest User

Untitled

a guest
May 26th, 2021
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. "use strict";
  2.  
  3. // Imports
  4.  
  5. const fs = require('fs')
  6. const DAT_PATH = '/home/pi/Documents/mml2data/DAT';
  7. const COMMON_PATH = '/home/pi/Documents/mml2data/COMMON';
  8. const OUT_PATH = '/home/pi/Documents/out';
  9.  
  10. // Constants
  11.  
  12. const PAL_TYPE = 0x02;
  13. const TIM_TYPE = 0x03;
  14. const EBD_TYPE = 0x0A;
  15.  
  16. // Characters (EBD)
  17.  
  18. fs.readdirSync(DAT_PATH).forEach(name => {
  19.  
  20. let file = fs.readFileSync(`${DAT_PATH}/${name}`);
  21. let type = file.readUInt32LE(0x00);
  22. if(type !== EBD_TYPE) {
  23. return;
  24. }
  25.  
  26. let len = file.readUInt32LE(0x04);
  27. let ebd = file.subarray(0x30, len + 0x30);
  28. let stub = name.substr(0, 4);
  29.  
  30. let dir = `${OUT_PATH}/${stub}`;
  31. if(!fs.existsSync(dir)) {
  32. fs.mkdirSync(dir);
  33. }
  34.  
  35. fs.writeFileSync(`${dir}/entities.ebd`, ebd);
  36.  
  37. });
  38.  
  39. // Textures (TIM)
  40.  
  41. console.log('Scanning for textures');
  42.  
  43. let found = false;
  44. fs.readdirSync(DAT_PATH).forEach(name => {
  45.  
  46. if(name !== 'ST1ET.BIN') {
  47. return;
  48. }
  49.  
  50. if(found) {
  51. return;
  52. }
  53.  
  54. let file = fs.readFileSync(`${DAT_PATH}/${name}`);
  55. for(let ofs = 0; ofs < file.length; ofs += 0x400) {
  56. let type = file.readUInt32LE(ofs);
  57. if(type !== TIM_TYPE) {
  58. continue;
  59. }
  60.  
  61. console.log('Found TIM at 0x%s', ofs.toString(16));
  62. found = true;
  63.  
  64. console.log('Found TIM in file %s, at offset 0x%s', name, ofs.toString(16));
  65. const header = {
  66. decompressed_size : file.readUInt16LE(ofs + 0x04),
  67. palette_x : file.readUInt16LE(ofs + 0x0c),
  68. palette_y : file.readUInt16LE(ofs + 0x0e),
  69. palette_size : file.readUInt16LE(ofs + 0x10),
  70. palette_count : file.readUInt16LE(ofs + 0x12),
  71. image_x : file.readUInt16LE(ofs + 0x14),
  72. image_y : file.readUInt16LE(ofs + 0x16),
  73. width : file.readUInt16LE(ofs + 0x18),
  74. height : file.readUInt16LE(ofs + 0x1a),
  75. bitfield_size : file.readUInt16LE(ofs + 0x24)
  76. }
  77.  
  78. console.log(header);
  79. console.log('Decompress Size: 0x%s', header.decompressed_size.toString(16));
  80. console.log('Bitfield Size: 0x%s', header.bitfield_size.toString(16));
  81.  
  82. // Decompress the Texture Data
  83.  
  84. let tmp = ofs + 0x30;
  85.  
  86. console.log('Start of Bitfield: 0x%s', tmp.toString(16));
  87. const bitfield = file.subarray(tmp, tmp + header.bitfield_size);
  88. tmp += header.bitfield_size;
  89. console.log('Current position: 0x%s', tmp.toString(16));
  90.  
  91. // Try to Decompress
  92.  
  93. let a = readBits8LE(bitfield);
  94. let b = readBits16LE(bitfield);
  95. let c = readBits32LE(bitfield);
  96. let d = readBits8LE(bitfield);
  97. let e = readBits16BE(bitfield);
  98. let f = readBits32BE(bitfield);
  99.  
  100. console.log(header.bitfield_size);
  101. console.log(' ----- ');
  102.  
  103. /*
  104. console.log();
  105. console.log('8 Bytes Low to High');
  106. decompress(file, tmp, a, header)
  107.  
  108. console.log();
  109. console.log('16 Bytes Low to High');
  110. decompress(file, tmp, b, header)
  111.  
  112. console.log();
  113. console.log('32 Bytes Low to High');
  114. decompress(file, tmp, c, header)
  115.  
  116. console.log();
  117. console.log('8 Bytes high to low');
  118. decompress(file, tmp, d, header)
  119.  
  120. console.log();
  121. console.log('16 Bytes high to low');
  122. decompress(file, tmp, e, header)
  123. */
  124.  
  125. console.log();
  126. console.log('32 Bytes high to low');
  127. decompress(file, tmp, f, header)
  128.  
  129. break;
  130. }
  131.  
  132. });
  133.  
  134. function readBits8LE(buffer) {
  135.  
  136. let bits = [];
  137. for(let ofs = 0; ofs < buffer.length; ofs += 1) {
  138. let val = buffer.readUInt8(ofs);
  139. for(let i = 0; i < 8; i++) {
  140. let bit = 1 << i;
  141. bits.push(val & bit ? 1 : 0);
  142. }
  143. }
  144. return bits;
  145.  
  146. }
  147.  
  148. function readBits16LE(buffer) {
  149.  
  150. let bits = [];
  151. for(let ofs = 0; ofs < buffer.length; ofs += 2) {
  152. let val = buffer.readUInt16LE(ofs);
  153. for(let i = 0; i < 16; i++) {
  154. let bit = 1 << i;
  155. bits.push(val & bit ? 1 : 0);
  156. }
  157. }
  158. return bits;
  159.  
  160. }
  161.  
  162. function readBits32LE(buffer) {
  163.  
  164. let bits = [];
  165. for(let ofs = 0; ofs < buffer.length; ofs += 4) {
  166. let val = buffer.readUInt32LE(ofs);
  167. for(let i = 0; i < 32; i++) {
  168. let bit = 1 << i;
  169. bits.push(val & bit ? 1 : 0);
  170. }
  171. }
  172. return bits;
  173.  
  174. }
  175.  
  176. function readBits8BE(buffer) {
  177.  
  178. let bits = [];
  179. for(let ofs = 0; ofs < buffer.length; ofs += 1) {
  180. let val = buffer.readUInt8(ofs);
  181. for(let i = 7; i >= 0; i--) {
  182. let bit = 1 << i;
  183. bits.push(val & bit ? 1 : 0);
  184. }
  185. }
  186. return bits;
  187.  
  188. }
  189.  
  190. function readBits16BE(buffer) {
  191.  
  192. let bits = [];
  193. for(let ofs = 0; ofs < buffer.length; ofs += 2) {
  194. let val = buffer.readUInt16LE(ofs);
  195. for(let i = 15; i >= 0; i--) {
  196. let bit = 1 << i;
  197. bits.push(val & bit ? 1 : 0);
  198. }
  199. }
  200. return bits;
  201.  
  202. }
  203.  
  204. function readBits32BE(buffer) {
  205.  
  206. let bits = [];
  207. for(let ofs = 0; ofs < buffer.length; ofs += 4) {
  208. let val = buffer.readUInt32LE(ofs);
  209. for(let i = 31; i >= 0; i--) {
  210. let bit = 1 << i;
  211. bits.push(val & bit ? 1 : 0);
  212. }
  213. }
  214. return bits;
  215.  
  216. }
  217.  
  218.  
  219. function decompress(file, ofs, bits, header) {
  220.  
  221. let out_ofs = 0;
  222. let window_ofs = 0;
  223. let buffer = Buffer.alloc(header.decompressed_size);
  224. console.log('Starting decompression at: 0x%s', ofs.toString(16));
  225.  
  226. for(let i = 0; i < bits.length; i++) {
  227.  
  228. let bit = bits[i];
  229. let word = file.readUInt16LE(ofs);
  230. ofs += 2;
  231.  
  232. if(bit === 0) {
  233. buffer.writeUInt16LE(word, out_ofs);
  234. out_ofs += 2;
  235. } else if(word === 0xffff) {
  236. window_ofs += 0x2000;
  237. } else {
  238. let val = ((word >> 3) & 0x1fff);
  239. let copy_from = window_ofs + val;
  240. let copy_len = (word & 0x07) + 2;
  241. while(copy_len--) {
  242. let w = buffer.readUInt16LE(copy_from);
  243. copy_from += 2;
  244. buffer.writeUInt16LE(w, out_ofs);
  245. out_ofs += 2;
  246. }
  247. }
  248.  
  249. if(window_ofs > buffer.length) {
  250. break;
  251. }
  252.  
  253. }
  254.  
  255. return buffer;
  256. }
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement