mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-18 03:15:24 +00:00
update to latest pulsar closes #30
This commit is contained in:
1 parent
3b1b56b41a
commit
d7b6343353
14 files changed
+205
-213
No files matched your search
Regular → Executable
File mode changed.
Regular → Executable
+120
-130
@@ -1,35 +1,44 @@
|
||||
package mantle.pulsar.control;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.google.common.eventbus.SubscriberExceptionContext;
|
||||
import com.google.common.eventbus.SubscriberExceptionHandler;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.FMLModContainer;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||
import net.minecraftforge.fml.common.ModContainer;
|
||||
import net.minecraftforge.fml.common.event.FMLEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
|
||||
import mantle.pulsar.config.IConfiguration;
|
||||
import mantle.pulsar.internal.Configuration;
|
||||
import mantle.pulsar.internal.CrashHandler;
|
||||
import mantle.pulsar.pulse.PulseMeta;
|
||||
import mantle.pulsar.internal.logging.ILogger;
|
||||
import mantle.pulsar.internal.logging.LogManager;
|
||||
import mantle.pulsar.pulse.Handler;
|
||||
import mantle.pulsar.pulse.IPulse;
|
||||
import mantle.pulsar.pulse.Pulse;
|
||||
import mantle.pulsar.pulse.PulseProxy;
|
||||
|
||||
/**
|
||||
* 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 <[email protected]>
|
||||
*/
|
||||
@SuppressWarnings({"unused", "deprecated"})
|
||||
@SuppressWarnings("unused")
|
||||
public class PulseManager {
|
||||
|
||||
private final ILogger log;
|
||||
private ILogger log;
|
||||
private final boolean useConfig;
|
||||
|
||||
private final LinkedHashMap<Object, PulseMeta> pulses = new LinkedHashMap<Object, PulseMeta>();
|
||||
@@ -37,31 +46,18 @@ public class PulseManager {
|
||||
private boolean blockNewRegistrations = false;
|
||||
private boolean configLoaded = false;
|
||||
private IConfiguration conf;
|
||||
|
||||
/**
|
||||
* Configuration-less constructor.
|
||||
*
|
||||
* PulseManagers created this way ignore configurability on child Pulses. This is not recommended.
|
||||
*
|
||||
* @param modId The parents ModID.
|
||||
*/
|
||||
@Deprecated
|
||||
public PulseManager(String modId) {
|
||||
log = LogManager.getLogger("PulseManager-" + modId);
|
||||
useConfig = false;
|
||||
conf = null;
|
||||
}
|
||||
private EventBus bus;
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Configuration-using constructor.
|
||||
*
|
||||
* This form creates a PulseManager that supports configuration of Pulses by file. Recommended approach.
|
||||
* This form creates a PulseManager that supports configuration of Pulses by file.
|
||||
*
|
||||
* @param modId The parents ModID.
|
||||
* @param configName The config file name.
|
||||
*/
|
||||
public PulseManager(String modId, String configName) {
|
||||
log = LogManager.getLogger("PulseManager-" + modId);
|
||||
public PulseManager(String configName) {
|
||||
init();
|
||||
useConfig = true;
|
||||
conf = new Configuration(configName, log);
|
||||
}
|
||||
@@ -72,15 +68,27 @@ public class PulseManager {
|
||||
* 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 modId The parents ModID.
|
||||
* @param config Configuration handler.
|
||||
*/
|
||||
public PulseManager(String modId, IConfiguration config) {
|
||||
log = LogManager.getLogger("PulseManager-" + modId);
|
||||
public PulseManager(IConfiguration config) {
|
||||
init();
|
||||
useConfig = true;
|
||||
conf = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared initialiser code between all the constructors.
|
||||
*/
|
||||
private void init() {
|
||||
String modId = Loader.instance().activeModContainer().getModId();
|
||||
this.id = modId;
|
||||
log = LogManager.getLogger("Pulsar-" + modId);
|
||||
FMLCommonHandler.instance().registerCrashCallable(new CrashHandler(modId, this));
|
||||
// Attach us to the mods FML bus and setup our own bus
|
||||
bus = new EventBus(new BusExceptionHandler(modId));
|
||||
attachToContainerEventBus(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new Pulse with the manager.
|
||||
*
|
||||
@@ -97,7 +105,7 @@ public class PulseManager {
|
||||
}
|
||||
|
||||
String id, description, deps;
|
||||
boolean forced, enabled, missingDeps = false;
|
||||
boolean forced, enabled, defaultEnabled, missingDeps = false;
|
||||
|
||||
try {
|
||||
Pulse p = pulse.getClass().getAnnotation(Pulse.class);
|
||||
@@ -106,6 +114,7 @@ public class PulseManager {
|
||||
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);
|
||||
}
|
||||
@@ -125,120 +134,65 @@ public class PulseManager {
|
||||
}
|
||||
}
|
||||
|
||||
PulseMeta meta = new PulseMeta(id, description, forced, enabled);
|
||||
PulseMeta meta = new PulseMeta(id, description, forced, enabled, defaultEnabled);
|
||||
meta.setEnabled(!missingDeps && getEnabledFromConfig(meta));
|
||||
|
||||
if (meta.isEnabled()) {
|
||||
parseAndAddProxies(pulse);
|
||||
pulses.put(pulse, meta);
|
||||
// Attach Pulse to internal event bus
|
||||
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();
|
||||
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) preInit((FMLPreInitializationEvent) evt);
|
||||
bus.post(evt);
|
||||
}
|
||||
|
||||
private boolean getEnabledFromConfig(PulseMeta meta) {
|
||||
if (meta.isForced() || !useConfig) return true; // Forced or no config set.
|
||||
|
||||
return conf.isModuleEnabled(meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated FML handles proxies now.
|
||||
*
|
||||
* @param pulse Pulse to parse for proxy annotations.
|
||||
*/
|
||||
@Deprecated
|
||||
private void parseAndAddProxies(Object pulse) {
|
||||
try {
|
||||
for (Field f : pulse.getClass().getDeclaredFields()) {
|
||||
log.debug("Parsing field: " + f);
|
||||
PulseProxy p = f.getAnnotation(PulseProxy.class);
|
||||
if (p != null) { // Support for deprecated PulseProxy annotation
|
||||
log.warn("Pulse " + pulse + " used the deprecated PulseProxy annotation. As of Pulsar 0.1.0, it's now preferred to use FML's SidedProxy annotation.");
|
||||
log.warn("The old PulseProxy parsing will be removed in the next breaking update (Pulsar 1.x).");
|
||||
setProxyField(pulse, f, p.clientSide(), p.serverSide());
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Pulse annotation parsing failed for Pulse " + pulse + "; " + ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private void setProxyField(Object pulse, Field f, String client, String server) throws Exception {
|
||||
boolean accessible = f.isAccessible();
|
||||
f.setAccessible(true);
|
||||
switch (FMLCommonHandler.instance().getSide()) {
|
||||
case CLIENT:
|
||||
f.set(pulse, Class.forName(client).newInstance());
|
||||
break;
|
||||
default:
|
||||
f.set(pulse, Class.forName(server).newInstance());
|
||||
}
|
||||
f.setAccessible(accessible);
|
||||
}
|
||||
|
||||
public void preInit(FMLPreInitializationEvent evt) {
|
||||
if (!blockNewRegistrations) conf.flush(); // First preInit call, so flush config
|
||||
blockNewRegistrations = true;
|
||||
|
||||
for (Map.Entry<Object, PulseMeta> e : pulses.entrySet()) {
|
||||
if(hasRequiredPulses(e)) {
|
||||
log.debug("Preinitialising Pulse " + e.getValue().getId() + "...");
|
||||
if (e.getKey() instanceof IPulse) { // Deprecated IPulse handling
|
||||
IPulse ip = (IPulse)e.getKey();
|
||||
ip.preInit(evt);
|
||||
} else findAndInvokeHandlers(e.getKey(), evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void init(FMLInitializationEvent evt) {
|
||||
for (Map.Entry<Object, PulseMeta> e : pulses.entrySet()) {
|
||||
log.debug("Initialising Pulse " + e.getValue().getId() + "...");
|
||||
|
||||
if (e.getKey() instanceof IPulse) { // Deprecated IPulse handling
|
||||
IPulse ip = (IPulse)e.getKey();
|
||||
ip.init(evt);
|
||||
log.warn("Pulse " + e.getValue().getId() + " is using the deprecated IPulse interface.");
|
||||
log.warn("This will be removed in the next major version (Pulsar 1.x) - Please switch to @Handler!");
|
||||
} else findAndInvokeHandlers(e.getKey(), evt);
|
||||
}
|
||||
}
|
||||
|
||||
public void postInit(FMLPostInitializationEvent evt) {
|
||||
for (Map.Entry<Object, PulseMeta> e : pulses.entrySet()) {
|
||||
log.debug("Postinitialising Pulse " + e.getValue().getId() + "...");
|
||||
|
||||
if (e.getKey() instanceof IPulse) { // Deprecated IPulse handling
|
||||
IPulse ip = (IPulse)e.getKey();
|
||||
ip.postInit(evt);
|
||||
} else findAndInvokeHandlers(e.getKey(), evt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an object for a matching handler for the given object.
|
||||
*
|
||||
* @param pulse Object to inspect for Handlers
|
||||
* @param evt The event object
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void findAndInvokeHandlers(Object pulse, Object evt) {
|
||||
for (Method m : pulse.getClass().getDeclaredMethods()) {
|
||||
try {
|
||||
if (m.getAnnotation(Handler.class) == null) continue; // Ignore non-@Handler methods
|
||||
|
||||
Class[] pTypes = m.getParameterTypes();
|
||||
if (pTypes.length != 1) continue;
|
||||
|
||||
Class pt = pTypes[0];
|
||||
if (pt.isAssignableFrom(evt.getClass())) {
|
||||
m.invoke(pulse, evt);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.warn("Caught exception in findAndInvokeHandlers: " + ex);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasRequiredPulses(Map.Entry<Object, PulseMeta> entry) {
|
||||
@@ -255,12 +209,48 @@ public class PulseManager {
|
||||
return 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<Object, PulseMeta> entry : pulses.entrySet()) {
|
||||
if(entry.getValue().getId().equals(pulseId)) {
|
||||
if (entry.getValue().getId().equals(pulseId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Collection<PulseMeta> getAllPulseMetadata() {
|
||||
return 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/" + id + " >> Exception uncaught in ["
|
||||
+ ctx.getSubscriber().getClass().getName() + ":" + ctx.getSubscriberMethod().getName()
|
||||
+ "] for event [" + ctx.getEvent().getClass().getSimpleName() + "]", true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PulseManager[" + id + "]";
|
||||
}
|
||||
}
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
package mantle.pulsar.debug;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import mantle.pulsar.internal.logging.ILogger;
|
||||
import mantle.pulsar.internal.logging.LogManager;
|
||||
import mantle.pulsar.pulse.Pulse;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
|
||||
/**
|
||||
* Debug Pulse to 'eavesdrop' on the PulseManager event bus traffic.
|
||||
*
|
||||
* @author Arkan <[email protected]>
|
||||
*/
|
||||
@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) {
|
||||
log.info("Received event: " + evt);
|
||||
}
|
||||
|
||||
}
|
||||
Regular → Executable
+1
-1
@@ -50,7 +50,7 @@ public class Configuration implements IConfiguration {
|
||||
public boolean isModuleEnabled(PulseMeta meta) {
|
||||
ConfigEntry entry = modules.get(meta.getId());
|
||||
if (entry == null) {
|
||||
modules.put(meta.getId(), new ConfigEntry(meta.isEnabled(), meta.getDescription()));
|
||||
modules.put(meta.getId(), new ConfigEntry(meta.isDefaultEnabled(), meta.getDescription()));
|
||||
return meta.isEnabled();
|
||||
} else {
|
||||
return entry.getEnabled();
|
||||
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
package mantle.pulsar.internal;
|
||||
|
||||
import net.minecraftforge.fml.common.ICrashCallable;
|
||||
import mantle.pulsar.control.PulseManager;
|
||||
import mantle.pulsar.pulse.PulseMeta;
|
||||
|
||||
/**
|
||||
* FML crash callable for Pulse Managers; dumps a list of loaded pulses to the error log.
|
||||
*
|
||||
* @author Arkan <[email protected]>
|
||||
*/
|
||||
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 id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
String out = "\n";
|
||||
for (PulseMeta meta : 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
Regular → Executable
File mode changed.
@@ -1,14 +0,0 @@
|
||||
package mantle.pulsar.pulse;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Annotates methods to be used for event handling.
|
||||
*
|
||||
* @author Arkan <[email protected]>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
public @interface Handler
|
||||
{}
|
||||
@@ -1,38 +0,0 @@
|
||||
package mantle.pulsar.pulse;
|
||||
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
|
||||
/**
|
||||
* Base interface for all Pulsar modules ("pulses").
|
||||
*
|
||||
* @deprecated Use the new {@link Handler} annotation.
|
||||
*
|
||||
* @author Arkan <[email protected]>
|
||||
*/
|
||||
@Deprecated
|
||||
public interface IPulse {
|
||||
|
||||
/**
|
||||
* FML preinit from the parent mod.
|
||||
*
|
||||
* @param evt The FML event inherited from this Pulses parent.
|
||||
*/
|
||||
public void preInit(FMLPreInitializationEvent evt);
|
||||
|
||||
/**
|
||||
* FML init from the parent mod.
|
||||
*
|
||||
* @param evt The FML event inherited from this Pulses parent.
|
||||
*/
|
||||
public void init(FMLInitializationEvent evt);
|
||||
|
||||
/**
|
||||
* FML postinit from the parent mod.
|
||||
*
|
||||
* @param evt The FML event inherited from this Pulses parent.
|
||||
*/
|
||||
public void postInit(FMLPostInitializationEvent evt);
|
||||
|
||||
}
|
||||
Regular → Executable
+3
@@ -5,6 +5,9 @@ import java.lang.annotation.*;
|
||||
/**
|
||||
* 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 <[email protected]>
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
Regular → Executable
+7
-2
@@ -8,13 +8,14 @@ package mantle.pulsar.pulse;
|
||||
public class PulseMeta {
|
||||
|
||||
private String id, description;
|
||||
private boolean forced, enabled;
|
||||
private boolean forced, enabled, defaultEnabled;
|
||||
|
||||
public PulseMeta(String id, String description, boolean forced, boolean enabled) {
|
||||
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() {
|
||||
@@ -36,4 +37,8 @@ public class PulseMeta {
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public boolean isDefaultEnabled() {
|
||||
return defaultEnabled;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package mantle.pulsar.pulse;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Annotation to denote a field that should be populated by a side-specific proxy.
|
||||
*
|
||||
* @deprecated Use FML's @SidedProxy instead.
|
||||
*
|
||||
* @author Arkan <[email protected]>
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Documented
|
||||
@Deprecated
|
||||
public @interface PulseProxy {
|
||||
|
||||
/**
|
||||
* @return The FQCN of the client proxy.
|
||||
*/
|
||||
public String clientSide();
|
||||
|
||||
/**
|
||||
* @return The FQCN of the server proxy.
|
||||
*/
|
||||
public String serverSide();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user