diff --git a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt index 925f113b14..fa49fbd478 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt @@ -25,6 +25,7 @@ import java.net.HttpURLConnection import java.net.URL import java.util.Locale import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean object BattleNetInstall { @@ -37,6 +38,7 @@ object BattleNetInstall { private const val VERSIONS = "https://%s.version.battle.net/v2/products/%s/versions" private const val BUILD_INFO = ".build.info" private const val CHECK_AGAIN_MS = 30 * 60_000L + private const val START_WAIT_MS = 60_000L internal val LOCALES = listOf( "enUS", "enGB", "esMX", "esES", "ptBR", "ptPT", "frFR", "deDE", "itIT", "ruRU", "jaJP", "koKR", "plPL", "trTR", @@ -114,18 +116,33 @@ object BattleNetInstall { fun uninstall(context: Context, game: InstalledStoreGame): Boolean { if (product(game.sidecar.id) == null || !BattleNet.installed(context)) return false - val uid = installUid(context, game) - var done = false - val code = runCatching { - run(context, listOf("agent-uninstall", uid), AddedGames.guestPath(context, game.folder), null) { line -> - if (line.startsWith(DONE)) done = true - } - }.onFailure { Log.w(TAG, "uninstall $uid", it) }.getOrDefault(-1) - Log.i(TAG, "uninstall $uid: ${if (done) "done" else "exit $code"}") + val done = agentUninstall(context, installUid(context, game), game.folder) if (done) StoresState.post { StoresState.notifyLibraryChanged(context.applicationContext) } return done } + internal fun agentUninstall(context: Context, uid: String, folder: File): Boolean { + val stop = AtomicBoolean(false) + val started = AtomicBoolean(false) + val done = AtomicBoolean(false) + val guard = Thread({ + try { + Thread.sleep(START_WAIT_MS) + if (!started.get()) stop.set(true) + } catch (_: InterruptedException) { + } + }, "battlenet-uninstall-wait").apply { isDaemon = true; start() } + val code = runCatching { + run(context, listOf("agent-uninstall", uid), AddedGames.guestPath(context, folder), stop) { line -> + if (line == STATE + "starting") started.set(true) + if (line.startsWith(DONE)) done.set(true) + } + }.onFailure { Log.w(TAG, "uninstall $uid", it) }.getOrDefault(-1) + guard.interrupt() + Log.i(TAG, "uninstall $uid: ${if (done.get()) "done" else if (stop.get()) "Battle.net stayed busy" else "exit $code"}") + return done.get() + } + internal fun buildKey(text: String): Pair? { val rows = text.lines().filter { it.isNotBlank() } if (rows.size < 2) return null @@ -215,10 +232,21 @@ internal class BattleNetInstaller( private val update: Boolean, ) : DownloadQueue.DownloadJob { private val cancelled = AtomicBoolean(false) + private val started = AtomicBoolean(false) + private val ended = java.util.concurrent.CountDownLatch(1) private var before = 0L private var kept = -1L override fun run(handle: DownloadQueue.JobHandle): String? { + started.set(true) + try { + return if (cancelled.get()) null else work(handle) + } finally { + ended.countDown() + } + } + + private fun work(handle: DownloadQueue.JobHandle): String? { before = DownloadQueue.entry(handle.key)?.bytesTotal ?: 0L handle.stage(DownloadStage.MANIFEST, app.getString(R.string.bnet_dl_starting)) if (!BattleNet.installed(app)) throw IllegalStateException(app.getString(R.string.bnet_dl_client_first)) @@ -272,12 +300,12 @@ internal class BattleNetInstaller( if (!deleteFiles || update) return val uid = item.extra[BattleNetInstall.INSTALL_UID] ?: BattleNet.title(item.id)?.uid ?: return Thread({ - Thread.sleep(2_000) - val gone = runCatching { - BattleNetInstall.run(app, listOf("agent-uninstall", uid), AddedGames.guestPath(app, folder), null) {} - }.getOrDefault(-1) - if (folder.isDirectory) com.droiddeck.launcher.core.FileUtils.delete(folder) - Log.i("BattleNetInstall", "cancelled install of $uid cleaned up (exit $gone)") + if (started.get() && !ended.await(END_WAIT_S, TimeUnit.SECONDS)) Log.w("BattleNetInstall", "the cancelled install of $uid is still stopping") + val gone = BattleNetInstall.agentUninstall(app, uid, folder) + if (folder.isDirectory && (gone || folder.list().isNullOrEmpty())) com.droiddeck.launcher.core.FileUtils.delete(folder) + Log.i("BattleNetInstall", "cancelled install of $uid: ${if (gone) "removed" else "left for Battle.net to remove"}") }, "battlenet-cancel").start() } } + +private const val END_WAIT_S = 120L diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index ab9b323b70..3a54ebd0df 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -2010,11 +2010,11 @@ Jogar Instalar Abrir o Battle.net - A iniciar o transferidor da Blizzard… + A iniciar o programa de transferência da Blizzard… A aguardar que o Battle.net feche… A aguardar a outra transferência do Battle.net… Abra o Battle.net e inicie sessão uma vez; depois instale os jogos dele aqui. - O transferidor da Blizzard parou: %1$s + O programa de transferência da Blizzard parou: %1$s Erro da Blizzard %1$s Atualizar Jogos da Blizzard diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet b/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet index 0552642b8e..5b3444be03 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet @@ -1248,7 +1248,7 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country=" "version": game.get("local_version", "")}, sort_keys=True)) say("%s %s in %s" % (uid, "updated" if action == "update" else "installed", game.get("install_dir", windows))) return 0 - if reason is None and action == "uninstall" and game.get("installed") is not True: + if reason is None and action == "uninstall" and game.get("installed") is False: emit("DONE", json.dumps({"uninstalled": uid})) say("%s uninstalled" % uid) return 0 diff --git a/tools/tests/test_battlenet.py b/tools/tests/test_battlenet.py index b44d7b424a..d0c01b9a08 100644 --- a/tools/tests/test_battlenet.py +++ b/tools/tests/test_battlenet.py @@ -1205,6 +1205,12 @@ class AgentJobTest(Harness): self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 0) self.assertEqual(self.lines[:2], [("STATE", "queued"), ("STATE", "starting")]) + def test_an_uninstall_needs_the_agent_to_say_the_game_is_gone(self): + self.install() + self.files = {".game": json.dumps({"error": 2})} + self.assertEqual(BNET["agent_job"](self.root, "uninstall", "gryphon", poll=0), 1) + self.assertEqual(self.lines[-1][0], "ERROR") + def test_without_battle_net_nothing_starts(self): self.assertEqual(BNET["agent_job"](self.root, "install", "gryphon", "gryphon", str(self.tmp / "g"), "enUS", "USA", "US", poll=0), 3) self.assertEqual(self.lines, [("ERROR", "client")])