Fixed internal plugin manager to fix name conflicts

This commit is contained in:
Blatzar 2022-10-06 18:30:58 +02:00
parent fbbcdb4889
commit fb248b6192
3 changed files with 42 additions and 27 deletions

View file

@ -174,8 +174,12 @@ object PluginManager {
val onlineData: Pair<String, SitePlugin>,
) {
val isOutdated =
onlineData.second.version != savedData.version || onlineData.second.version == PLUGIN_VERSION_ALWAYS_UPDATE
onlineData.second.version > savedData.version || onlineData.second.version == PLUGIN_VERSION_ALWAYS_UPDATE
val isDisabled = onlineData.second.status == PROVIDER_STATUS_DOWN
fun validOnlineData(context: Context): Boolean {
return getPluginPath(context, savedData.internalName, onlineData.first).absolutePath == savedData.filePath
}
}
// var allCurrentOutDatedPlugins: Set<OnlinePluginData> = emptySet()
@ -225,6 +229,8 @@ object PluginManager {
.filter { onlineData -> savedData.internalName == onlineData.second.internalName }
.map { onlineData ->
OnlinePluginData(savedData, onlineData)
}.filter {
it.validOnlineData(activity)
}
}.flatten().distinctBy { it.onlineData.second.url }
@ -416,6 +422,18 @@ object PluginManager {
) + "." + name.hashCode()
}
/**
* This should not be changed as it is used to also detect if a plugin is installed!
**/
fun getPluginPath(
context: Context,
internalName: String,
repositoryUrl: String
): File {
val folderName = getPluginSanitizedFileName(repositoryUrl) // Guaranteed unique
val fileName = getPluginSanitizedFileName(internalName)
return File("${context.filesDir}/${ONLINE_PLUGINS_FOLDER}/${folderName}/$fileName.cs3")
}
/**
* Used for fresh installs
@ -426,9 +444,7 @@ object PluginManager {
internalName: String,
repositoryUrl: String
): Boolean {
val folderName = getPluginSanitizedFileName(repositoryUrl) // Guaranteed unique
val fileName = getPluginSanitizedFileName(internalName)
val file = File("${activity.filesDir}/${ONLINE_PLUGINS_FOLDER}/${folderName}/$fileName.cs3")
val file = getPluginPath(activity, internalName, repositoryUrl)
downloadAndLoadPlugin(activity, pluginUrl, internalName, file)
return true
}
@ -454,7 +470,13 @@ object PluginManager {
return loadPlugin(
activity,
newFile ?: return false,
PluginData(internalName, pluginUrl, true, newFile.absolutePath, PLUGIN_VERSION_NOT_SET)
PluginData(
internalName,
pluginUrl,
true,
newFile.absolutePath,
PLUGIN_VERSION_NOT_SET
)
)
} catch (e: Exception) {
logError(e)

View file

@ -147,7 +147,7 @@ class PluginsFragment : Fragment() {
pluginViewModel.updatePluginListLocal()
tv_types_scroll_view?.isVisible = false
} else {
pluginViewModel.updatePluginList(url)
pluginViewModel.updatePluginList(context, url)
tv_types_scroll_view?.isVisible = true
// 💀💀💀💀💀💀💀 Recyclerview when

View file

@ -1,6 +1,7 @@
package com.lagradost.cloudstream3.ui.settings.extensions
import android.app.Activity
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
@ -13,6 +14,7 @@ import com.lagradost.cloudstream3.apmap
import com.lagradost.cloudstream3.mvvm.launchSafe
import com.lagradost.cloudstream3.plugins.PluginData
import com.lagradost.cloudstream3.plugins.PluginManager
import com.lagradost.cloudstream3.plugins.PluginManager.getPluginPath
import com.lagradost.cloudstream3.plugins.RepositoryManager
import com.lagradost.cloudstream3.plugins.SitePlugin
import com.lagradost.cloudstream3.ui.result.txt
@ -45,8 +47,8 @@ class PluginsViewModel : ViewModel() {
private val repositoryCache: MutableMap<String, List<Plugin>> = mutableMapOf()
const val TAG = "PLG"
private fun isDownloaded(plugin: Plugin, data: Set<String>? = null): Boolean {
return (data ?: getDownloads()).contains(plugin.second.internalName)
private fun isDownloaded(context: Context, pluginName: String, repositoryUrl: String): Boolean {
return getPluginPath(context, pluginName, repositoryUrl).exists()
}
private suspend fun getPlugins(
@ -63,24 +65,15 @@ class PluginsViewModel : ViewModel() {
?.also { repositoryCache[repositoryUrl] = it } ?: emptyList()
}
private fun getStoredPlugins(): Array<PluginData> {
return PluginManager.getPluginsOnline()
}
private fun getDownloads(): Set<String> {
return getStoredPlugins().map { it.internalName }.toSet()
}
/**
* @param viewModel optional, updates the plugins livedata for that viewModel if included
* */
fun downloadAll(activity: Activity?, repositoryUrl: String, viewModel: PluginsViewModel?) =
ioSafe {
if (activity == null) return@ioSafe
val stored = getDownloads()
val plugins = getPlugins(repositoryUrl)
plugins.filter { plugin -> !isDownloaded(plugin, stored) }.also { list ->
plugins.filter { plugin -> !isDownloaded(activity, plugin.second.internalName, repositoryUrl) }.also { list ->
main {
showToast(
activity,
@ -103,7 +96,7 @@ class PluginsViewModel : ViewModel() {
PluginManager.downloadAndLoadPlugin(
activity,
metadata.url,
metadata.name,
metadata.internalName,
repo
)
}.main { list ->
@ -117,7 +110,7 @@ class PluginsViewModel : ViewModel() {
),
Toast.LENGTH_SHORT
)
viewModel?.updatePluginListPrivate(repositoryUrl)
viewModel?.updatePluginListPrivate(activity, repositoryUrl)
} else if (list.isNotEmpty()) {
showToast(activity, R.string.download_failed, Toast.LENGTH_SHORT)
}
@ -140,7 +133,7 @@ class PluginsViewModel : ViewModel() {
if (activity == null) return@ioSafe
val (repo, metadata) = plugin
val (success, message) = if (isDownloaded(plugin) || isLocal) {
val (success, message) = if (isDownloaded(activity, plugin.second.internalName, plugin.first) || isLocal) {
PluginManager.deletePlugin(
metadata.url,
isLocal
@ -165,14 +158,13 @@ class PluginsViewModel : ViewModel() {
if (isLocal)
updatePluginListLocal()
else
updatePluginListPrivate(repositoryUrl)
updatePluginListPrivate(activity, repositoryUrl)
}
private suspend fun updatePluginListPrivate(repositoryUrl: String) {
val stored = getDownloads()
private suspend fun updatePluginListPrivate(context: Context, repositoryUrl: String) {
val plugins = getPlugins(repositoryUrl)
val list = plugins.map { plugin ->
PluginViewData(plugin, isDownloaded(plugin, stored))
PluginViewData(plugin, isDownloaded(context, plugin.second.internalName, plugin.first))
}
this.plugins = list
@ -211,9 +203,10 @@ class PluginsViewModel : ViewModel() {
_filteredPlugins.postValue(false to plugins.filterTvTypes().filterLang().sortByQuery(currentQuery))
}
fun updatePluginList(repositoryUrl: String) = viewModelScope.launchSafe {
fun updatePluginList(context: Context?, repositoryUrl: String) = viewModelScope.launchSafe {
if (context == null) return@launchSafe
Log.i(TAG, "updatePluginList = $repositoryUrl")
updatePluginListPrivate(repositoryUrl)
updatePluginListPrivate(context, repositoryUrl)
}
fun search(query: String?) {