diff --git a/app/src/main/cpp/Core/Canvas/Canvas.cpp b/app/src/main/cpp/Core/Canvas/Canvas.cpp index 16aa4dc..6f98b6a 100644 --- a/app/src/main/cpp/Core/Canvas/Canvas.cpp +++ b/app/src/main/cpp/Core/Canvas/Canvas.cpp @@ -41,4 +41,5 @@ namespace Canvas { PRIVATE_API const char *configsPath; PRIVATE_API std::vector onKeyboardCompleteListeners; + PRIVATE_API std::mutex onKeyboardCompleteListenersMutex; } \ No newline at end of file diff --git a/app/src/main/cpp/Core/Canvas/Canvas.h b/app/src/main/cpp/Core/Canvas/Canvas.h index 82c2517..3a58e47 100644 --- a/app/src/main/cpp/Core/Canvas/Canvas.h +++ b/app/src/main/cpp/Core/Canvas/Canvas.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -63,4 +64,5 @@ namespace Canvas { extern const char *configsPath; extern std::vector onKeyboardCompleteListeners; + extern std::mutex onKeyboardCompleteListenersMutex; } diff --git a/app/src/main/cpp/Core/Cipher/CipherPatch.cpp b/app/src/main/cpp/Core/Cipher/CipherPatch.cpp index 660d45c..a77c91a 100644 --- a/app/src/main/cpp/Core/Cipher/CipherPatch.cpp +++ b/app/src/main/cpp/Core/Cipher/CipherPatch.cpp @@ -65,13 +65,14 @@ CipherPatch::~CipherPatch() { std::lock_guard lock(cipher_patch_mtx); - CipherBase::s_InstanceVec.erase( - std::find( - CipherBase::s_InstanceVec.begin(), - CipherBase::s_InstanceVec.end(), - (CipherBase *)this - ) + auto it = std::find( + CipherBase::s_InstanceVec.begin(), + CipherBase::s_InstanceVec.end(), + (CipherBase *)this ); + if (it != CipherBase::s_InstanceVec.end()) { + CipherBase::s_InstanceVec.erase(it); + } CipherInternal::ForgetInstall(this); artpatch_die(this->patch); } diff --git a/app/src/main/cpp/Core/Cipher/CipherUtils.cpp b/app/src/main/cpp/Core/Cipher/CipherUtils.cpp index 3060a99..9e2c2a4 100644 --- a/app/src/main/cpp/Core/Cipher/CipherUtils.cpp +++ b/app/src/main/cpp/Core/Cipher/CipherUtils.cpp @@ -53,7 +53,7 @@ void CipherUtils::patternToBytes(const std::string _pattern, char* _bytesBuffer, } } - if (!(bytes.empty() || mask.empty() || bytes.size() != mask.size())) { + if (!(bytes.empty() || mask.empty() || bytes.size() != mask.size() || bytes.size() >= 256)) { std::memcpy(_bytesBuffer, bytes.data(), bytes.size()); _bytesBuffer[bytes.size()] = '\0'; _maskBuffer = mask; @@ -272,13 +272,13 @@ char* CipherUtils::readAsset(const char* _assetPath) { } void CipherUtils::addOnKeyboardCompleteListener(void (*_listener)(std::string)) { + std::lock_guard lock(Canvas::onKeyboardCompleteListenersMutex); Canvas::onKeyboardCompleteListeners.push_back(_listener); } DeviceInfo CipherUtils::get_DeviceInfo() { - DeviceInfo deviceInfo; - std::memcpy(&deviceInfo, &Canvas::deviceInfo, sizeof(deviceInfo)); - return deviceInfo; + return {Canvas::deviceInfo.xdpi, Canvas::deviceInfo.ydpi, Canvas::deviceInfo.density, + Canvas::deviceInfo.deviceName, Canvas::deviceInfo.deviceManufacturer, Canvas::deviceInfo.deviceModel}; } void CipherUtils::performHapticFeedback(HapticFeedbackType _type) { diff --git a/app/src/main/cpp/Core/Iconloader/IconLoader.cpp b/app/src/main/cpp/Core/Iconloader/IconLoader.cpp index 78ba2ed..b8130aa 100644 --- a/app/src/main/cpp/Core/Iconloader/IconLoader.cpp +++ b/app/src/main/cpp/Core/Iconloader/IconLoader.cpp @@ -101,6 +101,7 @@ PRIVATE_API SkyImage& IconLoader::uploadImageKtx(const char* name, const bool& i else IconLoader::images.insert(std::make_pair(name, image)); return *image; case KTX_UNSUPPORTED_TEXTURE_TYPE: + default: __android_log_print(ANDROID_LOG_ERROR,"IconLoader", "Unsupported texture type"); free(assetBuf); glDeleteTextures(1, &texid); diff --git a/app/src/main/cpp/Core/fileselector/fileselector.cpp b/app/src/main/cpp/Core/fileselector/fileselector.cpp index eece8e9..0af9e68 100644 --- a/app/src/main/cpp/Core/fileselector/fileselector.cpp +++ b/app/src/main/cpp/Core/fileselector/fileselector.cpp @@ -93,17 +93,28 @@ static jobjectArray fsel_build_mime_array(JNIEnv* env, return arr; } +namespace { +struct ThreadAttachment { + bool attached = false; + ~ThreadAttachment() { + if (attached) Canvas::javaVM->DetachCurrentThread(); + } +}; +} + bool requestFileMultiNamed(const char* const* mime_types, size_t mime_type_count, callback_function callback, bool save, const char* suggested_name) { if (!mime_types || mime_type_count == 0) return false; JNIEnv* env; + ThreadAttachment attachment; if (Canvas::javaVM->GetEnv((void**)&env, JNI_VERSION_1_6) == JNI_EDETACHED) { if (Canvas::javaVM->AttachCurrentThread(&env, nullptr) != JNI_OK) { __android_log_print(ANDROID_LOG_ERROR, "fileselector", "Failed to attach thread to JVM"); return false; } + attachment.attached = true; } if (!class_FileSelector || !method_nselectFileMultiNamed) { __android_log_print(ANDROID_LOG_ERROR, "fileselector", "FileSelector not initialized"); @@ -113,9 +124,6 @@ bool requestFileMultiNamed(const char* const* mime_types, size_t mime_type_count jobjectArray mimeArr = fsel_build_mime_array(env, mime_types, mime_type_count); if (!mimeArr) return false; - // Null stays null all the way into the Java side, where it means "leave the - // name field as it was" - the pre-EXTRA_TITLE behaviour, and what - // requestFileMulti below still asks for. jstring nameStr = nullptr; if (suggested_name && suggested_name[0] != '\0') { nameStr = env->NewStringUTF(suggested_name); @@ -150,11 +158,13 @@ bool requestFiles(const char* const* mime_types, size_t mime_type_count, if (!mime_types || mime_type_count == 0) return false; JNIEnv* env; + ThreadAttachment attachment; if (Canvas::javaVM->GetEnv((void**)&env, JNI_VERSION_1_6) == JNI_EDETACHED) { if (Canvas::javaVM->AttachCurrentThread(&env, nullptr) != JNI_OK) { __android_log_print(ANDROID_LOG_ERROR, "fileselector", "Failed to attach thread to JVM"); return false; } + attachment.attached = true; } if (!class_FileSelector || !method_nselectFiles) { __android_log_print(ANDROID_LOG_ERROR, "fileselector", "FileSelector not initialized"); diff --git a/app/src/main/cpp/Utils/artpatch/artpatch.c b/app/src/main/cpp/Utils/artpatch/artpatch.c index 47919d2..7d189da 100644 --- a/app/src/main/cpp/Utils/artpatch/artpatch.c +++ b/app/src/main/cpp/Utils/artpatch/artpatch.c @@ -44,6 +44,12 @@ __attribute__((visibility ("hidden"))) unsigned char artpatch_decode_hex_digit(c case 'D': return 13; case 'E': return 14; case 'F': return 15; + case 'a': return 10; + case 'b': return 11; + case 'c': return 12; + case 'd': return 13; + case 'e': return 14; + case 'f': return 15; default: return 255; } } @@ -88,21 +94,23 @@ __attribute__((visibility ("hidden"))) void artpatch_set_hex(patch_t patch, cons __attribute__((visibility ("hidden"))) void artpatch_apply(patch_t patch) { const void* pagestart = (void*)((uintptr_t)patch->patchDestination & -PAGE_SIZE); + size_t span = ((uintptr_t)patch->patchDestination + patch->patchLength - (uintptr_t)pagestart + PAGE_SIZE - 1) & -PAGE_SIZE; int prot_flags = procutils_read_mprotection(pagestart); - if(!mprotect(pagestart, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC)) { + if(!mprotect(pagestart, span, PROT_READ | PROT_WRITE | PROT_EXEC)) { if(!patch->patchApplied) memcpy(patch->backup, patch->patchDestination, patch->patchLength); memcpy(patch->patchDestination, patch->patch, patch->patchLength); - mprotect(pagestart, PAGE_SIZE, prot_flags); + mprotect(pagestart, span, prot_flags); patch->patchApplied = true; } } __attribute__((visibility ("hidden"))) void artpatch_restore(patch_t patch) { if(!patch->patchApplied) return; const void* pagestart = (void*)((uintptr_t)patch->patchDestination & -PAGE_SIZE); + size_t span = ((uintptr_t)patch->patchDestination + patch->patchLength - (uintptr_t)pagestart + PAGE_SIZE - 1) & -PAGE_SIZE; int prot_flags = procutils_read_mprotection(pagestart); - if(!mprotect(pagestart, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC)) { + if(!mprotect(pagestart, span, PROT_READ | PROT_WRITE | PROT_EXEC)) { memcpy(patch->patchDestination, patch->backup, patch->patchLength); - mprotect(pagestart, PAGE_SIZE, prot_flags); + mprotect(pagestart, span, prot_flags); patch->patchApplied = false; }else{ __android_log_print(ANDROID_LOG_FATAL,"artPatch","Unable to change memory protection to restore backup!"); diff --git a/app/src/main/cpp/Utils/artpatch/procutils.c b/app/src/main/cpp/Utils/artpatch/procutils.c index 3430985..744b042 100644 --- a/app/src/main/cpp/Utils/artpatch/procutils.c +++ b/app/src/main/cpp/Utils/artpatch/procutils.c @@ -48,7 +48,7 @@ __attribute__((visibility ("hidden"))) int procutils_read_mprotection(void* addr unsigned long addr0 = strtoul(b->mem, &end0, 0x10); char*end1 = (void*)0; unsigned long addr1 = strtoul(end0+1, &end1, 0x10); - if ((void*)addr0 <= addr && addr <= (void*)addr1) { + if ((void*)addr0 <= addr && addr < (void*)addr1) { res |= (end1+1)[0] == 'r' ? PROT_READ : 0; res |= (end1+1)[1] == 'w' ? PROT_WRITE : 0; res |= (end1+1)[2] == 'x' ? PROT_EXEC : 0; diff --git a/app/src/main/cpp/Utils/imgui_androidbk/androidbk.cpp b/app/src/main/cpp/Utils/imgui_androidbk/androidbk.cpp index 278f334..a5b51fd 100644 --- a/app/src/main/cpp/Utils/imgui_androidbk/androidbk.cpp +++ b/app/src/main/cpp/Utils/imgui_androidbk/androidbk.cpp @@ -32,7 +32,7 @@ static EGLSurface g_EglSurface = EGL_NO_SURFACE; static EGLContext g_EglContext = EGL_NO_CONTEXT; static EGLConfig egl_config; static EGLint egl_format; -static bool run = true; +static std::atomic run{true}; static ANativeWindow *androidWindow; static double g_time = 0; @@ -524,15 +524,17 @@ PRIVATE_API void renderloop() g_EglSurface = nullptr; } -PRIVATE_API void init_sfc(JNIEnv *env, jobject surface) { +PRIVATE_API bool init_sfc(JNIEnv *env, jobject surface) { if(androidWindow != nullptr) { ANativeWindow_release(androidWindow); } androidWindow = ANativeWindow_fromSurface(env, surface); + if (androidWindow == nullptr) return false; ANativeWindow_acquire(androidWindow); ANativeWindow_setBuffersGeometry(androidWindow, 0, 0, egl_format); g_EglSurface = eglCreateWindowSurface(g_EglDisplay, egl_config, androidWindow, nullptr); eglMakeCurrent(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext); + return true; } static jclass class_ImGUI; @@ -729,8 +731,8 @@ Java_git_artdeell_skymodloader_ImGUI_wantsMouse(JNIEnv *env, jclass clazz) { extern "C" JNIEXPORT void JNICALL Java_git_artdeell_skymodloader_ImGUI_resurface(JNIEnv *env, jclass clazz, jobject surface) { - init_sfc(env, surface); - eglMakeCurrent(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext); run = true; + if (!init_sfc(env, surface)) return; + eglMakeCurrent(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext); renderloop(); } \ No newline at end of file diff --git a/app/src/main/cpp/main.cpp b/app/src/main/cpp/main.cpp index a2574c4..4f3681d 100644 --- a/app/src/main/cpp/main.cpp +++ b/app/src/main/cpp/main.cpp @@ -362,7 +362,12 @@ Java_git_artdeell_skymodloader_MainActivity_onKeyboardCompleteNative( jstring message ) { std::string msg = env->GetStringUTFChars(message, nullptr); - for (auto& listener : Canvas::onKeyboardCompleteListeners) { + std::vector listeners; + { + std::lock_guard lock(Canvas::onKeyboardCompleteListenersMutex); + listeners = Canvas::onKeyboardCompleteListeners; + } + for (auto& listener : listeners) { listener(msg); } }