Commit 21526244 authored by Alexey Tsvetkov's avatar Alexey Tsvetkov

Adjust Library after conversion to Kotlin

parent 24f1cecb
package org.jetbrains.skiko package org.jetbrains.skiko
import java.io.File import java.io.File
import java.io.IOException
import java.net.URISyntaxException
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption 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 // based on https://github.com/JetBrains/skija/blob/bc6e0531021e4ff839c07196e0336d6456ed7520/src/main/java/org/jetbrains/skija/Library.java
object Library { 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 // https://github.com/adamheinrich/native-utils/blob/e6a39489662846a77504634b6fafa4995ede3b1d/src/main/java/cz/adamh/utils/NativeUtils.java
@Synchronized
fun load(resourcePath: String, name: String) { fun load(resourcePath: String, name: String) {
if (_loaded) return if (loaded) return
try {
var file: File val libFileName = "$libPrefix$name$libExtension"
val fileName = _getPrefix() + name + "." + _getExtension() val url = Library::class.java.getResource(resourcePath + libFileName)
val url = Library::class.java.getResource(resourcePath + fileName) // println("Loading " + url);
// System.out.println("Loading " + url); val libFile = when {
requireNotNull(url) { "Library $fileName is not found in $resourcePath" } url == null -> error("Library $libFileName is not found in $resourcePath")
if (url.protocol === "file") file = try { url.protocol == "file" -> File(url.toURI())
// System.out.println("Loading " + url); else -> url.openStream().use { input ->
File(url.toURI()) val tempRoot = File(System.getProperty("java.io.tmpdir"))
} catch (e: URISyntaxException) { val tempDir = tempRoot.resolve("skija_" + System.nanoTime()).apply { mkdirs() }
throw RuntimeException(e) File(tempDir, libFileName).also {
} else url.openStream().use { `is` -> it.deleteOnExit()
val tempDir = File(System.getProperty("java.io.tmpdir"), "skija_" + System.nanoTime()) Files.copy(input, it.toPath(), StandardCopyOption.REPLACE_EXISTING)
tempDir.mkdirs() }
tempDir.deleteOnExit() }
file = File(tempDir, fileName) }
file.deleteOnExit() System.load(libFile.absolutePath)
Files.copy(`is`, file.toPath(), StandardCopyOption.REPLACE_EXISTING) loaded = true
} }
System.load(file.absolutePath)
_loaded = true private val libPrefix: String
} catch (e: IOException) { get() = if (currentOS == OS.Windows) "" else "lib"
throw RuntimeException(e)
} private val libExtension: String
} get() = when (currentOS) {
OS.Mac -> ".dylib"
fun _getPrefix(): String { OS.Linux -> ".so"
val os = System.getProperty("os.name") OS.Windows -> ".dll"
val lowerCaseOs = os.toLowerCase() }
return if (lowerCaseOs.contains("windows")) "" else "lib"
} private val currentOS by lazy {
val osName = System.getProperty("os.name") ?: error("Unknown OS: 'os.name' is null")
fun _getExtension(): String { val os = osName.toLowerCase(Locale.ROOT)
val os = System.getProperty("os.name") ?: throw RuntimeException("Unknown operation system") when {
val lowerCaseOs = os.toLowerCase() os.contains("mac") || os.contains("darwin") -> OS.Mac
if (lowerCaseOs.contains("mac") || lowerCaseOs.contains("darwin")) return "dylib" os.contains("win") -> OS.Windows
if (lowerCaseOs.contains("windows")) return "dll" os.contains("nux") -> OS.Linux
if (lowerCaseOs.contains("nux")) return "so" else -> error("Unknown OS: $osName")
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