Open orx-olive script object loading to the public.
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
package org.openrndr.extra.olive
|
||||
|
||||
import java.io.InputStream
|
||||
import java.io.Reader
|
||||
import javax.script.ScriptEngineManager
|
||||
|
||||
internal class LoadException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause)
|
||||
|
||||
internal class KtsObjectLoader(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) {
|
||||
|
||||
private val engine = ScriptEngineManager(classLoader).getEngineByExtension("kts")
|
||||
|
||||
init {
|
||||
if (engine == null) {
|
||||
throw RuntimeException("could not create scripting engine")
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <R> safeEval(noinline evaluation: () -> R?) = try {
|
||||
evaluation()
|
||||
} catch (e: Exception) {
|
||||
throw LoadException("Cannot load script", e)
|
||||
}
|
||||
|
||||
inline fun <reified T> Any?.castOrError() = takeIf { it is T }?.let { it as T }
|
||||
?: throw IllegalArgumentException("Cannot cast $this to expected type ${T::class}")
|
||||
|
||||
inline fun <reified T> load(script: String): T = safeEval { engine.eval(script) }.castOrError()
|
||||
|
||||
inline fun <reified T> load(reader: Reader): T = safeEval { engine.eval(reader) }.castOrError()
|
||||
|
||||
inline fun <reified T> load(inputStream: InputStream): T = load(inputStream.reader())
|
||||
|
||||
inline fun <reified T> loadAll(vararg inputStream: InputStream): List<T> = inputStream.map(::load)
|
||||
}
|
||||
@@ -89,15 +89,13 @@ class Olive<P : Program>(val resources: Resources? = null) : Extension {
|
||||
|
||||
watcher = program.watchFile(File(script)) {
|
||||
try {
|
||||
val script = it.readText()
|
||||
val func = KtsObjectLoader().load<P.() -> Unit>(script)
|
||||
val func = loadFromScript<P.()->Unit>(it)
|
||||
|
||||
program.extensions.clear()
|
||||
program.extensions.addAll(originalExtensions)
|
||||
|
||||
trackedListeners.forEach { l -> l.restoreListeners(store) }
|
||||
session?.end()
|
||||
|
||||
session = Session.root.fork()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
59
orx-olive/src/main/kotlin/ScriptObjectLoader.kt
Normal file
59
orx-olive/src/main/kotlin/ScriptObjectLoader.kt
Normal file
@@ -0,0 +1,59 @@
|
||||
package org.openrndr.extra.olive
|
||||
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.Reader
|
||||
import java.net.MalformedURLException
|
||||
import java.net.URL
|
||||
import javax.script.ScriptEngineManager
|
||||
|
||||
class LoadException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause)
|
||||
|
||||
class ScriptObjectLoader(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) {
|
||||
val engine = ScriptEngineManager(classLoader).getEngineByExtension("kts")
|
||||
|
||||
init {
|
||||
require(engine != null) { "could not create scripting engine" }
|
||||
}
|
||||
|
||||
fun <R> safeEval(evaluation: () -> R?) = try {
|
||||
evaluation()
|
||||
} catch (e: Exception) {
|
||||
throw LoadException("Cannot load script", e)
|
||||
}
|
||||
|
||||
inline fun <reified T> Any?.castOrError() = takeIf { it is T }?.let { it as T }
|
||||
?: throw IllegalArgumentException("Cannot cast $this to expected type ${T::class}")
|
||||
|
||||
inline fun <reified T> load(script: String): T = safeEval { engine.eval(script) }.castOrError()
|
||||
|
||||
inline fun <reified T> load(reader: Reader): T = safeEval { engine.eval(reader) }.castOrError()
|
||||
|
||||
inline fun <reified T> load(inputStream: InputStream): T = load(inputStream.reader())
|
||||
|
||||
inline fun <reified T> loadAll(vararg inputStream: InputStream): List<T> = inputStream.map(::load)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an object from script.
|
||||
*/
|
||||
inline fun <reified T : Any> loadFromScript(fileOrUrl: String, loader: ScriptObjectLoader = ScriptObjectLoader()): T {
|
||||
val isUrl = try {
|
||||
URL(fileOrUrl); true
|
||||
} catch (e: MalformedURLException) {
|
||||
false
|
||||
}
|
||||
|
||||
val script = if (isUrl) {
|
||||
URL(fileOrUrl).readText()
|
||||
} else {
|
||||
File(fileOrUrl).readText()
|
||||
}
|
||||
return loader.load(script)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an object from script file
|
||||
*/
|
||||
inline fun <reified T : Any> loadFromScript(file: File, loader: ScriptObjectLoader = ScriptObjectLoader()): T =
|
||||
loader.load(file.readText())
|
||||
Reference in New Issue
Block a user