Created
January 14, 2017 00:58
-
-
Save juliandescottes/12e067b9c4783aa47d1052d10361a7d2 to your computer and use it in GitHub Desktop.
Recovery code for piskel files with weird encoding issues.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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