Skip to content

Instantly share code, notes, and snippets.

@juliandescottes
Created January 14, 2017 00:58
Show Gist options
  • Save juliandescottes/12e067b9c4783aa47d1052d10361a7d2 to your computer and use it in GitHub Desktop.
Save juliandescottes/12e067b9c4783aa47d1052d10361a7d2 to your computer and use it in GitHub Desktop.
Recovery code for piskel files with weird encoding issues.
decodePiskelFile : function (rawPiskel, onSuccess, onError) {
try {
var serializedPiskel = JSON.parse(rawPiskel);
var piskel = serializedPiskel.piskel;
pskl.utils.serialization.Deserializer.deserialize(serializedPiskel, function (piskel) {
onSuccess(piskel);
});
} catch (e) {
var layers = [];
var isInLayer = false;
for (var i = 0 ; i < rawPiskel.length ; i++) {
var c = rawPiskel.charCodeAt(i);
if (isInLayer) {
if (c > 30) {
layers[layers.length - 1].push(c);
} else {
isInLayer = false;
}
} else {
var isValidSequence = [i, i+1, i+2, i+3, i+4].every(function (_i) {
return rawPiskel.charCodeAt(_i) > 30;
});
if (isValidSequence) {
isInLayer = true;
layers.push([c]);
}
}
}
console.log(layers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment