diff --git a/src/main/kotlin/DataStore.kt b/src/main/kotlin/DataStore.kt new file mode 100644 index 0000000..91613a3 --- /dev/null +++ b/src/main/kotlin/DataStore.kt @@ -0,0 +1,39 @@ +import org.json.simple.JSONObject +import org.json.simple.parser.JSONParser +import java.io.File +import java.io.FileReader +import java.io.FileWriter +import java.lang.Exception + +object DataStore { + val savePath = File(System.getProperty("user.home") + File.separator + ".thanos_tool") + + var LastConfigPath: String = System.getProperty("user.home") + + fun parse() { + if(!savePath.exists()){ + savePath.mkdirs() + return + } + try { + val reader = FileReader(savePath.toString() + File.separator + "settings.json") + val parser = JSONParser().parse(reader) as JSONObject + + if (parser.containsKey("lastConfigPath")) { + LastConfigPath = parser["lastConfigPath"].toString() + } + } catch (ignored: Exception) {} + } + + fun save() { + val obj = JSONObject() + obj["lastConfigPath"] = LastConfigPath + Logger.logInfo("Saving user preferences...") + FileWriter(savePath.toString() + File.separator + "settings.json").use { + it.write(obj.toJSONString()) + it.flush() + it.close() + Logger.logInfo("User preferences saved!") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/MainScreen.kt b/src/main/kotlin/MainScreen.kt index 65f9432..4802aa8 100644 --- a/src/main/kotlin/MainScreen.kt +++ b/src/main/kotlin/MainScreen.kt @@ -9,7 +9,7 @@ import javax.swing.* import javax.swing.table.DefaultTableModel object MainScreen : JFrame("RS09 Thanos Tool") { - val dirChooser = object : JFileChooser(){ + val dirChooser = object : JFileChooser(DataStore.LastConfigPath){ override fun updateUI() { EditorConstants.updateTheme() super.updateUI() @@ -53,6 +53,7 @@ object MainScreen : JFrame("RS09 Thanos Tool") { showLoaded() if(loadedModel.rowCount > 0) { selectDir.isEnabled = false + DataStore.LastConfigPath = EditorConstants.CONFIG_PATH } } } @@ -142,5 +143,9 @@ fun showLoaded(){ } fun main() { + DataStore.parse() + Runtime.getRuntime().addShutdownHook(Thread { + DataStore.save() + }) MainScreen.isVisible = true } \ No newline at end of file