Created
November 16, 2016 10:47
-
-
Save cevek/2c73a16d6b3f2fb0d91750de6a4b19ab to your computer and use it in GitHub Desktop.
pak file format read/write
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const fs = require('fs'); | |
console.log(process.argv); | |
const version = 4; | |
const file = process.argv[2]; | |
const data = fs.readFileSync(file); | |
const count = data.readUInt32LE(4); | |
let start = 9; | |
const resources = {}; | |
const poses = {}; | |
const positions = [] | |
for (var i = 0; i < count + 1; i++) { | |
const id = data.readUInt16LE(start); | |
const pos = data.readUInt32LE(start + 2); | |
start += 6; | |
// console.log(id, pos); | |
positions.push({id, pos}); | |
} | |
for (var i = 0; i < count; i++) { | |
const id = positions[i].id; | |
const pos = positions[i].pos; | |
const toPos = positions[i + 1].pos; | |
resources[id] = data.slice(pos, toPos); | |
poses[id] = {start: pos, end: toPos}; | |
const str = data.toString('utf8', pos, toPos) | |
if (str.indexOf('WebInspector.Revealer.reveal=function(revealable,omitFocus)\n{') > -1) { | |
replace(alert); | |
console.log(id); | |
} | |
// console.log(id, Buffer.byteLength(resources[id]), toPos - pos); | |
// console.assert(Buffer.byteLength(resources[id]) == toPos - pos, id); | |
} | |
Buffer.compare(data, gen(resources)); | |
// gen({1: 'Yeah'}); | |
// console.log(resources[21451].toString()); | |
function gen(resources) { | |
const keys = Object.keys(resources); | |
// const len = keys.reduce((sum, id) => sum + poses[id].end - poses[id].start, (keys.length + 1) * 6 + 9); | |
const len = keys.reduce((sum, id) => sum + Buffer.byteLength(resources[id], 'utf8'), (keys.length + 1) * 6 + 9); | |
const buffer = new Buffer(len); | |
buffer.writeUInt32LE(0); // version | |
buffer.writeUInt32LE(4, keys.length); // count | |
buffer.writeUInt8(8, 1); // encoding | |
let pos = 9; | |
let offset = pos + (keys.length + 1) * 6; | |
for (var i = 0; i < keys.length; i++) { | |
var id = keys[i]; | |
buffer.writeUInt16LE(id, pos); | |
buffer.writeUInt32LE(offset, pos); | |
buffer.fill(resources[id], offset); | |
offset += Buffer.byteLength(resources[id]); | |
pos += 6; | |
} | |
buffer.writeUInt16LE(0, pos); | |
buffer.writeUInt32LE(buffer.length, pos + 2); | |
return buffer; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment