Skip to content

Commit

Permalink
Some changes and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RusJJ committed Oct 10, 2024
1 parent 266dcfa commit e3ec782
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 40 deletions.
34 changes: 26 additions & 8 deletions include/jnifn.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ inline jobject GetGlobalContext(JNIEnv *env)
jobject at = env->CallStaticObjectMethod(activityThread, currentActivityThread);
jmethodID getApplication = env->GetMethodID(activityThread, "getApplication", "()Landroid/app/Application;");
jobject context = env->CallObjectMethod(at, getApplication);
if (env->ExceptionCheck()) env->ExceptionClear();
return context;
}

Expand All @@ -17,32 +18,41 @@ inline jobject GetGlobalActivity(JNIEnv *env)
jobject at = env->CallStaticObjectMethod(activityThread, currentActivityThread);
jmethodID getApplication = env->GetMethodID(activityThread, "getApplication", "()Landroid/app/Application;");
jobject context = env->CallObjectMethod(at, getApplication);
if (env->ExceptionCheck()) env->ExceptionClear();
return context;
}

inline jstring GetPackageName(JNIEnv *env, jobject jActivity)
{
jmethodID method = env->GetMethodID(env->GetObjectClass(jActivity), "getPackageName", "()Ljava/lang/String;");
return (jstring)env->CallObjectMethod(jActivity, method);
jstring ret = (jstring)env->CallObjectMethod(jActivity, method);
if (env->ExceptionCheck()) env->ExceptionClear();
return ret;
}

inline jobject GetFilesDir(JNIEnv *env, jobject jActivity)
{
jmethodID method = env->GetMethodID(env->GetObjectClass(jActivity), "getFilesDir", "()Ljava/io/File;");
return (jstring)env->CallObjectMethod(jActivity, method);
jstring ret = (jstring)env->CallObjectMethod(jActivity, method);
if (env->ExceptionCheck()) env->ExceptionClear();
return ret;
}

inline jstring GetAbsolutePath(JNIEnv *env, jobject jFile)
{
jmethodID method = env->GetMethodID(env->GetObjectClass(jFile), "getAbsolutePath", "()Ljava/lang/String;");
return (jstring)env->CallObjectMethod(jFile, method);
jstring ret = (jstring)env->CallObjectMethod(jFile, method);
if (env->ExceptionCheck()) env->ExceptionClear();
return ret;
}

inline jstring GetAndroidPermission(JNIEnv* env, const char* szPermissionName)
{
jclass ClassManifestPermission = env->FindClass("android/Manifest$permission");
jfieldID lid_PERM = env->GetStaticFieldID(ClassManifestPermission, szPermissionName, "Ljava/lang/String;");
return (jstring)env->GetStaticObjectField(ClassManifestPermission, lid_PERM);
jstring ret = (jstring)env->GetStaticObjectField(ClassManifestPermission, lid_PERM);
if (env->ExceptionCheck()) env->ExceptionClear();
return ret;
}

inline bool HasPermissionGranted(JNIEnv* env, jobject jActivity, const char* szPermissionName)
Expand All @@ -55,6 +65,7 @@ inline bool HasPermissionGranted(JNIEnv* env, jobject jActivity, const char* szP

PERMISSION_GRANTED = env->GetStaticIntField(ClassPackageManager, lid_PERMISSION_GRANTED);
jint int_result = env->CallIntMethod(jActivity, env->GetMethodID(env->FindClass("android/content/Context"), "checkSelfPermission", "(Ljava/lang/String;)I"), ls_PERM);
if (env->ExceptionCheck()) env->ExceptionClear();
return (int_result == PERMISSION_GRANTED);
}

