Forráskód Böngészése

Merge pull request #1609 from glassechidna/android-filesystem

Fix Android platform filesystem usage inconsistencies
Sean Taylor 11 éve
szülő
commit
f651b29e8a
2 módosított fájl, 51 hozzáadás és 8 törlés
  1. 34 8
      gameplay/src/FileSystem.cpp
  2. 17 0
      gameplay/src/FileSystem.h

+ 34 - 8
gameplay/src/FileSystem.cpp

@@ -87,6 +87,7 @@ static bool androidFileExists(const char* filePath)
 
 
 /** @script{ignore} */
 /** @script{ignore} */
 static std::string __resourcePath("./");
 static std::string __resourcePath("./");
+static std::string __assetPath("");
 static std::map<std::string, std::string> __aliases;
 static std::map<std::string, std::string> __aliases;
 
 
 /**
 /**
@@ -343,14 +344,18 @@ bool FileSystem::fileExists(const char* filePath)
 {
 {
     GP_ASSERT(filePath);
     GP_ASSERT(filePath);
 
 
+    std::string fullPath;
+
 #ifdef __ANDROID__
 #ifdef __ANDROID__
-    if (androidFileExists(resolvePath(filePath)))
+    fullPath = __assetPath;
+    fullPath += resolvePath(filePath);
+
+    if (androidFileExists(fullPath.c_str()))
     {
     {
         return true;
         return true;
     }
     }
 #endif
 #endif
 
 
-    std::string fullPath;
     getFullPath(filePath, fullPath);
     getFullPath(filePath, fullPath);
 
 
     gp_stat_struct s;
     gp_stat_struct s;
@@ -364,17 +369,17 @@ Stream* FileSystem::open(const char* path, size_t streamMode)
     if ((streamMode & WRITE) != 0)
     if ((streamMode & WRITE) != 0)
         modeStr[0] = 'w';
         modeStr[0] = 'w';
 #ifdef __ANDROID__
 #ifdef __ANDROID__
+    std::string fullPath(__resourcePath);
+    fullPath += resolvePath(path);
+
     if ((streamMode & WRITE) != 0)
     if ((streamMode & WRITE) != 0)
     {
     {
         // Open a file on the SD card
         // Open a file on the SD card
-        std::string fullPath(__resourcePath);
-        fullPath += resolvePath(path);
-
         size_t index = fullPath.rfind('/');
         size_t index = fullPath.rfind('/');
         if (index != std::string::npos)
         if (index != std::string::npos)
         {
         {
             std::string directoryPath = fullPath.substr(0, index);
             std::string directoryPath = fullPath.substr(0, index);
-            struct stat s;
+            gp_stat_struct s;
             if (stat(directoryPath.c_str(), &s) != 0)
             if (stat(directoryPath.c_str(), &s) != 0)
                 makepath(directoryPath, 0777);
                 makepath(directoryPath, 0777);
         }
         }
@@ -382,8 +387,19 @@ Stream* FileSystem::open(const char* path, size_t streamMode)
     }
     }
     else
     else
     {
     {
-        // Open a file in the read-only asset directory
-        return FileStreamAndroid::create(resolvePath(path), modeStr);
+        // First try the SD card
+        Stream* stream = FileStream::create(fullPath.c_str(), modeStr);
+
+        if (!stream)
+        {
+            // Otherwise fall-back to assets loaded via the AssetManager
+            fullPath = __assetPath;
+            fullPath += resolvePath(path);
+
+            stream = FileStreamAndroid::create(fullPath.c_str(), modeStr);
+        }
+
+        return stream;
     }
     }
 #else
 #else
     std::string fullPath;
     std::string fullPath;
@@ -456,6 +472,16 @@ bool FileSystem::isAbsolutePath(const char* filePath)
 #endif
 #endif
 }
 }
 
 
+void FileSystem::setAssetPath(const char* path)
+{
+    __assetPath = path;
+}
+
+const char* FileSystem::getAssetPath()
+{
+    return __assetPath.c_str();
+}
+
 void FileSystem::createFileFromAsset(const char* path)
 void FileSystem::createFileFromAsset(const char* path)
 {
 {
 #ifdef __ANDROID__
 #ifdef __ANDROID__

+ 17 - 0
gameplay/src/FileSystem.h

@@ -201,6 +201,23 @@ public:
      */
      */
     static bool isAbsolutePath(const char* filePath);
     static bool isAbsolutePath(const char* filePath);
 
 
+    /**
+    * Sets the asset root path for the game on platforms that have separate assets (currently just Android).
+    *
+    * Once set, all asset paths will be loaded relative to the given path.
+    * The default asset path is an empty string ("").
+    *
+    * @param path The asset root path.
+    */
+    static void setAssetPath(const char* path);
+
+    /**
+    * Returns the currently set asset root path.
+    *
+    * @return The currently set asset root path.
+    */
+    static const char* getAssetPath();
+
     /**
     /**
      * Creates a file on the file system from the specified asset (Android-specific).
      * Creates a file on the file system from the specified asset (Android-specific).
      * 
      *