Skip to content

Commit

Permalink
resolveDownloadSrc
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 19, 2023
1 parent 1eeeb8b commit d1908ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 79 deletions.
92 changes: 16 additions & 76 deletions main/src/run/model/Games.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ package io.github.liplum.mindustry
import arc.util.serialization.Jval
import org.gradle.api.GradleException
import java.io.File
import java.io.InputStream
import java.io.Serializable
import java.net.URL

/**
* An abstract Mindustry game file.
*/
interface IGameLoc : Serializable {
sealed interface IGameLoc : Serializable {
val fileName4Local: String
fun resolveCacheFile(): File
}

sealed interface IDownloadableGameLoc : IGameLoc {
/**
* Generate an [IDownloadLoc] deterministically.
* Generate a [URL] deterministically.
*/
fun createDownloadLoc(): IDownloadLoc

fun resolveCacheFile(): File
fun resolveDownloadSrc(): URL
}

data class GitHubGameLoc(
val user: String,
val repo: String,
val tag: String,
val file: String,
) : IGameLoc {
val download = GitHubDownload.release(user, repo, tag, file)
override val fileName4Local = "$user-$repo-$tag-${download.name}"
override fun createDownloadLoc() = download
) : IDownloadableGameLoc {
override val fileName4Local = "$user-$repo-$tag-${file}"
override fun resolveDownloadSrc() = URL("https://github.com/$user/$repo/releases/download/$tag/$file")
override fun resolveCacheFile(): File {
return SharedCache.gamesDir.resolve("github").resolve(fileName4Local)
}
Expand All @@ -40,14 +40,14 @@ enum class MindustryEnd {

data class LatestOfficialMindustryLoc(
val end: MindustryEnd
) : IGameLoc {
) : IDownloadableGameLoc {
val fileBasename = when (end) {
MindustryEnd.Client -> R.officialRelease.client
MindustryEnd.Server -> R.officialRelease.server
}
override val fileName4Local = "${R.github.anuken}-${R.github.mindustry}-latest-$fileBasename"

override fun createDownloadLoc(): IDownloadLoc {
override fun resolveDownloadSrc(): URL {
val url = URL(R.github.tag.latestReleaseAPI)
val json = Jval.read(url.readText())
val version = json.getString("tag_name")
Expand All @@ -61,7 +61,7 @@ data class LatestOfficialMindustryLoc(
MindustryEnd.Server -> R.officialRelease.server
}
)
return delegate.createDownloadLoc()
return delegate.resolveDownloadSrc()
}

override fun resolveCacheFile(): File {
Expand All @@ -71,14 +71,14 @@ data class LatestOfficialMindustryLoc(

data class LatestMindustryBELoc(
val end: MindustryEnd
) : IGameLoc {
) : IDownloadableGameLoc {
val fileBasename = when (end) {
MindustryEnd.Client -> R.beRelease.client()
MindustryEnd.Server -> R.beRelease.server()
}
override val fileName4Local = "${R.github.anuken}-${R.github.mindustryBuilds}-latest-$fileBasename"

override fun createDownloadLoc(): IDownloadLoc {
override fun resolveDownloadSrc(): URL {
val url = URL(R.github.tag.beLatestReleaseAPI)
val json = Jval.read(url.readText())
val version = json.getString("tag_name")
Expand All @@ -92,7 +92,7 @@ data class LatestMindustryBELoc(
MindustryEnd.Server -> R.beRelease.server(version = version)
}
)
return delegate.createDownloadLoc()
return delegate.resolveDownloadSrc()
}

override fun resolveCacheFile(): File {
Expand All @@ -104,8 +104,6 @@ data class LocalGameLoc(
val file: File,
) : IGameLoc {
override val fileName4Local: String = file.name
val localCopy = LocalCopy(file)
override fun createDownloadLoc() = localCopy
/**
* It points to a local file
*/
Expand All @@ -114,61 +112,3 @@ data class LocalGameLoc(
}
}


/**
* An abstract download location, which can only open the input stream for reading
*/
interface IDownloadLoc : Serializable {
/**
* Open an input stream for reading.
* The caller has the responsibility to close this.
*/
fun openInputStream(): InputStream
/**
* The name of download location.
*/
val name: String
/**
* The path of download location.
*/
val path: String
}
/**
* A local download from disk
*/
data class LocalCopy(
var localFile: File,
) : IDownloadLoc {
override val name: String
get() = localFile.name
override val path: String
get() = localFile.absolutePath

override fun openInputStream(): InputStream =
localFile.inputStream()
}
/**
* A download from any GitHub url
*/
data class GitHubDownload(
override var name: String,
var url: URL,
) : IDownloadLoc {
override val path: String
get() = url.toString()

companion object {
@JvmStatic
fun release(
user: String, repo: String,
version: String,
assetName: String,
) = GitHubDownload(
assetName,
URL("https://github.com/$user/$repo/releases/download/$version/$assetName")
)
}

override fun openInputStream(): InputStream =
url.openStream()
}
7 changes: 4 additions & 3 deletions main/src/task/ResolveGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ open class ResolveGame : DefaultTask() {
if (!cacheFile.exists()) {
when (loc) {
is LocalGameLoc -> if (!cacheFile.isFile) throw GradleException("Local game $cacheFile doesn't exists.")
else -> loc.download(cacheFile)
is IDownloadableGameLoc -> loc.download(cacheFile)
else -> throw Exception("Unhandled game loc $loc")
}
}
createSymbolicLinkOrCopyCache(link = gameFile, target = cacheFile)
}

fun IGameLoc.download(cacheFile: File) {
fun IDownloadableGameLoc.download(cacheFile: File) {
logger.lifecycle("Downloading $this -> $cacheFile...")
try {
this.createDownloadLoc().openInputStream().use {
this.resolveDownloadSrc().openStream().use {
it.copyTo(cacheFile)
}
logger.lifecycle("${this.fileName4Local} was downloaded.")
Expand Down

0 comments on commit d1908ae

Please sign in to comment.