Skip to content

Commit

Permalink
game from url
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 20, 2023
1 parent 7cb3ad2 commit f66306f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
5 changes: 4 additions & 1 deletion TestProjectKt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ runMindustry {
official(version = "v146")
putDataAt(file = buildDir.resolve("customDataDir"))
}
addClient("default data dir"){
addClient("default data dir") {
official(version = "v146")
modpack = null
useDefaultDataDir()
}
addClient("from url") {
url("https://github.com/Anuken/Mindustry/releases/download/v146/Mindustry.jar")
}
addClient("debugging") {
official(version = "v146")
useModpack(name = "for debugging 2")
Expand Down
1 change: 1 addition & 0 deletions main/src/dsl/FileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fun InputStream.copyToTmpAndMove(file: File) {
this.use {
it.copyTo(tmp)
}
file.ensureParentDir()
tmp.renameTo(file)
// ignore the error when deleting the temp file
runCatching {
Expand Down
11 changes: 10 additions & 1 deletion main/src/run/model/GameSide.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.liplum.mindustry.LocalProperties.local
import org.gradle.api.Action
import org.gradle.api.Project
import java.io.File
import java.net.URL

enum class GameSideType {
Client, Server
Expand Down Expand Up @@ -225,8 +226,16 @@ abstract class AddGameSideSpec<T : GameSide> {
}
}

fun url(url: URL) {
UrlGameLoc(url).checkAndSet()
}

fun url(url: String) {
this.url(URL(url))
}

fun localFile(path: String) {
LocalGameLoc(File(path)).checkAndSet()
this.localFile(File(path))
}

fun localFile(file: File) {
Expand Down
12 changes: 12 additions & 0 deletions main/src/run/model/Games.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ sealed interface IDownloadableGameLoc : IGameLoc {

sealed interface ILatestDownloadableGameLoc : IDownloadableGameLoc

data class UrlGameLoc(
val url: URL
) : IDownloadableGameLoc {
override fun resolveDownloadSrc(): URL = url

override val fileName4Local: String = "${url.resolve4FileName()}.jar"

override fun resolveCacheFile(): File {
return SharedCache.gamesDir.resolve("url").resolve(fileName4Local)
}
}

data class GitHubGameLoc(
val user: String,
val repo: String,
Expand Down
12 changes: 2 additions & 10 deletions main/src/run/model/Mods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,10 @@ data class UrlMod(
) : IMod {
constructor(url: String) : this(URL(url))

override val fileName4Local: String = run {
val path: String = url.path
val last = path.substring(path.lastIndexOf('/') + 1)
if (last.endsWith(".zip")) last else "$last.zip"
}
override val fileName4Local: String = "${url.resolve4FileName()}.zip"

override fun resolveCacheFile(): File {
val urlInBytes = MessageDigest
.getInstance("SHA-1")
.digest(url.toString().toByteArray())
val urlHashed = urlInBytes.toString()
return SharedCache.modsDir.resolve("url").resolve(urlHashed)
return SharedCache.modsDir.resolve("url").resolve(fileName4Local)
}
}

Expand Down
18 changes: 18 additions & 0 deletions main/src/run/model/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.liplum.mindustry

import java.net.URL
import java.security.MessageDigest

fun URL.resolve4FileName(): String {
val urlInBytes = MessageDigest
.getInstance("SHA-1")
.digest(this.toString().toByteArray())

// Convert the byte array to a hexadecimal string
val hexString = StringBuilder()
for (byte in urlInBytes) {
hexString.append(String.format("%02x", byte))
}

return hexString.toString()
}

0 comments on commit f66306f

Please sign in to comment.