Create CacheLibrary
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import org.luaj.vm2.LuaValue
|
||||||
|
import org.luaj.vm2.lib.OneArgFunction
|
||||||
|
import org.luaj.vm2.lib.TwoArgFunction
|
||||||
|
import org.luaj.vm2.lib.jse.CoerceJavaToLua
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class CacheLibrary(private val context: Context) : TwoArgFunction() {
|
||||||
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
|
val cache = LuaValue.tableOf().apply {
|
||||||
|
set("file", FileFunction(context))
|
||||||
|
}
|
||||||
|
|
||||||
|
env.set("cache", cache)
|
||||||
|
|
||||||
|
return LuaValue.NIL
|
||||||
|
}
|
||||||
|
|
||||||
|
class FileFunction(private val context: Context) : OneArgFunction() {
|
||||||
|
override fun call(name: LuaValue) = File(context.cacheDir, name.checkjstring())
|
||||||
|
.let { CoerceJavaToLua.coerce(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,8 @@ class SandboxFactory(
|
|||||||
private val serverLibrary: ServerLibrary,
|
private val serverLibrary: ServerLibrary,
|
||||||
private val httpLibrary: HTTPLibrary,
|
private val httpLibrary: HTTPLibrary,
|
||||||
private val ttsLibrary: TTSLibrary,
|
private val ttsLibrary: TTSLibrary,
|
||||||
private val sonosLibrary: SonosLibrary
|
private val sonosLibrary: SonosLibrary,
|
||||||
|
private val cacheLibrary: CacheLibrary
|
||||||
) {
|
) {
|
||||||
fun createSandbox() = runBlocking {
|
fun createSandbox() = runBlocking {
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
@@ -55,6 +56,7 @@ class SandboxFactory(
|
|||||||
sandbox.load(httpLibrary)
|
sandbox.load(httpLibrary)
|
||||||
sandbox.load(ttsLibrary)
|
sandbox.load(ttsLibrary)
|
||||||
sandbox.load(sonosLibrary)
|
sandbox.load(sonosLibrary)
|
||||||
|
sandbox.load(cacheLibrary)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun install(sandbox: Globals) {
|
private fun install(sandbox: Globals) {
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ class LuaModule {
|
|||||||
serverLibrary: ServerLibrary,
|
serverLibrary: ServerLibrary,
|
||||||
httpLibrary: HTTPLibrary,
|
httpLibrary: HTTPLibrary,
|
||||||
ttsLibrary: TTSLibrary,
|
ttsLibrary: TTSLibrary,
|
||||||
sonosLibrary: SonosLibrary
|
sonosLibrary: SonosLibrary,
|
||||||
|
cacheLibrary: CacheLibrary
|
||||||
) = SandboxFactory(
|
) = SandboxFactory(
|
||||||
context,
|
context,
|
||||||
configLoader,
|
configLoader,
|
||||||
@@ -44,7 +45,8 @@ class LuaModule {
|
|||||||
serverLibrary,
|
serverLibrary,
|
||||||
httpLibrary,
|
httpLibrary,
|
||||||
ttsLibrary,
|
ttsLibrary,
|
||||||
sonosLibrary
|
sonosLibrary,
|
||||||
|
cacheLibrary
|
||||||
)
|
)
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@@ -72,6 +74,10 @@ class LuaModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
fun sonosLibrary() = SonosLibrary()
|
fun sonosLibrary() = SonosLibrary()
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun cacheLibrary(context: Context) = CacheLibrary(context)
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun scriptsInitializer(context: Context, preferences: SharedPreferences) =
|
fun scriptsInitializer(context: Context, preferences: SharedPreferences) =
|
||||||
|
|||||||
Reference in New Issue
Block a user