DEADSOFTWARE

Now android version can contain game resources in APK
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Sun, 13 May 2018 20:57:07 +0000 (23:57 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Sun, 13 May 2018 20:57:52 +0000 (23:57 +0300)
android/README
android/src/org/d2df/app/CopyAssets.java [new file with mode: 0644]
android/src/org/d2df/app/Doom2DF.java
src/game/Doom2DF.lpr
src/lib/sdl2/sdlsystem.inc

index e99a2f6342225d6ecd78920a7b44c8ef31af4453..de9cb1bab91ac03abe7e57b9357b7afb63e6385e 100644 (file)
@@ -20,6 +20,7 @@ Requirements:
 D2DF uses special version of nanoGL ( https://github.com/DeaDDooMER/nanogl ) with some added functions.
 Build all shared libraries using NDK toolchain and put into directory ./ass/lib/armeabi-v7a/.
 Also you need to build FPC crosscompiler ( http://wiki.freepascal.org/Android ).
+Put game resources into direcotor resources/ (or install it manually into external/internal storage).
 
 Generate keys:
 ```
@@ -60,7 +61,7 @@ ppcrossarm \
 Build APK and sign it:
 ```
 rm -rf bin obj gen
-mkdir -p bin obj gen
+mkdir -p bin obj gen resources
 aapt package -f -m -S res -J gen -M AndroidManifest.xml -I ${ANDROID_JAR}
 javac -source 1.6 -target 1.6 -d obj -bootclasspath ${ANDROID_JAR} -sourcepath src `find src -name '*.java'`
 dx --dex --output=bin/classes.dex obj
@@ -70,6 +71,7 @@ aapt package -f \
         -J gen \
         -I ${ANDROID_JAR} \
         -F bin/d2df.unsigned.apk \
+       -A resources
         bin ass
 jarsigner -sigalg SHA1withRSA -digestalg SHA1 \
         -keystore d2df.keystore \
diff --git a/android/src/org/d2df/app/CopyAssets.java b/android/src/org/d2df/app/CopyAssets.java
new file mode 100644 (file)
index 0000000..445f30a
--- /dev/null
@@ -0,0 +1,71 @@
+package org.d2df.app;
+
+import android.content.Context;
+import android.content.res.AssetManager;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+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);
+    }
+  }
+  
+}
index 175f4e48925150adb5fdc4e8857d9957dea6a556..ca1766609ac4b4a689bca384dba7db7d0a530ff8 100644 (file)
@@ -3,6 +3,7 @@ package org.d2df.app;
 import android.app.Activity;
 import android.os.Bundle;
 
+import org.libsdl.app.SDL;
 import org.libsdl.app.SDLActivity;
 
 public class Doom2DF extends SDLActivity {
@@ -18,4 +19,15 @@ public class Doom2DF extends SDLActivity {
                        "Doom2DF"
                };
        }
+
+       @Override
+       protected void onCreate(Bundle savedInstanceState) {
+               super.onCreate(savedInstanceState);
+               CopyAssets.copyAssets(SDL.getContext(), "");
+               CopyAssets.copyAssets(SDL.getContext(), "data");
+               CopyAssets.copyAssets(SDL.getContext(), "data/models");
+               CopyAssets.copyAssets(SDL.getContext(), "maps");
+               CopyAssets.copyAssets(SDL.getContext(), "maps/megawads");
+               CopyAssets.copyAssets(SDL.getContext(), "wads");
+       }
 }
index 8808d633f3181c3460a7b15b64ce27e2e613e13f..0b62430ad3d7c369f4dcf485b6f4fbb65f8e1f67 100644 (file)
@@ -148,14 +148,13 @@ begin
 {$IFDEF ANDROID}
 {$I-}
   e_SetSafeSlowLog(true);
-  Chdir('/sdcard/D2DF');
+  Chdir(SDL_AndroidGetExternalStoragePath());
   if IOresult <> 0 then
   begin
-    Mkdir('/sdcard/D2DF');
-    Chdir('/sdcard/D2DF');
+    Chdir(SDL_AndroidGetInternalStoragePath());
     if IOresult <> 0 then
     begin
-      e_WriteLog('Fail: cant chdir /sdcard/D2DF', TMsgType.Fatal);
+      e_WriteLog('Fuck! Cant chdir to any game directory :(', TMsgType.Fatal);
       result := 1;
       exit;
     end;
index 1e06d9f0ed7c28fec74703d5fb3e675c90101f04..8f5c6e252eb2df7261bca677f197e145312c14c4 100644 (file)
@@ -94,3 +94,13 @@ Function SDL_WinRTGetFSPathUTF8(pathType :TSDL_WinRT_Path):PChar;
    cdecl; external SDL_LibName;
 
 {$ENDIF}
+
+{$IF DEFINED(ANDROID)}
+
+Function SDL_AndroidGetExternalStoragePath:PChar;
+   cdecl; external SDL_LibName;
+
+Function SDL_AndroidGetInternalStoragePath:PChar;
+   cdecl; external SDL_LibName;
+
+{$ENDIF}