From 3b8da183baef453ea936c09cbc0343def1bc054d Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Sun, 14 Jul 2019 22:46:47 +0200 Subject: [PATCH 1/7] Add initial changes for Android port --- CMakeLists.txt | 84 +++++++++++++++++++++++++++++++++++++++++++----- SourceX/main.cpp | 6 ++++ 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 994206a5b93..f930559794a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,10 @@ if(HAIKU) set(ASAN OFF) endif() +if(ANDROID) + set(ASAN OFF) +endif() + if(DIST) set(sodium_USE_STATIC_LIBS ON) endif() @@ -65,13 +69,32 @@ if(NOT CMAKE_SIZEOF_VOID_P EQUAL 4) endif() find_package(Threads REQUIRED) -find_package(SDL2 CONFIG QUIET) -if (NOT SDL2_FOUND) - find_package(SDL2 REQUIRED) +if (NOT ANDROID) + find_package(SDL2 CONFIG QUIET) + if (NOT SDL2_FOUND) + find_package(SDL2 REQUIRED) + endif() + find_package(SDL2_ttf REQUIRED) + find_package(SDL2_mixer REQUIRED) + find_package(sodium REQUIRED) +endif() + +if (ANDROID) +add_library(devilutionAndroid STATIC + 3rdParty/Android/SDL_android_main.c) + +target_include_directories(devilutionAndroid PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../app/libs/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/include + + # SDL_internal.h is located in src required by SDL_android_main.c bridge + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/src + + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_image/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_gfx/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_mixer/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_ttf/include) endif() -find_package(SDL2_ttf REQUIRED) -find_package(SDL2_mixer REQUIRED) -find_package(sodium REQUIRED) add_library(smacker STATIC 3rdParty/libsmacker/smk_bitstream.c @@ -176,7 +199,7 @@ add_library(devilution STATIC Source/trigs.cpp Source/wave.cpp) -add_executable(devilutionx +SET(devilutionx_SRCS SourceX/dx.cpp SourceX/miniwin/misc.cpp SourceX/miniwin/misc_io.cpp @@ -209,6 +232,36 @@ add_executable(devilutionx SourceX/DiabloUI/title.cpp SourceX/main.cpp) +if (NOT ANDROID) +add_executable(devilutionx + ${devilutionx_SRCS}) +else() +add_library(devilutionx SHARED + ${devilutionx_SRCS}) +endif() + +if (ANDROID) + target_include_directories(devilutionx PUBLIC + SourceX + ${CMAKE_CURRENT_SOURCE_DIR}/../app/libs/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/include + + # SDL_internal.h is located in src required by SDL_android_main.c bridge + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/src + + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_image/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_gfx/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_mixer/include + ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_ttf/include + ) + + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/ ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_image/ ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_image) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_gfx/ ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_gfx) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_mixer/ ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_mixer) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_ttf/ ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_ttf) +endif() + target_include_directories(devilution PUBLIC Source SourceS) target_include_directories(devilutionx PRIVATE SourceX @@ -222,11 +275,22 @@ target_link_libraries(devilutionx PRIVATE PKWare StormLib smacker - Radon + Radon) + +if (NOT ANDROID) +target_link_libraries(devilutionx PRIVATE SDL2::SDL2main SDL2::SDL2_ttf SDL2::SDL2_mixer sodium) +else() + target_link_libraries(devilutionx PRIVATE + devilutionAndroid + SDL2 + SDL2_ttf + SDL2_mixer + ${CMAKE_CURRENT_SOURCE_DIR}/../app/libs/jni/${ANDROID_ARCH_NAME}/libsodiumjni.so) +endif() target_compile_definitions(devilution PRIVATE DEVILUTION_ENGINE) target_compile_definitions(devilution PUBLIC @@ -293,3 +357,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Silence appfat.cpp warnings set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing") endif() + +if (ANDROID) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format-security") +endif() diff --git a/SourceX/main.cpp b/SourceX/main.cpp index 63acbcc4f98..3d1b90388ce 100644 --- a/SourceX/main.cpp +++ b/SourceX/main.cpp @@ -22,10 +22,16 @@ static std::string build_cmdline(int argc, char **argv) return str; } +#ifdef __ANDROID__ +int SDL_main(int argc, char* argv[]) { + return dvl::WinMain(NULL, NULL, (char *)"", 0); +} +#else int main(int argc, char **argv) { auto cmdline = build_cmdline(argc, argv); return dvl::WinMain(NULL, NULL, (char *)cmdline.c_str(), 0); } +#endif From 3191c202b53d0b744f8238a12e379924cee03d41 Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Sun, 14 Jul 2019 22:47:22 +0200 Subject: [PATCH 2/7] Add android 3rd party files --- 3rdParty/Android/SDL_android_main.c | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 3rdParty/Android/SDL_android_main.c diff --git a/3rdParty/Android/SDL_android_main.c b/3rdParty/Android/SDL_android_main.c new file mode 100644 index 00000000000..1f81867808b --- /dev/null +++ b/3rdParty/Android/SDL_android_main.c @@ -0,0 +1,83 @@ +/* + SDL_android_main.c, placed in the public domain by Sam Lantinga 3/13/14 +*/ +#include "SDL_internal.h" + +#ifdef __ANDROID__ + +/* Include the SDL main definition header */ +#include "SDL_main.h" + +/******************************************************************************* + Functions called by JNI +*******************************************************************************/ +#include + +/* Called before SDL_main() to initialize JNI bindings in SDL library */ +extern void SDL_Android_Init(JNIEnv* env, jclass cls); + +/* This prototype is needed to prevent a warning about the missing prototype for global function below */ +JNIEXPORT int JNICALL Java_rocks_georgik_sdlapp_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array); + +/* Start up the SDL app */ +JNIEXPORT int JNICALL Java_rocks_georgik_sdlapp_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array) +{ + int i; + int argc; + int status; + int len; + char** argv; + + /* This interface could expand with ABI negotiation, callbacks, etc. */ + SDL_Android_Init(env, cls); + + SDL_SetMainReady(); + + /* Prepare the arguments. */ + + len = (*env)->GetArrayLength(env, array); + argv = SDL_stack_alloc(char*, 1 + len + 1); + argc = 0; + /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works. + https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start + */ + argv[argc++] = SDL_strdup("app_process"); + for (i = 0; i < len; ++i) { + const char* utf; + char* arg = NULL; + jstring string = (*env)->GetObjectArrayElement(env, array, i); + if (string) { + utf = (*env)->GetStringUTFChars(env, string, 0); + if (utf) { + arg = SDL_strdup(utf); + (*env)->ReleaseStringUTFChars(env, string, utf); + } + (*env)->DeleteLocalRef(env, string); + } + if (!arg) { + arg = SDL_strdup(""); + } + argv[argc++] = arg; + } + argv[argc] = NULL; + + + /* Run the application. */ + + status = SDL_main(argc, argv); + + /* Release the arguments. */ + + for (i = 0; i < argc; ++i) { + SDL_free(argv[i]); + } + SDL_stack_free(argv); + /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */ + /* exit(status); */ + + return status; +} + +#endif /* __ANDROID__ */ + +/* vi: set ts=4 sw=4 expandtab: */ From e1a689493beff34ae02118f0911547c3be4469c9 Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Mon, 15 Jul 2019 06:22:09 +0200 Subject: [PATCH 3/7] Remove unused anymore SDL_android_main.c, modify Cmake to handle it --- 3rdParty/Android/SDL_android_main.c | 83 ----------------------------- 1 file changed, 83 deletions(-) delete mode 100644 3rdParty/Android/SDL_android_main.c diff --git a/3rdParty/Android/SDL_android_main.c b/3rdParty/Android/SDL_android_main.c deleted file mode 100644 index 1f81867808b..00000000000 --- a/3rdParty/Android/SDL_android_main.c +++ /dev/null @@ -1,83 +0,0 @@ -/* - SDL_android_main.c, placed in the public domain by Sam Lantinga 3/13/14 -*/ -#include "SDL_internal.h" - -#ifdef __ANDROID__ - -/* Include the SDL main definition header */ -#include "SDL_main.h" - -/******************************************************************************* - Functions called by JNI -*******************************************************************************/ -#include - -/* Called before SDL_main() to initialize JNI bindings in SDL library */ -extern void SDL_Android_Init(JNIEnv* env, jclass cls); - -/* This prototype is needed to prevent a warning about the missing prototype for global function below */ -JNIEXPORT int JNICALL Java_rocks_georgik_sdlapp_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array); - -/* Start up the SDL app */ -JNIEXPORT int JNICALL Java_rocks_georgik_sdlapp_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array) -{ - int i; - int argc; - int status; - int len; - char** argv; - - /* This interface could expand with ABI negotiation, callbacks, etc. */ - SDL_Android_Init(env, cls); - - SDL_SetMainReady(); - - /* Prepare the arguments. */ - - len = (*env)->GetArrayLength(env, array); - argv = SDL_stack_alloc(char*, 1 + len + 1); - argc = 0; - /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works. - https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start - */ - argv[argc++] = SDL_strdup("app_process"); - for (i = 0; i < len; ++i) { - const char* utf; - char* arg = NULL; - jstring string = (*env)->GetObjectArrayElement(env, array, i); - if (string) { - utf = (*env)->GetStringUTFChars(env, string, 0); - if (utf) { - arg = SDL_strdup(utf); - (*env)->ReleaseStringUTFChars(env, string, utf); - } - (*env)->DeleteLocalRef(env, string); - } - if (!arg) { - arg = SDL_strdup(""); - } - argv[argc++] = arg; - } - argv[argc] = NULL; - - - /* Run the application. */ - - status = SDL_main(argc, argv); - - /* Release the arguments. */ - - for (i = 0; i < argc; ++i) { - SDL_free(argv[i]); - } - SDL_stack_free(argv); - /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */ - /* exit(status); */ - - return status; -} - -#endif /* __ANDROID__ */ - -/* vi: set ts=4 sw=4 expandtab: */ From 61927ac3f10fd26f38bfcfc9cee3ff306d3ea5c9 Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Mon, 15 Jul 2019 06:22:19 +0200 Subject: [PATCH 4/7] Remove unused anymore SDL_android_main.c, modify Cmake to handle it --- CMakeLists.txt | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f930559794a..2c39aa5a349 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,23 +79,6 @@ if (NOT ANDROID) find_package(sodium REQUIRED) endif() -if (ANDROID) -add_library(devilutionAndroid STATIC - 3rdParty/Android/SDL_android_main.c) - -target_include_directories(devilutionAndroid PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/../app/libs/include - ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/include - - # SDL_internal.h is located in src required by SDL_android_main.c bridge - ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2/src - - ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_image/include - ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_gfx/include - ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_mixer/include - ${CMAKE_CURRENT_SOURCE_DIR}/../SDL2_ttf/include) -endif() - add_library(smacker STATIC 3rdParty/libsmacker/smk_bitstream.c 3rdParty/libsmacker/smk_hufftree.c @@ -285,7 +268,6 @@ target_link_libraries(devilutionx PRIVATE sodium) else() target_link_libraries(devilutionx PRIVATE - devilutionAndroid SDL2 SDL2_ttf SDL2_mixer From 42c795d45f8a271fe7e817c6be9897618debdd5c Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Mon, 15 Jul 2019 06:22:38 +0200 Subject: [PATCH 5/7] Hardcode path --- SourceX/storm/storm.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SourceX/storm/storm.cpp b/SourceX/storm/storm.cpp index 915b8243c71..2a104d52186 100644 --- a/SourceX/storm/storm.cpp +++ b/SourceX/storm/storm.cpp @@ -14,14 +14,19 @@ namespace dvl { DWORD nLastError = 0; bool directFileAccess = false; + static std::string getIniPath() { +#ifdef __ANDROID__ + return "/storage/emulated/0/Android/data/org.kkszysiu.devilutionx/diablo.ini"; +#else char path[DVL_MAX_PATH]; int len = GetModuleFileNameA(ghInst, path, DVL_MAX_PATH); path[len - 1] = '/'; strcat(path, "diablo.ini"); return path; +#endif } static radon::File ini(getIniPath()); From a444e885182bcf7a24ea567be26ce46f68c80b95 Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Mon, 15 Jul 2019 06:25:18 +0200 Subject: [PATCH 6/7] RestrictedTest not workign yet on Android, will check later whats wrong --- Source/diablo.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index feb5644b8f8..cca0d4c343f 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -258,8 +258,10 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi #endif ghInst = hInst; +#ifndef __ANDROID__ if (RestrictedTest()) ErrOkDlg(IDD_DIALOG10, 0, "C:\\Src\\Diablo\\Source\\DIABLO.CPP", 877); +#endif if (ReadOnlyTest()) { if (!GetModuleFileName(ghInst, szFileName, sizeof(szFileName))) szFileName[0] = '\0'; From 99f541b55f6e8bba729fd8957234eeddbe8fed3d Mon Sep 17 00:00:00 2001 From: Krzysztof Klinikowski Date: Mon, 15 Jul 2019 21:16:44 +0200 Subject: [PATCH 7/7] Use symling instead of full path for Android data path --- SourceX/storm/storm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SourceX/storm/storm.cpp b/SourceX/storm/storm.cpp index 2a104d52186..f61a734776a 100644 --- a/SourceX/storm/storm.cpp +++ b/SourceX/storm/storm.cpp @@ -18,7 +18,7 @@ bool directFileAccess = false; static std::string getIniPath() { #ifdef __ANDROID__ - return "/storage/emulated/0/Android/data/org.kkszysiu.devilutionx/diablo.ini"; + return "/sdcard/Android/data/org.diasurgical.devilutionx/diablo.ini"; #else char path[DVL_MAX_PATH]; int len = GetModuleFileNameA(ghInst, path, DVL_MAX_PATH);