Browse Source

Cleaned up logic of loading games on the Java side of the App

Martin Felis 8 years ago
parent
commit
a00675c4cc
1 changed files with 71 additions and 94 deletions
  1. 71 94
      love/src/main/java/org/love2d/android/GameActivity.java

+ 71 - 94
love/src/main/java/org/love2d/android/GameActivity.java

@@ -15,6 +15,7 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URL;
 
 
 import android.app.Activity;
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.app.DownloadManager;
 import android.app.DownloadManager;
 import android.app.ProgressDialog;
 import android.app.ProgressDialog;
 import android.content.Context;
 import android.content.Context;
@@ -82,7 +83,12 @@ public class GameActivity extends SDLActivity {
 
 
     protected void handleIntent (Intent intent) {
     protected void handleIntent (Intent intent) {
       Uri game = intent.getData();
       Uri game = intent.getData();
+
       if (game != null) {
       if (game != null) {
+        // If we have a game via the intent data we we try to figure out how we have to load it. We
+        // support the following variations:
+        // * a main.lua file: set gamePath to the directory containing main.lua
+        // * otherwise: set gamePath to the file
         if (game.getScheme().equals ("file")) {
         if (game.getScheme().equals ("file")) {
           Log.d("GameActivity", "Received intent with path: " + game.getPath());
           Log.d("GameActivity", "Received intent with path: " + game.getPath());
           // If we were given the path of a main.lua then use its
           // If we were given the path of a main.lua then use its
@@ -94,35 +100,51 @@ public class GameActivity extends SDLActivity {
             gamePath = game.getPath();
             gamePath = game.getPath();
           }
           }
         } else {
         } else {
-            // if archive exists, copy it to the cache
-            copyGameToCache (game);
-            return;
+            Log.e("GameActivity", "Unsupported scheme: '" + game.getScheme() + "'.");
+
+            AlertDialog.Builder alert_dialog  = new AlertDialog.Builder(this);
+            alert_dialog.setMessage("Could not load LÖVE game '" + game.getPath()
+                    + "' as it uses unsupported scheme '" + game.getScheme()
+                    + "'. Please contact the developer.");
+            alert_dialog.setTitle("LÖVE for Android Error");
+            alert_dialog.setPositiveButton("Exit",
+                    new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog,int id) {
+                            finish();
+                        }
+                    });
+            alert_dialog.setCancelable(false);
+            alert_dialog.create().show();
         }
         }
-        Log.d("GameActivity", "new gamePath: " + gamePath);
-
-      } else {          
-          boolean archiveExists = false;
+      } else {
+          // No game specified via the intent data -> check whether we have a game.love in our assets.
+          boolean game_love_in_assets = false;
           try {
           try {
               List<String> assets = Arrays.asList(getAssets().list(""));
               List<String> assets = Arrays.asList(getAssets().list(""));
-              archiveExists = assets.contains("game.love");
+              game_love_in_assets = assets.contains("game.love");
           } catch (Exception e) {
           } catch (Exception e) {
               Log.d("GameActivity", "could not list application assets:" + e.getMessage());
               Log.d("GameActivity", "could not list application assets:" + e.getMessage());
           }
           }
-          if (archiveExists) {
-              String df = this.getCacheDir().getPath()+"/game.love";
-              if (mustCacheArchive && copyAssetFile("game.love",df))
-                  gamePath = df;
+
+          if (game_love_in_assets) {
+              // If we have a game.love in our assets folder copy it to the cache folder
+              // so that we can load it from native LÖVE code
+              String destination_file = this.getCacheDir().getPath()+"/game.love";
+              if (mustCacheArchive && copyAssetFile("game.love", destination_file))
+                  gamePath = destination_file;
               else
               else
                   gamePath = "game.love";
                   gamePath = "game.love";
           } else {
           } else {
+              // If no game.love was found fall back to the game in <external storage>/lovegame
               File ext = Environment.getExternalStorageDirectory();              
               File ext = Environment.getExternalStorageDirectory();              
               if ((new File(ext,"/lovegame/main.lua")).exists()) {
               if ((new File(ext,"/lovegame/main.lua")).exists()) {
                   gamePath = ext.getPath() + "/lovegame/";
                   gamePath = ext.getPath() + "/lovegame/";
               }
               }
           }
           }
-
-          Log.d("GameActivity", "new gamePath: " + gamePath);
       }
       }
+
+      Log.d("GameActivity", "new gamePath: " + gamePath);
     }
     }
 
 
     @Override
     @Override
@@ -224,104 +246,59 @@ public class GameActivity extends SDLActivity {
       context.startActivity(i);
       context.startActivity(i);
     }
     }
 
 
