Skip to content

Commit

Permalink
Support for custom library path. (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
olonho authored Mar 23, 2021
1 parent a4a1af9 commit a30747f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions samples/SkijaInjectSample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ val additionalArguments = mutableMapOf<String, String>()

val casualRun = tasks.named<JavaExec>("run") {
systemProperty("skiko.fps.enabled", "true")
// Use systemProperty("skiko.library.path", "/tmp") to test loader.
System.getProperties().entries
.associate {
(it.key as String) to (it.value as String)
Expand Down
35 changes: 21 additions & 14 deletions skiko/src/jvmMain/kotlin/org/jetbrains/skiko/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.nio.file.StandardCopyOption
object Library {
private var loaded = false

private val skikoLibraryPath = System.getProperty("skiko.library.path")
private val cacheRoot = "${System.getProperty("user.home")}/.skiko/"

private fun loadOrGet(cacheDir: File, path: String, resourceName: String, isLibrary: Boolean) {
Expand All @@ -33,23 +34,29 @@ object Library {
fun load() {
if (loaded) return

val resourcePath = "/"
val name = "skiko-$hostId"
val platformName = System.mapLibraryName(name)

val hashResourceStream = Library::class.java.getResourceAsStream(
"$resourcePath$platformName.sha256") ?: throw LibraryLoadException(
"Cannot find $platformName.sha256, proper native dependency missing."
)
val hash = hashResourceStream.use {
BufferedReader(InputStreamReader(it)).lines().toArray()[0] as String
}
val cacheDir = File("$cacheRoot/$hash")
cacheDir.mkdirs()
loadOrGet(cacheDir, resourcePath, platformName, true)
val loadIcu = hostOs.isWindows
if (loadIcu) {
loadOrGet(cacheDir, resourcePath, "icudtl.dat", false)
if (skikoLibraryPath != null) {
val library = File(File(skikoLibraryPath), platformName)
System.load(library.absolutePath)
} else {
val resourcePath = "/"
val hashResourceStream = Library::class.java.getResourceAsStream(
"$resourcePath$platformName.sha256"
) ?: throw LibraryLoadException(
"Cannot find $platformName.sha256, proper native dependency missing."
)
val hash = hashResourceStream.use {
BufferedReader(InputStreamReader(it)).lines().toArray()[0] as String
}
val cacheDir = File(File(cacheRoot), hash)
cacheDir.mkdirs()
loadOrGet(cacheDir, resourcePath, platformName, true)
val loadIcu = hostOs.isWindows
if (loadIcu) {
loadOrGet(cacheDir, resourcePath, "icudtl.dat", false)
}
}

// TODO move properties to SkikoProperties
Expand Down

0 comments on commit a30747f

Please sign in to comment.