Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit eeab7b4

Browse files
committed
Sync from next + Kotlin 1.4.30-RC + keeping kotlinx.coroutines version?
1 parent 2163977 commit eeab7b4

File tree

6 files changed

+77
-30
lines changed

6 files changed

+77
-30
lines changed

gradle.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# sytleguide
22
kotlin.code.style=official
33

4-
# Kotlin 1.4.21: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
5-
easyPluginVersion=0.12.3
4+
# Kotlin 1.4.30-RC: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
5+
easyPluginVersion=0.12.5
66

77
# version
88
group=com.soywiz.korlibs.korio
@@ -12,10 +12,10 @@ version=2.0.0-SNAPSHOT
1212
coroutinesVersion=1.4.2
1313

1414
# korlibs
15-
klockVersion=2.0.3
16-
kdsVersion=2.0.3
17-
kmemVersion=2.0.3
18-
kryptoVersion=2.0.3
15+
klockVersion=2.0.5
16+
kdsVersion=2.0.5
17+
kmemVersion=2.0.5
18+
kryptoVersion=2.0.5
1919

2020
# bintray location
2121
project.bintray.org=korlibs

korio/src/androidMain/kotlin/com/soywiz/korio/file/std/VfsAndroid.kt

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.soywiz.korio.file.std
22

3+
import android.content.Context
34
import com.soywiz.korio.android.androidContext
45
import com.soywiz.klock.*
5-
import com.soywiz.kmem.*
6-
import com.soywiz.korio.async.*
76
import com.soywiz.korio.file.*
87
import com.soywiz.korio.lang.Closeable
98
import com.soywiz.korio.stream.*
109
import com.soywiz.korio.util.*
1110
import kotlinx.coroutines.*
12-
import kotlinx.coroutines.channels.*
1311
import kotlinx.coroutines.flow.*
1412
import java.io.*
1513
import java.io.IOException
@@ -18,7 +16,20 @@ import java.net.*
1816
private val absoluteCwd by lazy { File(".").absolutePath }
1917
val tmpdir: String by lazy { System.getProperty("java.io.tmpdir") }
2018

21-
actual val resourcesVfs: VfsFile by lazy { ResourcesVfsProviderAndroid()().root.jail() }
19+
private var androidContext: Context? = null
20+
private var resourcesVfsProvider: ResourcesVfsProviderAndroid? = null
21+
private lateinit var jailedResourcesVfsFile: VfsFile
22+
23+
actual val resourcesVfs: VfsFile
24+
get() {
25+
if(resourcesVfsProvider == null) {
26+
resourcesVfsProvider = ResourcesVfsProviderAndroid().apply {
27+
jailedResourcesVfsFile = this.invoke().root.jail()
28+
}
29+
}
30+
return jailedResourcesVfsFile
31+
}
32+
2233
actual val rootLocalVfs: VfsFile by lazy { localVfs(absoluteCwd) }
2334
actual val applicationVfs: VfsFile by lazy { localVfs(absoluteCwd) }
2435
actual val applicationDataVfs: VfsFile by lazy { localVfs(absoluteCwd) }
@@ -38,38 +49,57 @@ suspend fun File.open(mode: VfsOpenMode) = localVfs(this).open(mode)
3849
fun File.toVfs() = localVfs(this)
3950
fun UrlVfs(url: URL): VfsFile = UrlVfs(url.toString())
4051

