|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2025 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.translations |
| 22 | + |
| 23 | +import com.google.gson.JsonParser |
| 24 | +import com.intellij.openapi.project.Project |
| 25 | +import com.intellij.openapi.roots.OrderEnumerator |
| 26 | +import com.intellij.openapi.roots.ProjectRootModificationTracker |
| 27 | +import com.intellij.openapi.vfs.VirtualFile |
| 28 | +import com.intellij.psi.util.CachedValueProvider |
| 29 | +import com.intellij.psi.util.CachedValuesManager |
| 30 | +import java.io.InputStreamReader |
| 31 | +import java.io.IOException |
| 32 | +import java.nio.charset.StandardCharsets |
| 33 | + |
| 34 | +class DeprecatedTranslations private constructor(val removed: Set<String>, val renamed: Map<String, String>) { |
| 35 | + val inverseRenamed = renamed.entries.associateBy({ it.value }) { it.key } |
| 36 | + |
| 37 | + val isEmpty |
| 38 | + get() = removed.isEmpty() && renamed.isEmpty() |
| 39 | + |
| 40 | + companion object { |
| 41 | + private val DEFAULT = DeprecatedTranslations(emptySet(), emptyMap()) |
| 42 | + private const val FILE_PATH = "assets/minecraft/lang/deprecated.json" |
| 43 | + |
| 44 | + fun getInstance(project: Project): DeprecatedTranslations { |
| 45 | + return CachedValuesManager.getManager(project).getCachedValue(project) { |
| 46 | + val file = findFile(project) ?: return@getCachedValue CachedValueProvider.Result( |
| 47 | + DEFAULT, |
| 48 | + ProjectRootModificationTracker.getInstance(project) |
| 49 | + ) |
| 50 | + CachedValueProvider.Result(getInstanceFromFile(file), file) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private fun findFile(project: Project): VirtualFile? { |
| 55 | + for (libraryRoot in OrderEnumerator.orderEntries(project).librariesOnly().classes().roots) { |
| 56 | + val file = libraryRoot.findFileByRelativePath(FILE_PATH) ?: continue |
| 57 | + if (!file.isDirectory) { |
| 58 | + return file |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return null |
| 63 | + } |
| 64 | + |
| 65 | + private fun getInstanceFromFile(file: VirtualFile): DeprecatedTranslations { |
| 66 | + try { |
| 67 | + val rootElement = InputStreamReader(file.inputStream, StandardCharsets.UTF_8).use { reader -> |
| 68 | + JsonParser.parseReader(reader) |
| 69 | + } |
| 70 | + |
| 71 | + if (rootElement == null || !rootElement.isJsonObject) { |
| 72 | + return DEFAULT |
| 73 | + } |
| 74 | + |
| 75 | + val obj = rootElement.asJsonObject |
| 76 | + |
| 77 | + val removedElt = obj.get("removed") |
| 78 | + val removed = if (removedElt != null && removedElt.isJsonArray) { |
| 79 | + val removedArr = removedElt.asJsonArray |
| 80 | + removedArr.mapNotNullTo(HashSet.newHashSet(removedArr.size())) { e -> |
| 81 | + if (e.isJsonPrimitive && e.asJsonPrimitive.isString) { |
| 82 | + e.asString |
| 83 | + } else { |
| 84 | + null |
| 85 | + } |
| 86 | + } |
| 87 | + } else { |
| 88 | + emptySet() |
| 89 | + } |
| 90 | + |
| 91 | + val renamedElt = obj.get("renamed") |
| 92 | + val renamed = if (renamedElt != null && renamedElt.isJsonObject) { |
| 93 | + val renamedObj = renamedElt.asJsonObject |
| 94 | + renamedObj.asMap().asSequence().mapNotNull { (k, v) -> |
| 95 | + if (v.isJsonPrimitive && v.asJsonPrimitive.isString) { |
| 96 | + k to v.asString |
| 97 | + } else { |
| 98 | + null |
| 99 | + } |
| 100 | + }.toMap(HashMap.newHashMap(renamedObj.size())) |
| 101 | + } else { |
| 102 | + emptyMap() |
| 103 | + } |
| 104 | + |
| 105 | + return DeprecatedTranslations(removed, renamed) |
| 106 | + } catch (_: IOException) { |
| 107 | + return DEFAULT |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments