diff --git a/app/src/main/kotlin/io/github/revenge/xposed/modules/HookScriptLoaderModule.kt b/app/src/main/kotlin/io/github/revenge/xposed/modules/HookScriptLoaderModule.kt index 2bf78cd..bd6fee8 100644 --- a/app/src/main/kotlin/io/github/revenge/xposed/modules/HookScriptLoaderModule.kt +++ b/app/src/main/kotlin/io/github/revenge/xposed/modules/HookScriptLoaderModule.kt @@ -11,6 +11,7 @@ import io.github.revenge.xposed.Utils.Log import io.github.revenge.xposed.modules.HookScriptLoaderModule.Companion.PRELOADS_DIR import kotlinx.coroutines.runBlocking import java.io.File +import java.lang.reflect.Method /** * Hooks React Native's script loading methods to load custom scripts and bundles. @@ -39,7 +40,7 @@ class HookScriptLoaderModule : Module() { val cacheDir = File(appInfo.dataDir, Constants.CACHE_DIR).apply { asDir() } val filesDir = File(appInfo.dataDir, Constants.FILES_DIR).apply { asDir() } - preloadsDir = File(filesDir, PRELOADS_DIR).apply { asFile() } + preloadsDir = File(filesDir, PRELOADS_DIR).apply { asDir() } mainScript = File(cacheDir, Constants.MAIN_SCRIPT_FILE).apply { asFile() } listOf( @@ -67,46 +68,58 @@ class HookScriptLoaderModule : Module() { loadScriptFromAssets.hook { before { - Log.i("Received call to loadScriptFromAssets, running our scripts first... (asset: ${args[1]}, sync: ${args[2]})") + Log.i("Received call to loadScriptFromAssets: ${args[1]} (sync: ${args[2]})") + runCustomScripts(loadScriptFromFile, loadScriptFromAssets) + } + } - // TODO: Is there a better way to do this? - runBlocking { UpdaterModule.job?.join() } - - val loadSynchronously = args[2] - val runScriptFile = { file: File -> - Log.i("Loading script: ${file.absolutePath}") - - XposedBridge.invokeOriginalMethod( - loadScriptFromFile, - thisObject, - arrayOf(file.absolutePath, file.absolutePath, loadSynchronously) - ) - - Unit - } - - try { - preloadsDir.walk() - .filter { it.isFile } - .forEach(runScriptFile) - - if (mainScript.exists()) runScriptFile(mainScript) - else { - Log.i("Main script does not exist, falling back") - - if (!::resources.isInitialized) resources = - XModuleResources.createInstance(modulePath, null) - - XposedBridge.invokeOriginalMethod( - loadScriptFromAssets, - thisObject, - arrayOf(resources.assets, "assets://revenge.bundle", loadSynchronously) - ) - } - } catch (e: Throwable) { - Log.e("Unable to run scripts:", e) - } + loadScriptFromFile.hook { + before { + Log.i("Received call to loadScriptFromFile: ${args[0]} (sync: ${args[2]})") + runCustomScripts(loadScriptFromFile, loadScriptFromAssets) } } } + + private fun HookScope.runCustomScripts(loadScriptFromFile: Method, loadScriptFromAssets: Method) { + Log.i("Running custom scripts...") + + // TODO: Is there a better way to do this? + runBlocking { UpdaterModule.job?.join() } + + val loadSynchronously = args[2] + val runScriptFile = { file: File -> + Log.i("Loading script: ${file.absolutePath}") + + XposedBridge.invokeOriginalMethod( + loadScriptFromFile, + thisObject, + arrayOf(file.absolutePath, file.absolutePath, loadSynchronously) + ) + + Unit + } + + try { + preloadsDir.walk() + .filter { it.isFile } + .forEach(runScriptFile) + + if (mainScript.exists()) runScriptFile(mainScript) + else { + Log.i("Main script does not exist, falling back") + + if (!::resources.isInitialized) resources = + XModuleResources.createInstance(modulePath, null) + + XposedBridge.invokeOriginalMethod( + loadScriptFromAssets, + thisObject, + arrayOf(resources.assets, "assets://revenge.bundle", loadSynchronously) + ) + } + } catch (e: Throwable) { + Log.e("Unable to run scripts:", e) + } + } } \ No newline at end of file