-    boolean copyAssetFile(String fileName, String destF)
+    /**
+     * Copies a given file from the assets folder to the destination.
+     * @return true if successful
+     */
+    boolean copyAssetFile(String fileName, String destinationFileName)
     {
     {
         boolean success = false;
         boolean success = false;
 
 
-        // open streams
-        BufferedOutputStream bos = null;
+        // open source and destination streams
+        InputStream source_stream = null;
         try {
         try {
-            bos = new BufferedOutputStream(new FileOutputStream(destF, false));
-        } catch (IOException e) {
-            Log.d ("GameActivity", "Could not open destination file:" + e.getMessage());
-        }
-        InputStream ins = null;
-        try {            
-            ins = getAssets().open(fileName);
+            source_stream = getAssets().open(fileName);
         } catch (IOException e) {
         } catch (IOException e) {
-            Log.d("GameActivity", "Could not open game file:" + e.getMessage());
+            Log.d("GameActivity", "Could not open game.love from assets: " + e.getMessage());
         }
         }
 
 
-        // copy
-        int bytes_written = 0;            
-        if (ins != null && bos != null) {
-            int chunk_read = 0;
-            try {
-                byte[] buf = new byte[8192];
-                chunk_read = ins.read(buf);
-                do {
-                    bos.write(buf, 0, chunk_read);
-                    bytes_written += chunk_read;
-                    chunk_read = ins.read(buf);
-                } while(chunk_read != -1);
-            } catch (IOException e) {
-                Log.d ("GameActivity", "Copying failed:" + e.getMessage());            
-            }
-        }
-        
-        // close streams
+        BufferedOutputStream destination_stream = null;
         try {
         try {
-            if (ins != null) ins.close();
-            if (bos != null) bos.close();
-            success = true;
+            destination_stream = new BufferedOutputStream(new FileOutputStream(destinationFileName, false));
         } catch (IOException e) {
         } catch (IOException e) {
-            Log.d ("GameActivity", "Copying failed: " + e.getMessage());
+            Log.d ("GameActivity", "Could not open destination file: " + e.getMessage());
         }
         }
 
 
-        Log.d("GameActivity", "Copied " + fileName + " to " + destF
-              + ". Wrote:" + bytes_written + " bytes");
-        return success;
-    }
-
-    void copyGameToCache (Uri sourceuri)
-    {
-      String destinationFilename = this.getCacheDir().getPath()+"/downloaded.love";
-      gamePath = destinationFilename;
-
-      BufferedOutputStream bos = null;
-      try {
-        bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
-      } catch (IOException e) {
-        Log.d ("GameActivity", "Could not open destination file:" + e.getMessage());
-      }
+        // perform the copying
+        int chunk_read = 0;
+        int bytes_written = 0;
 
 
-      int chunk_read = 0;
-      int bytes_written = 0;
+        assert (source_stream != null && destination_stream != null);
 
 
-      BufferedInputStream bis = null;
-      if (sourceuri.getScheme().equals("content")) {
         try {
         try {
-          bis = new BufferedInputStream(getContentResolver().openInputStream(sourceuri));
+            byte[] buf = new byte[1024];
+            chunk_read = source_stream.read(buf);
+            do {
+                destination_stream.write(buf, 0, chunk_read);
+                bytes_written += chunk_read;
+                chunk_read = source_stream.read(buf);
+            } while (chunk_read != -1);
         } catch (IOException e) {
         } catch (IOException e) {
-          Log.d ("GameActivity", "Could not open game file:" + e.getMessage());
+            Log.d("GameActivity", "Copying failed:" + e.getMessage());
         }
         }
-      } else {
-        Log.d ("GameActivity", "Unsupported scheme: " + sourceuri.getScheme());
-      }
 
 
-      if (bis != null) {
-        // actual copying
+        // close streams
         try {
         try {
-          byte[] buf = new byte[1024];
-          chunk_read = bis.read(buf);
-          do {
-            bos.write(buf, 0, chunk_read);
-            bytes_written += chunk_read;
-            chunk_read = bis.read(buf);        
-          } while(chunk_read != -1);
+            if (source_stream != null) source_stream.close();
+            if (destination_stream != null) destination_stream.close();
+            success = true;
         } catch (IOException e) {
         } catch (IOException e) {
-          Log.d ("GameActivity", "Copying failed:" + e.getMessage());
-        } 
-      }
-
-      // close streams
-      try {
-        if (bis != null) bis.close();
-        if (bos != null) bos.close();
-      } catch (IOException e) {
-        Log.d ("GameActivity", "Copying failed: " + e.getMessage());
-      }
+            Log.d ("GameActivity", "Copying failed: " + e.getMessage());
+        }
 
 
-      Log.d("GameActivity", "Copied " + bytes_written + " bytes");
+        Log.d("GameActivity", "Successfully copied " + fileName
+                + " to " + destinationFileName
+                + " (" + bytes_written + " bytes written).");
+        return success;
     }
     }
-}
+}