finishing up some file system handling functions for iOS. This should make updates and file management easier for iOS users.

This commit is contained in:
techdragon.nguyen@gmail.com
2012-01-05 09:29:53 +00:00
parent f09ce07b06
commit 29009902b0
4 changed files with 138 additions and 56 deletions

View File

@@ -16,6 +16,8 @@
- (void) handleWEngineCommand:(NSString *) command withParameter: (NSString *) parameter; - (void) handleWEngineCommand:(NSString *) command withParameter: (NSString *) parameter;
- (void) handleWEngineCommand:(NSString *) command withUIParameters: (CGFloat) x yCoordinate: (CGFloat) y width: (CGFloat) width height: (CGFloat) height; - (void) handleWEngineCommand:(NSString *) command withUIParameters: (CGFloat) x yCoordinate: (CGFloat) y width: (CGFloat) width height: (CGFloat) height;
- (void) unpackageResources: (NSString *) folderName pathPrefixName: (NSString *) pathPrefixName;
@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) EAGLViewController *glViewController; @property (nonatomic, retain) EAGLViewController *glViewController;
@property (nonatomic, retain) WagicDownloadProgressViewController *wagicDownloadController; @property (nonatomic, retain) WagicDownloadProgressViewController *wagicDownloadController;

View File

@@ -11,16 +11,60 @@
@synthesize wagicDownloadController; @synthesize wagicDownloadController;
@synthesize hostReach, wifiReach, internetReach; @synthesize hostReach, wifiReach, internetReach;
- (void) downloadResources - (void) downloadResources
{ {
wagicDownloadController = [[WagicDownloadProgressViewController alloc] init]; wagicDownloadController = [[WagicDownloadProgressViewController alloc] init];
[wagicDownloadController startDownload: @"core"];
[self.window addSubview: wagicDownloadController.view]; [self.window addSubview: wagicDownloadController.view];
[self.window makeKeyWindow]; [self.window makeKeyWindow];
} }
/* unpackage the zip file and delete it afterwards */
- (void) unpackageResources: (NSString *) folderName pathPrefixName: (NSString *) pathNamePrefix
{
NSString *pathPrefix = nil;
if ( pathNamePrefix == nil ) // default to User
pathPrefix = @"User";
else
pathPrefix = pathNamePrefix;
NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *userDocumentsDirectory = [paths objectAtIndex:0];
NSString *downloadFilePath = [userDocumentsDirectory stringByAppendingString: [NSString stringWithFormat: @"/%@/%@.zip", pathPrefix, folderName]];
ZipArchive *za = [[ZipArchive alloc] init];
if ([za UnzipOpenFile: downloadFilePath])
{
NSString *destinationFilePath = [NSString stringWithFormat: @"%@/%@/",userDocumentsDirectory, pathPrefix];
NSLog(@"Unzipping %@ to %@", downloadFilePath, destinationFilePath);
BOOL ret = [za UnzipFileTo: destinationFilePath overWrite: YES];
if (ret == NO)
{
// some error occurred
NSLog(@"An Error occurred while unpacking zip file.");
}
[za UnzipCloseFile];
if (ret == YES)
{
// delete the archive
[fm removeItemAtPath: downloadFilePath error: &error];
if (error != nil)
{
NSLog(@"error occurred while trying to delete zip file! %@\n%@", downloadFilePath, [error localizedDescription] );
}
}
}
[za release], za = nil;
}
- (NSString *) getDirContents: (NSString *) path - (NSString *) getDirContents: (NSString *) path
{ {
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -50,12 +94,12 @@
} }
/** /**
check for any zip files dropped into the documents directory. If so move them into the "User" directory. check for any zip files dropped into the documents directory before loading the game.
check for a "core" zip file in the Res directory. If it exists, then return YES. Otherwise, return NO. If so, move the "core" files into the "Res" directory and move all other zip files into the "User" directory.
Check for a "core" zip file in the Res directory. If it exists, then return YES. Otherwise, return NO.
*/ */
- (void) initializeResources
- (BOOL) hasResourceFiles
{ {
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -64,6 +108,8 @@
NSArray *docsPathContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: docsPath error:nil]; NSArray *docsPathContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: docsPath error:nil];
NSCompoundPredicate *compoundPredicate = [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType subpredicates: [NSArray arrayWithObjects: [NSPredicate predicateWithFormat:@"self ENDSWITH '.zip'"], [NSPredicate predicateWithFormat:@"NOT (self BEGINSWITH 'core_')"], nil]]; NSCompoundPredicate *compoundPredicate = [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType subpredicates: [NSArray arrayWithObjects: [NSPredicate predicateWithFormat:@"self ENDSWITH '.zip'"], [NSPredicate predicateWithFormat:@"NOT (self BEGINSWITH 'core_')"], nil]];
NSArray *coreFiles = [docsPathContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'core_'"]];
NSArray *resourceZipFiles = [docsPathContents filteredArrayUsingPredicate: compoundPredicate]; NSArray *resourceZipFiles = [docsPathContents filteredArrayUsingPredicate: compoundPredicate];
NSString *userPath = [NSString stringWithFormat: @"%@/User", docsPath]; NSString *userPath = [NSString stringWithFormat: @"%@/User", docsPath];
NSString *resPath = [NSString stringWithFormat: @"%@/Res", docsPath]; NSString *resPath = [NSString stringWithFormat: @"%@/Res", docsPath];
@@ -78,17 +124,38 @@
NSString *newPath = [NSString stringWithFormat: @"%@/%@", userPath, zipFile]; NSString *newPath = [NSString stringWithFormat: @"%@/%@", userPath, zipFile];
[fileManager moveItemAtPath: oldPath toPath:newPath error: &error]; [fileManager moveItemAtPath: oldPath toPath:newPath error: &error];
NSLog(@"Moving %@ to %@", oldPath, newPath);
if ( error != nil ) if ( error != nil )
{ {
NSLog(@"Error happened while trying to move %@ to %@: \n%@", oldPath, newPath, [error localizedDescription]); NSLog(@"Error happened while trying to move %@ to %@: \n%@", oldPath, newPath, [error localizedDescription]);
error = nil; error = nil;
} }
} }
for (NSString *zipFile in coreFiles)
{
NSString *oldPath = [NSString stringWithFormat: @"%@/%@", docsPath, zipFile];
NSString *newPath = [NSString stringWithFormat: @"%@/%@", resPath, zipFile];
[fileManager moveItemAtPath: oldPath toPath:newPath error: &error];
NSLog(@"Moving %@ to %@", oldPath, newPath);
if ( error != nil )
{
NSLog(@"Error happened while trying to move %@ to %@: \n%@", oldPath, newPath, [error localizedDescription]);
error = nil;
}
}
}
- (BOOL) hasResourceFiles
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex: 0];
NSString *resPath = [NSString stringWithFormat: @"%@/Res", docsPath];
NSArray *resDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: resPath error:nil]; NSArray *resDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: resPath error:nil];
NSArray *coreFiles = [resDirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'core_'"]]; NSArray *coreFiles = [resDirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'core_'"]];
[self createManifest: docsPath];
if ([coreFiles count] > 0) if ([coreFiles count] > 0)
{ {
return YES; return YES;
@@ -98,6 +165,53 @@
} }
- (void)preGameInitialization
{
NSString *docsPath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSFileManager *fm = [NSFileManager defaultManager];
NSString *userPath = [docsPath stringByAppendingString: @"/User"];
NSArray *userDirectories = [fm contentsOfDirectoryAtPath: userPath error: nil];
for (NSString *userFilename in userDirectories)
{
NSString *userPathname = [userPath stringByAppendingFormat: @"/%@", userFilename];
NSString *zipFileName = [userPathname stringByAppendingString: @".zip"];
if ([[fm attributesOfItemAtPath: userPathname error: nil] objectForKey: NSFileType] == NSFileTypeDirectory && ([fm fileExistsAtPath:zipFileName]))
{
[self unpackageResources: userFilename pathPrefixName: @"User"];
[fm removeItemAtPath: zipFileName error: nil];
}
else if ( [userFilename hasPrefix: @"ai_decks"] ) // special case to allow manual override of AI decks in User directory
{
[self unpackageResources: @"ai_decks" pathPrefixName: @"User/ai/baka"];
}
}
// scan for deck*.txt and collection.dat as well as options.txt in the Documents directory and copy them into the player directory
NSArray *playerDataFilePredicates = [NSArray arrayWithObjects:
[NSPredicate predicateWithFormat: @"SELF BEGINSWITH[cd] 'deck' AND SELF ENDSWITH '.txt'"],
[NSPredicate predicateWithFormat: @"SELF BEGINSWITH[cd] 'options' AND SELF ENDSWITH '.txt'"],
[NSPredicate predicateWithFormat: @"SELF BEGINSWITH[cd] 'tasks' AND SELF ENDSWITH '.dat'"],
[NSPredicate predicateWithFormat: @"SELF BEGINSWITH[cd] 'collection' AND SELF ENDSWITH '.dat'"], nil];
NSCompoundPredicate *playerDataPredicate = [[NSCompoundPredicate alloc] initWithType:NSOrPredicateType subpredicates: playerDataFilePredicates];
NSArray *playerDataFiles = [[fm contentsOfDirectoryAtPath: docsPath error: nil] filteredArrayUsingPredicate: playerDataPredicate];
for (NSString *file in playerDataFiles)
{
NSString *fromPath = [docsPath stringByAppendingFormat: @"/%@", file];
NSString *toPath = [docsPath stringByAppendingFormat: @"/User/player/%@", [file lowercaseString]];
[fm moveItemAtPath: fromPath toPath: toPath error: nil];
}
[self createManifest: docsPath];
[[NSNotificationCenter defaultCenter] postNotificationName: @"readyToStartGame" object: nil];
}
- (void) startGame - (void) startGame
{ {
if (glViewController != nil) if (glViewController != nil)
@@ -110,6 +224,7 @@
[self.window addSubview:self.glViewController.view]; [self.window addSubview:self.glViewController.view];
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc removeObserver: self name: @"intializeGame" object: nil];
[dnc removeObserver: self name: @"readyToStartGame" object: nil]; [dnc removeObserver: self name: @"readyToStartGame" object: nil];
} }
@@ -145,8 +260,10 @@
[self setupNetworkListeners]; [self setupNetworkListeners];
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self selector:@selector(preGameInitialization) name:@"initializeGame" object: nil];
[dnc addObserver:self selector:@selector(startGame) name:@"readyToStartGame" object: nil]; [dnc addObserver:self selector:@selector(startGame) name:@"readyToStartGame" object: nil];
[self initializeResources];
// check to see if the Res folder exists. If it does continue // check to see if the Res folder exists. If it does continue
// otherwise bring up the download dialog and download the core files // otherwise bring up the download dialog and download the core files
// once downloaded, extract the files and kick off the game. // once downloaded, extract the files and kick off the game.
@@ -157,7 +274,7 @@
} }
else else
{ {
[self startGame]; [self preGameInitialization];
} }
[self.window setBackgroundColor: [UIColor blackColor]]; [self.window setBackgroundColor: [UIColor blackColor]];
@@ -233,7 +350,6 @@
} }
else if ([command isEqualToString: @"combatGuiEndDamage"]) else if ([command isEqualToString: @"combatGuiEndDamage"])
{ {
[glViewController removeOkButtonListener];
} }
} }

