Fix config presets rewriting the config file before it loads

This commit is contained in:
James Seibel
2023-08-24 07:41:08 -05:00
parent 971caca9c9
commit 3361ba2fab
3 changed files with 21 additions and 3 deletions
@@ -31,7 +31,6 @@ import com.seibel.distanthorizons.core.config.eventHandlers.UnsafeValuesConfigLi
import com.seibel.distanthorizons.core.config.eventHandlers.WorldCurvatureConfigEventHandler;
import com.seibel.distanthorizons.core.config.eventHandlers.presets.ThreadPresetConfigEventHandler;
import com.seibel.distanthorizons.core.config.eventHandlers.presets.RenderQualityPresetConfigEventHandler;
import com.seibel.distanthorizons.core.config.listeners.ConfigChangeListener;
import com.seibel.distanthorizons.core.config.types.*;
import com.seibel.distanthorizons.core.config.types.enums.EConfigEntryAppearance;
import com.seibel.distanthorizons.core.config.types.enums.EConfigEntryPerformance;
@@ -61,6 +60,15 @@ public class Config
public static ConfigCategory client = new ConfigCategory.Builder().set(Client.class).build();
/**
* <strong>False</strong> if the config hasn't been loaded in from file yet.
* While in this state the config shouldn't be modified since it may cause file corruption. <br><br>
*
* <strong>True</strong> if the config has been loaded and is ready to use.
*/
public static boolean loaded = false;
public static class Client
{
@@ -1,5 +1,6 @@
package com.seibel.distanthorizons.core.config.eventHandlers.presets;
import com.seibel.distanthorizons.core.config.Config;
import com.seibel.distanthorizons.core.config.ConfigEntryWithPresetOptions;
import com.seibel.distanthorizons.core.config.listeners.IConfigListener;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
@@ -62,6 +63,12 @@ public abstract class AbstractPresetConfigEventHandler<TPresetEnum extends Enum<
@Override
public void onConfigValueSet()
{
// don't try modifying the config before it's been loaded from file
if (!Config.loaded)
{
return;
}
// don't have this method run on top of itself
if (this.changingPreset)
{
@@ -1,6 +1,7 @@
package com.seibel.distanthorizons.core.config.file;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.seibel.distanthorizons.core.config.Config;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.config.ConfigBase;
import com.seibel.distanthorizons.core.config.types.AbstractConfigType;
@@ -10,8 +11,6 @@ import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftSha
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -262,6 +261,10 @@ public class ConfigFileHandling
SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class).crashMinecraft("Loading file and resetting config file failed at path [" + configPath + "]. Please check the file is ok and you have the permissions", ex);
}
}
// if the config is modified before having been loaded it can cause file corruption
// and permissions errors
Config.loaded = true;
}