53 // listen to request to remove the modalviewcontroller |
53 // listen to request to remove the modalviewcontroller |
54 [[NSNotificationCenter defaultCenter] addObserver:self |
54 [[NSNotificationCenter defaultCenter] addObserver:self |
55 selector:@selector(dismissModalViewController) |
55 selector:@selector(dismissModalViewController) |
56 name: @"dismissModalView" |
56 name: @"dismissModalView" |
57 object:nil]; |
57 object:nil]; |
58 |
58 |
59 // initialize some files the first time we load the game |
59 // now check if some configuration files are already set; if they are present it means that the current copy must be updated |
60 if (!([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()])) |
60 NSError *err = nil; |
61 [NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil]; |
61 NSString *fileToCheck, *teamToCheck, *teamToUpdate, *schemeToCheck, *schemeToUpdate; |
62 |
62 NSString *resDir = [[NSBundle mainBundle] resourcePath]; |
|
63 |
|
64 NSString *dirToCheck = [NSString stringWithFormat:@"%@/Settings/", resDir]; |
|
65 if ([[NSFileManager defaultManager] fileExistsAtPath:dirToCheck] == YES) { |
|
66 |
|
67 // if the settings file is already present, we merge current preferences with the update |
|
68 fileToCheck = [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:fileToCheck]; |
|
72 [update addEntriesFromDictionary:settings]; |
|
73 [settings release]; |
|
74 [update writeToFile:SETTINGS_FILE() atomically:YES]; |
|
75 [update release]; |
|
76 } else |
|
77 [[NSFileManager defaultManager] copyItemAtPath:fileToCheck toPath:SETTINGS_FILE() error:&err]; |
|
78 |
|
79 // if the teams are already present we merge the old teams if they still exist |
|
80 fileToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir]; |
|
81 if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) { |
|
82 for (NSString *str in [[NSFileManager defaultManager] contentsAtPath:fileToCheck]) { |
|
83 teamToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str]; |
|
84 teamToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str]; |
|
85 if ([[NSFileManager defaultManager] fileExistsAtPath:teamToCheck]) { |
|
86 NSDictionary *team = [[NSDictionary alloc] initWithContentsOfFile:teamToCheck]; |
|
87 NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:teamToUpdate]; |
|
88 [update addEntriesFromDictionary:team]; |
|
89 [team release]; |
|
90 [update writeToFile:teamToCheck atomically:YES]; |
|
91 [update release]; |
|
92 } |
|
93 } |
|
94 } else |
|
95 [[NSFileManager defaultManager] copyItemAtPath:fileToCheck toPath:TEAMS_DIRECTORY() error:&err]; |
|
96 |
|
97 // the same holds for schemes (but they're arrays) |
|
98 fileToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir]; |
|
99 if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) { |
|
100 for (NSString *str in [[NSFileManager defaultManager] contentsAtPath:fileToCheck]) { |
|
101 schemeToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str]; |
|
102 schemeToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str]; |
|
103 if ([[NSFileManager defaultManager] fileExistsAtPath:schemeToCheck]) { |
|
104 NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemeToCheck]; |
|
105 NSArray *update = [[NSArray alloc] initWithContentsOfFile:schemeToUpdate]; |
|
106 if ([update count] > [scheme count]) |
|
107 [update writeToFile:schemeToCheck atomically:YES]; |
|
108 [update release]; |
|
109 [scheme release]; |
|
110 } |
|
111 } |
|
112 } else |
|
113 [[NSFileManager defaultManager] copyItemAtPath:fileToCheck 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 // clean this dir so that it doesn't get called again |
|
130 [[NSFileManager defaultManager] removeItemAtPath:dirToCheck error:&err]; |
|
131 if (err != nil) |
|
132 DLog(@"%@", err); |
|
133 } |
|
134 |
63 [super viewDidLoad]; |
135 [super viewDidLoad]; |
64 } |
136 } |
65 |
137 |
66 // this is called to verify whether it's the first time the app is launched |
|
67 // if it is it blocks user interaction with an alertView until files are created |
|
68 -(void) checkFirstRun { |
|
69 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
70 DLog(@"First time run, creating settings files at %@", SETTINGS_FILE()); |
|
71 |
|
72 // show a popup with an indicator to make the user wait |
|
73 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Please wait",@"") |
|
74 message:nil |
|
75 delegate:nil |
|
76 cancelButtonTitle:nil |
|
77 otherButtonTitles:nil]; |
|
78 [alert show]; |
|
79 |
|
80 UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] |
|
81 initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; |
|
82 indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50); |
|
83 [indicator startAnimating]; |
|
84 [alert addSubview:indicator]; |
|
85 [indicator release]; |
|
86 |
|
87 // create default files (teams/weapons/scheme) |
|
88 createTeamNamed(@"Pirates"); |
|
89 createTeamNamed(@"Ninjas"); |
|
90 createWeaponNamed(@"Default"); |
|
91 createSchemeNamed(@"Default"); |
|
92 |
|
93 // create settings.plist |
|
94 NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init]; |
|
95 |
|
96 [saveDict setObject:@"" forKey:@"username"]; |
|
97 [saveDict setObject:@"" forKey:@"password"]; |
|
98 [saveDict setObject:[NSNumber numberWithBool:YES] forKey:@"music"]; |
|
99 [saveDict setObject:[NSNumber numberWithBool:YES] forKey:@"sound"]; |
|
100 [saveDict setObject:[NSNumber numberWithBool:NO] forKey:@"alternate"]; |
|
101 |
|
102 [saveDict writeToFile:SETTINGS_FILE() atomically:YES]; |
|
103 [saveDict release]; |
|
104 |
|
105 // ok let the user take control |
|
106 [alert dismissWithClickedButtonIndex:0 animated:YES]; |
|
107 [alert release]; |
|
108 |
|
109 [pool release]; |
|
110 |
|
111 // TODO: instead of this useless runtime initialization, check that all ammos remain compatible with engine |
|
112 } |
|
113 |
138 |
114 #pragma mark - |
139 #pragma mark - |
115 -(IBAction) switchViews:(id) sender { |
140 -(IBAction) switchViews:(id) sender { |
116 UIButton *button = (UIButton *)sender; |
141 UIButton *button = (UIButton *)sender; |
117 UIAlertView *alert; |
142 UIAlertView *alert; |
118 NSString *debugStr; |
143 NSString *xib; |
119 |
144 |
120 switch (button.tag) { |
145 switch (button.tag) { |
121 case 0: |
146 case 0: |
122 if (nil == self.gameConfigViewController) { |
147 if (nil == self.gameConfigViewController) { |
123 GameConfigViewController *gcvc = [[GameConfigViewController alloc] initWithNibName:@"GameConfigViewController" bundle:nil]; |
148 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) |
|
149 xib = nil; |
|
150 else |
|
151 xib = @"GameConfigViewController"; |
|
152 |
|
153 GameConfigViewController *gcvc = [[GameConfigViewController alloc] initWithNibName:xib bundle:nil]; |
124 self.gameConfigViewController = gcvc; |
154 self.gameConfigViewController = gcvc; |
125 [gcvc release]; |
155 [gcvc release]; |
126 } |
156 } |
127 |
157 |
128 [self presentModalViewController:self.gameConfigViewController animated:YES]; |
158 [self presentModalViewController:self.gameConfigViewController animated:YES]; |