1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-02 00:10:22 +02:00

fix single process build for Android

This commit is contained in:
Andrey Filipenkov 2023-02-26 12:18:24 +03:00
parent 71d51beab2
commit c4e7e91850
10 changed files with 58 additions and 33 deletions

View File

@ -36,6 +36,11 @@ class BattleStateInfoForRetreat;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END
// in static AI build this file gets included into libvcmi
#ifdef STATIC_AI
VCMI_LIB_USING_NAMESPACE
#endif
class CClient; class CClient;
struct lua_State; struct lua_State;

View File

@ -756,6 +756,9 @@ namespace vstd
} }
using vstd::operator-=; using vstd::operator-=;
VCMI_LIB_NAMESPACE_END
#ifdef NO_STD_TOSTRING #ifdef NO_STD_TOSTRING
namespace std namespace std
{ {
@ -768,5 +771,3 @@ namespace std
} }
} }
#endif // NO_STD_TOSTRING #endif // NO_STD_TOSTRING
VCMI_LIB_NAMESPACE_END

View File

@ -18,6 +18,7 @@ public final class LibsLoader
SDL.setContext(ctx); SDL.setContext(ctx);
} }
// not used in single-process build
public static void loadServerLibs() public static void loadServerLibs()
{ {
SDL.loadLibrary("vcmiserver"); SDL.loadLibrary("vcmiserver");

View File

@ -65,7 +65,7 @@ template<typename T> class CApplyOnLobby;
const std::string CServerHandler::localhostAddress{"127.0.0.1"}; 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; extern std::atomic_bool androidTestServerReadyFlag;
#endif #endif
@ -196,12 +196,7 @@ void CServerHandler::startLocalServerAndConnect()
//no connection means that port is not busy and we can start local server //no connection means that port is not busy and we can start local server
} }
#ifdef VCMI_ANDROID #if defined(SINGLE_PROCESS_APP)
{
CAndroidVMHelper envHelper;
envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "startServer", true);
}
#elif defined(SINGLE_PROCESS_APP)
boost::condition_variable cond; boost::condition_variable cond;
std::vector<std::string> args{"--uuid=" + uuid, "--port=" + boost::lexical_cast<std::string>(getHostPort())}; std::vector<std::string> args{"--uuid=" + uuid, "--port=" + boost::lexical_cast<std::string>(getHostPort())};
if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool()) if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool())
@ -217,6 +212,11 @@ void CServerHandler::startLocalServerAndConnect()
onServerFinished(); onServerFinished();
}); });
threadRunLocalServer->detach(); threadRunLocalServer->detach();
#elif defined(VCMI_ANDROID)
{
CAndroidVMHelper envHelper;
envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "startServer", true);
}
#else #else
threadRunLocalServer = std::make_shared<boost::thread>(&CServerHandler::threadRunServer, this); //runs server executable; threadRunLocalServer = std::make_shared<boost::thread>(&CServerHandler::threadRunServer, this); //runs server executable;
#endif #endif
@ -224,16 +224,7 @@ void CServerHandler::startLocalServerAndConnect()
th->update(); th->update();
#ifdef VCMI_ANDROID #ifdef SINGLE_PROCESS_APP
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 VCMI_IOS #ifdef VCMI_IOS
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
@ -253,6 +244,15 @@ void CServerHandler::startLocalServerAndConnect()
}); });
#endif #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 #else
if(shm) if(shm)
shm->sr->waitTillReady(); shm->sr->waitTillReady();

View File

@ -52,11 +52,11 @@
#ifdef VCMI_ANDROID #ifdef VCMI_ANDROID
#include "lib/CAndroidVMHelper.h" #include "lib/CAndroidVMHelper.h"
#endif
#ifdef VCMI_ANDROID #ifndef SINGLE_PROCESS_APP
std::atomic_bool androidTestServerReadyFlag; std::atomic_bool androidTestServerReadyFlag;
#endif #endif
#endif
ThreadSafeVector<int> CClient::waitingRequest; ThreadSafeVector<int> CClient::waitingRequest;
@ -778,6 +778,7 @@ void CClient::removeGUI()
} }
#ifdef VCMI_ANDROID #ifdef VCMI_ANDROID
#ifndef SINGLE_PROCESS_APP
extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_notifyServerClosed(JNIEnv * env, jclass cls) extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_notifyServerClosed(JNIEnv * env, jclass cls)
{ {
logNetwork->info("Received server closed signal"); 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"); logNetwork->info("Received server ready signal");
androidTestServerReadyFlag.store(true); androidTestServerReadyFlag.store(true);
} }
#endif
extern "C" JNIEXPORT jboolean JNICALL Java_eu_vcmi_vcmi_NativeMethods_tryToSaveTheGame(JNIEnv * env, jclass cls) extern "C" JNIEXPORT jboolean JNICALL Java_eu_vcmi_vcmi_NativeMethods_tryToSaveTheGame(JNIEnv * env, jclass cls)
{ {

View File

@ -10,6 +10,8 @@
#include "CAndroidVMHelper.h" #include "CAndroidVMHelper.h"
#ifdef VCMI_ANDROID #ifdef VCMI_ANDROID
VCMI_LIB_NAMESPACE_BEGIN
static JavaVM * vmCache = nullptr; static JavaVM * vmCache = nullptr;
/// cached java classloader so that we can find our classes from other threads /// 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;"); vcmiFindClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
} }
VCMI_LIB_NAMESPACE_END
#endif #endif

View File

