Skip to content

Commit

Permalink
Support Linux platform (VLC not yet added) for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Sep 14, 2024
1 parent 4ba30dc commit 19c3069
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 37 deletions.
6 changes: 3 additions & 3 deletions app/desktop/src/main/kotlin/AniDesktop.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 OpenAni and contributors.
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
Expand Down Expand Up @@ -92,9 +92,9 @@ import me.him188.ani.utils.io.toKtPath
import me.him188.ani.utils.logging.error
import me.him188.ani.utils.logging.info
import me.him188.ani.utils.logging.logger
import me.him188.ani.utils.platform.Platform
import me.him188.ani.utils.platform.currentPlatform
import me.him188.ani.utils.platform.currentPlatformDesktop
import me.him188.ani.utils.platform.isMacOS
import org.jetbrains.compose.resources.painterResource
import org.koin.core.context.startKoin
import org.koin.dsl.module
Expand Down Expand Up @@ -284,7 +284,7 @@ object AniDesktop {

SideEffect {
// https://www.formdev.com/flatlaf/macos/
if (currentPlatformDesktop() is Platform.MacOS) {
if (currentPlatformDesktop().isMacOS()) {
window.rootPane.putClientProperty("apple.awt.application.appearance", "system")
window.rootPane.putClientProperty("apple.awt.fullscreenable", true)
if (windowImmersed) {
Expand Down
17 changes: 14 additions & 3 deletions app/desktop/src/main/kotlin/LocalDiscoveryDirectoryProvider.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.desktop

import me.him188.ani.utils.platform.Arch
Expand All @@ -12,17 +21,19 @@ class TestDiscoveryDirectoryProvider : DiscoveryDirectoryProvider {
override fun priority(): Int = DiscoveryProviderPriority.USER_DIR

override fun directories(): Array<String> {
val os = when (currentPlatformDesktop()) {
val platform = currentPlatformDesktop()
val os = when (platform) {
is Platform.MacOS -> "macos"
is Platform.Windows -> "windows"
is Platform.Linux -> "linux"
}

val arch = when (currentPlatformDesktop().arch) {
val arch = when (platform.arch) {
Arch.X86_64 -> "x64"
Arch.AARCH64 -> "arm64"

Arch.ARMV7A, Arch.ARMV8A ->
throw UnsupportedOperationException("Unsupported architecture: ${currentPlatformDesktop().arch}")
throw UnsupportedOperationException("Unsupported architecture: ${platform.arch}")
}

val libs = File(System.getProperty("user.dir")).resolve("../appResources/${os}-${arch}/lib")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.data.source.media.selector

import me.him188.ani.app.data.source.media.selector.SubtitleKindPreference.HIDE
Expand Down Expand Up @@ -40,6 +49,7 @@ value class MediaSelectorSubtitlePreferences(
fun forPlatform(platform: Platform = currentPlatform()): MediaSelectorSubtitlePreferences {
// 对于缺陷列表, 查看 https://github.com/open-ani/ani/issues/615
val map = when (platform) {
is Platform.Linux, // TODO: check linux MediaSelectorSubtitlePreferences
is Platform.MacOS -> ImmutableEnumMap<SubtitleKind, _> {
when (it) {
SubtitleKind.EMBEDDED -> NORMAL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.data.source.media.resolver

import io.github.bonigarcia.wdm.WebDriverManager
Expand All @@ -11,16 +20,15 @@ import me.him188.ani.app.data.models.preference.ProxyConfig
import me.him188.ani.app.data.models.preference.VideoResolverSettings
import me.him188.ani.app.data.models.preference.WebViewDriver
import me.him188.ani.app.data.repository.SettingsRepository
import me.him188.ani.app.videoplayer.data.VideoSource
import me.him188.ani.app.videoplayer.HttpStreamingVideoSource
import me.him188.ani.app.videoplayer.data.VideoSource
import me.him188.ani.datasources.api.Media
import me.him188.ani.datasources.api.matcher.WebVideoMatcher
import me.him188.ani.datasources.api.matcher.WebVideoMatcherContext
import me.him188.ani.datasources.api.topic.ResourceLocation
import me.him188.ani.utils.logging.error
import me.him188.ani.utils.logging.info
import me.him188.ani.utils.logging.logger
import me.him188.ani.utils.platform.Platform
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.openqa.selenium.WebDriver
Expand Down Expand Up @@ -157,31 +165,29 @@ class SeleniumWebViewVideoExtractor(
logger.info { "Starting Selenium with Edge to resolve video source from $pageUrl" }


val driver: RemoteWebDriver = when (me.him188.ani.utils.platform.currentPlatformDesktop()) {
is Platform.MacOS, is Platform.Windows -> {
val primaryDriverFunction = mapWebViewDriverToFunction(videoResolverSettings.driver)
val fallbackDriverFunctions = getFallbackDriverFunctions(primaryDriverFunction)
val driver: RemoteWebDriver = kotlin.run {
val primaryDriverFunction = mapWebViewDriverToFunction(videoResolverSettings.driver)
val fallbackDriverFunctions = getFallbackDriverFunctions(primaryDriverFunction)

// Try user-set ones first, then fallback on the others
val driverCreationFunctions = listOfNotNull(primaryDriverFunction) + fallbackDriverFunctions
var successfulDriver: (() -> RemoteWebDriver)? = null
// Try user-set ones first, then fallback on the others
val driverCreationFunctions = listOfNotNull(primaryDriverFunction) + fallbackDriverFunctions
var successfulDriver: (() -> RemoteWebDriver)? = null

val driver = driverCreationFunctions
.asSequence()
.mapNotNull { func ->
runCatching {
func().also { successfulDriver = func }
}.getOrNull()
}
.firstOrNull()
?: throw Exception("Failed to create a driver")
val driver = driverCreationFunctions
.asSequence()
.mapNotNull { func ->
runCatching {
func().also { successfulDriver = func }
}.getOrNull()
}
.firstOrNull()
?: throw Exception("Failed to create a driver")

// If the rollback is successful, update the user settings
// Except Safari for now, because it does not support proxy settings and is not listed in the optional list
// updateDriverSettingsIfNeeded(successfulDriver)
// If the rollback is successful, update the user settings
// Except Safari for now, because it does not support proxy settings and is not listed in the optional list
// updateDriverSettingsIfNeeded(successfulDriver)

driver
}
driver
}

logger.info { "Using WebDriver: $driver" }
Expand Down Expand Up @@ -272,15 +278,15 @@ class SeleniumWebViewVideoExtractor(
else -> null
}
}

private fun getFallbackDriverFunctions(primaryDriverFunction: (() -> RemoteWebDriver)?): List<() -> RemoteWebDriver> {
return listOf(
::createChromeDriver,
::createEdgeDriver,
// ::createSafariDriver,
).filter { it != primaryDriverFunction }
}

// private fun updateDriverSettingsIfNeeded(successfulDriver: (() -> RemoteWebDriver)?, primaryDriverFunction: (() -> RemoteWebDriver)?) {
// if (successfulDriver != primaryDriverFunction) {
// val fallbackDriverType = when (successfulDriver) {
Expand Down
10 changes: 10 additions & 0 deletions app/shared/src/desktopMain/kotlin/desktop/ScreenUtils.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.desktop

import androidx.compose.ui.unit.Density
Expand Down Expand Up @@ -25,6 +34,7 @@ object ScreenUtils {
val dimension: Dimension = Toolkit.getDefaultToolkit().screenSize

return when (me.him188.ani.utils.platform.currentPlatformDesktop()) {
is Platform.Linux, // TODO: 检查 linux 的 getScreenSize
is Platform.MacOS -> {
// macos dimension 是经过缩放的

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.tools.update

import me.him188.ani.app.platform.ContextMP
Expand All @@ -17,14 +26,15 @@ interface DesktopUpdateInstaller : UpdateInstaller {
override fun openForManualInstallation(file: SystemPath, context: ContextMP) {
FileOpener.openInFileBrowser(file)
}

fun deleteOldUpdater()

companion object {
fun currentOS(): DesktopUpdateInstaller {
return when (me.him188.ani.utils.platform.currentPlatformDesktop()) {
is Platform.MacOS -> MacOSUpdateInstaller
is Platform.Windows -> WindowsUpdateInstaller
is Platform.Linux -> LinuxUpdateInstaller
}
}
}
Expand All @@ -41,6 +51,17 @@ object MacOSUpdateInstaller : DesktopUpdateInstaller {
}
}

object LinuxUpdateInstaller : DesktopUpdateInstaller {
override fun deleteOldUpdater() {
// no-op
}

override fun install(file: SystemPath, context: ContextMP): InstallationResult {
Desktop.getDesktop().open(file.toFile())
exitProcess(0)
}
}

object WindowsUpdateInstaller : DesktopUpdateInstaller {
private val logger = logger<WindowsUpdateInstaller>()

Expand All @@ -53,7 +74,10 @@ object WindowsUpdateInstaller : DesktopUpdateInstaller {
return InstallationResult.Failed(InstallationFailureReason.UNSUPPORTED_FILE_STRUCTURE)
}

val resourcesDir = File(System.getProperty("compose.application.resources.dir") ?: throw IllegalStateException("Cannot get resources directory"))
val resourcesDir = File(
System.getProperty("compose.application.resources.dir")
?: throw IllegalStateException("Cannot get resources directory"),
)
val updateExecutable = resourcesDir.resolve("ani_update.exe")
if (!updateExecutable.exists()) {
logger.info { "'ani_update.exe' not found. Fallback to manual update" }
Expand All @@ -63,9 +87,11 @@ object WindowsUpdateInstaller : DesktopUpdateInstaller {
// Copy ani_update.exe to current dir
val copiedUpdateExecutable = appDir.resolve("ani_update.exe")
updateExecutable.copyTo(copiedUpdateExecutable, true)

ProcessBuilder("cmd", "/c", "start", "cmd", "/c",
"\"", copiedUpdateExecutable.absolutePath, file.absolutePath, appDir.absolutePath, "\"")

ProcessBuilder(
"cmd", "/c", "start", "cmd", "/c",
"\"", copiedUpdateExecutable.absolutePath, file.absolutePath, appDir.absolutePath, "\"",
)
.directory(appDir)
.start()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.platform.window

class LinuxWindowUtils : AwtWindowUtils()
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.platform.window

import androidx.compose.ui.awt.ComposeWindow
Expand Down Expand Up @@ -32,6 +41,7 @@ interface WindowUtils {
companion object : WindowUtils by (when (me.him188.ani.utils.platform.currentPlatformDesktop()) {
is Platform.MacOS -> MacosWindowUtils()
is Platform.Windows -> WindowsWindowUtils()
is Platform.Linux -> LinuxWindowUtils()
})
}

Expand Down
10 changes: 10 additions & 0 deletions torrent/anitorrent/src/jvmMain/kotlin/AnitorrentLibraryLoader.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.torrent.anitorrent

import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -50,6 +59,7 @@ object AnitorrentLibraryLoader : TorrentLibraryLoader {
val triple = when (platform) {
is Platform.MacOS -> "macos-$arch"
is Platform.Windows -> "windows-$arch"
is Platform.Linux -> "linux-$arch"
}

System.getProperty("user.dir")?.let { File(it) }?.resolve("../appResources/$triple")
Expand Down
18 changes: 17 additions & 1 deletion utils/platform/src/commonMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

@file:Suppress("NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress")

package me.him188.ani.utils.platform
Expand Down Expand Up @@ -45,7 +54,9 @@ sealed class Platform {
override val arch: Arch
) : Desktop("macOS")


data class Linux(
override val arch: Arch
) : Desktop("Linux")
}


Expand Down Expand Up @@ -110,6 +121,11 @@ inline fun Platform.isDesktop(): Boolean {
return this is Platform.Desktop
}

inline fun Platform.isMacOS(): Boolean {
contract { returns(true) implies (this@isMacOS is Platform.MacOS) }
return this is Platform.MacOS
}

inline fun Platform.isIos(): Boolean {
contract { returns(true) implies (this@isIos is Platform.Ios) }
return this is Platform.Ios
Expand Down

0 comments on commit 19c3069

Please sign in to comment.