41-
private class ResourcesVfsProviderAndroid {
52+
class ResourcesVfsProviderAndroid {
53+
54+
private var androidResourcesVfs: AndroidResourcesVfs? = null
55+
56+
fun deinit() {
57+
androidContext = null
58+
androidResourcesVfs?.context = null
59+
androidResourcesVfs = null
60+
}
61+
4262
operator fun invoke(): Vfs {
43-
val merged = MergedVfs()
4463

45-
return object : Vfs.Decorator(merged.root) {
46-
override suspend fun init() = run { merged += AndroidResourcesVfs(androidContext()).root }
64+
val merged = MergedVfs()
65+
66+
return object: Vfs.Decorator(merged.root) {
67+
override suspend fun init() = run<ResourcesVfsProviderAndroid, Unit> {
68+
androidContext = androidContext()
69+
androidResourcesVfs = AndroidResourcesVfs(androidContext).apply {
70+
merged += root
71+
}
72+
}
4773
override fun toString(): String = "ResourcesVfs"
4874
}
4975
}
5076
}
5177

52-
class AndroidResourcesVfs(val context: android.content.Context) : Vfs() {
78+
class AndroidResourcesVfs(var context: Context?) : Vfs() {
79+
5380
override suspend fun open(path: String, mode: VfsOpenMode): AsyncStream {
5481
return readRange(path, LONG_ZERO_TO_MAX_RANGE).openAsync(mode.cmode)
5582
}
5683

5784
override suspend fun readRange(path: String, range: LongRange): ByteArray = executeIo {
58-
//val path = "/assets/" + path.trim('/')
59-
val rpath = path.trim('/')
60-
61-
val fs = context.assets.open(rpath)
62-
fs.skip(range.start)
63-
val out = ByteArrayOutputStream()
64-
val temp = ByteArray(16 * 1024)
65-
var available = (range.endExclusiveClamped - range.start)
66-
while (available >= 0) {
67-
val read = fs.read(temp, 0, Math.min(temp.size.toLong(), available).toInt())
68-
if (read <= 0) break
69-
out.write(temp, 0, read)
70-
available -= read
71-
}
72-
out.toByteArray()
85+
context?.let { context ->
86+
87+
//val path = "/assets/" + path.trim('/')
88+
val rpath = path.trim('/')
89+
90+
val fs = context.assets.open(rpath)
91+
fs.skip(range.start)
92+
val out = ByteArrayOutputStream()
93+
val temp = ByteArray(16 * 1024)
94+
var available = (range.endExclusiveClamped - range.start)
95+
while (available >= 0) {
96+
val read = fs.read(temp, 0, Math.min(temp.size.toLong(), available).toInt())
97+
if (read <= 0) break
98+
out.write(temp, 0, read)
99+
available -= read
100+
}
101+
out.toByteArray()
102+
} ?: throw IllegalStateException("Android context not set and required to access assets")
73103
}
74104
}
75105

@@ -257,3 +287,9 @@ private class LocalVfsJvm : LocalVfsV2() {
257287

258288
override fun toString(): String = "LocalVfs"
259289
}
290+
291+
actual fun cleanUpResourcesVfs() {
292+
androidContext = null
293+
resourcesVfsProvider?.deinit()
294+
resourcesVfsProvider = null
295+
}

korio/src/commonMain/kotlin/com/soywiz/korio/file/std/LocalVfs.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ abstract class LocalVfsV2 : LocalVfs() {
2020

2121
var resourcesVfsDebug = false
2222
expect val resourcesVfs: VfsFile
23+
expect fun cleanUpResourcesVfs()
24+
2325
expect val rootLocalVfs: VfsFile
2426
expect val applicationVfs: VfsFile
2527
expect val applicationDataVfs: VfsFile

korio/src/jsMain/kotlin/com/soywiz/korio/file/std/LocalVfsJs.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ actual fun localVfs(path: String): VfsFile {
3737
}
3838
}
3939
}
40+
41+
actual fun cleanUpResourcesVfs() {
42+
}

korio/src/jvmMain/kotlin/com/soywiz/korio/file/std/LocalVfsJvm.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,6 @@ private class LocalVfsJvm : LocalVfsV2() {
466466

467467
override fun toString(): String = "LocalVfs"
468468
}
469+
470+
actual fun cleanUpResourcesVfs() {
471+
}

korio/src/nativeCommonMain/kotlin/com/soywiz/korio/file/std/LocalVfsNative.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,6 @@ class LocalVfsNative : LocalVfsV2() {
286286

287287
override fun toString(): String = "LocalVfs"
288288
}
289+
290+
actual fun cleanUpResourcesVfs() {
291+
}

0 commit comments

Comments
 (0)