Fix: fix disabling and enabling bundle injection

This commit is contained in:
kmmiio99o 2025-12-11 16:40:57 +01:00
parent 5876b7da10
commit 035f8aa570
3 changed files with 72 additions and 27 deletions

View file

@ -100,19 +100,29 @@ object HookScriptLoaderModule : Module() {
}
try {
preloadsDir.walk().filter { it.isFile }.forEach(runScriptFile)
// If a disabled marker exists next to the cached bundle, treat this as a global
// disable for the entire script-loading step. In that case, we skip preloads,
// cached main script and the assets fallback so nothing is injected.
val disabledMarker = File(mainScript.parentFile, "${Constants.MAIN_SCRIPT_FILE}.disabled")
if (disabledMarker.exists()) {
Log.i("Script loading disabled by marker; skipping preloads, cached bundle and fallback")
} else {
// Normal behaviour: run preloads then cached main script or fallback to bundled asset
preloadsDir.walk().filter { it.isFile }.forEach(runScriptFile)
if (mainScript.exists()) runScriptFile(mainScript)
else {
Log.i("Main script does not exist, falling back")
if (mainScript.exists()) {
runScriptFile(mainScript)
} else {
Log.i("Main script does not exist, falling back")
if (!::resources.isInitialized) resources = XModuleResources.createInstance(modulePath, null)
if (!::resources.isInitialized) resources = XModuleResources.createInstance(modulePath, null)
XposedBridge.invokeOriginalMethod(
loadScriptFromAssets,
thisObject,
arrayOf(resources.assets, "assets://Shiggy.bundle", loadSynchronously)
)
XposedBridge.invokeOriginalMethod(
loadScriptFromAssets,
thisObject,
arrayOf(resources.assets, "assets://Shiggy.bundle", loadSynchronously)
)
}
}
} catch (e: Throwable) {
Log.e("Unable to run scripts:", e)

View file

@ -24,6 +24,7 @@ import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage
import ShiggyXposed.xposed.Module
import ShiggyXposed.xposed.Constants
import ShiggyXposed.xposed.Utils.Companion.reloadApp
import ShiggyXposed.xposed.Utils.Log
import kotlinx.coroutines.*
@ -677,6 +678,15 @@ object LogBoxModule : Module() {
private fun isBundleInjectionDisabled(context: Context): Boolean {
return try {
// Prefer checking the cache location used by HookScriptLoader.
// If a disabled marker exists next to the bundle in the cache dir, treat injection as disabled.
val cacheDir = File(context.dataDir, Constants.CACHE_DIR)
val bundle = File(cacheDir, Constants.MAIN_SCRIPT_FILE)
val disabled = File(cacheDir, "${Constants.MAIN_SCRIPT_FILE}.disabled")
if (disabled.exists()) return true
if (bundle.exists()) return false
// Fallback to the persisted logbox setting if neither file exists
val settingsFile = File(context.filesDir, "logbox/LOGBOX_SETTINGS")
if (!settingsFile.exists()) return false
val json = JSONObject(settingsFile.readText())
@ -689,32 +699,36 @@ object LogBoxModule : Module() {
private fun toggleBundleInjection(context: Context) {
try {
val pyonDir = getPyoncordDirectory(context)
val bundleFile = File(pyonDir, "bundle.js")
val disabledFile = File(pyonDir, "bundle.js.disabled")
// Operate on the cache directory so HookScriptLoader sees the change immediately.
val cacheDir = File(context.dataDir, Constants.CACHE_DIR)
cacheDir.mkdirs()
val bundle = File(cacheDir, Constants.MAIN_SCRIPT_FILE)
val disabled = File(cacheDir, "${Constants.MAIN_SCRIPT_FILE}.disabled")
// Persist user intention in logbox settings as well
val settingsFile = File(context.filesDir, "logbox/LOGBOX_SETTINGS")
settingsFile.parentFile?.mkdirs()
val settings = if (settingsFile.exists()) JSONObject(settingsFile.readText()) else JSONObject()
val currentlyDisabled = settings.optBoolean("bundleInjectionDisabled", false)
if (!currentlyDisabled) {
// disable: rename bundle if exists
if (bundleFile.exists()) {
bundleFile.renameTo(disabledFile)
}
if (bundle.exists()) {
// disable injection by renaming the bundle out of the expected filename
if (disabled.exists()) disabled.delete()
val renamed = bundle.renameTo(disabled)
settings.put("bundleInjectionDisabled", true)
settingsFile.writeText(settings.toString())
showM3Toast(context, "Bundle injection disabled")
} else {
// enable: restore bundle if previously renamed
if (disabledFile.exists()) {
disabledFile.renameTo(bundleFile)
}
showM3Toast(context, if (renamed) "Bundle injection disabled" else "Bundle injection disabled (marker set)")
} else if (disabled.exists()) {
// enable injection by restoring the bundle filename
val renamed = disabled.renameTo(bundle)
settings.put("bundleInjectionDisabled", false)
settingsFile.writeText(settings.toString())
showM3Toast(context, "Bundle injection enabled")
showM3Toast(context, if (renamed) "Bundle injection enabled" else "Bundle injection enabled (marker removed)")
} else {
// Neither exists — create a disabled marker so injection stays disabled until enabled
disabled.writeText("")
settings.put("bundleInjectionDisabled", true)
settingsFile.writeText(settings.toString())
showM3Toast(context, "Bundle injection disabled (marker created)")
}
reloadApp()

View file

@ -48,6 +48,8 @@ object UpdaterModule : Module() {
private lateinit var cacheDir: File
private lateinit var bundle: File
private lateinit var etag: File
// Path to the application's data directory so we can read LogBox settings
private lateinit var appDataDir: File
private const val TIMEOUT_CACHED = 5000L
private const val TIMEOUT = 10000L
@ -58,6 +60,8 @@ object UpdaterModule : Module() {
"https://github.com/kmmiio99o/ShiggyCord/releases/latest/download/shiggycord.js"
override fun onLoad(packageParam: XC_LoadPackage.LoadPackageParam) = with(packageParam) {
// store app data dir for later checks (LogBox settings live under files/logbox)
appDataDir = File(appInfo.dataDir)
cacheDir = File(appInfo.dataDir, Constants.CACHE_DIR).apply { mkdirs() }
val filesDir = File(appInfo.dataDir, Constants.FILES_DIR).apply { mkdirs() }
@ -75,6 +79,23 @@ object UpdaterModule : Module() {
fun downloadScript(activity: Activity? = null): Job = scope.launch {
try {
// Respect LogBox setting to disable bundle injections.
// If the file "files/logbox/LOGBOX_SETTINGS" contains the flag bundleInjectionDisabled=true,
// skip attempting to download/update the bundle.
try {
val settingsFile = File(appDataDir, "files/logbox/LOGBOX_SETTINGS")
if (settingsFile.exists()) {
val txt = settingsFile.readText()
// look for a simple true marker; this avoids introducing a new JSON dependency here
if (txt.contains("\"bundleInjectionDisabled\"") && txt.contains("true")) {
Log.i("UpdaterModule: bundle injection disabled by LogBox settings — skipping download")
return@launch
}
}
} catch (ignore: Exception) {
// If anything goes wrong reading the setting, proceed with normal behaviour.
}
HttpClient(CIO) {
expectSuccess = false
install(UserAgent) { agent = Constants.USER_AGENT }