From 5b455839a27367c52829c605694f2e1ca535fc7f Mon Sep 17 00:00:00 2001 From: soywiz Date: Sat, 28 Nov 2020 16:03:53 +0100 Subject: [PATCH] Fixes https://github.com/korlibs/korge-next/issues/85 The by lazy were computed before customCwd as set by the korge gradle plugin --- .../soywiz/korio/file/std/DynamicRootVfs.kt | 11 ++++++++++ .../korio/file/std/DynamicRootVfsTest.kt | 17 +++++++++++++++ .../soywiz/korio/file/std/LocalVfsNative.kt | 21 ++++++++++++------- 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 korio/src/commonMain/kotlin/com/soywiz/korio/file/std/DynamicRootVfs.kt create mode 100644 korio/src/commonTest/kotlin/com/soywiz/korio/file/std/DynamicRootVfsTest.kt diff --git a/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/DynamicRootVfs.kt b/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/DynamicRootVfs.kt new file mode 100644 index 00000000..0df530bc --- /dev/null +++ b/korio/src/commonMain/kotlin/com/soywiz/korio/file/std/DynamicRootVfs.kt @@ -0,0 +1,11 @@ +package com.soywiz.korio.file.std + +import com.soywiz.korio.file.* + +fun DynamicRootVfs(base: Vfs, rootGet: () -> String) = DynamicRootVfsVfs(base, rootGet).root + +class DynamicRootVfsVfs(val base: Vfs, val rootGet: () -> String) : Vfs.Proxy() { + private fun getLocalAbsolutePath(path: String) = rootGet().pathInfo.lightCombine(path.pathInfo).fullPath + override fun getAbsolutePath(path: String): String = base.getAbsolutePath(getLocalAbsolutePath(path)) + override suspend fun access(path: String): VfsFile = base[getLocalAbsolutePath(path)] +} diff --git a/korio/src/commonTest/kotlin/com/soywiz/korio/file/std/DynamicRootVfsTest.kt b/korio/src/commonTest/kotlin/com/soywiz/korio/file/std/DynamicRootVfsTest.kt new file mode 100644 index 00000000..1eb58a8a --- /dev/null +++ b/korio/src/commonTest/kotlin/com/soywiz/korio/file/std/DynamicRootVfsTest.kt @@ -0,0 +1,17 @@ +package com.soywiz.korio.file.std + +import com.soywiz.korio.async.* +import kotlin.test.* + +class DynamicRootVfsTest { + @Test + fun test() = suspendTest { + val memoryVfs = MemoryVfsMix("path0/demo" to "world") + var base = "/path0" + val root = DynamicRootVfs(memoryVfs.vfs) { base } + assertEquals("/path0/demo", root["demo"].absolutePath) + assertEquals("world", root["demo"].readString()) + base = "/path1" + assertEquals("/path1/test", root["test"].absolutePath) + } +} 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 a1b0095f..6ba5b878 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 @@ -31,18 +31,23 @@ import com.soywiz.korio.process.posixExec val tmpdir: String by lazy { Environment["TMPDIR"] ?: Environment["TEMP"] ?: Environment["TMP"] ?: "/tmp" } var customCwd: String? = null -val cwd: String by lazy { customCwd ?: com.soywiz.korio.nativeCwd() } +val nativeCwd by lazy { com.soywiz.korio.nativeCwd() } +val cwd: String get() = customCwd ?: nativeCwd -actual val resourcesVfs: VfsFile by lazy { applicationDataVfs.jail() } -actual val rootLocalVfs: VfsFile by lazy { localVfs(cwd) } -actual val applicationVfs: VfsFile by lazy { localVfs(cwd) } -actual val applicationDataVfs: VfsFile by lazy { localVfs(cwd) } +val cwdVfs: VfsFile by lazy { DynamicRootVfs(rootLocalVfsNative) { cwd } } +actual val resourcesVfs: VfsFile by lazy { cwdVfs.jail() } actual val cacheVfs: VfsFile by lazy { MemoryVfs() } -actual val externalStorageVfs: VfsFile by lazy { localVfs(cwd) } -actual val userHomeVfs: VfsFile by lazy { localVfs(cwd) } actual val tempVfs: VfsFile by lazy { localVfs(tmpdir) } -actual fun localVfs(path: String): VfsFile = LocalVfsNative()[path] +actual val rootLocalVfs: VfsFile get() = cwdVfs +actual val applicationVfs: VfsFile get() = cwdVfs +actual val applicationDataVfs: VfsFile get() = cwdVfs +actual val externalStorageVfs: VfsFile get() = cwdVfs +actual val userHomeVfs: VfsFile get() = cwdVfs + +val rootLocalVfsNative by lazy { LocalVfsNative() } + +actual fun localVfs(path: String): VfsFile = rootLocalVfsNative[path] private val IOWorker by lazy { Worker.start().also { kotlin.native.Platform.isMemoryLeakCheckerActive = false } }