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

Commit

Permalink
Sync from next + Kotlin 1.4.30-RC + keeping kotlinx.coroutines version?
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jan 24, 2021
1 parent 2163977 commit eeab7b4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) }
Expand All @@ -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<ResourcesVfsProviderAndroid, Unit> {
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")
}
}

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

override fun toString(): String = "LocalVfs"
}

actual fun cleanUpResourcesVfs() {
androidContext = null
resourcesVfsProvider?.deinit()
resourcesVfsProvider = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ actual fun localVfs(path: String): VfsFile {
}
}
}

actual fun cleanUpResourcesVfs() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,6 @@ private class LocalVfsJvm : LocalVfsV2() {

override fun toString(): String = "LocalVfs"
}

actual fun cleanUpResourcesVfs() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,6 @@ class LocalVfsNative : LocalVfsV2() {

override fun toString(): String = "LocalVfs"
}

actual fun cleanUpResourcesVfs() {
}

0 comments on commit eeab7b4

Please sign in to comment.