project_files/HedgewarsMobile/Classes/CommodityFunctions.m
author koda
Sun, 31 Jul 2011 16:03:24 +0200
changeset 5483 fc755bb8096d
parent 5207 4c9ae0f484da
child 5486 e75f7c3c6275
permissions -rw-r--r--
ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    18
 * File created on 08/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "CommodityFunctions.h"
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    23
#import <sys/types.h>
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    24
#import <sys/sysctl.h>
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
#import <mach/mach.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
#import <mach/mach_host.h>
3983
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
    27
#import <QuartzCore/QuartzCore.h>
4510
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
    28
#import <AudioToolbox/AudioToolbox.h>
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
    29
#import <CommonCrypto/CommonDigest.h>
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
    30
#import <SystemConfiguration/SCNetworkReachability.h>
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
    31
#import <netinet/in.h>
3926
668b71f31e51 use dynamic data from engine instead of using hardcoded values
koda
parents: 3917
diff changeset
    32
#import "PascalImports.h"
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
    33
#import "hwconsts.h"
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3930
diff changeset
    36
NSInteger inline randomPort () {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    srandom(time(NULL));
3971
5c82ee165ed5 minor stuff
koda
parents: 3941
diff changeset
    38
    NSInteger res = (random() % 64511) + 1024;
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
    39
    return (res == NETGAME_DEFAULT_PORT) ? randomPort() : res;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
// by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
void print_free_memory () {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    mach_port_t host_port;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    mach_msg_type_number_t host_size;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    vm_size_t pagesize;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    47
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    host_port = mach_host_self();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    50
    host_page_size(host_port, &pagesize);
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    51
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    vm_statistics_data_t vm_stat;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    53
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
        DLog(@"Failed to fetch vm statistics");
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    56
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    57
    /* Stats in bytes */
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    natural_t mem_free = vm_stat.free_count * pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    natural_t mem_total = mem_used + mem_free;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
}
3668
3f7a95234d8a tap to play piano notes, fix for audio and pause glitch
koda
parents: 3660
diff changeset
    63
3930
8b00b4f93242 remove a useless field from weaponfile format
koda
parents: 3926
diff changeset
    64
BOOL inline isApplePhone () {
3996
eb549fd864a5 the ios port is also iphone compatible
koda
parents: 3983
diff changeset
    65
    return (IS_IPAD() == NO);
3668
3f7a95234d8a tap to play piano notes, fix for audio and pause glitch
koda
parents: 3660
diff changeset
    66
}
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    67
5207
4c9ae0f484da some general tweaks (more intelligent use of macros and more caching)
koda
parents: 5206
diff changeset
    68
NSString *getModelType () {
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    69
    size_t size;
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    70
    // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    71
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    72
    char *name = (char *)malloc(sizeof(char) * size);
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    73
    // get the platform name
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    74
    sysctlbyname("hw.machine", name, &size, NULL, 0);
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    75
    NSString *modelId = [NSString stringWithUTF8String:name];
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    76
    free(name);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    77
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    78
    return modelId;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    79
}
3783
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    80
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    81
void playSound (NSString *snd) {
5207
4c9ae0f484da some general tweaks (more intelligent use of macros and more caching)
koda
parents: 5206
diff changeset
    82
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == YES) {
4947
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    83
        // get the filename of the sound file:
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    84
        NSString *path = [NSString stringWithFormat:@"%@/%@.wav",[[NSBundle mainBundle] resourcePath],snd];
3783
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    85
4947
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    86
        // declare a system sound id and get a URL for the sound file
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    87
        SystemSoundID soundID;
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    88
        NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
3783
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    89
4947
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    90
        // use audio sevices to create and play the sound
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    91
        AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    92
        AudioServicesPlaySystemSound(soundID);
cfac6aab84a0 don't play button sounds when audio is off
koda
parents: 4512
diff changeset
    93
    }
3917
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
    94
}
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
    95
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
    96
