DEADSOFTWARE

Android: improved data unpacking
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Sun, 7 Oct 2018 20:27:27 +0000 (23:27 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Sun, 7 Oct 2018 20:27:27 +0000 (23:27 +0300)
android/src/org/d2df/app/CopyAssets.java
src/game/Doom2DF.lpr
src/lib/sdl2/sdlsystem.inc

index 445f30a80644fc052b45715e668963a2d63ffebd..f5f6c67e81a13c33decd8bf0e253fe687272f68a 100644 (file)
@@ -2,6 +2,7 @@ package org.d2df.app;
 
 import android.content.Context;
 import android.content.res.AssetManager;
+import android.os.Environment;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -13,59 +14,81 @@ import android.util.Log;
 
 public class CopyAssets {
 
-  public static void copyAssets(Context context, String prefix) {
-    AssetManager assetManager = context.getAssets();
-    String[] files = null;
-    try {
-        files = assetManager.list(prefix);
-    } catch (IOException e) {
-        Log.e("tag", "Failed to get asset file list.", e);
-    }
-    if (files != null) for (String filename : files) {
-        InputStream in = null;
-        OutputStream out = null;
-        try {
-          File f = new File(context.getExternalFilesDir(null), prefix);
-          if (!f.exists()) {
-            f.mkdirs();
-          }
-          File outFile = new File(context.getExternalFilesDir(null), prefix + "/" + filename);
-          if (!outFile.exists()) {
-            in = assetManager.open(prefix + "/" + filename);
-            out = new FileOutputStream(outFile);
-            copyFile(in, out);
-          }
-        } catch(IOException e) {
-            Log.e("tag", "Failed to copy asset file: " + filename, e);
-        }     
-        finally {
-            if (in != null) {
-                try {
-                    in.close();
-                    in = null;
-                } catch (IOException e) {
-                    
-                }
-            }
-            if (out != null) {
-                try {
-                    out.flush();
-                    out.close();
-                    out = null;
-                } catch (IOException e) {
-                    
-                }
-            }
-        }  
-    }
-  }
-  
-  public static void copyFile(InputStream in, OutputStream out) throws IOException {
-    byte[] buffer = new byte[1024];
-    int read;
-    while((read = in.read(buffer)) != -1){
-      out.write(buffer, 0, read);
-    }
-  }
-  
+       private static boolean ExtStorageMounted() {
+               return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
+       }
+
+       private static boolean ExtStorageReadonly() {
+               return Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState());
+       }
+
+       private static void CopyFile(InputStream in, OutputStream out)
+               throws IOException
+       {
+               byte[] buffer = new byte[1024];
+               int read;
+               while ((read = in.read(buffer)) != -1) {
+                       out.write(buffer, 0, read);
+               }
+       }
+
+       public static void copyAssets(Context context, String prefix) {
+               AssetManager assetManager = context.getAssets();
+               String[] files = null;
+               try {
+                       files = assetManager.list(prefix);
+               } catch (IOException e) {
+                       Log.e("tag", "Failed to get asset file list.", e);
+               }
+               if (files != null) {
+                       for (String filename : files) {
+                               InputStream in = null;
+                               OutputStream out = null;
+                               try {
+                                       if (ExtStorageMounted() && !ExtStorageReadonly()) {
+                                               /* Get External Storage Path */
+                                               File f = new File(context.getExternalFilesDir(null).getAbsolutePath(), prefix);
+                                               if (!f.exists()) {
+                                                       f.mkdirs();
+                                               }
+                                               File outFile = new File(context.getExternalFilesDir(null).getAbsolutePath(), prefix + "/" + filename);
+                                               if (!outFile.exists()) {
+                                                       in = assetManager.open(prefix + "/" + filename);
+                                                       out = new FileOutputStream(outFile);
+                                                       CopyFile(in, out);
+                                               }
+                                       } else {
+                                               /* Get Internal Storage Path */
+                                               File f = new File(context.getFilesDir().getAbsolutePath(), prefix);
+                                               if (!f.exists()) {
+                                                       f.mkdirs();
+                                               }
+                                               File outFile = new File(context.getFilesDir().getAbsolutePath(), prefix + "/" + filename);
+                                               if (!outFile.exists()) {
+                                                       in = assetManager.open(prefix + "/" + filename);
+                                                       out = new FileOutputStream(outFile);
+                                                       CopyFile(in, out);
+                                               }
+                                       }
+                               } catch(IOException e) {
+                                       Log.e("tag", "Failed to copy asset file: " + filename, e);
+                               } finally {
+                                       if (in != null) {
+                                               try {
+                                                       in.close();
+                                                       in = null;
+                                               } catch (IOException e) {}
+                                       }
+                                       if (out != null) {
+                                               try {
+                                                       out.flush();
+                                                       out.close();
+                                                       out = null;
+                                               } catch (IOException e) {}
+                                       }
+                               }
+                       }
+               }
+       }
+
 }
index c86d7441e795240e6d4ec170060d2f34cc45f406..27b90a5055c008fb42df86589ec8480f44ed549a 100644 (file)
@@ -141,6 +141,7 @@ function SDL_main(argc: CInt; argv: PPChar): CInt; cdecl;
 var
   f: Integer;
   noct: Boolean = false;
+  storage: String;
   //tfo: Text;
 begin
   SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); //k8: fuck off, that's why
@@ -148,16 +149,23 @@ begin
 {$IFDEF ANDROID}
 {$I-}
   e_SetSafeSlowLog(true);
-  Chdir(SDL_AndroidGetExternalStoragePath());
+  if SDL_AndroidGetExternalStorageState() <> 0 then
+  begin
+    storage := SDL_AndroidGetExternalStoragePath();
+    Chdir(storage);
+    e_WriteLog('Use external storage: ' + storage, TMsgType.Notify)
+  end
+  else
+  begin
+    storage := SDL_AndroidGetInternalStoragePath();
+    Chdir(storage);
+    e_WriteLog('Use internal storage: ' + storage, TMsgType.Notify)
+  end;
   if IOresult <> 0 then
   begin
-    Chdir(SDL_AndroidGetInternalStoragePath());
-    if IOresult <> 0 then
-    begin
-      e_WriteLog('Fuck! Cant chdir to any game directory :(', TMsgType.Fatal);
-      result := 1;
-      exit;
-    end;
+    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, PChar('Invalid path'), PChar('Can''t chdir to ' + storage), nil);
+    result := 1;
+    exit
   end;
   SetEnvVar('TIMIDITY_CFG', 'timidity.cfg');
 {$ENDIF ANDROID}
index 8f5c6e252eb2df7261bca677f197e145312c14c4..69bfc0821b16df131d34bf00d6b068c48c8dc8b8 100644 (file)
@@ -97,6 +97,9 @@ Function SDL_WinRTGetFSPathUTF8(pathType :TSDL_WinRT_Path):PChar;
 
 {$IF DEFINED(ANDROID)}
 
+Function SDL_AndroidGetExternalStorageState:SInt32;
+   cdecl; external SDL_LibName;
+
 Function SDL_AndroidGetExternalStoragePath:PChar;
    cdecl; external SDL_LibName;