1 /** 2 * The Render Engine 3 * RemoteLoader 4 * 5 * @fileoverview A resource loader for handling remote objects located on 6 * the server. 7 * 8 * @author: Brett Fattori (brettf@renderengine.com) 9 * @author: $Author: bfattori $ 10 * @version: $Revision: 1555 $ 11 * 12 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 * 32 */ 33 34 // The class this file defines and its required classes 35 R.Engine.define({ 36 "class":"R.resources.loaders.RemoteLoader", 37 "requires":[ 38 "R.resources.loaders.AbstractResourceLoader" 39 ] 40 }); 41 42 /** 43 * @class A base loader which implements the {@link #exists} method to 44 * synchronously check for the existence of a file. 45 * 46 * @constructor 47 * @param name {String=RemoteLoader} The name of the resource loader 48 * @extends R.resources.loaders.AbstractResourceLoader 49 */ 50 R.resources.loaders.RemoteLoader = function () { 51 return R.resources.loaders.AbstractResourceLoader.extend(/** @scope R.resources.loaders.RemoteLoader.prototype */{ 52 53 pathUrls:null, 54 55 /** @private */ 56 constructor:function (name) { 57 this.base(name || "RemoteLoader"); 58 this.pathUrls = {}; 59 }, 60 61 /** 62 * Performs a synchronous check for a file on the server. While this approach will 63 * work in most cases, there is the possibility that the server will become unavailable 64 * before the request is made. In this case, the application will hang until the 65 * request is satisfied (which may be never). 66 * 67 * @param url {String} The URL to check 68 * @return {Boolean} <tt>true</tt> if the file exists on the server or is in 69 * the cache. 70 */ 71 exists:function (url) { 72 var stat = jQuery.ajax({ 73 type:"GET", 74 url:url, 75 async:false, 76 dataType:"text" 77 }).status; 78 79 // If it returns OK or Cache not modified... 80 return (stat == R.resources.loaders.RemoteLoader.STATUS_OK || stat == R.resources.loaders.RemoteLoader.STATUS_CACHED); 81 }, 82 83 /** 84 * The name of the resource this loader will get. 85 * @returns {String} The string "remote" 86 */ 87 getResourceType:function () { 88 return "remote"; 89 }, 90 91 /** 92 * Load an resource from a remote URL. 93 * 94 * @param name {String} The name of the resource 95 * @param url {String} The URL where the resource is located 96 * @param data {Object} The loaded data to cache 97 */ 98 load:function (name, url, data, isReady) { 99 this.base(name, data, isReady); 100 this.setPathUrl(name, url); 101 }, 102 103 /** 104 * Set the path where a resource is located. 105 * @param name {String} the name of the resource 106 * @param url {String} The URL where the resource is located 107 */ 108 setPathUrl:function (name, url) { 109 // If the URL contains the game host or path, remove that 110 url = url.replace(R.Engine.getGame().getGamePath(), ""); 111 this.pathUrls[name] = url; 112 }, 113 114 /** 115 * Get the URL where the resource is located. 116 * @param name {String} The name of the resource 117 * @return {String} 118 */ 119 getPathUrl:function (name) { 120 return this.pathUrls[name]; 121 } 122 123 }, /** @scope R.resources.loaders.RemoteLoader.prototype */{ 124 /** 125 * Get the class name of this object. 126 * @return {String} The string "R.resources.loaders.RemoteLoader" 127 */ 128 getClassName:function () { 129 return "R.resources.loaders.RemoteLoader"; 130 }, 131 132 /** 133 * Transmit status ok 134 * @type {Number} 135 */ 136 STATUS_OK:200, 137 138 /** 139 * Transmit status - Cached 140 * @type {Number} 141 */ 142 STATUS_CACHED:304, 143 144 /** 145 * Transmit status - Not found 146 * @type {Number} 147 */ 148 STATUS_NOT_FOUND:404, 149 150 /** 151 * Transmit status - Server error 152 * @type {Number} 153 */ 154 STATUS_SERVER_ERROR:500 155 }); 156 157 }