From 54169de46afe9389c0e47d1439a3f073bf61a877 Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Thu, 2 May 2024 14:36:17 +0800 Subject: [PATCH] [fonts] Working impl --- .../io/github/pyoncord/xposed/FontsModule.kt | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/io/github/pyoncord/xposed/FontsModule.kt b/app/src/main/kotlin/io/github/pyoncord/xposed/FontsModule.kt index 564c627..aafddbe 100644 --- a/app/src/main/kotlin/io/github/pyoncord/xposed/FontsModule.kt +++ b/app/src/main/kotlin/io/github/pyoncord/xposed/FontsModule.kt @@ -35,10 +35,9 @@ import io.ktor.http.* @Serializable data class FontDefinition( - val name: String, - val description: String, - val spec: Int, - val hash: String, + val name: String? = null, + val description: String? = null, + val spec: Int? = null, val main: Map, ) @@ -52,7 +51,7 @@ class FontsModule: PyonModule() { override fun buildJson(builder: JsonObjectBuilder) { builder.apply { - put("fontPatch", 1) + put("fontPatch", 2) } } @@ -64,7 +63,7 @@ class FontsModule: PyonModule() { Json { ignoreUnknownKeys = true }.decodeFromString(fontDefFile.readText()) } catch (_: Throwable) { return@with } - fontsDir = File(appInfo.dataDir, "files/pyoncord/downloads/fonts").apply { mkdirs() } + fontsDir = File(appInfo.dataDir, "files/pyoncord/downloads/fonts/${fontDef.name}").apply { mkdirs() } fontsAbsPath = fontsDir.absolutePath + "/" fontsDir.listFiles()?.forEach { file -> @@ -78,27 +77,29 @@ class FontsModule: PyonModule() { } } - val scope = MainScope() - val downloadJob = scope.async(Dispatchers.IO) { - fontDef.main.forEach { (name, url) -> - try { - Log.i("Bunny", "Downloading $name from $url") - val file = File(fontsDir, "$name${FILE_EXTENSIONS.first { url.endsWith(it) }}") - val client = HttpClient(CIO) { - install(UserAgent) { agent = "BunnyXposed" } - } + val downloadJob = CoroutineScope(Dispatchers.IO).launch { + fontDef.main.keys.map { name -> + async { + val url = fontDef.main.getValue(name) + try { + Log.i("Bunny", "Downloading $name from $url") + val file = File(fontsDir, "$name${FILE_EXTENSIONS.first { url.endsWith(it) }}") + val client = HttpClient(CIO) { + install(UserAgent) { agent = "BunnyXposed" } + } - val response: HttpResponse = client.get(url) + val response: HttpResponse = client.get(url) - if (response.status == HttpStatusCode.OK) { - file.writeBytes(response.body()) - } + if (response.status == HttpStatusCode.OK) { + file.writeBytes(response.body()) + } - return@async - } catch (e: Throwable) { - Log.e("Bunny", "Failed to download fonts ($name from $url)") + return@async + } catch (e: Throwable) { + Log.e("Bunny", "Failed to download fonts ($name from $url)") + } } - } + }.awaitAll() } XposedHelpers.findAndHookMethod("com.facebook.react.views.text.ReactFontManager", classLoader, "createAssetTypeface",