Create CacheLibrary

This commit is contained in:
2020-07-07 17:37:15 +02:00
parent 56733b285b
commit 160f4fea29
3 changed files with 36 additions and 3 deletions

View File

@@ -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) }
}
}

View File

@@ -24,7 +24,8 @@ class SandboxFactory(
private val serverLibrary: ServerLibrary,
private val httpLibrary: HTTPLibrary,
private val ttsLibrary: TTSLibrary,
private val sonosLibrary: SonosLibrary
private val sonosLibrary: SonosLibrary,
private val cacheLibrary: CacheLibrary
) {
fun createSandbox() = runBlocking {
withContext(Dispatchers.Default) {
@@ -55,6 +56,7 @@ class SandboxFactory(
sandbox.load(httpLibrary)
sandbox.load(ttsLibrary)
sandbox.load(sonosLibrary)
sandbox.load(cacheLibrary)
}
private fun install(sandbox: Globals) {

View File

@@ -35,7 +35,8 @@ class LuaModule {
serverLibrary: ServerLibrary,
httpLibrary: HTTPLibrary,
ttsLibrary: TTSLibrary,
sonosLibrary: SonosLibrary
sonosLibrary: SonosLibrary,
cacheLibrary: CacheLibrary
) = SandboxFactory(
context,
configLoader,
@@ -44,7 +45,8 @@ class LuaModule {
serverLibrary,
httpLibrary,
ttsLibrary,
sonosLibrary
sonosLibrary,
cacheLibrary
)
@Provides
@@ -72,6 +74,10 @@ class LuaModule {
@Singleton
fun sonosLibrary() = SonosLibrary()
@Provides
@Singleton
fun cacheLibrary(context: Context) = CacheLibrary(context)
@Provides
@Singleton
fun scriptsInitializer(context: Context, preferences: SharedPreferences) =