@@ -13,26 +13,34 @@ extern "C" void init_gles2_shim();
1313using namespace Live2D ::Cubism::Framework;
1414
1515static JavaVM* g_jvm = nullptr ;
16+ static jclass g_libraryLoaderClass = nullptr ;
17+ static jmethodID g_loadResourceMethod = nullptr ;
18+ static jclass g_cubismFrameworkClass = nullptr ;
19+ static jmethodID g_onLogMethod = nullptr ;
1620
1721static csmByte* LoadFile (const std::string filePath, csmSizeInt* outSize) {
1822 JNIEnv* env;
1923 bool attached = false ;
20- if (g_jvm->GetEnv ((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
24+ jint res = g_jvm->GetEnv ((void **)&env, JNI_VERSION_1_6);
25+ if (res == JNI_EDETACHED) {
2126 g_jvm->AttachCurrentThread ((void **)&env, nullptr );
2227 attached = true ;
28+ } else if (res != JNI_OK) {
29+ return nullptr ;
2330 }
2431
2532 csmByte* buffer = nullptr ;
26- jclass loaderClass = env->FindClass (" dev/eatgrapes/live2d/LibraryLoader" );
27- jmethodID loadMethod = env->GetStaticMethodID (loaderClass, " loadResource" , " (Ljava/lang/String;)[B" );
28- jstring jPath = env->NewStringUTF (filePath.c_str ());
29- jbyteArray bytes = (jbyteArray)env->CallStaticObjectMethod (loaderClass, loadMethod, jPath);
30-
31- if (bytes) {
32- jsize len = env->GetArrayLength (bytes);
33- *outSize = static_cast <csmSizeInt>(len);
34- buffer = (csmByte*)malloc (len);
35- env->GetByteArrayRegion (bytes, 0 , len, (jbyte*)buffer);
33+ if (g_libraryLoaderClass && g_loadResourceMethod) {
34+ jstring jPath = env->NewStringUTF (filePath.c_str ());
35+ jbyteArray bytes = (jbyteArray)env->CallStaticObjectMethod (g_libraryLoaderClass, g_loadResourceMethod, jPath);
36+
37+ if (bytes) {
38+ jsize len = env->GetArrayLength (bytes);
39+ *outSize = static_cast <csmSizeInt>(len);
40+ buffer = (csmByte*)malloc (len);
41+ env->GetByteArrayRegion (bytes, 0 , len, (jbyte*)buffer);
42+ }
43+ env->DeleteLocalRef (jPath);
3644 }
3745
3846 if (attached) g_jvm->DetachCurrentThread ();
@@ -72,19 +80,18 @@ static void LogFunction(const char* message) {
7280
7381 JNIEnv* env;
7482 bool attached = false ;
75- if (g_jvm->GetEnv ((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
83+ jint res = g_jvm->GetEnv ((void **)&env, JNI_VERSION_1_6);
84+ if (res == JNI_EDETACHED) {
7685 g_jvm->AttachCurrentThread ((void **)&env, nullptr );
7786 attached = true ;
87+ } else if (res != JNI_OK) {
88+ return ;
7889 }
7990
80- jclass cls = env->FindClass (" dev/eatgrapes/live2d/CubismFramework" );
81- if (cls) {
82- jmethodID mid = env->GetStaticMethodID (cls, " onLog" , " (Ljava/lang/String;)V" );
83- if (mid) {
84- jstring str = env->NewStringUTF (message);
85- env->CallStaticVoidMethod (cls, mid, str);
86- env->DeleteLocalRef (str);
87- }
91+ if (g_cubismFrameworkClass && g_onLogMethod) {
92+ jstring str = env->NewStringUTF (message);
93+ env->CallStaticVoidMethod (g_cubismFrameworkClass, g_onLogMethod, str);
94+ env->DeleteLocalRef (str);
8895 }
8996
9097 if (attached) g_jvm->DetachCurrentThread ();
@@ -94,9 +101,23 @@ extern "C" {
94101
95102JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM* vm, void * reserved) {
96103 g_jvm = vm;
97- #ifdef _WIN32
98- init_gles2_shim ();
99- #endif
104+ JNIEnv* env;
105+ if (vm->GetEnv ((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
106+ return JNI_ERR;
107+ }
108+
109+ jclass loader = env->FindClass (" dev/eatgrapes/live2d/LibraryLoader" );
110+ if (loader) {
111+ g_libraryLoaderClass = (jclass)env->NewGlobalRef (loader);
112+ g_loadResourceMethod = env->GetStaticMethodID (loader, " loadResource" , " (Ljava/lang/String;)[B" );
113+ }
114+
115+ jclass framework = env->FindClass (" dev/eatgrapes/live2d/CubismFramework" );
116+ if (framework) {
117+ g_cubismFrameworkClass = (jclass)env->NewGlobalRef (framework);
118+ g_onLogMethod = env->GetStaticMethodID (framework, " onLog" , " (Ljava/lang/String;)V" );
119+ }
120+
100121 return JNI_VERSION_1_6;
101122}
102123
0 commit comments