diff --git a/JGE/src/iOS/EAGLViewController.m b/JGE/src/iOS/EAGLViewController.m index 27ae3cd48..18b8c24d4 100755 --- a/JGE/src/iOS/EAGLViewController.m +++ b/JGE/src/iOS/EAGLViewController.m @@ -1,7 +1,6 @@ #import "EAGLViewController.h" #import "EAGLView.h" - @interface EAGLViewController (PrivateMethods) - (NSString*)interfaceOrientationName:(UIInterfaceOrientation) interfaceOrientation; - (NSString*)deviceOrientationName:(UIDeviceOrientation) deviceOrientation; @@ -94,16 +93,6 @@ [[eaglView adView] rotateToOrientation: toInterfaceOrientation]; } -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { - - UIDeviceOrientation currentDeviceOrientation = [UIDevice currentDevice].orientation; - UIInterfaceOrientation currentInterfaceOrientation = self.interfaceOrientation; - - NSLog(@"EAGL ViewController - did Rotate From Interface: %@. Current Interface: %@. Current Device: %@", - [self interfaceOrientationName:fromInterfaceOrientation], - [self interfaceOrientationName:currentInterfaceOrientation], - [self deviceOrientationName:currentDeviceOrientation]); -} #pragma mark - #pragma mark Orientation Information diff --git a/JGE/src/iOS/wagicAppDelegate.m b/JGE/src/iOS/wagicAppDelegate.m index b4a582ddf..6b37a0485 100755 --- a/JGE/src/iOS/wagicAppDelegate.m +++ b/JGE/src/iOS/wagicAppDelegate.m @@ -62,10 +62,24 @@ } -- (void) applicationDidFinishLaunching:(UIApplication *)application +- (void) setupNetworkListeners +{ + hostReach = [[Reachability reachabilityForGoogleDNS] retain]; + internetReach = [[Reachability reachabilityForInternetConnection] retain]; + wifiReach = [[Reachability reachabilityForLocalWiFi] retain]; + + [hostReach startNotifier]; + [internetReach startNotifier]; + [wifiReach startNotifier]; +} + + +- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.glViewController = nil; + [self setupNetworkListeners]; + NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; [dnc addObserver:self selector:@selector(startGame) name:@"readyToStartGame" object: nil]; @@ -82,6 +96,7 @@ [self startGame]; } + [self.window setBackgroundColor: [UIColor blackColor]]; [self.window makeKeyAndVisible]; } diff --git a/projects/mtg/iOS/Reachability/Reachability 2.0.4ddg/Reachability/Reachability.m b/projects/mtg/iOS/Reachability/Reachability 2.0.4ddg/Reachability/Reachability.m index 4082a4877..566f4882f 100644 --- a/projects/mtg/iOS/Reachability/Reachability 2.0.4ddg/Reachability/Reachability.m +++ b/projects/mtg/iOS/Reachability/Reachability 2.0.4ddg/Reachability/Reachability.m @@ -364,6 +364,25 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach } // reachabilityWithAddress +#define INADDR_GOOGLE_DNS (u_int32_t)0x08080808 /* 8.8.8.8 */ + + ++ (Reachability *) reachabilityForGoogleDNS { + + struct sockaddr_in googleDnsAddress; + bzero(&googleDnsAddress, sizeof(googleDnsAddress)); + googleDnsAddress.sin_len = sizeof(googleDnsAddress); + googleDnsAddress.sin_family = AF_INET; + googleDnsAddress.sin_addr.s_addr = htonl(INADDR_GOOGLE_DNS); + + Reachability *r = [self reachabilityWithAddress: &googleDnsAddress]; + + r.key = kInternetConnection; + + return r; +} + + + (Reachability *) reachabilityForInternetConnection { struct sockaddr_in zeroAddress; diff --git a/projects/mtg/iOS/UI/WagicDownloadProgressViewController.h b/projects/mtg/iOS/UI/WagicDownloadProgressViewController.h index ad5209b14..d830a50fb 100644 --- a/projects/mtg/iOS/UI/WagicDownloadProgressViewController.h +++ b/projects/mtg/iOS/UI/WagicDownloadProgressViewController.h @@ -8,7 +8,7 @@ #import -@interface WagicDownloadProgressViewController : UIViewController +@interface WagicDownloadProgressViewController : UIViewController { } diff --git a/projects/mtg/iOS/UI/WagicDownloadProgressViewController.m b/projects/mtg/iOS/UI/WagicDownloadProgressViewController.m index 8ab0dd60a..c80477fcb 100644 --- a/projects/mtg/iOS/UI/WagicDownloadProgressViewController.m +++ b/projects/mtg/iOS/UI/WagicDownloadProgressViewController.m @@ -22,10 +22,30 @@ static NSString *kDownloadUrlPath = @"http://forevernow.net/wagic/"; static NSString *kDownloadFileName = @"core_017_iOS.zip"; + +- (void) handleFailedDownload: (NSNotification *) sender +{ + NSString *downloadType = [sender object]; + // figure out what to do. There could be other types of downloads, for now there is only "core" + if ( [downloadType isEqualToString: @"core"] ) + { + NSLog(@"Download Core files failed. Retrying... "); + [self.downloadMessageStatus setText: @"Download Core files failed. Retrying... "]; + + UIAlertView *noNetworkConnectionAlert = [[UIAlertView alloc] initWithTitle: @"No Network Connection" message: @"Internet connection not found. Download can not continue until it is restored. Restore Wifi or Cellular connection and retry" delegate: self cancelButtonTitle: @"Retry Download" otherButtonTitles: nil]; + [self.view addSubview: noNetworkConnectionAlert]; + [noNetworkConnectionAlert show]; + [noNetworkConnectionAlert release]; + } + +} + + - (void) unpackageResources { + [self.downloadMessageStatus setText: @"Installing Game Resource Files"]; NSError *error = nil; - + NSFileManager *fm = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); @@ -39,6 +59,7 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip"; if (ret == NO) { // some error occurred + NSLog(@"An Error occurred while unpacking zip file."); } [za UnzipCloseFile]; @@ -50,22 +71,88 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip"; { NSLog(@"error occurred while trying to delete zip file! %@\n%@", downloadFilePath, [error localizedDescription] ); } - else - { - wagicAppDelegate *appDelegate = (wagicAppDelegate *)[[UIApplication sharedApplication] delegate]; - - NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; - [dnc postNotificationName:@"readyToStartGame" object: appDelegate]; - } } } [za release], za = nil; + } +- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { + // the user clicked one of the OK/Cancel buttons + if (buttonIndex == 0 && [[actionSheet buttonTitleAtIndex: 0] isEqualToString: @"Retry Download"]) + { + [self startDownload: @"core"]; + } + else + { + NSLog(@"cancel"); + } +} + +- (void) startDownload: (NSString *) downloadType +{ + wagicAppDelegate *appDelegate = [(wagicAppDelegate *)[UIApplication sharedApplication] delegate]; + NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, + NSUserDomainMask, YES); + NSString *userResourceDirectory = [[paths objectAtIndex:0] stringByAppendingString: @"/Res"]; + NSString *downloadFilePath = [userResourceDirectory stringByAppendingString: [NSString stringWithFormat: @"/%@", kDownloadFileName]]; + NSError *error = nil; + // make sure Res directory exists + if ( ![[NSFileManager defaultManager] fileExistsAtPath: userResourceDirectory] ) + [[NSFileManager defaultManager] createDirectoryAtPath:userResourceDirectory withIntermediateDirectories: YES attributes:nil error: &error]; + + // if an error occurred while creating the directory, game can't really run so do something + // TODO: throw out a notification and deal with error + + NSURL *url = nil; + // determine which file to download + if ([downloadType isEqualToString: @"core"]) + { + url = [NSURL URLWithString: [NSString stringWithFormat: @"%@/%@", kDownloadUrlPath, kDownloadFileName]]; + } + else if ( [downloadType isEqualToString: @"someOtherType"] ) + { + NSLog( @"Not Implemented for type: %@", downloadType); + } + + __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; + [request setTemporaryFileDownloadPath: [NSString stringWithFormat: @"%@.tmp", userResourceDirectory]]; + + [request setDownloadDestinationPath: downloadFilePath]; + [request setDownloadProgressDelegate: downloadProgressView]; + [request setShouldContinueWhenAppEntersBackground: YES]; + [request setAllowCompressedResponse: YES]; + + [request setCompletionBlock:^{ + [self unpackageResources]; + wagicAppDelegate *appDelegate = (wagicAppDelegate *)[[UIApplication sharedApplication] delegate]; + NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; + [dnc postNotificationName:@"readyToStartGame" object: appDelegate]; + + }]; + [request setFailedBlock:^{ + // if there is an error in downloading the package what do we do? + // TODO: do something useful if there's an error in the download + // perhaps restart the download + + NSError *error = [request error]; + NSString *errorMessage = [NSString stringWithFormat: @"There was an error in downloading your files. %@", [error localizedDescription]]; + NSLog(@"Error with download: %@", errorMessage); + // post a notification that a download error has occurred. + NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; + [dnc postNotificationName:@"fileDownloadFailed" object: downloadType]; + + }]; + + [request startAsynchronous]; +} - (id) init { + NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; + [dnc addObserver: self selector: @selector(handleFailedDownload:) name: @"fileDownloadFailed" object: nil]; + self = [super init]; if (self) { bool isPhone = (UI_USER_INTERFACE_IDIOM()) == UIUserInterfaceIdiomPhone; @@ -96,26 +183,7 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip"; [self.downloadProgressView setFrame: CGRectMake(0, 0, 250, 50)]; [self.downloadProgressView setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ]; - NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, - NSUserDomainMask, YES); - NSString *userResourceDirectory = [[paths objectAtIndex:0] stringByAppendingString: @"/Res"]; - NSString *downloadFilePath = [[paths objectAtIndex: 0] stringByAppendingString: [NSString stringWithFormat: @"/%@", kDownloadFileName]]; - - // download the zip file but show a splash screen - NSURL *url = [NSURL URLWithString: [NSString stringWithFormat: @"%@/%@", kDownloadUrlPath, kDownloadFileName]]; - __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setTemporaryFileDownloadPath: [NSString stringWithFormat: @"%@.tmp", userResourceDirectory]]; - [request setDownloadDestinationPath: downloadFilePath]; - [request setDownloadProgressDelegate: downloadProgressView]; - - [request setCompletionBlock:^{ - [self unpackageResources]; - }]; - [request setFailedBlock:^{ - NSError *error = [request error]; - }]; - - [request startAsynchronous]; + [self startDownload: @"core"]; [self.view addSubview: downloadMessageStatus]; [self.view addSubview: downloadProgressView]; @@ -126,6 +194,7 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip"; #pragma mark Application Lifecycle + - (void) didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. @@ -183,8 +252,14 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip"; { [super viewDidLoad]; [self handleRotation: self.interfaceOrientation]; + } +- (void) viewDidUnload +{ + NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; + [dnc removeObserver: self name: @"coreFileDownloadFailed" object: nil]; +} // Only allow auto rotation on iPads. - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation diff --git a/projects/mtg/wagic.xcodeproj/project.pbxproj b/projects/mtg/wagic.xcodeproj/project.pbxproj index 6f656099d..d1d2e1375 100755 --- a/projects/mtg/wagic.xcodeproj/project.pbxproj +++ b/projects/mtg/wagic.xcodeproj/project.pbxproj @@ -306,12 +306,12 @@ 12211EBA14934A2C00641703 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; }; 12211EC71494360C00641703 /* WagicDownloadProgressViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WagicDownloadProgressViewController.h; path = UI/WagicDownloadProgressViewController.h; sourceTree = ""; }; 12211EC81494360C00641703 /* WagicDownloadProgressViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = WagicDownloadProgressViewController.m; path = UI/WagicDownloadProgressViewController.m; sourceTree = ""; }; - 12211ECA1494811D00641703 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = ""; }; - 12211ECC149481B600641703 /* Default-Landscape~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-Landscape~ipad.png"; sourceTree = ""; }; - 12211ECE149481C300641703 /* Default-Portrait~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-Portrait~ipad.png"; sourceTree = ""; }; - 12211ED01494823E00641703 /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = ""; }; - 12211ED21494916D00641703 /* Default-Landscape.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-Landscape.png"; sourceTree = ""; }; - 12211ED31494916D00641703 /* Default-Portrait.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-Portrait.png"; sourceTree = ""; }; + 12211ECA1494811D00641703 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Default.png; path = ../Default.png; sourceTree = ""; }; + 12211ECC149481B600641703 /* Default-Landscape~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-Landscape~ipad.png"; path = "../Default-Landscape~ipad.png"; sourceTree = ""; }; + 12211ECE149481C300641703 /* Default-Portrait~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-Portrait~ipad.png"; path = "../Default-Portrait~ipad.png"; sourceTree = ""; }; + 12211ED01494823E00641703 /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default@2x.png"; path = "../Default@2x.png"; sourceTree = ""; }; + 12211ED21494916D00641703 /* Default-Landscape.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-Landscape.png"; path = "../Default-Landscape.png"; sourceTree = ""; }; + 12211ED31494916D00641703 /* Default-Portrait.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-Portrait.png"; path = "../Default-Portrait.png"; sourceTree = ""; }; 12769483144127380088F6D3 /* AIPlayerBaka.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AIPlayerBaka.cpp; sourceTree = ""; }; 12769484144127380088F6D3 /* AIPlayerBakaB.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AIPlayerBakaB.cpp; sourceTree = ""; }; 12769485144127380088F6D3 /* TestSuiteAI.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestSuiteAI.cpp; sourceTree = ""; }; @@ -855,8 +855,6 @@ children = ( CE9A477512B514BA00C9F38A /* EAGLView.h */, CE9A477612B514BA00C9F38A /* EAGLView.m */, - 12211EC71494360C00641703 /* WagicDownloadProgressViewController.h */, - 12211EC81494360C00641703 /* WagicDownloadProgressViewController.m */, ); name = UI; sourceTree = ""; @@ -864,6 +862,11 @@ 12211EC6149435A300641703 /* Controllers */ = { isa = PBXGroup; children = ( + 12211EC71494360C00641703 /* WagicDownloadProgressViewController.h */, + 12211EC81494360C00641703 /* WagicDownloadProgressViewController.m */, + CE9A477C12B514BA00C9F38A /* ES2Renderer.h */, + CE9A477D12B514BA00C9F38A /* ES2Renderer.m */, + CE9A477E12B514BA00C9F38A /* ESRenderer.h */, CE9A477712B514BA00C9F38A /* EAGLViewController.h */, CE9A477812B514BA00C9F38A /* EAGLViewController.m */, ); @@ -1039,9 +1042,25 @@ path = JSON; sourceTree = ""; }; + 128FB96A149537A600ED4EE6 /* Images */ = { + isa = PBXGroup; + children = ( + 12211ED21494916D00641703 /* Default-Landscape.png */, + 12211ED31494916D00641703 /* Default-Portrait.png */, + 12211ED01494823E00641703 /* Default@2x.png */, + 12211ECE149481C300641703 /* Default-Portrait~ipad.png */, + 12211ECC149481B600641703 /* Default-Landscape~ipad.png */, + 12211ECA1494811D00641703 /* Default.png */, + ); + name = Images; + sourceTree = ""; + }; 129654D2148AA2390031100B /* iOS */ = { isa = PBXGroup; children = ( + CE9A478312B514BA00C9F38A /* wagicAppDelegate.h */, + CE9A478412B514BA00C9F38A /* wagicAppDelegate.m */, + 128FB96A149537A600ED4EE6 /* Images */, 12211EC6149435A300641703 /* Controllers */, 12211EC51494358300641703 /* UI */, 129654D3148AA2390031100B /* Res */, @@ -1118,28 +1137,15 @@ 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( - 12211ED21494916D00641703 /* Default-Landscape.png */, - 12211ED31494916D00641703 /* Default-Portrait.png */, - 12211ED01494823E00641703 /* Default@2x.png */, - 12211ECE149481C300641703 /* Default-Portrait~ipad.png */, - 12211ECC149481B600641703 /* Default-Landscape~ipad.png */, - 12211ECA1494811D00641703 /* Default.png */, - 12211EBA14934A2C00641703 /* CFNetwork.framework */, - 12211EB814934A1800641703 /* MobileCoreServices.framework */, - 12211EB01493449A00641703 /* Networking Packages */, 129654D2148AA2390031100B /* iOS */, - 125C5CF213B09AA200DF2F2C /* Tools */, - CE9E71DB1375A53600759DDC /* Boost */, - CE9A477C12B514BA00C9F38A /* ES2Renderer.h */, - CE9A477D12B514BA00C9F38A /* ES2Renderer.m */, - CE9A477E12B514BA00C9F38A /* ESRenderer.h */, - CE9A477F12B514BA00C9F38A /* main.m */, - CE9A478112B514BA00C9F38A /* wagic_Prefix.pch */, - CE9A478312B514BA00C9F38A /* wagicAppDelegate.h */, - CE9A478412B514BA00C9F38A /* wagicAppDelegate.m */, CEA376841291C5CD00B9016A /* Wagic */, CEE2320D128A00EC00C34032 /* JGE */, 29B97317FDCFA39411CA2CEA /* Resources */, + 12211EB01493449A00641703 /* Networking Packages */, + CE9E71DB1375A53600759DDC /* Boost */, + CE9A477F12B514BA00C9F38A /* main.m */, + CE9A478112B514BA00C9F38A /* wagic_Prefix.pch */, + 125C5CF213B09AA200DF2F2C /* Tools */, 29B97323FDCFA39411CA2CEA /* Frameworks */, 19C28FACFE9D520D11CA2CBB /* Products */, ); @@ -1162,6 +1168,8 @@ 29B97323FDCFA39411CA2CEA /* Frameworks */ = { isa = PBXGroup; children = ( + 12211EBA14934A2C00641703 /* CFNetwork.framework */, + 12211EB814934A1800641703 /* MobileCoreServices.framework */, 1216D632148F7411000F2295 /* libc++abi.dylib */, 128ED518148BF0E000C58E83 /* MediaPlayer.framework */, 128ED50F148BCC1900C58E83 /* libsqlite3.dylib */, @@ -1914,6 +1922,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = wagic_Prefix.pch; GCC_PREPROCESSOR_DEFINITIONS = ( + FT2_BUILD_LIBRARY, IOS, DEBUG, TESTSUITE,