Config hotloading

This commit is contained in:
Daniel Mills 2020-11-17 01:32:34 -05:00
parent 6d08faa30f
commit 802bc50102
2 changed files with 68 additions and 31 deletions

View File

@ -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())

View File

@ -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!");
}
}
}