-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
172 additions
and
130 deletions.
There are no files selected for viewing
72 changes: 55 additions & 17 deletions
72
src/main/kotlin/jp/zero_x_d/workaholic/merltlreader/MerLTLReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,75 @@ | ||
package jp.zero_x_d.workaholic.merltlreader | ||
|
||
import jp.zero_x_d.workaholic.merltlreader.app.CLIApp | ||
import jp.zero_x_d.workaholic.merltlreader.app.ILTLReaderApp | ||
import com.sys1yagi.mastodon4j.api.entity.Status | ||
import javafx.application.Application | ||
import jp.zero_x_d.workaholic.merltlreader.app.guifallback.GUIApp | ||
import jp.zero_x_d.workaholic.merltlreader.engine.ITTSEngine | ||
import jp.zero_x_d.workaholic.merltlreader.status.readContent | ||
import jp.zero_x_d.workaholic.merltlreader.status.readName | ||
import net.harawata.appdirs.AppDirsFactory | ||
import org.luaj.vm2.LuaError | ||
import java.io.File | ||
|
||
|
||
object MerLTLReader { | ||
val appName = "MerLTLReader" | ||
private val appVersion = "alpha" | ||
private val appAuthor = "called.0xd" | ||
private val appAuthor = "called_D" | ||
|
||
private var instance: ILTLReaderApp? = null | ||
val appInstance: ILTLReaderApp | ||
get() = requireNotNull(instance) | ||
val appDir by lazy { | ||
AppDirsFactory | ||
.getInstance() | ||
.getUserConfigDir(appName, appVersion, appAuthor) | ||
.let { | ||
File(it).apply { if (!exists()) mkdirs() } | ||
} | ||
} | ||
|
||
val preferences: Preferences | ||
get() = requireNotNull(appInstance?.preferences) | ||
var preferences: Preferences? = null | ||
|
||
val onStatus = mutableListOf<(Status) -> Unit>({ status -> | ||
println(status.readContent) | ||
}) | ||
|
||
val appDir by lazy { | ||
AppDirsFactory.getInstance().getUserConfigDir(appName, appVersion, appAuthor).let { | ||
File(it).apply { if (!exists()) mkdirs() } | ||
@JvmStatic fun main(args: Array<String>) { | ||
preferences = Preferences.fromArgs(*args) | ||
if (System.console() == null) { | ||
Application.launch(GUIApp::class.java, *args) | ||
} else { | ||
launch(*args) | ||
} | ||
} | ||
|
||
@JvmStatic fun main(arg: Array<String>) { | ||
instance = if (System.console() != null) { | ||
CLIApp() | ||
} else { // Fallback code | ||
GUIApp() | ||
private fun launch(vararg arg: String) { | ||
val credentials = | ||
Credentials.Builder(preferences!!) | ||
.loadOrAppRegister().loadOrLogin { | ||
mailPass = login(preferences) | ||
} | ||
val tl = Timeline(credentials) | ||
val tts = ITTSEngine.getEngineFromPreferences(preferences!!) | ||
|
||
runWithTTS(tl, tts) | ||
} | ||
|
||
fun runWithTTS(tl: Timeline, tts: ITTSEngine) { | ||
for (status in tl) { | ||
try { | ||
onStatus.forEach { it(status) } | ||
status.readName?.let { tts.say_username(it) } | ||
status.readContent?.let { tts.say_content(it) } | ||
} catch (e: LuaError) { | ||
e.printStackTrace() | ||
tts.say("るあえらーが発生しました") | ||
} | ||
} | ||
instance!!.launch(*arg) | ||
} | ||
|
||
private fun login(preferences: Preferences): LoginData { | ||
print("login for " + preferences.instance_url) | ||
val console = requireNotNull(System.console()) | ||
val mail = console.readLine("mail: ")?.toString() | ||
val pass = console.readPassword("password for %s: ", mail)?.toString() | ||
return LoginData(requireNotNull(mail), requireNotNull(pass)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
src/main/kotlin/jp/zero_x_d/workaholic/merltlreader/app/CLIApp.kt
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/main/kotlin/jp/zero_x_d/workaholic/merltlreader/app/ILTLReaderApp.kt
This file was deleted.
Oops, something went wrong.
76 changes: 34 additions & 42 deletions
76
src/main/kotlin/jp/zero_x_d/workaholic/merltlreader/app/guifallback/GUIApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,53 @@ | ||
package jp.zero_x_d.workaholic.merltlreader.app.guifallback | ||
|
||
import com.sys1yagi.mastodon4j.api.entity.Status | ||
import javafx.application.Application | ||
import javafx.application.Platform | ||
import javafx.scene.Scene | ||
import javafx.scene.control.TextArea | ||
import javafx.scene.layout.StackPane | ||
import javafx.stage.Stage | ||
import jp.zero_x_d.workaholic.merltlreader.LoginData | ||
import jp.zero_x_d.workaholic.merltlreader.MerLTLReader | ||
import jp.zero_x_d.workaholic.merltlreader.Preferences | ||
import jp.zero_x_d.workaholic.merltlreader.app.ILTLReaderApp | ||
import jp.zero_x_d.workaholic.merltlreader.* | ||
import jp.zero_x_d.workaholic.merltlreader.engine.ITTSEngine | ||
import jp.zero_x_d.workaholic.merltlreader.status.readContent | ||
import kotlin.concurrent.thread | ||
|
||
class GUIApp: Application() { | ||
override fun start(primaryStage: Stage) { | ||
Platform.setImplicitExit(true) | ||
val root = StackPane() | ||
val scene = Scene(root, 640.0, 480.0) | ||
var logArea = TextArea().apply { | ||
textProperty().addListener { _ -> | ||
scrollTop = Double.MAX_VALUE | ||
} | ||
} | ||
|
||
private var logArea: TextArea? = null | ||
MerLTLReader.onStatus += { status -> | ||
logArea?.appendText(status.readContent + "\n") | ||
} | ||
|
||
class GUIApp: ILTLReaderApp { | ||
override var preferences: Preferences? = null | ||
root.children.add(logArea) | ||
primaryStage.scene = scene | ||
|
||
override fun login(): LoginData { | ||
val dialog = LoginDialog(MerLTLReader.preferences) | ||
val result = dialog.showAndWait() | ||
return requireNotNull(result) | ||
} | ||
|
||
override fun launch(vararg args: String) { | ||
Application.launch(FXApp::class.java, *args) | ||
} | ||
primaryStage.setOnCloseRequest { e -> | ||
Platform.exit() | ||
println("close") | ||
// TODO あとできちんとInterruptする | ||
System.exit(0) | ||
} | ||
primaryStage.show() | ||
|
||
class FXApp: Application() { | ||
override fun start(primaryStage: Stage) { | ||
val root = StackPane() | ||
val scene = Scene(root, 640.0, 480.0) | ||
logArea = TextArea().apply { | ||
textProperty().addListener { _ -> | ||
scrollTop = Double.MAX_VALUE | ||
val credentials = | ||
Credentials.Builder(MerLTLReader.preferences!!) | ||
.loadOrAppRegister().loadOrLogin { | ||
val dialog = LoginDialog(preferences) | ||
mailPass = requireNotNull(dialog.showAndWait()) | ||
} | ||
} | ||
root.children.add(logArea) | ||
primaryStage.scene = scene | ||
thread { | ||
MerLTLReader.appInstance.run(*parameters.raw.toTypedArray()) | ||
} | ||
primaryStage.setOnCloseRequest { e -> | ||
Platform.exit() | ||
println("close") | ||
// TODO あとできちんとInterruptする | ||
System.exit(0) | ||
} | ||
primaryStage.show() | ||
} | ||
} | ||
|
||
override fun onStatus(stats: Status) { | ||
logArea?.appendText(stats.readContent + "\n") | ||
println(stats.readContent) | ||
thread { | ||
val tl = Timeline(credentials) | ||
val tts = ITTSEngine.getEngineFromPreferences(MerLTLReader.preferences!!) | ||
MerLTLReader.runWithTTS(tl, tts) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/jp/zero_x_d/workaholic/merltlreader/engine/BouyomiConnecter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/jp/zero_x_d/workaholic/merltlreader/engine/ITTSEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,44 @@ | ||
package jp.zero_x_d.workaholic.merltlreader.engine | ||
|
||
import jp.zero_x_d.workaholic.merltlreader.Preferences | ||
|
||
/** | ||
* Created by D on 17/06/20. | ||
*/ | ||
interface ITTSEngine { | ||
fun test(): Boolean | ||
fun say(readtext: String) | ||
fun say_username(username: String) | ||
fun say_content(content: String) | ||
|
||
enum class Engines { | ||
JTALK, | ||
BOUYOMI, | ||
AUTO | ||
} | ||
|
||
private object SilentEngine: ITTSEngine { | ||
override fun test(): Boolean = true | ||
override fun say(readtext: String) {} | ||
override fun say_username(username: String) {} | ||
override fun say_content(content: String) {} | ||
} | ||
|
||
companion object { | ||
fun getEngineFromPreferences(preferences: Preferences): ITTSEngine { | ||
return when(preferences.tts_engine) { | ||
ITTSEngine.Engines.JTALK -> JTalkConnecter(emptyMap()) | ||
ITTSEngine.Engines.BOUYOMI -> BouyomiConnecter(emptyMap()) | ||
Engines.AUTO -> autoSelect() | ||
} | ||
} | ||
|
||
private fun autoSelect(): ITTSEngine { | ||
return listOf( | ||
JTalkConnecter(emptyMap()), | ||
BouyomiConnecter(emptyMap()), | ||
SilentEngine | ||
).filter { it.test() }.first() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.