Skip to content

Commit 1d28f37

Browse files
committed
Some sweet changes
1 parent 4646882 commit 1d28f37

File tree

7 files changed

+91
-26
lines changed

7 files changed

+91
-26
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313

14-
- name: Checkout repository...
14+
- name: Checking out repository...
1515
uses: actions/checkout@v4
1616
with:
1717
submodules: recursive
1818

19-
- name: Install Android NDK (r24)...
19+
- name: Installing Android NDK (r24)...
2020
uses: nttld/setup-ndk@v1
2121
with:
2222
ndk-version: r24
2323
local-cache: true
2424

25-
- name: Build ARMPatch...
25+
- name: Building ARMPatch...
2626
run: ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./ARMPatch/armpatch_src/Android.mk NDK_APPLICATION_MK=./ARMPatch/armpatch_src/Application.mk NDK_DEBUG=0 -j12
2727

28-
- name: Build AML...
28+
- name: Building AML...
2929
run: ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk NDK_APPLICATION_MK=./Application.mk NDK_DEBUG=0 -j12
3030

31-
- name: Upload the mod!
31+
- name: Finished. Uploading the results.
3232
uses: actions/upload-artifact@v4
3333
with:
3434
name: AML_Libraries

aml.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ extern bool g_bEnableFileDownloads;
2828
extern CURL* curl;
2929
extern int g_nDownloadTimeout;
3030

31+
extern JavaVM *g_pJavaVM;
32+
JNIEnv* GetCurrentJNI();
33+
3134
inline bool HasFakeAppName()
3235
{
3336
return (g_szFakeAppName[0] != 0 && strlen(g_szFakeAppName) > 5);
@@ -226,7 +229,7 @@ void AML::ShowToast(bool longerDuration, const char* fmt, ...)
226229
va_list args;
227230
va_start(args, fmt);
228231
vsnprintf(txt, sizeof(txt), fmt, args);
229-
ShowToastMessage(env, appContext, txt, longerDuration ? 1 : 0);
232+
ShowToastMessage(GetCurrentJNI(), appContext, txt, longerDuration ? 1 : 0);
230233
va_end(args);
231234
}
232235

@@ -327,7 +330,7 @@ int AML::GetModsLoadedCount()
327330

328331
JNIEnv* AML::GetJNIEnvironment()
329332
{
330-
return env;
333+
return GetCurrentJNI();
331334
}
332335

333336
jobject AML::GetAppContextObject()

include/jnifn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <jni.h>
22

3-
extern JavaVM *myVM;
3+
extern JavaVM *g_pJavaVM;
44

55
inline jobject GetGlobalContext(JNIEnv *env)
66
{

main.cpp

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

230230
extern ModDesc* pLastModProcessed;
231+
231232
void StartSignalHandler();
232233
void HookALog();
233-
JavaVM *myVM = NULL;
234+
JavaVM *g_pJavaVM = NULL;
235+
void *g_pJavaReserved = NULL;
236+
234237
extern 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;

mod/logger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ void Logger::ToggleOutput(bool enabled)
3131
void Logger::SetTag(const char* szTag)
3232
{
3333
if(m_fnNewTagCallback) m_fnNewTagCallback(m_szTag, szTag);
34-
strncpy(m_szTag, szTag, sizeof(m_szTag));
34+
strncpy(m_szTag, szTag, sizeof(m_szTag)-1);
35+
m_szTag[sizeof(m_szTag)-1] = 0;
3536
}
3637

3738
void Logger::Print(eLogPrio prio, const char* szMessage, ...)

mod/logger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class Logger
5454
inline void SetToggleCB(LoggerToggledCB fnCB) { m_fnToggledCallback = fnCB; }
5555

5656
private:
57-
char m_szTag[32];
57+
char m_szTag[31];
58+
bool m_bEnabled;
5859
LoggerMessageCB m_fnLogCallback;
5960
LoggerSetTagCB m_fnNewTagCallback;
6061
LoggerToggledCB m_fnToggledCallback;
61-
bool m_bEnabled;
6262
};
6363

6464
#endif // _LOGGER_H

news.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
A new APKs are gonna out soon!
1+
A new version of SkyGFX is coming out soon! Yippee!

0 commit comments

Comments
 (0)