Skip to content

Commit 354c3fd

Browse files
author
SeanLin
committed
Merge pull request cocos2d#755 from dingpinglv/Iss1643_CCFileUtils
Fixed cocos2d#1643 implement fullPathForFilename,loadFilenameLookup etc.. for CCFileUtils.js
2 parents 346b0da + 6d4a1a1 commit 354c3fd

File tree

4 files changed

+225
-9
lines changed

4 files changed

+225
-9
lines changed

cocos2d/platform/CCFileUtils.js

Lines changed: 219 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
8585
" IEBinaryToArray_ByteStr_Last = " + '""' + "\r\n" +
8686
" End If\r\n" +
8787
"End Function\r\n";// +
88-
//"</script>\r\n";
88+
//"</script>\r\n";
8989

9090
// inject VBScript
9191
//document.write(IEBinaryToArray_ByteStr_Script);
@@ -118,8 +118,19 @@ if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
118118
cc.FileUtils = cc.Class.extend({
119119
_fileDataCache:null,
120120

121+
_directory:null,
122+
_filenameLookupDict:null,
123+
_searchResolutionsOrderArray:null,
124+
_searchPathArray:null,
125+
121126
ctor:function () {
122127
this._fileDataCache = {};
128+
129+
this._searchPathArray = [];
130+
this._searchPathArray.push("");
131+
132+
this._searchResolutionsOrderArray = [];
133+
this._searchResolutionsOrderArray.push("");
123134
},
124135
/**
125136
* Get resource file data
@@ -188,7 +199,7 @@ cc.FileUtils = cc.Class.extend({
188199
return null;
189200

190201
var fileContents = cc._convertResponseBodyToText(req.responseBody);
191-
if(fileContents){
202+
if (fileContents) {
192203
arrayInfo = this._stringConvertToArray(req.responseText);
193204
this._fileDataCache[fileUrl] = arrayInfo;
194205
}
@@ -237,9 +248,9 @@ cc.FileUtils = cc.Class.extend({
237248
removeSuffixFromFile:function (path) {
238249
},
239250

240-
//////////////////////////////////////////////////////////////////////////
241-
// Notification support when getFileData from invalid file path.
242-
//////////////////////////////////////////////////////////////////////////
251+
//////////////////////////////////////////////////////////////////////////
252+
// Notification support when getFileData from invalid file path.
253+
//////////////////////////////////////////////////////////////////////////
243254
/**
244255
* Notification support when getFileData from invalid file path.
245256
* @function
@@ -260,6 +271,98 @@ cc.FileUtils = cc.Class.extend({
260271
return pszRelativePath;
261272
},
262273

274+
/**
275+
* <p>
276+
* Returns the fullpath for a given filename. </br>
277+
* First it will try to get a new filename from the "filenameLookup" dictionary. If a new filename can't be found on the dictionary, it will use the original filename. </br>
278+
* Then it will try obtain the full path of the filename using the CCFileUtils search rules: resources directory </br>
279+
* </br>
280+
* If the filename can't be found on resource directory(e.g. Resources/iphone-hd), it will go back to the root of asset folder(e.g. Resources/) to find the filename. </br>
281+
* </br>
282+
* If the filename can't be found on the file system, it will return the filename directly. </br>
283+
* </br>
284+
* This method was added to simplify multiplatform support. Whether you are using cocos2d-js or any cross-compilation toolchain like StellaSDK or Apportable, </br>
285+
* you might need to load differerent resources for a given file in the different platforms. </br>
286+
* </br>
287+
* Examples: </br>
288+
* * In iOS: "image.png" -> "image.pvr" -> "/full/path/res_dir/image.pvr" </br>
289+
* * In Android: "image.png" -> "image.png" -> "/full/path/res_dir/image.png" </br>
290+
* </p>
291+
* @param {String} filename
292+
* @return {String} fullpath for a given filename.
293+
*/
294+
fullPathForFilename:function (filename) {
295+
var found = false;
296+
297+
var newFileName = this._getNewFilename(filename);
298+
var fullPath;
299+
300+
if (newFileName && newFileName.length > 1 && (newFileName.indexOf(":") == 1))
301+
return newFileName;
302+
303+
for(var i = 0; i< this._searchPathArray.length; i++){
304+
var searchPath = this._searchPathArray[i];
305+
for(var j = 0; j < this._searchResolutionsOrderArray.length; j++){
306+
var resourceDirectory = this._searchResolutionsOrderArray[j];
307+
fullPath = this._getPathForFilename(newFileName, resourceDirectory,searchPath);
308+
if(fullPath){
309+
found = true;
310+
break;
311+
}
312+
}
313+
if(found)
314+
break;
315+
}
316+
317+
return found ? fullPath : newFileName;
318+
},
319+
320+
/**
321+
* <p>
322+
* Loads the filenameLookup dictionary from the contents of a filename. <br/>
323+
* <br/>
324+
* @note The plist file name should follow the format below: <br/>
325+
* <?xml version="1.0" encoding="UTF-8"?> <br/>
326+
* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <br/>
327+
* <plist version="1.0"> <br/>
328+
* <dict> <br/>
329+
* <key>filenames</key> <br/>
330+
* <dict> <br/>
331+
* <key>sounds/click.wav</key> <br/>
332+
* <string>sounds/click.caf</string> <br/>
333+
* <key>sounds/endgame.wav</key> <br/>
334+
* <string>sounds/endgame.caf</string> <br/>
335+
* <key>sounds/gem-0.wav</key> <br/>
336+
* <string>sounds/gem-0.caf</string> <br/>
337+
* </dict> <br/>
338+
* <key>metadata</key> <br/>
339+
* <dict> <br/>
340+
* <key>version</key> <br/>
341+
* <integer>1</integer> <br/>
342+
* </dict> <br/>
343+
* </dict> <br/>
344+
* </plist> <br/>
345+
* </p>
346+
* @param {String} filename The plist file name.
347+
*/
348+
loadFilenameLookup:function (filename) {
349+
var fullPath = this.fullPathForFilename(filename);
350+
if (fullPath.length > 0) {
351+
var dict = cc.SAXParser.getInstance().parse(fullPath);
352+
var metadataDict = dict["metadata"];
353+
var version = parseInt(metadataDict["version"]);
354+
if (version != 1) {
355+
cc.log("cocos2d: ERROR: Invalid filenameLookup dictionary version: " + version + ". Filename: " + filename);
356+
return;
357+
}
358+
this.setFilenameLookupDictionary(dict["filenames"]);
359+
}
360+
},
361+
362+
setFilenameLookupDictionary:function (filenameLookupDict) {
363+
this._filenameLookupDict = filenameLookupDict;
364+
},
365+
263366
/**
264367
* Generate the relative path of the file.
265368
* @function
@@ -280,6 +383,62 @@ cc.FileUtils = cc.Class.extend({
280383
}
281384
},
282385

386+
/**
387+
* <p>
388+
* Array that contains the search order of the resources based for the device.
389+
* By default it will try to load resources in the following order until one is found:
390+
* - On iPad HD: iPad HD resources, iPad resources, resources not associated with any device
391+
* - On iPad: iPad resources, resources not associated with any device
392+
* - On iPhone 5 HD: iPhone 5 HD resources, iPhone HD resouces, iPhone 5 resources, iPhone resources, resources not associated with any device
393+
* - On iPhone HD: iPhone HD resources, iPhone resouces, resources not associated with any device
394+
* - On iPhone: iPhone resources, resources not associated with any device
395+
*
396+
* - On Mac HD: Mac HD resources, Mac resources, resources not associated with any device
397+
* - On Mac: Mac resources, resources not associated with any device
398+
*
399+
* If the property "enableiPhoneResourcesOniPad" is enabled, it will also search for iPhone resources if you are in an iPad.
400+
* </p>
401+
* @param {Array} searchResolutionsOrder
402+
*/
403+
setSearchResolutionsOrder:function (searchResolutionsOrder) {
404+
this._searchResolutionsOrderArray = searchResolutionsOrder;
405+
},
406+
407+
/**
408+
* return Array that contains the search order of the resources based for the device.
409+
* @return {Array}
410+
*/
411+
getSearchResolutionsOrder:function () {
412+
return this._searchResolutionsOrderArray;
413+
},
414+
415+
/**
416+
* <p>
417+
* Array of search paths. <br/>
418+
* You can use this array to modify the search path of the resources. <br/>
419+
* If you want to use "themes" or search resources in the "cache", you can do it easily by adding new entries in this array. <br/>
420+
* <br/>
421+
* By default it is an array with only the "" (empty string) element. <br/>
422+
* </p>
423+
* @param {Array} searchPaths
424+
*/
425+
setSearchPath:function (searchPaths) {
426+
this._searchPathArray = searchPaths;
427+
},
428+
429+
/**
430+
* return Array of search paths.
431+
* @return {Array}
432+
*/
433+
getSearchPath:function () {
434+
return this._searchPathArray;
435+
},
436+
437+
getResourceDirectory:function () {
438+
return this._directory;
439+
},
440+
441+
283442
/**
284443
* Set the ResourcePath,we will find resource in this path
285444
* @function
@@ -322,6 +481,7 @@ cc.FileUtils = cc.Class.extend({
322481
* @deprecated
323482
*/
324483
getWriteablePath:function () {
484+
return "";
325485
},
326486

327487
/**
@@ -340,6 +500,60 @@ cc.FileUtils = cc.Class.extend({
340500
*/
341501
isPopupNotify:function () {
342502
return cc.popupNotify;
503+
},
504+
505+
_resourceRootPath:"",
506+
getResourceRootPath:function(){
507+
return this._resourceRootPath;
508+
},
509+
510+
setResourceRootPath:function(resourceRootPath){
511+
this._resourceRootPath = resourceRootPath;
512+
},
513+
514+
_getNewFilename:function (filename) {
515+
var newFileName = null;
516+
var fileNameFound = this._filenameLookupDict ? this._filenameLookupDict[filename] : null;
517+
if (!fileNameFound || fileNameFound.length === 0)
518+
newFileName = filename;
519+
else {
520+
newFileName = fileNameFound;
521+
cc.log("FOUND NEW FILE NAME: %s", newFileName);
522+
}
523+
return newFileName;
524+
},
525+
526+
_getPathForFilename:function (filename, resourceDirectory, searchPath) {
527+
var ret ;
528+
var resourceRootPath = this.getResourceRootPath(); //cc.Application.getInstance().getResourceRootPath();
529+
530+
if(filename && (filename.length > 0) && (filename.indexOf('/') === 0 || filename.indexOf("\\") === 0)){
531+
532+
} else if( resourceRootPath.length > 0){
533+
ret = resourceRootPath;
534+
if(ret[ret.length -1] != '\\' && ret[ret.length -1] != '/')
535+
ret += "/";
536+
} else {
537+
ret = resourceRootPath;
538+
}
539+
540+
var file = filename;
541+
var file_path = "";
542+
var pos = filename.lastIndexOf('/');
543+
if(pos != -1){
544+
file_path = filename.substr(0,pos+1);
545+
file = filename.substr(pos+1);
546+
}
547+
var path = searchPath;
548+
if(path.length > 0 && path.indexOf('/') !== path.length -1)
549+
path += '/';
550+
path += file_path;
551+
path += resourceDirectory;
552+
if(path.length > 0 && path.indexOf("/") !== path.length -1)
553+
path += '/';
554+
path += file;
555+
ret += path;
556+
return ret;
343557
}
344558
});
345559

