summary refs log tree commit diff stats
path: root/libs/cocos2d/Support/CCFileUtils.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/Support/CCFileUtils.m')
-rwxr-xr-xlibs/cocos2d/Support/CCFileUtils.m169
1 files changed, 169 insertions, 0 deletions
diff --git a/libs/cocos2d/Support/CCFileUtils.m b/libs/cocos2d/Support/CCFileUtils.m new file mode 100755 index 0000000..6d33799 --- /dev/null +++ b/libs/cocos2d/Support/CCFileUtils.m
@@ -0,0 +1,169 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2008-2010 Ricardo Quesada
5 * Copyright (c) 2011 Zynga Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 */
26
27
28#import <Availability.h>
29#import "CCFileUtils.h"
30#import "../CCConfiguration.h"
31#import "../ccMacros.h"
32#import "../ccConfig.h"
33
34static NSFileManager *__localFileManager=nil;
35
36//
37NSInteger ccLoadFileIntoMemory(const char *filename, unsigned char **out)
38{
39 NSCAssert( out, @"ccLoadFileIntoMemory: invalid 'out' parameter");
40 NSCAssert( &*out, @"ccLoadFileIntoMemory: invalid 'out' parameter");
41
42 size_t size = 0;
43 FILE *f = fopen(filename, "rb");
44 if( !f ) {
45 *out = NULL;
46 return -1;
47 }
48
49 fseek(f, 0, SEEK_END);
50 size = ftell(f);
51 fseek(f, 0, SEEK_SET);
52
53 *out = malloc(size);
54 size_t read = fread(*out, 1, size, f);
55 if( read != size ) {
56 free(*out);
57 *out = NULL;
58 return -1;
59 }
60
61 fclose(f);
62
63 return size;
64}
65
66NSString *ccRemoveHDSuffixFromFile( NSString *path )
67{
68#if CC_IS_RETINA_DISPLAY_SUPPORTED
69
70 if( CC_CONTENT_SCALE_FACTOR() == 2 ) {
71
72 NSString *name = [path lastPathComponent];
73
74 // check if path already has the suffix.
75 if( [name rangeOfString:CC_RETINA_DISPLAY_FILENAME_SUFFIX].location != NSNotFound ) {
76
77 CCLOG(@"cocos2d: Filename(%@) contains %@ suffix. Removing it. See cocos2d issue #1040", path, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
78
79 NSString *newLastname = [name stringByReplacingOccurrencesOfString:CC_RETINA_DISPLAY_FILENAME_SUFFIX withString:@""];
80
81 NSString *pathWithoutLastname = [path stringByDeletingLastPathComponent];
82 return [pathWithoutLastname stringByAppendingPathComponent:newLastname];
83 }
84 }
85
86#endif // CC_IS_RETINA_DISPLAY_SUPPORTED
87
88 return path;
89
90}
91
92
93@implementation CCFileUtils
94
95+(void) initialize
96{
97 if( self == [CCFileUtils class] )
98 __localFileManager = [[NSFileManager alloc] init];
99}
100
101+(NSString*) getDoubleResolutionImage:(NSString*)path
102{
103#if CC_IS_RETINA_DISPLAY_SUPPORTED
104
105 if( CC_CONTENT_SCALE_FACTOR() == 2 )
106 {
107
108 NSString *pathWithoutExtension = [path stringByDeletingPathExtension];
109 NSString *name = [pathWithoutExtension lastPathComponent];
110
111 // check if path already has the suffix.
112 if( [name rangeOfString:CC_RETINA_DISPLAY_FILENAME_SUFFIX].location != NSNotFound ) {
113
114 CCLOG(@"cocos2d: WARNING Filename(%@) already has the suffix %@. Using it.", name, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
115 return path;
116 }
117
118
119 NSString *extension = [path pathExtension];
120
121 if( [extension isEqualToString:@"ccz"] || [extension isEqualToString:@"gz"] )
122 {
123 // All ccz / gz files should be in the format filename.xxx.ccz
124 // so we need to pull off the .xxx part of the extension as well
125 extension = [NSString stringWithFormat:@"%@.%@", [pathWithoutExtension pathExtension], extension];
126 pathWithoutExtension = [pathWithoutExtension stringByDeletingPathExtension];
127 }
128
129
130 NSString *retinaName = [pathWithoutExtension stringByAppendingString:CC_RETINA_DISPLAY_FILENAME_SUFFIX];
131 retinaName = [retinaName stringByAppendingPathExtension:extension];
132
133 if( [__localFileManager fileExistsAtPath:retinaName] )
134 return retinaName;
135
136 CCLOG(@"cocos2d: CCFileUtils: Warning HD file not found: %@", [retinaName lastPathComponent] );
137 }
138
139#endif // CC_IS_RETINA_DISPLAY_SUPPORTED
140
141 return path;
142}
143
144+(NSString*) fullPathFromRelativePath:(NSString*) relPath
145{
146 NSAssert(relPath != nil, @"CCFileUtils: Invalid path");
147
148 NSString *fullpath = nil;
149
150 // only if it is not an absolute path
151 if( ! [relPath isAbsolutePath] )
152 {
153 NSString *file = [relPath lastPathComponent];
154 NSString *imageDirectory = [relPath stringByDeletingLastPathComponent];
155
156 fullpath = [[NSBundle mainBundle] pathForResource:file
157 ofType:nil
158 inDirectory:imageDirectory];
159 }
160
161 if (fullpath == nil)
162 fullpath = relPath;
163
164 fullpath = [self getDoubleResolutionImage:fullpath];
165
166 return fullpath;
167}
168
169@end