rollback to modelv2 for all serializations except history service

This commit is contained in:
Julian Descottes 2016-10-16 00:22:06 +02:00
parent 0586756b92
commit 2a7957bce2
8 changed files with 56 additions and 53 deletions

View File

@ -34,7 +34,7 @@
// Do not save an unchanged piskel
if (hash !== this.lastHash) {
this.lastHash = hash;
var serializedPiskel = pskl.utils.convertBufferToString(this.piskelController.serialize());
var serializedPiskel = pskl.utils.StringSerializer.serializePiskel(piskel);
this.savePiskel_('next', serializedPiskel, JSON.stringify(info));
}
};
@ -51,7 +51,7 @@
var previousPiskel = window.localStorage.getItem('bkp.prev.piskel');
var previousInfo = window.localStorage.getItem('bkp.prev.info');
previousPiskel = pskl.utils.convertStringToBuffer(previousPiskel);
previousPiskel = JSON.parse(previousPiskel);
previousInfo = JSON.parse(previousInfo);
pskl.utils.serialization.Deserializer.deserialize(previousPiskel, function (piskel) {

View File

@ -24,7 +24,7 @@
return Q.reject('Invalid file name');
}
var serialized = pskl.utils.Serializer.serializePiskel(piskel);
var serialized = pskl.utils.StringSerializer.serializePiskel(piskel);
savePath = this.addExtensionIfNeeded_(savePath);
piskel.savePath = savePath;
piskel.setName(this.extractFilename_(savePath));

View File

@ -5,19 +5,21 @@
ns.FileDownloadStorageService.prototype.init = function () {};
ns.FileDownloadStorageService.prototype.save = function (piskel) {
var serialized = pskl.utils.Serializer.serializePiskel(piskel);
var serialized = pskl.utils.StringSerializer.serializePiskel(piskel, false);
var deferred = Q.defer();
var piskelName = piskel.getDescriptor().name;
var timestamp = pskl.utils.DateUtils.format(new Date(), '{{Y}}{{M}}{{D}}-{{H}}{{m}}{{s}}');
var fileName = piskelName + '-' + timestamp + '.piskel';
pskl.utils.BlobUtils.stringToBlob(serialized, function(blob) {
var piskelName = piskel.getDescriptor().name;
var timestamp = pskl.utils.DateUtils.format(new Date(), '{{Y}}{{M}}{{D}}-{{H}}{{m}}{{s}}');
var fileName = piskelName + '-' + timestamp + '.piskel';
try {
pskl.utils.FileUtils.downloadAsFile(new Blob([serialized], {type : 'application/piskel+json'}), fileName);
deferred.resolve();
} catch (e) {
deferred.reject(e.message);
}
try {
pskl.utils.FileUtils.downloadAsFile(blob, fileName);
deferred.resolve();
} catch (e) {
deferred.reject(e.message);
}
}.bind(this), 'application/piskel+json');
return deferred.promise;
};

View File

@ -11,8 +11,10 @@
var descriptor = piskel.getDescriptor();
var deferred = Q.defer();
var serialized = pskl.utils.StringSerializer.serializePiskel(piskel);
var data = {
framesheet : '[' + new Uint8Array(this.piskelController.serialize()) + ']',
framesheet : serialized,
fps : this.piskelController.getFPS(),
name : descriptor.name,
description : descriptor.description,

View File

@ -13,10 +13,8 @@
ns.LocalStorageService.prototype.save = function(piskel) {
var name = piskel.getDescriptor().name;
var description = piskel.getDescriptor().description;
var serialized = pskl.utils.Serializer.serializePiskel(piskel);
var serializedString = pskl.utils.convertBufferToString(serialized);
var serialized = pskl.utils.StringSerializer.serializePiskel(piskel);
if (pskl.app.localStorageService.getPiskel(name)) {
var confirmOverwrite = window.confirm('There is already a piskel saved as ' + name + '. Overwrite ?');
if (!confirmOverwrite) {
@ -27,7 +25,7 @@
try {
this.removeFromKeys_(name);
this.addToKeys_(name, description, Date.now());
window.localStorage.setItem('piskel.' + name, serializedString);
window.localStorage.setItem('piskel.' + name, serialized);
return Q.resolve();
} catch (e) {
return Q.reject(e.message);
@ -38,9 +36,7 @@
var piskelString = this.getPiskel(name);
var key = this.getKey_(name);
var serializedPiskel = pskl.utils.convertStringToBuffer(piskelString);
pskl.utils.serialization.Deserializer.deserialize(serializedPiskel, function (piskel, extra) {
pskl.utils.serialization.Deserializer.deserialize(JSON.parse(piskelString), function (piskel, extra) {
pskl.app.piskelController.setPiskel(piskel);
pskl.app.previewController.setFPS(extra.fps);
});

View File

@ -253,36 +253,4 @@ if (!Uint32Array.prototype.fill) {
return string;
};
ns.convertBufferToString = function (inputBuffer) {
if (typeof inputBuffer.buffer !== 'undefined') {
inputBuffer = inputBuffer.buffer;
}
var buffer;
if (inputBuffer.byteLength % 2 === 1) {
buffer = ArrayBuffer.transfer(inputBuffer, inputBuffer.byteLength + 1);
} else {
buffer = inputBuffer;
}
var string = '';
var uint16 = new Uint16Array(buffer);
for (var i = 0, length = uint16.length; i < length; i++) {
string += String.fromCharCode(uint16[i]);
}
return string;
};
ns.convertStringToBuffer = function(inputString) {
var uint16 = new Uint16Array(inputString.length);
for (var i = 0, length = inputString.length; i < length; i++) {
uint16[i] = inputString.charCodeAt(i);
}
return uint16.buffer;
};
})();

View File

@ -0,0 +1,34 @@
(function () {
var ns = $.namespace('pskl.utils');
ns.StringSerializer = {
serializePiskel : function (piskel) {
var serializedLayers = piskel.getLayers().map(function (l) {
return pskl.utils.StringSerializer.serializeLayer(l);
});
return JSON.stringify({
modelVersion : 2,
piskel : {
name : piskel.getDescriptor().name,
description : piskel.getDescriptor().description,
fps : pskl.app.piskelController.getFPS(),
height : piskel.getHeight(),
width : piskel.getWidth(),
layers : serializedLayers
}
});
},
serializeLayer : function (layer) {
var frames = layer.getFrames();
var layerToSerialize = {
name : layer.getName(),
opacity : layer.getOpacity(),
frameCount : frames.length
};
var renderer = new pskl.rendering.FramesheetRenderer(frames);
layerToSerialize.base64PNG = renderer.renderAsCanvas().toDataURL();
return JSON.stringify(layerToSerialize);
}
};
})();

View File

@ -40,6 +40,7 @@
"js/utils/WorkerUtils.js",
"js/utils/Xhr.js",
"js/utils/serialization/Serializer.js",
"js/utils/serialization/StringSerializer.js",
"js/utils/serialization/Deserializer.js",
"js/utils/serialization/backward/Deserializer_v0.js",
"js/utils/serialization/backward/Deserializer_v1.js",