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()
Files.copy(`is`, file.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 { private val libPrefix: String
val os = System.getProperty("os.name") get() = if (currentOS == OS.Windows) "" else "lib"
val lowerCaseOs = os.toLowerCase()
return if (lowerCaseOs.contains("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 { private enum class OS {
val os = System.getProperty("os.name") ?: throw RuntimeException("Unknown operation system") Linux, Mac, Windows
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")
} }
} }
\ 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