1
0

MCServer should run just fine on Android now :D

The server is also stoppable from Android

git-svn-id: http://mc-server.googlecode.com/svn/trunk@743 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
faketruth 2012-08-16 20:28:14 +00:00
parent 1c4c0b5698
commit 33ca4d5003
11 changed files with 54 additions and 20 deletions

View File

@ -6,7 +6,9 @@
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk android:minSdkVersion="10" /> <uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<application <application
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"

View File

@ -13,6 +13,9 @@ public final class R {
public static final class drawable { public static final class drawable {
public static final int ic_launcher=0x7f020000; public static final int ic_launcher=0x7f020000;
} }
public static final class id {
public static final int textView1=0x7f050000;
}
public static final class layout { public static final class layout {
public static final int main=0x7f030000; public static final int main=0x7f030000;
} }

View File

@ -59,7 +59,7 @@ bool cIniFile::ReadFile()
string keyname, valuename, value; string keyname, valuename, value;
string::size_type pLeft, pRight; string::size_type pLeft, pRight;
f.open( path.c_str(), ios::in); f.open( (FILE_IO_PREFIX + path).c_str(), ios::in);
if ( f.fail()) if ( f.fail())
return false; return false;
@ -127,7 +127,7 @@ bool cIniFile::WriteFile()
// a few bugs with ofstream. So ... fstream used. // a few bugs with ofstream. So ... fstream used.
fstream f; fstream f;
f.open( path.c_str(), ios::out); f.open( (FILE_IO_PREFIX + path).c_str(), ios::out);
if ( f.fail()) if ( f.fail())
return false; return false;

View File

@ -12,6 +12,7 @@
#include "cCriticalSection.h" #include "cCriticalSection.h"
#include "cRoot.h" #include "cRoot.h"
#include "cMakeDir.h"
#include <android/log.h> #include <android/log.h>
@ -20,17 +21,22 @@ cCriticalSection g_CriticalSection;
JNIEnv* g_CurrentJNIEnv = 0; JNIEnv* g_CurrentJNIEnv = 0;
jobject g_JavaRenderer = 0; jobject g_JavaRenderer = 0;
cRoot * pRoot = NULL;
/* Called when program/activity is created */ /* 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 )
{ {
g_CriticalSection.Lock(); g_CriticalSection.Lock();
g_CurrentJNIEnv = env; g_CurrentJNIEnv = env;
//if( !cLogger::GetSingletonPtr() ) new cLogger(); //if( !cLogger::GetSingletonPtr() ) new cLogger();
__android_log_print(ANDROID_LOG_ERROR,"Arashi", "%s", "Logging from C++!"); __android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "Logging from C++!");
g_CriticalSection.Unlock(); g_CriticalSection.Unlock();
cRoot Root; mkdir("/sdcard/mcserver", S_IRWXU | S_IRWXG | S_IRWXO);
Root.Start();
pRoot = new cRoot();
pRoot->Start();
delete pRoot;
} }
extern "C" void Java_com_mcserver_MCServerActivity_NativeCleanUp( JNIEnv* env ) extern "C" void Java_com_mcserver_MCServerActivity_NativeCleanUp( JNIEnv* env )
@ -39,6 +45,8 @@ extern "C" void Java_com_mcserver_MCServerActivity_NativeCleanUp( JNIEnv* env )
g_CurrentJNIEnv = env; g_CurrentJNIEnv = env;
g_CriticalSection.Unlock(); g_CriticalSection.Unlock();
pRoot->ServerCommand("stop");
} }
/* Call to initialize the graphics state */ /* Call to initialize the graphics state */

View File

@ -3,8 +3,9 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" > android:orientation="vertical" >
<TextView <TextView
android:id="@+id/textView1"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/hello" /> android:text="@string/hello" />

View File

@ -123,6 +123,12 @@ typedef short Int16;
#define USE_SQUIRREL #define USE_SQUIRREL
#endif #endif
#if defined(ANDROID_NDK)
#define FILE_IO_PREFIX "/sdcard/mcserver/"
#else
#define FILE_IO_PREFIX ""
#endif

View File

@ -76,14 +76,14 @@ bool cFile::Open(const AString & iFileName, EMode iMode)
return false; return false;
} }
} }
m_File = fopen(iFileName.c_str(), Mode); m_File = fopen( (FILE_IO_PREFIX + iFileName).c_str(), Mode);
if ((m_File == NULL) && (iMode == fmReadWrite)) if ((m_File == NULL) && (iMode == fmReadWrite))
{ {
// Fix for MS not following C spec, opening "a" mode files for writing at the end only // Fix for MS not following C spec, opening "a" mode files for writing at the end only
// The file open operation has been tried with "read update", fails if file not found // The file open operation has been tried with "read update", fails if file not found
// So now we know either the file doesn't exist or we don't have rights, no need to worry about file contents. // So now we know either the file doesn't exist or we don't have rights, no need to worry about file contents.
// Simply re-open for read-writing, erasing existing contents: // Simply re-open for read-writing, erasing existing contents:
m_File = fopen(iFileName.c_str(), "wb+"); m_File = fopen( (FILE_IO_PREFIX + iFileName).c_str(), "wb+");
} }
return (m_File != NULL); return (m_File != NULL);
} }

