2008-05-26 21:28:19 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
|
|
|
|
All rights reserved.
|
2008-09-27 16:46:05 +00:00
|
|
|
See the disclaimer in JNIEnvironment.h for licensing and distribution
|
|
|
|
conditions.
|
2008-05-26 21:28:19 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file Implementation of the JNI API for jAssimp */
|
|
|
|
|
|
|
|
#include "JNIEnvironment.h"
|
|
|
|
#include "JNILogger.h"
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
namespace JNIBridge {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool JNIEnvironment::AttachToCurrentThread (JNIEnv* pcEnv)
|
|
|
|
{
|
|
|
|
ai_assert(NULL != pcEnv);
|
|
|
|
|
|
|
|
// now initialize the thread-local storage
|
|
|
|
if (NULL == this->ptr.get())
|
|
|
|
{
|
|
|
|
// attach to the current thread
|
|
|
|
JavaVM* vm;
|
|
|
|
pcEnv->GetJavaVM(&vm);
|
|
|
|
vm->AttachCurrentThread((void **) &pcEnv, NULL);
|
|
|
|
|
|
|
|
this->ptr.reset(new JNIThreadData(pcEnv));
|
|
|
|
}
|
|
|
|
// increase the reference counter
|
|
|
|
else this->ptr->m_iNumRef++;
|
|
|
|
|
|
|
|
// attach the logger
|
|
|
|
((JNILogDispatcher*)DefaultLogger::get())->OnAttachToCurrentThread(this->ptr.get());
|
|
|
|
|
2008-08-07 22:55:02 +00:00
|
|
|
|
|
|
|
// get handles to all methods/fields/classes
|
|
|
|
this->Initialize();
|
|
|
|
|
2008-05-26 21:28:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool JNIEnvironment::DetachFromCurrentThread ()
|
|
|
|
{
|
|
|
|
ai_assert(NULL != pcEnv);
|
|
|
|
|
|
|
|
// detach the logger
|
|
|
|
((JNILogDispatcher*)DefaultLogger::get())->OnDetachFromCurrentThread(this->ptr.get());
|
|
|
|
|
|
|
|
// decrease the reference counter
|
|
|
|
if (NULL != this->ptr.get())
|
|
|
|
{
|
|
|
|
this->ptr->m_iNumRef--;
|
|
|
|
if (0 == this->ptr->m_iNumRef)
|
|
|
|
{
|
|
|
|
JavaVM* vm;
|
|
|
|
this->ptr->m_pcEnv->GetJavaVM(&vm);
|
|
|
|
vm->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
JNIThreadData* JNIEnvironment::GetThread()
|
|
|
|
{
|
|
|
|
ai_assert(NULL != this->ptr.get());
|
|
|
|
return this->ptr.get();
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-08-07 22:55:02 +00:00
|
|
|
void JNIEnvironment::_java::_lang::_String::Initialize()
|
|
|
|
{
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnv* pcEnv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
|
2008-08-07 22:55:02 +00:00
|
|
|
|
|
|
|
// first initialize some members
|
|
|
|
if( !(this->Class = pcEnv->FindClass("java.lang.String")))
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnvironment::Get()->ThrowNativeError("Unable to get handle of class java.lang.String");
|
2008-08-07 22:55:02 +00:00
|
|
|
|
|
|
|
if( !(this->getBytes = pcEnv->GetMethodID(this->Class,"getBytes","()[byte")))
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnvironment::Get()->ThrowNativeError("Unable to get handle of class java.lang.String");
|
2008-08-07 22:55:02 +00:00
|
|
|
|
|
|
|
if( !(this->constructor_ByteArray = pcEnv->GetStaticMethodID(
|
|
|
|
this->Class,"<init>","([byte)V")))
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnvironment::Get()->ThrowNativeError("Unable to get handle of class java.lang.String");
|
2008-08-07 22:55:02 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void JNIEnvironment::_java::_lang::_Array::Initialize()
|
|
|
|
{
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnv* pcEnv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
|
2008-08-07 22:55:02 +00:00
|
|
|
|
|
|
|
if( !(this->FloatArray_Class = pcEnv->FindClass("[F")))
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnvironment::Get()->ThrowNativeError("Unable to get handle of class float[]");
|
2008-08-07 22:55:02 +00:00
|
|
|
|
|
|
|
if( !(this->IntArray_Class = pcEnv->FindClass("[I")))
|
2008-09-27 16:46:05 +00:00
|
|
|
JNIEnvironment::Get()->ThrowNativeError("Unable to get handle of class int[]");
|
2008-08-07 22:55:02 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-05-26 21:28:19 +00:00
|
|
|
jstring JNU_NewStringNative(JNIEnv *env, const char *str)
|
|
|
|
{
|
|
|
|
jstring result;
|
|
|
|
jbyteArray bytes = 0;
|
|
|
|
int len;
|
|
|
|
if (env->EnsureLocalCapacity( 2) < 0)
|
|
|
|
{
|
|
|
|
return NULL; /* out of memory error */
|
|
|
|
}
|
2008-09-27 16:46:05 +00:00
|
|
|
len = (int)::strlen(str);
|
2008-05-26 21:28:19 +00:00
|
|
|
bytes = env->NewByteArray(len);
|
|
|
|
if (bytes != NULL)
|
|
|
|
{
|
|
|
|
env->SetByteArrayRegion(bytes, 0, len,
|
|
|
|
(jbyte *)str);
|
2008-08-07 22:55:02 +00:00
|
|
|
result = (jstring)env->NewObject(AIJ_GET_HANDLE(java.lang.String.Class),
|
|
|
|
AIJ_GET_HANDLE(java.lang.String.constructor_ByteArray), bytes);
|
2008-05-26 21:28:19 +00:00
|
|
|
env->DeleteLocalRef(bytes);
|
|
|
|
return result;
|
|
|
|
} /* else fall through */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr)
|
|
|
|
{
|
|
|
|
jbyteArray bytes = 0;
|
|
|
|
jthrowable exc;
|
|
|
|
char *result = 0;
|
|
|
|
if (env->EnsureLocalCapacity(2) < 0)
|
|
|
|
{
|
|
|
|
return 0; /* out of memory error */
|
|
|
|
}
|
|
|
|
|
2008-08-07 22:55:02 +00:00
|
|
|
bytes = (jbyteArray)env->CallObjectMethod(jstr,AIJ_GET_HANDLE(java.lang.String.getBytes));
|
2008-05-26 21:28:19 +00:00
|
|
|
exc = env->ExceptionOccurred();
|
|
|
|
if (!exc)
|
|
|
|
{
|
|
|
|
jint len = env->GetArrayLength(bytes);
|
|
|
|
result = (char *)malloc(len + 1);
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
env->DeleteLocalRef(bytes);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
env->GetByteArrayRegion(bytes, 0, len,
|
|
|
|
(jbyte *)result);
|
|
|
|
result[len] = 0; /* NULL-terminate */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
env->DeleteLocalRef(exc);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(bytes);
|
|
|
|
return result;
|
|
|
|
}
|
2008-08-07 22:55:02 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-09-27 16:46:05 +00:00
|
|
|
void JNU_CopyDataToArray(JNIEnv* pc, jarray jfl, void* data, unsigned int size)
|
2008-08-07 22:55:02 +00:00
|
|
|
{
|
|
|
|
void* pf;
|
|
|
|
jboolean iscopy = FALSE;
|
|
|
|
|
|
|
|
// lock the array and get direct access to its buffer
|
2008-09-27 16:46:05 +00:00
|
|
|
if(!(pf = pc->GetPrimitiveArrayCritical(jfl,&iscopy)))
|
2008-08-07 22:55:02 +00:00
|
|
|
JNIEnvironment::Get()->ThrowNativeError("Unable to lock array");
|
|
|
|
|
|
|
|
// copy the data to the array
|
|
|
|
::memcpy(pf,data,size);
|
|
|
|
|
|
|
|
// release our reference to the array
|
|
|
|
pc->ReleasePrimitiveArrayCritical(jfl,pf,0);
|
|
|
|
}
|
2008-09-27 16:46:05 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void JNU_CopyObjectArrayToVM(JNIEnv* pc, const void** in, unsigned int num,
|
|
|
|
JNIEnvironment::_Base& type, jobjectArray& out)
|
|
|
|
{
|
|
|
|
jobjectArray jarr = pc->NewObjectArray(num,type.Class,0);
|
|
|
|
for (unsigned int i = 0; i < num;++i)
|
|
|
|
{
|
|
|
|
jobject jobj = pc->NewObject(type.Class,type.DefaultCtor);
|
|
|
|
|
|
|
|
type.Fill(jobj,in[i]);
|
|
|
|
pc->SetObjectArrayElement(jarr,i,jobj);
|
|
|
|
}
|
|
|
|
out = jarr;
|
|
|
|
}
|
2008-05-26 21:28:19 +00:00
|
|
|
|
|
|
|
};};
|
|
|
|
|
2008-08-07 22:55:02 +00:00
|
|
|
|