author | koda |
Mon, 31 Oct 2011 01:44:32 +0100 | |
changeset 6246 | 6b2d19ed521a |
parent 6209 | 074ab6ebcb3e |
child 6247 | 6dfad55fd71c |
permissions | -rw-r--r-- |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
1 |
/* |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
4 |
* |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
8 |
* |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
13 |
* |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
17 |
* |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
18 |
* File created on 01/10/2011. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
19 |
*/ |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
20 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
21 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
22 |
#import "HWUtils.h" |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
23 |
#import <sys/types.h> |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
24 |
#import <sys/sysctl.h> |
6246 | 25 |
#import <netinet/in.h> |
26 |
#import <SystemConfiguration/SCNetworkReachability.h> |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
27 |
#import "hwconsts.h" |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
28 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
29 |
static NSString *cachedModel = nil; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
30 |
static NSArray *cachedColors = nil; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
31 |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
32 |
@implementation HWUtils |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
33 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
34 |
+(NSString *)modelType { |
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
35 |
if (cachedModel == nil) { |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
36 |
size_t size; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
37 |
// set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
38 |
sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
39 |
char *name = (char *)malloc(sizeof(char) * size); |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
40 |
// get the platform name |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
41 |
sysctlbyname("hw.machine", name, &size, NULL, 0); |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
42 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
43 |
cachedModel = [[NSString stringWithUTF8String:name] retain]; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
44 |
free(name); |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
45 |
} |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
46 |
return cachedModel; |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
47 |
} |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
48 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
49 |
+(NSArray *)teamColors { |
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
50 |
if (cachedColors == nil) { |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
51 |
// by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
52 |
unsigned int colors[] = HW_TEAMCOLOR_ARRAY; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
53 |
NSMutableArray *array = [[NSMutableArray alloc] init]; |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
54 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
55 |
int i = 0; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
56 |
while(colors[i] != 0) |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
57 |
[array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]]; |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
58 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
59 |
cachedColors = [[NSArray arrayWithArray:array] retain]; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
60 |
[array release]; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
61 |
} |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
62 |
return cachedColors; |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
63 |
} |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
diff
changeset
|
64 |
|
6246 | 65 |
+(NSInteger) randomPort { |
66 |
srandom(time(NULL)); |
|
67 |
NSInteger res = (random() % 64511) + 1024; |
|
68 |
return (res == NETGAME_DEFAULT_PORT) ? [HWUtils randomPort] : res; |
|
69 |
} |
|
70 |
||
71 |
+(BOOL) isNetworkReachable { |
|
72 |
// Create zero addy |
|
73 |
struct sockaddr_in zeroAddress; |
|
74 |
bzero(&zeroAddress, sizeof(zeroAddress)); |
|
75 |
zeroAddress.sin_len = sizeof(zeroAddress); |
|
76 |
zeroAddress.sin_family = AF_INET; |
|
77 |
||
78 |
// Recover reachability flags |
|
79 |
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); |
|
80 |
SCNetworkReachabilityFlags flags; |
|
81 |
||
82 |
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
|
83 |
CFRelease(defaultRouteReachability); |
|
84 |
||
85 |
if (!didRetrieveFlags) { |
|
86 |
NSLog(@"Error. Could not recover network reachability flags"); |
|
87 |
return NO; |
|
88 |
} |
|
89 |
||
90 |
BOOL isReachable = flags & kSCNetworkFlagsReachable; |
|
91 |
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; |
|
92 |
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection; |
|
93 |
||
94 |
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; |
|
95 |
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL |
|
96 |
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData |
|
97 |
timeoutInterval:20.0]; |
|
98 |
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; |
|
99 |
BOOL testResult = testConnection ? YES : NO; |
|
100 |
[testConnection release]; |
|
101 |
||
102 |
return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO; |
|
103 |
} |
|
104 |
||
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
105 |
+(void) releaseCache { |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
106 |
releaseAndNil(cachedModel); |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
107 |
releaseAndNil(cachedColors); |
6108 | 108 |
} |
109 |
||
110 |
@end |