ffpr
Version:
Save editor for games of Final Fantasy Pixel Remaster
64 lines (63 loc) • 2.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.save = exports.load = exports.pack = exports.unpack = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const types_1 = require("./types");
const serialization_1 = require("./serialization");
const zip_1 = require("./zip");
const crypt_1 = require("./crypt");
async function unpack(source, type = 'Switch') {
if (type == 'PlayStation') {
const data = await (0, serialization_1.deserialize)(source.toString('utf8'));
return data;
}
if (type == 'PC') {
// remove UTF-8 BOM
let trimmed = source;
if (source[0] == 239 && source[1] == 187 && source[2] == 191) {
trimmed = trimmed.slice(3);
}
trimmed = Buffer.concat([trimmed, Buffer.alloc((4 - source.length % 4) % 4, '=')]);
source = Buffer.from(trimmed.toString('utf8'), 'base64');
}
const decrypted = await (0, crypt_1.decrypt)(source);
const decompressed = await (0, zip_1.decompress)(decrypted);
const data = await (0, serialization_1.deserialize)(decompressed.toString('utf8'));
return data;
}
exports.unpack = unpack;
async function pack(data, type = 'Switch') {
const serialized = Buffer.from(await (0, serialization_1.serialize)(data), 'utf8');
if (type == 'PlayStation') {
return serialized;
}
const compressed = await (0, zip_1.compress)(serialized);
const encrypted = await (0, crypt_1.encrypt)(compressed);
if (type == 'Switch') {
return encrypted;
}
const based = encrypted.toString('base64');
return Buffer.from(based);
}
exports.pack = pack;
async function load(filename, type = 'Switch') {
const source = fs_1.default.readFileSync(path_1.default.resolve(filename));
const data = await unpack(source, type);
return data;
}
exports.load = load;
async function save(filename, data, type = 'Switch') {
const content = await pack(data, type);
fs_1.default.writeFileSync(path_1.default.resolve(filename), content);
}
exports.save = save;
exports.default = {
pack,
unpack,
load,
save: types_1.save,
};