View File

@@ -13,6 +13,8 @@
} }
- (void) startDownload: (NSString *) downloadType;
@property (nonatomic, retain) UITextView *downloadMessageStatus; @property (nonatomic, retain) UITextView *downloadMessageStatus;
@property (nonatomic, retain) UIProgressView *downloadProgressView; @property (nonatomic, retain) UIProgressView *downloadProgressView;

View File

@@ -8,7 +8,6 @@
#import "WagicDownloadProgressViewController.h" #import "WagicDownloadProgressViewController.h"
#import "wagicAppDelegate.h" #import "wagicAppDelegate.h"
#import "ZipArchive.h"
#import "ASIHTTPRequest.h" #import "ASIHTTPRequest.h"
#import "QuartzCore/QuartzCore.h" #import "QuartzCore/QuartzCore.h"
@@ -39,43 +38,6 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip";
} }
// No longer needed.
- (void) unpackageResources: (NSString *) folderName
{
[self.downloadMessageStatus setText: [NSString stringWithFormat: @"Updating User Game Resource Files: %@", folderName]];
NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *userDocumentsDirectory = [paths objectAtIndex:0];
NSString *downloadFilePath = [userDocumentsDirectory stringByAppendingString: [NSString stringWithFormat: @"/%@.zip", folderName]];
ZipArchive *za = [[ZipArchive alloc] init];
if ([za UnzipOpenFile: downloadFilePath])
{
BOOL ret = [za UnzipFileTo: [NSString stringWithFormat: @"%@/User/",userDocumentsDirectory] overWrite: YES];
if (ret == NO)
{
// some error occurred
NSLog(@"An Error occurred while unpacking zip file.");
}
[za UnzipCloseFile];
if (ret == YES)
{
// delete the archive
[fm removeItemAtPath: downloadFilePath error: &error];
if (error != nil)
{
NSLog(@"error occurred while trying to delete zip file! %@\n%@", downloadFilePath, [error localizedDescription] );
}
}
}
[za release], za = nil;
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons // the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0 && [[actionSheet buttonTitleAtIndex: 0] isEqualToString: @"Retry Download"]) if (buttonIndex == 0 && [[actionSheet buttonTitleAtIndex: 0] isEqualToString: @"Retry Download"])
@@ -90,6 +52,14 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip";
- (void) startDownload: (NSString *) downloadType - (void) startDownload: (NSString *) downloadType
{ {
[self.downloadMessageStatus setText: [NSString stringWithFormat: @"Please wait while the %@ files are being downloaded.", downloadType]];
downloadProgressView = [[UIProgressView alloc] initWithProgressViewStyle: UIProgressViewStyleDefault];
[self.downloadProgressView setFrame: CGRectMake(0, 0, 250, 50)];
[self.downloadProgressView setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
[self.view addSubview: downloadProgressView];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES); NSUserDomainMask, YES);
NSString *systemResourceDirectory = [[paths objectAtIndex:0] stringByAppendingString: @"/Res"]; NSString *systemResourceDirectory = [[paths objectAtIndex:0] stringByAppendingString: @"/Res"];
@@ -128,7 +98,7 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip";
[request setCompletionBlock:^{ [request setCompletionBlock:^{
wagicAppDelegate *appDelegate = (wagicAppDelegate *)[[UIApplication sharedApplication] delegate]; wagicAppDelegate *appDelegate = (wagicAppDelegate *)[[UIApplication sharedApplication] delegate];
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc postNotificationName:@"readyToStartGame" object: appDelegate]; [dnc postNotificationName:@"initializeGame" object: appDelegate];
}]; }];
[request setFailedBlock:^{ [request setFailedBlock:^{
@@ -177,15 +147,7 @@ static NSString *kDownloadFileName = @"core_017_iOS.zip";
else else
[self.downloadMessageStatus setFont: [UIFont systemFontOfSize: 35]]; [self.downloadMessageStatus setFont: [UIFont systemFontOfSize: 35]];
[self.downloadMessageStatus setText: @"Please wait while the core files are being downloaded."];
downloadProgressView = [[UIProgressView alloc] initWithProgressViewStyle: UIProgressViewStyleDefault];
[self.downloadProgressView setFrame: CGRectMake(0, 0, 250, 50)];
[self.downloadProgressView setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
[self startDownload: @"core"];
[self.view addSubview: downloadMessageStatus]; [self.view addSubview: downloadMessageStatus];
[self.view addSubview: downloadProgressView];
} }
return self; return self;