From defbc8a87bbfc67290bdbe413be9b58c27f20b53 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sun, 13 May 2018 23:57:07 +0300 Subject: [PATCH] Now android version can contain game resources in APK --- android/README | 4 +- android/src/org/d2df/app/CopyAssets.java | 71 ++++++++++++++++++++++++ android/src/org/d2df/app/Doom2DF.java | 12 ++++ src/game/Doom2DF.lpr | 7 +-- src/lib/sdl2/sdlsystem.inc | 10 ++++ 5 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 android/src/org/d2df/app/CopyAssets.java diff --git a/android/README b/android/README index e99a2f6..de9cb1b 100644 --- a/android/README +++ b/android/README @@ -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 index 0000000..445f30a --- /dev/null +++ b/android/src/org/d2df/app/CopyAssets.java @@ -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); + } + } + +} diff --git a/android/src/org/d2df/app/Doom2DF.java b/android/src/org/d2df/app/Doom2DF.java index 175f4e4..ca17666 100644 --- a/android/src/org/d2df/app/Doom2DF.java +++ b/android/src/org/d2df/app/Doom2DF.java @@ -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"); + } } diff --git a/src/game/Doom2DF.lpr b/src/game/Doom2DF.lpr index 8808d63..0b62430 100644 --- a/src/game/Doom2DF.lpr +++ b/src/game/Doom2DF.lpr @@ -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; diff --git a/src/lib/sdl2/sdlsystem.inc b/src/lib/sdl2/sdlsystem.inc index 1e06d9f..8f5c6e2 100644 --- a/src/lib/sdl2/sdlsystem.inc +++ b/src/lib/sdl2/sdlsystem.inc @@ -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} -- 2.29.2