From 6fcd92dec9f4045058a6688a26954c6799a79206 Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Thu, 22 Oct 2015 13:33:36 +0200 Subject: [PATCH] Welcome back pulsar! --- .../mantle/pulsar/config/ForgeCFG.java | 58 ++++ .../mantle/pulsar/config/IConfiguration.java | 31 ++ .../mantle/pulsar/control/PulseManager.java | 294 ++++++++++++++++++ .../mantle/pulsar/debug/EventSpy.java | 28 ++ .../mantle/pulsar/internal/Configuration.java | 253 +++++++++++++++ .../mantle/pulsar/internal/CrashHandler.java | 62 ++++ .../pulsar/internal/logging/ILogger.java | 24 ++ .../pulsar/internal/logging/JULLogger.java | 56 ++++ .../pulsar/internal/logging/Log4jLogger.java | 56 ++++ .../pulsar/internal/logging/LogManager.java | 53 ++++ .../mantle/pulsar/package-info.java | 36 +++ .../mantle/pulsar/pulse/Pulse.java | 53 ++++ .../mantle/pulsar/pulse/PulseMeta.java | 53 ++++ 13 files changed, 1057 insertions(+) create mode 100644 src/main/java/slimeknights/mantle/pulsar/config/ForgeCFG.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/debug/EventSpy.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/internal/CrashHandler.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/internal/logging/ILogger.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/internal/logging/JULLogger.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/internal/logging/Log4jLogger.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/internal/logging/LogManager.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/package-info.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/pulse/Pulse.java create mode 100644 src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java diff --git a/src/main/java/slimeknights/mantle/pulsar/config/ForgeCFG.java b/src/main/java/slimeknights/mantle/pulsar/config/ForgeCFG.java new file mode 100644 index 0000000..c75e344 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/config/ForgeCFG.java @@ -0,0 +1,58 @@ +package mantle.pulsar.config; + +import java.io.File; + +import mantle.pulsar.pulse.PulseMeta; +import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.fml.common.Loader; + +/** + * Mantle specific pulsar addon class to support using the forge CFG format for configurations + * @author progwml6 + */ +public class ForgeCFG implements IConfiguration +{ + + private static 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; + } + + @Override + public void load() + { + config = new Configuration(new File(this.confPath)); + config.load(); + } + + @Override + public boolean isModuleEnabled(PulseMeta meta) + { + return config.get(this.description, meta.getId(), meta.isEnabled(), meta.getDescription()).getBoolean(meta.isEnabled()); + } + + @Override + public void flush() + { + if (config.hasChanged()) + { + config.save(); + } + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java b/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java new file mode 100644 index 0000000..cf14e24 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java @@ -0,0 +1,31 @@ +package mantle.pulsar.config; + +import mantle.pulsar.pulse.PulseMeta; + +/** + * Interface for config handlers. + * + * @author Arkan + */ +public interface IConfiguration +{ + + /** + * Perform any configuration loading required. + */ + public void load(); + + /** + * Gets whether the given module is enabled in the config. + * + * @param meta The pulse metadata. + * @return Whether the module is enabled. + */ + public boolean isModuleEnabled(PulseMeta meta); + + /** + * Flush configuration to disk/database/whatever. + */ + public void flush(); + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java new file mode 100644 index 0000000..08e512a --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java @@ -0,0 +1,294 @@ +package mantle.pulsar.control; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import mantle.pulsar.config.IConfiguration; +import mantle.pulsar.internal.Configuration; +import mantle.pulsar.internal.CrashHandler; +import mantle.pulsar.internal.logging.ILogger; +import mantle.pulsar.internal.logging.LogManager; +import mantle.pulsar.pulse.Pulse; +import mantle.pulsar.pulse.PulseMeta; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.FMLModContainer; +import net.minecraftforge.fml.common.Loader; +import net.minecraftforge.fml.common.ModContainer; +import net.minecraftforge.fml.common.event.FMLEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; + +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import com.google.common.eventbus.SubscriberExceptionContext; +import com.google.common.eventbus.SubscriberExceptionHandler; + +/** + * Manager class for a given mods Pulses. + * + * This MUST be constructed by a mod BEFORE preinit as it registers on to the mod event bus - a static block would serve + * for this. No more Pulses can be registered after preinit has been caught, so assume preinit is too late to register + * new Pulses. + * + * Each Pulsar-enabled mod should create one and only one of these to manage its Pulses. + * + * @author Arkan + */ +@SuppressWarnings("unused") +public class PulseManager +{ + + private ILogger log; + + private final boolean useConfig; + + private final LinkedHashMap pulses = new LinkedHashMap(); + + private boolean blockNewRegistrations = false; + + private boolean configLoaded = false; + + private IConfiguration conf; + + private EventBus bus; + + private String id; + + /** + * Configuration-using constructor. + * + * This form creates a PulseManager that supports configuration of Pulses by file. + * + * @param configName The config file name. + */ + public PulseManager(String configName) + { + this.init(); + this.useConfig = true; + this.conf = new Configuration(configName, this.log); + } + + /** + * Custom configuration-using constructor. + * + * Don't like JSON? Heathen. Lets you handle configuration, to whatever media you like - File, database, death star. + * Whatever really. See {@link mantle.pulsar.config.IConfiguration}. + * + * @param config Configuration handler. + */ + public PulseManager(IConfiguration config) + { + this.init(); + this.useConfig = true; + this.conf = config; + } + + /** + * Shared initialiser code between all the constructors. + */ + private void init() + { + String modId = Loader.instance().activeModContainer().getModId(); + this.id = modId; + this.log = LogManager.getLogger("Pulsar-" + modId); + FMLCommonHandler.instance().registerCrashCallable(new CrashHandler(modId, this)); + // Attach us to the mods FML bus and setup our own bus + this.bus = new EventBus(new BusExceptionHandler(modId)); + this.attachToContainerEventBus(this); + } + + /** + * Register a new Pulse with the manager. + * + * This CANNOT be done after preInit() has been invoked. + * + * @param pulse The Pulse to register. + */ + public void registerPulse(Object pulse) + { + if (this.blockNewRegistrations) + { + throw new RuntimeException("A mod tried to register a plugin after preinit! Pulse: " + + pulse); + } + if (!this.configLoaded) + { + this.conf.load(); + this.configLoaded = true; + } + + String id, description, deps; + boolean forced, enabled, defaultEnabled, missingDeps = false; + + try + { + Pulse p = pulse.getClass().getAnnotation(Pulse.class); + id = p.id(); + description = p.description(); + deps = p.modsRequired(); + forced = p.forced(); + enabled = p.defaultEnable(); + defaultEnabled = p.defaultEnable(); + } + catch (NullPointerException ex) + { + throw new RuntimeException("Could not parse @Pulse annotation for Pulse: " + pulse); + } + + // Work around Java not allowing default-null fields. + if (description.equals("")) + { + description = null; + } + + if (!deps.equals("")) + { + String[] parsedDeps = deps.split(";"); + for (String s : parsedDeps) + { + if (!Loader.isModLoaded(s)) + { + this.log.info("Skipping Pulse " + id + "; missing dependency: " + s); + missingDeps = true; + enabled = false; + break; + } + } + } + + PulseMeta meta = new PulseMeta(id, description, forced, enabled, defaultEnabled); + meta.setEnabled(!missingDeps && this.getEnabledFromConfig(meta)); + + if (meta.isEnabled()) + { + this.pulses.put(pulse, meta); + // Attach Pulse to internal event bus + this.bus.register(pulse); + } + } + + /** + * Helper to attach a given object to the modcontainer event bus. + * + * @param obj Object to register. + */ + private void attachToContainerEventBus(Object obj) + { + ModContainer cnt = Loader.instance().activeModContainer(); + this.log.debug("Attaching [" + obj + "] to event bus for container [" + cnt + "]"); + try + { + FMLModContainer mc = (FMLModContainer) cnt; + Field ebf = mc.getClass().getDeclaredField("eventBus"); + + boolean access = ebf.isAccessible(); + ebf.setAccessible(true); + EventBus eb = (EventBus) ebf.get(mc); + ebf.setAccessible(access); + + eb.register(obj); + } + catch (NoSuchFieldException nsfe) + { + throw new RuntimeException("Pulsar >> Incompatible FML mod container (missing eventBus field) - wrong Forge version?"); + } + catch (IllegalAccessException iae) + { + throw new RuntimeException("Pulsar >> Security Manager blocked access to eventBus on mod container. Cannot continue."); + } + catch (ClassCastException cce) + { + throw new RuntimeException("Pulsar >> Something in the mod container had the wrong type? " + cce.getMessage()); + } + } + + /** + * Internal (but public for EventBus use) handler for events. + * + * DO NOT CALL THIS DIRECTLY! Let EventBus handle it. + * + * @param evt An event object. + */ + @Subscribe + public void propagateEvent(FMLEvent evt) + { + if (evt instanceof FMLPreInitializationEvent) + { + this.preInit((FMLPreInitializationEvent) evt); + } + this.bus.post(evt); + } + + private boolean getEnabledFromConfig(PulseMeta meta) + { + if (meta.isForced() || !this.useConfig) + { + return true; // Forced or no config set. + } + + return this.conf.isModuleEnabled(meta); + } + + public void preInit(FMLPreInitializationEvent evt) + { + if (!this.blockNewRegistrations) + { + this.conf.flush(); // First preInit call, so flush config + } + this.blockNewRegistrations = true; + } + + /** + * Check if a given Pulse ID is loaded in this manager. + * + * @param pulseId The ID to check. + * @return Whether the ID was present. + */ + public boolean isPulseLoaded(String pulseId) + { + for (Map.Entry entry : this.pulses.entrySet()) + { + if (entry.getValue().getId().equals(pulseId)) + { + return true; + } + } + return false; + } + + public Collection getAllPulseMetadata() + { + return this.pulses.values(); + } + + /** + * Needed because Google EventBus is a derp and by default swallows exceptions (dafuq guys?) + */ + private class BusExceptionHandler implements SubscriberExceptionHandler + { + private final String id; + + /** + * @param id Mod ID to include in exception raises. + */ + public BusExceptionHandler(String id) + { + this.id = id; + } + + @Override + public void handleException(Throwable exception, SubscriberExceptionContext ctx) + { + FMLCommonHandler.instance().raiseException(exception, "Pulsar/" + this.id + " >> Exception uncaught in [" + + ctx.getSubscriber().getClass().getName() + ":" + ctx.getSubscriberMethod().getName() + + "] for event [" + ctx.getEvent().getClass().getSimpleName() + "]", true); + } + } + + @Override + public String toString() + { + return "PulseManager[" + this.id + "]"; + } +} diff --git a/src/main/java/slimeknights/mantle/pulsar/debug/EventSpy.java b/src/main/java/slimeknights/mantle/pulsar/debug/EventSpy.java new file mode 100644 index 0000000..36d11aa --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/debug/EventSpy.java @@ -0,0 +1,28 @@ +package mantle.pulsar.debug; + +import mantle.pulsar.internal.logging.ILogger; +import mantle.pulsar.internal.logging.LogManager; +import mantle.pulsar.pulse.Pulse; +import net.minecraftforge.fml.common.Loader; + +import com.google.common.eventbus.Subscribe; + +/** + * Debug Pulse to 'eavesdrop' on the PulseManager event bus traffic. + * + * @author Arkan + */ +@SuppressWarnings("unused") +@Pulse(id = "EventSpy", description = "we iz in ur buses, monitorin ur eventz", forced = true) +public class EventSpy +{ + + private final ILogger log = LogManager.getLogger("EventSpy/" + Loader.instance().activeModContainer().getModId()); + + @Subscribe + public void receive(Object evt) + { + this.log.info("Received event: " + evt); + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java b/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java new file mode 100644 index 0000000..44769e9 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java @@ -0,0 +1,253 @@ +package mantle.pulsar.internal; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.HashMap; +import java.util.Map; + +import mantle.pulsar.config.IConfiguration; +import mantle.pulsar.internal.logging.ILogger; +import mantle.pulsar.pulse.PulseMeta; +import net.minecraftforge.fml.common.Loader; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +/** + * Default Gson Configuration helper. + * + * @author Arkan + */ +public class Configuration implements IConfiguration +{ + + private static final int CONFIG_LEVEL = 1; + + private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + private final String confPath; + + private final ILogger logger; + + private Map modules; + + /** + * 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 .json suffix) + * @param logger The logger to send debug info to. + */ + public Configuration(String confName, ILogger logger) + { + this.confPath = Loader.instance().getConfigDir().toString() + File.separator + confName + ".json"; + this.logger = logger; + } + + @Override + public void load() + { + this.getModulesFromJson(); + } + + @Override + public boolean isModuleEnabled(PulseMeta meta) + { + ConfigEntry entry = this.modules.get(meta.getId()); + if (entry == null) + { + this.modules.put(meta.getId(), new ConfigEntry(meta.isDefaultEnabled(), meta.getDescription())); + return meta.isEnabled(); + } + else + { + return entry.getEnabled(); + } + } + + @Override + public void flush() + { + this.writeModulesToJson(); + } + + private void getModulesFromJson() + { + // Step 1: Does the file exist? + File f = new File(this.confPath); + if (!f.exists()) + { + this.logger.info("Couldn't find config file; will generate a new one later."); + this.modules = new HashMap(); + return; + } + + // Step 2: File exists. Let's make sure it's usable. + if (!(f.canRead() && f.canWrite())) + { + throw new FileNotReadWritableException("Could not read/write Pulsar config: " + this.confPath); + } + + // Step 3: Good enough. Read it. + try + { + try + { + this.modules = this.parseV1Config(f); + } + catch (Exception ex) + { + this.logger.warn("Failed to parse " + f.getName() + " using the v1 parser; trying the v0 parser."); + Map conf = this.parseV0Config(f); + this.logger.info("Found valid v0 configuration. Upgrading it."); + this.modules = conf; + this.writeModulesToJson(); + this.logger.info("Upgrade complete! Config is now in v1 format."); + } + } + catch (Exception ex) + { + this.logger.warn("Invalid config file. Discarding."); + ex.printStackTrace(); + this.modules = new HashMap(); + } + } + + private Map parseV0Config(File f) throws Exception + { + try + { + JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(f))); + Map m = gson.fromJson(reader, new TypeToken>() + { + }.getType()); // NASTY! + if (m == null) + { + throw new NullPointerException("Gson returned null."); + } + Map out = new HashMap(); + for (Map.Entry e : m.entrySet()) + { + out.put(e.getKey(), new ConfigEntry(e.getValue())); + } + return out; + } + catch (FileNotFoundException fnfe) + { + throw new RuntimeException("This shouldn't be possible... " + fnfe); + } + } + + private Map parseV1Config(File f) throws Exception + { + try + { + JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(f))); + GsonConfig c = gson.fromJson(reader, GsonConfig.class); + if (c.getConfigVersion() > 1) + { + throw new RuntimeException("Pulsar config is from a newer version! Remove it! " + f.getAbsolutePath()); + } + if (c.getModules() == null) + { + throw new IllegalArgumentException("Not a valid GsonConfig. Try v0 parsing."); + } + return c.getModules(); + } + catch (FileNotFoundException fnfe) + { + throw new RuntimeException("This shouldn't be possible... " + fnfe); + } + } + + private void writeModulesToJson() + { + try + { + JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(new File(this.confPath)))); + writer.setIndent(" "); + GsonConfig out = new GsonConfig(CONFIG_LEVEL, this.modules); + gson.toJson(out, GsonConfig.class, writer); + writer.close(); + } + catch (Exception ex) + { + this.logger.warn("Could not write config? " + this.confPath); + } + } + + /** + * Internal exception for an unreadable/unwritable config. + */ + private static class FileNotReadWritableException extends RuntimeException + { + public FileNotReadWritableException(String message) + { + super(message); + } + } + + /** + * Internal data holder, for use in the map and JSON. + */ + private static class ConfigEntry + { + + private Boolean enabled; + + private String description = null; + + public ConfigEntry(Boolean enabled) + { + this.enabled = enabled; + } + + public ConfigEntry(Boolean enabled, String description) + { + this(enabled); + this.description = description; + } + + public Boolean getEnabled() + { + return this.enabled; + } + } + + /** + * Internal representation of a version 1+ config file. + */ + private static class GsonConfig + { + + private int CONFIG_VERSION = 0; + + private Map modules; + + public GsonConfig(int version, Map modules) + { + this.CONFIG_VERSION = version; + this.modules = modules; + } + + public int getConfigVersion() + { + return this.CONFIG_VERSION; + } + + public Map getModules() + { + return this.modules; + } + + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/CrashHandler.java b/src/main/java/slimeknights/mantle/pulsar/internal/CrashHandler.java new file mode 100644 index 0000000..aa9233d --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/internal/CrashHandler.java @@ -0,0 +1,62 @@ +package mantle.pulsar.internal; + +import mantle.pulsar.control.PulseManager; +import mantle.pulsar.pulse.PulseMeta; +import net.minecraftforge.fml.common.ICrashCallable; + +/** + * FML crash callable for Pulse Managers; dumps a list of loaded pulses to the error log. + * + * @author Arkan + */ +public class CrashHandler implements ICrashCallable +{ + + private String id; + + private PulseManager manager; + + public CrashHandler(String modId, PulseManager manager) + { + this.id = "Pulsar/" + modId + " loaded Pulses"; + this.manager = manager; + } + + @Override + public String getLabel() + { + return this.id; + } + + @Override + public String call() throws Exception + { + String out = "\n"; + for (PulseMeta meta : this.manager.getAllPulseMetadata()) + { + String state = getStateFromMeta(meta); + out += "\t\t- " + meta.getId() + " (" + state + ")\n"; // Yes, yes, manual indenting, I know... + } + return out; + } + + private static String getStateFromMeta(PulseMeta meta) + { + if (meta.isForced()) + { + return "Enabled/Forced"; + } + else + { + if (meta.isEnabled()) + { + return "Enabled/Not Forced"; + } + else + { + return "Disabled/Not Forced"; + } + } + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/logging/ILogger.java b/src/main/java/slimeknights/mantle/pulsar/internal/logging/ILogger.java new file mode 100644 index 0000000..d1b38d6 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/internal/logging/ILogger.java @@ -0,0 +1,24 @@ +package mantle.pulsar.internal.logging; + +/** + * Interface for log wrappers. + * + * @author Arkan + */ +@SuppressWarnings("unused") +public interface ILogger +{ + + public void fatal(String msg); + + public void severe(String msg); + + public void warn(String msg); + + public void info(String msg); + + public void debug(String msg); + + public void trace(String msg); + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/logging/JULLogger.java b/src/main/java/slimeknights/mantle/pulsar/internal/logging/JULLogger.java new file mode 100644 index 0000000..b632936 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/internal/logging/JULLogger.java @@ -0,0 +1,56 @@ +package mantle.pulsar.internal.logging; + +import java.util.logging.Logger; + +/** + * Java logger wrapper. + * + * @author Arkan + */ +public class JULLogger implements ILogger +{ + + private final Logger log; + + public JULLogger(String name) + { + this.log = Logger.getLogger(name); + } + + @Override + public void fatal(String msg) + { + this.log.severe(msg); // JUL doesn't -have- fatal... + } + + @Override + public void severe(String msg) + { + this.log.severe(msg); + } + + @Override + public void warn(String msg) + { + this.log.warning(msg); + } + + @Override + public void info(String msg) + { + this.log.info(msg); + } + + @Override + public void debug(String msg) + { + this.log.fine(msg); // Good enough. + } + + @Override + public void trace(String msg) + { + this.log.finest(msg); // Closest we'll get. + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/logging/Log4jLogger.java b/src/main/java/slimeknights/mantle/pulsar/internal/logging/Log4jLogger.java new file mode 100644 index 0000000..ab6c80d --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/internal/logging/Log4jLogger.java @@ -0,0 +1,56 @@ +package mantle.pulsar.internal.logging; + +import org.apache.logging.log4j.Logger; + +/** + * Log4j log wrapper. + * + * @author Arkan + */ +public class Log4jLogger implements ILogger +{ + + private final Logger log; + + public Log4jLogger(String name) + { + this.log = org.apache.logging.log4j.LogManager.getLogger(name); + } + + @Override + public void fatal(String msg) + { + this.log.fatal(msg); + } + + @Override + public void severe(String msg) + { + this.log.error(msg); + } + + @Override + public void warn(String msg) + { + this.log.warn(msg); + } + + @Override + public void info(String msg) + { + this.log.info(msg); + } + + @Override + public void debug(String msg) + { + this.log.debug(msg); + } + + @Override + public void trace(String msg) + { + this.log.trace(msg); + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/logging/LogManager.java b/src/main/java/slimeknights/mantle/pulsar/internal/logging/LogManager.java new file mode 100644 index 0000000..16b2ba9 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/internal/logging/LogManager.java @@ -0,0 +1,53 @@ +package mantle.pulsar.internal.logging; + +/** + * Static class for getting generic loggers. + * + * @author Arkan + */ +public class LogManager +{ + + private LogManager() + { + } // Singleton. + + private static final boolean useLog4j2; + + /* + * Detect the presence of log4j2. + * + * If it's present, we can assume we should be using the damn thing. + */ + static + { + boolean l4j = false; + try + { + Class cl = Class.forName("org.apache.logging.log4j.LogManager"); + if (cl == null) + { + throw new ClassNotFoundException("This should never happen..."); + } + l4j = true; + } + catch (ClassNotFoundException cnfe) + { + // Ignore + } + useLog4j2 = l4j; + } + + public static ILogger getLogger(String name) + { + if (useLog4j2) + { + return new Log4jLogger(name); + } + else + { + return new JULLogger(name); + } + } + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/package-info.java b/src/main/java/slimeknights/mantle/pulsar/package-info.java new file mode 100644 index 0000000..8c4fa42 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/package-info.java @@ -0,0 +1,36 @@ +/** + * Project Pulsar + * + * This is a repackaged copy of the Pulsar project, an open source library for internal module loading. + * The upstream repository is at https://github.com/Emberwalker/Pulsar + * + * This code package is licensed under the MIT License, as below. + * + * ----- + * + * The MIT License (MIT) + * + * Copyright (c) 2014 Arkan + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * ----- + * + * @author Arkan + */ +package mantle.pulsar; \ No newline at end of file diff --git a/src/main/java/slimeknights/mantle/pulsar/pulse/Pulse.java b/src/main/java/slimeknights/mantle/pulsar/pulse/Pulse.java new file mode 100644 index 0000000..ad9366c --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/pulse/Pulse.java @@ -0,0 +1,53 @@ +package mantle.pulsar.pulse; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Metadata annotation for IPulse implementations. + * + * Pulses should use the standard Google Event Bus @Subscribe annotation to catch FML events, which are forwarded from + * the current mod container (including Pre/Init/Post events). + * + * @author Arkan + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +@Documented +public @interface Pulse +{ + + /** + * @return This Pulses UID - Much like FML mods have mod IDs. + */ + public String id(); + + /** + * @return This Pulses description, human-readable for config files. Can be blank. + */ + public String description() default ""; + + /** + * @return Dependant mod IDs, seperated by ; as in FML. Skips checks when undefined. + */ + public String modsRequired() default ""; + + /** + * @return Dependeant Pulse IDs, seperated by ; . Skips checks when undefined. + */ + public String pulsesRequired() default ""; + + /** + * @return Whether this Pulse is mandatory or not (true -> mandatory). + */ + public boolean forced() default false; + + /** + * @return Whether a configurable Pulse should be enabled by default. Ignored where forced = true. + */ + public boolean defaultEnable() default true; + +} diff --git a/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java b/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java new file mode 100644 index 0000000..c1ef504 --- /dev/null +++ b/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java @@ -0,0 +1,53 @@ +package mantle.pulsar.pulse; + +/** + * Metadata wrapper for parsed @Pulse metadata. + * + * @author Arkan + */ +public class PulseMeta +{ + + private String id, description; + + private boolean forced, enabled, defaultEnabled; + + public PulseMeta(String id, String description, boolean forced, boolean enabled, boolean defaultEnabled) + { + this.id = id; + this.description = description; + this.forced = forced; + this.enabled = enabled; + this.defaultEnabled = defaultEnabled; + } + + public String getId() + { + return this.id; + } + + public String getDescription() + { + return this.description; + } + + public boolean isForced() + { + return this.forced; + } + + public boolean isEnabled() + { + return this.enabled; + } + + public void setEnabled(boolean enabled) + { + this.enabled = enabled; + } + + public boolean isDefaultEnabled() + { + return this.defaultEnabled; + } +}