Commit 21526244 authored by Alexey Tsvetkov's avatar Alexey Tsvetkov

Adjust Library after conversion to Kotlin

parent 24f1cecb
package org.jetbrains.skiko
import java.io.File
import java.io.IOException
import java.net.URISyntaxException
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.*
// based on https://github.com/JetBrains/skija/blob/bc6e0531021e4ff839c07196e0336d6456ed7520/src/main/java/org/jetbrains/skija/Library.java
object Library {
var _loaded = false
private var loaded = false
// https://github.com/adamheinrich/native-utils/blob/e6a39489662846a77504634b6fafa4995ede3b1d/src/main/java/cz/adamh/utils/NativeUtils.java
@Synchronized
fun load(resourcePath: String, name: String) {
if (_loaded) return
try {
var file: File
val fileName = _getPrefix() + name + "." + _getExtension()
val url = Library::class.java.getResource(resourcePath + fileName)
// System.out.println("Loading " + url);
requireNotNull(url) { "Library $fileName is not found in $resourcePath" }
if (url.protocol === "file") file = try {
// System.out.println("Loading " + url);
File(url.toURI())
} catch (e: URISyntaxException) {
throw RuntimeException(e)
} else url.openStream().use { `is` ->
val tempDir = File(System.getProperty("java.io.tmpdir"), "skija_" + System.nanoTime())
tempDir.mkdirs()
tempDir.deleteOnExit()
file = File(tempDir, fileName)
file.deleteOnExit()
Files.copy(`is`, file.toPath(), StandardCopyOption.REPLACE_EXISTING)
if (loaded) return
val libFileName = "$libPrefix$name$libExtension"
val url = Library::class.java.getResource(resourcePath + libFileName)
// println("Loading " + url);
val libFile = when {
url == null -> error("Library $libFileName is not found in $resourcePath")
url.protocol == "file" -> File(url.toURI())
else -> url.openStream().use { input ->
val tempRoot = File(System.getProperty("java.io.tmpdir"))
val tempDir = tempRoot.resolve("skija_" + System.nanoTime()).apply { mkdirs() }
File(tempDir, libFileName).also {
it.deleteOnExit()
Files.copy(input, it.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
}
System.load(file.absolutePath)
_loaded = true
} catch (e: IOException) {
throw RuntimeException(e)
}
System.load(libFile.absolutePath)
loaded = true
}
fun _getPrefix(): String {
val os = System.getProperty("os.name")
val lowerCaseOs = os.toLowerCase()
return if (lowerCaseOs.contains("windows")) "" else "lib"
private val libPrefix: String
get() = if (currentOS == OS.Windows) "" else "lib"
private val libExtension: String
get() = when (currentOS) {
OS.Mac -> ".dylib"
OS.Linux -> ".so"
OS.Windows -> ".dll"
}
private val currentOS by lazy {
val osName = System.getProperty("os.name") ?: error("Unknown OS: 'os.name' is null")
val os = osName.toLowerCase(Locale.ROOT)
when {
os.contains("mac") || os.contains("darwin") -> OS.Mac
os.contains("win") -> OS.Windows
os.contains("nux") -> OS.Linux
else -> error("Unknown OS: $osName")
}
}
fun _getExtension(): String {
val os = System.getProperty("os.name") ?: throw RuntimeException("Unknown operation system")
val lowerCaseOs = os.toLowerCase()
if (lowerCaseOs.contains("mac") || lowerCaseOs.contains("darwin")) return "dylib"
if (lowerCaseOs.contains("windows")) return "dll"
if (lowerCaseOs.contains("nux")) return "so"
throw RuntimeException("Unknown operation system: $os")
private enum class OS {
Linux, Mac, Windows
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment