ffpr
Version:
Save editor for games of Final Fantasy Pixel Remaster
86 lines (85 loc) • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserialize = exports.serialize = void 0;
const doNotSerializeKeys = [
'.userData.ownedTransportationList.target[].position',
'.userData.cheatSettingsData',
'.mapData.playerEntity.position',
];
async function iterSerialize(data, rootKey) {
if (rootKey == '.pictureData') {
if (typeof data == 'string') {
return data;
}
if (Buffer.isBuffer(data)) {
return data.toString('base64');
}
if (data instanceof Uint8Array) {
return Buffer.from(data).toString('base64');
}
throw new Error("serialize: Invalid picture data");
}
if (Buffer.isBuffer(data)) {
return data.toString('base64');
}
if (!(typeof data == 'object') || doNotSerializeKeys.includes(rootKey) || data == null) {
return data;
}
data = structuredClone(data);
if (Array.isArray(data)) {
await Promise.all(data.map(async (v, i, a) => {
const newRootKey = rootKey + '[]';
a[i] = await iterSerialize(v, newRootKey);
}));
return data;
}
for (let key in data) {
const newRootKey = rootKey + '.' + key;
data[key] = await iterSerialize(data[key], newRootKey);
}
return JSON.stringify(data);
}
async function serialize(data) {
return iterSerialize(data, '');
}
exports.serialize = serialize;
async function iterDeserialize(input, rootKey) {
try {
if (rootKey == '.pictureData' && typeof input == 'string') {
const picture = Buffer.from(input, 'base64');
return picture;
}
if (Buffer.isBuffer(input)) {
const data = JSON.parse(input.toString('utf8'));
return iterDeserialize(data, rootKey);
}
if (typeof input == 'string') {
const data = JSON.parse(input);
return iterDeserialize(data, rootKey);
}
if (typeof input == 'object' && input != null) {
const data = structuredClone(input);
if (Array.isArray(data)) {
await Promise.all(data.map(async (v, i, a) => a[i] = await iterDeserialize(v, rootKey + '[]')));
}
else {
for (let key in data) {
data[key] = await iterDeserialize(data[key], rootKey + '.' + key);
}
}
return data;
}
return input;
}
catch (e) {
// if the string is not JSON
if (typeof input == 'string') {
return input;
}
throw e;
}
}
async function deserialize(input) {
return iterDeserialize(input, '');
}
exports.deserialize = deserialize;