author | koda |
Sat, 12 Nov 2011 21:59:10 +0100 | |
changeset 6337 | 84e7d1a5e3df |
parent 6321 | 5a0416e5a6de |
child 6409 | ca93f38a1aab |
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" |
6321 | 28 |
#import "EngineProtocolNetwork.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
|
29 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
30 |
static NSString *cachedModel = nil; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
31 |
static NSArray *cachedColors = nil; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
32 |
|
6247
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
33 |
static TGameType gameType = gtNone; |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
34 |
static TGameStatus gameStatus = gsNone; |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
35 |
|
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
|
36 |
@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
|
37 |
|
6247
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
38 |
#pragma mark - |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
39 |
#pragma mark game status and type info |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
40 |
+(TGameType) gameType { |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
41 |
return gameType; |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
42 |
} |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
43 |
|
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
44 |
+(void) setGameType:(TGameType) type { |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
45 |
gameType = type; |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
46 |
} |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
47 |
|
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
48 |
+(TGameStatus) gameStatus { |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
49 |
return gameStatus; |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
50 |
} |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
51 |
|
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
52 |
+(void) setGameStatus:(TGameStatus) status { |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
53 |
gameStatus = status; |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
54 |
} |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
55 |
|
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
56 |
+(BOOL) isGameLaunched { |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
57 |
return ((gameStatus == gsLoading) || (gameStatus == gsInGame)); |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
58 |
} |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
59 |
|
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
60 |
+(BOOL) isGameRunning { |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
61 |
return (gameStatus == gsInGame); |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
62 |
} |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
63 |
|
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
64 |
#pragma mark - |
6dfad55fd71c
unified the objc game state in a single place, which allowed some optimization to ObjcExport class (and more)
koda
parents:
6246
diff
changeset
|
65 |
#pragma mark Helper Functions |
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
|
66 |
+(NSString *)modelType { |
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
67 |
if (cachedModel == nil) { |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
68 |
size_t size; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
69 |
// 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
|
70 |
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
|
71 |
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
|
72 |
// get the platform name |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
73 |
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
|
74 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
75 |
cachedModel = [[NSString stringWithUTF8String:name] retain]; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
76 |
free(name); |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
77 |
} |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
78 |
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
|
79 |
} |
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
|
80 |
|
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
|
81 |
+(NSArray *)teamColors { |
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
82 |
if (cachedColors == nil) { |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
83 |
// 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
|
84 |
unsigned int colors[] = HW_TEAMCOLOR_ARRAY; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
85 |
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
|
86 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
87 |
int i = 0; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
88 |
while(colors[i] != 0) |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
89 |
[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
|
90 |
|
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
91 |
cachedColors = [[NSArray arrayWithArray:array] retain]; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
92 |
[array release]; |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
93 |
} |
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
94 |
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
|
95 |
} |
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
|
96 |
|
6246 | 97 |
+(NSInteger) randomPort { |
98 |
srandom(time(NULL)); |
|
99 |
NSInteger res = (random() % 64511) + 1024; |
|
6321 | 100 |
// recall self until you get a free port |
101 |
if (res == NETGAME_DEFAULT_PORT || res == [EngineProtocolNetwork activeEnginePort]) |
|
102 |
return [self randomPort]; |
|
103 |
else |
|
104 |
return res; |
|
6246 | 105 |
} |
106 |
||
107 |
+(BOOL) isNetworkReachable { |
|
108 |
// Create zero addy |
|
109 |
struct sockaddr_in zeroAddress; |
|
110 |
bzero(&zeroAddress, sizeof(zeroAddress)); |
|
111 |
zeroAddress.sin_len = sizeof(zeroAddress); |
|
112 |
zeroAddress.sin_family = AF_INET; |
|
113 |
||
114 |
// Recover reachability flags |
|
115 |
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); |
|
116 |
SCNetworkReachabilityFlags flags; |
|
117 |
||
118 |
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
|
119 |
CFRelease(defaultRouteReachability); |
|
120 |
||
121 |
if (!didRetrieveFlags) { |
|
122 |
NSLog(@"Error. Could not recover network reachability flags"); |
|
123 |
return NO; |
|
124 |
} |
|
125 |
||
126 |
BOOL isReachable = flags & kSCNetworkFlagsReachable; |
|
127 |
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; |
|
128 |
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection; |
|
129 |
||
130 |
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; |
|
131 |
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL |
|
132 |
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData |
|
133 |
timeoutInterval:20.0]; |
|
134 |
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; |
|
135 |
BOOL testResult = testConnection ? YES : NO; |
|
136 |
[testConnection release]; |
|
137 |
||
138 |
return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO; |
|
139 |
} |
|
140 |
||
6208
b831679e9467
move all extra categories in a single source, add caching for HWUtils
koda
parents:
6108
diff
changeset
|
141 |
+(void) releaseCache { |
6262 | 142 |
[cachedModel release], cachedModel = nil; |
143 |
[cachedColors release], cachedColors = nil; |
|
6108 | 144 |
} |
145 |
||
146 |
@end |