Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / system / AutoSystem.kt
@Taehui Taehui on 18 Jul 2 KB v1.0-SNAPSHOT
package net.taehui.twilight.system

import net.taehui.twilight.Logger
import net.taehui.twilight.TwilightComponent
import org.apache.commons.io.FilenameUtils
import java.nio.file.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService

object AutoSystem : Logger {
    private val ses: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor {
        Executors.defaultThreadFactory().newThread(it).apply {
            isDaemon = true
        }
    }
    private val ws = FileSystems.getDefault().newWatchService()

    fun handleSystem() {
        logFuture {
            ws.use {
                TwilightComponent.SYSTEM_ENTRY_PATH.register(ws, StandardWatchEventKinds.ENTRY_MODIFY)
                TwilightComponent.DEFAULT_NOTE_ENTRY.register(ws, StandardWatchEventKinds.ENTRY_MODIFY)
                TwilightComponent.DEFAULT_UI_ENTRY.register(ws, StandardWatchEventKinds.ENTRY_MODIFY)
                while (true) {
                    val tws = ws.take()
                    tws.pollEvents().forEach {
                        val filePath = (tws.watchable() as Path).resolve(it.context() as Path)
                        val fileName = filePath.fileName.toString()
                        when (filePath.parent.fileName.toString()) {
                            "Note", "UI" -> {
                                if (FilenameUtils.isExtension(fileName, "zip") || FilenameUtils.isExtension(
                                        fileName,
                                        "rar"
                                    )
                                ) {
                                    Configure.loadDefaultDate()
                                }
                            }

                            "System" -> {
                                when (fileName) {
                                    "Awilight.py" -> AwilightHandler.loadAwilight()
                                }
                            }
                        }
                    }
                    tws.reset()
                }
            }
        }
    }
}