From c4e7e91850d2a5d9ae19553d0a2f7c2aa57e1d30 Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Sun, 26 Feb 2023 12:18:24 +0300 Subject: [PATCH] fix single process build for Android --- CCallback.h | 5 +++ Global.h | 5 +-- .../java/eu/vcmi/vcmi/util/LibsLoader.java | 1 + client/CServerHandler.cpp | 34 +++++++++---------- client/Client.cpp | 6 ++-- lib/CAndroidVMHelper.cpp | 3 ++ lib/CAndroidVMHelper.h | 4 +++ lib/logging/CLogger.cpp | 4 +++ server/CVCMIServer.cpp | 23 +++++++------ server/CVCMIServer.h | 6 +++- 10 files changed, 58 insertions(+), 33 deletions(-) diff --git a/CCallback.h b/CCallback.h index 419957a86..f8ddf2555 100644 --- a/CCallback.h +++ b/CCallback.h @@ -36,6 +36,11 @@ class BattleStateInfoForRetreat; VCMI_LIB_NAMESPACE_END +// in static AI build this file gets included into libvcmi +#ifdef STATIC_AI +VCMI_LIB_USING_NAMESPACE +#endif + class CClient; struct lua_State; diff --git a/Global.h b/Global.h index c1ca57ff1..554327ecd 100644 --- a/Global.h +++ b/Global.h @@ -756,6 +756,9 @@ namespace vstd } using vstd::operator-=; +VCMI_LIB_NAMESPACE_END + + #ifdef NO_STD_TOSTRING namespace std { @@ -768,5 +771,3 @@ namespace std } } #endif // NO_STD_TOSTRING - -VCMI_LIB_NAMESPACE_END diff --git a/android/vcmi-app/src/main/java/eu/vcmi/vcmi/util/LibsLoader.java b/android/vcmi-app/src/main/java/eu/vcmi/vcmi/util/LibsLoader.java index 1df9bd4ff..8bb04182a 100644 --- a/android/vcmi-app/src/main/java/eu/vcmi/vcmi/util/LibsLoader.java +++ b/android/vcmi-app/src/main/java/eu/vcmi/vcmi/util/LibsLoader.java @@ -18,6 +18,7 @@ public final class LibsLoader SDL.setContext(ctx); } + // not used in single-process build public static void loadServerLibs() { SDL.loadLibrary("vcmiserver"); diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index bf5b0194e..31a332c3c 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -65,7 +65,7 @@ template class CApplyOnLobby; const std::string CServerHandler::localhostAddress{"127.0.0.1"}; -#ifdef VCMI_ANDROID +#if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP) extern std::atomic_bool androidTestServerReadyFlag; #endif @@ -196,12 +196,7 @@ void CServerHandler::startLocalServerAndConnect() //no connection means that port is not busy and we can start local server } -#ifdef VCMI_ANDROID - { - CAndroidVMHelper envHelper; - envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "startServer", true); - } -#elif defined(SINGLE_PROCESS_APP) +#if defined(SINGLE_PROCESS_APP) boost::condition_variable cond; std::vector args{"--uuid=" + uuid, "--port=" + boost::lexical_cast(getHostPort())}; if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool()) @@ -217,6 +212,11 @@ void CServerHandler::startLocalServerAndConnect() onServerFinished(); }); threadRunLocalServer->detach(); +#elif defined(VCMI_ANDROID) + { + CAndroidVMHelper envHelper; + envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "startServer", true); + } #else threadRunLocalServer = std::make_shared(&CServerHandler::threadRunServer, this); //runs server executable; #endif @@ -224,16 +224,7 @@ void CServerHandler::startLocalServerAndConnect() th->update(); -#ifdef VCMI_ANDROID - logNetwork->info("waiting for server"); - while(!androidTestServerReadyFlag.load()) - { - logNetwork->info("still waiting..."); - boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); - } - logNetwork->info("waiting for server finished..."); - androidTestServerReadyFlag = false; -#elif defined(SINGLE_PROCESS_APP) +#ifdef SINGLE_PROCESS_APP { #ifdef VCMI_IOS dispatch_sync(dispatch_get_main_queue(), ^{ @@ -253,6 +244,15 @@ void CServerHandler::startLocalServerAndConnect() }); #endif } +#elif defined(VCMI_ANDROID) + logNetwork->info("waiting for server"); + while(!androidTestServerReadyFlag.load()) + { + logNetwork->info("still waiting..."); + boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); + } + logNetwork->info("waiting for server finished..."); + androidTestServerReadyFlag = false; #else if(shm) shm->sr->waitTillReady(); diff --git a/client/Client.cpp b/client/Client.cpp index c16c767ee..250a64742 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -52,11 +52,11 @@ #ifdef VCMI_ANDROID #include "lib/CAndroidVMHelper.h" -#endif -#ifdef VCMI_ANDROID +#ifndef SINGLE_PROCESS_APP std::atomic_bool androidTestServerReadyFlag; #endif +#endif ThreadSafeVector CClient::waitingRequest; @@ -778,6 +778,7 @@ void CClient::removeGUI() } #ifdef VCMI_ANDROID +#ifndef SINGLE_PROCESS_APP extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_notifyServerClosed(JNIEnv * env, jclass cls) { logNetwork->info("Received server closed signal"); @@ -791,6 +792,7 @@ extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_notifyServerRe logNetwork->info("Received server ready signal"); androidTestServerReadyFlag.store(true); } +#endif extern "C" JNIEXPORT jboolean JNICALL Java_eu_vcmi_vcmi_NativeMethods_tryToSaveTheGame(JNIEnv * env, jclass cls) { diff --git a/lib/CAndroidVMHelper.cpp b/lib/CAndroidVMHelper.cpp index 34428668b..59c690c21 100644 --- a/lib/CAndroidVMHelper.cpp +++ b/lib/CAndroidVMHelper.cpp @@ -10,6 +10,8 @@ #include "CAndroidVMHelper.h" #ifdef VCMI_ANDROID +VCMI_LIB_NAMESPACE_BEGIN + static JavaVM * vmCache = nullptr; /// cached java classloader so that we can find our classes from other threads @@ -109,4 +111,5 @@ void CAndroidVMHelper::initClassloader(void * baseEnv) vcmiFindClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;"); } +VCMI_LIB_NAMESPACE_END #endif diff --git a/lib/CAndroidVMHelper.h b/lib/CAndroidVMHelper.h index 92f0f3736..625677e2d 100644 --- a/lib/CAndroidVMHelper.h +++ b/lib/CAndroidVMHelper.h @@ -16,6 +16,8 @@ #include #include +VCMI_LIB_NAMESPACE_BEGIN + /// helper class that allows access to java vm to communicate with java code from native class DLL_LINKAGE CAndroidVMHelper { @@ -47,4 +49,6 @@ public: static constexpr const char * NATIVE_METHODS_DEFAULT_CLASS = "eu/vcmi/vcmi/NativeMethods"; }; +VCMI_LIB_NAMESPACE_END + #endif diff --git a/lib/logging/CLogger.cpp b/lib/logging/CLogger.cpp index 1405f0623..85b49b4b5 100644 --- a/lib/logging/CLogger.cpp +++ b/lib/logging/CLogger.cpp @@ -13,6 +13,8 @@ #ifdef VCMI_ANDROID #include +VCMI_LIB_NAMESPACE_BEGIN + namespace ELogLevel { int toAndroid(ELogLevel logLevel) @@ -30,6 +32,8 @@ namespace ELogLevel return ANDROID_LOG_UNKNOWN; } } + +VCMI_LIB_NAMESPACE_END #elif defined(VCMI_IOS) #import "iOS_utils.h" extern "C" { diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index bd5771804..b75a2cfff 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -188,8 +188,10 @@ void CVCMIServer::run() } #if defined(VCMI_ANDROID) +#ifndef SINGLE_PROCESS_APP CAndroidVMHelper vmHelper; vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "onServerReady"); +#endif #elif !defined(VCMI_IOS) if(shm) { @@ -1024,7 +1026,7 @@ void handleLinuxSignal(int sig) } #endif -static void handleCommandOptions(int argc, char * argv[], boost::program_options::variables_map & options) +static void handleCommandOptions(int argc, const char * argv[], boost::program_options::variables_map & options) { namespace po = boost::program_options; po::options_description opts("Allowed options"); @@ -1084,13 +1086,14 @@ static void handleCommandOptions(int argc, char * argv[], boost::program_options #ifdef SINGLE_PROCESS_APP #define main server_main #endif -#ifdef VCMI_ANDROID + +#if VCMI_ANDROID_DUAL_PROCESS void CVCMIServer::create() { const int argc = 1; - char * argv[argc] = { "android-server" }; + const char * argv[argc] = { "android-server" }; #else -int main(int argc, char * argv[]) +int main(int argc, const char * argv[]) { #endif @@ -1121,7 +1124,7 @@ int main(int argc, char * argv[]) srand((ui32)time(nullptr)); #ifdef SINGLE_PROCESS_APP - boost::condition_variable * cond = reinterpret_cast(argv[0]); + boost::condition_variable * cond = reinterpret_cast(const_cast(argv[0])); cond->notify_one(); #endif @@ -1155,20 +1158,19 @@ int main(int argc, char * argv[]) //and return non-zero status so client can detect error throw; } -#ifdef VCMI_ANDROID +#if VCMI_ANDROID_DUAL_PROCESS CAndroidVMHelper envHelper; envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "killServer"); #endif logConfig.deconfigure(); vstd::clear_pointer(VLC); -#ifndef VCMI_ANDROID +#if !VCMI_ANDROID_DUAL_PROCESS return 0; #endif } -#ifdef VCMI_ANDROID - +#if VCMI_ANDROID_DUAL_PROCESS extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_createServer(JNIEnv * env, jclass cls) { __android_log_write(ANDROID_LOG_INFO, "VCMI", "Got jni call to init server"); @@ -1181,13 +1183,12 @@ extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_initClassloade { CAndroidVMHelper::initClassloader(baseEnv); } - #elif defined(SINGLE_PROCESS_APP) void CVCMIServer::create(boost::condition_variable * cond, const std::vector & args) { std::vector argv = {cond}; for(auto & a : args) argv.push_back(a.c_str()); - main(argv.size(), reinterpret_cast(const_cast(&*argv.begin()))); + main(argv.size(), reinterpret_cast(&*argv.begin())); } #endif diff --git a/server/CVCMIServer.h b/server/CVCMIServer.h index b2fa7d703..35e1a5122 100644 --- a/server/CVCMIServer.h +++ b/server/CVCMIServer.h @@ -14,6 +14,10 @@ #include +#if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP) +#define VCMI_ANDROID_DUAL_PROCESS 1 +#endif + VCMI_LIB_NAMESPACE_BEGIN class CMapInfo; @@ -113,7 +117,7 @@ public: ui8 getIdOfFirstUnallocatedPlayer() const; -#ifdef VCMI_ANDROID +#if VCMI_ANDROID_DUAL_PROCESS static void create(); #elif defined(SINGLE_PROCESS_APP) static void create(boost::condition_variable * cond, const std::vector & args);