mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(battlenet): import, list and delete World of Warcraft addons (#2111)
World of Warcraft, Classic and Forever get an Addons tab in the Library. It imports addon zips into the right Interface/AddOns folder, lists the installed addons, and deletes one after a confirmation.
This commit is contained in:
1 parent
b9dbd006be
commit
a309428a2a
39 files changed
+1142
-1
No files matched your search
@@ -433,6 +433,27 @@ class FrontEndActivity : ComponentActivity() {
|
||||
pickSaveZip.launch(InAppFilePicker.buildIntent(this, listOf("zip"), getString(R.string.main_saves_pick_zip, name), GameSaves.savesDir().parentFile?.parentFile?.path))
|
||||
}
|
||||
|
||||
private fun importAddons(name: String, target: com.droiddeck.launcher.runtime.WowAddons.Target) {
|
||||
if (SessionState.running) {
|
||||
android.widget.Toast.makeText(this, R.string.main_addons_close_session, android.widget.Toast.LENGTH_LONG).show()
|
||||
return
|
||||
}
|
||||
onSavePicked = { zip ->
|
||||
saveAction(getString(R.string.main_addons_importing, name)) {
|
||||
if (SessionState.running) return@saveAction getString(R.string.main_addons_close_session)
|
||||
val imported = try {
|
||||
com.droiddeck.launcher.runtime.WowAddons.import(zip, target)
|
||||
} catch (_: com.droiddeck.launcher.runtime.WowAddons.TooLarge) {
|
||||
return@saveAction getString(R.string.main_addons_too_large, zip.name)
|
||||
}
|
||||
if (imported.isEmpty()) getString(R.string.main_addons_none, zip.name)
|
||||
else resources.getQuantityString(R.plurals.main_addons_imported, imported.size, imported.size, target.flavor)
|
||||
}
|
||||
}
|
||||
val downloads = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).path
|
||||
pickSaveZip.launch(InAppFilePicker.buildIntent(this, listOf("zip"), getString(R.string.main_addons_pick_zip, name), downloads))
|
||||
}
|
||||
|
||||
private fun exportSaves(name: String, layout: GameSaves.Layout, game: () -> GameSaves.Game) {
|
||||
onSavePicked = { dir ->
|
||||
saveAction(getString(R.string.main_saves_exporting, name)) {
|
||||
@@ -1282,6 +1303,13 @@ class FrontEndActivity : ComponentActivity() {
|
||||
onGameComponents = { game -> openComponents(gameKey = game.profileKey) },
|
||||
onComponentsTab = { tab -> openComponents(focusContent = true, tab = tab) },
|
||||
onSaveImport = { sg -> importSaves(sg.name) { GameSaves.game(sg) } },
|
||||
onAddonImport = { sg, target -> importAddons(sg.name, target) },
|
||||
onAddonDelete = { target, name ->
|
||||
saveAction(getString(R.string.main_addons_deleting, name)) {
|
||||
com.droiddeck.launcher.runtime.WowAddons.delete(target, name)
|
||||
getString(R.string.main_addons_deleted, name)
|
||||
}
|
||||
},
|
||||
onSaveExport = { sg, layout -> exportSaves(sg.name, layout) { GameSaves.game(sg) } },
|
||||
onPerformance = { refreshCores(); showComponents = false; showMapping = false; showPerformance = true },
|
||||
onRoms = { showRoms = true },
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import com.droiddeck.launcher.frontend.Library
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
import com.droiddeck.launcher.stores.StoreInstalls
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.LinkOption
|
||||
|
||||
object WowAddons {
|
||||
var imports by mutableIntStateOf(0)
|
||||
private set
|
||||
|
||||
class TooLarge : IOException()
|
||||
|
||||
class Target(val flavor: String, val addOns: File)
|
||||
|
||||
class Addon(val name: String, val root: String)
|
||||
|
||||
internal const val MAX_EXPANDED_BYTES = 4L * 1024 * 1024 * 1024
|
||||
|
||||
private val SUFFIX = Regex("[-_](standard|mainline|classic|vanilla|tbc|bcc|wrath|wotlkc|cata|mists|forever)$", RegexOption.IGNORE_CASE)
|
||||
|
||||
fun targets(game: Library.SteamGame): List<Target> {
|
||||
if (game.source != Store.BATTLENET.id) return emptyList()
|
||||
val title = game.storeId?.let(BattleNet::title) ?: return emptyList()
|
||||
if (title.flavor == null) return emptyList()
|
||||
val folder = game.gameFiles ?: return emptyList()
|
||||
return targets(title, folder)
|
||||
}
|
||||
|
||||
internal fun targets(title: BattleNet.Title, folder: File): List<Target> {
|
||||
val flavors = BattleNet.TITLES.filter { it.code == title.code }.mapNotNull { it.flavor }
|
||||
.flatMap { listOf(it, it.dropLast(1) + "_beta_") }.distinct()
|
||||
val root = if (folder.name in flavors) folder.parentFile ?: return emptyList() else folder
|
||||
return flavors.map { File(root, it) }.filter { it.isDirectory }.map { Target(it.name, addOnsFolder(it)) }
|
||||
}
|
||||
|
||||
fun installed(target: Target): List<String> =
|
||||
target.addOns.listFiles().orEmpty().filter { it.isDirectory && !it.name.startsWith(".") }
|
||||
.map { it.name }.sortedBy { it.lowercase() }
|
||||
|
||||
fun delete(target: Target, name: String) {
|
||||
val folder = target.addOns.listFiles()?.firstOrNull { it.name == name && !it.name.startsWith(".") }
|
||||
?: throw IOException("Cannot find $name")
|
||||
StoreInstalls.deleteTree(folder)
|
||||
if (Files.exists(folder.toPath(), LinkOption.NOFOLLOW_LINKS)) throw IOException("Cannot delete $name")
|
||||
imports++
|
||||
}
|
||||
|
||||
fun import(zipFile: File, target: Target): List<String> {
|
||||
ZipFile.builder().setFile(zipFile).get().use { zip ->
|
||||
val all = zip.entries.toList()
|
||||
if (all.any { it.isUnixSymlink }) return emptyList()
|
||||
val entries = all.filter { clean(it.name) != null }
|
||||
val addons = addons(entries.filter { !it.isDirectory }.mapNotNull { clean(it.name) })
|
||||
if (addons.isEmpty()) return emptyList()
|
||||
val addOns = target.addOns
|
||||
if (!addOns.isDirectory && !addOns.mkdirs()) throw IOException("Cannot create ${addOns.path}")
|
||||
val stage = File(addOns.parentFile, ".addon-import-${System.nanoTime()}")
|
||||
try {
|
||||
val fresh = File(stage, "new")
|
||||
val buffer = ByteArray(64 * 1024)
|
||||
var written = 0L
|
||||
for (entry in entries) {
|
||||
if (entry.isDirectory) continue
|
||||
val path = clean(entry.name) ?: continue
|
||||
val addon = addons.firstOrNull { path.startsWith(it.root) } ?: continue
|
||||
val out = inside(fresh, addon.name + "/" + path.removePrefix(addon.root)) ?: continue
|
||||
out.parentFile?.mkdirs()
|
||||
zip.getInputStream(entry).use { input ->
|
||||
out.outputStream().use { output ->
|
||||
while (true) {
|
||||
val read = input.read(buffer)
|
||||
if (read < 0) break
|
||||
written += read
|
||||
if (written > MAX_EXPANDED_BYTES) throw TooLarge()
|
||||
output.write(buffer, 0, read)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
swap(addons.map { File(fresh, it.name) to it.name }, addOns, File(stage, "old").apply { mkdirs() })
|
||||
} finally {
|
||||
StoreInstalls.deleteTree(stage)
|
||||
}
|
||||
imports++
|
||||
return addons.map { it.name }
|
||||
}
|
||||
}
|
||||
|
||||
internal fun addons(files: List<String>): List<Addon> {
|
||||
val tocs = files.filter { it.endsWith(".toc", ignoreCase = true) }
|
||||
val folders = tocs.map { it.substringBeforeLast('/', "") }.distinct().sortedBy { it.length }
|
||||
val roots = ArrayList<String>()
|
||||
for (folder in folders) {
|
||||
if (roots.none { it.isEmpty() || folder.startsWith("$it/") }) roots.add(folder)
|
||||
}
|
||||
return roots.mapNotNull { root ->
|
||||
val inFolder = tocs.filter { it.substringBeforeLast('/', "") == root }
|
||||
.map { it.substringAfterLast('/').dropLast(4) }
|
||||
val name = addonName(root.substringAfterLast('/'), inFolder)
|
||||
name.takeIf { it.isNotEmpty() && !it.startsWith(".") }?.let { Addon(it, if (root.isEmpty()) "" else "$root/") }
|
||||
}.distinctBy { it.name.lowercase() }
|
||||
}
|
||||
|
||||
private fun addonName(folder: String, tocs: List<String>): String {
|
||||
val match = tocs.firstOrNull { it.equals(folder, ignoreCase = true) }
|
||||
?: tocs.firstOrNull { it.replace(SUFFIX, "").equals(folder, ignoreCase = true) }
|
||||
if (match != null) return folder
|
||||
return tocs.map { it.replace(SUFFIX, "") }.minByOrNull { it.length }.orEmpty()
|
||||
}
|
||||
|
||||
private fun clean(name: String): String? {
|
||||
val parts = name.replace('\\', '/').trim('/').split('/')
|
||||
if (name.startsWith("/") || name.startsWith("\\") || parts.any { it.isEmpty() || it == "." || it == ".." || it.endsWith(":") }) return null
|
||||
if (parts.first() == "__MACOSX" || parts.last().startsWith("._")) return null
|
||||
return parts.joinToString("/")
|
||||
}
|
||||
|
||||
private fun inside(root: File, path: String): File? {
|
||||
val out = File(root, path)
|
||||
return out.takeIf { it.canonicalPath.startsWith(root.canonicalPath + File.separator) }
|
||||
}
|
||||
|
||||
private fun addOnsFolder(flavor: File): File = child(child(flavor, "Interface"), "AddOns")
|
||||
|
||||
private fun child(parent: File, name: String): File =
|
||||
parent.listFiles()?.firstOrNull { it.name.equals(name, ignoreCase = true) } ?: File(parent, name)
|
||||
|
||||
internal fun swap(fresh: List<Pair<File, String>>, addOns: File, old: File) {
|
||||
val done = ArrayList<Replaced>()
|
||||
try {
|
||||
for ((folder, name) in fresh) done.add(replace(folder, addOns, name, old))
|
||||
} catch (e: IOException) {
|
||||
done.asReversed().forEach { undo(it) }
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
private class Replaced(val installed: File, val previous: File?, val kept: File?)
|
||||
|
||||
private fun undo(replaced: Replaced) {
|
||||
StoreInstalls.deleteTree(replaced.installed)
|
||||
if (replaced.previous != null) replaced.kept?.renameTo(replaced.previous)
|
||||
}
|
||||
|
||||
private fun replace(fresh: File, addOns: File, name: String, old: File): Replaced {
|
||||
val current = addOns.listFiles()?.firstOrNull { it.name.equals(name, ignoreCase = true) }
|
||||
val kept = current?.let { File(old, it.name) }
|
||||
if (current != null && !current.renameTo(kept!!)) throw IOException("Cannot replace ${current.path}")
|
||||
val installed = File(addOns, name)
|
||||
if (!fresh.renameTo(installed)) {
|
||||
kept?.renameTo(current)
|
||||
throw IOException("Cannot install $name")
|
||||
}
|
||||
return Replaced(installed, current, kept)
|
||||
}
|
||||
}
|
||||
@@ -219,6 +219,8 @@ class FrontEndActions(
|
||||
val onGameComponents: (Library.SteamGame) -> Unit = { onComponents(true) },
|
||||
val onComponentsTab: (String) -> Unit = {},
|
||||
val onSaveImport: (Library.SteamGame) -> Unit = {},
|
||||
val onAddonImport: (Library.SteamGame, com.droiddeck.launcher.runtime.WowAddons.Target) -> Unit = { _, _ -> },
|
||||
val onAddonDelete: (com.droiddeck.launcher.runtime.WowAddons.Target, String) -> Unit = { _, _ -> },
|
||||
val onSaveExport: (Library.SteamGame, com.droiddeck.launcher.session.GameSaves.Layout) -> Unit = { _, _ -> },
|
||||
val onPerformance: () -> Unit,
|
||||
val onRoms: () -> Unit,
|
||||
|
||||
@@ -73,6 +73,7 @@ import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.res.pluralStringResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
@@ -86,7 +87,9 @@ import coil.compose.AsyncImage
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.frontend.GameRemoval
|
||||
import com.droiddeck.launcher.frontend.Library
|
||||
import com.droiddeck.launcher.runtime.WowAddons
|
||||
import com.droiddeck.launcher.session.GameSaves
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import com.droiddeck.launcher.stores.CatalogItem
|
||||
import com.droiddeck.launcher.stores.CloudSaves
|
||||
import com.droiddeck.launcher.stores.InstalledStoreGame
|
||||
@@ -136,7 +139,7 @@ internal class GameRef(val appId: Int?, val store: Store?, val storeId: String?)
|
||||
}
|
||||
}
|
||||
|
||||
internal enum class GameTab { ABOUT, ACHIEVEMENTS, LAUNCH, SAVES, INSTALL }
|
||||
internal enum class GameTab { ABOUT, ACHIEVEMENTS, LAUNCH, SAVES, ADDONS, INSTALL }
|
||||
|
||||
@Composable
|
||||
internal fun GameDetail(
|
||||
@@ -180,12 +183,14 @@ internal fun GameDetail(
|
||||
.takeUnless { it.trim().equals(title.trim(), ignoreCase = true) }.orEmpty()
|
||||
val scored = scores != null && (scores.metacritic != Lookup.None || scores.hltb != Lookup.None)
|
||||
val prefix = game?.protonPrefix?.takeIf { it.isDirectory }
|
||||
val addonTargets = remember(game?.gameId, game?.gameFiles) { game?.let(WowAddons::targets).orEmpty() }
|
||||
val tabs = buildList {
|
||||
if (description.isNotBlank() || developer.isNotBlank() || publisher.isNotBlank() || releaseDate.isNotBlank() || scored || (!s.shortcutPicker && title.isNotBlank() && title != id)) add(GameTab.ABOUT)
|
||||
if (achievementState != null) add(GameTab.ACHIEVEMENTS)
|
||||
val cloud = game?.library == Library.XBOX_CLOUD
|
||||
if (game != null && !cloud) add(GameTab.LAUNCH)
|
||||
if (game != null && game.library == Library.ADDED && prefix != null) add(GameTab.SAVES)
|
||||
if (addonTargets.isNotEmpty()) add(GameTab.ADDONS)
|
||||
if ((game != null && !cloud) || installed != null) add(GameTab.INSTALL)
|
||||
}
|
||||
var picked by remember(key) { mutableStateOf<GameTab?>(null) }
|
||||
@@ -248,6 +253,7 @@ internal fun GameDetail(
|
||||
} ?: stringResource(R.string.achievements_title)
|
||||
GameTab.LAUNCH -> stringResource(R.string.game_tab_launch)
|
||||
GameTab.SAVES -> stringResource(if (Store.byId(source).let { it == Store.GOG || it == Store.EPIC }) R.string.game_tab_saves_cloud else R.string.games_saves)
|
||||
GameTab.ADDONS -> stringResource(R.string.game_tab_addons)
|
||||
GameTab.INSTALL -> stringResource(R.string.game_tab_install)
|
||||
}
|
||||
}
|
||||
@@ -263,6 +269,7 @@ internal fun GameDetail(
|
||||
GameTab.ACHIEVEMENTS -> achievementState?.let { st -> AchievementsTab(StoreAchievements.forSource(store?.id)?.label.orEmpty(), st) }
|
||||
GameTab.LAUNCH -> if (game != null) LaunchTab(game, s, a, hopRail)
|
||||
GameTab.SAVES -> if (game != null && prefix != null) SavesTab(game, prefix, a)
|
||||
GameTab.ADDONS -> if (game != null) AddonsTab(game, addonTargets, a)
|
||||
GameTab.INSTALL -> InstallTab(game, installed, item, store)
|
||||
}
|
||||
}
|
||||
@@ -857,6 +864,44 @@ private fun SavesTab(g: Library.SteamGame, prefix: File, a: FrontEndActions) {
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AddonsTab(g: Library.SteamGame, targets: List<WowAddons.Target>, a: FrontEndActions) {
|
||||
Column(Modifier.fillMaxWidth().tabPanel(), verticalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
targets.forEach { target ->
|
||||
val names by produceState(emptyList<String>(), target.addOns, WowAddons.imports) {
|
||||
value = withContext(Dispatchers.IO) { WowAddons.installed(target) }
|
||||
}
|
||||
if (targets.size > 1) SectionLabel(target.flavor)
|
||||
MenuItem(
|
||||
stringResource(R.string.games_import_addons), checked = false,
|
||||
detail = pluralStringResource(R.plurals.games_addons_count, names.size, names.size),
|
||||
) { a.onAddonImport(g, target) }
|
||||
MenuItem(stringResource(R.string.games_open_addons_folder), checked = false) {
|
||||
target.addOns.mkdirs()
|
||||
a.onBrowseFiles(target.addOns)
|
||||
}
|
||||
names.forEach { name -> AddonRow(name, target, a) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AddonRow(name: String, target: WowAddons.Target, a: FrontEndActions) {
|
||||
var asking by remember(target.addOns, name) { mutableStateOf(false) }
|
||||
val delete = stringResource(R.string.common_delete)
|
||||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(start = 16.dp, top = 2.dp, bottom = 2.dp)) {
|
||||
Text(name, fontSize = 14.sp, color = MaterialTheme.colorScheme.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f))
|
||||
if (!SessionState.running) IconAction(Icons.Outlined.Delete, delete, "addon:delete:${target.flavor}:$name", danger = true) { asking = true }
|
||||
}
|
||||
if (asking) ConfirmDialog(
|
||||
title = stringResource(R.string.games_delete_addon_title, name),
|
||||
text = stringResource(R.string.games_delete_addon_body),
|
||||
confirm = delete,
|
||||
onConfirm = { a.onAddonDelete(target, name) },
|
||||
onDismiss = { asking = false },
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun CloudRows(g: Library.SteamGame) {
|
||||
val store = Store.byId(g.source)?.takeIf { it != Store.AMAZON } ?: return
|
||||
|
||||
Reference in new issue
Block a user