Add support for global config.lua file
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
package com.bartlomiejpluta.ttsserver.core.lua.loader
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import org.luaj.vm2.Globals
|
||||||
|
import org.luaj.vm2.LuaTable
|
||||||
|
import org.luaj.vm2.LuaValue
|
||||||
|
import org.luaj.vm2.lib.TwoArgFunction
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class ConfigLoader(context: Context) {
|
||||||
|
private val configDirectory = context.getExternalFilesDir("config")
|
||||||
|
|
||||||
|
fun loadConfig(env: Globals) {
|
||||||
|
val configFile = File(configDirectory, "config.lua")
|
||||||
|
val table = env.loadfile(configFile.absolutePath).call().checktable()
|
||||||
|
env.load(ConfigLibrary(table))
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConfigLibrary(private val table: LuaTable) : TwoArgFunction() {
|
||||||
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
|
env.set("config", table)
|
||||||
|
return LuaValue.NIL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ class EndpointLoader(
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
fun loadEndpoints(): List<Endpoint> {
|
fun loadEndpoints(): List<Endpoint> {
|
||||||
val scripts = context.getExternalFilesDir("Endpoints")?.listFiles() ?: emptyArray()
|
val scripts = context.getExternalFilesDir("endpoints")?.listFiles() ?: emptyArray()
|
||||||
|
|
||||||
return scripts.mapNotNull { loadEndpoint(it) }
|
return scripts.mapNotNull { loadEndpoint(it) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.ttsserver.core.lua.sandbox
|
package com.bartlomiejpluta.ttsserver.core.lua.sandbox
|
||||||
|
|
||||||
import com.bartlomiejpluta.ttsserver.core.lua.lib.*
|
import com.bartlomiejpluta.ttsserver.core.lua.lib.*
|
||||||
|
import com.bartlomiejpluta.ttsserver.core.lua.loader.ConfigLoader
|
||||||
import org.luaj.vm2.Globals
|
import org.luaj.vm2.Globals
|
||||||
import org.luaj.vm2.LoadState
|
import org.luaj.vm2.LoadState
|
||||||
import org.luaj.vm2.compiler.LuaC
|
import org.luaj.vm2.compiler.LuaC
|
||||||
@@ -12,6 +13,7 @@ import org.luaj.vm2.lib.jse.JseMathLib
|
|||||||
import org.luaj.vm2.lib.jse.JseOsLib
|
import org.luaj.vm2.lib.jse.JseOsLib
|
||||||
|
|
||||||
class SandboxFactory(
|
class SandboxFactory(
|
||||||
|
private val configLoader: ConfigLoader,
|
||||||
private val utilLibrary: UtilLibrary,
|
private val utilLibrary: UtilLibrary,
|
||||||
private val serverLibrary: ServerLibrary,
|
private val serverLibrary: ServerLibrary,
|
||||||
private val httpLibrary: HTTPLibrary,
|
private val httpLibrary: HTTPLibrary,
|
||||||
@@ -32,5 +34,6 @@ class SandboxFactory(
|
|||||||
it.load(sonosLibrary)
|
it.load(sonosLibrary)
|
||||||
LoadState.install(it)
|
LoadState.install(it)
|
||||||
LuaC.install(it)
|
LuaC.install(it)
|
||||||
|
configLoader.loadConfig(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ class DefaultEndpoint(
|
|||||||
private fun provideResponse(response: LuaTable) = when (response.get("data")) {
|
private fun provideResponse(response: LuaTable) = when (response.get("data")) {
|
||||||
is LuaString -> getTextResponse(response)
|
is LuaString -> getTextResponse(response)
|
||||||
is LuaUserdata -> getFileResponse(response)
|
is LuaUserdata -> getFileResponse(response)
|
||||||
else -> throw IllegalArgumentException("Supported only string and file data types")
|
else -> throw LuaError("Supported only string and file response data types")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTextResponse(response: LuaTable) = newFixedLengthResponse(
|
private fun getTextResponse(response: LuaTable) = newFixedLengthResponse(
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.ttsserver.di.module
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.bartlomiejpluta.ttsserver.core.lua.lib.*
|
import com.bartlomiejpluta.ttsserver.core.lua.lib.*
|
||||||
|
import com.bartlomiejpluta.ttsserver.core.lua.loader.ConfigLoader
|
||||||
import com.bartlomiejpluta.ttsserver.core.lua.loader.EndpointLoader
|
import com.bartlomiejpluta.ttsserver.core.lua.loader.EndpointLoader
|
||||||
import com.bartlomiejpluta.ttsserver.core.lua.sandbox.SandboxFactory
|
import com.bartlomiejpluta.ttsserver.core.lua.sandbox.SandboxFactory
|
||||||
import com.bartlomiejpluta.ttsserver.core.tts.engine.TTSEngine
|
import com.bartlomiejpluta.ttsserver.core.tts.engine.TTSEngine
|
||||||
@@ -18,16 +19,27 @@ class LuaModule {
|
|||||||
fun endpointLoader(context: Context, sandboxFactory: SandboxFactory) =
|
fun endpointLoader(context: Context, sandboxFactory: SandboxFactory) =
|
||||||
EndpointLoader(context, sandboxFactory)
|
EndpointLoader(context, sandboxFactory)
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun configLoader(context: Context) = ConfigLoader(context)
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun sandboxFactory(
|
fun sandboxFactory(
|
||||||
|
configLoader: ConfigLoader,
|
||||||
utilLibrary: UtilLibrary,
|
utilLibrary: UtilLibrary,
|
||||||
serverLibrary: ServerLibrary,
|
serverLibrary: ServerLibrary,
|
||||||
httpLibrary: HTTPLibrary,
|
httpLibrary: HTTPLibrary,
|
||||||
ttsLibrary: TTSLibrary,
|
ttsLibrary: TTSLibrary,
|
||||||
sonosLibrary: SonosLibrary
|
sonosLibrary: SonosLibrary
|
||||||
) =
|
) = SandboxFactory(
|
||||||
SandboxFactory(utilLibrary, serverLibrary, httpLibrary, ttsLibrary, sonosLibrary)
|
configLoader,
|
||||||
|
utilLibrary,
|
||||||
|
serverLibrary,
|
||||||
|
httpLibrary,
|
||||||
|
ttsLibrary,
|
||||||
|
sonosLibrary
|
||||||
|
)
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
Reference in New Issue
Block a user