1 /* |
|
2 * Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 * |
|
18 * File created on 08/04/2010. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "CommodityFunctions.h" |
|
23 #import <sys/types.h> |
|
24 #import <sys/sysctl.h> |
|
25 #import <mach/mach.h> |
|
26 #import <mach/mach_host.h> |
|
27 #import <QuartzCore/QuartzCore.h> |
|
28 #import <AudioToolbox/AudioToolbox.h> |
|
29 #import <CommonCrypto/CommonDigest.h> |
|
30 #import <SystemConfiguration/SCNetworkReachability.h> |
|
31 #import <netinet/in.h> |
|
32 #import "PascalImports.h" |
|
33 #import "hwconsts.h" |
|
34 |
|
35 |
|
36 NSInteger inline randomPort () { |
|
37 srandom(time(NULL)); |
|
38 NSInteger res = (random() % 64511) + 1024; |
|
39 return (res == NETGAME_DEFAULT_PORT) ? randomPort() : res; |
|
40 } |
|
41 |
|
42 // by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html |
|
43 void print_free_memory () { |
|
44 #ifdef DEBUG |
|
45 mach_port_t host_port; |
|
46 mach_msg_type_number_t host_size; |
|
47 vm_size_t pagesize; |
|
48 |
|
49 host_port = mach_host_self(); |
|
50 host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); |
|
51 host_page_size(host_port, &pagesize); |
|
52 |
|
53 vm_statistics_data_t vm_stat; |
|
54 |
|
55 if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) |
|
56 DLog(@"Failed to fetch vm statistics"); |
|
57 |
|
58 /* Stats in bytes */ |
|
59 natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize; |
|
60 natural_t mem_free = vm_stat.free_count * pagesize; |
|
61 natural_t mem_total = mem_used + mem_free; |
|
62 DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total); |
|
63 #endif |
|
64 } |
|
65 |
|
66 BOOL inline isApplePhone () { |
|
67 return (IS_IPAD() == NO); |
|
68 } |
|
69 |
|
70 NSString *getModelType () { |
|
71 size_t size; |
|
72 // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
|
73 sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
|
74 char *name = (char *)malloc(sizeof(char) * size); |
|
75 // get the platform name |
|
76 sysctlbyname("hw.machine", name, &size, NULL, 0); |
|
77 NSString *modelId = [NSString stringWithUTF8String:name]; |
|
78 free(name); |
|
79 |
|
80 return modelId; |
|
81 } |
|
82 |
|
83 void playSound (NSString *snd) { |
|
84 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == YES) { |
|
85 // get the filename of the sound file: |
|
86 NSString *path = [NSString stringWithFormat:@"%@/%@.wav",[[NSBundle mainBundle] resourcePath],snd]; |
|
87 |
|
88 // declare a system sound id and get a URL for the sound file |
|
89 SystemSoundID soundID; |
|
90 NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; |
|
91 |
|
92 // use audio sevices to create and play the sound |
|
93 AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); |
|
94 AudioServicesPlaySystemSound(soundID); |
|
95 } |
|
96 } |
|
97 |
|
98 NSArray *getAvailableColors (void) { |
|
99 // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing |
|
100 unsigned int colors[] = HW_TEAMCOLOR_ARRAY; |
|
101 NSMutableArray *array = [[NSMutableArray alloc] init]; |
|
102 |
|
103 int i = 0; |
|
104 while(colors[i] != 0) |
|
105 [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]]; |
|
106 |
|
107 NSArray *final = [NSArray arrayWithArray:array]; |
|
108 [array release]; |
|
109 return final; |
|
110 } |
|
111 |
|
112 UILabel *createBlueLabel (NSString *title, CGRect frame) { |
|
113 return createLabelWithParams(title, frame, 1.5f, UICOLOR_HW_YELLOW_BODER, UICOLOR_HW_DARKBLUE); |
|
114 } |
|
115 |
|
116 UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor) { |
|
117 UILabel *theLabel = [[UILabel alloc] initWithFrame:frame]; |
|
118 theLabel.backgroundColor = backgroundColor; |
|
119 |
|
120 if (title != nil) { |
|
121 theLabel.text = title; |
|
122 theLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
|
123 theLabel.textAlignment = UITextAlignmentCenter; |
|
124 theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100]; |
|
125 } |
|
126 |
|
127 [theLabel.layer setBorderWidth:borderWidth]; |
|
128 [theLabel.layer setBorderColor:borderColor.CGColor]; |
|
129 [theLabel.layer setCornerRadius:8.0f]; |
|
130 [theLabel.layer setMasksToBounds:YES]; |
|
131 |
|
132 return theLabel; |
|
133 } |
|
134 |
|
135 BOOL isNetworkReachable (void) { |
|
136 // Create zero addy |
|
137 struct sockaddr_in zeroAddress; |
|
138 bzero(&zeroAddress, sizeof(zeroAddress)); |
|
139 zeroAddress.sin_len = sizeof(zeroAddress); |
|
140 zeroAddress.sin_family = AF_INET; |
|
141 |
|
142 // Recover reachability flags |
|
143 SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); |
|
144 SCNetworkReachabilityFlags flags; |
|
145 |
|
146 BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
|
147 CFRelease(defaultRouteReachability); |
|
148 |
|
149 if (!didRetrieveFlags) { |
|
150 NSLog(@"Error. Could not recover network reachability flags"); |
|
151 return NO; |
|
152 } |
|
153 |
|
154 BOOL isReachable = flags & kSCNetworkFlagsReachable; |
|
155 BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; |
|
156 BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection; |
|
157 |
|
158 NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; |
|
159 NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL |
|
160 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData |
|
161 timeoutInterval:20.0]; |
|
162 NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; |
|
163 BOOL testResult = testConnection ? YES : NO; |
|
164 [testConnection release]; |
|
165 |
|
166 return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO; |
|
167 } |
|
168 |
|
169 // this routine checks for the PNG size without loading it in memory |
|
170 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m |
|
171 CGSize PSPNGSizeFromMetaData (NSString *aFileName) { |
|
172 // File Name to C String. |
|
173 const char *fileName = [aFileName UTF8String]; |
|
174 // source file |
|
175 FILE *infile = fopen(fileName, "rb"); |
|
176 if (infile == NULL) { |
|
177 DLog(@"Can't open the file: %@", aFileName); |
|
178 return CGSizeZero; |
|
179 } |
|
180 |
|
181 // Bytes Buffer. |
|
182 unsigned char buffer[30]; |
|
183 // Grab Only First Bytes. |
|
184 fread(buffer, 1, 30, infile); |
|
185 // Close File. |
|
186 fclose(infile); |
|
187 |
|
188 // PNG Signature. |
|
189 unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
|
190 |
|
191 // Compare File signature. |
|
192 if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) { |
|
193 DLog(@"The file (%@) is not a PNG file", aFileName); |
|
194 return CGSizeZero; |
|
195 } |
|
196 |
|
197 // Calc Sizes. Isolate only four bytes of each size (width, height). |
|
198 int width[4]; |
|
199 int height[4]; |
|
200 for (int d = 16; d < (16 + 4); d++) { |
|
201 width[d-16] = buffer[d]; |
|
202 height[d-16] = buffer[d+4]; |
|
203 } |
|
204 |
|
205 // Convert bytes to Long (Integer) |
|
206 long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3]; |
|
207 long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3]; |
|
208 |
|
209 // Return Size. |
|
210 return CGSizeMake(resultWidth,resultHeight); |
|
211 } |
|
212 |
|
213 @implementation NSString (extra) |
|
214 |
|
215 -(NSString *)MD5hash { |
|
216 const char *cStr = [self UTF8String]; |
|
217 unsigned char result[16]; |
|
218 CC_MD5( cStr, strlen(cStr), result ); |
|
219 return [NSString stringWithFormat: |
|
220 @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
|
221 result[0], result[1], result[2], result[3], result[4], result[5], |
|
222 result[6], result[7], result[8], result[9], result[10], result[11], |
|
223 result[12], result[13], result[14], result[15]]; |
|
224 } |
|
225 |
|
226 |
|
227 @end |
|