From a9bb9009943eb91b2054e2de707e0d9ae540dab3 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Mon, 8 Sep 2025 19:00:41 +0200 Subject: [PATCH] Change when plugins are disabled on reload and listing reloads --- .../platformplayer/states/StatePlatform.kt | 37 ++++++++++++++----- .../platformplayer/states/StatePlugins.kt | 29 ++++++++------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/futo/platformplayer/states/StatePlatform.kt b/app/src/main/java/com/futo/platformplayer/states/StatePlatform.kt index 972ce336..8f5fc3ba 100644 --- a/app/src/main/java/com/futo/platformplayer/states/StatePlatform.kt +++ b/app/src/main/java/com/futo/platformplayer/states/StatePlatform.kt @@ -177,16 +177,11 @@ class StatePlatform { } withContext(Dispatchers.IO) { + var toDisables = mutableListOf(); var enabled: Array; synchronized(_clientsLock) { for(e in _enabledClients) { - try { - e.disable(); - onSourceDisabled.emit(e); - } - catch(ex: Throwable) { - UIDialogs.appToast(ToastView.Toast("If this happens often, please inform the developers on Github", false, null, "Plugin [${e.name}] failed to disable")); - } + toDisables.add(e); } _enabledClients.clear(); @@ -236,6 +231,18 @@ class StatePlatform { } } selectClients(*enabled); + + for(toDisable in toDisables) { + launch(Dispatchers.IO) { + try { + toDisable.disable(); + onSourceDisabled.emit(toDisable); + } + catch(ex: Throwable) { + Logger.e(TAG, "FAILED TO DISABLE CLIENT [${toDisable?.name}] AFTER UpdateAvailableClients", ex); + } + } + } }; } @@ -348,11 +355,11 @@ class StatePlatform { StateApp.instance.handleCaptchaException(c, ex); } + var toDisable: IPlatformClient? = null; synchronized(_clientsLock) { if (_enabledClients.contains(client)) { _enabledClients.remove(client); - client.disable(); - onSourceDisabled.emit(client); + toDisable = client; newClient.initialize(); _enabledClients.add(newClient); } @@ -360,6 +367,18 @@ class StatePlatform { _availableClients.removeIf { it.id == id }; _availableClients.add(newClient); } + if(toDisable != null) { + launch(Dispatchers.IO) { + try { + toDisable?.disable(); + onSourceDisabled.emit(client); + } + catch (ex: Throwable) { + Logger.e(TAG, "FAILED TO DISABLE CLIENT [${toDisable?.name}] AFTER RELOAD", ex); + } + } + } + afterReload?.invoke(); return@withContext newClient; }; diff --git a/app/src/main/java/com/futo/platformplayer/states/StatePlugins.kt b/app/src/main/java/com/futo/platformplayer/states/StatePlugins.kt index e80a2df3..65674610 100644 --- a/app/src/main/java/com/futo/platformplayer/states/StatePlugins.kt +++ b/app/src/main/java/com/futo/platformplayer/states/StatePlugins.kt @@ -402,18 +402,25 @@ class StatePlugins { } val icon = config.absoluteIconUrl?.let { absIconUrl -> - withContext(Dispatchers.Main) { - it.setText("Saving plugin..."); - it.setProgress(0.75); - } val iconResp = client.get(absIconUrl); if(iconResp.isOk) return@let iconResp.body?.byteStream()?.use { it.readBytes() }; return@let null; } + + withContext(Dispatchers.Main) { + it.setText("Saving plugin..."); + it.setProgress(0.75); + } + val installEx = StatePlugins.instance.createPlugin(config, script, icon, reinstall); if(installEx != null) throw installEx; + + withContext(Dispatchers.Main) { + it.setText("Reloading available plugins..."); + it.setProgress(0.9); + } StatePlatform.instance.updateAvailableClients(context); withContext(Dispatchers.Main) { @@ -522,9 +529,7 @@ class StatePlugins { if(id == StateDeveloper.DEV_ID) throw IllegalStateException("Attempted to retrieve a persistent developer plugin, this is not allowed"); - synchronized(_plugins) { - return _plugins.findItem { it.config.id == id }; - } + return _plugins.findItem { it.config.id == id }; } fun getPlugins(): List { return _plugins.getItems(); @@ -533,12 +538,10 @@ class StatePlugins { fun deletePlugin(id: String) { synchronized(_pluginScripts) { - synchronized(_plugins) { - _pluginScripts.deleteFile(id); - val plugins = _plugins.findItems { it.config.id == id }; - for(plugin in plugins) - _plugins.delete(plugin); - } + _pluginScripts.deleteFile(id); + val plugins = _plugins.findItems { it.config.id == id }; + for(plugin in plugins) + _plugins.delete(plugin); } } fun createPlugin(config: SourcePluginConfig, script: String, icon: ByteArray? = null, reinstall: Boolean = false, flags: List = listOf()) : Throwable? {