project_files/HedgewarsMobile/Classes/MainMenuViewController.m
changeset 4149 51200479f9d8
parent 3996 eb549fd864a5
child 4281 e033cf015b2c
equal deleted inserted replaced
4148:eaff5615f976 4149:51200479f9d8
    53     if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
    53     if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
    54         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    54         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    55     [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    55     [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    56     
    56     
    57     // if the settings file is already present, we merge current preferences with the update
    57     // if the settings file is already present, we merge current preferences with the update
    58     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
    58     fileToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
    59     if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
    59     if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
    60         NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    60         NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    61         NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:directoryToCheck];
    61         NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToCheck];
    62         [update addEntriesFromDictionary:settings];
    62         [update addEntriesFromDictionary:settings];
    63         [settings release];
    63         [settings release];
    64         [update writeToFile:SETTINGS_FILE() atomically:YES];
    64         [update writeToFile:SETTINGS_FILE() atomically:YES];
    65         [update release];
    65         [update release];
    66     } else 
    66     } else 
    67         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SETTINGS_FILE() error:&err];
    67         [[NSFileManager defaultManager] copyItemAtPath:fileToCheck toPath:SETTINGS_FILE() error:&err];
    68     
    68     
    69     // if the teams are already present we merge the old teams if they still exist
    69     // TODO: scrap this and always copy the bundled files; update exisising ones in some way
       
    70     // if the teams are already present we merge the old teams, else we copy new teams
    70     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir];
    71     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir];
    71     if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) {
    72     if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) {
    72         for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:&err]) {
    73         for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:&err]) {
    73             fileToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str];
    74             fileToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str];
    74             fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str];
    75             fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str];
    77                 NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToUpdate];
    78                 NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToUpdate];
    78                 [update addEntriesFromDictionary:team];
    79                 [update addEntriesFromDictionary:team];
    79                 [team release];
    80                 [team release];
    80                 [update writeToFile:fileToCheck atomically:YES];
    81                 [update writeToFile:fileToCheck atomically:YES];
    81                 [update release];
    82                 [update release];
    82             }
    83             } else
       
    84                 [[NSFileManager defaultManager] copyItemAtPath:fileToUpdate toPath:fileToCheck error:&err];
    83         }
    85         }
    84     } else
    86     } else
    85         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:TEAMS_DIRECTORY() error:&err];
    87         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:TEAMS_DIRECTORY() error:&err];
    86     
    88     
    87     // the same holds for schemes (but they're arrays)
    89     // TODO: scrap this and always copy the bundled files; update exisising ones in some way
       
    90     // the same holds for schemes (but they're dictionaries containing arrays)
    88     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir];
    91     directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir];
    89     if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) {
    92     if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) {
    90         for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
    93         for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
    91             fileToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str];
    94             fileToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str];
    92             fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str];
    95             fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str];
    93             if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
    96             if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
    94                 NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:fileToCheck];
    97                 NSDictionary *scheme = [[NSDictionary alloc] initWithContentsOfFile:fileToCheck];
    95                 NSArray *update = [[NSArray alloc] initWithContentsOfFile:fileToUpdate];
    98                 NSDictionary *update = [[NSDictionary alloc] initWithContentsOfFile:fileToUpdate];
    96                 if ([update count] > [scheme count])
    99                 if ([[update objectForKey:@"basic"] count] > [[scheme objectForKey:@"basic"] count] ||
       
   100                     [[update objectForKey:@"gamemod"] count] > [[scheme objectForKey:@"gamemod"] count])
    97                     [update writeToFile:fileToCheck atomically:YES];
   101                     [update writeToFile:fileToCheck atomically:YES];
    98                 [update release];
   102                 [update release];
    99                 [scheme release];
   103                 [scheme release];
   100             }
   104             } else
       
   105                 [[NSFileManager defaultManager] copyItemAtPath:fileToUpdate toPath:fileToCheck error:&err];
   101         }
   106         }
   102     } else
   107     } else
   103         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SCHEMES_DIRECTORY() error:&err];
   108         [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SCHEMES_DIRECTORY() error:&err];
   104     
   109     
   105     // we create weapons the first time only, they are autoupdated each time
   110     // weapons are autoupdated at runtime but it's better to update then every new version
   106     if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO) {
   111     if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO)
   107         [[NSFileManager defaultManager] createDirectoryAtPath:WEAPONS_DIRECTORY()
   112         [[NSFileManager defaultManager] createDirectoryAtPath:WEAPONS_DIRECTORY()
   108                                   withIntermediateDirectories:YES
   113                                   withIntermediateDirectories:YES
   109                                                    attributes:nil
   114                                                    attributes:nil
   110                                                         error:&err];
   115                                                         error:&err];
   111         createWeaponNamed(@"Default", 0);
   116     createWeaponNamed(@"Default", 0);
   112         createWeaponNamed(@"Crazy", 1);
   117     createWeaponNamed(@"Crazy", 1);
   113         createWeaponNamed(@"Pro mode", 2);
   118     createWeaponNamed(@"Pro mode", 2);
   114         createWeaponNamed(@"Shoppa", 3);
   119     createWeaponNamed(@"Shoppa", 3);
   115         createWeaponNamed(@"Basketball", 4);
   120     createWeaponNamed(@"Clean slate", 4);
   116         createWeaponNamed(@"Minefield", 5);
   121     createWeaponNamed(@"Minefield", 5);
   117     }
   122 
   118     
       
   119     if (err != nil) 
   123     if (err != nil) 
   120         DLog(@"%@", err);
   124         DLog(@"%@", err);
   121     else
   125     else
   122         DLog(@"Success");
   126         DLog(@"Success");
   123 }
   127 }