Permalink
Browse files

Handle savefiles in library.load()

  • Loading branch information...
curiousdannii committed Apr 7, 2012
1 parent d96220f commit 3763343836103b2683d3b0327aee07bcf017d483
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,7 +1,7 @@
CACHE MANIFEST
# Parchment offline cache manifest
-# 2012-02-18
+# 2012-03-03
index.html
lib/glkote.min.js
@@ -415,7 +415,7 @@ RoutineContext = Context.subClass({
// Easily build a new opcode from a class
opcode_builder = function( Class, func, flags )
{
- var flags = flags || {};
+ flags = flags || {};
if ( func )
{
/*if ( func.pop )
@@ -347,7 +347,6 @@ Text = Object.subClass({
addr_start = addr,
dict = {},
- seperators_len,
entry_len,
endaddr,
anentry,
@@ -317,7 +317,7 @@ var ZVM_core = {
// Return control to the ZVM runner to perform some action
act: function( code, options )
{
- var options = options || {};
+ options = options || {};
// Flush the buffer
this.ui.flush();
View
@@ -127,12 +127,19 @@ parchment = window.parchment = {
},
// Load a VM's dependent files - attached by vms.add()
-load_vm = function()
+load_vm = function( callback )
{
+ // We've loaded this VM before, so run the callback immediately
+ if ( this.loaded )
+ {
+ return callback( this );
+ }
+
+ this.loaded = 1;
+
var self = this,
i = 0,
dependency,
- deferred,
// Ensure that the files are loaded in the correct order (Debug only)
/* DEBUG */
@@ -160,15 +167,6 @@ load_vm = function()
scripts = [];
/* ENDDEBUG */
- // We've loaded this VM before, so return the Deferred
- if ( this.loaded )
- {
- return this.loaded;
- }
-
- // This is our first time, so make a Deferred
- deferred = this.loaded = $.Deferred();
-
// Load all the dependencies
/* DEBUG */
script_callback();
@@ -191,13 +189,17 @@ load_vm = function()
// Use jQuery.when() to get a promise for all of the scripts
$.when.apply( this, scripts )
- // When all the scripts are loaded, then resolve our deferred with this vm
- .done( function(){ deferred.resolve( self ); } );
+ // When all the scripts are loaded, then run the callback function with this vm
+ .done( function(){ callback( self ); } );
//.fail( scripts_fail );
-
- return deferred;
};
+// Callback to show an error if a VM's dependant scripts could be successfully loaded
+// Currently not usable as errors are not detected :(
+/*scripts_fail = function(){
+ throw new FatalError( 'Parchment could not load everything it needed to run this story. Check your connection and try refreshing the page.' );
+};*/
+
// VM helper functions - here is as good a place as any to define them
extend( parchment.vms, {
add: function( defn )
View
@@ -22,6 +22,14 @@ TODO:
var Glkfile = Model.subClass( 'Glkfile', {
// modified
// name
+
+ // Get the savefile from storage
+ load: function( callback )
+ {
+ // Fetch the data if we can
+ this.data( function( data ) { callback( data ); } );
+ },
+
}),
// Storyfile
@@ -33,8 +41,11 @@ Story = Model.subClass( 'Story', {
// url
// Load this story and its VM
- launch: function( vm )
+ launch: function( vm, savefile )
{
+ var self = this,
+ flow = Flow();
+
// Get a VM if we can
vm = parchment.vms.match( vm || this.vm, this.url );
@@ -44,29 +55,38 @@ Story = Model.subClass( 'Story', {
return ui.error( 'Story type is not supported', 'Unfortunately Parchment can\'t run this story. Please try a desktop interpreter instead.' );
}
- // Get a promise which will resolve when the story and VM are both finished loading
- $.when( this.load(), vm.load() )
- // And then launch the VM!
- .done( launch_callback );
+ // Load the story and VM, and then finally launch it
+ flow
+ .par( function( next ) { self.load( next ); } )
+ .par( function( next ) { vm.load( next ); } );
+ if ( savefile )
+ {
+ flow.par( function( next ) { savefile.load( next ); } );
+ }
+ flow.seq( function( next, results )
+ {
+ launch_callback.apply( self, results );
- // Update some stats
- this.set( 'lastplay', ( new Date() ).getTime() );
- this.set( 'playcount', ( this.playcount || 0 ) + 1 );
+ // Update some stats
+ self.set({
+ lastplay: ( new Date() ).getTime(),
+ playcount: ( self.playcount || 0 ) + 1
+ });
+ });
},
// Get the story file from storage, or download it
- load: function()
+ load: function( callback )
{
- var self = this,
- _deferred = self._deferred = $.Deferred();
+ var self = this;
// Fetch the data if we can
this.data( function( data ) {
// We have the data, so resolve the Deferred
if ( data )
{
// Fake an XHR, all we access are these properties
- _deferred.resolve({
+ callback({
responseText: data,
responseArray: self._array || ( self._array = file.text_to_array( data ) )
});
@@ -75,15 +95,13 @@ Story = Model.subClass( 'Story', {
// Otherwise download the file
else
{
- self.download();
+ self.download( callback );
}
});
-
- return _deferred;
},
// Download the file
- download: function()
+ download: function( callback )
{
// Re-show the indicator if that's needed
if ( !ui.modal )
@@ -97,7 +115,7 @@ Story = Model.subClass( 'Story', {
.done( function( data, textStatus, jqXHR )
{
// Resolve our deferred with the XHR
- self._deferred.resolve( jqXHR );
+ callback( jqXHR );
// Save the data to storage
self.data( jqXHR.responseText );
self._array = jqXHR.responseArray;
@@ -107,14 +125,14 @@ Story = Model.subClass( 'Story', {
ui.error(
'Parchment could not load the story',
'Please check your connection, and that the URL is correct',
- 'Retry', function() { self.download(); }
+ 'Retry', function() { self.download( callback ); }
);
});
}
}),
-// Launcher. Will be run by jQuery.when(). jqXHR is args[2]
-launch_callback = function( storydata, vm )
+// Launcher. Will be run by jQuery.when().
+launch_callback = function( storydata, vm, savefile )
{
// Hide the load indicator
ui.endmodal();
@@ -125,8 +143,6 @@ launch_callback = function( storydata, vm )
vm.engine
);
- var savefile = location.hash;
-
// Add the callback
runner.toParchment = function( event ) { library.fromRunner( runner, event ); };
@@ -137,11 +153,11 @@ launch_callback = function( storydata, vm )
});
// Restore if we have a savefile
- if ( savefile && savefile != '#' ) // IE will set location.hash for an empty fragment, FF won't
+ if ( savefile )
{
runner.fromParchment({
code: 'restore',
- data: file.base64_decode( savefile.slice( 1 ) )
+ data: file.base64_decode( savefile )
});
}
// Restart if we don't
@@ -151,12 +167,6 @@ launch_callback = function( storydata, vm )
}
},
-// Callback to show an error if a VM's dependant scripts could be successfully loaded
-// Currently not usable as errors are not detected :(
-/*scripts_fail = function(){
- throw new FatalError( 'Parchment could not load everything it needed to run this story. Check your connection and try refreshing the page.' );
-};*/
-
// A blorbed story file
Blorb = IFF.subClass({
// Parse a zblorb or naked zcode story file
@@ -299,6 +309,11 @@ Library = Object.subClass({
load: function()
{
var vm = urloptions.vm,
+ Savefiles = this.Glkfiles,
+ savefile,
+ savefiledata = location.hash || '#', // Set this now to simplify the check below - Now we'll either have data or '#'
+ i = 0,
+ flow = Flow(),
// Get the requested story, if there is one
story = this.get_story();
@@ -326,10 +341,38 @@ Library = Object.subClass({
story.set( 'vm', vm );
}
+ // Check for a url hash savefile
+ if ( savefiledata != '#' )
+ {
+ savefiledata = file.base64_decode( savefiledata.slice( 1 ) );
+
+ // Check if we've already got this savefile
+ Savefiles = Savefiles.find( '_length', savefiledata.length );
+ if ( Savefiles.length )
+ {
+ while ( i < Savefiles.length )
+ {
+ flow.par( function( callback )
+ {
+
+ }
+ }
+ }
+ // Otherwise add a new savefile
+ else
+ {
+ story.Glkfile.add( savefile = new Glkfile({
+ modified: ( new Date() ).getTime(),
+ name: 'Bookmark',
+ _data: savefiledata
+ }) );
+ }
+ }
+
// Launch the story
try
{
- story.launch( vm );
+ story.launch( vm, savefile );
}
catch (e)
{
@@ -403,6 +446,10 @@ Library = Object.subClass({
new Dialog({
title: 'Save',
content: [
+ {
+ type: 'input',
+ label: 'Save name'
+ },
{
type: 'actions',
labels: ['Save', 'Cancel']
@@ -412,7 +459,7 @@ Library = Object.subClass({
text: 'Bookmark',
href: '#' + file.base64_encode( event.data ),
target: '_blank',
- // Stop the cancel putton from saying we didn't save
+ // Stop the cancel button from saying we didn't save
click: function() { $( '.modal input[value=Cancel]' ).off( 'click' ); }
}
],
View
@@ -38,6 +38,12 @@ var Model = Object.subClass({
}
this._id = this._id || Model.id++
+
+ // Save data too
+ if ( this._data )
+ {
+ this.data( this._data );
+ }
},
// Save an instance to localStorage
@@ -51,8 +57,8 @@ var Model = Object.subClass({
for ( itemid in this )
{
- // Don't save undefined props, _ props, Collections or functions!
- if ( this[itemid] != undefined && itemid.charAt(0) != '_' && !Model.models[itemid] && typeof this[itemid] != 'function' )
+ // Don't save undefined props, _ props (excepting _length), Collections or functions!
+ if ( this[itemid] != undefined && !/^_(?!l)/.test( itemid ) && !Model.models[itemid] && typeof this[itemid] != 'function' )
{
data[itemid] = this[itemid];
}
@@ -80,7 +86,14 @@ var Model = Object.subClass({
// Set a property - please run this so that we will stay up to date
set: function( prop, value )
{
- this[prop] = value;
+ if ( typeof prop == 'string' )
+ {
+ this[prop] = value;
+ }
+ else
+ {
+ extend( this, prop );
+ }
this.save();
},
@@ -108,6 +121,7 @@ var Model = Object.subClass({
{
this._data = data;
storage.set( datastring, data );
+ this.set( '_length', data.length );
}
}
}),
@@ -224,5 +238,5 @@ extend( Model, {
models: {},
// Default ID
- id: ( new Date() ).getTime(),
+ id: ( new Date() ).getTime()
});
View
@@ -15,12 +15,15 @@ http://code.google.com/p/parchment
TODO:
Flash support: default for LOCAL, because loading extra files is no harm
Offer to Undum developer when complete
+ Auto convert localStorage to IndexedDB
*/
var storage_factory = (function(){
-var indexedDB = !LOCAL && ( window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB ),
+// For file:, no built in storage support :(
+var LOCAL = location.protocol == 'file:',
+indexedDB = !LOCAL && ( window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB ),
localStorage = !LOCAL && window.localStorage,
// IndexedDB class
Oops, something went wrong.

0 comments on commit 3763343

Please sign in to comment.