Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- // Imports
- const fs = require('fs')
- const DAT_PATH = '/home/pi/Documents/mml2data/DAT';
- const COMMON_PATH = '/home/pi/Documents/mml2data/COMMON';
- const OUT_PATH = '/home/pi/Documents/out';
- // Constants
- const PAL_TYPE = 0x02;
- const TIM_TYPE = 0x03;
- const EBD_TYPE = 0x0A;
- // Characters (EBD)
- fs.readdirSync(DAT_PATH).forEach(name => {
- let file = fs.readFileSync(`${DAT_PATH}/${name}`);
- let type = file.readUInt32LE(0x00);
- if(type !== EBD_TYPE) {
- return;
- }
- let len = file.readUInt32LE(0x04);
- let ebd = file.subarray(0x30, len + 0x30);
- let stub = name.substr(0, 4);
- let dir = `${OUT_PATH}/${stub}`;
- if(!fs.existsSync(dir)) {
- fs.mkdirSync(dir);
- }
- fs.writeFileSync(`${dir}/entities.ebd`, ebd);
- });
- // Textures (TIM)
- console.log('Scanning for textures');
- let found = false;
- fs.readdirSync(DAT_PATH).forEach(name => {
- if(name !== 'ST1ET.BIN') {
- return;
- }
- if(found) {
- return;
- }
- let file = fs.readFileSync(`${DAT_PATH}/${name}`);
- for(let ofs = 0; ofs < file.length; ofs += 0x400) {
- let type = file.readUInt32LE(ofs);
- if(type !== TIM_TYPE) {
- continue;
- }
- console.log('Found TIM at 0x%s', ofs.toString(16));
- found = true;
- console.log('Found TIM in file %s, at offset 0x%s', name, ofs.toString(16));
- const header = {
- decompressed_size : file.readUInt16LE(ofs + 0x04),
- palette_x : file.readUInt16LE(ofs + 0x0c),
- palette_y : file.readUInt16LE(ofs + 0x0e),
- palette_size : file.readUInt16LE(ofs + 0x10),
- palette_count : file.readUInt16LE(ofs + 0x12),
- image_x : file.readUInt16LE(ofs + 0x14),
- image_y : file.readUInt16LE(ofs + 0x16),
- width : file.readUInt16LE(ofs + 0x18),
- height : file.readUInt16LE(ofs + 0x1a),
- bitfield_size : file.readUInt16LE(ofs + 0x24)
- }
- console.log(header);
- console.log('Decompress Size: 0x%s', header.decompressed_size.toString(16));
- console.log('Bitfield Size: 0x%s', header.bitfield_size.toString(16));
- // Decompress the Texture Data
- let tmp = ofs + 0x30;
- console.log('Start of Bitfield: 0x%s', tmp.toString(16));
- const bitfield = file.subarray(tmp, tmp + header.bitfield_size);
- tmp += header.bitfield_size;
- console.log('Current position: 0x%s', tmp.toString(16));
- // Try to Decompress
- let a = readBits8LE(bitfield);
- let b = readBits16LE(bitfield);
- let c = readBits32LE(bitfield);
- let d = readBits8LE(bitfield);
- let e = readBits16BE(bitfield);
- let f = readBits32BE(bitfield);
- console.log(header.bitfield_size);
- console.log(' ----- ');
- /*
- console.log();
- console.log('8 Bytes Low to High');
- decompress(file, tmp, a, header)
- console.log();
- console.log('16 Bytes Low to High');
- decompress(file, tmp, b, header)
- console.log();
- console.log('32 Bytes Low to High');
- decompress(file, tmp, c, header)
- console.log();
- console.log('8 Bytes high to low');
- decompress(file, tmp, d, header)
- console.log();
- console.log('16 Bytes high to low');
- decompress(file, tmp, e, header)
- */
- console.log();
- console.log('32 Bytes high to low');
- decompress(file, tmp, f, header)
- break;
- }
- });
- function readBits8LE(buffer) {
- let bits = [];
- for(let ofs = 0; ofs < buffer.length; ofs += 1) {
- let val = buffer.readUInt8(ofs);
- for(let i = 0; i < 8; i++) {
- let bit = 1 << i;
- bits.push(val & bit ? 1 : 0);
- }
- }
- return bits;
- }
- function readBits16LE(buffer) {
- let bits = [];
- for(let ofs = 0; ofs < buffer.length; ofs += 2) {
- let val = buffer.readUInt16LE(ofs);
- for(let i = 0; i < 16; i++) {
- let bit = 1 << i;
- bits.push(val & bit ? 1 : 0);
- }
- }
- return bits;
- }
- function readBits32LE(buffer) {
- let bits = [];
- for(let ofs = 0; ofs < buffer.length; ofs += 4) {
- let val = buffer.readUInt32LE(ofs);
- for(let i = 0; i < 32; i++) {
- let bit = 1 << i;
- bits.push(val & bit ? 1 : 0);
- }
- }
- return bits;
- }
- function readBits8BE(buffer) {
- let bits = [];
- for(let ofs = 0; ofs < buffer.length; ofs += 1) {
- let val = buffer.readUInt8(ofs);
- for(let i = 7; i >= 0; i--) {
- let bit = 1 << i;
- bits.push(val & bit ? 1 : 0);
- }
- }
- return bits;
- }
- function readBits16BE(buffer) {
- let bits = [];
- for(let ofs = 0; ofs < buffer.length; ofs += 2) {
- let val = buffer.readUInt16LE(ofs);
- for(let i = 15; i >= 0; i--) {
- let bit = 1 << i;
- bits.push(val & bit ? 1 : 0);
- }
- }
- return bits;
- }
- function readBits32BE(buffer) {
- let bits = [];
- for(let ofs = 0; ofs < buffer.length; ofs += 4) {
- let val = buffer.readUInt32LE(ofs);
- for(let i = 31; i >= 0; i--) {
- let bit = 1 << i;
- bits.push(val & bit ? 1 : 0);
- }
- }
- return bits;
- }
- function decompress(file, ofs, bits, header) {
- let out_ofs = 0;
- let window_ofs = 0;
- let buffer = Buffer.alloc(header.decompressed_size);
- console.log('Starting decompression at: 0x%s', ofs.toString(16));
- for(let i = 0; i < bits.length; i++) {
- let bit = bits[i];
- let word = file.readUInt16LE(ofs);
- ofs += 2;
- if(bit === 0) {
- buffer.writeUInt16LE(word, out_ofs);
- out_ofs += 2;
- } else if(word === 0xffff) {
- window_ofs += 0x2000;
- } else {
- let val = ((word >> 3) & 0x1fff);
- let copy_from = window_ofs + val;
- let copy_len = (word & 0x07) + 2;
- while(copy_len--) {
- let w = buffer.readUInt16LE(copy_from);
- copy_from += 2;
- buffer.writeUInt16LE(w, out_ofs);
- out_ofs += 2;
- }
- }
- if(window_ofs > buffer.length) {
- break;
- }
- }
- return buffer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement