OSDN Git Service

Changed internal for the desktop to look in the classpath if not found on the filesys...
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Wed, 17 Nov 2010 22:10:40 +0000 (22:10 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Wed, 17 Nov 2010 22:10:40 +0000 (22:10 +0000)
backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidFileHandle.java
backends/gdx-backend-jogl/src/com/badlogic/gdx/backends/jogl/JoglFileHandle.java
backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglFileHandle.java
gdx/src/com/badlogic/gdx/Files.java
gdx/src/com/badlogic/gdx/files/FileHandle.java
tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java

index 70aa349..be7e096 100644 (file)
 package com.badlogic.gdx.backends.android;\r
 \r
 import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
-import java.io.OutputStream;\r
 \r
 import android.content.res.AssetManager;\r
 \r
-import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.Files.FileType;\r
+import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.files.FileHandle;\r
 import com.badlogic.gdx.utils.GdxRuntimeException;\r
 \r
@@ -92,10 +88,18 @@ public class AndroidFileHandle extends FileHandle {
        public FileHandle parent () {\r
                File parent = file.getParentFile();\r
                if (parent == null) {\r
-                       if (type == FileType.Classpath || type == FileType.Absolute)\r
+                       switch (type) {\r
+                       case Classpath:\r
+                       case Absolute:\r
                                parent = new File("/");\r
-                       else\r
-                               parent = new File(".");\r
+                               break;\r
+                       case Internal:\r
+                               parent = new File("");\r
+                               break;\r
+                       case External:\r
+                               parent = new File(Gdx.files.getExternalStoragePath());\r
+                               break;\r
+                       }\r
                }\r
                return new AndroidFileHandle(assets, parent, type);\r
        }\r
index bd3f85f..53f1ad0 100644 (file)
@@ -16,6 +16,7 @@ package com.badlogic.gdx.backends.jogl;
 import java.io.File;\r
 \r
 import com.badlogic.gdx.Files.FileType;\r
+import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.files.FileHandle;\r
 \r
 /**\r
@@ -38,10 +39,18 @@ public class JoglFileHandle extends FileHandle {
        public FileHandle parent () {\r
                File parent = file.getParentFile();\r
                if (parent == null) {\r
-                       if (type == FileType.Classpath || type == FileType.Absolute)\r
+                       switch (type) {\r
+                       case Classpath:\r
+                       case Absolute:\r
                                parent = new File("/");\r
-                       else\r
-                               parent = new File(".");\r
+                               break;\r
+                       case Internal:\r
+                               parent = new File("");\r
+                               break;\r
+                       case External:\r
+                               parent = new File(Gdx.files.getExternalStoragePath());\r
+                               break;\r
+                       }\r
                }\r
                return new JoglFileHandle(parent, type);\r
        }\r
index ce5a4b8..27fc13d 100644 (file)
@@ -16,6 +16,7 @@ package com.badlogic.gdx.backends.lwjgl;
 import java.io.File;\r
 \r
 import com.badlogic.gdx.Files.FileType;\r
+import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.files.FileHandle;\r
 \r
 /**\r
@@ -38,10 +39,18 @@ final class LwjglFileHandle extends FileHandle {
        public FileHandle parent () {\r
                File parent = file.getParentFile();\r
                if (parent == null) {\r
-                       if (type == FileType.Classpath || type == FileType.Absolute)\r
+                       switch (type) {\r
+                       case Classpath:\r
+                       case Absolute:\r
                                parent = new File("/");\r
-                       else\r
-                               parent = new File(".");\r
+                               break;\r
+                       case Internal:\r
+                               parent = new File("");\r
+                               break;\r
+                       case External:\r
+                               parent = new File(Gdx.files.getExternalStoragePath());\r
+                               break;\r
+                       }\r
                }\r
                return new LwjglFileHandle(parent, type);\r
        }\r
index 1349d35..d9b932f 100644 (file)
@@ -36,8 +36,9 @@ public interface Files {
                Classpath,\r
 \r
                /**\r
-                * Path relative to the asset directory on Android and to the application's root directory on the desktop. Internal files\r
-                * are always readonly.\r
+                * Path relative to the asset directory on Android and to the application's root directory on the desktop. On the desktop,\r
+                * if the file is not, the classpath is then checked. This enables files to be found when using JWS or applets. Internal\r
+                * files are always readonly.\r
                 */\r
                Internal,\r
 \r
index 46beb0e..851b25c 100644 (file)
@@ -42,15 +42,15 @@ public abstract class FileHandle {
                this.type = type;\r
 \r
                switch (type) {\r
+               case Internal:\r
+                       file = new File(fileName);\r
+                       if (file.exists()) break;\r
+                       // Fall through.\r
                case Classpath:\r
                        if (FileHandle.class.getResourceAsStream("/" + fileName) == null)\r
                                throw new GdxRuntimeException("File not found: " + fileName + " (" + type + ")");\r
                        file = new File("/" + fileName);\r
                        break;\r
-               case Internal:\r
-                       file = new File(fileName);\r
-                       if (!file.exists()) throw new GdxRuntimeException("File not found: " + file + " (" + type + ")");\r
-                       break;\r
                case External:\r
                        file = new File(Gdx.files.getExternalStoragePath() + fileName);\r
                        break;\r
@@ -109,13 +109,11 @@ public abstract class FileHandle {
         *        {@link FileType#Internal} and the file doesn't exist, or if it could not be read.\r
         */\r
        public InputStream read () {\r
-               if (type == FileType.Classpath) {\r
+               if (type == FileType.Classpath || (type == FileType.Internal && !file.exists())) {\r
                        InputStream input = FileHandle.class.getResourceAsStream(file.getPath().replace('\\', '/'));\r
                        if (input == null) throw new GdxRuntimeException("File not found: " + file + " (" + type + ")");\r
                        return input;\r
                }\r
-               if (type == FileType.Internal && !file.exists())\r
-                       throw new GdxRuntimeException("File not found: " + file + " (" + type + ")");\r
                try {\r
                        return new FileInputStream(file);\r
                } catch (FileNotFoundException ex) {\r
@@ -251,7 +249,7 @@ public abstract class FileHandle {
         * Returns the length in bytes of this file, or 0 if this file is a directory or does not exist.\r
         */\r
        public long length () {\r
-               if (type == FileType.Classpath || type == FileType.Internal) {\r
+               if (type == FileType.Classpath || (type == FileType.Internal && !file.exists())) {\r
                        try {\r
                                InputStream input = read();\r
                                long length = input.available();\r
index 649c8c6..1556675 100644 (file)
@@ -105,6 +105,7 @@ public class FilesTest extends GdxTest {
 \r
        private void testWriteRead (String path, FileType type) {\r
                FileHandle handle = Gdx.files.getFileHandle(path, type);\r
+               handle.delete();\r
                if (handle.exists()) fail();\r
                if (handle.isDirectory()) fail();\r
                if (handle.delete()) fail();\r