Skip to content

Commit 519b121

Browse files
committed
CCFileUtils: Methods added to find paths of a file in all search paths.
Method added to load all filenameLookups in all search paths and merge them.
1 parent 716e06e commit 519b121

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

cocos2d/Support/CCFileUtils.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,20 @@ typedef NS_ENUM(NSUInteger, CCFileUtilsSearchMode) {
285285
*/
286286
-(NSString*) fullPathFromRelativePathIgnoringResolutions:(NSString*)relPath;
287287

288-
/**
288+
/**
289+
* Returns all fullpaths of a filename in all search paths without taking into account the screen resolution suffixes or directories.
290+
* It will use the "searchPath" though.
291+
* If the file can't be found, it will return an empty array.
292+
*
293+
* Useful for loading the fileLookup.plist and spriteFrameFileList.plist for packages
294+
*
295+
* @param relPath Relative path.
296+
*
297+
* @return Array of full paths.
298+
*/
299+
- (NSArray *)fullPathsOfFileNameInAllSearchPaths:(NSString *)filename;
300+
301+
/**
289302
* Returns the fullpath for a given filename.
290303
* 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.
291304
* Then it will try obtain the full path of the filename using the CCFileUtils search rules: resolutions, and search paths
@@ -358,6 +371,15 @@ typedef NS_ENUM(NSUInteger, CCFileUtilsSearchMode) {
358371
*/
359372
-(void) loadFilenameLookupDictionaryFromFile:(NSString*)filename;
360373

374+
/**
375+
* Loads the filenameLookup dictionary from the contents of a filename in all search paths.
376+
*
377+
* Used for packages to merge filenameLookups found in different search paths.
378+
*
379+
* @param filename Filename to query.
380+
*/
381+
- (void)loadFileNameLookupsInAllSearchPathsWithName:(NSString *)filename;
382+
361383
/**
362384
* Removes the suffix from a path.
363385
* On iPhone RetinaDisplay it will remove the -hd suffix

cocos2d/Support/CCFileUtils.m

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,68 @@ -(NSString*) fullPathFromRelativePathIgnoringResolutions:(NSString*)relPath
468468
return ret;
469469
}
470470

471+
- (NSArray *)fullPathsOfFileNameInAllSearchPaths:(NSString *)filename
472+
{
473+
NSMutableArray *result = [NSMutableArray array];
474+
475+
for (NSString *path in self.searchPath)
476+
{
477+
NSString *aPath = [path stringByAppendingPathComponent:filename];
478+
NSFileManager *fileManager = [NSFileManager defaultManager];
479+
480+
if ([fileManager fileExistsAtPath:aPath])
481+
{
482+
[result addObject:aPath];
483+
continue;
484+
}
485+
486+
NSString *file = [aPath lastPathComponent];
487+
NSString *file_path = [aPath stringByDeletingLastPathComponent];
488+
// Default to normal resource directory
489+
NSString *foundPath = [[NSBundle mainBundle] pathForResource:file
490+
ofType:nil
491+
inDirectory:file_path];
492+
if (foundPath)
493+
{
494+
[result addObject:aPath];
495+
}
496+
}
497+
498+
return result;
499+
}
500+
501+
- (void)loadAndAddFilenameLookupDictionaryFromFile:(NSString *)filename
502+
{
503+
NSString *fullpath = [self fullPathForFilenameIgnoringResolutions:filename];
504+
if( fullpath ) {
505+
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:fullpath];
506+
507+
NSDictionary *metadata = dict[@"metadata"];
508+
NSInteger version = [metadata[@"version"] integerValue];
509+
if( version != 1) {
510+
CCLOG(@"cocos2d: ERROR: Invalid filenameLookup dictionary version: %ld. Filename: %@", (long)version, filename);
511+
return;
512+
}
513+
514+
NSDictionary *filenames = dict[@"filenames"];
515+
NSMutableDictionary *newFileLookup = [NSMutableDictionary dictionary];
516+
[newFileLookup addEntriesFromDictionary:filenames];
517+
[newFileLookup addEntriesFromDictionary:_filenameLookup];
518+
519+
self.filenameLookup = newFileLookup;
520+
}
521+
}
522+
523+
- (void)loadFileNameLookupsInAllSearchPathsWithName:(NSString *)filename
524+
{
525+
NSArray *paths = [self fullPathsOfFileNameInAllSearchPaths:filename];
526+
527+
for (NSString *fileLookupFullPath in paths)
528+
{
529+
[self loadAndAddFilenameLookupDictionaryFromFile:fileLookupFullPath];
530+
}
531+
}
532+
471533
-(NSString*) fullPathForFilename:(NSString*)filename
472534
{
473535
return [self fullPathForFilename:filename contentScale:NULL];

0 commit comments

Comments
 (0)