cocos2d/sprite_nodes/CCSprite.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
548548
cc.Assert(filename != null, "Sprite#initWithFile():Invalid filename for sprite");
549549
var selfPointer = this;
550550

551-
var texture = cc.TextureCache.getInstance().textureForKey(filename);
551+
var texture = cc.TextureCache.getInstance().textureForKey(cc.FileUtils.getInstance().fullPathForFilename(filename));
552+
//var texture = cc.TextureCache.getInstance().textureForKey(filename);
552553
if (!texture) {
553554
//texture = cc.TextureCache.getInstance().addImage(filename);
554555
this._visible = false;

cocos2d/textures/CCTextureCache.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
154154
addImage:function (path) {
155155
cc.Assert(path != null, "TextureCache: path MUST not be null");
156156

157-
path = cc.FileUtils.getInstance().fullPathFromRelativePath(path);
157+
path = cc.FileUtils.getInstance().fullPathForFilename(path);
158158

159159
var texture = this.textures[path.toString()];
160160
if (texture) {
@@ -174,10 +174,11 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
174174
this.textures[path.toString()] = texture;
175175
}
176176

177-
if (cc.renderContextType == cc.CANVAS) {
177+
if (cc.renderContextType === cc.CANVAS) {
178178
return this.textures[path.toString()];
179179
} else {
180180
//todo texture for gl
181+
return null;
181182
}
182183
},
183184

0 commit comments

Comments
 (0)