EtaOrionFabric2/src/main/java/net/moonleay/etaorion/config/AutoTextConfig.kt

97 lines
3.1 KiB
Kotlin

package net.moonleay.etaorion.config
import net.moonleay.etaorion.client.autotext.AutoText
import net.moonleay.etaorion.client.autotext.KeyBindingManager
import net.moonleay.etaorion.client.modules.component.MovableModule
import org.apache.logging.log4j.LogManager
import java.io.*
import java.util.*
object AutoTextConfig {
private val lgr = LogManager.getLogger(AutoTextConfig::class.java)
private const val basePath = "./config/EtaOrionFabric/AutoTexts/"
fun delete(index: Int){
val baseDir = File(basePath)
if (!baseDir.exists()) {
lgr.info("No AT folder found.")
return
}
var textFile: File
textFile = File(baseDir, "${index}.EOF")
if(!textFile.exists()){
lgr.info("No AT # ${index} found. No need to delete.")
return
}
textFile.delete()
}
fun load(index: Int): AutoText? {
val baseDir = File(basePath)
if (!baseDir.exists()) {
lgr.info("No AT folder found.")
return null
}
var textFile: File
var input: InputStream?
var prop: Properties
textFile = File(baseDir, "${index}.EOF")
if (!textFile.exists()) {
lgr.info("No AT # ${index} found. No need to load.")
return null
}
lgr.info("Loading AT # ${index}, file: ${textFile.path}")
try {
input = FileInputStream(textFile)
prop = Properties()
prop.load(input)
val keyBinding = KeyBindingManager.bookWithIndex(index)?: KeyBindingManager.bookKeyBinding()
if(keyBinding == null){
lgr.info("Shit. No free keybinding found for AT # ${index}")
return null
}
// Resort to a new keybinding if the old one is in use, return null if there is no free keybinding
lgr.info("Loaded AutoText #${index}")
return AutoText(prop.getProperty("text"), keyBinding)
} catch (e: IOException) {
throw RuntimeException(e)
}
}
fun save(at: AutoText) {
val baseDir = File(basePath)
if (!baseDir.exists()) this.createFolder()
var textFile: File
var output: OutputStream?
var prop: Properties
textFile = File(baseDir, at.eoKeyBinding.index.toString() + ".EOF")
if (!textFile.exists()) {
try {
textFile.createNewFile()
} catch (e: IOException) {
throw RuntimeException(e)
}
}
lgr.info("Saving AT # ${at.eoKeyBinding.index}, File: " + textFile)
try {
output = FileOutputStream(textFile)
prop = Properties()
prop.setProperty("text", at.textToSend)
prop.store(output, null)
lgr.info("Saved Module Settings")
} catch (e: IOException) {
throw RuntimeException(e)
}
}
private fun createFolder(){
val baseDir = File(basePath)
if (!baseDir.exists()) baseDir.mkdirs()
}
fun saveTemplate(int: Int){
}
}