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> {
|
||||
val scripts = context.getExternalFilesDir("Endpoints")?.listFiles() ?: emptyArray()
|
||||
val scripts = context.getExternalFilesDir("endpoints")?.listFiles() ?: emptyArray()
|
||||
|
||||
return scripts.mapNotNull { loadEndpoint(it) }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.ttsserver.core.lua.sandbox
|
||||
|
||||
import com.bartlomiejpluta.ttsserver.core.lua.lib.*
|
||||
import com.bartlomiejpluta.ttsserver.core.lua.loader.ConfigLoader
|
||||
import org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.LoadState
|
||||
import org.luaj.vm2.compiler.LuaC
|
||||
@@ -12,6 +13,7 @@ import org.luaj.vm2.lib.jse.JseMathLib
|
||||
import org.luaj.vm2.lib.jse.JseOsLib
|
||||
|
||||
class SandboxFactory(
|
||||
private val configLoader: ConfigLoader,
|
||||
private val utilLibrary: UtilLibrary,
|
||||
private val serverLibrary: ServerLibrary,
|
||||
private val httpLibrary: HTTPLibrary,
|
||||
@@ -32,5 +34,6 @@ class SandboxFactory(
|
||||
it.load(sonosLibrary)
|
||||
LoadState.install(it)
|
||||
LuaC.install(it)
|
||||
configLoader.loadConfig(it)
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class DefaultEndpoint(
|
||||
private fun provideResponse(response: LuaTable) = when (response.get("data")) {
|
||||
is LuaString -> getTextResponse(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(
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.ttsserver.di.module
|
||||
|
||||
import android.content.Context
|
||||
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.sandbox.SandboxFactory
|
||||
import com.bartlomiejpluta.ttsserver.core.tts.engine.TTSEngine
|
||||
@@ -18,16 +19,27 @@ class LuaModule {
|
||||
fun endpointLoader(context: Context, sandboxFactory: SandboxFactory) =
|
||||
EndpointLoader(context, sandboxFactory)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun configLoader(context: Context) = ConfigLoader(context)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun sandboxFactory(
|
||||
configLoader: ConfigLoader,
|
||||
utilLibrary: UtilLibrary,
|
||||
serverLibrary: ServerLibrary,
|
||||
httpLibrary: HTTPLibrary,
|
||||
ttsLibrary: TTSLibrary,
|
||||
sonosLibrary: SonosLibrary
|
||||
) =
|
||||
SandboxFactory(utilLibrary, serverLibrary, httpLibrary, ttsLibrary, sonosLibrary)
|
||||
) = SandboxFactory(
|
||||
configLoader,
|
||||
utilLibrary,
|
||||
serverLibrary,
|
||||
httpLibrary,
|
||||
ttsLibrary,
|
||||
sonosLibrary
|
||||
)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
||||
Reference in New Issue
Block a user