From: DeaDDooMER Date: Sun, 7 Oct 2018 20:27:27 +0000 (+0300) Subject: Android: improved data unpacking X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=2f2f9c997a76f5394c4deb2fa7d54b99a6186c87;p=d2df-sdl.git Android: improved data unpacking --- diff --git a/android/src/org/d2df/app/CopyAssets.java b/android/src/org/d2df/app/CopyAssets.java index 445f30a..f5f6c67 100644 --- a/android/src/org/d2df/app/CopyAssets.java +++ b/android/src/org/d2df/app/CopyAssets.java @@ -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) {} + } + } + } + } + } + } diff --git a/src/game/Doom2DF.lpr b/src/game/Doom2DF.lpr index c86d744..27b90a5 100644 --- a/src/game/Doom2DF.lpr +++ b/src/game/Doom2DF.lpr @@ -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} diff --git a/src/lib/sdl2/sdlsystem.inc b/src/lib/sdl2/sdlsystem.inc index 8f5c6e2..69bfc08 100644 --- a/src/lib/sdl2/sdlsystem.inc +++ b/src/lib/sdl2/sdlsystem.inc @@ -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;