Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs committed Nov 6, 2023
1 parent f6f7f64 commit 801377d
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,24 @@ class ConfigManager {
}

fun saveConfig(reason: String) {
saveFile(configFile, "config", SkyHanniMod.feature, reason)
}

fun saveSackData(reason: String) {
saveFile(sackFile, "sacks", SkyHanniMod.sackData, reason)
}

private fun saveFile(file: File?, fileName: String, data: Any, reason: String) {
if (disableSaving) return
logger.log("saveConfig: $reason")
val file = configFile ?: throw Error("Can not save config, configFile is null!")
if (file == null) throw Error("Can not save $fileName, ${fileName}File is null!")
try {
logger.log("Saving config file")
logger.log("Saving $fileName file")
file.parentFile.mkdirs()
val unit = file.parentFile.resolve("config.json.write")
val unit = file.parentFile.resolve("$fileName.json.write")
unit.createNewFile()
BufferedWriter(OutputStreamWriter(FileOutputStream(unit), StandardCharsets.UTF_8)).use { writer ->
// TODO remove old "hidden" area
writer.write(gson.toJson(SkyHanniMod.feature))
writer.write(gson.toJson(data))
}
// Perform move — which is atomic, unlike writing — after writing is done.
Files.move(
Expand All @@ -212,24 +219,7 @@ class ConfigManager {
StandardCopyOption.ATOMIC_MOVE
)
} catch (e: IOException) {
logger.log("Could not save config file to $file")
e.printStackTrace()
}
}

fun saveSackData(reason: String) {
if (disableSaving) return
logger.log("saveSackData: $reason")
val file = sackFile ?: throw Error("Can not save sacks, sackFile is null!")
try {
logger.log("Saving sack file")
file.parentFile.mkdirs()
file.createNewFile()
BufferedWriter(OutputStreamWriter(FileOutputStream(file), StandardCharsets.UTF_8)).use { writer ->
writer.write(gson.toJson(SkyHanniMod.sackData))
}
} catch (e: IOException) {
logger.log("Could not save sacks file to $file")
logger.log("Could not save $fileName file to $file")
e.printStackTrace()
}
}
Expand Down

0 comments on commit 801377d

Please sign in to comment.