diff --git a/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt b/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt index 6eebc45c..a03a357b 100644 --- a/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt +++ b/src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt @@ -30,10 +30,12 @@ class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable + val isLocked = locks[path]!! > 1 + // decrease the lock count if the editor was opened for a file + locks.merge(path, -1, Integer::sum) + isLocked + } ?: false + + // if unlocked, write back to the file + if (!isLocked) { + it.value.use { stream -> + val result = StreamResult(stream) + TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), result) + } + + it.value.close() + return + } } - // remove the lock, if it exists - filePath?.let { - locks.remove(it) - } + closed = true } private companion object { - // list of locked file paths - val locks = mutableListOf() + // map of concurrent open files + val locks = mutableMapOf() } }