From c40af4c4ab0541638a6079a97c601a774ed65d02 Mon Sep 17 00:00:00 2001 From: faketruth Date: Fri, 17 Aug 2012 22:20:35 +0000 Subject: Android: Pressing back button sends stop command to server Android: ToJava can call functions on Java by using JNI Android: Plugins work Android: Added android specific files to VS2008 project, but they are excluded from compiling git-svn-id: http://mc-server.googlecode.com/svn/trunk@747 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- VC2008/MCServer.vcproj | 64 ++++++++++++++++++++++++++++++++++ jni/Android.mk | 2 +- jni/ToJava.cpp | 3 ++ jni/ToJava.h | 47 +++++++++++++++++++++++++ jni/app-android.cpp | 57 ++++++++---------------------- source/cLog.cpp | 2 ++ source/cPlugin_NewLua.cpp | 2 +- src/com/mcserver/MCServerActivity.java | 39 ++++++++++++++++----- 8 files changed, 163 insertions(+), 53 deletions(-) create mode 100644 jni/ToJava.cpp create mode 100644 jni/ToJava.h diff --git a/VC2008/MCServer.vcproj b/VC2008/MCServer.vcproj index fec83695d..90ceaca56 100644 --- a/VC2008/MCServer.vcproj +++ b/VC2008/MCServer.vcproj @@ -1620,6 +1620,70 @@ RelativePath="..\source\cTimer.h" > + + + + + + + + + + + + + + + + + + + + + + + + + + + +#include +extern JNIEnv* g_CurrentJNIEnv; + +extern jobject g_JavaThread; +//extern jobject g_JavaActivity; + +//__android_log_vprint(ANDROID_LOG_ERROR,"MCServer", a_Format, argList); + +static void CallJavaFunction_Void_String( jobject a_Object, const std::string & a_FunctionName, const std::string & a_StringParam ) +{ + //__android_log_print(ANDROID_LOG_ERROR,"MCServer", "JNIEnv: %i Object: %i", g_CurrentJNIEnv, a_Object ); + jclass cls = g_CurrentJNIEnv->GetObjectClass( a_Object ); + //__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jclass: %i", cls ); + jmethodID mid = g_CurrentJNIEnv->GetMethodID( cls, a_FunctionName.c_str(), "(Ljava/lang/String;)V"); // void a_FunctionName( String ) + //__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jmethodID: %i", mid ); + if (mid != 0) + { + //__android_log_print(ANDROID_LOG_ERROR,"MCServer", "Going to call right NOW! %s", a_FunctionName.c_str() ); + g_CurrentJNIEnv->CallVoidMethod( a_Object, mid, g_CurrentJNIEnv->NewStringUTF( a_StringParam.c_str() ) ); + } + else + { + __android_log_print(ANDROID_LOG_ERROR,"MCServer", "It was 0, derp" ); + } +} + + +static void CallJavaFunction_Void_Void( jobject a_Object, const std::string & a_FunctionName ) +{ + __android_log_print(ANDROID_LOG_ERROR,"MCServer", "JNIEnv: %i Object: %i", g_CurrentJNIEnv, a_Object ); + jclass cls = g_CurrentJNIEnv->GetObjectClass( a_Object ); + __android_log_print(ANDROID_LOG_ERROR,"MCServer", "jclass: %i", cls ); + jmethodID mid = g_CurrentJNIEnv->GetMethodID( cls, a_FunctionName.c_str(), "()V"); // void a_FunctionName( String ) + __android_log_print(ANDROID_LOG_ERROR,"MCServer", "jmethodID: %i", mid ); + if (mid != 0) + { + __android_log_print(ANDROID_LOG_ERROR,"MCServer", "Going to call right NOW! %s", a_FunctionName.c_str() ); + g_CurrentJNIEnv->CallVoidMethod( a_Object, mid ); + } + else + { + __android_log_print(ANDROID_LOG_ERROR,"MCServer", "It was 0, derp" ); + } +} \ No newline at end of file diff --git a/jni/app-android.cpp b/jni/app-android.cpp index d02947440..461f433bf 100644 --- a/jni/app-android.cpp +++ b/jni/app-android.cpp @@ -13,24 +13,34 @@ #include "cCriticalSection.h" #include "cRoot.h" #include "cMakeDir.h" +#include "ToJava.h" #include cCriticalSection g_CriticalSection; JNIEnv* g_CurrentJNIEnv = 0; -jobject g_JavaRenderer = 0; +jobject g_JavaThread = 0; +//jobject g_JavaActivity = 0; cRoot * pRoot = NULL; + + + + /* Called when program/activity is created */ -extern "C" void Java_com_mcserver_MainThread_NativeOnCreate( JNIEnv* env ) +extern "C" void Java_com_mcserver_MainThread_NativeOnCreate( JNIEnv* env, jobject thiz ) { g_CriticalSection.Lock(); g_CurrentJNIEnv = env; + g_JavaThread = thiz; //if( !cLogger::GetSingletonPtr() ) new cLogger(); __android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "Logging from C++!"); g_CriticalSection.Unlock(); + + //CallJavaFunction_Void_Void(g_JavaActivity, "TestTest" ); + //CallJavaFunction_Void_String(g_JavaThread, "AddToLog", "herpderpderp!!" ); mkdir("/sdcard/mcserver", S_IRWXU | S_IRWXG | S_IRWXO); @@ -39,57 +49,20 @@ extern "C" void Java_com_mcserver_MainThread_NativeOnCreate( JNIEnv* env ) delete pRoot; } -extern "C" void Java_com_mcserver_MCServerActivity_NativeCleanUp( JNIEnv* env ) -{ - g_CriticalSection.Lock(); - g_CurrentJNIEnv = env; - - g_CriticalSection.Unlock(); - pRoot->ServerCommand("stop"); -} -/* Call to initialize the graphics state */ -extern "C" void Java_com_ballz_CppWrapperRenderer_NativeInitGL( JNIEnv* env, jobject thiz ) -{ - g_CriticalSection.Lock(); - g_CurrentJNIEnv = env; - g_JavaRenderer = thiz; - g_CriticalSection.Unlock(); -} -extern "C" void Java_com_ballz_CppWrapperRenderer_NativeResize( JNIEnv* env, jobject thiz, jint w, jint h ) +extern "C" void Java_com_mcserver_MainThread_NativeCleanUp( JNIEnv* env, jobject thiz ) { g_CriticalSection.Lock(); g_CurrentJNIEnv = env; - g_JavaRenderer = thiz; - + g_JavaThread = thiz; g_CriticalSection.Unlock(); -} - -extern "C" void Java_com_ballz_CppWrapperRenderer_NativeRender( JNIEnv* env, jobject thiz ) -{ - g_CriticalSection.Lock(); - g_CurrentJNIEnv = env; - g_JavaRenderer = thiz; - - g_CriticalSection.Unlock(); + pRoot->ServerCommand("stop"); } -extern "C" void Java_com_ballz_CppWrapperGLSurfaceView_NativeTouchScreen( JNIEnv* env, jobject thiz, jint mouseid, jint touched ) -{ - g_CriticalSection.Lock(); - g_CurrentJNIEnv = env; - g_CriticalSection.Unlock(); -} -extern "C" void Java_com_ballz_CppWrapperGLSurfaceView_NativeTouchEvent( JNIEnv* env, jobject thiz, jint mouseid, jfloat x, jfloat y ) -{ - g_CriticalSection.Lock(); - g_CurrentJNIEnv = env; - g_CriticalSection.Unlock(); -} \ No newline at end of file diff --git a/source/cLog.cpp b/source/cLog.cpp index d1874f7e2..57ad9b418 100644 --- a/source/cLog.cpp +++ b/source/cLog.cpp @@ -11,6 +11,7 @@ #if defined(ANDROID_NDK) #include +#include "ToJava.h" #endif @@ -130,6 +131,7 @@ void cLog::Log(const char * a_Format, va_list argList) // Print to console: #if defined(ANDROID_NDK) __android_log_vprint(ANDROID_LOG_ERROR,"MCServer", a_Format, argList); + //CallJavaFunction_Void_String(g_JavaThread, "AddToLog", Line ); #else printf("%s", Line.c_str()); #endif diff --git a/source/cPlugin_NewLua.cpp b/source/cPlugin_NewLua.cpp index feba602ba..7f7f0c20c 100644 --- a/source/cPlugin_NewLua.cpp +++ b/source/cPlugin_NewLua.cpp @@ -73,7 +73,7 @@ bool cPlugin_NewLua::Initialize() ManualBindings::Bind( m_LuaState ); } - std::string PluginPath = std::string("Plugins/") + m_Directory + "/"; + std::string PluginPath = FILE_IO_PREFIX + std::string("Plugins/") + m_Directory + "/"; // Load all files for this plugin, and execute them AStringList Files = GetDirectoryContents(PluginPath.c_str()); diff --git a/src/com/mcserver/MCServerActivity.java b/src/com/mcserver/MCServerActivity.java index f7bfc76e3..ff1a4fae2 100644 --- a/src/com/mcserver/MCServerActivity.java +++ b/src/com/mcserver/MCServerActivity.java @@ -2,35 +2,42 @@ package com.mcserver; import android.app.Activity; import android.os.Bundle; +import android.util.Log; import android.view.KeyEvent; public class MCServerActivity extends Activity { + MainThread mThread = null; + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); - MainThread p = new MainThread(); - p.start(); + mThread = new MainThread(); + mThread.start(); } - public boolean onKeyDown(int keyCode, KeyEvent event) - { + public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK) { //android.os.Process.killProcess(android.os.Process.myPid()); - NativeCleanUp(); + mThread.NativeCleanUp(); return super.onKeyDown(keyCode, event); } return false; } + public void AddToLog( String logMessage ) { + + } + static { System.loadLibrary("mcserver"); } - private static native void NativeCleanUp(); + + } @@ -39,9 +46,23 @@ class MainThread extends Thread { MainThread() { } + public void AddToLog( String logMessage ) { + //Log.d("MCServer", "Add to log: " + logMessage); + } + + public void TestTest(){ + Log.d("MCServer", "in testtest"); + } + public void run() { NativeOnCreate(); } - - private static native void NativeOnCreate(); -} \ No newline at end of file + public native void NativeOnCreate(); + public native void NativeCleanUp(); +} + + + + + + -- cgit v1.2.3