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 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) {
|
||||
|
||||
@@ -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) =
|
||||
|
||||
Reference in New Issue
Block a user