View File

@ -26,7 +26,7 @@ cLog::cLog(const AString & a_FileName )
// create logs directory // create logs directory
cMakeDir::MakeDir("logs"); cMakeDir::MakeDir("logs");
OpenLog( (std::string("logs/") + a_FileName).c_str() ); OpenLog( (FILE_IO_PREFIX + std::string("logs/") + a_FileName).c_str() );
} }
@ -100,10 +100,6 @@ void cLog::ClearLog()
void cLog::Log(const char * a_Format, va_list argList) void cLog::Log(const char * a_Format, va_list argList)
{ {
#if defined(ANDROID_NDK)
__android_log_vprint(ANDROID_LOG_ERROR,"MCServer", a_Format, argList);
return; // This is as far as android goes
#endif
AString Message; AString Message;
AppendVPrintf(Message, a_Format, argList); AppendVPrintf(Message, a_Format, argList);
@ -132,8 +128,12 @@ void cLog::Log(const char * a_Format, va_list argList)
} }
// Print to console: // Print to console:
#if defined(ANDROID_NDK)
__android_log_vprint(ANDROID_LOG_ERROR,"MCServer", a_Format, argList);
#else
printf("%s", Line.c_str()); printf("%s", Line.c_str());
#endif
#if defined (_WIN32) && defined(_DEBUG) #if defined (_WIN32) && defined(_DEBUG)
// In a Windows Debug build, output the log to debug console as well: // In a Windows Debug build, output the log to debug console as well:
OutputDebugString(Line.c_str()); OutputDebugString(Line.c_str());

View File

@ -14,9 +14,9 @@ void cMakeDir::MakeDir(const AString & a_Directory)
Attrib.nLength = sizeof(SECURITY_ATTRIBUTES); Attrib.nLength = sizeof(SECURITY_ATTRIBUTES);
Attrib.lpSecurityDescriptor = NULL; Attrib.lpSecurityDescriptor = NULL;
Attrib.bInheritHandle = false; Attrib.bInheritHandle = false;
::CreateDirectory(a_Directory.c_str(), &Attrib); ::CreateDirectory( (FILE_IO_PREFIX + a_Directory).c_str(), &Attrib);
#else #else
mkdir(a_Directory.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); mkdir( (FILE_IO_PREFIX + a_Directory).c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif #endif
} }

View File

@ -77,6 +77,9 @@ cRoot::~cRoot()
void cRoot::InputThread(void* a_Params) void cRoot::InputThread(void* a_Params)
{ {
#if defined(ANDROID_NDK)
return;
#else
cRoot& self = *(cRoot*)a_Params; cRoot& self = *(cRoot*)a_Params;
while( !(self.m_bStop || self.m_bRestart) ) while( !(self.m_bStop || self.m_bRestart) )
@ -85,6 +88,7 @@ void cRoot::InputThread(void* a_Params)
std::getline(std::cin, Command); std::getline(std::cin, Command);
self.ServerCommand( Command.c_str() ); self.ServerCommand( Command.c_str() );
} }
#endif
} }

View File

@ -2,6 +2,7 @@ package com.mcserver;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.view.KeyEvent;
public class MCServerActivity extends Activity { public class MCServerActivity extends Activity {
/** Called when the activity is first created. */ /** Called when the activity is first created. */
@ -12,16 +13,25 @@ public class MCServerActivity extends Activity {
MainThread p = new MainThread(); MainThread p = new MainThread();
p.start(); p.start();
//NativeOnCreate();
} }
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
//android.os.Process.killProcess(android.os.Process.myPid());
NativeCleanUp();
return super.onKeyDown(keyCode, event);
}
return false;
}
static { static {
System.loadLibrary("mcserver"); System.loadLibrary("mcserver");
} }
private static native void NativeOnCreate(); private static native void NativeCleanUp();
} }