Implement TOML file support for Pulsar Configs; Feedback accepted.

This commit is contained in:
alexbegt committed 2019-04-26 20:55:58 -04:00
1 parent a81f47c074
commit a85acc6f16
7 files changed
+213 -82

No files matched your search

@@ -1,73 +0,0 @@
package slimeknights.mantle.pulsar.config;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fml.common.Loader;
import java.io.File;
import java.util.Locale;
import javax.annotation.Nonnull;
import slimeknights.mantle.pulsar.pulse.PulseMeta;
/**
* Mantle specific pulsar addon class to support using the forge CFG format for configurations
* @author progwml6
*/
public class ForgeCFG implements IConfiguration
{
private Configuration config;
private final String confPath;
private final String description;
/**
* Creates a new Configuration object.
*
* Do NOT make this the same as the overall mod configuration; it will clobber it!
*
* @param confName The config file name (without path or .cfg suffix)
* @param description The description for the group that the config entries will be placed in.
*/
public ForgeCFG(String confName, String description)
{
this.confPath = Loader.instance().getConfigDir().toString() + File.separator + confName + ".cfg";
this.description = description.toLowerCase(Locale.US);
}
@Override
public void load()
{
config = new Configuration(new File(this.confPath), "1");
config.load();
}
@Override
public boolean isModuleEnabled(@Nonnull PulseMeta meta)
{
Property prop = config.get(this.description, meta.getId(), meta.isDefaultEnabled(), meta.getDescription());
prop.setRequiresMcRestart(true);
return prop.getBoolean(meta.isEnabled());
}
@Override
public void flush()
{
if (config.hasChanged())
{
config.save();
}
}
public Configuration getConfig() {
return config;
}
public ConfigCategory getCategory() {
return config.getCategory(this.description);
}
}
@@ -2,6 +2,7 @@ package slimeknights.mantle.pulsar.config;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import slimeknights.mantle.pulsar.pulse.PulseMeta;
/**
@@ -12,6 +13,13 @@ import slimeknights.mantle.pulsar.pulse.PulseMeta;
@ParametersAreNonnullByDefault
public interface IConfiguration {
/**
* Used via @PulseManager to determine the type of config file used.
*
* @return Whether the config file is a TOML file or not.
*/
boolean isUsingTomlConfig();
/**
* Perform any configuration loading required.
*/
@@ -23,11 +31,44 @@ public interface IConfiguration {
* @param meta The pulse metadata.
* @return Whether the module is enabled.
*/
boolean isModuleEnabled(PulseMeta meta);
default boolean isModuleEnabled(PulseMeta meta) {
return false;
}
/**
* Gets whether the given module is enabled in the config.
* Use this if you are using TOML files (PulsarConfig).
*
* @param booleanValue The boolean value returned by getConfigEntry
* @return Whether the module is enabled.
*/
default boolean isModuleEnabled(BooleanValue booleanValue) {
return false;
}
/**
* Gets the specific boolean value for the pulsar module.
* Use this if you are using TOML files (PulsarConfig).
*
* @param meta The pulse metadata.
* @return The BooleanValue issued by the TOML config
*/
default BooleanValue getConfigEntry(PulseMeta meta) {
return null;
}
/**
* Flush configuration to disk/database/whatever.
*/
void flush();
/**
* Used only by TOML configs to set the description to use for the configs.
*/
default void popBuilder() {}
/**
* Used only by TOML configs to clear the description used by the config settings.
*/
default void pushBuilder() {}
}
@@ -0,0 +1,96 @@
package slimeknights.mantle.pulsar.config;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.io.WritingMode;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.loading.FMLPaths;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import slimeknights.mantle.pulsar.control.PulseManager;
import slimeknights.mantle.pulsar.pulse.PulseMeta;
import java.nio.file.Path;
import java.util.Locale;
public class PulsarConfig implements IConfiguration
{
private static final Logger LOGGER = LogManager.getLogger();
private final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
private ForgeConfigSpec SPEC = BUILDER.build();
private final CommentedFileConfig config;
private final String description;
private boolean builderPushed;
private boolean configBuilt;
/**
* Creates the config to be used later.
*
* Do NOT make this the same as the overall mod configuration; it will clobber it!
*
* @param confName The config file name (without path or .toml suffix)
* @param description The description for the group that the config entries will be placed in.
*/
public PulsarConfig(String confName, String description) {
this.config = createConfig(FMLPaths.CONFIGDIR.get(), confName);
this.description = description.toLowerCase(Locale.US);
this.builderPushed = false;
this.configBuilt = false;
}
@Override
public boolean isUsingTomlConfig() {
return true;
}
@Override
public void load() {
if(!this.configBuilt) {
SPEC = BUILDER.build();
SPEC.setConfig(config);
}
configBuilt = true;
}
@Override
public void pushBuilder() {
if(!builderPushed) BUILDER.push(this.description);
builderPushed = true;
}
@Override
public void popBuilder() {
BUILDER.pop();
}
@Override
public boolean isModuleEnabled(ForgeConfigSpec.BooleanValue configValue) {
return configValue.get();
}
@Override
public ForgeConfigSpec.BooleanValue getConfigEntry(PulseMeta meta) {
ForgeConfigSpec.BooleanValue value = BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled());
return value;
}
@Override
public void flush() {
((CommentedFileConfig)this.config).save();
}
public CommentedFileConfig createConfig(Path configBasePath, String configName) {
final Path configPath = configBasePath.resolve(String.format("%s.toml", configName));
final CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync().autosave().writingMode(WritingMode.REPLACE).build();
LOGGER.debug(PulseManager.CONFIG, "Built TOML config for {}", configPath.toString());
configData.load();
LOGGER.debug(PulseManager.CONFIG, "Loaded TOML config file {}", configPath.toString());
return configData;
}
}
@@ -1,7 +1,7 @@
package slimeknights.mantle.pulsar.control;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.eventbus.EventBus;
import net.minecraftforge.eventbus.api.Event;
@@ -23,6 +23,8 @@ import java.util.Map;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import slimeknights.mantle.pulsar.config.IConfiguration;
import slimeknights.mantle.pulsar.flightpath.Flightpath;
import slimeknights.mantle.pulsar.flightpath.IExceptionHandler;
@@ -50,9 +52,11 @@ public class PulseManager {
private Logger log;
private final boolean useConfig;
private final boolean usingATomlConfig;
private static final Map<String, Map<Object, PulseMeta>> ALL_PULSES = new HashMap<>();
private final Map<Object, PulseMeta> pulses = new LinkedHashMap<>();
private final Map<Object, PulseMeta> allPulses = new LinkedHashMap<>();
// Use the Google @Subscribe to avoid confusion/breaking changes.
private final Flightpath flightpath = new Flightpath(new AnnotationLocator(SubscribeEvent.class));
@@ -61,6 +65,8 @@ public class PulseManager {
private IConfiguration conf;
private String id;
public static final Marker CONFIG = MarkerManager.getMarker("PULSARCONFIG");
/**
* Configuration-using constructor.
*
@@ -71,6 +77,7 @@ public class PulseManager {
public PulseManager(String configName) {
init();
useConfig = true;
usingATomlConfig = false;
conf = new Configuration(configName, log);
}
@@ -85,6 +92,7 @@ public class PulseManager {
public PulseManager(IConfiguration config) {
init();
useConfig = true;
usingATomlConfig = config.isUsingTomlConfig();
conf = config;
}
@@ -123,7 +131,8 @@ public class PulseManager {
if (blockNewRegistrations) throw new RuntimeException("A mod tried to register a plugin after preinit! Pulse: "
+ pulse);
if (!configLoaded) {
conf.load();
if(!usingATomlConfig) conf.load();
configLoaded = true;
}
@@ -160,11 +169,18 @@ public class PulseManager {
PulseMeta meta = new PulseMeta(id, description, forced, enabled, defaultEnabled);
meta.setMissingDeps(missingDeps || !hasRequiredPulses(meta, pulseDeps));
meta.setEnabled(getEnabledFromConfig(meta));
if (meta.isEnabled()) {
pulses.put(pulse, meta);
flightpath.register(pulse);
if(usingATomlConfig) {
conf.pushBuilder();
meta.setConfigEntry(getConfigEntryForConfig(meta));
allPulses.put(pulse, meta);
} else {
meta.setEnabled(getEnabledFromConfig(meta));
if (meta.isEnabled()) {
pulses.put(pulse, meta);
flightpath.register(pulse);
}
}
}
@@ -286,4 +302,39 @@ public class PulseManager {
public String toString() {
return "PulseManager[" + id + "]";
}
//TOML Support Start
public void enablePulses() {
if(usingATomlConfig) {
conf.popBuilder();
conf.load();
for (Map.Entry<Object, PulseMeta> entry : allPulses.entrySet()) {
PulseMeta meta = entry.getValue();
Object pulse = entry.getKey();
meta.setEnabled(getEnabledFromConfig(meta.getConfigEntry(), meta));
if (meta.isEnabled()) {
pulses.put(pulse, meta);
flightpath.register(pulse);
}
}
} else {
log.info("Calling enablePulses isn't needed unless you are using a TOML file.");
}
}
private BooleanValue getConfigEntryForConfig(PulseMeta meta) {
if (meta.isForced() || !useConfig) return null;
return conf.getConfigEntry(meta);
}
private boolean getEnabledFromConfig(BooleanValue boolValue, PulseMeta meta) {
if (meta.isForced() || !useConfig) return true; // Forced or no config set.
return conf.isModuleEnabled(boolValue);
}
//TOML Support End
}
@@ -53,6 +53,11 @@ public class Configuration implements IConfiguration {
this.logger = logger;
}
@Override
public boolean isUsingTomlConfig() {
return false;
}
@Override
public void load() {
getModulesFromJson();
@@ -1,5 +1,7 @@
package slimeknights.mantle.pulsar.pulse;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -14,6 +16,7 @@ public class PulseMeta {
private String id, description;
private boolean forced, enabled, defaultEnabled;
private boolean missingDeps = false;
private BooleanValue configEntry;
public PulseMeta(String id, @Nullable String description, boolean forced, boolean enabled, boolean defaultEnabled) {
this.id = id;
@@ -50,4 +53,12 @@ public class PulseMeta {
public boolean isDefaultEnabled() {
return defaultEnabled;
}
public void setConfigEntry(BooleanValue entry) {
this.configEntry = entry;
}
public BooleanValue getConfigEntry() {
return this.configEntry;
}
}