Create debug() function
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||||
|
import com.bartlomiejpluta.R
|
||||||
|
import com.bartlomiejpluta.ttsserver.ui.main.MainActivity
|
||||||
|
import org.luaj.vm2.LuaValue
|
||||||
|
import org.luaj.vm2.lib.OneArgFunction
|
||||||
|
import org.luaj.vm2.lib.TwoArgFunction
|
||||||
|
|
||||||
|
class DebugLibrary(private val context: Context) : TwoArgFunction() {
|
||||||
|
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||||
|
env.set("debug", DebugFunction(context))
|
||||||
|
|
||||||
|
return LuaValue.NIL
|
||||||
|
}
|
||||||
|
|
||||||
|
class DebugFunction(private val context: Context) : OneArgFunction() {
|
||||||
|
override fun call(arg: LuaValue): LuaValue {
|
||||||
|
LocalBroadcastManager
|
||||||
|
.getInstance(context)
|
||||||
|
.sendBroadcast(Intent(MainActivity.POPUP).apply {
|
||||||
|
putExtra(MainActivity.TITLE, context.resources.getString(R.string.debug))
|
||||||
|
putExtra(MainActivity.MESSAGE, arg.toString())
|
||||||
|
})
|
||||||
|
|
||||||
|
return LuaValue.NIL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.ttsserver.core.lua.loader
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||||
|
import com.bartlomiejpluta.R
|
||||||
import com.bartlomiejpluta.ttsserver.core.lua.sandbox.SandboxFactory
|
import com.bartlomiejpluta.ttsserver.core.lua.sandbox.SandboxFactory
|
||||||
import com.bartlomiejpluta.ttsserver.core.web.endpoint.DefaultEndpoint
|
import com.bartlomiejpluta.ttsserver.core.web.endpoint.DefaultEndpoint
|
||||||
import com.bartlomiejpluta.ttsserver.core.web.endpoint.Endpoint
|
import com.bartlomiejpluta.ttsserver.core.web.endpoint.Endpoint
|
||||||
@@ -44,8 +45,9 @@ class EndpointLoader(
|
|||||||
|
|
||||||
private fun handleError(exception: LuaError) = LocalBroadcastManager
|
private fun handleError(exception: LuaError) = LocalBroadcastManager
|
||||||
.getInstance(context)
|
.getInstance(context)
|
||||||
.sendBroadcast(Intent(MainActivity.LUA_ERROR).also {
|
.sendBroadcast(Intent(MainActivity.POPUP).apply {
|
||||||
it.putExtra(MainActivity.MESSAGE, exception.message)
|
putExtra(MainActivity.TITLE, context.resources.getString(R.string.error))
|
||||||
|
putExtra(MainActivity.MESSAGE, exception.message)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import org.luaj.vm2.lib.jse.JseOsLib
|
|||||||
class SandboxFactory(
|
class SandboxFactory(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val configLoader: ConfigLoader,
|
private val configLoader: ConfigLoader,
|
||||||
|
private val debugLibrary: DebugLibrary,
|
||||||
private val threadLibrary: ThreadLibrary,
|
private val threadLibrary: ThreadLibrary,
|
||||||
private val serverLibrary: ServerLibrary,
|
private val serverLibrary: ServerLibrary,
|
||||||
private val httpLibrary: HTTPLibrary,
|
private val httpLibrary: HTTPLibrary,
|
||||||
@@ -39,6 +40,7 @@ class SandboxFactory(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun loadApplicationLibraries(sandbox: Globals) {
|
private fun loadApplicationLibraries(sandbox: Globals) {
|
||||||
|
sandbox.load(debugLibrary)
|
||||||
sandbox.load(threadLibrary)
|
sandbox.load(threadLibrary)
|
||||||
sandbox.load(serverLibrary)
|
sandbox.load(serverLibrary)
|
||||||
sandbox.load(httpLibrary)
|
sandbox.load(httpLibrary)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.ttsserver.core.web.worker
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||||
|
import com.bartlomiejpluta.R
|
||||||
import com.bartlomiejpluta.ttsserver.core.web.dto.Request
|
import com.bartlomiejpluta.ttsserver.core.web.dto.Request
|
||||||
import com.bartlomiejpluta.ttsserver.service.foreground.ForegroundService
|
import com.bartlomiejpluta.ttsserver.service.foreground.ForegroundService
|
||||||
import com.bartlomiejpluta.ttsserver.service.state.ServiceState
|
import com.bartlomiejpluta.ttsserver.service.state.ServiceState
|
||||||
@@ -36,8 +37,9 @@ class Worker(
|
|||||||
private fun handleLuaError(exception: LuaError) {
|
private fun handleLuaError(exception: LuaError) {
|
||||||
LocalBroadcastManager
|
LocalBroadcastManager
|
||||||
.getInstance(context)
|
.getInstance(context)
|
||||||
.sendBroadcast(Intent(MainActivity.LUA_ERROR).also {
|
.sendBroadcast(Intent(MainActivity.POPUP).apply {
|
||||||
it.putExtra(MainActivity.MESSAGE, exception.message)
|
putExtra(MainActivity.TITLE, context.resources.getString(R.string.error))
|
||||||
|
putExtra(MainActivity.MESSAGE, exception.message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class LuaModule {
|
|||||||
fun sandboxFactory(
|
fun sandboxFactory(
|
||||||
context: Context,
|
context: Context,
|
||||||
configLoader: ConfigLoader,
|
configLoader: ConfigLoader,
|
||||||
|
debugLibrary: DebugLibrary,
|
||||||
threadLibrary: ThreadLibrary,
|
threadLibrary: ThreadLibrary,
|
||||||
serverLibrary: ServerLibrary,
|
serverLibrary: ServerLibrary,
|
||||||
httpLibrary: HTTPLibrary,
|
httpLibrary: HTTPLibrary,
|
||||||
@@ -38,6 +39,7 @@ class LuaModule {
|
|||||||
) = SandboxFactory(
|
) = SandboxFactory(
|
||||||
context,
|
context,
|
||||||
configLoader,
|
configLoader,
|
||||||
|
debugLibrary,
|
||||||
threadLibrary,
|
threadLibrary,
|
||||||
serverLibrary,
|
serverLibrary,
|
||||||
httpLibrary,
|
httpLibrary,
|
||||||
@@ -47,7 +49,11 @@ class LuaModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun utilLibrary() = ThreadLibrary()
|
fun debugLibrary(context: Context) = DebugLibrary(context)
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun threadLibrary() = ThreadLibrary()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class MainActivity : DaggerAppCompatActivity() {
|
|||||||
private val receiver = object : BroadcastReceiver() {
|
private val receiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) = when (intent?.action) {
|
override fun onReceive(context: Context?, intent: Intent?) = when (intent?.action) {
|
||||||
CHANGE_STATE -> dispatchChangeStateIntent(intent)
|
CHANGE_STATE -> dispatchChangeStateIntent(intent)
|
||||||
LUA_ERROR -> dispatchLuaErrorIntent(intent)
|
POPUP -> dispatchLuaErrorIntent(intent)
|
||||||
else -> throw UnsupportedOperationException("This action is not supported")
|
else -> throw UnsupportedOperationException("This action is not supported")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ class MainActivity : DaggerAppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun dispatchLuaErrorIntent(intent: Intent) = AlertDialog.Builder(this)
|
private fun dispatchLuaErrorIntent(intent: Intent) = AlertDialog.Builder(this)
|
||||||
.setTitle(R.string.error_title)
|
.setTitle(intent.getStringExtra(TITLE))
|
||||||
.setMessage(intent.getStringExtra(MESSAGE))
|
.setMessage(intent.getStringExtra(MESSAGE))
|
||||||
.setPositiveButton(android.R.string.ok) { _, _ -> }
|
.setPositiveButton(android.R.string.ok) { _, _ -> }
|
||||||
.create()
|
.create()
|
||||||
@@ -103,7 +103,7 @@ class MainActivity : DaggerAppCompatActivity() {
|
|||||||
super.onResume()
|
super.onResume()
|
||||||
val filter = IntentFilter().apply {
|
val filter = IntentFilter().apply {
|
||||||
addAction(CHANGE_STATE)
|
addAction(CHANGE_STATE)
|
||||||
addAction(LUA_ERROR)
|
addAction(POPUP)
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalBroadcastManager
|
LocalBroadcastManager
|
||||||
@@ -140,9 +140,10 @@ class MainActivity : DaggerAppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val CHANGE_STATE = "com.bartlomiejpluta.ttsserver.service.CHANGE_STATE"
|
const val CHANGE_STATE = "com.bartlomiejpluta.ttsserver.ui.main.CHANGE_STATE"
|
||||||
const val LUA_ERROR = "com.bartlomiejpluta.ttsserver.service.LUA_ERROR"
|
const val POPUP = "com.bartlomiejpluta.ttsserver.ui.main.POPUP"
|
||||||
const val MESSAGE = "message"
|
const val TITLE = "TITLE"
|
||||||
|
const val MESSAGE = "MESSAGE"
|
||||||
const val STATE = "STATE"
|
const val STATE = "STATE"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,8 @@
|
|||||||
<string name="menu_settings">Settings</string>
|
<string name="menu_settings">Settings</string>
|
||||||
<string name="menu_help">Help</string>
|
<string name="menu_help">Help</string>
|
||||||
|
|
||||||
<string name="error_title">Error</string>
|
|
||||||
<string name="error_invalid_endpoint_script">Skipping %1$s file because of error:\n%2$s</string>
|
<string name="error_invalid_endpoint_script">Skipping %1$s file because of error:\n%2$s</string>
|
||||||
|
|
||||||
|
<string name="error">Error</string>
|
||||||
|
<string name="debug">Debug</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user