diff options
author | nea <romangraef@gmail.com> | 2022-08-03 02:31:05 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-08-03 02:31:05 +0200 |
commit | 2625eeb7dedb05b524b08721e1267acdcd93e5c1 (patch) | |
tree | 476117af52a76f22c445b8b8dac4778f24ce5c51 /src/main/kotlin/moe/nea/notenoughupdates/repo | |
parent | b9455467863f194accf30a89b2c2998822f2c105 (diff) | |
download | firmament-2625eeb7dedb05b524b08721e1267acdcd93e5c1.tar.gz firmament-2625eeb7dedb05b524b08721e1267acdcd93e5c1.tar.bz2 firmament-2625eeb7dedb05b524b08721e1267acdcd93e5c1.zip |
scuffed config screen
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/repo')
-rw-r--r-- | src/main/kotlin/moe/nea/notenoughupdates/repo/RepoDownloadManager.kt | 34 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt | 40 |
2 files changed, 50 insertions, 24 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoDownloadManager.kt b/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoDownloadManager.kt index 977c035..34279af 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoDownloadManager.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoDownloadManager.kt @@ -24,22 +24,18 @@ object RepoDownloadManager { val repoSavedLocation = NotEnoughUpdates.DATA_DIR.resolve("repo-extracted") val repoMetadataLocation = NotEnoughUpdates.DATA_DIR.resolve("loaded-repo-sha.txt") - val user = "NotEnoughUpdates" - val repo = "NotEnoughUpdates-REPO" - val branch = "dangerous" - private fun loadSavedVersionHash(): String? = - if (repoSavedLocation.exists()) { - if (repoMetadataLocation.exists()) { - try { - repoMetadataLocation.readText().trim() - } catch (e: IOException) { + if (repoSavedLocation.exists()) { + if (repoMetadataLocation.exists()) { + try { + repoMetadataLocation.readText().trim() + } catch (e: IOException) { + null + } + } else { null } - } else { - null - } - } else null + } else null private fun saveVersionHash(versionHash: String) { latestSavedVersionHash = versionHash @@ -54,7 +50,7 @@ object RepoDownloadManager { private suspend fun requestLatestGithubSha(): String? { val response = - NotEnoughUpdates.httpClient.get("https://api.github.com/repos/$user/$repo/commits/$branch") + NotEnoughUpdates.httpClient.get("https://api.github.com/repos/${RepoManager.config.user}/${RepoManager.config.repo}/commits/${RepoManager.config.branch}") if (response.status.value != 200) { return null } @@ -81,11 +77,11 @@ object RepoDownloadManager { } val currentSha = loadSavedVersionHash() if (latestSha != currentSha) { - val requestUrl = "https://github.com/$user/$repo/archive/$latestSha.zip" + val requestUrl = "https://github.com/${RepoManager.config.user}/${RepoManager.config.repo}/archive/$latestSha.zip" logger.info("Planning to upgrade repository from $currentSha to $latestSha from $requestUrl") val zipFile = downloadGithubArchive(requestUrl) logger.info("Download repository zip file to $zipFile. Deleting old repository") - withContext(IO) { repoSavedLocation.deleteIfExists() } + withContext(IO) { repoSavedLocation.toFile().deleteRecursively() } logger.info("Extracting new repository") withContext(IO) { extractNewRepository(zipFile) } logger.info("Repository loaded on disk.") @@ -104,9 +100,9 @@ object RepoDownloadManager { val entry = cis.nextEntry ?: break if (entry.isDirectory) continue val extractedLocation = - repoSavedLocation.resolve( - entry.name.substringAfter('/', missingDelimiterValue = "") - ) + repoSavedLocation.resolve( + entry.name.substringAfter('/', missingDelimiterValue = "") + ) if (repoSavedLocation !in extractedLocation.iterate { it.parent }) { logger.error("Not Enough Updates detected an invalid zip file. This is a potential security risk, please report this in the Moulberry discord.") throw RuntimeException("Not Enough Updates detected an invalid zip file. This is a potential security risk, please report this in the Moulberry discord.") diff --git a/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt b/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt index f3d53e8..70ee18e 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt @@ -2,29 +2,59 @@ package moe.nea.notenoughupdates.repo import io.github.moulberry.repo.NEURepository import kotlinx.coroutines.launch +import kotlinx.serialization.Serializable +import kotlinx.serialization.serializer import moe.nea.notenoughupdates.NotEnoughUpdates import moe.nea.notenoughupdates.NotEnoughUpdates.logger +import moe.nea.notenoughupdates.util.ConfigHolder +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents import net.minecraft.client.Minecraft import net.minecraft.network.protocol.game.ClientboundUpdateRecipesPacket -object RepoManager { +object RepoManager : ConfigHolder<RepoManager.Config>(serializer(), "repo", ::Config) { + @Serializable + data class Config( + var user: String = "NotEnoughUpdates", + var repo: String = "NotEnoughUpdates-REPO", + var autoUpdate: Boolean = true, + var branch: String = "dangerous", + ) + var recentlyFailedToUpdateItemList = false val neuRepo: NEURepository = NEURepository.of(RepoDownloadManager.repoSavedLocation).apply { registerReloadListener(ItemCache) registerReloadListener { - if (Minecraft.getInstance().connection?.handleUpdateRecipes(ClientboundUpdateRecipesPacket(mutableListOf())) == null) { + if (!trySendClientboundUpdateRecipesPacket()) { logger.warn("Failed to issue a ClientboundUpdateRecipesPacket (to reload REI). This may lead to an outdated item list.") + recentlyFailedToUpdateItemList = true } } } + private fun trySendClientboundUpdateRecipesPacket(): Boolean { + return Minecraft.getInstance().level != null && Minecraft.getInstance().connection?.handleUpdateRecipes(ClientboundUpdateRecipesPacket(mutableListOf())) != null + } + + init { + ClientTickEvents.START_WORLD_TICK.register(ClientTickEvents.StartWorldTick { + if (recentlyFailedToUpdateItemList && trySendClientboundUpdateRecipesPacket()) + recentlyFailedToUpdateItemList = false + }) + } fun launchAsyncUpdate() { NotEnoughUpdates.coroutineScope.launch { - if (RepoDownloadManager.downloadUpdate()) { - neuRepo.reload() - } + RepoDownloadManager.downloadUpdate() + neuRepo.reload() + } + } + + fun initialize() { + if (config.autoUpdate) { + launchAsyncUpdate() + } else { + neuRepo.reload() } } |