NSArray *getAvailableColors (void) {
5207
4c9ae0f484da some general tweaks (more intelligent use of macros and more caching)
koda
parents: 5206
diff changeset
    97
    // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
5201
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
    98
    unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
    99
    NSMutableArray *array = [[NSMutableArray alloc] init];
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   100
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   101
    int i = 0;
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   102
    while(colors[i] != 0)
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   103
        [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   104
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   105
    NSArray *final = [NSArray arrayWithArray:array];
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   106
    [array release];
7b9aa7aac336 * moved colours and default port from .cpp.in to .h
koda
parents: 4947
diff changeset
   107
    return final;
3917
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   108
}
3983
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   109
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   110
UILabel *createBlueLabel (NSString *title, CGRect frame) {
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   111
    return createLabelWithParams(title, frame, 1.5f, UICOLOR_HW_YELLOW_BODER, UICOLOR_HW_DARKBLUE);
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   112
}
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   113
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   114
UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor) {
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   115
    UILabel *theLabel = [[UILabel alloc] initWithFrame:frame];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   116
    theLabel.backgroundColor = backgroundColor;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   117
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   118
    if (title != nil) {
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   119
        theLabel.text = title;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   120
        theLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   121
        theLabel.textAlignment = UITextAlignmentCenter;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   122
        theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   123
    }
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   124
    
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   125
    [theLabel.layer setBorderWidth:borderWidth];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   126
    [theLabel.layer setBorderColor:borderColor.CGColor];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   127
    [theLabel.layer setCornerRadius:8.0f];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   128
    [theLabel.layer setMasksToBounds:YES];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   129
    
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   130
    return theLabel;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   131
}
4290
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   132
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   133
BOOL isNetworkReachable (void) {
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   134
    // Create zero addy
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   135
    struct sockaddr_in zeroAddress;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   136
    bzero(&zeroAddress, sizeof(zeroAddress));
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   137
    zeroAddress.sin_len = sizeof(zeroAddress);
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   138
    zeroAddress.sin_family = AF_INET;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   139
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   140
    // Recover reachability flags
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   141
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   142
    SCNetworkReachabilityFlags flags;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   143
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   144
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   145
    CFRelease(defaultRouteReachability);
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   146
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   147
    if (!didRetrieveFlags) {
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   148
        NSLog(@"Error. Could not recover network reachability flags");
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   149
        return NO;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   150
    }
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   151
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   152
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   153
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   154
    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   155
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   156
    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   157
    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   158
                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   159
                                             timeoutInterval:20.0];
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   160
    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   161
    BOOL testResult = testConnection ? YES : NO;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   162
    [testConnection release];
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   163
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   164
    return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   165
}
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5207
diff changeset
   166
4290
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   167
// this routine checks for the PNG size without loading it in memory
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   168
// https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   169
CGSize PSPNGSizeFromMetaData (NSString *aFileName) {
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   170
    // File Name to C String.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   171
    const char *fileName = [aFileName UTF8String];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   172
    // source file
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   173
    FILE *infile = fopen(fileName, "rb");
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   174
    if (infile == NULL) {
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   175
        DLog(@"Can't open the file: %@", aFileName);
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   176
        return CGSizeZero;
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   177
    }
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   178
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   179
    // Bytes Buffer.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   180
    unsigned char buffer[30];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   181
    // Grab Only First Bytes.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   182
    fread(buffer, 1, 30, infile);
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   183
    // Close File.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   184
    fclose(infile);
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   185
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   186
    // PNG Signature.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   187
    unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   188
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   189
    // Compare File signature.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   190
    if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) {
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   191
        DLog(@"The file (%@) is not a PNG file", aFileName);
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   192
        return CGSizeZero;
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   193
    }
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   194
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   195
    // Calc Sizes. Isolate only four bytes of each size (width, height).
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   196
    int width[4];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   197
    int height[4];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   198
    for (int d = 16; d < (16 + 4); d++) {
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   199
        width[d-16] = buffer[d];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   200
        height[d-16] = buffer[d+4];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   201
    }
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   202
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   203
    // Convert bytes to Long (Integer)
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   204
    long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   205
    long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3];
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   206
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   207
    // Return Size.
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   208
    return CGSizeMake(resultWidth,resultHeight);
82f1f1d819c0 don't list too big maps for certain devices
koda
parents: 4281
diff changeset
   209
}
4510
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   210
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   211
@implementation NSString (extra)
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   212
4512
c6aff8ceada0 more server stubs
koda
parents: 4510
diff changeset
   213
-(NSString *)MD5hash {
4510
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   214
    const char *cStr = [self UTF8String];
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   215
    unsigned char result[16];
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   216
    CC_MD5( cStr, strlen(cStr), result );
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   217
    return [NSString stringWithFormat:
4512
c6aff8ceada0 more server stubs
koda
parents: 4510
diff changeset
   218
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
4510
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   219
            result[0], result[1], result[2], result[3], result[4], result[5],
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   220
            result[6], result[7], result[8], result[9], result[10], result[11],
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   221
            result[12], result[13], result[14], result[15]];
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   222
}
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   223
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   224
ce9b8206e681 add some connection facility to netserver
koda
parents: 4356
diff changeset
   225
@end