Skip to content

Commit

Permalink
download lock
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 21, 2023
1 parent 898dc94 commit 289c23d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
16 changes: 8 additions & 8 deletions main/src/SharedCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlin.math.absoluteValue
private
const val lockFileEx = "lock.json"

data class GihHubDownloadTrack(
data class DownloadLock(
/**
* It's changed when the mod is updated or network error.
*/
Expand Down Expand Up @@ -92,7 +92,7 @@ object SharedCache {
}

internal
fun updateGitHubDownloadTrack(
fun updateDownloadLock(
lockFile: File,
newTimestamp: Long = System.currentTimeMillis(),
logger: Logger? = null,
Expand All @@ -101,7 +101,7 @@ object SharedCache {
if (infoFi.isDirectory) {
infoFi.deleteRecursively()
}
val meta = GihHubDownloadTrack(lastUpdateTimestamp = newTimestamp)
val meta = DownloadLock(lastUpdateTimestamp = newTimestamp)
val json = gson.toJson(meta)
try {
infoFi.writeText(json)
Expand All @@ -120,18 +120,18 @@ object SharedCache {
if (infoFi.exists()) infoFi.delete()
return false
}
val meta = tryReadGitHubDownloadTrack(infoFi)
val meta = tryReadDownloadTrack(infoFi)
val curTime = System.currentTimeMillis()
return curTime - meta.lastUpdateTimestamp < outOfDate
}

internal
fun tryReadGitHubDownloadTrack(
fun tryReadDownloadTrack(
infoFile: File,
logger: Logger? = null,
): GihHubDownloadTrack {
fun writeAndGetDefault(): GihHubDownloadTrack {
val meta = GihHubDownloadTrack(lastUpdateTimestamp = System.currentTimeMillis())
): DownloadLock {
fun writeAndGetDefault(): DownloadLock {
val meta = DownloadLock(lastUpdateTimestamp = System.currentTimeMillis())
val infoContent = gson.toJson(meta)
try {
infoFile.ensureParentDir().writeText(infoContent)
Expand Down
9 changes: 4 additions & 5 deletions main/src/run/model/Mods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package io.github.liplum.mindustry

import arc.util.serialization.Jval
import io.github.liplum.dsl.*
import io.github.liplum.mindustry.SharedCache.updateGitHubDownloadTrack
import io.github.liplum.mindustry.SharedCache.updateDownloadLock
import org.gradle.api.GradleException
import java.io.File
import java.io.Serializable
import java.net.URL
import java.security.MessageDigest

/**
* An abstract mod file.
Expand Down Expand Up @@ -74,7 +73,7 @@ data class GitHubUntypedMod(
val repo: String,
) : IGitHubMod {
override fun resolveDownloadSrc(): URL {
updateGitHubDownloadTrack(lockFile = resolveCacheFile())
updateDownloadLock(lockFile = resolveCacheFile())
val jsonText = URL("https://api.github.com/repos/$repo").readText()
val json = Jval.read(jsonText)
val lan = json.getString("language")
Expand All @@ -95,7 +94,7 @@ data class GitHubPlainMod(
) : IGitHubMod {
val fileNameWithoutExtension = linkString(separator = "-", repo.repo2Path(), branch)
override fun resolveDownloadSrc(): URL {
updateGitHubDownloadTrack(lockFile = resolveCacheFile())
updateDownloadLock(lockFile = resolveCacheFile())
val jsonText = URL("https://api.github.com/repos/$repo").readText()
val json = Jval.read(jsonText)
val branch = if (!branch.isNullOrBlank()) branch
Expand All @@ -113,7 +112,7 @@ data class GitHubJvmMod(
) : IGitHubMod {
val fileNameWithoutExtension = linkString(separator = "-", repo.repo2Path(), tag)
override fun resolveDownloadSrc(): URL {
updateGitHubDownloadTrack(lockFile = resolveCacheFile())
updateDownloadLock(lockFile = resolveCacheFile())
return if (tag == null) {
resolveJvmModSrc(repo)
} else {
Expand Down

0 comments on commit 289c23d

Please sign in to comment.