Skip to content

Commit

Permalink
chore: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
noxzym committed May 26, 2022
1 parent 3673cbe commit 703df26
Show file tree
Hide file tree
Showing 49 changed files with 1,841 additions and 1,842 deletions.
109 changes: 52 additions & 57 deletions src/main/kotlin/lavalink/server/Launcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
package lavalink.server

import com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary
import lavalink.server.info.AppInfo
import lavalink.server.info.GitRepoState
import org.slf4j.LoggerFactory
import org.springframework.boot.Banner
import org.springframework.boot.SpringApplication
Expand All @@ -34,77 +32,74 @@ import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
import org.springframework.boot.context.event.ApplicationFailedEvent
import org.springframework.context.ApplicationListener
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter

@SpringBootApplication
class LavalinkApplication

object Launcher {

private val log = LoggerFactory.getLogger(Launcher::class.java)
private val log = LoggerFactory.getLogger(Launcher::class.java)

val startTime = System.currentTimeMillis()
val startTime = System.currentTimeMillis()

private fun getVersionInfo(indentation: String = "\t", vanity: Boolean = true): String {
val version = "CUSTOM"
val buildNumber = "Unofficial"
private fun getVersionInfo(indentation: String = "\t", vanity: Boolean = true): String {
val version = "CUSTOM"
val buildNumber = "Unofficial"

return buildString {
if (vanity) {
appendLine()
appendLine()
appendLine(getVanity())
}
return buildString {
if (vanity) {
appendLine()
appendLine()
appendLine(getVanity())
}

appendLine()
append("${indentation}Version: "); appendLine(version)
append("${indentation}Build: "); appendLine(buildNumber)
append("${indentation}Kotlin: "); appendLine(KotlinVersion.CURRENT)
append("${indentation}JVM: "); appendLine(System.getProperty("java.version"))
append("${indentation}Lavaplayer: "); appendLine(PlayerLibrary.VERSION)
appendLine()
append("${indentation}Version: "); appendLine(version)
append("${indentation}Build: "); appendLine(buildNumber)
append("${indentation}Kotlin: "); appendLine(KotlinVersion.CURRENT)
append("${indentation}JVM: "); appendLine(System.getProperty("java.version"))
append("${indentation}Lavaplayer: "); appendLine(PlayerLibrary.VERSION)
}
}
}

private fun getVanity(): String {
//ansi color codes
val red = ""
val green = ""
val defaultC = ""

var vanity = ("g . r _ _ _ _ g__ _ _\n"
+ "g /\\\\ r| | __ ___ ____ _| (_)_ __ | | __g\\ \\ \\ \\\n"
+ "g ( ( )r| |/ _` \\ \\ / / _` | | | '_ \\| |/ /g \\ \\ \\ \\\n"
+ "g \\\\/ r| | (_| |\\ V / (_| | | | | | | < g ) ) ) )\n"
+ "g ' r|_|\\__,_| \\_/ \\__,_|_|_|_| |_|_|\\_\\g / / / /\n"
+ "d =========================================g/_/_/_/d")
private fun getVanity(): String {
//ansi color codes
val red = ""
val green = ""
val defaultC = ""

vanity = vanity.replace("r".toRegex(), red)
vanity = vanity.replace("g".toRegex(), green)
vanity = vanity.replace("d".toRegex(), defaultC)
return vanity
}
var vanity = ("g . r _ _ _ _ g__ _ _\n"
+ "g /\\\\ r| | __ ___ ____ _| (_)_ __ | | __g\\ \\ \\ \\\n"
+ "g ( ( )r| |/ _` \\ \\ / / _` | | | '_ \\| |/ /g \\ \\ \\ \\\n"
+ "g \\\\/ r| | (_| |\\ V / (_| | | | | | | < g ) ) ) )\n"
+ "g ' r|_|\\__,_| \\_/ \\__,_|_|_|_| |_|_|\\_\\g / / / /\n"
+ "d =========================================g/_/_/_/d")

@JvmStatic
fun main(args: Array<String>) {
when (args.firstOrNull()?.lowercase()) {
"-v", "--version" -> return println(getVersionInfo(indentation = "", vanity = false))
vanity = vanity.replace("r".toRegex(), red)
vanity = vanity.replace("g".toRegex(), green)
vanity = vanity.replace("d".toRegex(), defaultC)
return vanity
}

/* start the spring application */
val sa = SpringApplication(LavalinkApplication::class.java)
sa.webApplicationType = WebApplicationType.SERVLET
sa.setBannerMode(Banner.Mode.OFF) // We have our own
sa.addListeners(
ApplicationListener { event: Any ->
when (event) {
is ApplicationEnvironmentPreparedEvent -> log.info(getVersionInfo())
is ApplicationFailedEvent -> log.error("Application failed", event.exception)
@JvmStatic
fun main(args: Array<String>) {
when (args.firstOrNull()?.lowercase()) {
"-v", "--version" -> return println(getVersionInfo(indentation = "", vanity = false))
}
}
)

sa.run(*args)
}
/* start the spring application */
val sa = SpringApplication(LavalinkApplication::class.java)
sa.webApplicationType = WebApplicationType.SERVLET
sa.setBannerMode(Banner.Mode.OFF) // We have our own
sa.addListeners(
ApplicationListener { event: Any ->
when (event) {
is ApplicationEnvironmentPreparedEvent -> log.info(getVersionInfo())
is ApplicationFailedEvent -> log.error("Application failed", event.exception)
}
}
)

sa.run(*args)
}
}
227 changes: 114 additions & 113 deletions src/main/kotlin/lavalink/server/config/AudioPlayerConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,128 +57,129 @@ import java.util.function.Predicate
@Configuration
class AudioPlayerConfiguration {

private val log = LoggerFactory.getLogger(AudioPlayerConfiguration::class.java)

@Bean
fun audioPlayerManagerSupplier(
sources: AudioSourcesConfig,
lavaplayerProps: LavaplayerConfigProperties,
routePlanner: AbstractRoutePlanner?
): AudioPlayerManager {
val audioPlayerManager = DefaultAudioPlayerManager()

if (lavaplayerProps.isGcWarnings) {
audioPlayerManager.enableGcMonitoring()
}

if (sources.youtube) {
val youtube = YoutubeAudioSourceManager(
lavaplayerProps.isYoutubeSearchEnabled,
lavaplayerProps.youtubeConfig?.email,
lavaplayerProps.youtubeConfig?.password
)
if (routePlanner != null) {
val retryLimit = lavaplayerProps.ratelimit?.retryLimit ?: -1
when {
retryLimit < 0 -> YoutubeIpRotatorSetup(routePlanner).forSource(youtube).setup()
retryLimit == 0 -> YoutubeIpRotatorSetup(routePlanner).forSource(youtube).withRetryLimit(Int.MAX_VALUE)
.setup()
else -> YoutubeIpRotatorSetup(routePlanner).forSource(youtube).withRetryLimit(retryLimit).setup()
private val log = LoggerFactory.getLogger(AudioPlayerConfiguration::class.java)

@Bean
fun audioPlayerManagerSupplier(
sources: AudioSourcesConfig,
lavaplayerProps: LavaplayerConfigProperties,
routePlanner: AbstractRoutePlanner?
): AudioPlayerManager {
val audioPlayerManager = DefaultAudioPlayerManager()

if (lavaplayerProps.isGcWarnings) {
audioPlayerManager.enableGcMonitoring()
}
}

val playlistLoadLimit = lavaplayerProps.youtubePlaylistLoadLimit
if (playlistLoadLimit != null) {
youtube.setPlaylistPageCount(playlistLoadLimit)
}

audioPlayerManager.registerSourceManager(youtube)
}
if (sources.soundcloud) audioPlayerManager.registerSourceManager(
SoundCloudAudioSourceManager.createDefault(
lavaplayerProps.isSoundcloudSearchEnabled
)
)
if (sources.bandcamp) audioPlayerManager.registerSourceManager(BandcampAudioSourceManager())
if (sources.tiktok) audioPlayerManager.registerSourceManager(TiktokAudioSourceManager())
if (sources.twitch) audioPlayerManager.registerSourceManager(TwitchStreamAudioSourceManager())
if (sources.vimeo) audioPlayerManager.registerSourceManager(VimeoAudioSourceManager())
if (sources.reddit) audioPlayerManager.registerSourceManager(RedditAudioSourceManager())
if (sources.rumble) audioPlayerManager.registerSourceManager(RumbleAudioSourceManager())
if (sources.odysee) audioPlayerManager.registerSourceManager(OdyseeAudioSourceManager(lavaplayerProps.isOdyseeSearchEnabled))
if (sources.yandex) {
audioPlayerManager.registerSourceManager(YandexMusicAudioSourceManager(lavaplayerProps.isYandexMusicSearchEnabled))
YandexHttpContextFilter.setOAuthToken(lavaplayerProps.yandexOAuthToken)
}
if (sources.getyarn) audioPlayerManager.registerSourceManager(GetyarnAudioSourceManager())
if (sources.spotify) {
if (lavaplayerProps.spotifyConfig?.spotifyUsername == null) {
log.warn("Skipping spotify source. spotifyUsername can't null or empty")
}
if (lavaplayerProps.spotifyConfig?.spotifyPassword == null) {
log.warn("Skipping spotify source. spotifyPassword can't null or empty")
}
if (lavaplayerProps.spotifyConfig?.spotifyUsername !== null && lavaplayerProps.spotifyConfig?.spotifyPassword !== null) {
if (lavaplayerProps.spotifyPlaylistLoadLimit == null) {
lavaplayerProps.spotifyPlaylistLoadLimit = 0
}
if (lavaplayerProps.spotifyConfig?.audioQuality == null) {
lavaplayerProps.spotifyConfig?.audioQuality = "NORMAL"
if (sources.youtube) {
val youtube = YoutubeAudioSourceManager(
lavaplayerProps.isYoutubeSearchEnabled,
lavaplayerProps.youtubeConfig?.email,
lavaplayerProps.youtubeConfig?.password
)
if (routePlanner != null) {
val retryLimit = lavaplayerProps.ratelimit?.retryLimit ?: -1
when {
retryLimit < 0 -> YoutubeIpRotatorSetup(routePlanner).forSource(youtube).setup()
retryLimit == 0 -> YoutubeIpRotatorSetup(routePlanner).forSource(youtube)
.withRetryLimit(Int.MAX_VALUE)
.setup()
else -> YoutubeIpRotatorSetup(routePlanner).forSource(youtube).withRetryLimit(retryLimit).setup()
}
}

val playlistLoadLimit = lavaplayerProps.youtubePlaylistLoadLimit
if (playlistLoadLimit != null) {
youtube.setPlaylistPageCount(playlistLoadLimit)
}

audioPlayerManager.registerSourceManager(youtube)
}
audioPlayerManager
.registerSourceManager(
SpotifyAudioSourceManager(
lavaplayerProps.spotifyConfig,
lavaplayerProps.spotifyPlaylistLoadLimit!!,
lavaplayerProps.isSpotifySearchEnabled,
if (sources.soundcloud) audioPlayerManager.registerSourceManager(
SoundCloudAudioSourceManager.createDefault(
lavaplayerProps.isSoundcloudSearchEnabled
)
)
}
}
if (sources.http) audioPlayerManager.registerSourceManager(HttpAudioSourceManager())
if (sources.local) audioPlayerManager.registerSourceManager(LocalAudioSourceManager())

audioPlayerManager.configuration.isFilterHotSwapEnabled = true
audioPlayerManager.frameBufferDuration = lavaplayerProps.frameBufferDuration
if (lavaplayerProps.nonAllocating) {
log.info("Using the non-allocating frame buffer.")
audioPlayerManager.configuration.setFrameBufferFactory(::NonAllocatingAudioFrameBuffer)
}

return audioPlayerManager
}
)
if (sources.bandcamp) audioPlayerManager.registerSourceManager(BandcampAudioSourceManager())
if (sources.tiktok) audioPlayerManager.registerSourceManager(TiktokAudioSourceManager())
if (sources.twitch) audioPlayerManager.registerSourceManager(TwitchStreamAudioSourceManager())
if (sources.vimeo) audioPlayerManager.registerSourceManager(VimeoAudioSourceManager())
if (sources.reddit) audioPlayerManager.registerSourceManager(RedditAudioSourceManager())
if (sources.rumble) audioPlayerManager.registerSourceManager(RumbleAudioSourceManager())
if (sources.odysee) audioPlayerManager.registerSourceManager(OdyseeAudioSourceManager(lavaplayerProps.isOdyseeSearchEnabled))
if (sources.yandex) {
audioPlayerManager.registerSourceManager(YandexMusicAudioSourceManager(lavaplayerProps.isYandexMusicSearchEnabled))
YandexHttpContextFilter.setOAuthToken(lavaplayerProps.yandexOAuthToken)
}
if (sources.getyarn) audioPlayerManager.registerSourceManager(GetyarnAudioSourceManager())
if (sources.spotify) {
if (lavaplayerProps.spotifyConfig?.spotifyUsername == null) {
log.warn("Skipping spotify source. spotifyUsername can't null or empty")
}
if (lavaplayerProps.spotifyConfig?.spotifyPassword == null) {
log.warn("Skipping spotify source. spotifyPassword can't null or empty")
}
if (lavaplayerProps.spotifyConfig?.spotifyUsername !== null && lavaplayerProps.spotifyConfig?.spotifyPassword !== null) {
if (lavaplayerProps.spotifyPlaylistLoadLimit == null) {
lavaplayerProps.spotifyPlaylistLoadLimit = 0
}
if (lavaplayerProps.spotifyConfig?.audioQuality == null) {
lavaplayerProps.spotifyConfig?.audioQuality = "NORMAL"
}
audioPlayerManager
.registerSourceManager(
SpotifyAudioSourceManager(
lavaplayerProps.spotifyConfig,
lavaplayerProps.spotifyPlaylistLoadLimit!!,
lavaplayerProps.isSpotifySearchEnabled,
)
)
}
}
if (sources.http) audioPlayerManager.registerSourceManager(HttpAudioSourceManager())
if (sources.local) audioPlayerManager.registerSourceManager(LocalAudioSourceManager())

audioPlayerManager.configuration.isFilterHotSwapEnabled = true
audioPlayerManager.frameBufferDuration = lavaplayerProps.frameBufferDuration
if (lavaplayerProps.nonAllocating) {
log.info("Using the non-allocating frame buffer.")
audioPlayerManager.configuration.setFrameBufferFactory(::NonAllocatingAudioFrameBuffer)
}

@Bean
fun routePlanner(lavaplayerProps: LavaplayerConfigProperties): AbstractRoutePlanner? {
val rateLimitConfig = lavaplayerProps.ratelimit
if (rateLimitConfig == null) {
log.debug("No rate limit config block found, skipping setup of route planner")
return null
return audioPlayerManager
}

val ipBlockList = rateLimitConfig.ipBlocks
if (ipBlockList.isEmpty()) {
log.info("List of ip blocks is empty, skipping setup of route planner")
return null
}
@Bean
fun routePlanner(lavaplayerProps: LavaplayerConfigProperties): AbstractRoutePlanner? {
val rateLimitConfig = lavaplayerProps.ratelimit
if (rateLimitConfig == null) {
log.debug("No rate limit config block found, skipping setup of route planner")
return null
}

val blacklisted = rateLimitConfig.excludedIps.map { InetAddress.getByName(it) }
val filter = Predicate<InetAddress> { !blacklisted.contains(it) }
val ipBlocks = ipBlockList.map {
when {
Ipv4Block.isIpv4CidrBlock(it) -> Ipv4Block(it)
Ipv6Block.isIpv6CidrBlock(it) -> Ipv6Block(it)
else -> throw RuntimeException("Invalid IP Block '$it', make sure to provide a valid CIDR notation")
}
}
val ipBlockList = rateLimitConfig.ipBlocks
if (ipBlockList.isEmpty()) {
log.info("List of ip blocks is empty, skipping setup of route planner")
return null
}

return when (rateLimitConfig.strategy.lowercase().trim()) {
"rotateonban" -> RotatingIpRoutePlanner(ipBlocks, filter, rateLimitConfig.searchTriggersFail)
"loadbalance" -> BalancingIpRoutePlanner(ipBlocks, filter, rateLimitConfig.searchTriggersFail)
"nanoswitch" -> NanoIpRoutePlanner(ipBlocks, rateLimitConfig.searchTriggersFail)
"rotatingnanoswitch" -> RotatingNanoIpRoutePlanner(ipBlocks, filter, rateLimitConfig.searchTriggersFail)
else -> throw RuntimeException("Unknown strategy!")
val blacklisted = rateLimitConfig.excludedIps.map { InetAddress.getByName(it) }
val filter = Predicate<InetAddress> { !blacklisted.contains(it) }
val ipBlocks = ipBlockList.map {
when {
Ipv4Block.isIpv4CidrBlock(it) -> Ipv4Block(it)
Ipv6Block.isIpv6CidrBlock(it) -> Ipv6Block(it)
else -> throw RuntimeException("Invalid IP Block '$it', make sure to provide a valid CIDR notation")
}
}

return when (rateLimitConfig.strategy.lowercase().trim()) {
"rotateonban" -> RotatingIpRoutePlanner(ipBlocks, filter, rateLimitConfig.searchTriggersFail)
"loadbalance" -> BalancingIpRoutePlanner(ipBlocks, filter, rateLimitConfig.searchTriggersFail)
"nanoswitch" -> NanoIpRoutePlanner(ipBlocks, rateLimitConfig.searchTriggersFail)
"rotatingnanoswitch" -> RotatingNanoIpRoutePlanner(ipBlocks, filter, rateLimitConfig.searchTriggersFail)
else -> throw RuntimeException("Unknown strategy!")
}
}
}

}
Loading

0 comments on commit 703df26

Please sign in to comment.