From 802bc501023a3a95f7d4ed6dafab9d19439e7ac1 Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Tue, 17 Nov 2020 01:32:34 -0500 Subject: [PATCH] Config hotloading --- src/main/java/com/volmit/iris/Iris.java | 15 +++- .../java/com/volmit/iris/IrisSettings.java | 84 ++++++++++++------- 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/volmit/iris/Iris.java b/src/main/java/com/volmit/iris/Iris.java index f9c36fb12..8d58029b4 100644 --- a/src/main/java/com/volmit/iris/Iris.java +++ b/src/main/java/com/volmit/iris/Iris.java @@ -1,6 +1,5 @@ package com.volmit.iris; -import com.volmit.iris.nms.INMS; import com.volmit.iris.manager.*; import com.volmit.iris.manager.command.CommandIris; import com.volmit.iris.manager.command.PermissionIris; @@ -8,6 +7,7 @@ import com.volmit.iris.manager.link.BKLink; import com.volmit.iris.manager.link.CitizensLink; import com.volmit.iris.manager.link.MultiverseCoreLink; import com.volmit.iris.manager.link.MythicMobsLink; +import com.volmit.iris.nms.INMS; import com.volmit.iris.object.IrisCompat; import com.volmit.iris.scaffold.IrisWorldCreator; import com.volmit.iris.scaffold.IrisWorlds; @@ -47,6 +47,7 @@ public class Iris extends VolmitPlugin public static boolean biome3d = doesSupport3DBiomes(); public static boolean lowMemoryMode = false; public static IrisCompat compat; + public static FileWatcher configWatcher; @Permission public static PermissionIris perm; @@ -158,14 +159,26 @@ public class Iris extends VolmitPlugin linkBK = new BKLink(); linkMythicMobs = new MythicMobsLink(); edit = new EditManager(); + configWatcher = new FileWatcher(getDataFile("settings.json")); J.a(() -> IO.delete(getTemp())); J.a(this::bstats); J.s(this::splash, 20); J.sr(this::tickQueue, 0); + J.ar(this::checkConfigHotload, 50); PaperLib.suggestPaper(this); super.onEnable(); } + private void checkConfigHotload() { + if(configWatcher.checkModified()) + { + IrisSettings.invalidate(); + IrisSettings.get(); + configWatcher.checkModified(); + Iris.info("Hotloaded settings.json"); + } + } + public void onDisable() { if(IrisSettings.get().isStudio()) diff --git a/src/main/java/com/volmit/iris/IrisSettings.java b/src/main/java/com/volmit/iris/IrisSettings.java index d525347e7..924d8d4a7 100644 --- a/src/main/java/com/volmit/iris/IrisSettings.java +++ b/src/main/java/com/volmit/iris/IrisSettings.java @@ -109,49 +109,73 @@ public class IrisSettings J.a(() -> { - JSONObject j = new JSONObject(ss); - boolean u = false; - for(String i : def.keySet()) + try { - if(!j.has(i)) + JSONObject j = new JSONObject(ss); + boolean u = false; + for(String i : def.keySet()) { - u = true; - j.put(i, def.get(i)); - Iris.warn("Adding new config key: " + i); + if(!j.has(i)) + { + u = true; + j.put(i, def.get(i)); + Iris.warn("Adding new config key: " + i); + } + } + + for(String i : new KSet<>(j.keySet())) + { + if(!def.has(i)) + { + u = true; + j.remove(i); + Iris.warn("Removing unused config key: " + i); + } + } + + if(u) + { + try + { + IO.writeAll(s, j.toString(4)); + Iris.info("Updated Configuration Files"); + } + + catch(Throwable e) + { + e.printStackTrace(); + } } } - for(String i : j.keySet()) + catch(Throwable ee) { - if(!def.has(i)) - { - u = true; - j.remove(i); - Iris.warn("Removing unused config key: " + i); - } - } - - if(u) - { - try - { - IO.writeAll(s, j.toString(4)); - Iris.info("Updated Configuration Files"); - } - - catch(Throwable ignored) - { + Iris.error("Configuration Error in settings.json! " + ee.getClass().getSimpleName() + ": " + ee.getMessage()); + Iris.warn("Attempting to fix configuration while retaining valid in-memory settings..."); + try { + IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); + Iris.info("Configuration Fixed!"); + } catch (IOException e) { + e.printStackTrace(); + Iris.error("ERROR! CONFIGURATION IMPOSSIBLE TO READ! Using an unmodifiable configuration from memory. Please delete the settings.json at some point to try to restore configurability!"); } } }); } - catch(JSONException | IOException e) + catch(Throwable ee) { - e.printStackTrace(); - // noinspection ResultOfMethodCallIgnored - s.delete(); + Iris.error("Configuration Error in settings.json! " + ee.getClass().getSimpleName() + ": " + ee.getMessage()); + Iris.warn("Attempting to fix configuration while retaining valid in-memory settings..."); + + try { + IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); + Iris.info("Configuration Fixed!"); + } catch (IOException e) { + e.printStackTrace(); + Iris.error("ERROR! CONFIGURATION IMPOSSIBLE TO READ! Using an unmodifiable configuration from memory. Please delete the settings.json at some point to try to restore configurability!"); + } } }