xash93d/src/ModPackage.js
2022-10-28 02:48:42 -06:00

74 lines
1.8 KiB
JavaScript
Executable File

const { gzip } = require('./util/gzip')
// I don't like using a loader in this way but I
// guess this is punishment for putting off
// putting AsarHandler in it's own project
// and finishing FakeBuffer
const { AsarHandler: Asar, Buffer } = le._apps.abnt
function toArrayBuffer(buffer) {
let ab = new ArrayBuffer(buffer.length),
view = new Uint8Array(ab)
for (let i = 0; i < buffer.length; ++i) {
view[i] = buffer[i]
}
return ab
}
const textDecoder = new TextDecoder('utf-8')
class ModPackage {
static async unpack(buffer, direct = false) {
let asar = new Asar(buffer),
manifestString = textDecoder.decode( asar.get('manifest.json') ),
files = [ ...asar.contents ]
const decompress = path => new Promise((resolve, reject) => {
// gzip(asar.get(path), (error, data) => {
// console.log('GZIP: ', path, data)
// if(error)
// reject(error)
// else
// resolve([ path, toArrayBuffer(data) ])
// })
resolve([ path, asar.get(path) ])
})
files.splice(files.indexOf('manifest.json'), 1)
files = files.map(decompress)
files = await Promise.all(files)
return direct ? { files, manifestString } : new this(new Map(files), manifestString)
}
constructor(files, manifestString) {
this.manifest = JSON.parse(manifestString)
this.files = files
this.cache = new Map()
}
get(path) {
if(this.cache.has(path))
return this.cache.get(path)
let data = this.files.get(path),
ext = $fs.utils.getExt(path),
file = new Blob([ data ], { type: le._get.ext.mime[ext] })
this.cache.set(path, file)
return file
}
getURL(path) {
let file = this.get(path),
url = URL.createObjectURL(file)
return url
}
}
module.exports = ModPackage