@@ -174,7 +174,7 @@ void LoadMods(const char* path)
174174 if (diread->d_name [0 ] == ' .' ) continue ; // Skip . and ..
175175 if (!EndsWithSO (diread->d_name ))
176176 {
177- // Useless info for us!
177+ // Useless info for us
178178 // logger->Error("File %s is not a mod, atleast it is NOT .SO file!", diread->d_name);
179179 continue ;
180180 }
@@ -183,15 +183,15 @@ void LoadMods(const char* path)
183183 // unlink(dataBuf);
184184 chmod (dataBuf, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP); // XMDS
185185 int removeStatus = remove (dataBuf);
186- // if(removeStatus != 0) logger->Error("Failed to remove temporary mod file! This may broke the mod loading! Error %d", removeStatus);
186+ // if(removeStatus != 0) logger->Error("Failed to remove temp mod file! This might break the mod loading. Error %d", removeStatus);
187187 if (!CopyFileFaster (buf, dataBuf) && !CopyFile (buf, dataBuf))
188188 {
189189 logger->Error (" File %s is failed to be copied! :(" , diread->d_name );
190190 continue ;
191191 }
192192 chmod (dataBuf, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
193193
194- handle = dlopen (dataBuf, RTLD_NOW); // Load it to RAM!
194+ handle = dlopen (dataBuf, RTLD_NOW); // Load it to RAM
195195 modInfoFn = (GetModInfoFn)dlsym (handle, " __GetModInfo" );
196196 if (modInfoFn != NULL )
197197 {
@@ -208,7 +208,7 @@ void LoadMods(const char* path)
208208 goto nextMod;
209209 }
210210
211- logger->Info (" Mod (GUID %s) has been processed.. ." , pModInfo->GUID ());
211+ logger->Info (" Mod (GUID %s) has been preprocessed ." , pModInfo->GUID ());
212212 }
213213 else
214214 {
@@ -217,41 +217,79 @@ void LoadMods(const char* path)
217217 }
218218 // unlink(dataBuf);
219219 removeStatus = remove (dataBuf);
220- if (removeStatus != 0 ) logger->Error (" Failed to remove temporary mod file! This may broke the mod loading! Error %d" , removeStatus);
220+ if (removeStatus != 0 ) logger->Error (" Failed to remove temp mod file! This might break the mod loading. Error %d" , removeStatus);
221221 }
222222 closedir (dir);
223223 }
224224 else
225225 {
226- logger->Error (" Failed to load mods: DIR IS NOT OPEN " );
226+ logger->Error (" Failed to load mods: unable to open directory " );
227227 }
228228}
229229
230230extern ModDesc* pLastModProcessed;
231+
231232void StartSignalHandler ();
232233void HookALog ();
233- JavaVM *myVM = NULL ;
234+ JavaVM *g_pJavaVM = NULL ;
235+ void *g_pJavaReserved = NULL ;
236+
234237extern bool bAndroidLog_OnlyImportant, bAndroidLog_NoAfter, bAML_HasFastmanModified;
235- JNIEXPORT jint JNI_OnLoad (JavaVM *vm, void *reserved)
238+ bool g_bAMLStarted = false ;
239+
240+ pthread_key_t g_JNIThreadKey;
241+ JNIEnv* GetCurrentJNI ()
236242{
237- myVM = vm;
238-
243+ JNIEnv* env = NULL ;
244+ if (g_JNIThreadKey)
245+ {
246+ env = (JNIEnv*)pthread_getspecific (g_JNIThreadKey);
247+ if (env) return env;
248+ }
249+ else
250+ {
251+ pthread_key_create (&g_JNIThreadKey, NULL );
252+ }
253+
254+ if (g_pJavaVM)
255+ {
256+ jint result = g_pJavaVM->AttachCurrentThread (&env, NULL );
257+ if (result == 0 && env)
258+ {
259+ pthread_setspecific (g_JNIThreadKey, env);
260+ return env;
261+ }
262+ }
263+ return NULL ;
264+ }
265+
266+ void StartAMLRightNow (const char * libName1 = NULL , const char * libName2 = NULL )
267+ {
268+ if (g_bAMLStarted)
269+ {
270+ logger->Error (" Something was trying to boot-up AML again." );
271+ return ;
272+ }
273+
274+ void * lib1 = libName1 ? dlopen (libName1, RTLD_NOW) : NULL ;
275+ void * lib2 = libName2 ? dlopen (libName2, RTLD_NOW) : NULL ;
276+
239277 logger->SetTag (" AndroidModLoader" );
240278 const char * szTmp; jstring jTmp;
241279
242280 /* JNI Environment */
243- if (vm ->GetEnv (reinterpret_cast <void **>(&env), JNI_VERSION_1_6) != JNI_OK)
281+ if (g_pJavaVM ->GetEnv (reinterpret_cast <void **>(&env), JNI_VERSION_1_6) != JNI_OK)
244282 {
245283 logger->Error (" Cannot get JNI Environment!" );
246- return - 1 ;
284+ return ;
247285 }
248286
249287 /* Application Context */
250288 appContext = GetGlobalContext (env);
251289 if (appContext == NULL )
252290 {
253291 logger->Error (" AML Library should be loaded in \" onCreate\" or by injecting it directly into the main game library!" );
254- return JNI_VERSION_1_6 ;
292+ return ;
255293 }
256294
257295 /* Must Have for mods */
@@ -472,7 +510,30 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
472510 pLastModProcessed = NULL ;
473511
474512 /* Fake crash for crash handler testing (does not work?) */
475- if (g_bCrashAML) __builtin_trap ();
513+ // if(g_bCrashAML) __builtin_trap(); // Dont let really weird guys to use this...
514+
515+ // TODO: should be loaded in a different thread..?
516+ if (lib1)
517+ {
518+ auto libEntry = (void (*)(JavaVM*, void *))dlsym (lib1, " JNI_OnLoad" );
519+ if (libEntry) libEntry (g_pJavaVM, g_pJavaReserved);
520+ }
521+ if (lib2)
522+ {
523+ auto libEntry = (void (*)(JavaVM*, void *))dlsym (lib2, " JNI_OnLoad" );
524+ if (libEntry) libEntry (g_pJavaVM, g_pJavaReserved);
525+ }
526+
527+ g_bAMLStarted = true ;
528+ }
529+
530+ JNIEXPORT jint JNI_OnLoad (JavaVM *vm, void *reserved)
531+ {
532+ g_pJavaVM = vm;
533+ g_pJavaReserved = reserved;
534+
535+ /* For the delayed start-up (later) */
536+ StartAMLRightNow ();
476537
477538 /* Return the value it needs */
478539 return JNI_VERSION_1_6;
0 commit comments