diff --git a/gradle.properties b/gradle.properties index a684feaa..65134eee 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,8 +1,8 @@ # sytleguide kotlin.code.style=official -# Kotlin 1.4.21: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin -easyPluginVersion=0.12.3 +# Kotlin 1.4.30-RC: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin +easyPluginVersion=0.12.5 # version group=com.soywiz.korlibs.korio @@ -12,10 +12,10 @@ version=2.0.0-SNAPSHOT coroutinesVersion=1.4.2 # korlibs -klockVersion=2.0.3 -kdsVersion=2.0.3 -kmemVersion=2.0.3 -kryptoVersion=2.0.3 +klockVersion=2.0.5 +kdsVersion=2.0.5 +kmemVersion=2.0.5 +kryptoVersion=2.0.5 # bintray location project.bintray.org=korlibs diff --git a/korio/src/androidMain/kotlin/com/soywiz/korio/file/std/VfsAndroid.kt b/korio/src/androidMain/kotlin/com/soywiz/korio/file/std/VfsAndroid.kt index 4ee93c23..9f15681f 100644 --- a/korio/src/androidMain/kotlin/com/soywiz/korio/file/std/VfsAndroid.kt +++ b/korio/src/androidMain/kotlin/com/soywiz/korio/file/std/VfsAndroid.kt @@ -1,15 +1,13 @@ package com.soywiz.korio.file.std +import android.content.Context import com.soywiz.korio.android.androidContext import com.soywiz.klock.* -import com.soywiz.kmem.* -import com.soywiz.korio.async.* import com.soywiz.korio.file.* import com.soywiz.korio.lang.Closeable import com.soywiz.korio.stream.* import com.soywiz.korio.util.* import kotlinx.coroutines.* -import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* import java.io.* import java.io.IOException @@ -18,7 +16,20 @@ import java.net.* private val absoluteCwd by lazy { File(".").absolutePath } val tmpdir: String by lazy { System.getProperty("java.io.tmpdir") } -actual val resourcesVfs: VfsFile by lazy { ResourcesVfsProviderAndroid()().root.jail() } +private var androidContext: Context? = null +private var resourcesVfsProvider: ResourcesVfsProviderAndroid? = null +private lateinit var jailedResourcesVfsFile: VfsFile + +actual val resourcesVfs: VfsFile +get() { + if(resourcesVfsProvider == null) { + resourcesVfsProvider = ResourcesVfsProviderAndroid().apply { + jailedResourcesVfsFile = this.invoke().root.jail() + } + } + return jailedResourcesVfsFile +} + actual val rootLocalVfs: VfsFile by lazy { localVfs(absoluteCwd) } actual val applicationVfs: VfsFile by lazy { localVfs(absoluteCwd) } actual val applicationDataVfs: VfsFile by lazy { localVfs(absoluteCwd) } @@ -38,38 +49,57 @@ suspend fun File.open(mode: VfsOpenMode) = localVfs(this).open(mode) fun File.toVfs() = localVfs(this) fun UrlVfs(url: URL): VfsFile = UrlVfs(url.toString()) -private class ResourcesVfsProviderAndroid { +class ResourcesVfsProviderAndroid { + + private var androidResourcesVfs: AndroidResourcesVfs? = null + + fun deinit() { + androidContext = null + androidResourcesVfs?.context = null + androidResourcesVfs = null + } + operator fun invoke(): Vfs { - val merged = MergedVfs() - return object : Vfs.Decorator(merged.root) { - override suspend fun init() = run { merged += AndroidResourcesVfs(androidContext()).root } + val merged = MergedVfs() + + return object: Vfs.Decorator(merged.root) { + override suspend fun init() = run { + androidContext = androidContext() + androidResourcesVfs = AndroidResourcesVfs(androidContext).apply { + merged += root + } + } override fun toString(): String = "ResourcesVfs" } } } -class AndroidResourcesVfs(val context: android.content.Context) : Vfs() { +class AndroidResourcesVfs(var context: Context?) : Vfs() { + override suspend fun open(path: String, mode: VfsOpenMode): AsyncStream { return readRange(path, LONG_ZERO_TO_MAX_RANGE).openAsync(mode.cmode) } override suspend fun readRange(path: String, range: LongRange): ByteArray = executeIo { - //val path = "/assets/" + path.trim('/') - val rpath = path.trim('/') - - val fs = context.assets.open(rpath) - fs.skip(range.start) - val out = ByteArrayOutputStream() - val temp = ByteArray(16 * 1024) - var available = (range.endExclusiveClamped - range.start) - while (available >= 0) { - val read = fs.read(temp, 0, Math.min(temp.size.toLong(), available).toInt()) - if (read <= 0) break - out.write(temp, 0, read) - available -= read - } - out.toByteArray() + context?.let { context -> + + //val path = "/assets/" + path.trim('/') + val rpath = path.trim('/') + + val fs = context.assets.open(rpath) + fs.skip(range.start) + val out = ByteArrayOutputStream() + val temp = ByteArray(16 * 1024) + var available = (range.endExclusiveClamped - range.start) + while (available >= 0) { + val read = fs.read(temp, 0, Math.min(temp.size.toLong(), available).toInt()) + if (read <= 0) break + out.write(temp, 0, read) + available -= read + } + out.toByteArray() + } ?: throw IllegalStateException("Android context not set and required to access assets") } } @@ -257,3 +287,9 @@ private class LocalVfsJvm : LocalVfsV2() { override fun toString(): String = "LocalVfs" } + +actual fun cleanUpResourcesVfs() { + androidContext = null + resourcesVfsProvider?.deinit() + resourcesVfsProvider = null +} diff --git a/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/LocalVfs.kt b/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/LocalVfs.kt index 328540d0..68122381 100644 --- a/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/LocalVfs.kt +++ b/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/LocalVfs.kt @@ -20,6 +20,8 @@ abstract class LocalVfsV2 : LocalVfs() { var resourcesVfsDebug = false expect val resourcesVfs: VfsFile +expect fun cleanUpResourcesVfs() + expect val rootLocalVfs: VfsFile expect val applicationVfs: VfsFile expect val applicationDataVfs: VfsFile diff --git a/korio/src/jsMain/kotlin/com/soywiz/korio/file/std/LocalVfsJs.kt b/korio/src/jsMain/kotlin/com/soywiz/korio/file/std/LocalVfsJs.kt index d4da0c8e..1cd57a11 100644 --- a/korio/src/jsMain/kotlin/com/soywiz/korio/file/std/LocalVfsJs.kt +++ b/korio/src/jsMain/kotlin/com/soywiz/korio/file/std/LocalVfsJs.kt @@ -37,3 +37,6 @@ actual fun localVfs(path: String): VfsFile { } } } + +actual fun cleanUpResourcesVfs() { +} diff --git a/korio/src/jvmMain/kotlin/com/soywiz/korio/file/std/LocalVfsJvm.kt b/korio/src/jvmMain/kotlin/com/soywiz/korio/file/std/LocalVfsJvm.kt index 77b467af..6453b43c 100644 --- a/korio/src/jvmMain/kotlin/com/soywiz/korio/file/std/LocalVfsJvm.kt +++ b/korio/src/jvmMain/kotlin/com/soywiz/korio/file/std/LocalVfsJvm.kt @@ -466,3 +466,6 @@ private class LocalVfsJvm : LocalVfsV2() { override fun toString(): String = "LocalVfs" } + +actual fun cleanUpResourcesVfs() { +} diff --git a/korio/src/nativeCommonMain/kotlin/com/soywiz/korio/file/std/LocalVfsNative.kt b/korio/src/nativeCommonMain/kotlin/com/soywiz/korio/file/std/LocalVfsNative.kt index 6ba5b878..7a5e6a85 100644 --- a/korio/src/nativeCommonMain/kotlin/com/soywiz/korio/file/std/LocalVfsNative.kt +++ b/korio/src/nativeCommonMain/kotlin/com/soywiz/korio/file/std/LocalVfsNative.kt @@ -286,3 +286,6 @@ class LocalVfsNative : LocalVfsV2() { override fun toString(): String = "LocalVfs" } + +actual fun cleanUpResourcesVfs() { +}