Fixed loading disabled plugins on download

This commit is contained in:
Blatzar 2023-01-07 14:56:52 +01:00
parent 657971d008
commit 308affb6aa
4 changed files with 60 additions and 69 deletions

View File

@ -255,11 +255,12 @@ object PluginManager {
//updatedPlugins.add(activity.getString(R.string.single_plugin_disabled, pluginData.onlineData.second.name))
unloadPlugin(pluginData.savedData.filePath)
} else if (pluginData.isOutdated) {
downloadAndLoadPlugin(
downloadPlugin(
activity,
pluginData.onlineData.second.url,
pluginData.savedData.internalName,
File(pluginData.savedData.filePath)
File(pluginData.savedData.filePath),
true
).let { success ->
if (success)
updatedPlugins.add(pluginData.onlineData.second.name)
@ -341,11 +342,12 @@ object PluginManager {
//Log.i(TAG, "notDownloadedPlugins => ${notDownloadedPlugins.toJson()}")
notDownloadedPlugins.apmap { pluginData ->
downloadAndLoadPlugin(
downloadPlugin(
activity,
pluginData.onlineData.second.url,
pluginData.savedData.internalName,
pluginData.onlineData.first
pluginData.onlineData.first,
!pluginData.isDisabled
).let { success ->
if (success)
newDownloadPlugins.add(pluginData.onlineData.second.name)
@ -496,7 +498,7 @@ object PluginManager {
}
}
private fun unloadPlugin(absolutePath: String) {
fun unloadPlugin(absolutePath: String) {
Log.i(TAG, "Unloading plugin: $absolutePath")
val plugin = plugins[absolutePath]
if (plugin == null) {
@ -547,49 +549,48 @@ object PluginManager {
return File("${context.filesDir}/${ONLINE_PLUGINS_FOLDER}/${folderName}/$fileName.cs3")
}
/**
* Used for fresh installs
* */
suspend fun downloadAndLoadPlugin(
suspend fun downloadPlugin(
activity: Activity,
pluginUrl: String,
internalName: String,
repositoryUrl: String
repositoryUrl: String,
loadPlugin: Boolean
): Boolean {
val file = getPluginPath(activity, internalName, repositoryUrl)
downloadAndLoadPlugin(activity, pluginUrl, internalName, file)
return true
return downloadPlugin(activity, pluginUrl, internalName, file, loadPlugin)
}
/**
* Used for updates.
*
* Uses a file instead of repository url, as extensions can get moved it is better to directly
* update the files instead of getting the filepath from repo url.
* */
private suspend fun downloadAndLoadPlugin(
suspend fun downloadPlugin(
activity: Activity,
pluginUrl: String,
internalName: String,
file: File,
loadPlugin: Boolean
): Boolean {
try {
unloadPlugin(file.absolutePath)
Log.d(TAG, "Downloading plugin: $pluginUrl to ${file.absolutePath}")
// The plugin file needs to be salted with the repository url hash as to allow multiple repositories with the same internal plugin names
val newFile = downloadPluginToFile(pluginUrl, file)
return loadPlugin(
activity,
newFile ?: return false,
PluginData(
internalName,
pluginUrl,
true,
newFile.absolutePath,
PLUGIN_VERSION_NOT_SET
)
val newFile = downloadPluginToFile(pluginUrl, file) ?: return false
val data = PluginData(
internalName,
pluginUrl,
true,
newFile.absolutePath,
PLUGIN_VERSION_NOT_SET
)
return if (loadPlugin) {
unloadPlugin(file.absolutePath)
loadPlugin(
activity,
newFile,
data
)
} else {
setPluginData(data)
true
}
} catch (e: Exception) {
logError(e)
return false

View File

@ -7,8 +7,10 @@ import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.amap
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.mvvm.suspendSafeApiCall
import com.lagradost.cloudstream3.plugins.PluginManager.getPluginSanitizedFileName
import com.lagradost.cloudstream3.plugins.PluginManager.unloadPlugin
import com.lagradost.cloudstream3.ui.settings.extensions.REPOSITORIES_KEY
import com.lagradost.cloudstream3.ui.settings.extensions.RepositoryData
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
@ -77,7 +79,7 @@ object RepositoryManager {
} else if (fixedUrl.contains("^(cloudstreamrepo://)|(https://cs\\.repo/\\??)".toRegex())) {
fixedUrl.replace("^(cloudstreamrepo://)|(https://cs\\.repo/\\??)".toRegex(), "").let {
return@let if (!it.contains("^https?://".toRegex()))
"https://${it}"
"https://${it}"
else fixedUrl
}
} else if (fixedUrl.matches("^[a-zA-Z0-9!_-]+$".toRegex())) {
@ -132,7 +134,9 @@ object RepositoryManager {
file.mkdirs()
// Overwrite if exists
if (file.exists()) { file.delete() }
if (file.exists()) {
file.delete()
}
file.createNewFile()
val body = app.get(pluginUrl).okhttpResponse.body
@ -141,34 +145,6 @@ object RepositoryManager {
}
}
suspend fun downloadPluginToFile(
context: Context,
pluginUrl: String,
/** Filename without .cs3 */
fileName: String,
folder: String
): File? {
return suspendSafeApiCall {
val extensionsDir = File(context.filesDir, ONLINE_PLUGINS_FOLDER)
if (!extensionsDir.exists())
extensionsDir.mkdirs()
val newDir = File(extensionsDir, folder)
newDir.mkdirs()
val newFile = File(newDir, "${fileName}.cs3")
// Overwrite if exists
if (newFile.exists()) {
newFile.delete()
}
newFile.createNewFile()
val body = app.get(pluginUrl).okhttpResponse.body
write(body.byteStream(), newFile.outputStream())
newFile
}
}
fun getRepositories(): Array<RepositoryData> {
return getKey(REPOSITORIES_KEY) ?: emptyArray()
}
@ -200,9 +176,17 @@ object RepositoryManager {
extensionsDir,
getPluginSanitizedFileName(repository.url)
)
PluginManager.deleteRepositoryData(file.absolutePath)
file.delete()
// Unload all plugins, not using deletePlugin since we
// delete all data and files in deleteRepositoryData
normalSafeApiCall {
file.listFiles { plugin: File ->
unloadPlugin(plugin.absolutePath)
false
}
}
PluginManager.deleteRepositoryData(file.absolutePath)
}
private fun write(stream: InputStream, output: OutputStream) {

View File

@ -9,6 +9,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.PROVIDER_STATUS_DOWN
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.amap
import com.lagradost.cloudstream3.mvvm.launchSafe
@ -102,11 +103,12 @@ class PluginsViewModel : ViewModel() {
)
}
}.amap { (repo, metadata) ->
PluginManager.downloadAndLoadPlugin(
PluginManager.downloadPlugin(
activity,
metadata.url,
metadata.internalName,
repo
repo,
metadata.status != PROVIDER_STATUS_DOWN
)
}.main { list ->
if (list.any { it }) {
@ -151,12 +153,15 @@ class PluginsViewModel : ViewModel() {
val (success, message) = if (file.exists()) {
PluginManager.deletePlugin(file) to R.string.plugin_deleted
} else {
PluginManager.downloadAndLoadPlugin(
val isEnabled = plugin.second.status != PROVIDER_STATUS_DOWN
val message = if (isEnabled) R.string.plugin_loaded else R.string.plugin_downloaded
PluginManager.downloadPlugin(
activity,
metadata.url,
metadata.name,
repo
) to R.string.plugin_loaded
repo,
isEnabled
) to message
}
runOnMainThread {

View File

@ -539,6 +539,7 @@
<string name="repository_name_hint">Repository name</string>
<string name="repository_url_hint">Repository URL</string>
<string name="plugin_loaded">Plugin Loaded</string>
<string name="plugin_downloaded">Plugin Downloaded</string>
<string name="plugin_deleted">Plugin Deleted</string>
<string name="plugin_load_fail" formatted="true">Could not load %s</string>
<string name="is_adult">18+</string>