@ -16,6 +16,8 @@
#include <jni.h> #include <jni.h>
#include <string> #include <string>
VCMI_LIB_NAMESPACE_BEGIN
/// helper class that allows access to java vm to communicate with java code from native /// helper class that allows access to java vm to communicate with java code from native
class DLL_LINKAGE CAndroidVMHelper class DLL_LINKAGE CAndroidVMHelper
{ {
@ -47,4 +49,6 @@ public:
static constexpr const char * NATIVE_METHODS_DEFAULT_CLASS = "eu/vcmi/vcmi/NativeMethods"; static constexpr const char * NATIVE_METHODS_DEFAULT_CLASS = "eu/vcmi/vcmi/NativeMethods";
}; };
VCMI_LIB_NAMESPACE_END
#endif #endif

View File

@ -13,6 +13,8 @@
#ifdef VCMI_ANDROID #ifdef VCMI_ANDROID
#include <android/log.h> #include <android/log.h>
VCMI_LIB_NAMESPACE_BEGIN
namespace ELogLevel namespace ELogLevel
{ {
int toAndroid(ELogLevel logLevel) int toAndroid(ELogLevel logLevel)
@ -30,6 +32,8 @@ namespace ELogLevel
return ANDROID_LOG_UNKNOWN; return ANDROID_LOG_UNKNOWN;
} }
} }
VCMI_LIB_NAMESPACE_END
#elif defined(VCMI_IOS) #elif defined(VCMI_IOS)
#import "iOS_utils.h" #import "iOS_utils.h"
extern "C" { extern "C" {

View File

@ -188,8 +188,10 @@ void CVCMIServer::run()
} }
#if defined(VCMI_ANDROID) #if defined(VCMI_ANDROID)
#ifndef SINGLE_PROCESS_APP
CAndroidVMHelper vmHelper; CAndroidVMHelper vmHelper;
vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "onServerReady"); vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "onServerReady");
#endif
#elif !defined(VCMI_IOS) #elif !defined(VCMI_IOS)
if(shm) if(shm)
{ {
@ -1024,7 +1026,7 @@ void handleLinuxSignal(int sig)
} }
#endif #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; namespace po = boost::program_options;
po::options_description opts("Allowed 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 #ifdef SINGLE_PROCESS_APP
#define main server_main #define main server_main
#endif #endif
#ifdef VCMI_ANDROID
#if VCMI_ANDROID_DUAL_PROCESS
void CVCMIServer::create() void CVCMIServer::create()
{ {
const int argc = 1; const int argc = 1;
char * argv[argc] = { "android-server" }; const char * argv[argc] = { "android-server" };
#else #else
int main(int argc, char * argv[]) int main(int argc, const char * argv[])
{ {
#endif #endif
@ -1121,7 +1124,7 @@ int main(int argc, char * argv[])
srand((ui32)time(nullptr)); srand((ui32)time(nullptr));
#ifdef SINGLE_PROCESS_APP #ifdef SINGLE_PROCESS_APP
boost::condition_variable * cond = reinterpret_cast<boost::condition_variable *>(argv[0]); boost::condition_variable * cond = reinterpret_cast<boost::condition_variable *>(const_cast<char *>(argv[0]));
cond->notify_one(); cond->notify_one();
#endif #endif
@ -1155,20 +1158,19 @@ int main(int argc, char * argv[])
//and return non-zero status so client can detect error //and return non-zero status so client can detect error
throw; throw;
} }
#ifdef VCMI_ANDROID #if VCMI_ANDROID_DUAL_PROCESS
CAndroidVMHelper envHelper; CAndroidVMHelper envHelper;
envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "killServer"); envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "killServer");
#endif #endif
logConfig.deconfigure(); logConfig.deconfigure();
vstd::clear_pointer(VLC); vstd::clear_pointer(VLC);
#ifndef VCMI_ANDROID #if !VCMI_ANDROID_DUAL_PROCESS
return 0; return 0;
#endif #endif
} }
#ifdef VCMI_ANDROID #if VCMI_ANDROID_DUAL_PROCESS
extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_createServer(JNIEnv * env, jclass cls) 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"); __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); CAndroidVMHelper::initClassloader(baseEnv);
} }
#elif defined(SINGLE_PROCESS_APP) #elif defined(SINGLE_PROCESS_APP)
void CVCMIServer::create(boost::condition_variable * cond, const std::vector<std::string> & args) void CVCMIServer::create(boost::condition_variable * cond, const std::vector<std::string> & args)
{ {
std::vector<const void *> argv = {cond}; std::vector<const void *> argv = {cond};
for(auto & a : args) for(auto & a : args)
argv.push_back(a.c_str()); argv.push_back(a.c_str());
main(argv.size(), reinterpret_cast<char **>(const_cast<void **>(&*argv.begin()))); main(argv.size(), reinterpret_cast<const char **>(&*argv.begin()));
} }
#endif #endif

View File

@ -14,6 +14,10 @@
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
#define VCMI_ANDROID_DUAL_PROCESS 1
#endif
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class CMapInfo; class CMapInfo;
@ -113,7 +117,7 @@ public:
ui8 getIdOfFirstUnallocatedPlayer() const; ui8 getIdOfFirstUnallocatedPlayer() const;
#ifdef VCMI_ANDROID #if VCMI_ANDROID_DUAL_PROCESS
static void create(); static void create();
#elif defined(SINGLE_PROCESS_APP) #elif defined(SINGLE_PROCESS_APP)
static void create(boost::condition_variable * cond, const std::vector<std::string> & args); static void create(boost::condition_variable * cond, const std::vector<std::string> & args);