Rework Pulsar Configs to require less TOML specific code;

Fix Pulsar not working with generic events such as registering blocks and items.
This commit is contained in:
alexbegt committed 2019-05-20 12:14:31 -04:00
1 parent c25aa1a34b
commit fe2064a964
8 files changed
+106 -145

No files matched your search

@@ -2,7 +2,6 @@ package slimeknights.mantle.pulsar.config;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import slimeknights.mantle.pulsar.pulse.PulseMeta;
/**
@@ -13,62 +12,34 @@ 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.
*/
void load();
/**
* Perform anything after all pulses have been registered.
*/
void postLoad();
/**
* Gets whether the given module is enabled in the config.
*
* @param meta The pulse metadata.
* @return Whether the module is enabled.
*/
default boolean isModuleEnabled(PulseMeta meta) {
return false;
}
boolean isModuleEnabled(PulseMeta meta);
/**
* 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).
* Perform any action whenever a new pulse get's registered.
*
* @param meta The pulse metadata.
* @return The BooleanValue issued by the TOML config
* @return Whether the module is enabled.
*/
default BooleanValue getConfigEntry(PulseMeta meta) {
return null;
}
void addPulse(PulseMeta meta);
/**
* 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() {}
}
@@ -7,10 +7,14 @@ 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.internal.Configuration;
import slimeknights.mantle.pulsar.pulse.PulseMeta;
import javax.annotation.Nonnull;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
public class PulsarConfig implements IConfiguration
{
@@ -25,6 +29,7 @@ public class PulsarConfig implements IConfiguration
private boolean builderPushed;
private boolean configBuilt;
private final Map<String, ForgeConfigSpec.BooleanValue> modules = new LinkedHashMap<>();
/**
* Creates the config to be used later.
@@ -39,16 +44,16 @@ public class PulsarConfig implements IConfiguration
this.description = description.toLowerCase(Locale.US);
this.builderPushed = false;
this.configBuilt = false;
BUILDER.push(this.description);
}
@Override
public boolean isUsingTomlConfig() {
return true;
}
public void load() {}
@Override
public void load() {
public void postLoad() {
if(!this.configBuilt) {
BUILDER.pop();
SPEC = BUILDER.build();
SPEC.setConfig(config);
}
@@ -57,18 +62,26 @@ public class PulsarConfig implements IConfiguration
}
@Override
public void pushBuilder() {
if(!builderPushed) BUILDER.push(this.description);
public boolean isModuleEnabled(@Nonnull PulseMeta meta) {
ForgeConfigSpec.BooleanValue entry = modules.get(meta.getId());
builderPushed = true;
if (entry == null) {
modules.put(meta.getId(), BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled()));
return meta.isEnabled();
} else {
return entry.get();
}
}
@Override
public void popBuilder() {
BUILDER.pop();
public void addPulse(@Nonnull PulseMeta meta) {
ForgeConfigSpec.BooleanValue entry = modules.get(meta.getId());
if (entry == null) {
modules.put(meta.getId(), BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled()));
}
}
@Override
/*@Override
public boolean isModuleEnabled(ForgeConfigSpec.BooleanValue configValue) {
return configValue.get();
}
@@ -78,7 +91,7 @@ public class PulsarConfig implements IConfiguration
ForgeConfigSpec.BooleanValue value = BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled());
return value;
}
}*/
@Override
public void flush() {
@@ -52,7 +52,6 @@ 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<>();
@@ -77,7 +76,6 @@ public class PulseManager {
public PulseManager(String configName) {
init();
useConfig = true;
usingATomlConfig = false;
conf = new Configuration(configName, log);
}
@@ -92,7 +90,6 @@ public class PulseManager {
public PulseManager(IConfiguration config) {
init();
useConfig = true;
usingATomlConfig = config.isUsingTomlConfig();
conf = config;
}
@@ -131,7 +128,7 @@ public class PulseManager {
if (blockNewRegistrations) throw new RuntimeException("A mod tried to register a plugin after preinit! Pulse: "
+ pulse);
if (!configLoaded) {
if(!usingATomlConfig) conf.load();
conf.load();
configLoaded = true;
}
@@ -170,18 +167,8 @@ public class PulseManager {
PulseMeta meta = new PulseMeta(id, description, forced, enabled, defaultEnabled);
meta.setMissingDeps(missingDeps || !hasRequiredPulses(meta, pulseDeps));
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);
}
}
conf.addPulse(meta);
allPulses.put(pulse, meta);
}
/**
@@ -302,57 +289,26 @@ public class PulseManager {
public String toString() {
return "PulseManager[" + id + "]";
}
/**
* If you go and use PulsarConfig, you will need to call this method after you register all your pulses.
*
* This is due to how TOML configs work; If you try to get the value too early; it throws NullPointerException
* Enable or Disable all Pulses registered
*
* This MUST be called after you register all your pulses using registerPulse and must be called BEFORE preinit
*/
public void enablePulses() {
if(usingATomlConfig) {
conf.popBuilder();
conf.load();
if(blockNewRegistrations) throw new RuntimeException("A mod tried to enable their plugins after preinit!");
for (Map.Entry<Object, PulseMeta> entry : allPulses.entrySet()) {
PulseMeta meta = entry.getValue();
Object pulse = entry.getKey();
if(configLoaded) conf.postLoad();
meta.setEnabled(getEnabledFromConfig(meta.getConfigEntry(), meta));
for (Map.Entry<Object, PulseMeta> entry : allPulses.entrySet()) {
PulseMeta meta = entry.getValue();
Object pulse = entry.getKey();
if (meta.isEnabled()) {
pulses.put(pulse, meta);
flightpath.register(pulse);
}
meta.setEnabled(getEnabledFromConfig(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.");
}
}
/**
* Used for getting the config entry from the pulse meta.
*
* @param meta the pulse meta
*
* @return the config entry
*/
private BooleanValue getConfigEntryForConfig(PulseMeta meta) {
if (meta.isForced() || !useConfig) return null;
return conf.getConfigEntry(meta);
}
/**
* Used to check if the pulse should be enabled or not.
*
* @param boolValue the config entry
* @param meta the pulse meta
*
* @return Whether the pulse should be enabled.
*/
private boolean getEnabledFromConfig(BooleanValue boolValue, PulseMeta meta) {
if (meta.isForced() || !useConfig) return true; // Forced or no config set.
return conf.isModuleEnabled(boolValue);
}
}
@@ -2,12 +2,15 @@ package slimeknights.mantle.pulsar.flightpath;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.IGenericEvent;
import slimeknights.mantle.pulsar.flightpath.lib.AnnotationLocator;
import slimeknights.mantle.pulsar.flightpath.lib.BlackholeExceptionHandler;
@@ -22,8 +25,7 @@ public class Flightpath {
private final ISubscriberLocator locator;
// Note we *MUST* use a linked map, otherwise order is lost.
private final LinkedHashMap<Object, Map<Class, Set<Method>>> subscribers = new LinkedHashMap<Object,
Map<Class, Set<Method>>>();
private final LinkedHashMap<Object, Map<Class<?>, Map<Method, Type>>> subscribers = new LinkedHashMap<Object, Map<Class<?>, Map<Method, Type>>>();
// Anything that manipulates state *MUST* acquire this lock. It prevents awkward issues when iterating.
private final Object lock = new Object();
@@ -77,18 +79,22 @@ public class Flightpath {
*
* @param evt The event to post.
*/
public void post(Object evt) {
public void post(Event evt) {
synchronized (lock) {
for (Map.Entry<Object, Map<Class, Set<Method>>> ent : subscribers.entrySet()) {
for (Map.Entry<Class, Set<Method>> objEnt: ent.getValue().entrySet()) {
if (!objEnt.getKey().isAssignableFrom(evt.getClass())) continue;
Set<Method> ms = objEnt.getValue();
for (Method m : ms) {
for (Map.Entry<Object, Map<Class<?>, Map<Method, Type>>> ent : subscribers.entrySet()) {
for (Map.Entry<Class<?>, Map<Method, Type>> obj: ent.getValue().entrySet()) {
if (!obj.getKey().isAssignableFrom(evt.getClass())) continue;
for(Map.Entry<Method, Type> objEnt : obj.getValue().entrySet()){
Method m = objEnt.getKey();
Type filter = objEnt.getValue();
try {
boolean access = m.isAccessible();
m.setAccessible(true);
m.invoke(ent.getKey(), evt);
m.setAccessible(access);
if (filter == null || filter == ((IGenericEvent)evt).getGenericType()) {
boolean access = m.isAccessible();
m.setAccessible(true);
m.invoke(ent.getKey(), evt);
m.setAccessible(access);
}
} catch(InvocationTargetException ex) {
// thrown when a method throws an exception, so use that exception instead of the wrapper for a better stacktrace
if(ex.getTargetException() instanceof Exception) {
@@ -1,6 +1,7 @@
package slimeknights.mantle.pulsar.flightpath;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;
@@ -14,6 +15,6 @@ import javax.annotation.ParametersAreNonnullByDefault;
public interface ISubscriberLocator {
@Nonnull
Map<Class, Set<Method>> findSubscribers(Object obj);
Map<Class<?>, Map<Method, Type>> findSubscribers(Object obj);
}
@@ -2,6 +2,8 @@ package slimeknights.mantle.pulsar.flightpath.lib;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -10,6 +12,7 @@ import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraftforge.eventbus.api.IGenericEvent;
import slimeknights.mantle.pulsar.flightpath.ISubscriberLocator;
/**
@@ -27,16 +30,32 @@ public class AnnotationLocator implements ISubscriberLocator {
}
@Override
public @Nonnull Map<Class, Set<Method>> findSubscribers(Object obj) {
Map<Class, Set<Method>> methods = new HashMap<Class, Set<Method>>();
public @Nonnull Map<Class<?>, Map<Method, Type>> findSubscribers(Object obj) {
Map<Class<?>, Map<Method, Type>> methods = new HashMap<Class<?>, Map<Method, Type>>();
for (Method m : obj.getClass().getMethods()) {
if (m.isAnnotationPresent(annotation) && m.getParameterTypes().length == 1) {
Class param = m.getParameterTypes()[0];
if (!methods.containsKey(param)) methods.put(param, new HashSet<Method>());
methods.get(param).add(m);
Class<?> param = m.getParameterTypes()[0];
Type filter = null;
if(IGenericEvent.class.isAssignableFrom(param)) {
Type type = m.getGenericParameterTypes()[0];
if (type instanceof ParameterizedType) {
filter = ((ParameterizedType) type).getActualTypeArguments()[0];
if (filter instanceof ParameterizedType) {
filter = ((ParameterizedType) filter).getRawType();
}
}
}
if (!methods.containsKey(param)) methods.put(param, new HashMap<Method, Type>());
methods.get(param).put(m, filter);
}
}
return methods;
}
}
@@ -53,27 +53,33 @@ public class Configuration implements IConfiguration {
this.logger = logger;
}
@Override
public boolean isUsingTomlConfig() {
return false;
}
@Override
public void load() {
getModulesFromJson();
}
@Override
public void postLoad() {}
@Override
public boolean isModuleEnabled(@Nonnull PulseMeta meta) {
ConfigEntry entry = modules.get(meta.getId());
if (entry == null) {
modules.put(meta.getId(), new ConfigEntry(meta.isDefaultEnabled(), meta.getDescription()));
return meta.isEnabled();
} else {
return entry.getEnabled();
}
}
@Override
public void addPulse(@Nonnull PulseMeta meta) {
ConfigEntry entry = modules.get(meta.getId());
if (entry == null) {
modules.put(meta.getId(), new ConfigEntry(meta.isDefaultEnabled(), meta.getDescription()));
}
}
@Override
public void flush() {
writeModulesToJson();
@@ -1,7 +1,5 @@
package slimeknights.mantle.pulsar.pulse;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -16,7 +14,6 @@ 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;
@@ -53,12 +50,4 @@ public class PulseMeta {
public boolean isDefaultEnabled() {
return defaultEnabled;
}
public void setConfigEntry(BooleanValue entry) {
this.configEntry = entry;
}
public BooleanValue getConfigEntry() {
return this.configEntry;
}
}