Expand All @@ -69,7 +80,9 @@ inline bool HasPermissionGranted(JNIEnv* env, jobject jActivity, const char* szP
inline jobject GetExternalFilesDir(JNIEnv* env, jobject jActivity) // getExternalFilesDir creates directory in Android/data, lol
{
jmethodID method = env->GetMethodID(env->GetObjectClass(jActivity), "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
return (jstring)env->CallObjectMethod(jActivity, method, NULL);
jstring ret = (jstring)env->CallObjectMethod(jActivity, method, NULL);
if (env->ExceptionCheck()) env->ExceptionClear();
return ret;
}

// fastman92
Expand All @@ -95,8 +108,7 @@ inline bool GetExternalFilesDir_FLA(JNIEnv* env, jobject context, char* strPath,

if (!bReadFromF92launcher)
{
if (env->ExceptionCheck())
env->ExceptionClear();
if (env->ExceptionCheck()) env->ExceptionClear();

jclass android_content_Context = env->GetObjectClass(context);

Expand Down Expand Up @@ -124,7 +136,9 @@ inline bool GetExternalFilesDir_FLA(JNIEnv* env, jobject context, char* strPath,
inline jobject GetStorageDir(JNIEnv* env) // /storage/emulated/0 instead of /sdcard (example)
{
jclass classEnvironment = env->FindClass("android/os/Environment");
return (jstring)env->CallStaticObjectMethod(classEnvironment, env->GetStaticMethodID(classEnvironment, "getExternalStorageDirectory", "()Ljava/io/File;"));
jstring ret = (jstring)env->CallStaticObjectMethod(classEnvironment, env->GetStaticMethodID(classEnvironment, "getExternalStorageDirectory", "()Ljava/io/File;"));
if (env->ExceptionCheck()) env->ExceptionClear();
return ret;
}

inline void ShowToastMessage(JNIEnv* env, jobject jActivity, const char* txt, int msDuration)
Expand All @@ -140,6 +154,8 @@ inline void ShowToastMessage(JNIEnv* env, jobject jActivity, const char* txt, in
env->CallVoidMethod(toast, showMethodID);

env->DeleteLocalRef(message);

if (env->ExceptionCheck()) env->ExceptionClear();
}

inline void ShowToastMessage2(JNIEnv* env, jobject jActivity, const char* txt, jint duration)
Expand All @@ -155,4 +171,6 @@ inline void ShowToastMessage2(JNIEnv* env, jobject jActivity, const char* txt, j
env->CallVoidMethod(toast, showMethodID);

env->DeleteLocalRef(message);

if (env->ExceptionCheck()) env->ExceptionClear();
}
2 changes: 1 addition & 1 deletion interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LIST_START(Interfaces)
}
static void* Get(const char* name)
{
LIST_FOR(listInterfaces)
LIST_FOR_FAST(listInterfaces)
{
if (!strcmp(item->szName, name)) return item->pInterface;
}
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
/* Load news first! */
if(g_nEnableNews > 0)
{
char newsBuf[1024]; newsBuf[0] = 0;
char newsBuf[512]; memset(newsBuf, 0, sizeof(newsBuf));

if(aml->DownloadFileToData("https://raw.githubusercontent.com/RusJJ/AndroidModLoader/main/news.txt", newsBuf, sizeof(newsBuf)) && newsBuf[0])
{
Expand Down
20 changes: 10 additions & 10 deletions mls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ void MLS::SaveFile()
FILE* f = fopen(path, "wb");
if(!f) return;

LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
fwrite(&item->storage, sizeof(MLSStorage), 1, f);
}
fclose(f);
}
bool MLS::HasValue(const char* key)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key)) return true;
}
Expand All @@ -96,7 +96,7 @@ void MLS::DeleteValue(const char* key)

void MLS::SetInt(const char* key, int32_t val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -116,7 +116,7 @@ void MLS::SetInt(const char* key, int32_t val)
}
void MLS::SetFloat(const char* key, float val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -138,7 +138,7 @@ void MLS::SetFloat(const char* key, float val)
#pragma clang diagnostic ignored "-Wformat" // dumb ass clang cries about "ld needs to be lld" and "lld needs to be ld" at the SAME TIME
void MLS::SetInt64(const char* key, int64_t val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -159,7 +159,7 @@ void MLS::SetInt64(const char* key, int64_t val)
#pragma clang diagnostic pop
void MLS::SetStr(const char* key, const char *val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -180,7 +180,7 @@ void MLS::SetStr(const char* key, const char *val)

bool MLS::GetInt(const char* key, int32_t *val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -192,7 +192,7 @@ bool MLS::GetInt(const char* key, int32_t *val)
}
bool MLS::GetFloat(const char* key, float *val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -204,7 +204,7 @@ bool MLS::GetFloat(const char* key, float *val)
}
bool MLS::GetInt64(const char* key, int64_t *val)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand All @@ -216,7 +216,7 @@ bool MLS::GetInt64(const char* key, int64_t *val)
}
bool MLS::GetStr(const char* key, char *val, size_t len)
{
LIST_FOR(listSets)
LIST_FOR_FAST(listSets)
{
if(!strcmp(item->storage.szKey, key))
{
Expand Down
26 changes: 13 additions & 13 deletions modslist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LIST_START(Mods)
}
static Mods* Get(const char* guid)
{
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
if (!strcmp(item->pModInfo->szGUID, guid)) return item;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ bool ModsList::RemoveMod(const char* szGUID)

bool ModsList::HasMod(const char* szGUID)
{
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
if(!strcmp(item->pModInfo->szGUID, szGUID))
{
Expand Down Expand Up @@ -122,7 +122,7 @@ bool ModsList::HasModOfVersion(const char* szGUID, const char* szVersion)
}

ModInfo* pInfo = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
pInfo = item->pModInfo;
if(!strcmp(pInfo->szGUID, szGUID))
Expand Down Expand Up @@ -161,7 +161,7 @@ bool ModsList::HasModOfBiggerVersion(const char* szGUID, const char* szVersion)
}

ModInfo* pInfo = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
pInfo = item->pModInfo;
if(!strcmp(pInfo->szGUID, szGUID))
Expand Down Expand Up @@ -189,7 +189,7 @@ void ModsList::ProcessDependencies()

label_run_dependencies_check:
//logger->Info("Checking dependencies from the start! Mods count: %d", modlist->GetModsNum());
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
// If the mod is already ok or doesnt require check, depList = NULL
depList = item->pModDesc->m_aDependencies;
Expand Down Expand Up @@ -229,7 +229,7 @@ void ModsList::ProcessPreLoading()
OnModLoadFn onModPreLoadFn;
ModDesc* desc;
void* handle;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
handle = item->pHandle;
if(handle != NULL)
Expand Down Expand Up @@ -266,7 +266,7 @@ void ModsList::ProcessPreLoading()
void ModsList::ProcessLoading()
{
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
desc = item->pModDesc;
pLastModProcessed = desc;
Expand All @@ -278,7 +278,7 @@ void ModsList::ProcessLoading()
void ModsList::ProcessUnloading()
{
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
desc = item->pModDesc;
pLastModProcessed = desc;
Expand All @@ -289,7 +289,7 @@ void ModsList::ProcessUnloading()
void ModsList::ProcessUpdater()
{
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
desc = item->pModDesc;
pLastModProcessed = desc;
Expand All @@ -312,7 +312,7 @@ void ModsList::ProcessUpdater()
void ModsList::ProcessCrash(const char* szLibName, int sig, int code, uintptr_t libaddr, mcontext_t* mcontext)
{
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
desc = item->pModDesc;
pLastModProcessed = desc;
Expand All @@ -331,7 +331,7 @@ void ModsList::PrintModsList(std::ofstream& logfile)

ModInfo* info = NULL;
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
info = item->pModInfo;
desc = item->pModDesc;
Expand All @@ -344,7 +344,7 @@ void ModsList::PrintModsList(std::ofstream& logfile)
void ModsList::OnInterfaceAdded(const char* name, const void* ptr)
{
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
desc = item->pModDesc;
pLastModProcessed = desc;
Expand All @@ -355,7 +355,7 @@ void ModsList::OnInterfaceAdded(const char* name, const void* ptr)
void ModsList::OnAllModsLoaded()
{
ModDesc* desc = NULL;
LIST_FOR(listMods)
LIST_FOR_FAST(listMods)
{
desc = item->pModDesc;
pLastModProcessed = desc;
Expand Down
17 changes: 10 additions & 7 deletions signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ void Handler(int sig, siginfo_t *si, void *ptr)

g_pLogFile << "Exception Signal " << sig << " - " << SignalEnum(sig) << " (" << CodeEnum(sig, si->si_code) << ")" << std::endl;
g_pLogFile << "Fault address: 0x" << std::hex << std::uppercase << faultAddr << std::nouppercase << std::endl;
g_pLogFile << "An overall reason of the crash:\n- ";
g_pLogFile << "A POSSIBLE (!) reason of the crash:\n- ";
switch(sig)
{
case SIGABRT:
g_pLogFile << "Because an application is killed by something (Android`s Low Memory Killer?)" << std::endl;
g_pLogFile << "Probably the game is closed by something (Android`s Low Memory Killer?)" << std::endl;
break;
case SIGBUS:
g_pLogFile << "Not enough memory, or invalid execution address, or bad mod patch" << std::endl;
g_pLogFile << "Not enough memory, or invalid execution address, or a bad mod patch" << std::endl;
break;
case SIGFPE:
g_pLogFile << "An error somewhere in the code, often - dividing by zero" << std::endl;
Expand All @@ -241,7 +241,7 @@ void Handler(int sig, siginfo_t *si, void *ptr)
g_pLogFile << "Stack fault on coprocessor" << std::endl;
break;
case SIGTRAP:
g_pLogFile << "It`s a trap! Somewhere in the application is called \"it`s a trap! stop the application!\"" << std::endl;
g_pLogFile << "It`s a trap! Somewhere in the game was called \"it`s a trap! stop the application!\"" << std::endl;
break;
}

Expand All @@ -265,7 +265,7 @@ void Handler(int sig, siginfo_t *si, void *ptr)
{
// Unsuccess
label_unsuccess:
g_pLogFile << "Program counter: Unknown Lib, 0x" << std::hex << std::uppercase << PC;
g_pLogFile << "Program counter: Unknown Lib + 0x" << std::hex << std::uppercase << PC;
}

if(dlInfo.dli_sname)
Expand All @@ -280,7 +280,7 @@ void Handler(int sig, siginfo_t *si, void *ptr)

char sysprop_str[92];
g_pLogFile << "\n----------------------------------------------------\nShort device info:" << std::endl;
g_pLogFile << "Android SDK: " << g_nAndroidSDKVersion << std::endl;
g_pLogFile << "Android SDK Version: " << std::dec << g_nAndroidSDKVersion << std::endl;
if(__system_property_get("ro.product.brand", sysprop_str) || __system_property_get("ro.product.system.brand", sysprop_str))
{
g_pLogFile.flush();
Expand All @@ -294,7 +294,9 @@ void Handler(int sig, siginfo_t *si, void *ptr)
if(__system_property_get("ro.system.product.cpu.abilist", sysprop_str))
{
g_pLogFile.flush();
g_pLogFile << "Supported ABIs: " << sysprop_str << std::endl;
g_pLogFile << "Supported ABIs: " << sysprop_str;
if(strstr(sysprop_str, "86") != NULL) g_pLogFile << " (it looks like you`re on emulator?)";
g_pLogFile << std::endl;
}
if(__system_property_get("ro.build.date", sysprop_str))
{
Expand Down Expand Up @@ -528,6 +530,7 @@ void Handler(int sig, siginfo_t *si, void *ptr)
skip_logging:
logger->Info("Notifying mods about the crash...");
modlist->ProcessCrash(dlInfo.dli_fname ? GetFilenamePart(dlInfo.dli_fname) : "", sig, si->si_code, (uintptr_t)dlInfo.dli_fbase, mcontext);
logger->Info("Telling mods to unload after the crash...");
modlist->ProcessUnloading();

oldSigaction[SignalInnerId(sig)].sa_sigaction(sig, si, ptr);
Expand Down

0 comments on commit e3ec782

Please sign in to comment.