project_files/HedgewarsMobile/Classes/HWUtils.m
changeset 6208 b831679e9467
parent 6108 7a8da11a6144
child 6209 074ab6ebcb3e
--- a/project_files/HedgewarsMobile/Classes/HWUtils.m	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/HWUtils.m	Tue Oct 25 22:51:10 2011 +0200
@@ -22,134 +22,48 @@
 #import "HWUtils.h"
 #import <sys/types.h>
 #import <sys/sysctl.h>
-#import <QuartzCore/QuartzCore.h>
-#import <CommonCrypto/CommonDigest.h>
-#import "PascalImports.h"
 #import "hwconsts.h"
 
+static NSString *cachedModel = nil;
+static NSArray *cachedColors = nil;
+
 @implementation HWUtils
 
 +(NSString *)modelType {
-    size_t size;
-    // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
-    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
-    char *name = (char *)malloc(sizeof(char) * size);
-    // get the platform name
-    sysctlbyname("hw.machine", name, &size, NULL, 0);
-    NSString *modelId = [NSString stringWithUTF8String:name];
-    free(name);
+    if (cachedModel == nil) {
+        size_t size;
+        // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
+        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
+        char *name = (char *)malloc(sizeof(char) * size);
+        // get the platform name
+        sysctlbyname("hw.machine", name, &size, NULL, 0);
 
-    return modelId;
+        cachedModel = [[NSString stringWithUTF8String:name] retain];
+        free(name);
+        DLog(@"Cache now contains: %@",cachedModel);
+    }
+    return cachedModel;
 }
 
 +(NSArray *)teamColors {
-    // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
-    unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
-    NSMutableArray *array = [[NSMutableArray alloc] init];
+    if (cachedColors == nil) {
+        // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
+        unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
+        NSMutableArray *array = [[NSMutableArray alloc] init];
 
-    int i = 0;
-    while(colors[i] != 0)
-        [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
+        int i = 0;
+        while(colors[i] != 0)
+            [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
 
-    NSArray *final = [NSArray arrayWithArray:array];
-    [array release];
-    return final;
+        cachedColors = [[NSArray arrayWithArray:array] retain];
+        [array release];
+    }
+    return cachedColors;
 }
 
-@end
-
-
-@implementation UITableView (extra)
-
--(void) setBackgroundColorForAnyTable:(UIColor *) color {
-    UIView *backView = [[UIView alloc] initWithFrame:self.frame];
-    backView.backgroundColor = color;
-    self.backgroundView = backView;
-    [backView release];
-    self.backgroundColor = [UIColor clearColor];
++(void) releaseCache {
+    releaseAndNil(cachedModel);
+    releaseAndNil(cachedColors);
 }
 
 @end
-
-
-
-@implementation UIColor (extra)
-
-+(UIColor *)darkYellowColor {
-    return [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xC0/255 blue:0 alpha:1];
-}
-
-+(UIColor *)lightYellowColor {
-    return [UIColor colorWithRed:(CGFloat)0xF0/255 green:(CGFloat)0xD0/255 blue:0 alpha:1];
-}
-
-+(UIColor *)darkBlueColor {
-    return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:1];
-}
-
-+(UIColor *)darkBlueColorTransparent {
-    return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x55/255 alpha:0.6f];
-}
-
-+(UIColor *)blackColorTransparent {
-    return [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65f];
-}
-
-@end
-
-
-@implementation UILabel (extra)
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title {
-    return [self initWithFrame:frame
-                      andTitle:title
-               withBorderWidth:1.5f
-               withBorderColor:[UIColor darkYellowColor]
-           withBackgroundColor:[UIColor darkBlueColor]];
-}
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth {
-    return [self initWithFrame:frame
-                      andTitle:title
-               withBorderWidth:borderWidth
-               withBorderColor:[UIColor darkYellowColor]
-           withBackgroundColor:[UIColor darkBlueColorTransparent]];
-}
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth
-          withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor{
-    UILabel *theLabel = [self initWithFrame:frame];
-    theLabel.backgroundColor = backColor;
-
-    if (title != nil) {
-        theLabel.text = title;
-        theLabel.textColor = [UIColor lightYellowColor];
-        theLabel.textAlignment = UITextAlignmentCenter;
-        theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
-    }
-
-    [theLabel.layer setBorderWidth:borderWidth];
-    [theLabel.layer setBorderColor:borderColor.CGColor];
-    [theLabel.layer setCornerRadius:8.0f];
-    [theLabel.layer setMasksToBounds:YES];
-
-    return theLabel;
-}
-
-@end
-
-
-@implementation NSString (extra)
-
--(NSString *)MD5hash {
-    const char *cStr = [self UTF8String];
-    unsigned char result[16];
-    CC_MD5( cStr, strlen(cStr), result );
-    return [NSString stringWithFormat:
-            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
-            result[0], result[1], result[2], result[3], result[4], result[5],
-            result[6], result[7], result[8], result[9], result[10], result[11],
-            result[12], result[13], result[14], result[15]];
-}
-
-@end