Przeglądaj źródła

Add Android asset manager equivalents to FileSystem resourcePath

Benjamin Dobell 11 lat temu
rodzic
commit
48e4aca294
2 zmienionych plików z 38 dodań i 3 usunięć
  1. 21 3
      gameplay/src/FileSystem.cpp
  2. 17 0
      gameplay/src/FileSystem.h

+ 21 - 3
gameplay/src/FileSystem.cpp

@@ -87,6 +87,7 @@ static bool androidFileExists(const char* filePath)
 
 /** @script{ignore} */
 static std::string __resourcePath("./");
+static std::string __assetPath("");
 static std::map<std::string, std::string> __aliases;
 
 /**
@@ -343,14 +344,18 @@ bool FileSystem::fileExists(const char* filePath)
 {
     GP_ASSERT(filePath);
 
+    std::string fullPath;
+
 #ifdef __ANDROID__
-    if (androidFileExists(resolvePath(filePath)))
+    fullPath = __assetPath;
+    fullPath += resolvePath(filePath);
+
+    if (androidFileExists(fullPath.c_str()))
     {
         return true;
     }
 #endif
 
-    std::string fullPath;
     getFullPath(filePath, fullPath);
 
     gp_stat_struct s;
@@ -388,7 +393,10 @@ Stream* FileSystem::open(const char* path, size_t streamMode)
         if (!stream)
         {
             // Otherwise fall-back to assets loaded via the AssetManager
-            stream = FileStreamAndroid::create(resolvePath(path), modeStr);
+            fullPath = __assetPath;
+            fullPath += resolvePath(path);
+
+            stream = FileStreamAndroid::create(fullPath.c_str(), modeStr);
         }
 
         return stream;
@@ -464,6 +472,16 @@ bool FileSystem::isAbsolutePath(const char* filePath)
 #endif
 }
 
+void FileSystem::setAssetPath(const char* path)
+{
+    __assetPath = path;
+}
+
+const char* FileSystem::getAssetPath()
+{
+    return __assetPath.c_str();
+}
+
 void FileSystem::createFileFromAsset(const char* path)
 {
 #ifdef __ANDROID__

+ 17 - 0
gameplay/src/FileSystem.h

@@ -201,6 +201,23 @@ public:
      */
     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).
      *