project_files/HedgewarsMobile/Classes/HWUtils.m
branchios-develop
changeset 12877 00215a7ec5f5
parent 11559 73e6a3d2f768
equal deleted inserted replaced
12876:2c06b1120749 12877:00215a7ec5f5
    33 
    33 
    34 @implementation HWUtils
    34 @implementation HWUtils
    35 
    35 
    36 #pragma mark -
    36 #pragma mark -
    37 #pragma mark game status and type info
    37 #pragma mark game status and type info
    38 +(TGameType) gameType {
    38 + (TGameType)gameType {
    39     return gameType;
    39     return gameType;
    40 }
    40 }
    41 
    41 
    42 +(void) setGameType:(TGameType) type {
    42 + (void)setGameType:(TGameType)type {
    43     gameType = type;
    43     gameType = type;
    44 }
    44 }
    45 
    45 
    46 +(TGameStatus) gameStatus {
    46 + (TGameStatus)gameStatus {
    47     return gameStatus;
    47     return gameStatus;
    48 }
    48 }
    49 
    49 
    50 +(void) setGameStatus:(TGameStatus) status {
    50 + (void)setGameStatus:(TGameStatus)status {
    51     gameStatus = status;
    51     gameStatus = status;
    52 }
    52 }
    53 
    53 
    54 +(BOOL) isGameLaunched {
    54 + (BOOL)isGameLaunched {
    55     return ((gameStatus == gsLoading) || (gameStatus == gsInGame));
    55     return ((gameStatus == gsLoading) || (gameStatus == gsInGame));
    56 }
    56 }
    57 
    57 
    58 +(BOOL) isGameRunning {
    58 + (BOOL)isGameRunning {
    59     return (gameStatus == gsInGame);
    59     return (gameStatus == gsInGame);
    60 }
    60 }
    61 
    61 
    62 #pragma mark -
    62 #pragma mark -
    63 #pragma mark Helper Functions with cache
    63 #pragma mark Helper Functions with cache
    64 +(NSString *)modelType {
    64 + (NSString *)modelType {
    65     if (cachedModel == nil) {
    65     if (cachedModel == nil) {
    66         size_t size;
    66         size_t size;
    67         // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
    67         // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
    68         sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    68         sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    69         char *name = (char *)malloc(sizeof(char) * size);
    69         char *name = (char *)malloc(sizeof(char) * size);
    70         // get the platform name
    70         // get the platform name
    71         sysctlbyname("hw.machine", name, &size, NULL, 0);
    71         sysctlbyname("hw.machine", name, &size, NULL, 0);
    72 
    72 
    73         cachedModel = [[NSString stringWithUTF8String:name] retain];
    73         cachedModel = [NSString stringWithUTF8String:name];
    74         free(name);
    74         free(name);
    75     }
    75     }
    76     return cachedModel;
    76     return cachedModel;
    77 }
    77 }
    78 
    78 
    79 +(NSArray *)teamColors {
    79 + (NSArray *)teamColors {
    80     if (cachedColors == nil) {
    80     if (cachedColors == nil) {
    81         // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
    81         // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
    82         unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
    82         unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
    83         NSMutableArray *array = [[NSMutableArray alloc] init];
    83         NSMutableArray *array = [[NSMutableArray alloc] init];
    84 
    84 
    85         int i = 0;
    85         int i = 0;
    86         while(colors[i] != 0)
    86         while(colors[i] != 0)
    87             [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
    87             [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
    88 
    88 
    89         cachedColors = [[NSArray arrayWithArray:array] retain];
    89         cachedColors = [NSArray arrayWithArray:array];
    90         [array release];
       
    91     }
    90     }
    92     return cachedColors;
    91     return cachedColors;
    93 }
    92 }
    94 
    93 
    95 +(void) releaseCache {
    94 + (void)releaseCache {
    96     [cachedModel release], cachedModel = nil;
    95     cachedModel = nil;
    97     [cachedColors release], cachedColors = nil;
    96     cachedColors = nil;
    98     // don't release activePorts here
    97     // don't release activePorts here
    99 }
    98 }
   100 
    99 
   101 #pragma mark -
   100 #pragma mark -
   102 #pragma mark Helper Functions without cache
   101 #pragma mark Helper Functions without cache
   103 +(NSInteger) randomPort {
   102 + (NSInteger)randomPort {
   104     // set a new feed only at initialization time and forbid connecting to the server port
   103     // set a new feed only at initialization time and forbid connecting to the server port
   105     if (activePorts == nil) {
   104     if (activePorts == nil) {
   106         activePorts = [[NSMutableArray arrayWithObject:[NSNumber numberWithInt:NETGAME_DEFAULT_PORT]] retain];
   105         activePorts = [NSMutableArray arrayWithObject:[NSNumber numberWithInt:NETGAME_DEFAULT_PORT]];
   107     }
   106     }
   108 
   107 
   109     // pick a random number from the free ports list
   108     // pick a random number from the free ports list
   110     NSInteger res = 0;
   109     NSInteger res = 0;
   111     do {
   110     do {
   115     // add this number to the forbdding list
   114     // add this number to the forbdding list
   116     [activePorts addObject:[NSNumber numberWithInteger:res]];
   115     [activePorts addObject:[NSNumber numberWithInteger:res]];
   117     return res;
   116     return res;
   118 }
   117 }
   119 
   118 
   120 +(void) freePort:(NSInteger) port {
   119 + (void)freePort:(NSInteger)port {
   121     [activePorts removeObject:[NSNumber numberWithInteger:port]];
   120     [activePorts removeObject:[NSNumber numberWithInteger:port]];
   122 }
   121 }
   123 
   122 
   124 +(BOOL) isNetworkReachable {
   123 + (BOOL)isNetworkReachable {
   125     // Create zero addy
   124     // Create zero addy
   126     struct sockaddr_in zeroAddress;
   125     struct sockaddr_in zeroAddress;
   127     bzero(&zeroAddress, sizeof(zeroAddress));
   126     bzero(&zeroAddress, sizeof(zeroAddress));
   128     zeroAddress.sin_len = sizeof(zeroAddress);
   127     zeroAddress.sin_len = sizeof(zeroAddress);
   129     zeroAddress.sin_family = AF_INET;
   128     zeroAddress.sin_family = AF_INET;
   148     NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
   147     NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
   149                                                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
   148                                                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
   150                                              timeoutInterval:20.0];
   149                                              timeoutInterval:20.0];
   151     NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
   150     NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
   152     BOOL testResult = testConnection ? YES : NO;
   151     BOOL testResult = testConnection ? YES : NO;
   153     [testConnection release];
       
   154 
   152 
   155     return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
   153     return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
   156 }
   154 }
   157 
   155 
   158 + (NSString *)languageID
   156 + (NSString *)languageID
   160     NSString *language = [[NSLocale preferredLanguages] firstObject];
   158     NSString *language = [[NSLocale preferredLanguages] firstObject];
   161     return [[language componentsSeparatedByString:@"-"] firstObject];
   159     return [[language componentsSeparatedByString:@"-"] firstObject];
   162 }
   160 }
   163 
   161 
   164 /*
   162 /*
   165 +(UIView *)mainSDLViewInstance {
   163 + (UIView *)mainSDLViewInstance {
   166     SDL_Window *window = HW_getSDLWindow();
   164     SDL_Window *window = HW_getSDLWindow();
   167     if (window == NULL) {
   165     if (window == NULL) {
   168         SDL_SetError("Window does not exist");
   166         SDL_SetError("Window does not exist");
   169         return nil;
   167         return nil;
   170     }
   168     }
   175 */
   173 */
   176 
   174 
   177 + (NSString *)seed
   175 + (NSString *)seed
   178 {
   176 {
   179     CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
   177     CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
   180     NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
   178     NSString *seed = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
   181     CFRelease(uuid);
   179     CFRelease(uuid);
   182     return seed;
   180     return seed;
   183 }
   181 }
   184 
   182 
   185 @end
   183 @end