"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pakVerifier = void 0;
const crypto_1 = __importDefault(require("crypto"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
class pakVerifier {
    logger;
    constructor(logger) {
        this.logger = logger;
    }
    verifyPak(pakFile, filePath) {
        let parse = path_1.default.parse(filePath);
        let find_footer = pakFile.pak.data.indexOf(Buffer.from('MLPublish.......'));
        if (find_footer > -1) {
            let d = pakFile.pak.data.slice(0x0, find_footer);
            let hash = crypto_1.default
                .createHash('sha512')
                .update(d)
                .digest('hex');
            if (hash !== pakFile.pak.footer._hash) {
                this.logger.error('Pak file ' + parse.name + ' is corrupt.');
                return false;
            }
        }
        else {
            if (pakFile.pak.data.readUInt8(0x0f) > 0x02) {
                this.logger.error('Pak file ' + parse.name + ' is corrupt.');
                return false;
            }
            this.logger.error('Pak file ' +
                parse.name +
                ' is using an outdated version of the pak format. Tell the author it needs updating.');
        }
        return true;
    }
    extractPakToTemp(pakFile, filePath) {
        let ndir = fs_1.default.mkdtempSync('ModLoader64_temp_');
        pakFile.extractAll(ndir);
        let d = "";
        fs_1.default.readdirSync(ndir).forEach((dir) => {
            d = dir;
        });
        return path_1.default.join(ndir, d);
    }
    extractPakFileToTarget(pakFile, filePath, dest) {
        if (!fs_1.default.existsSync(dest)) {
            fs_1.default.mkdirSync(dest);
        }
        pakFile.extractAll(dest);
        let d = "";
        fs_1.default.readdirSync(dest).forEach((dir) => {
            d = dir;
        });
        return path_1.default.join(dest, d);
    }
}
exports.pakVerifier = pakVerifier;
//# sourceMappingURL=pakVerifier.js.map