From 008da8d8d65b7540ae8249a885f4cc81e8658299 Mon Sep 17 00:00:00 2001 From: makamys Date: Thu, 9 Jun 2022 14:32:33 +0200 Subject: Implement config hot swapping, organize configs more --- src/main/java/makamys/neodymium/Config.java | 63 +++++++++++++++++++--- src/main/java/makamys/neodymium/Neodymium.java | 9 +++- .../makamys/neodymium/renderer/NeoRenderer.java | 2 +- 3 files changed, 65 insertions(+), 9 deletions(-) (limited to 'src/main') diff --git a/src/main/java/makamys/neodymium/Config.java b/src/main/java/makamys/neodymium/Config.java index ade4fb1..378fb4d 100644 --- a/src/main/java/makamys/neodymium/Config.java +++ b/src/main/java/makamys/neodymium/Config.java @@ -1,6 +1,18 @@ package makamys.neodymium; +import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; +import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; +import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; + +import static makamys.neodymium.Neodymium.LOGGER; +import static makamys.neodymium.Neodymium.MODID; + import java.io.File; +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -8,11 +20,14 @@ import java.util.stream.Collectors; import org.lwjgl.input.Keyboard; +import net.minecraft.launchwrapper.Launch; import net.minecraftforge.common.config.Configuration; public class Config { public static boolean enabled; + public static boolean hotswap; + public static boolean simplifyChunkMeshes; public static int maxMeshesPerFrame; public static int sortFrequency; @@ -34,28 +49,38 @@ public class Config { public static boolean disableSimpleMeshes = true; public static boolean saveMeshes = false; - static File configFile; + private static File configFile = new File(Launch.minecraftHome, "config/" + MODID + ".cfg"); + private static WatchService watcher; public static void reloadConfig() { Configuration config = new Configuration(configFile); config.load(); - enabled = config.getBoolean("enabled", "general", true, "Set this to false to fully disable the mod (this can be useful to compare it with the vanilla renderer)"); + enabled = config.getBoolean("enabled", "_general", true, "Set this to false to fully disable the mod."); + hotswap = config.getBoolean("hotswap", "_general", false, "Apply changes made in the config file immediately without having to reload the world. Off by default because it could potentially cause poor performance on certain platforms. Note that not all settings can be hotswapped."); simplifyChunkMeshes = config.getBoolean("simplifyChunkMeshes", "render", false, "Simplify chunk meshes so they are made of less vertices. Proof of concept, produces very janky results."); - maxMeshesPerFrame = config.getInt("maxMeshesPerFrame", "debug", -1, -1, Integer.MAX_VALUE, ""); - - sortFrequency = config.getInt("sortFrequency", "render", 1, 1, Integer.MAX_VALUE, "Interval (in frames) between the sorting of meshes."); - gcRate = config.getInt("gcRate", "render", 1, 1, Integer.MAX_VALUE, "Maximum number of meshes to relocate each frame. Setting this to a higher value will make it harder for the VRAM to get full (which causes a lag spike when it happens), but slightly reduces framerate. The VRAM debugger can be used to find the right value."); + sortFrequency = config.getInt("sortFrequency", "render", 1, 1, Integer.MAX_VALUE, "Interval (in frames) between the sorting of meshes. Increasing this might increase framerate, but increase the likelyhood of graphical artifacts when moving quickly."); + gcRate = config.getInt("gcRate", "render", 1, 1, Integer.MAX_VALUE, "Maximum number of meshes to relocate in the buffer each frame. Setting this to a higher value will make it harder for the VRAM to get full (which causes a lag spike when it happens), but slightly reduces framerate. The VRAM debugger can be used to find the right value."); VRAMSize = config.getInt("VRAMSize", "render", 1024, 1, Integer.MAX_VALUE, "VRAM buffer size (MB). 512 seems to be a good value on Normal render distance. Increase this if you encounter warnings about the VRAM getting full. Does not affect RAM usage."); - renderFog = config.getBoolean("drawFog", "render", true, "Render frog? Disable this to increase framerate."); + renderFog = config.getBoolean("renderFog", "render", true, "Render frog? Disabling this might increase framerate."); + + maxMeshesPerFrame = config.getInt("maxMeshesPerFrame", "debug", -1, -1, Integer.MAX_VALUE, ""); debugPrefix = config.getInt("debugPrefix", "debug", Keyboard.KEY_F4, -1, Integer.MAX_VALUE, "This key has to be held down while pressing the debug keybinds. LWJGL keycode. Setting this to 0 will make the keybinds usable without holding anything else down. Setting this to -1 will disable debug keybinds entirely."); debugInfoStartY = config.getInt("debugInfoStartY", "debug", 80, -1, Integer.MAX_VALUE, "The Y position of the first line of the debug info in the F3 overlay. Set this to -1 to disable showing that info."); if(config.hasChanged()) { config.save(); } + + if(hotswap && watcher == null) { + try { + registerWatchService(); + } catch(IOException e) { + LOGGER.warn("Failed to register watch service: " + e + " (" + e.getMessage() + "). Changes to the config file will not be reflected"); + } + } } // Unused @@ -81,4 +106,28 @@ public class Config { saveMeshes = config.getBoolean("saveMeshes", "render", false, ""); } + public static boolean reloadIfChanged() { + boolean reloaded = false; + if(watcher != null) { + WatchKey key = watcher.poll(); + + if(key != null) { + for(WatchEvent event: key.pollEvents()) { + if(event.context().toString().equals(configFile.getName())) { + reloadConfig(); + reloaded = true; + } + } + key.reset(); + } + } + + return reloaded; + } + + private static void registerWatchService() throws IOException { + watcher = FileSystems.getDefault().newWatchService(); + configFile.toPath().getParent().register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); + } + } diff --git a/src/main/java/makamys/neodymium/Neodymium.java b/src/main/java/makamys/neodymium/Neodymium.java index 594c181..fd4eb82 100644 --- a/src/main/java/makamys/neodymium/Neodymium.java +++ b/src/main/java/makamys/neodymium/Neodymium.java @@ -51,7 +51,6 @@ public class Neodymium @EventHandler public void preInit(FMLPreInitializationEvent event) { - Config.configFile = event.getSuggestedConfigurationFile(); Config.reloadConfig(); } @@ -106,6 +105,14 @@ public class Neodymium @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { if(event.phase == TickEvent.Phase.START) { + if(Config.hotswap) { + if(Config.reloadIfChanged()) { + if(renderer != null) { + renderer.loadShader(); + } + } + } + EntityPlayer player = Minecraft.getMinecraft().thePlayer; World world = player != null ? player.worldObj : null; if(world != getRendererWorld()) { diff --git a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java index 3c788ff..4d80675 100644 --- a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java +++ b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java @@ -441,7 +441,7 @@ public class NeoRenderer { return true; } - private void loadShader() { + public void loadShader() { int vertexShader; vertexShader = glCreateShader(GL_VERTEX_SHADER); -- cgit