commit 5d5a067319df31d56d5e9b3d1612f47c3af160ab
parent 07282e7b48b7e7f162a4512cb1b55d527725e999
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Thu, 30 May 2024 19:42:30 +0200
fix(native): custom emoji font crash
Diffstat:
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/util/ktx/FileHandleManagerKtx.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/util/ktx/FileHandleManagerKtx.kt
@@ -16,7 +16,9 @@ fun FileHandleManager.getFileHandleLocalPath(
     fileUniqueIdentifier: String,
 ): String? {
     return getFileHandle(scope.key, name)?.open(ParcelFileDescriptor.MODE_READ_ONLY)?.use { pfd ->
-        val cacheFile = context.androidContext.cacheDir.resolve((fileUniqueIdentifier + Build.FINGERPRINT).longHashCode().absoluteValue.toString(16))
+        val cacheFile = context.androidContext.cacheDir.also {
+            it.mkdirs()
+        }.resolve((fileUniqueIdentifier + Build.FINGERPRINT).longHashCode().absoluteValue.toString(16))
         if (!cacheFile.exists() || pfd.statSize != cacheFile.length()) {
             FileOutputStream(cacheFile).use { output ->
                 ParcelFileDescriptor.AutoCloseInputStream(pfd).use { input ->
diff --git a/native/jni/src/hooks/custom_emoji_font.h b/native/jni/src/hooks/custom_emoji_font.h
@@ -1,9 +1,15 @@
 #pragma once
 
+#include <sys/stat.h>
+
 namespace CustomEmojiFont {
     HOOK_DEF(int, open_hook, const char *pathname, int flags, mode_t mode) {
-        if (strstr(pathname, "/system/fonts/NotoColorEmoji.ttf") != 0 && common::native_config->custom_emoji_font_path[0] != 0) {
-            pathname = common::native_config->custom_emoji_font_path;
+        auto custom_path = common::native_config->custom_emoji_font_path;
+        if (strstr(pathname, "/system/fonts/NotoColorEmoji.ttf") != 0 && custom_path[0] != 0) {
+            struct stat buffer;
+            if (stat(custom_path, &buffer) == 0) {
+                pathname = custom_path;
+            }
         }
         return open_hook_original(pathname, flags, mode);
     }