aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-03-29 17:10:01 +0200
committernea <nea@nea.moe>2023-03-29 17:10:01 +0200
commited2dfa0bfd7a6d69a2eb7349009625991e363fee (patch)
treee103dbfb8fbe0ad9c547c57d855a2ad60ca9d1df /src/main
parent3948531e6098fdb44082312ffb461536f4c522f9 (diff)
downloadskyhanni-ed2dfa0bfd7a6d69a2eb7349009625991e363fee.tar.gz
skyhanni-ed2dfa0bfd7a6d69a2eb7349009625991e363fee.tar.bz2
skyhanni-ed2dfa0bfd7a6d69a2eb7349009625991e363fee.zip
config backups and better property adapter
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java4
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt37
2 files changed, 33 insertions, 8 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
index f3909ebce..782ae6d4a 100644
--- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
+++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
@@ -84,6 +84,9 @@ public class SkyHanniMod {
public static RepoManager repo;
public static ConfigManager configManager;
private static Logger logger;
+ public static org.slf4j.Logger getLogger(String name) {
+ return org.slf4j.LoggerFactory.getLogger("SkyHanni." + name);
+ }
public static List<Object> modules = new ArrayList<>();
public static Job globalJob = JobKt.Job(null);
@@ -241,7 +244,6 @@ public class SkyHanniMod {
configManager = new ConfigManager();
configManager.firstLoad();
- new FirstConfigLoadedEvent().postAndCatch();
MinecraftConsoleFilter.Companion.initLogging();
diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
index e3b040391..75e31ab2a 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
@@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.config
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import com.google.gson.GsonBuilder
+import io.github.moulberry.moulconfig.observer.PropertyTypeAdapterFactory
import io.github.moulberry.moulconfig.processor.BuiltinMoulConfigGuis
import io.github.moulberry.moulconfig.processor.ConfigProcessorDriver
import io.github.moulberry.moulconfig.processor.MoulConfigProcessor
@@ -11,9 +12,14 @@ import java.nio.charset.StandardCharsets
class ConfigManager {
companion object {
- val gson = GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create()
+ val gson = GsonBuilder().setPrettyPrinting()
+ .excludeFieldsWithoutExposeAnnotation()
+ .registerTypeAdapterFactory(PropertyTypeAdapterFactory())
+ .create()
}
+ val logger = SkyHanniMod.getLogger("ConfigManager")
+
var configDirectory = File("config/skyhanni")
private var configFile: File? = null
lateinit var processor: MoulConfigProcessor<Features>
@@ -26,23 +32,39 @@ class ConfigManager {
configFile = File(configDirectory, "config.json")
+ logger.info("Trying to load config from $configFile")
+
if (configFile!!.exists()) {
try {
BufferedReader(InputStreamReader(FileInputStream(configFile!!), StandardCharsets.UTF_8)).use { reader ->
- SkyHanniMod.feature = gson.fromJson(reader,
- Features::class.java)
+ SkyHanniMod.feature = gson.fromJson(
+ reader,
+ Features::class.java
+ )
}
- ConfigLoadEvent().postAndCatch()
+ logger.info("Loaded config from file")
} catch (e: Exception) {
- e.printStackTrace()
+ val backupFile = configFile!!.resolveSibling("config-${System.currentTimeMillis()}-backup.json")
+ logger.error(
+ "Exception while reading $configFile. Will load blank config and save backup to $backupFile",
+ e
+ )
+ try {
+ configFile!!.copyTo(backupFile)
+ } catch (e: Exception) {
+ logger.error("Could not create backup for config file", e)
+ }
}
}
if (SkyHanniMod.feature == null) {
+ logger.info("Creating blank config and saving to file")
SkyHanniMod.feature = Features()
saveConfig()
}
+ ConfigLoadEvent().postAndCatch()
+
val features = SkyHanniMod.feature
processor = MoulConfigProcessor(SkyHanniMod.feature)
BuiltinMoulConfigGuis.addProcessors(processor)
@@ -51,17 +73,18 @@ class ConfigManager {
features,
processor
)
- SkyHanniMod.DEPENDENCIES
}
fun saveConfig() {
try {
+ logger.info("Saving config file")
+ configFile!!.parentFile.mkdirs()
configFile!!.createNewFile()
BufferedWriter(OutputStreamWriter(FileOutputStream(configFile!!), StandardCharsets.UTF_8)).use { writer ->
writer.write(gson.toJson(SkyHanniMod.feature))
}
} catch (e: IOException) {
- e.printStackTrace()
+ logger.error("Could not save config file to $configFile", e)
}
}
} \ No newline at end of file