Skip to content

Commit

Permalink
CCFileUtils: Adds a cache to remove/obtain path to improve the speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoquesada authored and lhunath committed Apr 5, 2012
1 parent 54dd374 commit a21a028
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cocos2d/CCDirector.m
Expand Up @@ -53,6 +53,7 @@
#import "Support/OpenGL_Internal.h"
#import "Support/CGPointExtension.h"
#import "Support/CCProfiling.h"
#import "Support/CCFileUtils.h"

#ifdef __CC_PLATFORM_IOS
#import "Platforms/iOS/CCDirectorIOS.h"
Expand Down Expand Up @@ -251,6 +252,7 @@ -(void) purgeCachedData
{
[CCLabelBMFont purgeCachedData];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[[CCFileUtils sharedFileUtils] purgeCachedEntries];
}

#pragma mark Director - Scene OpenGL Helper
Expand Down Expand Up @@ -438,6 +440,7 @@ -(void) end
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
[CCTextureCache purgeSharedTextureCache];
[CCShaderCache purgeSharedShaderCache];
[[CCFileUtils sharedFileUtils] purgeCachedEntries];

// OpenGL view

Expand Down
8 changes: 8 additions & 0 deletions cocos2d/Support/CCFileUtils.h
Expand Up @@ -33,6 +33,8 @@
{
NSFileManager *fileManager_;
NSBundle *bundle_;
NSMutableDictionary *fullPathCache_;
NSMutableDictionary *removeSuffixCache_;

#ifdef __CC_PLATFORM_IOS
BOOL enableFallbackSuffixes_;
Expand Down Expand Up @@ -110,6 +112,12 @@
-(NSString*) fullPathFromRelativePath:(NSString*) relPath;


/** Purge cached entries.
Will be called automatically by the Director when a memory warning is received
*/
-(void) purgeCachedEntries;


#ifdef __CC_PLATFORM_IOS

/** Returns the fullpath of an filename including the resolution of the image.
Expand Down
81 changes: 71 additions & 10 deletions cocos2d/Support/CCFileUtils.m
Expand Up @@ -32,38 +32,71 @@
#import "../ccTypes.h"


NSString *ccRemoveSuffixFromPath( NSString *suffix, NSString *path);
#pragma mark - Helper free functions

//
NSInteger ccLoadFileIntoMemory(const char *filename, unsigned char **out)
{
NSCAssert( out, @"ccLoadFileIntoMemory: invalid 'out' parameter");
NSCAssert( &*out, @"ccLoadFileIntoMemory: invalid 'out' parameter");

size_t size = 0;
FILE *f = fopen(filename, "rb");
if( !f ) {
*out = NULL;
return -1;
}

fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);

*out = malloc(size);
size_t read = fread(*out, 1, size, f);
if( read != size ) {
free(*out);
*out = NULL;
return -1;
}

fclose(f);

return size;
}

#pragma mark - CCCacheValue

@interface CCCacheValue : NSObject
{
NSString *fullpath_;
ccResolutionType resolutionType_;
}
@property (nonatomic, readwrite, retain) NSString *fullpath;
@property (nonatomic, readwrite ) ccResolutionType resolutionType;
@end

@implementation CCCacheValue
@synthesize fullpath = fullpath_;
@synthesize resolutionType = resolutionType_;
-(id) initWithFullPath:(NSString*)path resolutionType:(ccResolutionType)resolutionType
{
if( (self=[super init]) )
{
self.fullpath = path;
self.resolutionType = resolutionType;
}

return self;
}

- (void)dealloc
{
[fullpath_ release];

[super dealloc];
}
@end

#pragma mark - CCFileUtils

#ifdef __CC_PLATFORM_IOS
@interface CCFileUtils()
Expand Down Expand Up @@ -97,6 +130,9 @@ -(id) init
if( (self=[super init])) {
fileManager_ = [[NSFileManager alloc] init];

fullPathCache_ = [[NSMutableDictionary alloc] initWithCapacity:30];
removeSuffixCache_ = [[NSMutableDictionary alloc] initWithCapacity:30];

#ifdef __CC_PLATFORM_IOS
iPhoneRetinaDisplaySuffix_ = @"-hd";
iPadSuffix_ = @"-ipad";
Expand All @@ -110,10 +146,18 @@ -(id) init
return self;
}

-(void) purgeCachedEntries
{
[fullPathCache_ removeAllObjects];
[removeSuffixCache_ removeAllObjects];
}

- (void)dealloc
{
[fileManager_ release];
[bundle_ release];
[fullPathCache_ release];
[removeSuffixCache_ release];

#ifdef __CC_PLATFORM_IOS
[iPhoneRetinaDisplaySuffix_ release];
Expand Down Expand Up @@ -166,6 +210,12 @@ -(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResol
{
NSAssert(relPath != nil, @"CCFileUtils: Invalid path");

CCCacheValue *value = [fullPathCache_ objectForKey:relPath];
if( value ) {
*resolutionType = value.resolutionType;
return value.fullpath;
}

NSString *fullpath = nil;

// only if it is not an absolute path
Expand All @@ -178,8 +228,6 @@ -(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResol
fullpath = [[NSBundle mainBundle] pathForResource:file
ofType:nil
inDirectory:imageDirectory];


}

if (fullpath == nil)
Expand Down Expand Up @@ -241,15 +289,22 @@ -(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResol
ret = fullpath;
}

return ret;
fullpath = ret;

#elif defined(__CC_PLATFORM_MAC)


*resolutionType = kCCResolutionMac;

return fullpath;

#endif // __CC_PLATFORM_MAC

value = [[CCCacheValue alloc] initWithFullPath:fullpath resolutionType:*resolutionType];
[fullPathCache_ setObject:value forKey:relPath];
[value release];

return fullpath;

}

Expand Down Expand Up @@ -288,6 +343,10 @@ -(NSString *) removeSuffix:(NSString*)suffix fromPath:(NSString*)path

-(NSString*) removeSuffixFromFile:(NSString*) path
{
NSString *withoutSuffix = [removeSuffixCache_ objectForKey:path];
if( withoutSuffix )
return withoutSuffix;

NSString *ret = nil;

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
Expand All @@ -305,6 +364,8 @@ -(NSString*) removeSuffixFromFile:(NSString*) path
ret = path;
}

[removeSuffixCache_ setObject:ret forKey:path];

return ret;
}

Expand Down

0 comments on commit a21a028

Please sign in to comment.