Skip to content

Commit

Permalink
fix: undelay module initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Oct 11, 2024
1 parent 42a08d1 commit 59d85ef
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "io.github.pyoncord.xposed"
minSdk = 24
targetSdk = 33
versionCode = 203
versionName = "0.2.3"
versionCode = 204
versionName = "0.2.4"
}

buildTypes {
Expand Down
11 changes: 3 additions & 8 deletions app/src/main/kotlin/io/github/pyoncord/xposed/FontsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@ package io.github.pyoncord.xposed

import android.content.res.AssetManager
import android.os.Build
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.Typeface.CustomFallbackBuilder
import android.graphics.fonts.Font
import android.graphics.fonts.FontFamily
import android.util.Log
import android.webkit.URLUtil
import de.robv.android.xposed.XC_MethodReplacement
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.*
import java.io.IOException
import java.io.File
import java.net.HttpURLConnection
import java.net.URL
import kotlinx.coroutines.*

import io.ktor.client.*
Expand Down Expand Up @@ -67,7 +62,7 @@ class FontsModule: PyonModule() {
val assetManager: AssetManager = param.args[2] as AssetManager
return createAssetTypeface(fontFamilyName, style, assetManager)
}
});
})

val fontDefFile = File(appInfo.dataDir, "files/pyoncord/fonts.json")
if (!fontDefFile.exists()) return@with
Expand Down Expand Up @@ -144,7 +139,7 @@ class FontsModule: PyonModule() {
// ignore
}

for (fontRootPath in arrayOf(fontsAbsPath, FONTS_ASSET_PATH).filter { it != null }) {
for (fontRootPath in arrayOf(fontsAbsPath, FONTS_ASSET_PATH).filterNotNull()) {
for (fileExtension in FILE_EXTENSIONS) {
val fileName = java.lang.StringBuilder()
.append(fontRootPath)
Expand Down Expand Up @@ -220,7 +215,7 @@ class FontsModule: PyonModule() {

// Lastly, after all those checks above, this is the original RN logic for
// getting the typeface.
for (fontRootPath in arrayOf(fontsAbsPath, FONTS_ASSET_PATH).filter { it != null }) {
for (fontRootPath in arrayOf(fontsAbsPath, FONTS_ASSET_PATH).filterNotNull()) {
for (fileExtension in FILE_EXTENSIONS) {
val fileName = java.lang.StringBuilder()
.append(fontRootPath)
Expand Down
34 changes: 25 additions & 9 deletions app/src/main/kotlin/io/github/pyoncord/xposed/Main.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.pyoncord.xposed

import android.app.Activity
import android.app.AndroidAppHelper
import android.content.Context
import android.app.Activity
import android.content.res.AssetManager
import android.content.res.Resources
import android.util.Log
Expand All @@ -12,7 +10,6 @@ import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.callbacks.XC_LoadPackage
import io.github.pyoncord.xposed.BuildConfig
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
Expand Down Expand Up @@ -60,14 +57,27 @@ class Main : IXposedHookLoadPackage {
lpparam.classLoader.loadClass("com.discord.react_activities.ReactActivity")
}.getOrElse { return } // Package is not our the target app, return

var activity: Activity? = null;
val onActivityCreateCallback = mutableSetOf<(activity: Activity) -> Unit>()

XposedBridge.hookMethod(reactActivity.getDeclaredMethod("onCreate", Bundle::class.java), object : XC_MethodHook() {
override fun beforeHookedMethod(param: MethodHookParam) {
init(lpparam, param.thisObject as Activity)
activity = param.thisObject as Activity;
onActivityCreateCallback.forEach { cb -> cb(activity!!) }
onActivityCreateCallback.clear()
}
})

init(lpparam) { cb ->
if (activity != null) cb(activity!!)
else onActivityCreateCallback.add(cb)
}
}

fun init(param: XC_LoadPackage.LoadPackageParam, activity: Activity) = with (param) {
private fun init(
param: XC_LoadPackage.LoadPackageParam,
onActivityCreate: ((activity: Activity) -> Unit) -> Unit
) = with (param) {
val catalystInstanceImpl = classLoader.loadClass("com.facebook.react.bridge.CatalystInstanceImpl")

for (module in pyonModules) module.onInit(param)
Expand Down Expand Up @@ -150,10 +160,16 @@ class Main : IXposedHookLoadPackage {
return@async
} catch (e: RedirectResponseException) {
if (e.response.status != HttpStatusCode.NotModified) throw e;
Log.e("Bunny", "Server reponded with status code 304 - no changes to file")
Log.e("Bunny", "Server responded with status code 304 - no changes to file")
} catch (e: Throwable) {
activity.runOnUiThread {
Toast.makeText(activity.applicationContext, "Failed to fetch JS bundle, Bunny may not load!", Toast.LENGTH_SHORT).show()
onActivityCreate { activity ->
activity.runOnUiThread {
Toast.makeText(
activity.applicationContext,
"Failed to fetch JS bundle, Bunny may not load!",
Toast.LENGTH_SHORT
).show()
}
}

Log.e("Bunny", "Failed to download bundle", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class SysColors(

class SysColorsModule : PyonModule() {
private lateinit var context: Context
fun isSupported() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
private fun isSupported() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

override fun buildJson(builder: JsonObjectBuilder) {
context = AndroidAppHelper.currentApplication()
Expand All @@ -43,7 +43,7 @@ class SysColorsModule : PyonModule() {
}
}

fun convertToColor(id: Int): String {
private fun convertToColor(id: Int): String {
val clr = if (isSupported()) ContextCompat.getColor(context, id) else 0
return String.format("#%06X", 0xFFFFFF and clr)
}
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/kotlin/io/github/pyoncord/xposed/ThemeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class ThemeModule : PyonModule() {
hookTheme()
}

fun File.isValidish(): Boolean {
private fun File.isValidish(): Boolean {
if (!this.exists()) return false

val text = this.readText()
return !text.isBlank() && text != "{}" && text != "null"
return text.isNotBlank() && text != "{}" && text != "null"
}

fun getTheme(): Theme? {
private fun getTheme(): Theme? {
val filesDir = File(param.appInfo.dataDir, "files").apply { mkdirs() }
val pyonDir = File(filesDir, "pyoncord").apply { mkdirs() }
val themeFile = File(pyonDir, "current-theme.json")
Expand Down Expand Up @@ -139,12 +139,12 @@ class ThemeModule : PyonModule() {
}

// Convert 0xRRGGBBAA to 0XAARRGGBB
fun hexStringToColorInt(hexString: String): Int {
private fun hexStringToColorInt(hexString: String): Int {
val parsed = Color.parseColor(hexString)
return parsed.takeIf { hexString.length == 7 } ?: parsed and 0xFFFFFF or (parsed ushr 24)
return parsed.takeIf { hexString.length == 7 } ?: (parsed and 0xFFFFFF or (parsed ushr 24))
}

fun hookThemeMethod(themeClass: Class<*>, methodName: String, themeValue: Int) {
private fun hookThemeMethod(themeClass: Class<*>, methodName: String, themeValue: Int) {
try {
themeClass.getDeclaredMethod(methodName).let { method ->
// Log.i("Hooking $methodName -> ${themeValue.toString(16)}")
Expand Down

0 comments on commit 59d85ef

Please sign in to comment.