Skip to content

Commit

Permalink
添加代理端资源获取
Browse files Browse the repository at this point in the history
1. 更改配置文档文件名
2. 添加代理端资源获取
3. 修复代理端错误
  • Loading branch information
Taskeren committed Oct 16, 2022
1 parent 7b2c4b6 commit 8052389
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package explode2.booster.graphql

import com.expediagroup.graphql.generator.extensions.print
import explode2.booster.*
import explode2.booster.event.KtorInitEvent
import explode2.booster.BoosterPlugin
import explode2.booster.event.KtorModuleEvent
import explode2.booster.subscribeEvents
import explode2.labyrinth.LabyrinthPlugin
import explode2.logging.Colors
import io.ktor.http.*
Expand All @@ -12,7 +12,6 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -41,10 +40,6 @@ class GraphQLPlugin : BoosterPlugin {
subscribeEvents()
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onKtorInit(ignored: KtorInitEvent) {
}

@Subscribe
fun onKtorModule(e: KtorModuleEvent) {
logger.info("Configuring GraphQL paths in Ktor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package explode2.booster.graphql
import explode2.booster.graphql.definition.ExplodeMutation
import explode2.booster.graphql.definition.ExplodeQuery
import java.util.*
import kotlin.jvm.optionals.getOrNull

interface MazeProvider {

Expand All @@ -15,7 +14,6 @@ interface MazeProvider {
/**
* 返回 [MazeProvider],如果没有其他的,则返回 [BasicMaze] 提供的。
*/
@OptIn(ExperimentalStdlibApi::class)
fun getProvider(): MazeProvider {
return ServiceLoader.load(MazeProvider::class.java).findFirst().orElseGet { BasicMaze }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class GoodBirdOracleTest {
val acc = //0.9626344086021505
GoodBirdOracle.calculateAccuracy(perf, good, miss)

val rank = GoodBirdOracle.calculateRank(acc, 15.2).toInt()
val rank = GoodBirdOracle.calculateRank(acc, 15.2)
require(rank == 551)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import explode2.labyrinth.event.SongCreatedEvent
import org.greenrobot.eventbus.Subscribe
import java.io.File

class FileSystemProvider(private val root: File) : ResourceProvider {
class FileSystemProvider(private val root: File) : SimpleResourceProvider {

init {
root.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import explode2.labyrinth.LabyrinthPlugin
import explode2.logging.Colors
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*
Expand All @@ -20,15 +21,16 @@ internal val logger = LoggerFactory.getLogger("Resource")
class ResourcePlugin : BoosterPlugin {

override val id: String = "resource"
override val version: String = "1.0"
override val version: String = "1.0.0"

override fun onPreInit() {
subscribeEvents()
}

@Subscribe
fun registerKtor(e: KtorModuleEvent) {
val srv = ServiceLoader.load(ResourceProvider::class.java).findFirst().orElseGet { FileSystemProvider(File(".explode_data")) }
val srv = ServiceLoader.load(ResourceProvider::class.java).findFirst()
.orElseGet { FileSystemProvider(File(".explode_data")) }
logger.info("Using Resource: ${Colors.TianYi}${srv.javaClass.canonicalName}")

logger.info("Configuring Ktor")
Expand All @@ -41,28 +43,59 @@ class ResourcePlugin : BoosterPlugin {
route("download") {
get("/music/encoded/{sid}") {
val sid = call.parameters["sid"] ?: return@get invalidResp()
call.respondBytes(srv.getMusic(sid))
val token = call.request.header("x-soudayo")

when(srv) {
is RedirectResourceProvider -> call.respondRedirect(srv.getMusic(sid, token))
is ByteArrayResourceProvider -> call.respondBytes(srv.getMusic(sid, token))
}
}
get("/preview/encoded/{sid}") {
val sid = call.parameters["sid"] ?: return@get invalidResp()
call.respondBytes(srv.getPreviewMusic(sid))
val token = call.request.header("x-soudayo")

when(srv) {
is RedirectResourceProvider -> call.respondRedirect(srv.getPreviewMusic(sid, token))
is ByteArrayResourceProvider -> call.respondBytes(srv.getPreviewMusic(sid, token))
}
}
get("/cover/encoded/{sid}") {
val sid = call.parameters["sid"] ?: return@get invalidResp()
call.respondBytes(srv.getCoverPicture(sid))
val token = call.request.header("x-soudayo")

when(srv) {
is RedirectResourceProvider -> call.respondRedirect(srv.getCoverPicture(sid, token))
is ByteArrayResourceProvider -> call.respondBytes(srv.getCoverPicture(sid, token))
}
}
get("/chart/encoded/{cid}") {
val cid = call.parameters["cid"] ?: return@get invalidResp()
val sid = LabyrinthPlugin.labyrinth.songSetFactory.getSongSetByChart(cid)?.id ?: return@get invalidResp("Invalid Binding Set")
call.respondBytes(srv.getChartXML(cid, sid))
val sid = LabyrinthPlugin.labyrinth.songSetFactory.getSongSetByChart(cid)?.id
?: return@get invalidResp("Invalid binding set")
val token = call.request.header("x-soudayo")

when(srv) {
is RedirectResourceProvider -> call.respondRedirect(srv.getChartXML(cid, sid, token))
is ByteArrayResourceProvider -> call.respondBytes(srv.getChartXML(cid, sid, token))
}
}
get("/cover/480x270_jpg/{sid}") {
val sid = call.parameters["sid"] ?: return@get invalidResp()
call.respondBytes(srv.getStoreCoverPicture(sid))
val token = call.request.header("x-soudayo")

when(srv) {
is RedirectResourceProvider -> call.respondRedirect(srv.getStoreCoverPicture(sid, token))
is ByteArrayResourceProvider -> call.respondBytes(srv.getStoreCoverPicture(sid, token))
}
}
get("/avatar/256x256_jpg/{uid}") {
val uid = call.parameters["uid"] ?: return@get invalidResp()
call.respondBytes(srv.getUserAvatar(uid))
val token = call.request.header("x-soudayo")

when(srv) {
is RedirectResourceProvider -> call.respondRedirect(srv.getUserAvatar(uid, token))
is ByteArrayResourceProvider -> call.respondBytes(srv.getUserAvatar(uid, token))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
package explode2.booster.resource

interface ResourceProvider {
sealed interface ResourceProvider {
fun getMusic(id: String, token: String?): Any
fun getPreviewMusic(id: String, token: String?): Any
fun getCoverPicture(id: String, token: String?): Any
fun getStoreCoverPicture(id: String, token: String?): Any
fun getChartXML(cid: String, sid: String, token: String?): Any
fun getUserAvatar(id: String, token: String?): Any
}

interface ByteArrayResourceProvider : ResourceProvider {
override fun getMusic(id: String, token: String?): ByteArray
override fun getPreviewMusic(id: String, token: String?): ByteArray
override fun getCoverPicture(id: String, token: String?): ByteArray
override fun getStoreCoverPicture(id: String, token: String?): ByteArray
override fun getChartXML(cid: String, sid: String, token: String?): ByteArray
override fun getUserAvatar(id: String, token: String?): ByteArray
}

interface RedirectResourceProvider : ResourceProvider {
override fun getMusic(id: String, token: String?): String
override fun getPreviewMusic(id: String, token: String?): String
override fun getCoverPicture(id: String, token: String?): String
override fun getStoreCoverPicture(id: String, token: String?): String
override fun getChartXML(cid: String, sid: String, token: String?): String
override fun getUserAvatar(id: String, token: String?): String
}

interface SimpleResourceProvider : ByteArrayResourceProvider {

fun getMusic(id: String): ByteArray
fun getPreviewMusic(id: String): ByteArray
fun getCoverPicture(id: String): ByteArray
fun getStoreCoverPicture(id: String): ByteArray
fun getChartXML(cid: String, sid: String): ByteArray
fun getUserAvatar(id: String): ByteArray

override fun getMusic(id: String, token: String?): ByteArray = getMusic(id)
override fun getPreviewMusic(id: String, token: String?): ByteArray = getPreviewMusic(id)
override fun getCoverPicture(id: String, token: String?): ByteArray = getCoverPicture(id)
override fun getStoreCoverPicture(id: String, token: String?): ByteArray = getStoreCoverPicture(id)
override fun getChartXML(cid: String, sid: String, token: String?): ByteArray = getChartXML(cid, sid)
override fun getUserAvatar(id: String, token: String?): ByteArray = getUserAvatar(id)
}
2 changes: 1 addition & 1 deletion booster/src/main/kotlin/explode2/booster/Booster.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.util.logging.Level

object Booster {

val config = Configuration(File("explode.config.toml"))
val config = Configuration(File("explode.cfg"))

val eventbus: EventBus = EventBus.builder()
.logger(object : org.greenrobot.eventbus.Logger {
Expand Down
2 changes: 1 addition & 1 deletion booster/src/main/kotlin/explode2/booster/BoosterPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun <T> T.subscribeEvents() {
}

val BoosterPlugin.config: Configuration
get() = Configuration(File("$id.config.toml"))
get() = Configuration(File("$id.cfg"))

@Suppress("UNCHECKED_CAST")
val <T: BoosterPlugin> Class<T>.instance get(): T? =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package explode2.booster.util

import java.util.stream.Stream

fun <T> Iterable<T>.forEachExceptional(block: (T) -> Unit, exceptional: (T, exception: Throwable) -> Unit) {
this.forEach { el ->
el.runCatching(block).onFailure { exceptional(el, it) }
}
}

fun <T, R> Iterable<T>.mapExceptional(block: (T) -> R, exceptional: (T, exception: Throwable) -> Unit): List<R> {
val wrappedBlock = { el: T -> el.runCatching(block).onFailure { exceptional(el, it) }.getOrNull() }
return this.mapNotNull(wrappedBlock)
}
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
java
kotlin("jvm") version "1.7.20"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package explode2.booster.graphql.proxy

import explode2.gateau.*
import explode2.labyrinth.*
import java.time.OffsetDateTime

/**
* 用于代理服务器的 LabyrinthProvider,
Expand All @@ -9,15 +11,90 @@ import explode2.labyrinth.*
class NothingLabyrinthProvider : LabyrinthProvider {

override val gameUserFactory: GameUserFactory
get() = TODO("Not yet implemented")
get() = getAndError()
override val songSetFactory: SongSetFactory
get() = TODO("Not yet implemented")
get() = object : SongSetFactory {
override fun getSongSetById(id: String): SongSet? {
getAndError()
}

override fun getSongSetByMusic(musicName: String): SongSet? {
getAndError()
}

override fun getSongSetByChart(chartId: String): SongSet {
return object : SongSet {
override val id: String
get() = ""
override var musicName: String
get() = getAndError()
set(_) {}
override var musicComposer: String
get() = getAndError()
set(_) {}
override var introduction: String
get() = getAndError()
set(_) {}
override var coinPrice: Int
get() = getAndError()
set(_) {}
override var noterName: String
get() = getAndError()
set(_) {}
override var noterUserId: String?
get() = getAndError()
set(_) {}
override var chartIds: List<String>
get() = getAndError()
set(_) {}
override val charts: List<SongChart>
get() = getAndError()
override val playCount: Int
get() = getAndError()
override val publishTime: OffsetDateTime
get() = getAndError()
override val state: SongState
get() = getAndError()

override fun isUserGot(user: GameUser): Boolean {
getAndError()
}
}
}

override fun createSongSet(
musicName: String,
musicComposer: String,
introduction: String,
noterName: String,
charts: List<String>,
state: SongState,
id: String?,
coinPrice: Int,
publishTime: OffsetDateTime,
noterUser: GameUser?
): SongSet {
getAndError()
}

override fun searchSongSets(
matchingName: String?,
matchingCategory: SearchCategory?,
sortBy: SearchSort?,
limit: Int,
skip: Int
): Collection<SongSet> {
getAndError()
}
}
override val songChartFactory: SongChartFactory
get() = TODO("Not yet implemented")
get() = getAndError()
override val assessmentInfoFactory: AssessmentInfoFactory
get() = TODO("Not yet implemented")
get() = getAndError()
override val gameRecordFactory: GameRecordFactory
get() = TODO("Not yet implemented")
get() = getAndError()
override val assessmentRecordFactory: AssessmentRecordFactory
get() = TODO("Not yet implemented")
get() = getAndError()

private fun getAndError(): Nothing = error("NothingLabyrinthProvider provides nothing, plugins should never try getting the factories!")
}
Loading

0 comments on commit 8052389

Please sign in to comment.