Permalink
Browse files

CCFileUtils: Adds a cache to remove/obtain path to improve the speed

  • Loading branch information...
1 parent 54dd374 commit a21a02819263f923585d53cbf6595188a724423f @ricardoquesada ricardoquesada committed with Mar 23, 2012
Showing with 82 additions and 10 deletions.
  1. +3 −0 cocos2d/CCDirector.m
  2. +8 −0 cocos2d/Support/CCFileUtils.h
  3. +71 −10 cocos2d/Support/CCFileUtils.m
View
@@ -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"
@@ -251,6 +252,7 @@ -(void) purgeCachedData
{
[CCLabelBMFont purgeCachedData];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
+ [[CCFileUtils sharedFileUtils] purgeCachedEntries];
}
#pragma mark Director - Scene OpenGL Helper
@@ -438,6 +440,7 @@ -(void) end
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
[CCTextureCache purgeSharedTextureCache];
[CCShaderCache purgeSharedShaderCache];
+ [[CCFileUtils sharedFileUtils] purgeCachedEntries];
// OpenGL view
@@ -33,6 +33,8 @@
{
NSFileManager *fileManager_;
NSBundle *bundle_;
+ NSMutableDictionary *fullPathCache_;
+ NSMutableDictionary *removeSuffixCache_;
#ifdef __CC_PLATFORM_IOS
BOOL enableFallbackSuffixes_;
@@ -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.
@@ -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()
@@ -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";
@@ -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];
@@ -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
@@ -178,8 +228,6 @@ -(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResol
fullpath = [[NSBundle mainBundle] pathForResource:file
ofType:nil
inDirectory:imageDirectory];
-
-
}
if (fullpath == nil)
@@ -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;
}
@@ -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 )
@@ -305,6 +364,8 @@ -(NSString*) removeSuffixFromFile:(NSString*) path
ret = path;
}
+ [removeSuffixCache_ setObject:ret forKey:path];
+
return ret;
}

0 comments on commit a21a028

Please sign in to comment.