Refactor libraries
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
||||
|
||||
import com.bartlomiejpluta.ttsserver.core.web.mime.MimeType
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.TwoArgFunction
|
||||
@@ -8,10 +9,13 @@ class HTTPLibrary : TwoArgFunction() {
|
||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||
val methods = LuaValue.tableOf()
|
||||
val responses = LuaValue.tableOf()
|
||||
val mimeTypes = LuaValue.tableOf()
|
||||
NanoHTTPD.Method.values().forEach { methods.set(it.name, it.name) }
|
||||
NanoHTTPD.Response.Status.values().forEach { responses.set(it.name, it.requestStatus) }
|
||||
MimeType.values().forEach { mimeTypes.set(it.name, it.mimeType) }
|
||||
env.set("Method", methods)
|
||||
env.set("Status", responses)
|
||||
env.set("Mime", mimeTypes)
|
||||
return methods
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ import org.luaj.vm2.lib.jse.CoerceJavaToLua
|
||||
class SonosLibrary : TwoArgFunction() {
|
||||
|
||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||
val sonos = LuaValue.tableOf().also {
|
||||
it.set("discover", DiscoverFunction())
|
||||
it.set("of", OfFunction())
|
||||
val sonos = LuaValue.tableOf().apply {
|
||||
set("discover", DiscoverFunction())
|
||||
set("of", OfFunction())
|
||||
}
|
||||
|
||||
env.set("sonos", sonos)
|
||||
|
||||
@@ -11,9 +11,11 @@ import java.util.*
|
||||
|
||||
class TTSLibrary(private val ttsEngine: TTSEngine) : TwoArgFunction() {
|
||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||
val tts = LuaValue.tableOf()
|
||||
tts.set("say", SayMethod(ttsEngine))
|
||||
tts.set("sayToFile", FileMethod(ttsEngine))
|
||||
val tts = LuaValue.tableOf().apply {
|
||||
set("say", SayMethod(ttsEngine))
|
||||
set("sayToFile", FileMethod(ttsEngine))
|
||||
}
|
||||
|
||||
env.set("tts", tts)
|
||||
|
||||
val audioFormats = LuaValue.tableOf()
|
||||
|
||||
@@ -4,9 +4,13 @@ import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.OneArgFunction
|
||||
import org.luaj.vm2.lib.TwoArgFunction
|
||||
|
||||
class UtilLibrary : TwoArgFunction() {
|
||||
class ThreadLibrary : TwoArgFunction() {
|
||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||
env.set("sleep", SleepFunction())
|
||||
val thread = LuaValue.tableOf().apply {
|
||||
set("sleep", SleepFunction())
|
||||
}
|
||||
|
||||
env.set("thread", thread)
|
||||
|
||||
return LuaValue.NIL
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import org.luaj.vm2.lib.jse.JseOsLib
|
||||
|
||||
class SandboxFactory(
|
||||
private val configLoader: ConfigLoader,
|
||||
private val utilLibrary: UtilLibrary,
|
||||
private val threadLibrary: ThreadLibrary,
|
||||
private val serverLibrary: ServerLibrary,
|
||||
private val httpLibrary: HTTPLibrary,
|
||||
private val ttsLibrary: TTSLibrary,
|
||||
@@ -27,7 +27,7 @@ class SandboxFactory(
|
||||
it.load(StringLib())
|
||||
it.load(JseMathLib())
|
||||
it.load(JseOsLib())
|
||||
it.load(utilLibrary)
|
||||
it.load(threadLibrary)
|
||||
it.load(serverLibrary)
|
||||
it.load(httpLibrary)
|
||||
it.load(ttsLibrary)
|
||||
|
||||
@@ -11,6 +11,7 @@ enum class MimeType(val mimeType: String) {
|
||||
WMA("audio/x-ms-wma"),
|
||||
WAV("audio/x-wav"),
|
||||
FLAC("audio/x-wav"),
|
||||
TEXT("text/plain"),
|
||||
JSON("application/json");
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -27,14 +27,14 @@ class LuaModule {
|
||||
@Singleton
|
||||
fun sandboxFactory(
|
||||
configLoader: ConfigLoader,
|
||||
utilLibrary: UtilLibrary,
|
||||
threadLibrary: ThreadLibrary,
|
||||
serverLibrary: ServerLibrary,
|
||||
httpLibrary: HTTPLibrary,
|
||||
ttsLibrary: TTSLibrary,
|
||||
sonosLibrary: SonosLibrary
|
||||
) = SandboxFactory(
|
||||
configLoader,
|
||||
utilLibrary,
|
||||
threadLibrary,
|
||||
serverLibrary,
|
||||
httpLibrary,
|
||||
ttsLibrary,
|
||||
@@ -43,7 +43,7 @@ class LuaModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun utilLibrary() = UtilLibrary()
|
||||
fun utilLibrary() = ThreadLibrary()
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
||||
Reference in New Issue
Block a user