refactor: composer loader

Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com>
This commit is contained in:
rhunk
2024-06-08 21:31:47 +02:00
parent c72cc1d320
commit 31ae06ed62
4 changed files with 15 additions and 15 deletions

View File

@@ -91,6 +91,12 @@ class RemoteFileHandleManager(
File(userImportFolder, name.substringAfterLast("/"))
)
}
FileHandleScope.COMPOSER -> {
return AssetFileHandle(
context,
"composer/${name.substringAfterLast("/")}"
)
}
else -> return null
}
}

View File

@@ -193,12 +193,6 @@ class RemoteScriptManager(
}
override fun getScriptContent(moduleName: String): ParcelFileDescriptor? {
if (moduleName.startsWith("composer/")) {
return runCatching {
context.androidContext.assets.open("composer/${moduleName.removePrefix("composer/")}")
.toParcelFileDescriptor(context.coroutineScope)
}.getOrNull()
}
return getScriptInputStream(moduleName) { it?.toParcelFileDescriptor(context.coroutineScope) }
}

View File

@@ -15,7 +15,8 @@ enum class FileHandleScope(
) {
INTERNAL("internal"),
LOCALE("locale"),
USER_IMPORT("user_import");
USER_IMPORT("user_import"),
COMPOSER("composer");
companion object {
fun fromValue(name: String): FileHandleScope? = entries.find { it.key == name }

View File

@@ -19,6 +19,8 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch
import me.rhunk.snapenhance.common.bridge.FileHandleScope
import me.rhunk.snapenhance.common.bridge.toWrapper
import me.rhunk.snapenhance.common.ui.AppMaterialTheme
import me.rhunk.snapenhance.common.ui.createComposeAlertDialog
import me.rhunk.snapenhance.common.ui.createComposeView
@@ -193,14 +195,11 @@ class ComposerHooks: Feature("ComposerHooks", loadParams = FeatureLoadParams.INI
context.log.error("ComposerHooks cannot be loaded without NativeLib")
return
}
val loaderScript = context.scriptRuntime.scripting.getScriptContent("composer/loader.js")?.let { pfd ->
ParcelFileDescriptor.AutoCloseInputStream(pfd).use {
it.readBytes().toString(Charsets.UTF_8)
}
} ?: run {
context.log.error("Failed to load composer loader script")
return
}
val loaderScript = runCatching {
context.fileHandlerManager.getFileHandle(FileHandleScope.COMPOSER.key, "loader.js").toWrapper().readBytes().toString(Charsets.UTF_8)
}.onFailure {
context.log.error("Failed to load composer loader script", it)
}.getOrNull() ?: return
context.native.setComposerLoader("""
(() => { const _getImportsFunctionName = "$getImportsFunctionName"; $loaderScript })();
""".trimIndent().trim())