Skip to content

Commit ddde257

Browse files
committed
Cleaned up configurations
1 parent 8281bff commit ddde257

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package com.lambda.config
1919

2020
import com.google.gson.JsonElement
21+
import com.google.gson.JsonIOException
2122
import com.google.gson.JsonObject
2223
import com.google.gson.JsonParser
24+
import com.google.gson.JsonSyntaxException
2325
import com.lambda.Lambda.LOG
2426
import com.lambda.Lambda.gson
2527
import com.lambda.config.configurations.ModuleConfig
@@ -28,10 +30,13 @@ import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
2830
import com.lambda.threading.runIO
2931
import com.lambda.util.Communication.info
3032
import com.lambda.util.Communication.logError
33+
import com.lambda.util.FileUtils.createIfNotExists
34+
import com.lambda.util.FileUtils.ifExists
35+
import com.lambda.util.FileUtils.ifNotExists
3136
import com.lambda.util.StringUtils.capitalize
3237
import java.io.File
33-
import java.time.Duration
3438
import kotlin.concurrent.fixedRateTimer
39+
import kotlin.time.Duration.Companion.minutes
3540

3641

3742
/**
@@ -58,7 +63,6 @@ abstract class Configuration : Jsonable {
5863

5964
init {
6065
listenUnsafe<ClientEvent.Startup> { tryLoad() }
61-
6266
listenUnsafe<ClientEvent.Shutdown>(Int.MIN_VALUE) { trySave() }
6367

6468
register()
@@ -69,11 +73,9 @@ abstract class Configuration : Jsonable {
6973
fixedRateTimer(
7074
daemon = true,
7175
name = "Scheduler-config-${configName}",
72-
initialDelay = Duration.ofMinutes(5).toMillis(),
73-
period = Duration.ofMinutes(5).toMillis()
74-
) {
75-
trySave()
76-
}
76+
initialDelay = 5.minutes.inWholeMilliseconds,
77+
period = 5.minutes.inWholeMilliseconds,
78+
) { trySave() }
7779

7880
configurations.add(this)
7981
}
@@ -87,71 +89,64 @@ abstract class Configuration : Jsonable {
8789

8890
override fun loadFromJson(serialized: JsonElement) {
8991
serialized.asJsonObject.entrySet().forEach { (name, value) ->
90-
configurables.find {
91-
it.name == name
92-
}?.loadFromJson(value)
92+
configurableByName(name)
93+
?.loadFromJson(value)
9394
?: LOG.warn("No matching setting found for saved setting $name with $value in ${configName.capitalize()} config")
9495
}
9596
}
9697

97-
private fun save() {
98-
with(primary) {
99-
if (exists()) copyTo(backup, true)
100-
101-
parentFile.mkdirs()
102-
writeText(gson.toJson(toJson()))
98+
fun save() = runCatching {
99+
primary.createIfNotExists {
100+
it.parentFile.mkdirs()
101+
it.writeText(gson.toJson(toJson()))
102+
it.copyTo(backup, true)
103103
}
104104
}
105105

106-
private fun load(file: File) {
107-
if (!file.exists()) {
108-
LOG.warn("No configuration file found for ${configName.capitalize()}. Creating new file when saving.")
109-
return
110-
}
111-
112-
loadFromJson(JsonParser.parseReader(file.reader()).asJsonObject)
106+
/**
107+
* Loads the config from the [file]
108+
* Encapsulates [JsonIOException] and [JsonSyntaxException] in a runCatching block
109+
*/
110+
fun load(file: File) = runCatching {
111+
file.ifNotExists { LOG.warn("No configuration file found for ${configName.capitalize()}. Creating new file when saving.") }
112+
.ifExists { loadFromJson(JsonParser.parseReader(it.reader()).asJsonObject) }
113113
}
114114

115-
fun tryLoad() {
116-
runIO {
117-
runCatching { load(primary) }
118-
.onSuccess {
119-
val message = "${configName.capitalize()} config loaded."
120-
LOG.info(message)
121-
info(message)
122-
}
123-
.onFailure {
124-
var message: String
125-
runCatching { load(backup) }
126-
.onSuccess {
127-
message = "${configName.capitalize()} config loaded from backup"
128-
LOG.info(message)
129-
info(message)
130-
}
131-
.onFailure { error ->
132-
message =
133-
"Failed to load ${configName.capitalize()} config from backup, unrecoverable error"
134-
LOG.error(message, error)
135-
logError(message)
136-
}
137-
}
138-
}
115+
fun tryLoad() = runIO {
116+
load(primary)
117+
.onSuccess {
118+
val message = "${configName.capitalize()} config loaded."
119+
LOG.info(message)
120+
info(message)
121+
}
122+
.onFailure {
123+
var message: String
124+
runCatching { load(backup) }
125+
.onSuccess {
126+
message = "${configName.capitalize()} config loaded from backup"
127+
LOG.info(message)
128+
info(message)
129+
}
130+
.onFailure { error ->
131+
message = "Failed to load ${configName.capitalize()} config from backup, unrecoverable error"
132+
LOG.error(message, error)
133+
logError(message)
134+
}
135+
}
139136
}
140137

141-
fun trySave(logToChat: Boolean = false) {
142-
runIO {
143-
runCatching { save() }
144-
.onSuccess {
145-
val message = "Saved ${configName.capitalize()} config."
146-
LOG.info(message)
147-
if (logToChat) info(message)
148-
}
149-
.onFailure {
150-
val message = "Failed to save ${configName.capitalize()} config"
151-
LOG.error(message, it)
152-
logError(message)
153-
}
154-
}
138+
fun trySave(logToChat: Boolean = false) = runIO {
139+
save()
140+
.onSuccess {
141+
val message = "Saved ${configName.capitalize()} config."
142+
LOG.info(message)
143+
if (logToChat) info(message)
144+
}
145+
.onFailure {
146+
val message = "Failed to save ${configName.capitalize()} config"
147+
LOG.error(message, it)
148+
logError(message)
149+
}
155150
}
156151

157152
companion object {

0 commit comments

Comments
 (0)