diff --git a/files/utils.cc b/files/utils.cc index 764f95105..e39cfce35 100644 --- a/files/utils.cc +++ b/files/utils.cc @@ -792,9 +792,14 @@ void setup_data_dir(const std::string& data_path, const char* runpath) { if (is_system_path_defined("")) { // We have the flxfiles in the bundle, so lets use it. // But lets use in the iTunes file sharing. - string path(IOSGetDocumentsDir()); + bool has_data = false; + string path(IOSGetDocumentsDir(&has_data)); path += "/data"; add_system_path("", path); + if(!has_data) { + string source_data_dir = "./data/game"; + copy_data_to_documents_dir(source_data_dir.c_str()); + } return; } #endif diff --git a/ios/include/ios_utils.h b/ios/include/ios_utils.h index c6dca6b62..6093e332c 100644 --- a/ios/include/ios_utils.h +++ b/ios/include/ios_utils.h @@ -34,6 +34,7 @@ class TouchUI_iOS : public TouchUI { void onDpadLocationChanged() final; }; -const char* IOSGetDocumentsDir(); +const char* IOSGetDocumentsDir(bool * = NULL); +void copy_data_to_documents_dir(const char *); #endif diff --git a/ios/ios_utils.mm b/ios/ios_utils.mm index d18e3db96..0241d9197 100644 --- a/ios/ios_utils.mm +++ b/ios/ios_utils.mm @@ -274,15 +274,45 @@ - (void)hideButtonControls { /* ---------------------------------------------------------------------- */ -const char* IOSGetDocumentsDir() { +const char* IOSGetDocumentsDir(bool * hasData) { + NSString *docDirectory = nil; if (gDocsDir[0] == 0) { NSArray* paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); - NSString* docDirectory = [paths objectAtIndex:0]; + docDirectory = [paths objectAtIndex:0]; strcpy(gDocsDir, docDirectory.UTF8String); printf("Documents: %s\n", gDocsDir); // chdir(gDocsDir); // *strncpy(gDocsDir, , sizeof(gDocsDir) - 1) = 0; } + if (hasData) { + if(!docDirectory) { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + docDirectory = [paths objectAtIndex:0]; + } + + BOOL isDir = NO; + if([[NSFileManager defaultManager] fileExistsAtPath:[docDirectory stringByAppendingPathComponent:@"game"] isDirectory:&isDir]) { + if(isDir) { + *hasData = YES; + return gDocsDir; + } + } + *hasData = NO; + } return gDocsDir; } + +void copy_data_to_documents_dir(const char * path) +{ + bool has_data = false; + NSString * src = [NSString stringWithUTF8String:path]; + NSString * dest = [NSString stringWithUTF8String:IOSGetDocumentsDir(&has_data)]; + NSError * err = nil; + BOOL result = [[NSFileManager defaultManager] copyItemAtPath:src toPath:[dest stringByAppendingPathComponent:@"game"] error:&err]; + if(!result) { + NSLog(@"Error copying data to documents directory: %@", err); + } else { + NSLog(@"Copied game data to documents directory."); + } +} \ No newline at end of file