project_files/HedgewarsMobile/Classes/CommodityFunctions.m
branchhedgeroid
changeset 6224 42b256eca362
parent 6055 88cfcd9161d3
parent 6223 cc3eb9b7230f
child 6226 3106add9a5bf
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
     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 <CommonCrypto/CommonDigest.h>
       
    29 #import <SystemConfiguration/SCNetworkReachability.h>
       
    30 #import <netinet/in.h>
       
    31 #import "PascalImports.h"
       
    32 #import "hwconsts.h"
       
    33 
       
    34 
       
    35 NSInteger inline randomPort () {
       
    36     srandom(time(NULL));
       
    37     NSInteger res = (random() % 64511) + 1024;
       
    38     return (res == NETGAME_DEFAULT_PORT) ? randomPort() : res;
       
    39 }
       
    40 
       
    41 // by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
       
    42 void print_free_memory () {
       
    43 #ifdef DEBUG
       
    44     mach_port_t host_port;
       
    45     mach_msg_type_number_t host_size;
       
    46     vm_size_t pagesize;
       
    47 
       
    48     host_port = mach_host_self();
       
    49     host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
       
    50     host_page_size(host_port, &pagesize);
       
    51 
       
    52     vm_statistics_data_t vm_stat;
       
    53 
       
    54     if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
       
    55         DLog(@"Failed to fetch vm statistics");
       
    56 
       
    57     /* Stats in bytes */
       
    58     natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
       
    59     natural_t mem_free = vm_stat.free_count * pagesize;
       
    60     natural_t mem_total = mem_used + mem_free;
       
    61     DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
       
    62 #endif
       
    63 }
       
    64 
       
    65 NSString *getModelType () {
       
    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
       
    68     sysctlbyname("hw.machine", NULL, &size, NULL, 0);
       
    69     char *name = (char *)malloc(sizeof(char) * size);
       
    70     // get the platform name
       
    71     sysctlbyname("hw.machine", name, &size, NULL, 0);
       
    72     NSString *modelId = [NSString stringWithUTF8String:name];
       
    73     free(name);
       
    74 
       
    75     return modelId;
       
    76 }
       
    77 
       
    78 NSArray *getAvailableColors (void) {
       
    79     // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
       
    80     unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
       
    81     NSMutableArray *array = [[NSMutableArray alloc] init];
       
    82 
       
    83     int i = 0;
       
    84     while(colors[i] != 0)
       
    85         [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
       
    86 
       
    87     NSArray *final = [NSArray arrayWithArray:array];
       
    88     [array release];
       
    89     return final;
       
    90 }
       
    91 
       
    92 UILabel *createBlueLabel (NSString *title, CGRect frame) {
       
    93     return createLabelWithParams(title, frame, 1.5f, UICOLOR_HW_YELLOW_BODER, UICOLOR_HW_DARKBLUE);
       
    94 }
       
    95 
       
    96 UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor) {
       
    97     UILabel *theLabel = [[UILabel alloc] initWithFrame:frame];
       
    98     theLabel.backgroundColor = backgroundColor;
       
    99 
       
   100     if (title != nil) {
       
   101         theLabel.text = title;
       
   102         theLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
       
   103         theLabel.textAlignment = UITextAlignmentCenter;
       
   104         theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
       
   105     }
       
   106     
       
   107     [theLabel.layer setBorderWidth:borderWidth];
       
   108     [theLabel.layer setBorderColor:borderColor.CGColor];
       
   109     [theLabel.layer setCornerRadius:8.0f];
       
   110     [theLabel.layer setMasksToBounds:YES];
       
   111     
       
   112     return theLabel;
       
   113 }
       
   114 
       
   115 BOOL isNetworkReachable (void) {
       
   116     // Create zero addy
       
   117     struct sockaddr_in zeroAddress;
       
   118     bzero(&zeroAddress, sizeof(zeroAddress));
       
   119     zeroAddress.sin_len = sizeof(zeroAddress);
       
   120     zeroAddress.sin_family = AF_INET;
       
   121 
       
   122     // Recover reachability flags
       
   123     SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
       
   124     SCNetworkReachabilityFlags flags;
       
   125 
       
   126     BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
       
   127     CFRelease(defaultRouteReachability);
       
   128 
       
   129     if (!didRetrieveFlags) {
       
   130         NSLog(@"Error. Could not recover network reachability flags");
       
   131         return NO;
       
   132     }
       
   133 
       
   134     BOOL isReachable = flags & kSCNetworkFlagsReachable;
       
   135     BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
       
   136     BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
       
   137 
       
   138     NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
       
   139     NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
       
   140                                                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
       
   141                                              timeoutInterval:20.0];
       
   142     NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
       
   143     BOOL testResult = testConnection ? YES : NO;
       
   144     [testConnection release];
       
   145 
       
   146     return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
       
   147 }
       
   148 
       
   149 // this routine checks for the PNG size without loading it in memory
       
   150 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
       
   151 CGSize PSPNGSizeFromMetaData (NSString *aFileName) {
       
   152     // File Name to C String.
       
   153     const char *fileName = [aFileName UTF8String];
       
   154     // source file
       
   155     FILE *infile = fopen(fileName, "rb");
       
   156     if (infile == NULL) {
       
   157         DLog(@"Can't open the file: %@", aFileName);
       
   158         return CGSizeZero;
       
   159     }
       
   160 
       
   161     // Bytes Buffer.
       
   162     unsigned char buffer[30];
       
   163     // Grab Only First Bytes.
       
   164     fread(buffer, 1, 30, infile);
       
   165     // Close File.
       
   166     fclose(infile);
       
   167 
       
   168     // PNG Signature.
       
   169     unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
       
   170 
       
   171     // Compare File signature.
       
   172     if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) {
       
   173         DLog(@"The file (%@) is not a PNG file", aFileName);
       
   174         return CGSizeZero;
       
   175     }
       
   176 
       
   177     // Calc Sizes. Isolate only four bytes of each size (width, height).
       
   178     int width[4];
       
   179     int height[4];
       
   180     for (int d = 16; d < (16 + 4); d++) {
       
   181         width[d-16] = buffer[d];
       
   182         height[d-16] = buffer[d+4];
       
   183     }
       
   184 
       
   185     // Convert bytes to Long (Integer)
       
   186     long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3];
       
   187     long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3];
       
   188 
       
   189     // Return Size.
       
   190     return CGSizeMake(resultWidth,resultHeight);
       
   191 }
       
   192 
       
   193 @implementation NSString (extra)
       
   194 
       
   195 -(NSString *)MD5hash {
       
   196     const char *cStr = [self UTF8String];
       
   197     unsigned char result[16];
       
   198     CC_MD5( cStr, strlen(cStr), result );
       
   199     return [NSString stringWithFormat:
       
   200             @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
       
   201             result[0], result[1], result[2], result[3], result[4], result[5],
       
   202             result[6], result[7], result[8], result[9], result[10], result[11],
       
   203             result[12], result[13], result[14], result[15]];
       
   204 }
       
   205 
       
   206 
       
   207 @end