|
| 1 | +package dev.openrune |
| 2 | + |
| 3 | +import dev.openrune.cache.CacheDownloader |
| 4 | +import dev.openrune.cache.CachePathHelper |
| 5 | +import dev.openrune.cache.tools.CacheEnvironment |
| 6 | +import dev.openrune.cache.tools.DownloadListener |
| 7 | +import dev.openrune.cache.tools.GameType |
| 8 | +import dev.openrune.cache.tools.OpenRS2 |
| 9 | +import kotlinx.coroutines.Dispatchers |
| 10 | +import kotlinx.coroutines.delay |
| 11 | +import kotlinx.coroutines.runBlocking |
| 12 | +import kotlinx.coroutines.withContext |
| 13 | +import kotlinx.coroutines.suspendCancellableCoroutine |
| 14 | +import kotlin.coroutines.resume |
| 15 | +import kotlin.coroutines.resumeWithException |
| 16 | +import mu.KotlinLogging |
| 17 | +import java.io.File |
| 18 | +import java.io.IOException |
| 19 | + |
| 20 | +private val logger = KotlinLogging.logger {} |
| 21 | + |
| 22 | +private const val MAX_DOWNLOAD_RETRIES = 3 |
| 23 | +private const val RETRY_DELAY_MS = 15_000L |
| 24 | +private const val DELAY_BETWEEN_DOWNLOADS_MS = 60_000L |
| 25 | + |
| 26 | +/** |
| 27 | + * Downloads and unzips all OSRS caches from rev 1 to latest (e.g. 236), one by one. |
| 28 | + * Skips revs that already have cache/data/cache on disk. |
| 29 | + * Run overnight so caches are ready for dumping in the morning. |
| 30 | + * |
| 31 | + * Usage: run this class as main (e.g. gradle runDownloadAllCaches or IDE run config). |
| 32 | + */ |
| 33 | +fun main() = runBlocking { |
| 34 | + val gameType = GameType.OLDSCHOOL |
| 35 | + val environment = CacheEnvironment.LIVE |
| 36 | + val downloader = CacheDownloader() |
| 37 | + |
| 38 | + OpenRS2.loadCaches() |
| 39 | + val game = gameType.name |
| 40 | + val env = environment.name |
| 41 | + val available = OpenRS2.allCaches |
| 42 | + .filter { (it.game ?: "").equals(game, true) && (it.environment ?: "").equals(env, true) } |
| 43 | + .flatMap { c -> (c.builds ?: emptyList()).map { b -> b.major } } |
| 44 | + .distinct() |
| 45 | + .filter { it >= 1 } |
| 46 | + .sorted() |
| 47 | + |
| 48 | + if (available.isEmpty()) { |
| 49 | + logger.error { "No OSRS revisions found in OpenRS2 for $game / $env" } |
| 50 | + return@runBlocking |
| 51 | + } |
| 52 | + |
| 53 | + val total = available.size |
| 54 | + val maxRev = available.maxOrNull() ?: return@runBlocking |
| 55 | + logger.info { "Cache download: $total caches to process (rev 1..$maxRev). Existing caches skipped." } |
| 56 | + |
| 57 | + var skipped = 0 |
| 58 | + var downloaded = 0 |
| 59 | + var failed = 0 |
| 60 | + available.forEachIndexed { index, rev -> |
| 61 | + withContext(Dispatchers.IO) { |
| 62 | + val current = index + 1 |
| 63 | + val cacheDir = CachePathHelper.getCacheDirectory(gameType, environment, rev) |
| 64 | + val cachePath = File(cacheDir, "data/cache") |
| 65 | + if (cachePath.exists()) { |
| 66 | + skipped++ |
| 67 | + logger.info { "[$current/$total] Rev $rev: already present, skip" } |
| 68 | + return@withContext |
| 69 | + } |
| 70 | + cacheDir.mkdirs() |
| 71 | + var success = false |
| 72 | + var lastError: Exception? = null |
| 73 | + repeat(MAX_DOWNLOAD_RETRIES) { attempt -> |
| 74 | + if (success) return@repeat |
| 75 | + try { |
| 76 | + if (attempt > 0) { |
| 77 | + logger.warn { "[$current/$total] Rev $rev: retry $attempt/$MAX_DOWNLOAD_RETRIES after ${RETRY_DELAY_MS / 1000}s (last: ${lastError?.message})" } |
| 78 | + delay(RETRY_DELAY_MS) |
| 79 | + } |
| 80 | + logger.info { "[$current/$total] Rev $rev: downloading..." } |
| 81 | + suspendCancellableCoroutine<Unit> { cont -> |
| 82 | + val listener = object : DownloadListener { |
| 83 | + override fun onProgress(progress: Int, max: Long, currentBytes: Long) { |
| 84 | + if (max <= 0) return |
| 85 | + val pct = (currentBytes * 100 / max).toInt().coerceIn(0, 100) |
| 86 | + if (pct % 10 == 0 || pct == 100) { |
| 87 | + val curMb = currentBytes / (1024 * 1024) |
| 88 | + val maxMb = max / (1024 * 1024) |
| 89 | + logger.info { "[$current/$total] Rev $rev: download $pct% (${curMb}MB / ${maxMb}MB)" } |
| 90 | + } |
| 91 | + } |
| 92 | + override fun onError(exception: Exception) { |
| 93 | + cont.resumeWithException(exception) |
| 94 | + } |
| 95 | + override fun onFinished() { |
| 96 | + cont.resume(Unit) |
| 97 | + } |
| 98 | + } |
| 99 | + try { |
| 100 | + OpenRS2.downloadCacheByRevision(rev, cacheDir, gameType, environment, -1, listener) |
| 101 | + } catch (e: Exception) { |
| 102 | + cont.resumeWithException(e) |
| 103 | + } |
| 104 | + } |
| 105 | + logger.info { "[$current/$total] Rev $rev: download done, extracting..." } |
| 106 | + downloader.unzipCache(cacheDir) { _, progress, _ -> |
| 107 | + progress?.let { p -> |
| 108 | + val pct = ((p - 50) / 49 * 100).toInt().coerceIn(0, 100) |
| 109 | + if (pct % 25 == 0 || pct == 100) { |
| 110 | + logger.info { "[$current/$total] Rev $rev: extract $pct%" } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + logger.info { "[$current/$total] Rev $rev: done" } |
| 115 | + downloaded++ |
| 116 | + success = true |
| 117 | + if (current < total) { |
| 118 | + logger.info { "Waiting ${DELAY_BETWEEN_DOWNLOADS_MS / 1000}s before next..." } |
| 119 | + delay(DELAY_BETWEEN_DOWNLOADS_MS) |
| 120 | + } |
| 121 | + } catch (e: Exception) { |
| 122 | + lastError = e |
| 123 | + val isRetryable = e is IOException || e.cause is IOException |
| 124 | + if (!isRetryable || attempt == MAX_DOWNLOAD_RETRIES - 1) { |
| 125 | + logger.error(e) { "[$current/$total] Rev $rev: failed (${e.message}). Skipping." } |
| 126 | + failed++ |
| 127 | + if (current < total) { |
| 128 | + logger.info { "Waiting ${DELAY_BETWEEN_DOWNLOADS_MS / 1000}s before next..." } |
| 129 | + delay(DELAY_BETWEEN_DOWNLOADS_MS) |
| 130 | + } |
| 131 | + return@repeat |
| 132 | + } |
| 133 | + logger.warn { "[$current/$total] Rev $rev: attempt ${attempt + 1} failed (${e.message}), will retry" } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + logger.info { "Cache download complete: $total total, $downloaded downloaded+extracted, $skipped already present, $failed failed." } |
| 140 | +} |
0 commit comments