project_files/HedgewarsMobile/Classes/MainMenuViewController.m
changeset 3923 694e6f6e0e30
parent 3903 db01c37494af
child 3971 5c82ee165ed5
equal deleted inserted replaced
3922:44804043b691 3923:694e6f6e0e30
    20 
    20 
    21 
    21 
    22 #import "MainMenuViewController.h"
    22 #import "MainMenuViewController.h"
    23 #import "CommodityFunctions.h"
    23 #import "CommodityFunctions.h"
    24 #import "SDL_uikitappdelegate.h"
    24 #import "SDL_uikitappdelegate.h"
    25 #import "SDL_mixer.h"
       
    26 #import "PascalImports.h"
    25 #import "PascalImports.h"
    27 #import "GameConfigViewController.h"
    26 #import "GameConfigViewController.h"
    28 #import "SplitViewRootController.h"
    27 #import "SplitViewRootController.h"
    29 #import "AboutViewController.h"
    28 #import "AboutViewController.h"
    30 #import "SavedGamesViewController.h"
    29 #import "SavedGamesViewController.h"
    31 
    30 
    32 @implementation MainMenuViewController
    31 @implementation MainMenuViewController
    33 @synthesize versionLabel, gameConfigViewController, settingsViewController, aboutViewController, savedGamesViewController;
    32 @synthesize gameConfigViewController, settingsViewController, aboutViewController, savedGamesViewController;
    34 
    33 
    35 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    34 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    36     return rotationManager(interfaceOrientation);
    35     return rotationManager(interfaceOrientation);
    37 }
    36 }
    38 
    37 
    53     [pool release];
    52     [pool release];
    54 }
    53 }
    55 
    54 
    56 // check if some configuration files are already set; if they are present it means that the current copy must be updated
    55 // check if some configuration files are already set; if they are present it means that the current copy must be updated
    57 -(void) createNecessaryFiles {
    56 -(void) createNecessaryFiles {
    58     BOOL doCreateFiles = NO;
    57     NSError *err = nil;
       
    58     NSString *directoryToCheck, *fileToCheck, *fileToUpdate;
    59     NSString *resDir = [[NSBundle mainBundle] resourcePath];
    59     NSString *resDir = [[NSBundle mainBundle] resourcePath];
    60     
    60     DLog(@"Creating necessary files");
    61     NSString *versionFileToCheck = [NSString stringWithFormat:@"%@/version.txt",DOCUMENTS_FOLDER()];
    61     
    62     if ([[NSFileManager defaultManager] fileExistsAtPath:versionFileToCheck]) {
    62     // create an empty saves directory by deleting the previous one (saves are incompatible between releases)
    63         NSString *currentVersion = [NSString stringWithContentsOfFile:versionFileToCheck encoding:NSUTF8StringEncoding error:nil];
    63     if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
    64         NSString *newVersion = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/Settings/version.txt",resDir] encoding:NSUTF8StringEncoding error:nil];
    64         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    65         if ([currentVersion intValue] < [newVersion intValue]) {
    65     [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    66             doCreateFiles = YES;
    66     
    67             [newVersion writeToFile:versionFileToCheck atomically:YES encoding:NSUTF8StringEncoding error:nil];
    67     // if the settings file is already present, we merge current preferences with the update
       
    68     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
       
    69     if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
       
    70         NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
       
    71         NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:directoryToCheck];
       
    72         [update addEntriesFromDictionary:settings];
       
    73         [settings release];
       
    74         [update writeToFile:SETTINGS_FILE() atomically:YES];
       
    75         [update release];
       
    76     } else 
       
    77         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SETTINGS_FILE() error:&err];
       
    78     
       
    79     // if the teams are already present we merge the old teams if they still exist
       
    80     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir];
       
    81     if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) {
       
    82         for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:&err]) {
       
    83             fileToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str];
       
    84             fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str];
       
    85             if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
       
    86                 NSDictionary *team = [[NSDictionary alloc] initWithContentsOfFile:fileToCheck];
       
    87                 NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToUpdate];
       
    88                 [update addEntriesFromDictionary:team];
       
    89                 [team release];
       
    90                 [update writeToFile:fileToCheck atomically:YES];
       
    91                 [update release];
       
    92             }
    68         }
    93         }
    69     } else {
    94     } else
    70         doCreateFiles = YES;
    95         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:TEAMS_DIRECTORY() error:&err];
    71         [[NSFileManager defaultManager] copyItemAtPath:[NSString stringWithFormat:@"%@/Settings/version.txt",resDir] toPath:versionFileToCheck error:nil];
    96     
    72     } 
    97     // the same holds for schemes (but they're arrays)
    73 
    98     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir];
    74     
    99     if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) {
    75     if (doCreateFiles == YES) {
   100         for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
    76         NSError *err = nil;
   101             fileToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str];
    77         NSString *directoryToCheck, *fileToCheck, *fileToUpdate;
   102             fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str];
    78         DLog(@"Creating necessary files");
   103             if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
    79         
   104                 NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:fileToCheck];
    80         // create an empty saves directory by deleting the previous one (saves are incompatible between releases)
   105                 NSArray *update = [[NSArray alloc] initWithContentsOfFile:fileToUpdate];
    81         if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
   106                 if ([update count] > [scheme count])
    82             [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
       
    83         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
       
    84         
       
    85         // if the settings file is already present, we merge current preferences with the update
       
    86         directoryToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
       
    87         if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
       
    88             NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
       
    89             NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:directoryToCheck];
       
    90             [update addEntriesFromDictionary:settings];
       
    91             [settings release];
       
    92             [update writeToFile:SETTINGS_FILE() atomically:YES];
       
    93             [update release];
       
    94         } else 
       
    95             [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SETTINGS_FILE() error:&err];
       
    96         
       
    97         // if the teams are already present we merge the old teams if they still exist
       
    98         directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir];
       
    99         if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) {
       
   100             for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
       
   101                 fileToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str];
       
   102                 fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str];
       
   103                 if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
       
   104                     NSDictionary *team = [[NSDictionary alloc] initWithContentsOfFile:fileToCheck];
       
   105                     NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToUpdate];
       
   106                     [update addEntriesFromDictionary:team];
       
   107                     [team release];
       
   108                     [update writeToFile:fileToCheck atomically:YES];
   107                     [update writeToFile:fileToCheck atomically:YES];
   109                     [update release];
   108                 [update release];
   110                 }
   109                 [scheme release];
   111             }
   110             }
   112         } else
       
   113             [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:TEAMS_DIRECTORY() error:&err];
       
   114 
       
   115         // the same holds for schemes (but they're arrays)
       
   116         directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir];
       
   117         if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) {
       
   118             for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
       
   119                 fileToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str];
       
   120                 fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str];
       
   121                 if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
       
   122                     NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:fileToCheck];
       
   123                     NSArray *update = [[NSArray alloc] initWithContentsOfFile:fileToUpdate];
       
   124                     if ([update count] > [scheme count])
       
   125                         [update writeToFile:fileToCheck atomically:YES];
       
   126                     [update release];
       
   127                     [scheme release];
       
   128                 }
       
   129             }
       
   130         } else
       
   131             [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SCHEMES_DIRECTORY() error:&err];
       
   132         
       
   133         // we create weapons the first time only, they are autoupdated each time
       
   134         if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO) {
       
   135             [[NSFileManager defaultManager] createDirectoryAtPath:WEAPONS_DIRECTORY()
       
   136                                       withIntermediateDirectories:YES
       
   137                                                        attributes:nil
       
   138                                                             error:&err];
       
   139             createWeaponNamed(@"Default", 0);
       
   140             createWeaponNamed(@"Crazy", 1);
       
   141             createWeaponNamed(@"Pro mode", 2);
       
   142             createWeaponNamed(@"Shoppa", 3);
       
   143             createWeaponNamed(@"Basketball", 4);
       
   144             createWeaponNamed(@"Minefield", 5);
       
   145         }
   111         }
   146         
   112     } else
       
   113         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SCHEMES_DIRECTORY() error:&err];
       
   114     
       
   115     // we create weapons the first time only, they are autoupdated each time
       
   116     if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO) {
       
   117         [[NSFileManager defaultManager] createDirectoryAtPath:WEAPONS_DIRECTORY()
       
   118                                   withIntermediateDirectories:YES
       
   119                                                    attributes:nil
       
   120                                                         error:&err];
       
   121         createWeaponNamed(@"Default", 0);
       
   122         createWeaponNamed(@"Crazy", 1);
       
   123         createWeaponNamed(@"Pro mode", 2);
       
   124         createWeaponNamed(@"Shoppa", 3);
       
   125         createWeaponNamed(@"Basketball", 4);
       
   126         createWeaponNamed(@"Minefield", 5);
       
   127     }
       
   128     
       
   129     if (err != nil) 
       
   130         DLog(@"%@", err);
       
   131     else
   147         DLog(@"Success");
   132         DLog(@"Success");
   148         
   133 }
   149         if (err != nil) 
       
   150             DLog(@"%@", err);
       
   151     }
       
   152 }
       
   153 
       
   154 /* // ask the user to leave a review for this app
       
   155 -(void) reviewCounter {
       
   156     CGFloat reviewInt = [[NSUserDefaults standardUserDefaults] integerForKey: @"intValueKey"];
       
   157     
       
   158     if (reviewInt) {
       
   159         reviewInt++;
       
   160         [[NSUserDefaults standardUserDefaults] setInteger:reviewInt forKey:@"intValueKey"];
       
   161     } else {
       
   162         CGFloat start = 1;
       
   163         NSUserDefaults *reviewPrefs = [NSUserDefaults standardUserDefaults];
       
   164         [reviewPrefs setInteger:start forKey: @"intValueKey"];
       
   165         [reviewPrefs synchronize]; // writes modifications to disk
       
   166     }
       
   167     
       
   168     if (1) {
       
   169         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mabuhay!"
       
   170                                                         message:@"Looks like you Enjoy using this app. Could you spare a moment of your time to review it in the AppStore?"
       
   171                                                        delegate:self
       
   172                                               cancelButtonTitle:nil
       
   173                                               otherButtonTitles: @"OK, I'll Review It Now", @"Remind Me Later", @"Don't Remind Me", nil];
       
   174         [alert show]; 
       
   175         [alert release];
       
   176         
       
   177         reviewInt++;
       
   178         
       
   179         [[NSUserDefaults standardUserDefaults] setInteger:reviewInt forKey:@"intValueKey"];
       
   180     }
       
   181 }
       
   182 
       
   183 #pragma mark -
       
   184 #pragma mark alert view delegate
       
   185 -(void) alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger) buttonIndex {
       
   186     // the user clicked one of the OK/Cancel buttons
       
   187     if (buttonIndex == 0) {
       
   188         NSString *str = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa";
       
   189         str = [NSString stringWithFormat:@"%@/wa/viewContentsUserReviews?", str]; 
       
   190         str = [NSString stringWithFormat:@"%@type=Vittorio+Giovara&id=", str];
       
   191         
       
   192         // Here is the app id from itunesconnect
       
   193         str = [NSString stringWithFormat:@"%@391234866", str]; 
       
   194         
       
   195         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=391234866&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software"]]; 
       
   196     } else if (buttonIndex == 1) {
       
   197         int startAgain = 0;
       
   198         [[NSUserDefaults standardUserDefaults] setInteger:startAgain forKey:@"intValueKey"];
       
   199         
       
   200     } else if (buttonIndex == 2) { 
       
   201         int neverRemind = 4;
       
   202         [[NSUserDefaults standardUserDefaults] setInteger:neverRemind forKey:@"intValueKey"];
       
   203     }
       
   204 } */
       
   205 
   134 
   206 #pragma mark -
   135 #pragma mark -
   207 -(void) viewDidLoad {
   136 -(void) viewDidLoad {
   208     [NSThread detachNewThreadSelector:@selector(initAudioThread)
   137     [super viewDidLoad];
   209                              toTarget:self
   138 
   210                            withObject:nil];
   139     // listen to request to remove the modalviewcontroller (needed due to the splitcontroller)
   211 
       
   212     char *ver;
       
   213     HW_versionInfo(NULL, &ver);
       
   214     NSString *versionNumber = [[NSString alloc] initWithCString:ver];
       
   215     self.versionLabel.text = @"";//versionNumber;
       
   216     [versionNumber release];
       
   217 
       
   218     // listen to request to remove the modalviewcontroller
       
   219     [[NSNotificationCenter defaultCenter] addObserver:self
   140     [[NSNotificationCenter defaultCenter] addObserver:self
   220                                              selector:@selector(dismissModalViewController)
   141                                              selector:@selector(dismissModalViewController)
   221                                                  name: @"dismissModalView"
   142                                                  name: @"dismissModalView"
   222                                                object:nil];
   143                                                object:nil];
   223 
   144 
   224     [self createNecessaryFiles];
   145     // get the app's version
   225     
   146     NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
   226     [super viewDidLoad];
   147 
       
   148     // get the version number that we've been tracking
       
   149     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
       
   150     NSString *trackingVersion = [userDefaults stringForKey:@"HedgeVersion"];
       
   151 
       
   152     if (trackingVersion == nil || [trackingVersion isEqualToString:version] == NO) {
       
   153         [userDefaults setObject:version forKey:@"HedgeVersion"];
       
   154         [userDefaults synchronize];
       
   155         [self createNecessaryFiles];
       
   156     }
   227 }
   157 }
   228 
   158 
   229 
   159 
   230 #pragma mark -
   160 #pragma mark -
   231 -(IBAction) switchViews:(id) sender {
   161 -(IBAction) switchViews:(id) sender {
   316     [self dismissModalViewControllerAnimated:YES];
   246     [self dismissModalViewControllerAnimated:YES];
   317 }
   247 }
   318 
   248 
   319 -(void) viewDidUnload {
   249 -(void) viewDidUnload {
   320     [[NSNotificationCenter defaultCenter] removeObserver:self];
   250     [[NSNotificationCenter defaultCenter] removeObserver:self];
   321     self.versionLabel = nil;
       
   322     self.gameConfigViewController = nil;
   251     self.gameConfigViewController = nil;
   323     self.settingsViewController = nil;
   252     self.settingsViewController = nil;
   324     self.aboutViewController = nil;
   253     self.aboutViewController = nil;
   325     self.savedGamesViewController = nil;
   254     self.savedGamesViewController = nil;
   326     MSG_DIDUNLOAD();
   255     MSG_DIDUNLOAD();
   327     [super viewDidUnload];
   256     [super viewDidUnload];
   328 }
   257 }
   329 
   258 
   330 -(void) dealloc {
   259 -(void) dealloc {
   331     [versionLabel release];
       
   332     [settingsViewController release];
   260     [settingsViewController release];
   333     [gameConfigViewController release];
   261     [gameConfigViewController release];
   334     [aboutViewController release];
   262     [aboutViewController release];
   335     [savedGamesViewController release];
   263     [savedGamesViewController release];
   336     [super dealloc];
   264     [super dealloc];