From 8e56b9eee7e5d32824c950c7c1b496a93bbb91be Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 9 Sep 2013 10:17:44 +0000 Subject: [PATCH] --- js/libs/gal/gal.filesystem.js | 42 ++++++++++++++++++++++++---------- js/libs/gal/gal.js | 43 +++++++++++++++++++++++++++++++---- js/main.js | 4 +++- 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/js/libs/gal/gal.filesystem.js b/js/libs/gal/gal.filesystem.js index 70f061db..2685572c 100644 --- a/js/libs/gal/gal.filesystem.js +++ b/js/libs/gal/gal.filesystem.js @@ -21,7 +21,7 @@ */ (function(gal) { - var ROOT_DIR = 'gal'; + var ROOT_DIR; var DEFAULT_QUOTA = 1024 * 1024 * 100; function onError(error) { @@ -142,7 +142,14 @@ * initialized. * @param {string} opt_quota The quota (in bytes) to request (optional). */ - GALFS.prototype.init = function(callback, opt_quota) { + GALFS.prototype.init = function(fsprefix, callback, opt_quota) { + if (fsprefix == undefined) { + fsprefix = ''; + } + if (fsprefix != '') { + fsprefix += '/'; + } + ROOT_DIR = fsprefix + "gal"; // requestFileSystem and storageInfo shims var requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; @@ -155,11 +162,15 @@ var onInitFs = function(fs) { that.fs = fs; - // Create a directory for the root of the GAL - fs.root.getDirectory(ROOT_DIR, {create: true}, function(dirEntry) { - that.root = dirEntry; - callback(); - }, onError); + createDir_(fs.root, ROOT_DIR.split('/'), function(res) { + // Create a directory for the root of the GAL + fs.root.getDirectory(ROOT_DIR, {create: true}, function(dirEntry) { + that.root = dirEntry; + callback(); + }, onError); + }); + + }; // Callback when the filesystem API has granted quota @@ -237,17 +248,24 @@ /** * Clears everything out of the root directory. Mostly for unit testing. */ - GALFS.prototype.clear = function() { + GALFS.prototype.clear = function(cb) { // Remove the root directory this.root.removeRecursively(function() { }, onError); var that = this; - this.lookupTable = {}; // And then recreate it - this.fs.root.getDirectory(ROOT_DIR, {create: true}, function(dirEntry) { - that.root = dirEntry; - }, onError); + fs = this.fs; + + createDir_(fs.root, ROOT_DIR.split('/'), function(res) { + // Create a directory for the root of the GAL + fs.root.getDirectory(ROOT_DIR, {create: true}, function(dirEntry) { + that.root = dirEntry; + callback(); + }, onError); + }); + + cb(); }; if (!gal) { diff --git a/js/libs/gal/gal.js b/js/libs/gal/gal.js index 634d1810..4848f28c 100644 --- a/js/libs/gal/gal.js +++ b/js/libs/gal/gal.js @@ -25,7 +25,12 @@ * @constructor * @param {Object} manifest URL to manifest file. */ - var GAL = function(manifest) { + var GAL = function(manifest, fsprefix) { + if (fsprefix == undefined) { + fsprefix = ''; + } + + this._fsprefix = fsprefix; this._manifest = manifest; // Dictionary of arrays of all of the bundles contained in the manifest. this.bundles = {}; @@ -35,6 +40,7 @@ this.loaded = {}; this.progress = {}; this.error = {}; + this.adapter = null; }; /** @@ -204,11 +210,26 @@ return this.adapter.root.toURL(); }; + GAL.prototype.initAdapter = function(callback) { + if (null == this.adapter) { + initAdapter_(callback); + } else { + callback(this.adapter); + } + }; GAL.prototype.downloadAll = function() { - console.log('downloadAll'); downloadAll_.call(this); }; + GAL.prototype.clear = function(callback) { + var $this = this; + + this.initAdapter( + function(adapter) { + adapter.clear(callback); + } + ); + }; /** * @private @@ -216,9 +237,16 @@ * @param {function} callback Called when the adapter has initialized. */ function initAdapter_(callback) { + var $this = this; + + if (this.adapter != null) { + callback(this.adapter); + return; + } + this.adapter = new GAL.adapterClass(); - this.adapter.init(function() { - callback(); + this.adapter.init(this._fsprefix, function() { + callback($this.adapter); }); } @@ -248,6 +276,7 @@ initAdapter_.call(context, function() { setManifest_.call(context, manifest); // Optionally, start auto-download. + console.log('autodownload ' + manifest.autoDownload); if (manifest.autoDownload && context.online()) { downloadAll_.call(context); } @@ -301,7 +330,7 @@ } /** - * + * @private * Starts downloading all of the assets in the manifest, in order. */ function downloadAll_() { @@ -309,12 +338,16 @@ var that = this; // Start by downloading the first bundle, then download subsequent ones. (function loop(bundleIndex) { + console.log(bundleIndex); if (bundleIndex == that.bundleOrder.length) { // We're done downloading stuff! + console.log('ok !'); return; } + var bundleName = that.bundleOrder[bundleIndex]; + console.log('loading ' + bundleName); that.onLoaded(bundleName, function() { // Once bundle is loaded, load the next bundle. loop(bundleIndex + 1); diff --git a/js/main.js b/js/main.js index 1bab2dc1..2ab62ac0 100644 --- a/js/main.js +++ b/js/main.js @@ -52,10 +52,12 @@ function onAppResume() { } function onDeviceReady() { + var manifest = window.sessionStorage.getItem('manifest.' + DATAS.id); + var fsprefix = window.sessionStorage.getItem('galfsprefix'); if (navigator.onLine && manifest) { gal = new GameAssetLoader(JSON.parse(manifest)); - gal.init(function() { + gal.init(fsprefix, function() { startAfterLoading = true; init(); }); -- 2.39.5