Browse Source

Fixed bug where FileSystem::fileExists was creating an empty directory on Android.
Updated CHANGES.md

Darryl Gough 13 years ago
parent
commit
86d7d04085
2 changed files with 40 additions and 4 deletions
  1. 4 0
      CHANGES.md
  2. 36 4
      gameplay/src/FileSystem.cpp

+ 4 - 0
CHANGES.md

@@ -1,3 +1,7 @@
+## v1.6.0
+- Adds Stream interface for reading/writing files instead of using fread/fwrite. 
+- Android no longer needs to copy files to the SD card before reading them. None of the Android samples require an SD card.
+
 ## v1.5.0
 
 - Linux support. (tested on Ubuntu 12)

+ 36 - 4
gameplay/src/FileSystem.cpp

@@ -29,7 +29,7 @@ namespace gameplay
 #ifdef __ANDROID__
 #include <unistd.h>
 
-void makepath(std::string path, int mode)
+static void makepath(std::string path, int mode)
 {
     std::vector<std::string> dirs;
     while (path.length() > 0)
@@ -64,6 +64,22 @@ void makepath(std::string path, int mode)
     
     return;
 }
+
+/**
+ * Returns true if the file exists in the android read-only asset directory.
+ */
+static bool androidFileExists(const char* filePath)
+{
+    AAsset* asset = AAssetManager_open(__assetManager, filePath, AASSET_MODE_RANDOM);
+    if (asset)
+    {
+        int lenght = AAsset_getLength(asset);
+        AAsset_close(asset);
+        return length > 0;
+    }
+    return false;
+}
+
 #endif
 
 /** @script{ignore} */
@@ -274,11 +290,16 @@ bool FileSystem::fileExists(const char* filePath)
 {
     GP_ASSERT(filePath);
 
+#ifdef __ANDROID__
+    if (androidFileExists(filePath))
+    {
+        return true;
+    }
+#endif
+
     std::string fullPath(__resourcePath);
     fullPath += resolvePath(filePath);
 
-    createFileFromAsset(filePath);
-
     gp_stat_struct s;
 
 #ifdef WIN32
@@ -309,7 +330,18 @@ Stream* FileSystem::open(const char* path, size_t mode)
     if ((mode & WRITE) != 0)
         modeStr[0] = 'w';
 #ifdef __ANDROID__
-    return FileStreamAndroid::create(resolvePath(path), modeStr);
+    if ((mode & WRITE) != 0)
+    {
+        // Open a file on the SD card
+        std::string fullPath(__resourcePath);
+        fullPath += resolvePath(path);
+        return FileStreamAndroid::create(fullPath.c_str(), modeStr);
+    }
+    else
+    {
+        // Open a file in the read-only asset directory
+        return FileStreamAndroid::create(resolvePath(path), modeStr);
+    }
 #else
     std::string fullPath(__resourcePath);
     fullPath += resolvePath(path);